streamline applying Stoppable

This commit is contained in:
Jonathan Shook 2023-02-05 20:31:01 -06:00
parent 9fc0530c17
commit 996675f79d
3 changed files with 38 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,4 +22,12 @@ public interface Stoppable {
* completes, the request will cause the component to stop cooperatively. * completes, the request will cause the component to stop cooperatively.
*/ */
void requestStop(); void requestStop();
static void stop(Object... candidates) {
for (Object candidate : candidates) {
if (candidate instanceof Stoppable stoppable) {
stoppable.requestStop();
}
}
}
} }

View File

@ -463,12 +463,7 @@ public class CoreMotor<D> implements ActivityDefObserver, Motor<D>, Stoppable {
public synchronized void requestStop() { public synchronized void requestStop() {
RunState currentState = motorState.get(); RunState currentState = motorState.get();
if (Objects.requireNonNull(currentState) == Running) { if (Objects.requireNonNull(currentState) == Running) {
if (input instanceof Stoppable) { Stoppable.stop(input, action);
((Stoppable) input).requestStop();
}
if (action instanceof Stoppable) {
((Stoppable) action).requestStop();
}
motorState.enterState(Stopping); motorState.enterState(Stopping);
} else { } else {
logger.warn(() -> "attempted to stop motor " + this.getSlotId() + ": from non Running state:" + currentState); logger.warn(() -> "attempted to stop motor " + this.getSlotId() + ": from non Running state:" + currentState);

View File

@ -20,6 +20,8 @@ import io.nosqlbench.engine.api.activityapi.core.RunState;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.util.Arrays;
/** /**
* A value type which encodes the atomic state of a RunState tally. * A value type which encodes the atomic state of a RunState tally.
*/ */
@ -39,21 +41,43 @@ public class RunStateImage {
} }
public boolean is(RunState runState) { public boolean is(RunState runState) {
return counts[runState.ordinal()]>0; return counts[runState.ordinal()] > 0;
} }
public boolean isOnly(RunState runState) { public boolean isOnly(RunState runState) {
for (int i = 0; i < counts.length; i++) { for (int i = 0; i < counts.length; i++) {
if (counts[i]>0 && i!=runState.ordinal()) { if (counts[i] > 0 && i != runState.ordinal()) {
return false; return false;
} }
} }
return true; return true;
} }
public boolean isNonOther(RunState... runStates) {
int[] scan = Arrays.copyOf(counts, counts.length);
for (RunState runState : runStates) {
scan[runState.ordinal()]=0;
}
for (int i : scan) {
if (i>0) {
return false;
}
}
return true;
}
public RunState getMinState() {
for (int ord = 0; ord < counts.length - 1; ord++) {
if (counts[ord] > 0) {
return RunState.values()[ord];
}
}
throw new RuntimeException("There were zero states, so min state is undefined");
}
public RunState getMaxState() { public RunState getMaxState() {
for (int ord = counts.length-1; ord >= 0; ord--) { for (int ord = counts.length - 1; ord >= 0; ord--) {
if (counts[ord]>0) { if (counts[ord] > 0) {
return RunState.values()[ord]; return RunState.values()[ord];
} }
} }