Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions core/src/main/java/com/google/adk/workflow/NodeRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.adk.workflow;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.isNullOrEmpty;
Expand All @@ -37,6 +38,7 @@
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Scheduler;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.processors.FlowableProcessor;
Expand Down Expand Up @@ -201,12 +203,32 @@ public static Maybe<Object> runChild(
checkNotNull(parent);
checkNotNull(node);
checkNotNull(config);
return Maybe.defer(() -> startChild(parent, node, nodeInput, config));
return Maybe.defer(
() -> startChild(parent, node, nodeInput, config, /* reservedRunId= */ null));
}

/**
* Runs {@code node} as {@link #runChild(Context, Node, Object, RunNodeConfig)} does, using the
* {@code runId} that the caller reserved with {@link NodeExecutionState#nextChildRunId}. {@code
* config} must leave its run ID unset.
*/
static Maybe<Object> runChild(
Context parent, Node node, @Nullable Object nodeInput, RunNodeConfig config, String runId) {
checkNotNull(parent);
checkNotNull(node);
checkNotNull(config);
checkNotNull(runId);
checkArgument(config.runId().isEmpty(), "The config sets its own run ID.");
return Maybe.defer(() -> startChild(parent, node, nodeInput, config, runId));
}

/** Runs {@code node} as a child of {@code parent} and turns the child's run into its result. */
private static Maybe<Object> startChild(
Context parent, Node node, @Nullable Object nodeInput, RunNodeConfig config) {
Context parent,
Node node,
@Nullable Object nodeInput,
RunNodeConfig config,
@Nullable String reservedRunId) {
NodeExecutionState parentState = parent.requireNodeState();
checkState(
parentState.node().rerunOnResume(),
Expand All @@ -220,7 +242,10 @@ private static Maybe<Object> startChild(
if (delegatesOutput) {
parentState.claimOutputDelegation();
}
String runId = config.runId().orElseGet(() -> parentState.nextChildRunId(node.name()));
String runId =
reservedRunId != null
? reservedRunId
: config.runId().orElseGet(() -> parentState.nextChildRunId(node.name()));
return sharedChildRun(parent, node, nodeInput, runId, config)
.doOnError(
error -> {
Expand Down Expand Up @@ -536,8 +561,7 @@ private Completable withTimeout(Completable run) {
.andThen(Completable.error(() -> new NodeTimeoutException(node.name(), limit)));
io.opentelemetry.context.Context traceContext = io.opentelemetry.context.Context.current();
// Off the caller's thread, so a body that blocks does not block the caller.
return Completable.timer(0, NANOSECONDS, invocationContext.scheduler())
// Unlike subscribeOn, canceling this hop does not interrupt the handling of node events.
return completeOn(invocationContext.scheduler())
.andThen(run.compose(Tracing.withContext(traceContext)))
// convert saturates, so a timeout too long to count in nanoseconds waits about 292 years.
.timeout(NANOSECONDS.convert(limit), NANOSECONDS, timedOut)
Expand Down Expand Up @@ -802,6 +826,15 @@ static String segment(String name, String runId) {
return name + "@" + runId;
}

/**
* Returns a {@link Completable} that completes on {@code scheduler}, so work chained after it
* starts on that scheduler instead of the caller's thread. Unlike {@code subscribeOn}, canceling
* the chained work does not interrupt the thread it runs on.
*/
static Completable completeOn(Scheduler scheduler) {
return Completable.timer(0, NANOSECONDS, scheduler);
}

/** Returns whether {@code event} is a final event whose content is the node's output. */
private static boolean isMessageAsOutput(Event event) {
return !event.partial().orElse(false)
Expand Down
32 changes: 26 additions & 6 deletions core/src/main/java/com/google/adk/workflow/ParallelWorkers.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import com.google.adk.JsonBaseModel;
import com.google.adk.agents.Context;
import com.google.adk.annotations.Experimental;
import com.google.adk.telemetry.Tracing;
import com.google.common.collect.ImmutableList;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
import com.google.genai.types.Schema;
import com.google.genai.types.Type;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Single;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -84,7 +86,7 @@ private static final class ParallelWorkerNode extends BaseNode {
JsonBaseModel.getMapper()
.readerFor(Object.class)
.with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
// No run ID: items subscribe in input order, so each gets the same generated ID on resume.
// No run ID: FanOut reserves IDs in input order, so each item keeps its ID on resume.
private static final RunNodeConfig ITEM_CONFIG =
RunNodeConfig.builder().useSubBranch(true).build();

Expand Down Expand Up @@ -153,6 +155,11 @@ private static boolean acceptsString(Schema schema) {
private final class FanOut {
private final Context context;
private final List<?> items;
// Reserved up front, because the items begin on other threads in no fixed order.
private final ImmutableList<String> runIds;
// The items start on other threads, so each restores the node's trace context.
private final io.opentelemetry.context.Context traceContext =
io.opentelemetry.context.Context.current();
// Written by the item runs, which may finish on different threads.
private final AtomicReferenceArray<Object> outputs;
private final AtomicReferenceArray<DynamicNodeFailedException> failures;
Expand All @@ -161,6 +168,13 @@ private final class FanOut {
FanOut(Context context, List<?> items) {
this.context = context;
this.items = items;
NodeExecutionState state = context.requireNodeState();
ImmutableList.Builder<String> reserved =
ImmutableList.builderWithExpectedSize(items.size());
for (int i = 0; i < items.size(); i++) {
reserved.add(state.nextChildRunId(worker.name()));
}
this.runIds = reserved.build();
this.outputs = new AtomicReferenceArray<>(items.size());
this.failures = new AtomicReferenceArray<>(items.size());
}
Expand All @@ -178,11 +192,17 @@ Flowable<?> run() {

/** Runs one item, or replays its result on resume, and emits whether it failed. */
private Single<Boolean> runItem(int index) {
return context
.runNode(worker, items.get(index), ITEM_CONFIG)
.doOnSuccess(output -> outputs.set(index, output))
.ignoreElement()
.toSingleDefault(false)
Maybe<Object> run =
NodeRunner.runChild(context, worker, items.get(index), ITEM_CONFIG, runIds.get(index));
// Off the caller's thread, so an item that blocks its thread cannot hold up the others.
return NodeRunner.completeOn(context.invocationContext().scheduler())
.andThen(run.compose(Tracing.withContext(traceContext)))
// Not doOnSuccess, which reports a canceled item's late failure as undeliverable.
.map(output -> {
outputs.set(index, output);
return false;
})
.defaultIfEmpty(false)
.onErrorResumeNext(error -> settle(index, error));
}

Expand Down
Loading
Loading