Skip to content

Commit

Permalink
refactor job splitter
Browse files Browse the repository at this point in the history
  • Loading branch information
ponfee committed Sep 20, 2024
1 parent 3e59578 commit d93bc8c
Show file tree
Hide file tree
Showing 44 changed files with 327 additions and 357 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public AjaxResult pause(@PathVariable("instanceId") Long instanceId) {
openapiService.pauseInstance(instanceId);
Threads.waitUntil(WAIT_SLEEP_ROUND, WAIT_SLEEP_MILLIS, () -> {
SchedInstanceResponse instance = openapiService.getInstance(instanceId, false);
return !RunState.Const.PAUSABLE_LIST.contains(RunState.of(instance.getRunState()));
return !RunState.of(instance.getRunState()).isPausable();
});
return success();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ default boolean equalsValue(int value) {
return value == value();
}

static <T extends Enum<T> & IntValueEnum<T>> T of(Class<T> type, int value) {
for (T e : type.getEnumConstants()) {
if (e.value() == value) {
return e;
}
}
throw new IllegalArgumentException("Invalid enum " + type + " int value: " + value);
}

static List<IntValueDesc> values(Class<? extends IntValueEnum<?>> clazz) {
return Arrays.stream(clazz.getEnumConstants())
.map(e -> IntValueDesc.of(e.value(), e.desc()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public interface ValueEnum<V, T extends Enum<T> & ValueEnum<V, T>> {
*/
String desc();

/**
* Return ValueEnum object of value
* <p>Enum.values()和EnumClass.getEnumConstants()都会clone数组,效率不高
*
* @param type the ValueEnum class
* @param value the value
* @param <V> value type
* @param <T> ValueEnum type
* @return ValueEnum object
*/
static <V, T extends Enum<T> & ValueEnum<V, T>> T of(Class<T> type, V value) {
for (T e : type.getEnumConstants()) {
if (value.equals(e.value())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.commons.lang3.EnumUtils;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

Expand All @@ -39,7 +38,7 @@ public class Enums {
* @return the immutable map of enum to map enums, never null
* @see EnumUtils#getEnumMap(Class)
*/
public static <E extends Enum<E>> Map<String, E> toMap(Class<E> enumType) {
public static <E extends Enum<E>> ImmutableMap<String, E> toMap(Class<E> enumType) {
return toMap(enumType, Enum::name);
}

Expand All @@ -52,7 +51,7 @@ public static <E extends Enum<E>> Map<String, E> toMap(Class<E> enumType) {
* @param <E> the enum type
* @return the immutable map of enum to map enums, never null
*/
public static <K, E extends Enum<E>> Map<K, E> toMap(Class<E> enumType, Function<E, K> keyMapper) {
public static <K, E extends Enum<E>> ImmutableMap<K, E> toMap(Class<E> enumType, Function<E, K> keyMapper) {
return Arrays.stream(enumType.getEnumConstants())
.collect(ImmutableMap.toImmutableMap(keyMapper, Function.identity()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package cn.ponfee.disjob.core.dto.supervisor;

import cn.ponfee.disjob.common.base.ToJsonString;
import cn.ponfee.disjob.core.base.Worker;
import cn.ponfee.disjob.core.enums.JobType;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -44,14 +43,14 @@ public class StartTaskParam extends ToJsonString implements Serializable {
private JobType jobType;

public static StartTaskParam of(long jobId, Long wnstanceId, long instanceId, long taskId,
JobType jobType, Worker worker, String startRequestId) {
JobType jobType, String worker, String startRequestId) {
StartTaskParam param = new StartTaskParam();
param.setJobId(jobId);
param.setWnstanceId(wnstanceId);
param.setInstanceId(instanceId);
param.setTaskId(taskId);
param.setJobType(jobType);
param.setWorker(worker.serialize());
param.setWorker(worker);
param.setStartRequestId(startRequestId);

param.check();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,15 @@ public String desc() {
return desc;
}

public static CollidedStrategy of(Integer value) {
return IntValueEnum.of(CollidedStrategy.class, value);
public static CollidedStrategy of(int value) {
for (CollidedStrategy e : VALUES) {
if (e.value() == value) {
return e;
}
}
throw new IllegalArgumentException("Invalid collided strategy value: " + value);
}

private static final CollidedStrategy[] VALUES = CollidedStrategy.values();

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

import cn.ponfee.disjob.common.base.IntValueEnum;
import cn.ponfee.disjob.common.util.Enums;
import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Map;
import java.util.Objects;

Expand Down Expand Up @@ -127,6 +125,10 @@ public RunState runState() {
return runState;
}

public boolean isPausable() {
return this == WAITING || this == EXECUTING;
}

public boolean isTerminal() {
return runState.isTerminal();
}
Expand All @@ -136,28 +138,9 @@ public boolean isFailure() {
}

public static ExecuteState of(Integer value) {
return Objects.requireNonNull(Const.MAPPING.get(value), () -> "Invalid execute state value: " + value);
return Objects.requireNonNull(MAPPING.get(value), () -> "Invalid execute state value: " + value);
}

public static final class Const {
private static final Map<Integer, ExecuteState> MAPPING = Enums.toMap(ExecuteState.class, ExecuteState::value);

/**
* State list of can transit to PAUSED
*/
public static final List<ExecuteState> PAUSABLE_LIST = ImmutableList.of(WAITING, EXECUTING);

/**
* State list of can transit to EXECUTING
*/
public static final List<ExecuteState> EXECUTABLE_LIST = ImmutableList.of(WAITING, PAUSED);

/**
* State list of can transit to terminated
*
* @see #isTerminal()
*/
public static final List<ExecuteState> TERMINABLE_LIST = ImmutableList.of(WAITING, EXECUTING, PAUSED);
}
private static final Map<Integer, ExecuteState> MAPPING = Enums.toMap(ExecuteState.class, ExecuteState::value);

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,15 @@ public String desc() {
return desc;
}

public static JobState of(Integer value) {
return IntValueEnum.of(JobState.class, value);
public static JobState of(int value) {
for (JobState e : VALUES) {
if (e.value() == value) {
return e;
}
}
throw new IllegalArgumentException("Invalid job state value: " + value);
}

private static final JobState[] VALUES = JobState.values();

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,15 @@ public String desc() {
return desc;
}

public static JobType of(Integer value) {
return IntValueEnum.of(JobType.class, value);
public static JobType of(int value) {
for (JobType e : VALUES) {
if (e.value() == value) {
return e;
}
}
throw new IllegalArgumentException("Invalid job type value: " + value);
}

private static final JobType[] VALUES = JobType.values();

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,15 @@ public String desc() {
return desc;
}

public static MisfireStrategy of(Integer value) {
return IntValueEnum.of(MisfireStrategy.class, value);
public static MisfireStrategy of(int value) {
for (MisfireStrategy e : VALUES) {
if (e.value() == value) {
return e;
}
}
throw new IllegalArgumentException("Invalid misfire strategy value: " + value);
}

private static final MisfireStrategy[] VALUES = MisfireStrategy.values();

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,15 @@ public Operation operation() {
return operation;
}

public static RedeployStrategy of(Integer value) {
return IntValueEnum.of(RedeployStrategy.class, value);
public static RedeployStrategy of(int value) {
for (RedeployStrategy e : VALUES) {
if (e.value() == value) {
return e;
}
}
throw new IllegalArgumentException("Invalid redeploy strategy value: " + value);
}

private static final RedeployStrategy[] VALUES = RedeployStrategy.values();

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,15 @@ public String desc() {
return desc;
}

public static RetryType of(Integer value) {
return IntValueEnum.of(RetryType.class, value);
public static RetryType of(int value) {
for (RetryType e : VALUES) {
if (e.value() == value) {
return e;
}
}
throw new IllegalArgumentException("Invalid retry type value: " + value);
}

private static final RetryType[] VALUES = RetryType.values();

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,15 @@ public boolean isNotRoundRobin() {
return !isRoundRobin();
}

public static RouteStrategy of(Integer value) {
return IntValueEnum.of(RouteStrategy.class, value);
public static RouteStrategy of(int value) {
for (RouteStrategy e : VALUES) {
if (e.value() == value) {
return e;
}
}
throw new IllegalArgumentException("Invalid route strategy value: " + value);
}

private static final RouteStrategy[] VALUES = RouteStrategy.values();

}
36 changes: 12 additions & 24 deletions disjob-core/src/main/java/cn/ponfee/disjob/core/enums/RunState.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
package cn.ponfee.disjob.core.enums;

import cn.ponfee.disjob.common.base.IntValueEnum;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* The run state enum definition.
Expand Down Expand Up @@ -88,6 +85,10 @@ public String desc() {
return desc;
}

public boolean isPausable() {
return this == WAITING || this == RUNNING;
}

public boolean isTerminal() {
return terminal;
}
Expand All @@ -96,28 +97,15 @@ public boolean isFailure() {
return failure;
}

public static RunState of(Integer value) {
return IntValueEnum.of(RunState.class, value);
public static RunState of(int value) {
for (RunState e : VALUES) {
if (e.value() == value) {
return e;
}
}
throw new IllegalArgumentException("Invalid run state value: " + value);
}

public static final class Const {

/**
* State list of can transit to PAUSED
*/
public static final List<RunState> PAUSABLE_LIST = ImmutableList.of(WAITING, RUNNING);

/**
* State list of can transit to RUNNING
*/
public static final List<RunState> RUNNABLE_LIST = ImmutableList.of(WAITING, PAUSED);

/**
* State list of can transit to terminated
*
* @see #isTerminal()
*/
public static final List<RunState> TERMINABLE_LIST = ImmutableList.of(WAITING, RUNNING, PAUSED);
}
private static final RunState[] VALUES = RunState.values();

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,15 @@ public long getUniqueFlag() {
throw new UnsupportedOperationException(this + " cannot supported unique flag.");
}

public static RunType of(Integer value) {
return IntValueEnum.of(RunType.class, value);
public static RunType of(int value) {
for (RunType e : VALUES) {
if (e.value() == value) {
return e;
}
}
throw new IllegalArgumentException("Invalid run type value: " + value);
}

private static final RunType[] VALUES = RunType.values();

}
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,14 @@ public final List<Date> computeNextTriggerTimes(String triggerValue, Date date,
}

public static TriggerType of(int value) {
return IntValueEnum.of(TriggerType.class, value);
for (TriggerType e : VALUES) {
if (e.value() == value) {
return e;
}
}
throw new IllegalArgumentException("Invalid trigger type value: " + value);
}

private static final TriggerType[] VALUES = TriggerType.values();

}
Loading

0 comments on commit d93bc8c

Please sign in to comment.