diff --git a/connector/axon-server-connector/src/main/java/io/axoniq/framework/axonserver/connector/query/FlowControlledResponseSender.java b/connector/axon-server-connector/src/main/java/io/axoniq/framework/axonserver/connector/query/FlowControlledResponseSender.java index 85e18e735a..e93cbea53d 100644 --- a/connector/axon-server-connector/src/main/java/io/axoniq/framework/axonserver/connector/query/FlowControlledResponseSender.java +++ b/connector/axon-server-connector/src/main/java/io/axoniq/framework/axonserver/connector/query/FlowControlledResponseSender.java @@ -11,12 +11,16 @@ import io.axoniq.axonserver.connector.FlowControl; import io.axoniq.axonserver.connector.ReplyChannel; import io.axoniq.axonserver.grpc.query.QueryResponse; +import io.axoniq.framework.messaging.distributed.CallbackHandOff; import org.axonframework.common.annotation.Internal; import org.axonframework.conversion.Converter; import org.axonframework.messaging.core.MessageStream; import org.axonframework.messaging.queryhandling.QueryResponseMessage; import org.jspecify.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.lang.invoke.MethodHandles; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; @@ -32,6 +36,8 @@ @Internal class FlowControlledResponseSender implements FlowControl { + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private final String clientId; private final String queryIdentifier; private final MessageStream upstream; @@ -73,7 +79,8 @@ public void request(long requested) { if (requests.getAndUpdate(current -> current > Long.MAX_VALUE - requested ? Long.MAX_VALUE : current + requested) == 0) { - upstream.setCallback(this::responseSendingLoop); + // The stream calls back on the thread of whoever produced a response, which may not read from it, or block + upstream.setCallback(CallbackHandOff.handOff(this::responseSendingLoop)); } responseSendingLoop(); } @@ -123,11 +130,39 @@ private void sendResponses() { } ); } + } catch (RuntimeException e) { + fail(e); } finally { sendingGate.set(false); } } + /* + * Ends the query with the given failure, as nothing reads the stream after this, and nobody else would tell + * the client: it would wait for the query to time out. + */ + private void fail(RuntimeException failure) { + if (closed.getAndSet(true)) { + // The terminal response was already being sent when this failed; there is nothing left to tell the client + logger.warn("Failed to complete the response to query [{}].", queryIdentifier, failure); + return; + } + + upstream.close(); + + try { + downstream.sendLast(buildErrorResponse(clientId, queryIdentifier, failure, converter)); + } catch (RuntimeException e) { + // Most likely the failure's details could not be converted: retry without them + try { + downstream.sendLast(buildErrorResponse(clientId, queryIdentifier, failure, null)); + } catch (RuntimeException ex) { + ex.addSuppressed(failure); + logger.warn("Failed to report a failure to the client for query [{}].", queryIdentifier, ex); + } + } + } + @Override public void cancel() { upstream.close(); diff --git a/connector/axon-server-connector/src/test/java/io/axoniq/framework/axonserver/connector/query/FlowControlledResponseSenderTest.java b/connector/axon-server-connector/src/test/java/io/axoniq/framework/axonserver/connector/query/FlowControlledResponseSenderTest.java index 9663a2e304..4fa6f1ed42 100644 --- a/connector/axon-server-connector/src/test/java/io/axoniq/framework/axonserver/connector/query/FlowControlledResponseSenderTest.java +++ b/connector/axon-server-connector/src/test/java/io/axoniq/framework/axonserver/connector/query/FlowControlledResponseSenderTest.java @@ -21,19 +21,25 @@ import org.axonframework.messaging.queryhandling.QueryResponseMessage; import org.jspecify.annotations.NonNull; import org.junit.jupiter.api.*; +import org.mockito.ArgumentCaptor; import java.nio.charset.StandardCharsets; +import java.time.Duration; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; import static org.mockito.Mockito.*; class FlowControlledResponseSenderTest { public static final byte[] PAYLOAD = "test".getBytes(StandardCharsets.UTF_8); + + // What the sender does when its stream signals happens on a thread of its own, which the tests wait for + private static final long AWAIT_MILLIS = 5000; private QueueMessageStream upstream; private ReplyChannel stubDownstream; private FlowControlledResponseSender testSubject; @@ -56,8 +62,8 @@ void shouldSendAllAlreadyAvailableMessagesUntilCompletionOnUnlimitedPermits() { } upstream.seal(); testSubject.request(Long.MAX_VALUE); + verify(stubDownstream, timeout(AWAIT_MILLIS)).complete(); verify(stubDownstream, times(10)).send(any()); - verify(stubDownstream).complete(); } @Test @@ -67,8 +73,8 @@ void shouldSendMessagesUntilCompletionOnUnlimitedPermitsWhenMessagesBecomeAvaila upstream.offer(newMessage(), Context.empty()); } upstream.seal(); + verify(stubDownstream, timeout(AWAIT_MILLIS)).complete(); verify(stubDownstream, times(10)).send(any()); - verify(stubDownstream).complete(); } @Test @@ -79,8 +85,8 @@ void shouldNotOverflowOnMoreThanMaxLongRequests() { upstream.offer(newMessage(), Context.empty()); } upstream.seal(); + verify(stubDownstream, timeout(AWAIT_MILLIS)).complete(); verify(stubDownstream, times(10)).send(any()); - verify(stubDownstream).complete(); } @Test @@ -91,9 +97,9 @@ void shouldHonorTheFlowRequestsWhenEnoughMessagesAreAvailable() { upstream.seal(); for (int i = 0; i < 10; i++) { testSubject.request(1); - verify(stubDownstream, times(i + 1)).send(any()); + verify(stubDownstream, timeout(AWAIT_MILLIS).times(i + 1)).send(any()); } - verify(stubDownstream).complete(); + verify(stubDownstream, timeout(AWAIT_MILLIS)).complete(); } @Test @@ -103,10 +109,10 @@ void shouldTriggerCloseWhenUpstreamCompletesAfterSendingAllRequestedMessages() { } for (int i = 0; i < 10; i++) { testSubject.request(1); - verify(stubDownstream, times(i + 1)).send(any()); + verify(stubDownstream, timeout(AWAIT_MILLIS).times(i + 1)).send(any()); } upstream.seal(); - verify(stubDownstream).complete(); + verify(stubDownstream, timeout(AWAIT_MILLIS)).complete(); } @Test @@ -115,22 +121,22 @@ void shouldOnlySendMessagesWhenRequested() { upstream.offer(newMessage(), Context.empty()); testSubject.request(1); - verify(stubDownstream, times(1)).send(any()); + verify(stubDownstream, timeout(AWAIT_MILLIS).times(1)).send(any()); testSubject.request(2); - verify(stubDownstream, times(2)).send(any()); + verify(stubDownstream, timeout(AWAIT_MILLIS).times(2)).send(any()); // there is 1 outstanding request for this message upstream.offer(newMessage(), Context.empty()); - verify(stubDownstream, times(3)).send(any()); + verify(stubDownstream, timeout(AWAIT_MILLIS).times(3)).send(any()); testSubject.request(1); // this message won't arrive upstream.seal(); - // still 3 messages sent + // still 3 messages sent, once the stream was seen to be completed + verify(stubDownstream, timeout(AWAIT_MILLIS)).complete(); verify(stubDownstream, times(3)).send(any()); - verify(stubDownstream).complete(); } @Test @@ -144,8 +150,8 @@ void shouldSendLastMessageWithErrorWhenUpstreamCompletesExceptionally() { // this should also trigger the completion testSubject.request(1); - verify(stubDownstream, times(1)).send(any()); - verify(stubDownstream).sendLast(assertArg(e -> { + verify(stubDownstream, timeout(AWAIT_MILLIS).times(1)).send(any()); + verify(stubDownstream, timeout(AWAIT_MILLIS)).sendLast(assertArg(e -> { assertThat(e.getErrorMessage().getMessage()).isEqualTo("Custom message"); assertThat(e.getErrorMessage().getErrorCode()).isEqualTo(ErrorCategory.QUERY_EXECUTION_ERROR.errorCode()); assertThat(e.getErrorCode()).isEqualTo(ErrorCategory.QUERY_EXECUTION_ERROR.errorCode()); @@ -164,24 +170,56 @@ void shouldSendLastMessageWithDetailsPayloadWhenUpstreamCompletesWithHandlerExec detailsTestSubject.request(1); - verify(stubDownstream).sendLast(assertArg(e -> { + verify(stubDownstream, timeout(AWAIT_MILLIS)).sendLast(assertArg(e -> { assertThat(e.getErrorMessage().getMessage()).isEqualTo("Custom message"); assertThat(e.getPayload().getData().toStringUtf8()).isEqualTo("Some details"); })); } + @Test + void shouldEndTheQueryWithAnErrorAndCloseUpstreamWhenSendingAResponseFails() { + doThrow(new IllegalStateException("cannot send")).when(stubDownstream).send(any()); + upstream.offer(newMessage(), Context.empty()); + + testSubject.request(1); + + verify(stubDownstream, timeout(AWAIT_MILLIS)).sendLast(assertArg(e -> { + assertThat(e.getRequestIdentifier()).isEqualTo("test"); + assertThat(e.getErrorMessage().getMessage()).isEqualTo("cannot send"); + assertThat(e.getErrorCode()).isEqualTo(ErrorCategory.QUERY_EXECUTION_ERROR.errorCode()); + })); + assertThat(upstream.isCompleted()).isTrue(); + verify(stubDownstream, never()).complete(); + } + + @Test + void shouldReportAFailureWithoutDetailsWhenTheDetailsCannotBeConverted() { + doThrow(new IllegalStateException("cannot send")).when(stubDownstream).send(any()); + doThrow(new IllegalArgumentException("cannot convert details")).doNothing().when(stubDownstream).sendLast(any()); + upstream.offer(newMessage(), Context.empty()); + + testSubject.request(1); + + ArgumentCaptor responses = ArgumentCaptor.forClass(QueryResponse.class); + + verify(stubDownstream, timeout(AWAIT_MILLIS).times(2)).sendLast(responses.capture()); + + assertThat(responses.getAllValues().get(1).getErrorMessage().getMessage()).isEqualTo("cannot send"); + assertThat(responses.getAllValues().get(1).hasPayload()).isFalse(); + } + @Test void shouldSendEmptySentinelResponseWhenUpstreamCompletesWithoutAnyMessage() { upstream.seal(); testSubject.request(Long.MAX_VALUE); - verify(stubDownstream, never()).send(any()); - verify(stubDownstream, never()).complete(); - verify(stubDownstream).sendLast(assertArg(response -> { + verify(stubDownstream, timeout(AWAIT_MILLIS)).sendLast(assertArg(response -> { assertThat(response.getRequestIdentifier()).isEqualTo("test"); assertThat(response.getPayload().getType()).isEqualTo("empty"); assertThat(response.getPayload().getData().isEmpty()).isTrue(); })); + verify(stubDownstream, never()).send(any()); + verify(stubDownstream, never()).complete(); } @Test @@ -232,8 +270,10 @@ void shouldNeverLoseTheResponseWhenResultAndPermitsArriveConcurrently() throws E } } - // then - assertThat(sent.get()).isEqualTo(iterations); - assertThat(completed.get()).isEqualTo(iterations); + // then, once the sending that was handed off has caught up + await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> { + assertThat(sent.get()).isEqualTo(iterations); + assertThat(completed.get()).isEqualTo(iterations); + }); } } diff --git a/connector/axoniq-springcloud/src/main/java/io/axoniq/framework/springcloud/query/IncomingQueryInvoker.java b/connector/axoniq-springcloud/src/main/java/io/axoniq/framework/springcloud/query/IncomingQueryInvoker.java index 588115a143..55f34fa33d 100644 --- a/connector/axoniq-springcloud/src/main/java/io/axoniq/framework/springcloud/query/IncomingQueryInvoker.java +++ b/connector/axoniq-springcloud/src/main/java/io/axoniq/framework/springcloud/query/IncomingQueryInvoker.java @@ -8,6 +8,7 @@ package io.axoniq.framework.springcloud.query; +import io.axoniq.framework.messaging.distributed.CallbackHandOff; import io.axoniq.framework.messaging.queryhandling.distributed.QueryBusConnector; import io.axoniq.framework.messaging.queryhandling.distributed.QueryBusConnector.Handler; import org.axonframework.common.FutureUtils; @@ -29,6 +30,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.locks.ReentrantLock; import java.util.function.Supplier; /** @@ -92,8 +94,8 @@ public void bind(QueryBusConnector.Handler handler) { * Handles the given {@code request}, received from another member, writing its responses to the given * {@code sink}. *

- * Returns as soon as the query has been handed to the local handler. Responses are written to the {@code sink} as - * the handler produces them, which for a handler that answers asynchronously is after this method returned. + * Returns as soon as the query has been handed to the local handler. The responses are written to the + * {@code sink} later, on another thread, whenever the handler's stream has some. * * @param request the request received from another member * @param sink receives the responses to the query, and the outcome that ends them @@ -322,7 +324,8 @@ public CompletableFuture completeExceptionally(Throwable cause) { /** * Drains a response stream into a sink, as and when the stream has something to write. *

- * The stream reports availability on whichever thread produced it, so draining is serialised here: two threads + * The stream calls back on the thread of whoever produced a response, which only schedules a drain on a thread of + * its own. Drains are serialised here: a signal arriving while one runs schedules the next, and two of them * writing to one sink would interleave the responses of a single query. */ private class ResponseWriter { @@ -330,8 +333,11 @@ private class ResponseWriter { private final String requestIdentifier; private final MessageStream responses; private final QueryResponseSink sink; - // Volatile rather than guarded by this writer's monitor, so that cancelling does not wait on a drain that - // is blocked writing to a member which has already stopped reading. + // A drain blocks while writing to the sink, and a virtual thread that blocks inside a monitor holds on to its + // carrier thread on JDK 21, so drains are serialised with a lock. + private final ReentrantLock drainLock = new ReentrantLock(); + // Volatile, so that cancelling does not wait on a drain that is blocked writing to a member which has already + // stopped reading. private volatile boolean terminated = false; private ResponseWriter(String requestIdentifier, @@ -344,8 +350,9 @@ private ResponseWriter(String requestIdentifier, private void start() { sink.onUnavailable(this::cancel); - responses.setCallback(this::drain); - drain(); + // The stream calls back on the thread of whoever produced a response, which may not read from it, or block. + // It also calls back at once when it has a response already, or has ended, so nothing is drained here. + responses.setCallback(CallbackHandOff.handOff(this::drain)); } /** @@ -363,34 +370,38 @@ private void cancel() { responses.close(); } - private synchronized void drain() { - if (terminated) { - return; - } + private void drain() { + drainLock.lock(); + try { - // Asked once per decision: a second ask can take a response that arrived in between, swallowing the - // notification that would have drained it, and leave it unwritten. - boolean hasNext = responses.hasNextAvailable(); - while (hasNext) { - Optional> next = responses.next(); - if (next.isEmpty()) { - hasNext = false; - break; - } - sink.response(QueryConverter.convertResponseMessage(next.get().message(), requestIdentifier)); - hasNext = responses.hasNextAvailable(); + if (terminated) { + return; } - // Reading a stream that has just ended reports its completion, which drains it again from inside - // this drain. That nested drain already finished the answer, so finishing it here would do so twice. - if (!hasNext && !terminated && responses.isCompleted()) { - responses.error().ifPresentOrElse(this::fail, this::finish); + try { + // Reads until the stream has nothing available: a response it reported and that is left unread + // is not announced by another callback. + while (responses.hasNextAvailable()) { + Optional> next = responses.next(); + if (next.isEmpty()) { + break; + } + sink.response(QueryConverter.convertResponseMessage(next.get().message(), requestIdentifier)); + } + // Reading a stream that has just ended reports its completion, which schedules another drain. + // That one waits for this one and then finds the answer finished, so it is finished only once. + if (!terminated && responses.isCompleted()) { + responses.error().ifPresentOrElse(this::fail, this::finish); + } + } catch (Exception e) { + // Writing failed, which usually means the member that asked is gone. Reporting the failure back is + // pointless in that case, so the stream is simply released. + logger.debug("Could not write the responses to query [{}]; abandoning it.", requestIdentifier, e); + terminated = true; + responses.close(); } - } catch (Exception e) { - // Writing failed, which usually means the member that asked is gone. Reporting the failure back is - // pointless in that case, so the stream is simply released. - logger.debug("Could not write the responses to query [{}]; abandoning it.", requestIdentifier, e); - terminated = true; - responses.close(); + } + finally { + drainLock.unlock(); } } diff --git a/connector/axoniq-springcloud/src/main/java/io/axoniq/framework/springcloud/query/SpringCloudQueryBusConnector.java b/connector/axoniq-springcloud/src/main/java/io/axoniq/framework/springcloud/query/SpringCloudQueryBusConnector.java index c7784fe6d8..f9a9a150c2 100644 --- a/connector/axoniq-springcloud/src/main/java/io/axoniq/framework/springcloud/query/SpringCloudQueryBusConnector.java +++ b/connector/axoniq-springcloud/src/main/java/io/axoniq/framework/springcloud/query/SpringCloudQueryBusConnector.java @@ -8,6 +8,7 @@ package io.axoniq.framework.springcloud.query; +import io.axoniq.framework.messaging.distributed.CallbackHandOff; import io.axoniq.framework.messaging.queryhandling.distributed.QueryBusConnector; import io.axoniq.framework.springcloud.query.RemoteQueryDispatcher.SubscriptionListener; import io.axoniq.framework.springcloud.routing.Member; @@ -49,6 +50,7 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.ReentrantLock; /** * A {@link QueryBusConnector} distributing queries over the members a Spring Cloud discovery implementation reports. @@ -212,29 +214,31 @@ private SubscriptionUpdates(QueryMessage query, private void add(Member member, MessageStream updates) { Source source = new Source(member, updates); sources.add(source); - updates.setCallback(() -> drain(source)); - drain(source); + // The stream calls back on the thread of whoever produced an update, which may not read from it, or block. + // It also calls back at once when it has an update already, or has ended, so nothing is drained here. + updates.setCallback(CallbackHandOff.handOff(() -> drain(source))); } /** * Moves what the given {@code source} has available onto the merged stream. *

- * Synchronised on the source, because a stream reports availability on whichever thread produced the update - * and two of those draining the same source would take entries out from under each other. + * Serialised per source by its lock, because a signal arriving while a drain runs schedules the next one, and + * two of them draining the same source would take entries out from under each other. It is a lock as a virtual + * thread that waits for a monitor holds on to its carrier thread on JDK 21. */ private void drain(Source source) { - synchronized (source) { + source.lock.lock(); + + try { if (source.counted) { return; } try { - // Asked once per decision: a second ask can take an update that arrived in between, swallowing - // the notification that would have drained it, and leave it stranded. - boolean hasNext = source.stream.hasNextAvailable(); - while (hasNext) { + // Reads until the stream has nothing available: an update it reported and that is left unread is + // not announced by another callback. + while (source.stream.hasNextAvailable()) { Optional> next = source.stream.next(); if (next.isEmpty()) { - hasNext = false; break; } if (!merged.offer(next.get().message(), Context.empty())) { @@ -245,9 +249,8 @@ private void drain(Source source) { ))); return; } - hasNext = source.stream.hasNextAvailable(); } - if (!hasNext && source.stream.isCompleted()) { + if (source.stream.isCompleted()) { // A stream that ends without a failure is one whose member said it is leaving: the // dispatcher fails a stream that ends without saying why. A member reporting the subscription // over has already ended the merged updates, so counting it changes nothing. @@ -259,15 +262,18 @@ private void drain(Source source) { end(source, () -> fail(e)); } } + finally { + source.lock.unlock(); + } } /** * Accounts for the given {@code source} having ended, unless it already has been. *

- * Reading a stream that has just been sealed reports its completion, which drains that same source again - * from inside this drain. Checking on the way in is not enough, because the nested drain reaches the end - * first and the outer one has already passed that check: both would count the same member, taking the tally - * past zero and cutting off the members still holding subscriptions. + * Reading a stream that has just been sealed reports its completion, which schedules another drain of that + * same source. Checking on the way in is not enough, because that drain can reach the end first once this one + * has released the source, and this one has already passed the check: both would count the same member, taking + * the tally past zero and cutting off the members still holding subscriptions. */ private static void end(Source source, Runnable ended) { if (!source.counted) { @@ -339,7 +345,8 @@ private static final class Source { private final Member member; private final MessageStream stream; - // Guarded by this source's monitor, which every drain holds. + // Guarded by the lock, which every drain holds. + private final ReentrantLock lock = new ReentrantLock(); private boolean counted; private Source(Member member, MessageStream stream) { diff --git a/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/HttpRemoteQueryDispatcherTest.java b/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/HttpRemoteQueryDispatcherTest.java index 3fe652d321..0370416dc4 100644 --- a/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/HttpRemoteQueryDispatcherTest.java +++ b/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/HttpRemoteQueryDispatcherTest.java @@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.json.JsonMapper; +import io.axoniq.framework.messaging.distributed.CallbackHandOff; import io.axoniq.framework.springcloud.query.RemoteQueryDispatcher.SubscriptionListener; import io.axoniq.framework.springcloud.routing.Member; import org.axonframework.common.ExceptionUtils; @@ -46,6 +47,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.Executors; @@ -603,13 +605,16 @@ void stopsReadingTheMemberOnceTheBufferOverflowed() throws IOException { @Test void carriesOnWhenTheConsumerKeepsUp() throws IOException { // given the same small buffer, and a consumer draining as responses arrive - List received = new ArrayList<>(); + List received = new CopyOnWriteArrayList<>(); MessageStream responses = dispatcher(2).dispatch(MEMBER, query()); - responses.setCallback(() -> drainInto(responses, received)); + responses.setCallback(CallbackHandOff.handOff(() -> drainInto(responses, received))); - // when more responses than the buffer holds are streamed + // when more responses than the buffer holds are streamed, each one once the consumer has taken the last for (int i = 1; i <= 20; i++) { body.write(responseEvent("response-" + i)); + + int expected = i; + awaitUntil(() -> received.size() == expected); } body.end(); diff --git a/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/IncomingQueryInvokerTest.java b/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/IncomingQueryInvokerTest.java index 6d9c7b67e5..1214cf87cb 100644 --- a/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/IncomingQueryInvokerTest.java +++ b/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/IncomingQueryInvokerTest.java @@ -22,6 +22,9 @@ import org.axonframework.messaging.queryhandling.SubscriptionQueryUpdateMessage; import org.junit.jupiter.api.*; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; @@ -33,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.awaitility.Awaitility.await; /** * Tests how {@link IncomingQueryInvoker} answers a query from another member, and how it reports what it cannot @@ -58,6 +62,11 @@ void setUp() { testSubject.bind(handler); } + // The responses are written on a thread of the invoker, after handle() returned + private void awaitTheEndOfTheAnswer() { + await().until(() -> sink.completed() || sink.error() != null); + } + private static QueryDispatchRequest request() { return new QueryDispatchRequest("query-1", FIND_COURSE_TYPE.toString(), @@ -88,6 +97,7 @@ void writesEveryResponseTheHandlerProduces() { // when testSubject.handle(request(), sink); + awaitTheEndOfTheAnswer(); // then assertThat(sink.responses()).extracting(QueryDispatchResponse::identifier) @@ -103,6 +113,7 @@ void namesTheQueryEveryResponseAnswers() { // when testSubject.handle(request(), sink); + awaitTheEndOfTheAnswer(); // then the member that asked matches the responses to the query it sent assertThat(sink.responses()).singleElement() @@ -114,6 +125,7 @@ void namesTheQueryEveryResponseAnswers() { void completesWithoutResponsesWhenTheHandlerProducesNone() { // when — a query with no answer is answered, just not with anything testSubject.handle(request(), sink); + awaitTheEndOfTheAnswer(); // then assertThat(sink.responses()).isEmpty(); @@ -178,9 +190,11 @@ public MessageStream query(QueryMessage query) { responses.seal(); // then - assertThat(sink.responses()).extracting(QueryDispatchResponse::identifier) - .containsExactly("response-1", "response-2"); - assertThat(sink.completed()).isTrue(); + await().untilAsserted(() -> { + assertThat(sink.responses()).extracting(QueryDispatchResponse::identifier) + .containsExactly("response-1", "response-2"); + assertThat(sink.completed()).isTrue(); + }); } @Test @@ -196,8 +210,7 @@ public MessageStream query(QueryMessage query) { } }); int iterations = 20_000; - int written = 0; - int completed = 0; + List sinks = new ArrayList<>(); try (ExecutorService handlerThread = Executors.newSingleThreadExecutor()) { for (int i = 0; i < iterations; i++) { @@ -217,14 +230,13 @@ public MessageStream query(QueryMessage query) { testSubject.handle(request(), writing); answer.get(5, TimeUnit.SECONDS); - written += writing.responses().size(); - completed += writing.completed() ? 1 : 0; + sinks.add(writing); } } - // then - assertThat(written).isEqualTo(iterations); - assertThat(completed).isEqualTo(iterations); + // then every answer was written, which happens on threads of the invoker, so it is waited for + await().atMost(Duration.ofSeconds(30)).until(() -> sinks.stream().allMatch(RecordingQueryResponseSink::completed)); + assertThat(sinks.stream().mapToInt(writing -> writing.responses().size()).sum()).isEqualTo(iterations); } } @@ -263,13 +275,17 @@ void writesNothingFurther() { // given testSubject.handle(request(), sink); responses.offer(response("response-1"), Context.empty()); + await().until(() -> sink.responses().size() == 1); sink.becomeUnavailable(); // when a response arrives after the member stopped reading responses.offer(response("response-2"), Context.empty()); - // then - assertThat(sink.responses()).extracting(QueryDispatchResponse::identifier).containsExactly("response-1"); + // then nothing further is written, however long it is given + await().during(Duration.ofMillis(200)).untilAsserted( + () -> assertThat(sink.responses()).extracting(QueryDispatchResponse::identifier) + .containsExactly("response-1") + ); } @Test @@ -291,6 +307,7 @@ void leavesAQueryAnsweredInFullAlone() { testSubject.handle(request(), sink); responses.offer(response("response-1"), Context.empty()); responses.seal(); + await().until(sink::completed); // when the container reports completion, as it does for every stream it closes sink.becomeUnavailable(); @@ -577,8 +594,9 @@ public void error(QueryDispatchFailure error) { // when testSubject.handle(request(), countingSink); - // then - assertThat(failures).hasValue(1); + // then the failure is reported once, however many drains saw the stream end + await().during(Duration.ofMillis(200)) + .untilAsserted(() -> assertThat(failures).hasValue(1)); } @Test @@ -588,6 +606,7 @@ void reportsAFailingHandler() { // when testSubject.handle(request(), sink); + awaitTheEndOfTheAnswer(); // then assertThat(sink.error()).isNotNull(); @@ -602,6 +621,7 @@ void namesTheMemberAFailureOccurredOn() { // when testSubject.handle(request(), sink); + awaitTheEndOfTheAnswer(); // then assertThat(sink.error()).isNotNull(); diff --git a/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/SpringCloudQueryBusConnectorTest.java b/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/SpringCloudQueryBusConnectorTest.java index f0e1adf0f9..5141a1cacb 100644 --- a/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/SpringCloudQueryBusConnectorTest.java +++ b/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/SpringCloudQueryBusConnectorTest.java @@ -53,6 +53,8 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.locks.LockSupport; +import java.util.function.Predicate; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -119,6 +121,26 @@ private static List drain(MessageStream stream) { return collected; } + /** + * Drains the stream until what it has carried so far satisfies {@code done}, or it takes too long. The connector + * moves the updates of the members onto the stream on a thread of its own, so they arrive some time after the + * member emitted them. + */ + private static List drainUntil(MessageStream stream, Predicate> done) { + List collected = new ArrayList<>(); + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5); + + while (true) { + collected.addAll(drain(stream)); + + if (done.test(collected) || System.nanoTime() > deadline) { + return collected; + } + + LockSupport.parkNanos(50_000); + } + } + private static SubscriptionQueryUpdateMessage update(String identifier) { return new GenericSubscriptionQueryUpdateMessage( new GenericMessage(identifier, RESPONSE_TYPE, PAYLOAD, Map.of()) @@ -502,9 +524,11 @@ void failsWithoutAskingForTheInitialResultWhenAnotherMembersSubscriptionCannotBe new QueryExecutionException("node-c is unreachable.", null)); // then the subscription could never be complete, so no initial result is asked for - drain(responses); + await().untilAsserted(() -> { + drain(responses); + assertThat(responses.error()).isPresent(); + }); assertThat(dispatcher.dispatches()).isEmpty(); - assertThat(responses.error()).isPresent(); assertThat(responses.error().orElseThrow()).hasMessageContaining("node-c is unreachable."); } @@ -523,7 +547,7 @@ void deliversAnUpdateEmittedWhileTheInitialResultIsProducedAfterThatResult() { received.addAll(drain(responses)); initialResult.offer(response("initial-1"), Context.empty()); initialResult.seal(); - received.addAll(drain(responses)); + received.addAll(drainUntil(responses, drained -> received.size() + drained.size() >= 2)); // then the update is not lost, and arrives after the initial result it may or may not be part of assertThat(received).extracting(QueryResponseMessage::identifier) @@ -545,9 +569,13 @@ void asksForTheInitialResultAndCompletesWhenAMemberReportsItOverBeforeEveryMembe // then no member left to register can emit an update that matters, so the initial result is the // final state, and the subscription completes after it rather than failing - assertThat(drain(responses)).extracting(QueryResponseMessage::identifier) - .containsExactly("initial-1"); - assertThat(responses.isCompleted()).isTrue(); + List received = new ArrayList<>(); + + await().untilAsserted(() -> { + received.addAll(drain(responses)); + assertThat(responses.isCompleted()).isTrue(); + }); + assertThat(received).extracting(QueryResponseMessage::identifier).containsExactly("initial-1"); assertThat(responses.error()).isEmpty(); } } @@ -562,9 +590,11 @@ void failsWhenTheAnsweringMembersSubscriptionCannotBeOpened() { MessageStream responses = testSubject.subscriptionQuery(query(), null, 16); // then the initial result is not left waiting on a subscription that will never open - drain(responses); + await().untilAsserted(() -> { + drain(responses); + assertThat(responses.error()).isPresent(); + }); assertThat(dispatcher.dispatches()).isEmpty(); - assertThat(responses.error()).isPresent(); assertThat(responses.error().orElseThrow()).hasMessageContaining("node-b is unreachable."); } @@ -578,7 +608,7 @@ void answersWithTheInitialResultBeforeTheUpdates() { MessageStream responses = testSubject.subscriptionQuery(query(), null, 16); List received = new ArrayList<>(drain(responses)); dispatcher.emit(remoteMember(), response("update-1")); - received.addAll(drain(responses)); + received.addAll(drainUntil(responses, updates -> !updates.isEmpty())); // then assertThat(received).extracting(QueryResponseMessage::identifier) @@ -597,8 +627,9 @@ void carriesTheUpdatesOfEveryMember() { dispatcher.emit(otherRemoteMember(), response("update-from-c")); // then a subscriber sees one stream of updates however many members produce them - assertThat(drain(responses)).extracting(QueryResponseMessage::identifier) - .containsExactlyInAnyOrder("update-from-b", "update-from-c"); + assertThat(drainUntil(responses, updates -> updates.size() >= 2)) + .extracting(QueryResponseMessage::identifier) + .containsExactlyInAnyOrder("update-from-b", "update-from-c"); } @Test @@ -631,7 +662,9 @@ void carriesAnUpdateEmittedWhileTheSubscriptionIsBeingOpened() throws Exception MessageStream responses = connector.subscriptionQuery(query(), null, 16); emitting.get(5, TimeUnit.SECONDS); - if (drain(responses).stream().anyMatch(update -> update.identifier().equals("update-1"))) { + if (drainUntil(responses, updates -> updates.stream().anyMatch( + update -> update.identifier().equals("update-1") + )).stream().anyMatch(update -> update.identifier().equals("update-1"))) { carried++; } responses.close(); @@ -657,8 +690,10 @@ void failsWhenAMemberStartsHandlingTheQuery() { // then the updates it emitted before being subscribed to are gone, so carrying on would be silently // incomplete - drain(responses); - assertThat(responses.error()).containsInstanceOf(SubscriptionQueryMembersChangedException.class); + await().untilAsserted(() -> { + drain(responses); + assertThat(responses.error()).containsInstanceOf(SubscriptionQueryMembersChangedException.class); + }); } @Test @@ -677,8 +712,10 @@ void failsWhenAMemberStartsHandlingTheQueryWhileTheSubscriptionIsStillBeingOpene MessageStream responses = testSubject.subscriptionQuery(query(), null, 16); // then the updates it emitted before being subscribed to are gone, however narrow the window it joined in - drain(responses); - assertThat(responses.error()).containsInstanceOf(SubscriptionQueryMembersChangedException.class); + await().untilAsserted(() -> { + drain(responses); + assertThat(responses.error()).containsInstanceOf(SubscriptionQueryMembersChangedException.class); + }); } @Test @@ -745,8 +782,10 @@ void failsWhenAMemberEndsTheSubscription() { dispatcher.fail(remoteMember(), new QueryExecutionException("The course store is unavailable.", null)); // then - drain(responses); - assertThat(responses.error()).isPresent(); + await().untilAsserted(() -> { + drain(responses); + assertThat(responses.error()).isPresent(); + }); assertThat(responses.error().orElseThrow()).hasMessageContaining("The course store is unavailable."); } @@ -759,10 +798,12 @@ void releasesTheOtherMembersSubscriptionsWhenOneOfThemFails() { // when one of them fails dispatcher.fail(remoteMember(), new QueryExecutionException("The course store is unavailable.", null)); - drain(responses); // then the member still standing is not left emitting into a subscription that has already failed - assertThat(dispatcher.subscriptions()).allMatch(RecordingRemoteQueryDispatcher.Subscription::released); + await().untilAsserted(() -> { + drain(responses); + assertThat(dispatcher.subscriptions()).allMatch(RecordingRemoteQueryDispatcher.Subscription::released); + }); } @Test @@ -774,13 +815,16 @@ void keepsTheSubscriptionOpenWhileAnyMemberStillHoldsIt() { // when one of them leaves the cluster dispatcher.leave(remoteMember()); - drain(responses); // then the other still has updates to give - assertThat(responses.isCompleted()).isFalse(); + await().during(Duration.ofMillis(200)).untilAsserted(() -> { + drain(responses); + assertThat(responses.isCompleted()).isFalse(); + }); dispatcher.emit(otherRemoteMember(), response("update-from-c")); - assertThat(drain(responses)).extracting(QueryResponseMessage::identifier) - .containsExactly("update-from-c"); + assertThat(drainUntil(responses, updates -> !updates.isEmpty())) + .extracting(QueryResponseMessage::identifier) + .containsExactly("update-from-c"); } @Test @@ -792,10 +836,12 @@ void endsTheWholeSubscriptionWhenAMemberReportsItOver() { // when one of them reports the subscription over, which says there will never be another update dispatcher.completeSubscription(remoteMember()); - drain(responses); // then waiting on the other members would be waiting for updates that are never coming - assertThat(responses.isCompleted()).isTrue(); + await().untilAsserted(() -> { + drain(responses); + assertThat(responses.isCompleted()).isTrue(); + }); assertThat(responses.error()).isEmpty(); } @@ -809,10 +855,12 @@ void failsOnceEveryMemberHasLeft() { // when dispatcher.leave(remoteMember()); dispatcher.leave(otherRemoteMember()); - drain(responses); // then nobody said the subscription is over; it only has nobody left to emit its updates - assertThat(responses.error()).get().isInstanceOf(SubscriptionQueryMembersChangedException.class); + await().untilAsserted(() -> { + drain(responses); + assertThat(responses.error()).get().isInstanceOf(SubscriptionQueryMembersChangedException.class); + }); } @Test @@ -827,7 +875,9 @@ void subscribesOnThisMemberWhenItHandlesTheQuery() { // then a local subscription carries updates just as a remote one does assertThat(handler.subscriptions()).hasSize(1); - assertThat(drain(responses)).extracting(QueryResponseMessage::identifier).containsExactly("update-1"); + assertThat(drainUntil(responses, updates -> !updates.isEmpty())) + .extracting(QueryResponseMessage::identifier) + .containsExactly("update-1"); } @Test @@ -846,8 +896,10 @@ void failsWhenThisMembersHandlerOutpacesTheSubscriber() { // then an update produced and not carried is one the subscriber will never see, so the subscription ends // rather than leaving it with some of the updates and the belief it has all of them, as a member // outpacing this application over the wire does - drain(responses); - assertThat(responses.error()).isPresent(); + await().untilAsserted(() -> { + drain(responses); + assertThat(responses.error()).isPresent(); + }); assertThat(responses.error().orElseThrow()).hasMessageContaining("raise the update buffer size"); } diff --git a/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/SseQueryResponseSinkFramingTest.java b/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/SseQueryResponseSinkFramingTest.java index d4b2a4ba62..f0bc3a2062 100644 --- a/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/SseQueryResponseSinkFramingTest.java +++ b/connector/axoniq-springcloud/src/test/java/io/axoniq/framework/springcloud/query/SseQueryResponseSinkFramingTest.java @@ -29,6 +29,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; +import java.time.Duration; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; @@ -40,6 +41,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.awaitility.Awaitility.await; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; /** @@ -117,6 +119,13 @@ private MvcResult dispatch() throws Exception { /** * Reads what went onto the wire the way the member that asked would. */ + // The responses are written on a thread of the invoker, after the request returned + private List wireOf(MvcResult result, int events) throws IOException { + await().until(() -> wireOf(result).size() >= events); + + return wireOf(result); + } + private List wireOf(MvcResult result) throws IOException { List events = new ArrayList<>(); ServerSentEventReader.read( @@ -141,7 +150,7 @@ void writesOneEventPerResponseMessage() throws Exception { response("response-3"))); // when - List wire = wireOf(dispatch()); + List wire = wireOf(dispatch(), 3); // then three responses came back as three events, not as one run-together answer assertThat(wire).hasSize(3); @@ -155,7 +164,7 @@ void writesEachResponseWholeAndInOrder() throws Exception { .answeringWith(response("response-1"), response("response-2"))); // when - List wire = wireOf(dispatch()); + List wire = wireOf(dispatch(), 2); // then each event carries exactly one response, in the order the handler produced them List responses = new ArrayList<>(); @@ -173,7 +182,7 @@ void writesOneEventForASingleResponse() throws Exception { invoker.bind(new RecordingQueryHandler().answeringWith(response("response-1"))); // when - List wire = wireOf(dispatch()); + List wire = wireOf(dispatch(), 1); // then assertThat(wire).hasSize(1); @@ -200,7 +209,7 @@ void keepsAResponseWhoseContentContainsNewlinesInOneEvent() throws Exception { invoker.bind(new RecordingQueryHandler().answeringWith(response("response-1", metadata))); // when - List wire = wireOf(dispatch()); + List wire = wireOf(dispatch(), 1); // then the content did not split one response across several events assertThat(wire).hasSize(1); @@ -231,9 +240,11 @@ public MessageStream query(QueryMessage query) { responses.seal(); // then each arrival was framed on its own rather than batched into one event at the end - List wire = wireOf(result); - assertThat(wire).hasSize(3); - assertThat(wire).extracting(ServerSentEvent::event).containsOnly(QueryConverter.RESPONSE_EVENT); + await().untilAsserted(() -> { + List wire = wireOf(result); + assertThat(wire).hasSize(3); + assertThat(wire).extracting(ServerSentEvent::event).containsOnly(QueryConverter.RESPONSE_EVENT); + }); } } @@ -257,9 +268,11 @@ public MessageStream query(QueryMessage query) { responses.sealExceptionally(new QueryExecutionException("The course store is unavailable.", null)); // then the response and the failure are separate events, told apart by their type - List wire = wireOf(result); - assertThat(wire).extracting(ServerSentEvent::event) - .containsExactly(QueryConverter.RESPONSE_EVENT, QueryConverter.ERROR_EVENT); + await().untilAsserted(() -> { + List wire = wireOf(result); + assertThat(wire).extracting(ServerSentEvent::event) + .containsExactly(QueryConverter.RESPONSE_EVENT, QueryConverter.ERROR_EVENT); + }); } @Test @@ -268,10 +281,13 @@ void writesNoErrorEventForAQueryAnsweredInFull() throws Exception { invoker.bind(new RecordingQueryHandler().answeringWith(response("response-1"))); // when - List wire = wireOf(dispatch()); + MvcResult result = dispatch(); - // then a stream that ends without an error event is how success is reported - assertThat(wire).extracting(ServerSentEvent::event).containsExactly(QueryConverter.RESPONSE_EVENT); + // then a stream that ends without an error event is how success is reported, however long it is given + await().during(Duration.ofMillis(200)).untilAsserted( + () -> assertThat(wireOf(result)).extracting(ServerSentEvent::event) + .containsExactly(QueryConverter.RESPONSE_EVENT) + ); } @Test @@ -281,7 +297,7 @@ void writesTheFailureAsSomethingTheAskingMemberCanActOn() throws Exception { .failingWith(new QueryExecutionException("The course store is unavailable.", null))); // when - List wire = wireOf(dispatch()); + List wire = wireOf(dispatch(), 1); // then assertThat(wire).hasSize(1); diff --git a/messaging/axoniq-distributed-messaging/src/main/java/io/axoniq/framework/messaging/distributed/CallbackHandOff.java b/messaging/axoniq-distributed-messaging/src/main/java/io/axoniq/framework/messaging/distributed/CallbackHandOff.java new file mode 100644 index 0000000000..753cb34681 --- /dev/null +++ b/messaging/axoniq-distributed-messaging/src/main/java/io/axoniq/framework/messaging/distributed/CallbackHandOff.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2010-2026. Axoniq B.V. + * + * Licensed under the Axoniq Terms of Service (the "Terms"). You may not + * use this file except in compliance with the Terms, available at + * https://www.axoniq.io/legal/terms-of-service + */ + +package io.axoniq.framework.messaging.distributed; + +import org.axonframework.common.annotation.Internal; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.invoke.MethodHandles; +import java.util.Objects; +import java.util.concurrent.Executor; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Turns the work a consumer does when a {@link org.axonframework.messaging.core.MessageStream} has something to read + * into a callback that is safe to register with that stream. + *

+ * A stream calls its callback on the thread of whoever produced the entry or ended the stream. That thread is usually + * shared with other streams, so a callback must finish in microseconds, and must not read from the stream: reading + * can involve I/O, and the stream rejects it. The callback returned by this class only schedules the work on another + * thread, where the consumer can read, and write the entries on to wherever they go, as long as it needs to. + *

+ * Signals that arrive before the scheduled work has started share that one run, as the work reads everything that is + * available when it starts. A signal that arrives while the work runs schedules the work again, so an entry that + * arrives after the work last looked is never missed. + *

+ * Internal, as it exists for the connectors of this framework to share. + * + * @author John Hendrikx + * @since 5.5.0 + */ +@Internal +public final class CallbackHandOff { + + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + private CallbackHandOff() { + // utility class + } + + /** + * Returns a callback that runs the given {@code task} on a virtual thread each time the stream signals. + *

+ * See {@link #handOff(Executor, Runnable)} for how signals are combined. + * + * @param task the work to run when the stream has something to read, which should handle its own failures + * @return a callback for {@link org.axonframework.messaging.core.MessageStream#setCallback(Runnable)}, never + * {@code null} + * @throws NullPointerException if {@code task} is {@code null} + */ + public static Runnable handOff(Runnable task) { + return handOff(Thread::startVirtualThread, task); + } + + /** + * Returns a callback that runs the given {@code task} on a thread of the given {@code executor} each time the + * stream signals. + *

+ * A signal that arrives while a run is scheduled, but has not started, does not schedule another one. A signal that + * arrives once a run has started does, as that run may already have passed what the signal announces. + *

+ * The task is the one that knows what a failure means to whoever consumes the stream, and should report it there. An + * exception that the task throws anyway is logged and dropped, and later signals still run the task. + *

+ * If the {@code executor} rejects the work, the exception is thrown from the callback, which completes the stream + * with it, and later signals are scheduled again. + * + * @param executor the executor to run the task on, which must not run it on the thread that signals + * @param task the work to run when the stream has something to read, which should handle its own failures + * @return a callback for {@link org.axonframework.messaging.core.MessageStream#setCallback(Runnable)}, never + * {@code null} + * @throws NullPointerException if {@code executor} or {@code task} is {@code null} + */ + public static Runnable handOff(Executor executor, Runnable task) { + Objects.requireNonNull(executor, "The executor parameter must not be null."); + Objects.requireNonNull(task, "The task parameter must not be null."); + + AtomicBoolean scheduled = new AtomicBoolean(); + + return () -> { + if (scheduled.compareAndSet(false, true)) { + try { + executor.execute(() -> { + scheduled.set(false); // before the task, so that a signal during the task schedules it again + + try { + task.run(); + } catch (RuntimeException e) { + logger.error("A task handed off from a callback failed.", e); + } + }); + } catch (RuntimeException e) { + scheduled.set(false); + + throw e; + } + } + }; + } +} diff --git a/messaging/axoniq-distributed-messaging/src/main/java/io/axoniq/framework/messaging/distributed/package-info.java b/messaging/axoniq-distributed-messaging/src/main/java/io/axoniq/framework/messaging/distributed/package-info.java new file mode 100644 index 0000000000..8d04309d15 --- /dev/null +++ b/messaging/axoniq-distributed-messaging/src/main/java/io/axoniq/framework/messaging/distributed/package-info.java @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2010-2026. Axoniq B.V. + * + * Licensed under the Axoniq Terms of Service (the "Terms"). You may not + * use this file except in compliance with the Terms, available at + * https://www.axoniq.io/legal/terms-of-service + */ + +/** + * Support shared by the distributed bus connectors. + */ +@NullMarked +package io.axoniq.framework.messaging.distributed; + +import org.jspecify.annotations.NullMarked; diff --git a/messaging/axoniq-distributed-messaging/src/test/java/io/axoniq/framework/messaging/distributed/CallbackHandOffTest.java b/messaging/axoniq-distributed-messaging/src/test/java/io/axoniq/framework/messaging/distributed/CallbackHandOffTest.java new file mode 100644 index 0000000000..043822c48a --- /dev/null +++ b/messaging/axoniq-distributed-messaging/src/test/java/io/axoniq/framework/messaging/distributed/CallbackHandOffTest.java @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2010-2026. Axoniq B.V. + * + * Licensed under the Axoniq Terms of Service (the "Terms"). You may not + * use this file except in compliance with the Terms, available at + * https://www.axoniq.io/legal/terms-of-service + */ + +package io.axoniq.framework.messaging.distributed; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayDeque; +import java.util.Queue; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.awaitility.Awaitility.await; + +/** + * Test class validating {@link CallbackHandOff}. + */ +class CallbackHandOffTest { + + @Test + void shouldRunTheTaskOnAnotherThread() { + // given + AtomicReference runner = new AtomicReference<>(); + Runnable callback = CallbackHandOff.handOff(() -> runner.set(Thread.currentThread())); + + // when + callback.run(); + + // then + await().untilAsserted(() -> assertThat(runner.get()).isNotNull()); + assertThat(runner.get()).isNotSameAs(Thread.currentThread()); + assertThat(runner.get().isVirtual()).isTrue(); + } + + @Test + void shouldNotRunTheTaskOnTheThreadThatSignals() { + // given + Queue scheduled = new ArrayDeque<>(); + AtomicInteger runs = new AtomicInteger(); + Runnable callback = CallbackHandOff.handOff(scheduled::add, runs::incrementAndGet); + + // when + callback.run(); + + // then + assertThat(runs).hasValue(0); + assertThat(scheduled).hasSize(1); + } + + @Test + void shouldShareOneRunBetweenSignalsThatArriveBeforeItStarts() { + // given + Queue scheduled = new ArrayDeque<>(); + AtomicInteger runs = new AtomicInteger(); + Runnable callback = CallbackHandOff.handOff(scheduled::add, runs::incrementAndGet); + + // when + callback.run(); + callback.run(); + callback.run(); + + // then + assertThat(scheduled).hasSize(1); + + scheduled.remove().run(); + + assertThat(runs).hasValue(1); + } + + @Test + void shouldScheduleTheTaskAgainWhenASignalArrivesWhileItRuns() { + // given + Queue scheduled = new ArrayDeque<>(); + AtomicReference callback = new AtomicReference<>(); + AtomicInteger runs = new AtomicInteger(); + + callback.set(CallbackHandOff.handOff(scheduled::add, () -> { + if (runs.incrementAndGet() == 1) { + callback.get().run(); // a signal that arrives after the task looked, while it is still running + } + })); + + // when + callback.get().run(); + scheduled.remove().run(); + + // then + assertThat(scheduled).hasSize(1); + + scheduled.remove().run(); + + assertThat(runs).hasValue(2); + } + + @Test + void shouldScheduleTheTaskAgainAfterItRan() { + // given + Queue scheduled = new ArrayDeque<>(); + AtomicInteger runs = new AtomicInteger(); + Runnable callback = CallbackHandOff.handOff(scheduled::add, runs::incrementAndGet); + + callback.run(); + scheduled.remove().run(); + + // when + callback.run(); + + // then + assertThat(scheduled).hasSize(1); + } + + @Test + void shouldThrowWhenTheExecutorRejectsTheTaskAndAcceptLaterSignals() { + // given + Queue scheduled = new ArrayDeque<>(); + AtomicInteger attempts = new AtomicInteger(); + Runnable callback = CallbackHandOff.handOff(task -> { + if (attempts.incrementAndGet() == 1) { + throw new RejectedExecutionException("no capacity"); + } + + scheduled.add(task); + }, () -> { }); + + // when / then + assertThatThrownBy(callback::run).isInstanceOf(RejectedExecutionException.class); + + callback.run(); + + assertThat(scheduled).hasSize(1); + } + + @Test + void shouldNotLetATaskFailureEscapeAndKeepRunningTheTaskOnLaterSignals() { + // given + Queue scheduled = new ArrayDeque<>(); + AtomicInteger runs = new AtomicInteger(); + Runnable callback = CallbackHandOff.handOff(scheduled::add, () -> { + if (runs.incrementAndGet() == 1) { + throw new IllegalStateException("task failure"); + } + }); + + // when + callback.run(); + scheduled.remove().run(); + callback.run(); + scheduled.remove().run(); + + // then + assertThat(runs).hasValue(2); + } + + @Test + void shouldRejectANullExecutorOrTask() { + assertThatThrownBy(() -> CallbackHandOff.handOff(null, () -> { })).isInstanceOf(NullPointerException.class); + assertThatThrownBy(() -> CallbackHandOff.handOff(Runnable::run, null)).isInstanceOf(NullPointerException.class); + } +}