From 134af4f00cb0302f1ba465e51973fe44e1646e19 Mon Sep 17 00:00:00 2001 From: Bjørn Christian Seime Date: Thu, 9 Dec 2021 16:25:27 +0100 Subject: Remove use of Guava ListenableFuture from com.yahoo.jdisc.handler This change is not 100% API compatible. Many classes from this package inherited types from Guava. The classes will have the same methods as before, but their type has obviously changed. Two options; merge now with the small risk of breakage or wait for Vespa 8 branch. --- .../jdisc/handler/FastContentOutputStream.java | 6 ++-- .../com/yahoo/jdisc/handler/FastContentWriter.java | 36 +++++----------------- .../com/yahoo/jdisc/handler/FutureCompletion.java | 11 ++++--- .../com/yahoo/jdisc/handler/FutureConjunction.java | 31 +++++++++++-------- .../com/yahoo/jdisc/handler/FutureResponse.java | 10 ++++-- .../com/yahoo/jdisc/handler/RequestDispatch.java | 33 +++++++++++--------- .../com/yahoo/jdisc/handler/ResponseDispatch.java | 26 ++++++++++------ 7 files changed, 77 insertions(+), 76 deletions(-) (limited to 'jdisc_core/src/main') diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java index 54e50df5a25..e001db2ab81 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java @@ -1,12 +1,11 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.ListenableFuture; - import java.nio.ByteBuffer; import java.util.Objects; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -20,7 +19,7 @@ import java.util.concurrent.TimeoutException; * * @author Simon Thoresen Hult */ -public class FastContentOutputStream extends AbstractContentOutputStream implements ListenableFuture { +public class FastContentOutputStream extends AbstractContentOutputStream implements Future { private final FastContentWriter out; @@ -78,7 +77,6 @@ public class FastContentOutputStream extends AbstractContentOutputStream impleme return out.get(timeout, unit); } - @Override public void addListener(Runnable listener, Executor executor) { out.addListener(listener, executor); } diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentWriter.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentWriter.java index 596ae07f1d5..7c278c67d59 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentWriter.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentWriter.java @@ -1,16 +1,11 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.SettableFuture; - import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.Objects; -import java.util.concurrent.ExecutionException; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -25,13 +20,12 @@ import java.util.concurrent.atomic.AtomicInteger; * * @author Simon Thoresen Hult */ -public class FastContentWriter implements ListenableFuture, AutoCloseable { +public class FastContentWriter extends CompletableFuture implements AutoCloseable { private final AtomicBoolean closed = new AtomicBoolean(false); private final AtomicInteger numPendingCompletions = new AtomicInteger(); private final CompletionHandler completionHandler = new SimpleCompletionHandler(); private final ContentChannel out; - private final SettableFuture future = SettableFuture.create(); /** *

Creates a new FastContentWriter that encapsulates a given {@link ContentChannel}.

@@ -87,7 +81,7 @@ public class FastContentWriter implements ListenableFuture, AutoCloseab try { out.write(buf, completionHandler); } catch (Throwable t) { - future.setException(t); + completeExceptionally(t); throw t; } } @@ -103,14 +97,13 @@ public class FastContentWriter implements ListenableFuture, AutoCloseab try { out.close(completionHandler); } catch (Throwable t) { - future.setException(t); + completeExceptionally(t); throw t; } } - @Override public void addListener(Runnable listener, Executor executor) { - future.addListener(listener, executor); + whenCompleteAsync((__, ___) -> listener.run(), executor); } @Override @@ -123,34 +116,19 @@ public class FastContentWriter implements ListenableFuture, AutoCloseab return false; } - @Override - public boolean isDone() { - return future.isDone(); - } - - @Override - public Boolean get() throws InterruptedException, ExecutionException { - return future.get(); - } - - @Override - public Boolean get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { - return future.get(timeout, unit); - } - private class SimpleCompletionHandler implements CompletionHandler { @Override public void completed() { numPendingCompletions.decrementAndGet(); if (closed.get() && numPendingCompletions.get() == 0) { - future.set(true); + complete(true); } } @Override public void failed(Throwable t) { - future.setException(t); + completeExceptionally(t); } } } diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureCompletion.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureCompletion.java index ab989b89b1f..a188be6145f 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureCompletion.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureCompletion.java @@ -1,7 +1,8 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.AbstractFuture; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; /** *

This class provides an implementation of {@link CompletionHandler} that allows you to wait for either {@link @@ -13,16 +14,16 @@ import com.google.common.util.concurrent.AbstractFuture; * * @author Simon Thoresen Hult */ -public final class FutureCompletion extends AbstractFuture implements CompletionHandler { +public final class FutureCompletion extends CompletableFuture implements CompletionHandler { @Override public void completed() { - set(true); + complete(true); } @Override public void failed(Throwable t) { - setException(t); + completeExceptionally(t); } @Override @@ -34,4 +35,6 @@ public final class FutureCompletion extends AbstractFuture implements C public final boolean isCancelled() { return false; } + + public void addListener(Runnable r, Executor e) { whenCompleteAsync((__, ___) -> r.run(), e); } } diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureConjunction.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureConjunction.java index 9d0204f33c5..ba304d9e2de 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureConjunction.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureConjunction.java @@ -1,39 +1,46 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.JdkFutureAdapters; -import com.google.common.util.concurrent.ListenableFuture; +import com.yahoo.concurrent.CompletableFutures; import java.util.LinkedList; import java.util.List; -import java.util.concurrent.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** *

This class implements a Future<Boolean> that is conjunction of zero or more other Future<Boolean>s, * i.e. it evaluates to true if, and only if, all its operands evaluate to true. To use this class, - * simply create an instance of it and add operands to it using the {@link #addOperand(ListenableFuture)} method.

- * TODO: consider rewriting usage of FutureConjunction to use CompletableFuture instead. + * simply create an instance of it and add operands to it using the {@link #addOperand(CompletableFuture)} method.

* * @author Simon Thoresen Hult */ -final class FutureConjunction implements ListenableFuture { +final class FutureConjunction implements Future { - private final List> operands = new LinkedList<>(); + private final List> operands = new LinkedList<>(); /** - *

Adds a ListenableFuture<Boolean> to this conjunction. This can be called at any time, even after having called + *

Adds a {@link CompletableFuture} to this conjunction. This can be called at any time, even after having called * {@link #get()} previously.

* * @param operand The operand to add to this conjunction. */ - public void addOperand(ListenableFuture operand) { + public void addOperand(CompletableFuture operand) { operands.add(operand); } - @Override public void addListener(Runnable listener, Executor executor) { - Futures.allAsList(operands).addListener(listener, executor); + CompletableFutures.allOf(operands) + .whenCompleteAsync((__, ___) -> listener.run(), executor); + } + + CompletableFuture completableFuture() { + return CompletableFutures.allOf(operands) + .thenApply(ops -> ops.stream().allMatch(bool -> bool)); } @Override diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureResponse.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureResponse.java index b8073865667..2284c563f50 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureResponse.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureResponse.java @@ -1,16 +1,18 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.AbstractFuture; import com.yahoo.jdisc.Response; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; + /** * This class provides an implementation of {@link ResponseHandler} that allows you to wait for a {@link Response} to * be returned. * * @author Simon Thoresen Hult */ -public final class FutureResponse extends AbstractFuture implements ResponseHandler { +public final class FutureResponse extends CompletableFuture implements ResponseHandler { private final ResponseHandler handler; @@ -38,6 +40,8 @@ public final class FutureResponse extends AbstractFuture implements Re }); } + public void addListener(Runnable r, Executor e) { whenCompleteAsync((__, ___) -> r.run(), e); } + /** *

Constructs a new FutureResponse that calls the given {@link ResponseHandler} when {@link * #handleResponse(Response)} is invoked.

@@ -50,7 +54,7 @@ public final class FutureResponse extends AbstractFuture implements Re @Override public ContentChannel handleResponse(Response response) { - set(response); + complete(response); return handler.handleResponse(response); } diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java index c85aa6375af..c1457290904 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java @@ -1,19 +1,20 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; +import com.yahoo.jdisc.References; import com.yahoo.jdisc.Request; import com.yahoo.jdisc.ResourceReference; import com.yahoo.jdisc.Response; import com.yahoo.jdisc.SharedResource; -import com.yahoo.jdisc.References; import java.nio.ByteBuffer; import java.util.Collections; -import java.util.concurrent.*; -import java.util.ArrayList; -import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** *

This class provides a convenient way of safely dispatching a {@link Request}. Using this class you do not have to @@ -46,7 +47,7 @@ import java.util.List; * * @author Simon Thoresen Hult */ -public abstract class RequestDispatch implements ListenableFuture, ResponseHandler { +public abstract class RequestDispatch implements Future, ResponseHandler { private final FutureConjunction completions = new FutureConjunction(); private final FutureResponse futureResponse = new FutureResponse(this); @@ -106,22 +107,26 @@ public abstract class RequestDispatch implements ListenableFuture, Res * * @return A Future that can be waited for. */ - public final ListenableFuture dispatch() { + public final CompletableFuture dispatch() { try (FastContentWriter writer = new FastContentWriter(connect())) { for (ByteBuffer buf : requestContent()) { writer.write(buf); } completions.addOperand(writer); } - return this; + return CompletableFuture.allOf(completions.completableFuture(), futureResponse) + .thenApply(__ -> { + try { + return futureResponse.get(); + } catch (InterruptedException | ExecutionException e) { + throw new IllegalStateException(e); // Should not happens since both futures are complete + } + }); } - @Override public void addListener(Runnable listener, Executor executor) { - List> combined = new ArrayList<>(2); - combined.add(completions); - combined.add(futureResponse); - Futures.allAsList(combined).addListener(listener, executor); + CompletableFuture.allOf(completions.completableFuture(), futureResponse) + .whenCompleteAsync((__, ___) -> listener.run(), executor); } @Override diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/ResponseDispatch.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/ResponseDispatch.java index 377c8ecf4a9..9387171c1ac 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/ResponseDispatch.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/ResponseDispatch.java @@ -1,15 +1,17 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.ForwardingListenableFuture; -import com.google.common.util.concurrent.ListenableFuture; import com.yahoo.jdisc.Response; import com.yahoo.jdisc.SharedResource; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Collections; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** *

This class provides a convenient way of safely dispatching a {@link Response}. It is similar in use to {@link @@ -34,7 +36,7 @@ import java.util.concurrent.Future; * * @author Simon Thoresen Hult */ -public abstract class ResponseDispatch extends ForwardingListenableFuture { +public abstract class ResponseDispatch implements Future { private final FutureConjunction completions = new FutureConjunction(); @@ -90,19 +92,14 @@ public abstract class ResponseDispatch extends ForwardingListenableFuture dispatch(ResponseHandler responseHandler) { + public final CompletableFuture dispatch(ResponseHandler responseHandler) { try (FastContentWriter writer = new FastContentWriter(connect(responseHandler))) { for (ByteBuffer buf : responseContent()) { writer.write(buf); } completions.addOperand(writer); } - return this; - } - - @Override - protected final ListenableFuture delegate() { - return completions; + return completions.completableFuture(); } @Override @@ -115,6 +112,15 @@ public abstract class ResponseDispatch extends ForwardingListenableFutureFactory method for creating a ResponseDispatch with a {@link Response} that has the given status code, and * ByteBuffer content.

-- cgit v1.2.3