Skip to content

Commit

Permalink
Track dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
julienp committed Nov 27, 2024
1 parent 4e4ddb2 commit ec37d0f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -156,6 +157,14 @@ public OutputData<T> withDependency(Resource resource) {
return ofNullable(newDependencies, this.value, this.known, this.secret);
}

public OutputData<T> withDependencies(List<Resource> resources) {
var newDependencies = Sets.union(
this.resources,
ImmutableSet.copyOf(resources)
).immutableCopy();
return ofNullable(newDependencies, this.value, this.known, this.secret);
}

public <U> OutputData<U> apply(Function<? super T, ? extends U> function) {
if (known) {
return ofNullable(resources, function.apply(value), true, secret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,14 +535,19 @@ public <T> Output<T> invoke(String token, TypeShape<T> targetType, InvokeArgs ar
? CompletableFuture.completedFuture(null)
: packageRef;

// Find all the resource dependencies from dependsOn, we need to wait for this task to complete
// before calling the invoke.
var depsFuture = this.prepare.getAllTransitivelyReferencedResourceUrnsAsync(ImmutableSet.copyOf(options.getDependsOn()));

// Wait for all values from args to be available, and then perform the RPC.
return new OutputInternal<>(this.featureSupport.monitorSupportsResourceReferences()
.thenCompose(keepResources -> this.serializeInvokeArgs(token, args, keepResources))
.thenCompose(serializedArgs -> {
if (!serializedArgs.containsUnknowns) {
return packageRefFuture
.thenCompose(packageRefString -> this.invokeRawAsync(token, serializedArgs, options, packageRefString))
.thenApply(result -> parseInvokeResponse(token, targetType, result));
return CompletableFuture.allOf(depsFuture, packageRefFuture)
.thenCompose(v -> this.invokeRawAsync(token, serializedArgs, options, packageRefFuture.join()))
.thenApply(result -> parseInvokeResponse(token, targetType, result))
.thenApply(output -> output.withDependencies(options.getDependsOn()));
} else {
return CompletableFuture.completedFuture(OutputData.unknown());
}
Expand Down Expand Up @@ -701,9 +706,6 @@ private CompletableFuture<SerializationResult> invokeRawAsync(
}
);

// Wait for all the resource dependencies from dependsOn to be available before we call the invoke
this.prepare.getAllTransitivelyReferencedResourceUrnsAsync(ImmutableSet.copyOf(options.getDependsOn())).join();

return providerFuture.thenCompose(provider -> {
var version = options.getVersion();
log.debugOrExcessive(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ public CompletableFuture<ResourceResult> newResourceAsync(ResourceArgs args) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
return ResourceResult.of(Optional.of(args.id + "_id"), ImmutableMap.of("prop", "some value"));
return ResourceResult.of(Optional.of(args.id + "_id"),
ImmutableMap.of("prop", "some value"));
});
return result;
}

@Override
public CompletableFuture<Map<String, Object>> callAsync(CallArgs args) {
assertThat(marker.resolved).isTrue(); // The resource should have been resolved
return CompletableFuture.completedFuture(
ImmutableMap.of(
"result",
ImmutableList.of(ImmutableMap.of("root",
ImmutableMap.of("test1", ImmutableList.of("1", "2", "3"))))));
ImmutableMap.of("result", ImmutableList.of(ImmutableMap.of("root",
ImmutableMap.of("test1", ImmutableList.of("1", "2", "3"))))));
}
})
.build();
Expand All @@ -80,24 +80,19 @@ public CompletableFuture<Map<String, Object>> callAsync(CallArgs args) {
deps.add(res);

var opts = new InvokeOutputOptions(null, null, null, deps);
var out = CustomInvokes.doStuff(CustomArgs.Empty, opts).applyValue(r -> {
CustomInvokes.doStuff(CustomArgs.Empty, opts).applyValue(r -> {
assertThat(r).hasSize(1);
assertThat(r).contains(ImmutableMap.of("root", ImmutableMap.of("test1", ImmutableList.of("1", "2", "3"))));
return (Void) null;
assertThat(r)
.contains(ImmutableMap.of("root", ImmutableMap.of("test1", ImmutableList.of("1", "2", "3"))));
return r;
});

// Check that the resource was resolved when we called the invoke
assertThat(marker.resolved).isTrue();

Internal.of(out).getDataAsync().join();
});

assertThat(result.exceptions()).hasSize(0);
assertThat(result.exitCode()).isEqualTo(Runner.ProcessExitedSuccessfully);
}

public static final class MyArgs extends ResourceArgs {
// Empty
}

public static final class ResolveMarker {
Expand All @@ -118,32 +113,23 @@ public MyCustomResource(String name, @Nullable MyArgs args, @Nullable CustomReso

static class CustomInvokes {
static Output<ImmutableList<ImmutableMap<String, Object>>> doStuff(
@SuppressWarnings("SameParameterValue") CustomArgs args,
@Nullable InvokeOutputOptions options) {
return Deployment.getInstance().invoke(
"tests:custom:stuff",
TypeShape.of(CustomResult.class),
args,
options).applyValue(r -> {
@SuppressWarnings("SameParameterValue") CustomArgs args, @Nullable InvokeOutputOptions options) {
return Deployment.getInstance()
.invoke("tests:custom:stuff", TypeShape.of(CustomResult.class), args, options).applyValue(r -> {
return r.result;
});
}
}

static class CustomArgs extends InvokeArgs {
public static final CustomArgs Empty = new CustomArgs(null, null);
public static final CustomArgs Empty = new CustomArgs(null);

@Import(name = "text")
@Nullable
public final String text;

@Import(name = "defaultNamespace")
@Nullable
public final String defaultNamespace;

CustomArgs(@Nullable String text, @Nullable String defaultNamespace) {
CustomArgs(@Nullable String text) {
this.text = text;
this.defaultNamespace = defaultNamespace;
}
}

Expand Down

0 comments on commit ec37d0f

Please sign in to comment.