Skip to content

Commit

Permalink
rename enum equals method to equalsValue
Browse files Browse the repository at this point in the history
  • Loading branch information
ponfee committed Feb 19, 2024
1 parent 6f78f55 commit 54638eb
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public AjaxResult resume(@PathVariable("instanceId") Long instanceId) {
openapiService.resumeInstance(instanceId);
Threads.waitUntil(WAIT_SLEEP_ROUND, new long[]{500, 200}, () -> {
SchedInstanceResponse instance = openapiService.getInstance(instanceId, false);
return !RunState.PAUSED.equals(instance.getRunState());
return !RunState.PAUSED.equalsValue(instance.getRunState());
});
return success();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,26 @@ public interface IntValueEnum<T extends Enum<T> & IntValueEnum<T>> {
*/
String desc();

default boolean equals(Integer value) {
/**
* Returns IntValueEnum instance is equals Integer value
*
* @param value the Integer value
* @return {@code true} if equals
*/
default boolean equalsValue(Integer value) {
return value != null && value == value();
}

/**
* Returns IntValueEnum instance is equals int value
*
* @param value the int value
* @return {@code true} if equals
*/
default boolean equalsValue(int value) {
return value == value();
}

static <T extends Enum<T> & IntValueEnum<T>> T of(Class<T> type, Integer value) {
if (type == null) {
throw new IllegalArgumentException("Enum int type cannot be null: " + type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public boolean retryable(RunState runState, int retriedCount) {
if (!runState.isFailure()) {
return false;
}
return !RetryType.NONE.equals(retryType) && retriedCount < retryCount;
return !RetryType.NONE.equalsValue(retryType) && retriedCount < retryCount;
}

/**
Expand All @@ -283,7 +283,7 @@ public boolean retryable(RunState runState, int retriedCount) {
* @return retry trigger time milliseconds
*/
public long computeRetryTriggerTime(int failCount, Date current) {
Assert.isTrue(!RetryType.NONE.equals(retryType), () -> "Sched job '" + jobId + "' retry type is NONE.");
Assert.isTrue(!RetryType.NONE.equalsValue(retryType), () -> "Sched job '" + jobId + "' retry type is NONE.");
Assert.isTrue(retryCount > 0, () -> "Sched job '" + jobId + "' retry count must greater than 0, but actual " + retryCount);
Assert.isTrue(failCount <= retryCount, () -> "Sched job '" + jobId + "' retried " + failCount + " exceed " + retryCount + " limit.");
// exponential backoff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void updateJob(SchedJob job) throws JobException {
public void deleteJob(long jobId) {
SchedJob job = jobMapper.get(jobId);
Assert.notNull(job, "Job id not found: " + jobId);
if (JobState.ENABLE.equals(job.getJobState())) {
if (JobState.ENABLE.equalsValue(job.getJobState())) {
throw new IllegalStateException("Please disable job before delete this job.");
}
assertOneAffectedRow(jobMapper.softDelete(jobId), "Delete sched job fail or conflict.");
Expand All @@ -162,7 +162,7 @@ public void deleteJob(long jobId) {

protected boolean updateFixedDelayNextTriggerTime(SchedJob job, Date baseTime) {
TriggerType fixedDelay = TriggerType.FIXED_DELAY;
if (!fixedDelay.equals(job.getTriggerType())) {
if (!fixedDelay.equalsValue(job.getTriggerType())) {
return false;
}
Date date = baseTime == null ? null : fixedDelay.computeNextTriggerTime(job.getTriggerValue(), baseTime);
Expand Down Expand Up @@ -200,7 +200,7 @@ public boolean hasAliveExecuting(List<SchedTask> tasks) {
return false;
}
return tasks.stream()
.filter(e -> ExecuteState.EXECUTING.equals(e.getExecuteState()))
.filter(e -> ExecuteState.EXECUTING.equalsValue(e.getExecuteState()))
.map(SchedTask::getWorker)
.anyMatch(this::isAliveWorker);
}
Expand All @@ -227,7 +227,7 @@ public boolean hasNotDiscoveredWorkers() {
}

public boolean isNeedRedispatch(SchedTask task) {
if (!ExecuteState.WAITING.equals(task.getExecuteState())) {
if (!ExecuteState.WAITING.equalsValue(task.getExecuteState())) {
return false;
}
if (StringUtils.isBlank(task.getWorker())) {
Expand Down Expand Up @@ -256,7 +256,7 @@ public boolean dispatch(SchedJob job, SchedInstance instance, List<SchedTask> ta
String supervisorToken = SchedGroupService.createSupervisorAuthenticationToken(job.getGroup());
ExecuteTaskParam.Builder builder = ExecuteTaskParam.builder(instance, job, supervisorToken);
List<ExecuteTaskParam> list;
if (RouteStrategy.BROADCAST.equals(job.getRouteStrategy())) {
if (RouteStrategy.BROADCAST.equalsValue(job.getRouteStrategy())) {
list = new ArrayList<>(tasks.size());
for (SchedTask task : tasks) {
Assert.hasText(task.getWorker(), () -> "Broadcast route strategy worker must pre assign: " + task.getTaskId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public boolean startTask(StartTaskParam param) {
Date now = new Date();
// start sched instance(also possibly started by other task)
int row = 0;
if (RunState.WAITING.equals(instance.getRunState())) {
if (RunState.WAITING.equalsValue(instance.getRunState())) {
row = instanceMapper.start(param.getInstanceId(), now);
}

Expand Down Expand Up @@ -373,7 +373,7 @@ public boolean purgeInstance(SchedInstance inst) {

// task execute state must not 10
List<SchedTask> tasks = taskMapper.findBaseByInstanceId(instanceId);
if (tasks.stream().anyMatch(e -> ExecuteState.WAITING.equals(e.getExecuteState()))) {
if (tasks.stream().anyMatch(e -> ExecuteState.WAITING.equalsValue(e.getExecuteState()))) {
LOG.warn("Purge instance failed, has waiting task: {}", tasks);
return false;
}
Expand All @@ -398,7 +398,7 @@ public boolean purgeInstance(SchedInstance inst) {
tasks.stream()
.filter(e -> EXECUTE_STATE_PAUSABLE.contains(e.getExecuteState()))
.forEach(e -> {
String worker = ExecuteState.EXECUTING.equals(e.getExecuteState()) ? Strings.requireNonBlank(e.getWorker()) : null;
String worker = ExecuteState.EXECUTING.equalsValue(e.getExecuteState()) ? Strings.requireNonBlank(e.getWorker()) : null;
taskMapper.terminate(e.getTaskId(), worker, ExecuteState.EXECUTE_TIMEOUT.value(), e.getExecuteState(), new Date(), null);
});

Expand Down Expand Up @@ -478,7 +478,7 @@ public boolean resumeInstance(long instanceId) {
Long wnstanceId = instanceMapper.getWnstanceId(instanceId);
return doTransactionLockInSynchronized(instanceId, wnstanceId, instance -> {
Assert.notNull(instance, () -> "Cancel failed, instance_id not found: " + instanceId);
if (!RunState.PAUSED.equals(instance.getRunState())) {
if (!RunState.PAUSED.equalsValue(instance.getRunState())) {
return false;
}

Expand All @@ -489,7 +489,7 @@ public boolean resumeInstance(long instanceId) {
assertOneAffectedRow(row, () -> "Resume workflow lead instance failed: " + instanceId);
workflowMapper.resumeWaiting(instanceId);
for (SchedInstance nodeInstance : instanceMapper.findWorkflowNode(instanceId)) {
if (RunState.PAUSED.equals(nodeInstance.getRunState())) {
if (RunState.PAUSED.equalsValue(nodeInstance.getRunState())) {
resumeInstance(nodeInstance);
updateWorkflowEdgeState(nodeInstance, RunState.RUNNING.value(), RUN_STATE_PAUSED);
}
Expand Down Expand Up @@ -538,7 +538,7 @@ private boolean doTransactionLockInSynchronized(long instanceId, Long wnstanceId

private boolean shouldTerminateDispatchFailedTask(long taskId) {
SchedTask task = taskMapper.get(taskId);
if (!ExecuteState.WAITING.equals(task.getExecuteState())) {
if (!ExecuteState.WAITING.equalsValue(task.getExecuteState())) {
return false;
}
int currentDispatchFailedCount = task.getDispatchFailedCount();
Expand Down Expand Up @@ -678,7 +678,7 @@ private void updateWorkflowLeadState(SchedInstance instance) {
RunState state = graph.anyMatch(e -> e.getValue().isFailure()) ? RunState.CANCELED : RunState.FINISHED;
int row = instanceMapper.terminate(instance.getWnstanceId(), state.value(), RUN_STATE_TERMINABLE, new Date());
assertOneAffectedRow(row, () -> "Update workflow lead instance state failed: " + instance + ", " + state);
} else if (workflows.stream().noneMatch(e -> RunState.RUNNING.equals(e.getRunState()))) {
} else if (workflows.stream().noneMatch(e -> RunState.RUNNING.equalsValue(e.getRunState()))) {
RunState state = RunState.PAUSED;
int row = instanceMapper.updateState(instance.getWnstanceId(), state.value(), instance.getRunState());
assertOneAffectedRow(row, () -> "Update workflow lead instance state failed: " + instance + ", " + state);
Expand Down Expand Up @@ -714,7 +714,7 @@ private void createWorkflowNode(SchedInstance leadInstance, WorkflowGraph graph,
for (Map.Entry<DAGEdge, SchedWorkflow> entry : map.entrySet()) {
DAGNode target = entry.getKey().getTarget();
SchedWorkflow workflow = entry.getValue();
if (target.isEnd() || !RunState.WAITING.equals(workflow.getRunState()) || !duplicates.add(target)) {
if (target.isEnd() || !RunState.WAITING.equalsValue(workflow.getRunState()) || !duplicates.add(target)) {
// 当前节点为结束结点 或 当前节点不为等待状态,则跳过
continue;
}
Expand Down Expand Up @@ -787,7 +787,7 @@ private void updateFixedDelayNextTriggerTime(SchedInstance curr, LazyLoader<Sche
// 2、必须是SCHEDULE方式运行的实例:不会存在`A -> ... -> A`问题,因为在添加Job时做了不能循环依赖的校验
long rnstanceId = curr.obtainRnstanceId();
SchedInstance root = (rnstanceId == curr.getInstanceId()) ? curr : instanceMapper.get(rnstanceId);
if (!root.getJobId().equals(curr.getJobId()) || !RunType.SCHEDULE.equals(root.getRunType())) {
if (!root.getJobId().equals(curr.getJobId()) || !RunType.SCHEDULE.equalsValue(root.getRunType())) {
return;
}

Expand Down Expand Up @@ -900,7 +900,7 @@ private void retryJob(SchedInstance prev, LazyLoader<SchedJob> lazyJob) {
.stream()
.filter(e -> ExecuteState.of(e.getExecuteState()).isFailure())
// broadcast task cannot support partial retry
.filter(e -> !RouteStrategy.BROADCAST.equals(schedJob.getRouteStrategy()) || super.isAliveWorker(e.getWorker()))
.filter(e -> !RouteStrategy.BROADCAST.equalsValue(schedJob.getRouteStrategy()) || super.isAliveWorker(e.getWorker()))
.map(e -> SchedTask.create(e.getTaskParam(), generateId(), retryInstanceId, e.getTaskNo(), e.getTaskCount(), now, e.getWorker()))
.collect(Collectors.toList());
} else {
Expand Down Expand Up @@ -933,7 +933,7 @@ private void dependJob(SchedInstance parentInstance) {
LOG.error("Child sched job not found: {}, {}", depend.getParentJobId(), depend.getChildJobId());
continue;
}
if (JobState.DISABLE.equals(childJob.getJobState())) {
if (JobState.DISABLE.equalsValue(childJob.getJobState())) {
continue;
}

Expand Down Expand Up @@ -971,7 +971,7 @@ private List<ExecuteTaskParam> loadExecutingTasks(SchedInstance instance, Operat
// immediate trigger
long triggerTime = 0L;
for (SchedTask task : taskMapper.findBaseByInstanceId(instance.getInstanceId())) {
if (!ExecuteState.EXECUTING.equals(task.getExecuteState())) {
if (!ExecuteState.EXECUTING.equalsValue(task.getExecuteState())) {
continue;
}
Worker worker = Worker.deserialize(task.getWorker());
Expand All @@ -998,7 +998,7 @@ private Tuple3<SchedJob, SchedInstance, List<SchedTask>> buildDispatchParam(long
Assert.notNull(job, "Not found job: " + instance.getJobId());
List<SchedTask> waitingTasks = taskMapper.findLargeByInstanceId(instanceId)
.stream()
.filter(e -> ExecuteState.WAITING.equals(e.getExecuteState()))
.filter(e -> ExecuteState.WAITING.equalsValue(e.getExecuteState()))
.collect(Collectors.toList());
if (waitingTasks.size() != expectTaskSize) {
throw new IllegalStateException("Invalid dispatching tasks size: expect=" + expectTaskSize + ", actual=" + waitingTasks.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private void processEach(SchedInstance instance, Date now) {
}

List<SchedTask> tasks = jobQuerier.findBaseInstanceTasks(instance.getInstanceId());
List<SchedTask> waitingTasks = Collects.filter(tasks, e -> ExecuteState.WAITING.equals(e.getExecuteState()));
List<SchedTask> waitingTasks = Collects.filter(tasks, e -> ExecuteState.WAITING.equalsValue(e.getExecuteState()));

if (CollectionUtils.isNotEmpty(waitingTasks)) {
// 1、has waiting state task
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private void processJob(SchedJob job, Date now, long maxNextTriggerTime) {
* @return accurate next trigger time milliseconds
*/
private Long reComputeNextTriggerTime(SchedJob job, Date now) {
if (TriggerType.FIXED_DELAY.equals(job.getTriggerType())) {
if (TriggerType.FIXED_DELAY.equalsValue(job.getTriggerType())) {
// 固定延时类型不重新计算nextTriggerTime
return job.obtainNextTriggerTime();
}
Expand All @@ -201,7 +201,7 @@ private Long reComputeNextTriggerTime(SchedJob job, Date now) {
* @return newly next trigger time milliseconds
*/
private static Long doComputeNextTriggerTime(SchedJob job, Date now) {
if (TriggerType.FIXED_DELAY.equals(job.getTriggerType())) {
if (TriggerType.FIXED_DELAY.equalsValue(job.getTriggerType())) {
// 固定延时类型的nextTriggerTime:先更新为long最大值,当任务实例运行完成时去主动计算并更新
// null值已被用作表示没有下次触发时间
return Long.MAX_VALUE;
Expand Down Expand Up @@ -259,7 +259,7 @@ private boolean checkBlockCollidedTrigger(SchedJob job, Date now) {
}

private boolean checkBlockCollidedTrigger(SchedJob job, List<SchedInstance> instances, CollidedStrategy collidedStrategy, Date now) {
if (TriggerType.FIXED_DELAY.equals(job.getTriggerType())) {
if (TriggerType.FIXED_DELAY.equalsValue(job.getTriggerType())) {
SchedInstance first = instances.get(0);
log.error("Fixed delay trigger type cannot happen run collided: {}, {}", first.obtainRnstanceId(), job.getNextTriggerTime());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private void processEach(SchedInstance instance) {
}

List<SchedTask> tasks = jobQuerier.findBaseInstanceTasks(instance.getInstanceId());
List<SchedTask> waitingTasks = Collects.filter(tasks, e -> ExecuteState.WAITING.equals(e.getExecuteState()));
List<SchedTask> waitingTasks = Collects.filter(tasks, e -> ExecuteState.WAITING.equalsValue(e.getExecuteState()));

if (CollectionUtils.isNotEmpty(waitingTasks)) {
// 1、has waiting state task
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public List<SplitTask> split(String jobParamString) {
public ExecuteResult execute(ExecutingTask executingTask, Savepoint savepoint) throws Exception {
long sum = executingTask.getWorkflowPredecessorNodes()
.stream()
.peek(e -> Assert.state(RunState.FINISHED.equals(e.getRunState()), "Previous instance unfinished: " + e.getInstanceId()))
.peek(e -> Assert.state(RunState.FINISHED == e.getRunState(), "Previous instance unfinished: " + e.getInstanceId()))
.flatMap(e -> e.getExecutedTasks().stream())
.map(AbstractExecutionTask::getExecuteSnapshot)
.map(e -> Jsons.fromJson(e, PrimeCountJobHandler.ExecuteSnapshot.class))
Expand Down

0 comments on commit 54638eb

Please sign in to comment.