diff options
author | Arne H Juul <arnej@yahooinc.com> | 2022-01-03 13:05:08 +0000 |
---|---|---|
committer | Arne H Juul <arnej@yahooinc.com> | 2022-01-03 13:05:08 +0000 |
commit | ef67e176184d8d2261d9f6de788897c37fea0294 (patch) | |
tree | ac87d6d673b92b44a7dd19eaf322b1e72c38ed64 | |
parent | 2ff775ff41d2d759c62f7423a34c795c3ea2a504 (diff) | |
parent | 1f3e5d1003a33d9a7076575eab8059c582611cdb (diff) |
Merge branch 'master' into arnej/feature-flag-for-user-agents
Conflicts:
config-model-api/src/main/java/com/yahoo/config/model/api/ModelContext.java
configserver/src/main/java/com/yahoo/vespa/config/server/deploy/ModelContextImpl.java
16 files changed, 288 insertions, 58 deletions
diff --git a/config-lib/src/main/java/com/yahoo/config/ConfigInstance.java b/config-lib/src/main/java/com/yahoo/config/ConfigInstance.java index fad386b288e..5f06bc27dec 100644 --- a/config-lib/src/main/java/com/yahoo/config/ConfigInstance.java +++ b/config-lib/src/main/java/com/yahoo/config/ConfigInstance.java @@ -110,11 +110,20 @@ public abstract class ConfigInstance extends InnerNode { } } - + /** + * @deprecated do not use + */ + @Deprecated + // TODO: Remove in Vespa 8 public String getConfigMd5() { return configMd5; } + /** + * @deprecated do not use + */ + @Deprecated + // TODO: Remove in Vespa 8 public void setConfigMd5(String configMd5) { this.configMd5 = configMd5; } diff --git a/config-model-api/src/main/java/com/yahoo/config/model/api/ModelContext.java b/config-model-api/src/main/java/com/yahoo/config/model/api/ModelContext.java index 77bf2ea759a..a0dba36fef5 100644 --- a/config-model-api/src/main/java/com/yahoo/config/model/api/ModelContext.java +++ b/config-model-api/src/main/java/com/yahoo/config/model/api/ModelContext.java @@ -114,6 +114,7 @@ public interface ModelContext { @ModelFeatureFlag(owners = {"hmusum"}) default boolean failDeploymentWithInvalidJvmOptions() { return false; } @ModelFeatureFlag(owners = {"baldersheim"}) default double tlsSizeFraction() { throw new UnsupportedOperationException("TODO specify default value"); } @ModelFeatureFlag(owners = {"arnej", "andreer"}) default List<String> ignoredHttpUserAgents() { return List.of(); } + @ModelFeatureFlag(owners = {"bjorncs"}) default boolean enableServerOcspStapling() { return false; } } /** Warning: As elsewhere in this package, do not make backwards incompatible changes that will break old config models! */ diff --git a/config-model/src/main/java/com/yahoo/vespa/model/container/ApplicationContainer.java b/config-model/src/main/java/com/yahoo/vespa/model/container/ApplicationContainer.java index 9ad257fad04..7c386875d02 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/container/ApplicationContainer.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/container/ApplicationContainer.java @@ -23,6 +23,7 @@ public final class ApplicationContainer extends Container implements private static final String defaultHostedJVMArgs = "-XX:+SuppressFatalErrorMessage"; private final boolean isHostedVespa; + private final boolean enableServerOcspStapling; public ApplicationContainer(AbstractConfigProducer<?> parent, String name, int index, DeployState deployState) { this(parent, name, false, index, deployState); @@ -31,6 +32,7 @@ public final class ApplicationContainer extends Container implements public ApplicationContainer(AbstractConfigProducer<?> parent, String name, boolean retired, int index, DeployState deployState) { super(parent, name, retired, index, deployState); this.isHostedVespa = deployState.isHosted(); + this.enableServerOcspStapling = deployState.featureFlags().enableServerOcspStapling(); addComponent(new SimpleComponent("com.yahoo.container.jdisc.messagebus.NetworkMultiplexerHolder")); addComponent(new SimpleComponent("com.yahoo.container.jdisc.messagebus.NetworkMultiplexerProvider")); @@ -64,10 +66,23 @@ public final class ApplicationContainer extends Container implements /** Returns the jvm arguments this should start with */ @Override public String getJvmOptions() { + StringBuilder b = new StringBuilder(); + if (isHostedVespa) { + if (hasDocproc()) { + b.append(ApplicationContainer.defaultHostedJVMArgs).append(' '); + } + if (enableServerOcspStapling) { + b.append("-Djdk.tls.server.enableStatusRequestExtension=true ") + .append("-Djdk.tls.stapling.responseTimeout=2000 ") + .append("-Djdk.tls.stapling.cacheSize=256 ") + .append("-Djdk.tls.stapling.cacheLifetime=3600 "); + } + } String jvmArgs = super.getJvmOptions(); - return isHostedVespa && hasDocproc() - ? ("".equals(jvmArgs) ? defaultHostedJVMArgs : defaultHostedJVMArgs + " " + jvmArgs) - : jvmArgs; + if (!jvmArgs.isBlank()) { + b.append(jvmArgs.trim()); + } + return b.toString().trim(); } private boolean hasDocproc() { diff --git a/config-model/src/main/java/com/yahoo/vespa/model/generic/GenericServicesBuilder.java b/config-model/src/main/java/com/yahoo/vespa/model/generic/GenericServicesBuilder.java index 77acffe7f9d..7a0278faa48 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/generic/GenericServicesBuilder.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/generic/GenericServicesBuilder.java @@ -13,6 +13,8 @@ import java.util.List; /** * @author Ulf Lilleengen * @since 5.1 + * + * TODO: remove in Vespa 8 */ public class GenericServicesBuilder extends ConfigModelBuilder<GenericServicesModel> { @@ -22,7 +24,7 @@ public class GenericServicesBuilder extends ConfigModelBuilder<GenericServicesMo @Override public List<ConfigModelId> handlesElements() { - return Arrays.asList(ConfigModelId.fromName("service")); + return List.of(ConfigModelId.fromName("service")); } @Override diff --git a/config-model/src/main/java/com/yahoo/vespa/model/generic/builder/DomServiceClusterBuilder.java b/config-model/src/main/java/com/yahoo/vespa/model/generic/builder/DomServiceClusterBuilder.java index ea2151648cc..1ac668049f0 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/generic/builder/DomServiceClusterBuilder.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/generic/builder/DomServiceClusterBuilder.java @@ -8,10 +8,13 @@ import com.yahoo.vespa.model.builder.xml.dom.VespaDomBuilder; import com.yahoo.vespa.model.generic.service.ServiceCluster; import org.w3c.dom.Element; import java.util.Map; +import java.util.logging.Level; /** -* @author Ulf Lilleengen -*/ + * @author Ulf Lilleengen + * + * TODO: remove in Vespa 8 + */ public class DomServiceClusterBuilder extends VespaDomBuilder.DomConfigProducerBuilder<ServiceCluster> { private final String name; @@ -22,6 +25,9 @@ public class DomServiceClusterBuilder extends VespaDomBuilder.DomConfigProducerB @Override protected ServiceCluster doBuild(DeployState deployState, AbstractConfigProducer<?> ancestor, Element spec) { + deployState.getDeployLogger().logApplicationPackage( + Level.WARNING, "The 'service' element is deprecated and will be removed in Vespa 8, without replacement."); + ServiceCluster cluster = new ServiceCluster(ancestor, name, spec.getAttribute("command")); int nodeIndex = 0; for (Element nodeSpec : XML.getChildren(spec, "node")) { diff --git a/config/src/main/java/com/yahoo/config/subscription/impl/JRTConfigSubscription.java b/config/src/main/java/com/yahoo/config/subscription/impl/JRTConfigSubscription.java index 0b98e9cd1b2..68e22ad5656 100644 --- a/config/src/main/java/com/yahoo/config/subscription/impl/JRTConfigSubscription.java +++ b/config/src/main/java/com/yahoo/config/subscription/impl/JRTConfigSubscription.java @@ -124,6 +124,7 @@ public class JRTConfigSubscription<T extends ConfigInstance> extends ConfigSubsc * @param jrtRequest a config request * @return an instance of a config class (subclass of ConfigInstance) */ + @SuppressWarnings("deprecation") private T toConfigInstance(JRTClientConfigRequest jrtRequest) { Payload payload = jrtRequest.getNewPayload(); ConfigPayload configPayload = ConfigPayload.fromUtf8Array(payload.withCompression(CompressionType.UNCOMPRESSED).getData()); @@ -157,7 +158,6 @@ public class JRTConfigSubscription<T extends ConfigInstance> extends ConfigSubsc } @Override - @SuppressWarnings("serial") public void close() { super.close(); reqQueue = new LinkedBlockingQueue<>() { diff --git a/config/src/main/java/com/yahoo/config/subscription/impl/MockConnection.java b/config/src/main/java/com/yahoo/config/subscription/impl/MockConnection.java index 6446758a9ba..24a080d5824 100644 --- a/config/src/main/java/com/yahoo/config/subscription/impl/MockConnection.java +++ b/config/src/main/java/com/yahoo/config/subscription/impl/MockConnection.java @@ -40,10 +40,7 @@ public class MockConnection implements ConnectionPool, Connection { public void invokeAsync(Request request, double jrtTimeout, RequestWaiter requestWaiter) { numberOfRequests++; lastRequest = request; - responseHandler.requestWaiter(requestWaiter).request(request); - Thread t = new Thread(responseHandler); - t.setDaemon(true); - t.run(); + responseHandler.handle(request, requestWaiter); } @Override @@ -85,7 +82,7 @@ public class MockConnection implements ConnectionPool, Connection { long generation = 1; - protected void createResponse() { + protected void createResponse(Request request) { JRTServerConfigRequestV3 jrtReq = JRTServerConfigRequestV3.createFromRequest(request); Payload payload = Payload.from(ConfigPayload.empty()); jrtReq.addOkResponse(payload, generation, false, PayloadChecksums.fromPayload(payload)); @@ -94,51 +91,22 @@ public class MockConnection implements ConnectionPool, Connection { } - public interface ResponseHandler extends Runnable { + public interface ResponseHandler { - RequestWaiter requestWaiter(); + void handle(Request request, RequestWaiter requestWaiter); - Request request(); - - ResponseHandler requestWaiter(RequestWaiter requestWaiter); - - ResponseHandler request(Request request); } public abstract static class AbstractResponseHandler implements ResponseHandler { - private RequestWaiter requestWaiter; - protected Request request; - - @Override - public RequestWaiter requestWaiter() { - return requestWaiter; - } - - @Override - public Request request() { - return request; - } - - @Override - public ResponseHandler requestWaiter(RequestWaiter requestWaiter) { - this.requestWaiter = requestWaiter; - return this; - } - @Override - public ResponseHandler request(Request request) { - this.request = request; - return this; - } - - @Override - public void run() { - createResponse(); + public void handle(Request request, RequestWaiter requestWaiter) { + createResponse(request); requestWaiter.handleRequestDone(request); } - protected abstract void createResponse(); + protected abstract void createResponse(Request request); + } } diff --git a/config/src/test/java/com/yahoo/config/subscription/impl/JRTConfigRequesterTest.java b/config/src/test/java/com/yahoo/config/subscription/impl/JRTConfigRequesterTest.java index dca0c2d0018..07c3a5af56c 100644 --- a/config/src/test/java/com/yahoo/config/subscription/impl/JRTConfigRequesterTest.java +++ b/config/src/test/java/com/yahoo/config/subscription/impl/JRTConfigRequesterTest.java @@ -4,6 +4,7 @@ package com.yahoo.config.subscription.impl; import com.yahoo.config.subscription.ConfigSourceSet; import com.yahoo.foo.SimpletypesConfig; import com.yahoo.jrt.Request; +import com.yahoo.jrt.RequestWaiter; import com.yahoo.vespa.config.ConfigKey; import com.yahoo.vespa.config.ConnectionPool; import com.yahoo.vespa.config.ErrorCode; @@ -209,10 +210,10 @@ public class JRTConfigRequesterTest { } @Override - public void run() { + public void handle(Request request, RequestWaiter requestWaiter) { System.out.println("Running error response handler"); - request().setError(errorCode, "error"); - requestWaiter().handleRequestDone(request()); + request.setError(errorCode, "error"); + requestWaiter.handleRequestDone(request); } } @@ -224,16 +225,15 @@ public class JRTConfigRequesterTest { } @Override - public void run() { - System.out.println("Running delayed response handler (waiting " + waitTimeMilliSeconds + - ") before responding"); + public void handle(Request request, RequestWaiter requestWaiter) { + System.out.println("Running delayed response handler (waiting " + waitTimeMilliSeconds + ") before responding"); try { Thread.sleep(waitTimeMilliSeconds); } catch (InterruptedException e) { e.printStackTrace(); } - request().setError(com.yahoo.jrt.ErrorCode.TIMEOUT, "error"); - requestWaiter().handleRequestDone(request()); + request.setError(com.yahoo.jrt.ErrorCode.TIMEOUT, "error"); + requestWaiter.handleRequestDone(request); } } diff --git a/config/src/test/java/com/yahoo/vespa/config/protocol/JRTConfigRequestV3Test.java b/config/src/test/java/com/yahoo/vespa/config/protocol/JRTConfigRequestV3Test.java index 8d160076db9..b3285e5157a 100644 --- a/config/src/test/java/com/yahoo/vespa/config/protocol/JRTConfigRequestV3Test.java +++ b/config/src/test/java/com/yahoo/vespa/config/protocol/JRTConfigRequestV3Test.java @@ -203,7 +203,7 @@ public class JRTConfigRequestV3Test { public void created_from_existing_subscription() { MockConnection connection = new MockConnection(new MockConnection.AbstractResponseHandler() { @Override - public void createResponse() { + public void createResponse(Request request) { JRTServerConfigRequest serverRequest = createReq(request); serverRequest.addOkResponse(createPayload(), currentGeneration, false, payloadChecksums); } diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/deploy/ModelContextImpl.java b/configserver/src/main/java/com/yahoo/vespa/config/server/deploy/ModelContextImpl.java index 942a84a2730..a77a8d1b5b8 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/deploy/ModelContextImpl.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/deploy/ModelContextImpl.java @@ -206,6 +206,7 @@ public class ModelContextImpl implements ModelContext { private final boolean failDeploymentWithInvalidJvmOptions; private final double tlsSizeFraction; private final List<String> ignoredHttpUserAgents; + private final boolean enableServerOcspStapling; public FeatureFlags(FlagSource source, ApplicationId appId) { this.defaultTermwiseLimit = flagValue(source, appId, Flags.DEFAULT_TERM_WISE_LIMIT); @@ -250,6 +251,7 @@ public class ModelContextImpl implements ModelContext { this.failDeploymentWithInvalidJvmOptions = flagValue(source, appId, Flags.FAIL_DEPLOYMENT_WITH_INVALID_JVM_OPTIONS); this.tlsSizeFraction = flagValue(source, appId, Flags.TLS_SIZE_FRACTION); this.ignoredHttpUserAgents = flagValue(source, appId, PermanentFlags.IGNORED_HTTP_USER_AGENTS); + this.enableServerOcspStapling = flagValue(source, appId, Flags.ENABLE_SERVER_OCSP_STAPLING); } @Override public double defaultTermwiseLimit() { return defaultTermwiseLimit; } @@ -296,6 +298,7 @@ public class ModelContextImpl implements ModelContext { @Override public int maxCompactBuffers() { return maxCompactBuffers; } @Override public double tlsSizeFraction() { return tlsSizeFraction; } @Override public List<String> ignoredHttpUserAgents() { return ignoredHttpUserAgents; } + @Override public boolean enableServerOcspStapling() { return enableServerOcspStapling; } private static <V> V flagValue(FlagSource source, ApplicationId appId, UnboundFlag<? extends V, ?, ?> flag) { return flag.bindTo(source) diff --git a/flags/src/main/java/com/yahoo/vespa/flags/Flags.java b/flags/src/main/java/com/yahoo/vespa/flags/Flags.java index c3394c9dd76..5ef22580f28 100644 --- a/flags/src/main/java/com/yahoo/vespa/flags/Flags.java +++ b/flags/src/main/java/com/yahoo/vespa/flags/Flags.java @@ -303,7 +303,7 @@ public class Flags { ); public static final UnboundBooleanFlag ENABLE_ROUTING_REUSE_PORT = defineFeatureFlag( - "enable-routing-reuse-port", false, + "enable-routing-reuse-port", true, List.of("mortent"), "2021-09-29", "2022-02-01", "Enable reuse port in routing configuration", "Takes effect on container restart", @@ -400,6 +400,13 @@ public class Flags { "Takes effect at redeployment", ZONE_ID, APPLICATION_ID); + public static final UnboundBooleanFlag ENABLE_SERVER_OCSP_STAPLING = defineFeatureFlag( + "enable-server-ocsp-stapling", false, + List.of("bjorncs"), "2021-12-17", "2022-06-01", + "Enable server OCSP stapling for jdisc containers", + "Takes effect on redeployment", + ZONE_ID, APPLICATION_ID); + /** WARNING: public for testing: All flags should be defined in {@link Flags}. */ public static UnboundBooleanFlag defineFeatureFlag(String flagId, boolean defaultValue, List<String> owners, String createdAt, String expiresAt, String description, diff --git a/vespa-feed-client-api/abi-spec.json b/vespa-feed-client-api/abi-spec.json index a9047365a7a..8af7798984f 100644 --- a/vespa-feed-client-api/abi-spec.json +++ b/vespa-feed-client-api/abi-spec.json @@ -103,6 +103,8 @@ "public abstract java.util.concurrent.CompletableFuture put(ai.vespa.feed.client.DocumentId, java.lang.String, ai.vespa.feed.client.OperationParameters)", "public abstract java.util.concurrent.CompletableFuture update(ai.vespa.feed.client.DocumentId, java.lang.String, ai.vespa.feed.client.OperationParameters)", "public abstract java.util.concurrent.CompletableFuture remove(ai.vespa.feed.client.DocumentId, ai.vespa.feed.client.OperationParameters)", + "public static java.util.List await(java.util.List)", + "public static varargs java.util.List await(java.util.concurrent.CompletableFuture[])", "public abstract ai.vespa.feed.client.OperationStats stats()", "public abstract ai.vespa.feed.client.FeedClient$CircuitBreaker$State circuitBreakerState()", "public abstract void close(boolean)", @@ -221,6 +223,19 @@ ], "fields": [] }, + "ai.vespa.feed.client.MultiFeedException": { + "superClass": "java.lang.RuntimeException", + "interfaces": [], + "attributes": [ + "public" + ], + "methods": [ + "public void <init>(java.util.Collection)", + "public java.util.Collection feedExceptions()", + "public java.util.Set documentIds()" + ], + "fields": [] + }, "ai.vespa.feed.client.OperationParameters": { "superClass": "java.lang.Object", "interfaces": [], diff --git a/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/FeedClient.java b/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/FeedClient.java index d463c611d6a..5e95990a078 100644 --- a/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/FeedClient.java +++ b/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/FeedClient.java @@ -2,6 +2,7 @@ package ai.vespa.feed.client; import java.io.Closeable; +import java.util.List; import java.util.concurrent.CompletableFuture; /** @@ -37,6 +38,24 @@ public interface FeedClient extends Closeable { */ CompletableFuture<Result> remove(DocumentId documentId, OperationParameters params); + /** + * Waits for all feed operations to complete, either successfully or with exception. + * @throws MultiFeedException if any operation fails + * @return list of results with the same ordering as the {@code promises} parameter + * */ + static List<Result> await(List<CompletableFuture<Result>> promises) throws MultiFeedException { + return Helper.await(promises); + } + + /** + * Same as {@link #await(List)} except {@code promises} parameter is a vararg + * @see #await(List) + */ + @SafeVarargs + static List<Result> await(CompletableFuture<Result>... promises) throws MultiFeedException { + return Helper.await(promises); + } + /** Returns a snapshot of the stats for this feed client, such as requests made, and responses by status. */ OperationStats stats(); diff --git a/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/Helper.java b/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/Helper.java new file mode 100644 index 00000000000..59c12077bef --- /dev/null +++ b/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/Helper.java @@ -0,0 +1,42 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.feed.client; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.stream.Collectors; + +/** + * @author bjorncs + */ +class Helper { + + private Helper() {} + + @SafeVarargs + static List<Result> await(CompletableFuture<Result>... promises) throws MultiFeedException { + List<CompletableFuture<Result>> list = new ArrayList<>(); + for (CompletableFuture<Result> p : promises) list.add(p); + return await(list); + } + + static List<Result> await(List<CompletableFuture<Result>> promises) throws MultiFeedException { + try { + CompletableFuture.allOf(promises.toArray(new CompletableFuture<?>[0])).join(); + return promises.stream() + .map(p -> Objects.requireNonNull(p.getNow(null))) + .collect(Collectors.toList()); + } catch (CompletionException e) { + List<FeedException> exceptions = new ArrayList<>(); + for (CompletableFuture<Result> promise : promises) { + if (promise.isCompletedExceptionally()) { + // Lambda is executed on this thread since the future is already completed + promise.whenComplete((__, error) -> exceptions.add((FeedException) error)); + } + } + throw new MultiFeedException(exceptions); + } + } +} diff --git a/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/MultiFeedException.java b/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/MultiFeedException.java new file mode 100644 index 00000000000..5db687b49ff --- /dev/null +++ b/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/MultiFeedException.java @@ -0,0 +1,38 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.feed.client; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Aggregates multiple instances of {@link FeedException} + * + * @author bjorncs + */ +public class MultiFeedException extends RuntimeException { + + private final List<FeedException> exceptions; + + public MultiFeedException(Collection<FeedException> exceptions) { + super(toMessage(exceptions)); + this.exceptions = Collections.unmodifiableList(new ArrayList<>(exceptions)); + } + + public Collection<FeedException> feedExceptions() { return exceptions; } + + public Set<DocumentId> documentIds() { + return exceptions.stream() + .filter(e -> e.documentId().isPresent()) + .map(e -> e.documentId().get()) + .collect(Collectors.toSet()); + } + + private static String toMessage(Collection<FeedException> exceptions) { + return String.format("%d feed operations failed", exceptions.size()); + } + +} diff --git a/vespa-feed-client-api/src/test/java/ai/vespa/feed/client/FeedClientTest.java b/vespa-feed-client-api/src/test/java/ai/vespa/feed/client/FeedClientTest.java new file mode 100644 index 00000000000..688f311bb05 --- /dev/null +++ b/vespa-feed-client-api/src/test/java/ai/vespa/feed/client/FeedClientTest.java @@ -0,0 +1,105 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.feed.client; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author bjorncs + */ +class FeedClientTest { + + private ExecutorService executor; + + @BeforeEach + void setUp() { + executor = Executors.newSingleThreadExecutor(); + } + + @AfterEach + void tearDown() throws InterruptedException { + executor.shutdown(); + assertTrue(executor.awaitTermination(60, TimeUnit.SECONDS)); + } + + @Test + void await_returns_list_of_result_on_success() { + MyResult r1 = new MyResult(); + CompletableFuture<Result> f1 = CompletableFuture.completedFuture(r1); + MyResult r2 = new MyResult(); + CompletableFuture<Result> f2 = CompletableFuture.completedFuture(r2); + MyResult r3 = new MyResult(); + CompletableFuture<Result> f3 = CompletableFuture.completedFuture(r3); + + List<Result> aggregated = FeedClient.await(f1, f2, f3); + assertEquals(3, aggregated.size()); + assertEquals(r1, aggregated.get(0)); + assertEquals(r2, aggregated.get(1)); + assertEquals(r3, aggregated.get(2)); + } + + @Test + void await_handles_async_completion_with_success() throws ExecutionException, InterruptedException { + CompletableFuture<Result> f1 = new CompletableFuture<>(); + CompletableFuture<Result> f2 = new CompletableFuture<>(); + CompletableFuture<Result> f3 = new CompletableFuture<>(); + + CompletableFuture<List<Result>> awaitPromise = CompletableFuture.supplyAsync(() -> FeedClient.await(f1, f2, f3), executor); + // Completed in reverse order + MyResult r3 = new MyResult(); + f3.complete(r3); + MyResult r2 = new MyResult(); + f2.complete(r2); + MyResult r1 = new MyResult(); + f1.complete(r1); + + List<Result> aggregated = awaitPromise.get(); + assertEquals(3, aggregated.size()); + assertEquals(r1, aggregated.get(0)); + assertEquals(r2, aggregated.get(1)); + assertEquals(r3, aggregated.get(2)); + } + + @Test + void await_throws_when_some_results_completes_exceptionally() { + CompletableFuture<Result> f1 = new CompletableFuture<>(); + DocumentId docId1 = DocumentId.of("music", "music", "doc1"); + FeedException exceptionDoc1 = new FeedException(docId1, "Doc1 failed"); + f1.completeExceptionally(exceptionDoc1); + CompletableFuture<Result> f2 = new CompletableFuture<>(); + DocumentId docId2 = DocumentId.of("music", "music", "doc2"); + FeedException exceptionDoc2 = new FeedException(docId2, "Doc2 failed"); + f2.completeExceptionally(exceptionDoc2); + CompletableFuture<Result> f3 = CompletableFuture.completedFuture(new MyResult()); + + MultiFeedException multiException = assertThrows(MultiFeedException.class, () -> FeedClient.await(f1, f2, f3)); + Set<DocumentId> expectedDocsIds = new HashSet<>(Arrays.asList(docId1, docId2)); + assertEquals(expectedDocsIds, new HashSet<>(multiException.documentIds())); + Set<FeedException> expectedExceptions = new HashSet<>(Arrays.asList(exceptionDoc1, exceptionDoc2)); + assertEquals(expectedExceptions, new HashSet<>(multiException.feedExceptions())); + assertEquals("2 feed operations failed", multiException.getMessage()); + } + + static class MyResult implements Result { + @Override public Type type() { return null; } + @Override public DocumentId documentId() { return null; } + @Override public Optional<String> resultMessage() { return Optional.empty(); } + @Override public Optional<String> traceMessage() { return Optional.empty(); } + } +} |