Skip to content

Commit

Permalink
refactor: TicketType for external custom tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
ishland committed Aug 9, 2024
1 parent bac4163 commit 3a1b849
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/ishland/flowsched/scheduler/ItemHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public void addDependencyTicket(StatusAdvancingScheduler<K, V, Ctx, ?> scheduler
if (info.refCnt[ordinal] == -1) {
info.refCnt[ordinal] = 0;
info.callbacks[ordinal] = new ObjectArrayList<>();
scheduler.addTicketWithSource(key, ItemTicket.TicketType.DEPENDENCY, this.getKey(), status, () -> {
scheduler.addTicket(key, ItemTicket.TicketType.DEPENDENCY, this.getKey(), status, () -> {
final ObjectArrayList<Runnable> list;
synchronized (this.dependencyInfos) {
list = info.callbacks[ordinal];
Expand Down Expand Up @@ -352,7 +352,7 @@ public void cleanupDependencies(StatusAdvancingScheduler<K, V, Ctx, ?> scheduler
boolean isEmpty = true;
for (int ordinal = 0, refCntLength = refCnt.length; ordinal < refCntLength; ordinal++) {
if (refCnt[ordinal] == 0) {
scheduler.removeTicketWithSource(key, ItemTicket.TicketType.DEPENDENCY, this.getKey(), this.unloadedStatus.getAllStatuses()[ordinal]);
scheduler.removeTicket(key, ItemTicket.TicketType.DEPENDENCY, this.getKey(), this.unloadedStatus.getAllStatuses()[ordinal]);
refCnt[ordinal] = -1;
info.callbacks[ordinal] = null;
}
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/com/ishland/flowsched/scheduler/ItemTicket.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,21 @@ public int hashCode() {
// return hc;
// }

public enum TicketType {
EXTERNAL,
DEPENDENCY, // source: K
public static class TicketType {
public static TicketType DEPENDENCY = new TicketType("flowsched:dependency");
public static TicketType EXTERNAL = new TicketType("flowsched:external");

private final String description;

public TicketType(String description) {
this.description = description;
}

public String getDescription() {
return this.description;
}

// use default equals() and hashCode()

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,14 @@ private CancellationSignaller getDependencyFuture0(KeyStatusPair<K, V, Ctx>[] de
}

public ItemHolder<K, V, Ctx, UserData> addTicket(K key, ItemStatus<K, V, Ctx> targetStatus, Runnable callback) {
return this.addTicketWithSource(key, ItemTicket.TicketType.EXTERNAL, key, targetStatus, callback);
return this.addTicket(key, key, targetStatus, callback);
}

ItemHolder<K, V, Ctx, UserData> addTicketWithSource(K key, ItemTicket.TicketType type, Object source, ItemStatus<K, V, Ctx> targetStatus, Runnable callback) {
public ItemHolder<K, V, Ctx, UserData> addTicket(K key, Object source, ItemStatus<K, V, Ctx> targetStatus, Runnable callback) {
return this.addTicket(key, ItemTicket.TicketType.EXTERNAL, source, targetStatus, callback);
}

public ItemHolder<K, V, Ctx, UserData> addTicket(K key, ItemTicket.TicketType type, Object source, ItemStatus<K, V, Ctx> targetStatus, Runnable callback) {
return this.addTicket0(key, new ItemTicket<>(type, source, targetStatus, callback));
}

Expand Down Expand Up @@ -428,10 +432,10 @@ private ItemHolder<K, V, Ctx, UserData> createHolder(K k) {
}

public void removeTicket(K key, ItemStatus<K, V, Ctx> targetStatus) {
this.removeTicketWithSource(key, ItemTicket.TicketType.EXTERNAL, key, targetStatus);
this.removeTicket(key, ItemTicket.TicketType.EXTERNAL, key, targetStatus);
}

void removeTicketWithSource(K key, ItemTicket.TicketType type, Object source, ItemStatus<K, V, Ctx> targetStatus) {
public void removeTicket(K key, ItemTicket.TicketType type, Object source, ItemStatus<K, V, Ctx> targetStatus) {
ItemHolder<K, V, Ctx, UserData> holder = this.getHolder(key);
if (holder == null) {
throw new IllegalStateException("No such item");
Expand Down

0 comments on commit 3a1b849

Please sign in to comment.