Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Optimize ]optimize exception throw #3116

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.dinky.assertion;

import org.dinky.data.dto.TaskDTO;
import org.dinky.data.enums.Status;
import org.dinky.data.exception.BusException;
import org.dinky.data.model.ClusterInstance;
import org.dinky.data.model.Task;
Expand All @@ -28,17 +30,35 @@
*
* @since 2021/5/30 11:13
*/
public interface Assert {
public interface DinkyAssert {

static void check(ClusterInstance clusterInstance) {
if (clusterInstance.getId() == null) {
throw new BusException("Flink 集群【" + clusterInstance.getId() + "】不存在");
}
}

static void check(TaskDTO task) {
if (task == null) {
throw new BusException(Status.TASK_NOT_EXIST);
}
}

static void check(Task task) {
if (task == null) {
throw new BusException("作业不存在");
throw new BusException(Status.TASK_NOT_EXIST);
}
}

static void checkNull(Object o, Status status) {
if (o == null) {
throw new BusException(status);
}
}

static void checkNull(Object o, String msg) {
if (o == null) {
throw new BusException(msg);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.dinky.service.impl;

import org.dinky.assertion.DinkyAssert;
import org.dinky.data.dto.ClusterConfigurationDTO;
import org.dinky.data.enums.Status;
import org.dinky.data.exception.BusException;
Expand All @@ -45,8 +46,6 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.lang.Assert;

/**
* ClusterConfigServiceImpl
*
Expand Down Expand Up @@ -76,7 +75,7 @@ public List<ClusterConfiguration> listEnabledAllClusterConfig() {
@Override
public FlinkClusterConfig getFlinkClusterCfg(Integer id) {
ClusterConfiguration cfg = this.getClusterConfigById(id);
Assert.notNull(cfg, "The clusterConfiguration not exists!");
DinkyAssert.checkNull(cfg, "The clusterConfiguration not exists!");
return FlinkClusterConfig.create(cfg.getType(), cfg.getConfigJson());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

package org.dinky.service.impl;

import org.dinky.assertion.Assert;
import org.dinky.assertion.Asserts;
import org.dinky.assertion.DinkyAssert;
import org.dinky.cluster.FlinkCluster;
import org.dinky.cluster.FlinkClusterInfo;
import org.dinky.data.dto.ClusterInstanceDTO;
Expand Down Expand Up @@ -84,14 +84,14 @@ public FlinkClusterInfo checkHeartBeat(String hosts, String host) {
@Override
public String getJobManagerAddress(ClusterInstance clusterInstance) {
// TODO 这里判空逻辑有问题,clusterInstance有可能为null
Assert.check(clusterInstance);
DinkyAssert.check(clusterInstance);
FlinkClusterInfo info =
FlinkCluster.testFlinkJobManagerIP(clusterInstance.getHosts(), clusterInstance.getJobManagerHost());
String host = null;
if (info.isEffective()) {
host = info.getJobManagerAddress();
}
Assert.checkHost(host);
DinkyAssert.checkHost(host);
if (!host.equals(clusterInstance.getJobManagerHost())) {
clusterInstance.setJobManagerHost(host);
updateById(clusterInstance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
import org.dinky.data.dto.LoginDTO;
import org.dinky.data.enums.Status;
import org.dinky.data.exception.AuthException;
import org.dinky.data.exception.BusException;
import org.dinky.data.model.LdapUserIdentification;
import org.dinky.data.model.SystemConfiguration;
import org.dinky.data.model.rbac.User;
import org.dinky.service.LdapService;

import org.apache.http.util.TextUtils;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -39,7 +42,6 @@
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.stereotype.Service;

import cn.hutool.core.lang.Assert;
import lombok.extern.slf4j.Slf4j;

@Service
Expand Down Expand Up @@ -112,7 +114,9 @@ public User authenticate(LoginDTO loginDTO) throws AuthException {
@Override
public List<User> listUsers() {
String filter = configuration.getLdapFilter().getValue();
Assert.notBlank(filter, Status.LDAP_FILTER_INCORRECT.getMessage());
if (TextUtils.isEmpty(filter)) {
throw new BusException(Status.LDAP_FILTER_INCORRECT);
}

LdapTemplate ldapTemplate = new LdapTemplate(LdapContext.getLdapContext());
List<User> result = ldapTemplate.search(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.dinky.service.impl;

import org.dinky.assertion.Asserts;
import org.dinky.assertion.DinkyAssert;
import org.dinky.config.Dialect;
import org.dinky.context.TenantContextHolder;
import org.dinky.data.annotations.ProcessStep;
Expand Down Expand Up @@ -174,7 +175,7 @@ public TaskDTO prepareTask(TaskSubmitDto submitDto) {

log.info("Start check and config task, task:{}", task.getName());

Assert.notNull(task, Status.TASK_NOT_EXIST.getMessage());
DinkyAssert.check(task);

if (StringUtils.isNotBlank(submitDto.getSavePointPath())) {
task.setSavePointStrategy(SavePointStrategy.CUSTOM.getValue());
Expand Down Expand Up @@ -359,10 +360,10 @@ public JobResult restartTask(Integer id, String savePointPath) throws Exception
TaskDTO task = this.getTaskInfoById(id);
boolean useSavepoint = !TextUtils.isEmpty(savePointPath);

Asserts.checkNull(task, Status.TASK_NOT_EXIST.getMessage());
DinkyAssert.check(task);
if (!Dialect.isCommonSql(task.getDialect()) && Asserts.isNotNull(task.getJobInstanceId())) {
JobInstance jobInstance = jobInstanceService.getById(task.getJobInstanceId());
Assert.notNull(jobInstance, Status.JOB_INSTANCE_NOT_EXIST.getMessage());
DinkyAssert.checkNull(jobInstance, Status.JOB_INSTANCE_NOT_EXIST);
String status = jobInstance.getStatus();
if (!JobStatus.isDone(status)) {
log.info("JobInstance [{}] status is [{}], stop it now", jobInstance.getName(), status);
Expand Down Expand Up @@ -409,9 +410,9 @@ public boolean cancelTaskJob(TaskDTO task, boolean withSavePoint, boolean forceC
return true;
}
JobInstance jobInstance = jobInstanceService.getById(task.getJobInstanceId());
Assert.notNull(jobInstance, Status.JOB_INSTANCE_NOT_EXIST.getMessage());
DinkyAssert.checkNull(jobInstance, Status.JOB_INSTANCE_NOT_EXIST.getMessage());
ClusterInstance clusterInstance = clusterInstanceService.getById(jobInstance.getClusterId());
Assert.notNull(clusterInstance, Status.CLUSTER_NOT_EXIST.getMessage());
DinkyAssert.checkNull(clusterInstance, Status.CLUSTER_NOT_EXIST.getMessage());

JobManager jobManager;
try {
Expand Down Expand Up @@ -446,7 +447,7 @@ public boolean cancelTaskJob(TaskDTO task, boolean withSavePoint, boolean forceC
@Override
public SavePointResult savepointTaskJob(TaskDTO task, SavePointType savePointType) {
JobInstance jobInstance = jobInstanceService.getById(task.getJobInstanceId());
Assert.notNull(jobInstance, Status.JOB_INSTANCE_NOT_EXIST.getMessage());
DinkyAssert.checkNull(jobInstance, Status.JOB_INSTANCE_NOT_EXIST.getMessage());

JobManager jobManager = JobManager.build(buildJobConfig(task));
String jobId = jobInstance.getJid();
Expand Down Expand Up @@ -490,7 +491,7 @@ public ObjectNode getStreamGraph(TaskDTO taskDTO) {
@Override
public String exportSql(Integer id) {
TaskDTO task = this.getTaskInfoById(id);
Asserts.checkNull(task, Status.TASK_NOT_EXIST.getMessage());
DinkyAssert.check(task);
if (Dialect.isCommonSql(task.getDialect())) {
return task.getStatement();
}
Expand All @@ -514,7 +515,7 @@ public String exportSql(Integer id) {
@Override
public TaskDTO getTaskInfoById(Integer id) {
Task mTask = this.getById(id);
Assert.notNull(mTask, Status.TASK_NOT_EXIST.getMessage());
DinkyAssert.check(mTask);
TaskDTO taskDTO = new TaskDTO();
BeanUtil.copyProperties(mTask, taskDTO);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.dinky.service.resource.impl;

import org.dinky.assertion.DinkyAssert;
import org.dinky.data.dto.TreeNodeDTO;
import org.dinky.data.enums.Status;
import org.dinky.data.exception.BusException;
Expand Down Expand Up @@ -137,7 +138,7 @@ public TreeNodeDTO createFolderOrGet(Integer pid, String fileName, String desc)
public void rename(Integer id, String fileName, String desc) {
Resources byId = getById(id);
String sourceFullName = byId.getFullName();
Assert.notNull(byId, () -> new BusException("resource is not exists!"));
DinkyAssert.checkNull(byId, Status.RESOURCE_DIR_OR_FILE_NOT_EXIST);
long count = count(new LambdaQueryWrapper<Resources>()
.eq(Resources::getPid, byId.getPid())
.eq(Resources::getFileName, fileName)
Expand Down Expand Up @@ -216,15 +217,15 @@ private static TreeNodeDTO convertTree(Resources resources) {
@Override
public String getContentByResourceId(Integer id) {
Resources resources = getById(id);
Assert.notNull(resources, () -> new BusException(Status.RESOURCE_DIR_OR_FILE_NOT_EXIST));
DinkyAssert.checkNull(resources, Status.RESOURCE_DIR_OR_FILE_NOT_EXIST);
Assert.isFalse(resources.getSize() > ALLOW_MAX_CAT_CONTENT_SIZE, () -> new BusException("file is too large!"));
return getBaseResourceManager().getFileContent(resources.getFullName());
}

@Override
public File getFile(Integer id) {
Resources resources = getById(id);
Assert.notNull(resources, () -> new BusException(Status.RESOURCE_DIR_OR_FILE_NOT_EXIST));
DinkyAssert.checkNull(resources, Status.RESOURCE_DIR_OR_FILE_NOT_EXIST);
Assert.isFalse(resources.getSize() > ALLOW_MAX_CAT_CONTENT_SIZE, () -> new BusException("file is too large!"));
return URLUtils.toFile("rs://" + resources.getFullName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public static Configuration.OptionBuilder key(Status status) {

private final Configuration<String> ldapFilter = key(Status.SYS_LDAP_SETTINGS_FILTER)
.stringType()
.defaultValue("")
.defaultValue("(&(objectClass=inetOrgPerson))")
.note(Status.SYS_LDAP_SETTINGS_FILTER_NOTE);

private final Configuration<Boolean> ldapAutoload = key(Status.SYS_LDAP_SETTINGS_AUTOLOAD)
Expand Down
1 change: 1 addition & 0 deletions dinky-web/src/models/Sse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default () => {
.catch((e) => ErrorMessage(e));
};
const reconnectSse = () => {
uuidRef.current = uuidv4();
const sseUrl = '/api/sse/connect?sessionKey=' + uuidRef.current;
eventSource?.close();
setEventSource(new EventSource(sseUrl));
Expand Down
Loading