Skip to content

Commit

Permalink
runtime-v2: tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
ibodrov committed May 6, 2024
1 parent 04968e3 commit 57118f3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

public interface RunnerLogger {

void withContext(LogContext context, Runnable runnable);
void withLogContext(LogContext context, Runnable runnable);

@Nullable
Long createSegment(String segmentName, UUID correlationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Long createSegment(String segmentName, UUID correlationId) {
}

@Override
public void withContext(LogContext context, Runnable runnable) {
public void withLogContext(LogContext context, Runnable runnable) {
ThreadGroup threadGroup = new LogContextThreadGroup(context);
executeInThreadGroup(threadGroup, "thread-" + context.segmentName(), () -> {
// make sure the redirection is enabled in the current thread
Expand Down Expand Up @@ -99,7 +99,7 @@ private static Map<String, Serializable> meta(AbstractStep<?> step) {

/**
* Executes the {@link Callable} in the specified {@link ThreadGroup}.
* A bit expensive as it is creates a new thread.
* A bit expensive as it is creating a new thread.
*/
private static void executeInThreadGroup(ThreadGroup group, String threadName, Runnable runnable) {
ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadGroupAwareThreadFactory(group, threadName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Long createSegment(String segmentName, UUID correlationId) {
}

@Override
public void withContext(LogContext context, Runnable runnable) {
public void withLogContext(LogContext context, Runnable runnable) {
try {
runnable.run();
} catch (RuntimeException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,17 @@ public void eval(Runtime runtime, State state, ThreadId threadId) {

logContext = getLogContext(runtime, ctx, correlationId);
if (logContext == null) {
executeWithContext(ctx, runtime, state, threadId);
ContextProvider.withContext(ctx, () -> execute(runtime, state, threadId));
} else {
runtime.getService(RunnerLogger.class).withContext(logContext,
() -> executeWithContext(ctx, runtime, state, threadId));
runtime.getService(RunnerLogger.class).withLogContext(logContext,
() -> ContextProvider.withContext(ctx, () -> execute(runtime, state, threadId)));
}
}

public UUID getCorrelationId() {
return correlationId;
}

private void executeWithContext(Context ctx, Runtime runtime, State state, ThreadId threadId) {
ContextProvider.withContext(ctx, () -> execute(runtime, state, threadId));
}

@Override
public void onException(Runtime runtime, Exception e, State state, ThreadId threadId) {
if (step.getLocation() == null) {
Expand All @@ -119,7 +115,7 @@ public void onException(Runtime runtime, Exception e, State state, ThreadId thre
if (logContext == null) {
logException(e, state, threadId);
} else {
runtime.getService(RunnerLogger.class).withContext(logContext,
runtime.getService(RunnerLogger.class).withLogContext(logContext,
() -> logException(e, state, threadId));
}
}
Expand Down
12 changes: 7 additions & 5 deletions runtime/v2/vm/src/main/java/com/walmartlabs/concord/svm/VM.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serial;
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
Expand Down Expand Up @@ -91,12 +92,12 @@ public void resume(State state, Set<String> eventRefs) throws Exception {
public void run(State state, Command cmd) throws Exception {
log.debug("run ['{}'] -> start", cmd);

Runtime rt = runtimeFactory.create(this);
Runtime runtime = runtimeFactory.create(this);
ThreadId threadId = state.getRootThreadId();
try {
cmd.eval(rt, state, threadId);
cmd.eval(runtime, state, threadId);
} catch (Exception e) {
cmd.onException(rt, e, state, threadId);
cmd.onException(runtime, e, state, threadId);
throw e;
}

Expand Down Expand Up @@ -166,7 +167,7 @@ private EvalResult execute(Runtime runtime, State state) throws Exception {
EvalResult result;

while (true) {
// if we're restoring from a previously saved state or we had new threads created
// if we're restoring from a previously saved state, or we had new threads created
// on the previous iteration we need to spawn all READY threads
for (Map.Entry<ThreadId, ThreadStatus> e : state.threadStatus().entrySet()) {
if (e.getKey() != state.getRootThreadId() && e.getValue() == ThreadStatus.READY) {
Expand Down Expand Up @@ -232,8 +233,9 @@ private static void wakeSuspended(State state) {
}
}

private static class EvalResult implements Serializable {
public static class EvalResult implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

private final Frame lastFrame;
Expand Down

0 comments on commit 57118f3

Please sign in to comment.