From 150aff6805eaa1b23a72bad7b01b89cee72ea6cf Mon Sep 17 00:00:00 2001 From: Bjørn Christian Seime Date: Thu, 24 Jun 2021 17:10:28 +0200 Subject: Implement dryrun in FeedClient, use it from CLI and Hadoop feeder --- .../java/ai/vespa/feed/client/DryrunCluster.java | 42 ++++++++++++++++++++++ .../ai/vespa/feed/client/FeedClientBuilder.java | 6 ++++ .../ai/vespa/feed/client/HttpRequestStrategy.java | 2 +- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 vespa-feed-client/src/main/java/ai/vespa/feed/client/DryrunCluster.java (limited to 'vespa-feed-client/src/main') diff --git a/vespa-feed-client/src/main/java/ai/vespa/feed/client/DryrunCluster.java b/vespa-feed-client/src/main/java/ai/vespa/feed/client/DryrunCluster.java new file mode 100644 index 00000000000..9e6ad0150c7 --- /dev/null +++ b/vespa-feed-client/src/main/java/ai/vespa/feed/client/DryrunCluster.java @@ -0,0 +1,42 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.feed.client; + +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.util.concurrent.CompletableFuture; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Dryrun implementation that reports every request/operation as successful + * + * @author bjorncs + */ +class DryrunCluster implements Cluster { + + private final static Logger log = Logger.getLogger(DryrunCluster.class.getName()); + + static final Duration DELAY = Duration.ofMillis(1); + + @Override + public void dispatch(HttpRequest request, CompletableFuture vessel) { + long millis = DELAY.toMillis(); + log.log(Level.FINE, "Dryrun of request '{0}' with delay of {1}ms", new Object[]{request, millis}); + if (millis > 0) { + try { + Thread.sleep(millis); + } catch (InterruptedException e) { + vessel.cancel(true); + Thread.currentThread().interrupt(); + return; + } + } + vessel.complete(new SimpleOkResponse()); + } + + private static class SimpleOkResponse implements HttpResponse { + @Override public int code() { return 200; } + @Override public byte[] body() { return "{\"message\":\"dummy dryrun message\"}".getBytes(StandardCharsets.UTF_8); } + } + +} diff --git a/vespa-feed-client/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java b/vespa-feed-client/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java index 0f685ec5b7f..57aaf67c2d9 100644 --- a/vespa-feed-client/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java +++ b/vespa-feed-client/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java @@ -45,6 +45,7 @@ public class FeedClientBuilder { PrivateKey privateKey; Collection caCertificates; boolean benchmark; + boolean dryrun; /** Creates a builder for a single container endpoint **/ public static FeedClientBuilder create(URI endpoint) { return new FeedClientBuilder(Collections.singletonList(endpoint)); } @@ -158,6 +159,11 @@ public class FeedClientBuilder { return setCertificate(Collections.singletonList(certificate), privateKey); } + public FeedClientBuilder setDryrun(boolean enabled) { + this.dryrun = enabled; + return this; + } + /** * Overrides JVM default SSL truststore * @param caCertificatesFile Path to PEM encoded file containing trusted certificates diff --git a/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpRequestStrategy.java b/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpRequestStrategy.java index 1987bae18f9..1b939a0361c 100644 --- a/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpRequestStrategy.java +++ b/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpRequestStrategy.java @@ -59,7 +59,7 @@ class HttpRequestStrategy implements RequestStrategy { }); HttpRequestStrategy(FeedClientBuilder builder) throws IOException { - this(builder, new ApacheCluster(builder)); + this(builder, builder.dryrun ? new DryrunCluster() : new ApacheCluster(builder)); } HttpRequestStrategy(FeedClientBuilder builder, Cluster cluster) { -- cgit v1.2.3