Skip to content

Commit

Permalink
global Exe timeout to ThreadLocal, BNG timeout had limited local sims.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcschaff committed Mar 11, 2024
1 parent 5af5a1e commit 99b6eb6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public Integer call() {
CLIPythonManager.getInstance().instantiatePythonProcess();


Executable.setTimeoutMS(EXECUTABLE_MAX_WALLCLOCK_MILLIS);
Executable.setGlobalTimeoutMS(EXECUTABLE_MAX_WALLCLOCK_MILLIS);
logger.info("Beginning execution");
if (inputFilePath.isDirectory()) {
logger.debug("Batch mode requested");
Expand Down
52 changes: 37 additions & 15 deletions vcell-core/src/main/java/org/vcell/util/exe/Executable.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class Executable implements IExecutable {
private String errorString = "";
private Integer exitValue = null;
private ExecutableStatus status = null;
private static long timeoutMS = 0;
private final long localTimeoutMS;
private static final ThreadLocal<Long> globalTimeoutMS = ThreadLocal.withInitial(() -> 0L);
private File workingDir = null;
private Thread runThread = null;
private CountDownLatch stoppingLatch = null;
Expand Down Expand Up @@ -90,19 +91,49 @@ public class Executable implements IExecutable {
*/

public Executable(String[] command) {
// Do NOT arbitrarily set a static value to a number!!
this(command, Executable.getTimeoutMS());
// no local timeout specified for this invocation, but still subject to globalTimeoutMS.
// see getEffectiveTimeoutMS()
this(command, 0);
}

/**
* Executable constructor comment.
*/
public Executable(String[] command, long arg_timeoutMS) {
public Executable(String[] command, long arg_localTimeoutMS) {
// timeout is to minimum of nonzero minimum of arg_timeoutMS and globalTimeoutMS.
// see getEffectiveTimeoutMS()
setCommand(command);
setStatus(ExecutableStatus.READY);
timeoutMS = arg_timeoutMS;
localTimeoutMS = arg_localTimeoutMS;
}

private long getEffectiveTimeoutMS() {
if (localTimeoutMS <= 0 && getGlobalTimeoutMS() <= 0) {
return 0;
}
if (localTimeoutMS > 0 && getGlobalTimeoutMS() > 0) {
return Math.min(localTimeoutMS, getGlobalTimeoutMS());
}
if (localTimeoutMS > 0) {
return localTimeoutMS;
}else{
return getGlobalTimeoutMS();
}
}

public static long getGlobalTimeoutMS() {
// zero or null means no timeout
if (Executable.globalTimeoutMS.get() == null) {
return 0;
}
return Executable.globalTimeoutMS.get();
}

public static void setGlobalTimeoutMS(long timeoutMS) {
// zero means no timeout
logger.trace("Setting timeout to: " + Long.toString(timeoutMS) + "ms");
Executable.globalTimeoutMS.set(timeoutMS);
}

/**
* This method was created by a SmartGuide.
Expand Down Expand Up @@ -298,7 +329,7 @@ protected final int monitorProcess(InputStream inputStreamOut, InputStream input

boolean running = true;
while (running || (numReadOut > 0) || (numReadErr > 0)) {
if (timeoutMS > 0 && System.currentTimeMillis() - t > timeoutMS) {
if (getEffectiveTimeoutMS() > 0 && System.currentTimeMillis() - t > getEffectiveTimeoutMS()) {
throw new ExecutableException("Process timed out");
}
try {
Expand Down Expand Up @@ -508,13 +539,4 @@ public void addEnvironmentVariable(String varName,String varValue){
}
addedEnvironmentVariables.put(varName,varValue);
}

public static long getTimeoutMS() {
return Executable.timeoutMS;
}

public static void setTimeoutMS(long timeoutMS) {
logger.trace("Setting timeout to: " + Long.toString(timeoutMS) + "ms");
Executable.timeoutMS = timeoutMS;
}
}

0 comments on commit 99b6eb6

Please sign in to comment.