diff --git a/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/events/DefaultEventProcessor.java b/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/events/DefaultEventProcessor.java index f25a05b3..719e4d48 100644 --- a/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/events/DefaultEventProcessor.java +++ b/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/events/DefaultEventProcessor.java @@ -62,6 +62,7 @@ public final class DefaultEventProcessor implements Closeable, EventProcessor { private ScheduledFuture periodicDiagnosticEventTask; private volatile boolean inputCapacityExceeded = false; private final LDLogger logger; + private final EventDispatcher dispatcher; /** * Creates an instance. @@ -86,7 +87,7 @@ public DefaultEventProcessor( inBackground = new AtomicBoolean(eventsConfig.initiallyInBackground); offline = new AtomicBoolean(eventsConfig.initiallyOffline); - new EventDispatcher( + dispatcher = new EventDispatcher( eventsConfig, sharedExecutor, threadPriority, @@ -96,7 +97,8 @@ public DefaultEventProcessor( closed, logger ); - // we don't need to save a reference to this - we communicate with it entirely through the inbox queue. + // We communicate with the dispatcher through the inbox queue; the reference is kept only so that + // flushBlocking(long) can wait for its delivery threads. // Decide whether to start scheduled tasks that depend on the background/offline state. updateScheduledTasks(eventsConfig.initiallyInBackground, eventsConfig.initiallyOffline); @@ -129,6 +131,26 @@ public void flushBlocking() { } } + @Override + public boolean flushBlocking(long timeoutMillis) { + if (closed.get()) { + return false; + } + long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(Math.max(0, timeoutMillis)); + // First let any delivery in progress finish, so the flush below finds a free worker instead of + // being skipped because they are all busy. + if (!dispatcher.waitUntilAllFlushWorkersInactive(deadline)) { + return false; + } + // The dispatcher counts the payload as busy before it completes the FLUSH message, so once the + // message is done the wait below cannot miss the delivery it started. + // COVERAGE: the inbox being full, or the dispatcher not reaching the message in time, can't be + // caused reliably in tests. + EventProcessorMessage message = new EventProcessorMessage(MessageType.FLUSH, null, true); + boolean flushed = postToChannel(message) && message.waitForCompletion(deadline); + return flushed && dispatcher.waitUntilAllFlushWorkersInactive(deadline); + } + @Override public void setInBackground(boolean inBackground) { synchronized (stateLock) { @@ -281,6 +303,16 @@ void completed() { } } + // Returns false if the deadline (a System.nanoTime value) passes first. + boolean waitForCompletion(long deadlineNanos) { + while (true) { + try { + return reply.tryAcquire(Math.max(0, deadlineNanos - System.nanoTime()), TimeUnit.NANOSECONDS); + } catch (InterruptedException ex) { // COVERAGE: there is no way to make this happen from test code. + } + } + } + void waitForCompletion() { if (reply == null) { // COVERAGE: there is no way to make this happen from test code return; @@ -515,6 +547,22 @@ private void waitUntilAllFlushWorkersInactive() { } } + // Returns false if the deadline (a System.nanoTime value) passes before the workers are idle. + boolean waitUntilAllFlushWorkersInactive(long deadlineNanos) { + synchronized(busyFlushWorkersCount) { + while (busyFlushWorkersCount.get() != 0) { + long remaining = deadlineNanos - System.nanoTime(); + if (remaining <= 0) { + return false; + } + try { + TimeUnit.NANOSECONDS.timedWait(busyFlushWorkersCount, remaining); + } catch (InterruptedException e) {} // COVERAGE: there is no way to cause this condition in tests + } + return true; + } + } + private void processEvent(Event e, EventBuffer outbox) { if (disabled.get()) { return; @@ -640,7 +688,7 @@ private void triggerFlush(EventBuffer outbox, BlockingQueue payloa outbox.summarizer.restoreTo(payload.summaries); synchronized(busyFlushWorkersCount) { busyFlushWorkersCount.decrementAndGet(); - busyFlushWorkersCount.notify(); + busyFlushWorkersCount.notifyAll(); // there can be more than one waiter } } } diff --git a/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/events/EventProcessor.java b/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/events/EventProcessor.java index 2f26e2fc..981cd4f8 100644 --- a/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/events/EventProcessor.java +++ b/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/events/EventProcessor.java @@ -21,6 +21,24 @@ public interface EventProcessor { * Flushes and blocks until the flush is done. */ void flushBlocking(); + + /** + * Flushes and blocks until the events buffered before this call have been delivered, or until the + * timeout elapses. Unlike {@link #flushBlocking()}, which returns once the flush has been handed to + * a delivery thread, this waits for every delivery in progress to finish, so it suits an + * application that is about to exit. + *

+ * The default implementation calls {@link #flushBlocking()} and returns true, for implementations + * that predate this method. + * + * @param timeoutMillis the longest time to wait, in milliseconds; zero or less means don't wait + * @return true if delivery finished in time, false if the timeout elapsed first or the event + * processor was closed + */ + default boolean flushBlocking(long timeoutMillis) { + flushBlocking(); + return true; + } /** * Tells the event processor whether we should be in background mode. This is only applicable in the client-side diff --git a/lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/events/DefaultEventProcessorTest.java b/lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/events/DefaultEventProcessorTest.java index 7024c9e0..6766c75b 100644 --- a/lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/events/DefaultEventProcessorTest.java +++ b/lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/events/DefaultEventProcessorTest.java @@ -20,6 +20,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; /** @@ -394,4 +395,108 @@ public void eventsAreKeptInBufferIfAllFlushWorkersAreBusy() throws Exception { isIdentifyEvent(event3, testUserJson3))); } } + + // An event sender that blocks each delivery until the test releases it. + private static final class GatedEventSender implements EventSender { + final CountDownLatch entered = new CountDownLatch(1); + final CountDownLatch release = new CountDownLatch(1); + final AtomicInteger delivered = new AtomicInteger(); + + @Override + public Result sendAnalyticsEvents(byte[] data, int eventCount, URI eventsBaseUri) { + entered.countDown(); + try { + release.await(); + } catch (InterruptedException e) {} + delivered.incrementAndGet(); + return Result.success(null); + } + + @Override + public Result sendDiagnosticEvent(byte[] data, URI eventsBaseUri) { + return Result.success(null); + } + + @Override + public void close() throws IOException { + release.countDown(); + } + } + + @Test + public void flushBlockingWithTimeoutWaitsForDelivery() throws Exception { + GatedEventSender es = new GatedEventSender(); + try (DefaultEventProcessor ep = makeEventProcessor(baseConfig(es))) { + ep.sendEvent(identifyEvent(user)); + new Thread(() -> { + try { + es.entered.await(); + Thread.sleep(100); + } catch (InterruptedException e) {} + es.release.countDown(); + }).start(); + assertTrue(ep.flushBlocking(5000)); + assertEquals(1, es.delivered.get()); // the delivery had finished when flushBlocking returned + } + } + + @Test + public void flushBlockingWithTimeoutReturnsFalseIfDeliveryDoesNotFinishInTime() throws Exception { + GatedEventSender es = new GatedEventSender(); + try (DefaultEventProcessor ep = makeEventProcessor(baseConfig(es))) { + ep.sendEvent(identifyEvent(user)); + assertFalse(ep.flushBlocking(100)); + assertTrue(es.entered.await(5, TimeUnit.SECONDS)); // the delivery started but was still going + assertEquals(0, es.delivered.get()); + es.release.countDown(); + } + } + + @Test + public void flushBlockingWithTimeoutReturnsFalseIfEarlierDeliveryDoesNotFinishInTime() throws Exception { + GatedEventSender es = new GatedEventSender(); + try (DefaultEventProcessor ep = makeEventProcessor(baseConfig(es))) { + ep.sendEvent(identifyEvent(user)); + ep.flushAsync(); + assertTrue(es.entered.await(5, TimeUnit.SECONDS)); + ep.sendEvent(identifyEvent(user)); + assertFalse(ep.flushBlocking(50)); // still waiting for the first delivery + es.release.countDown(); + assertTrue(ep.flushBlocking(5000)); // now the second batch goes too + assertEquals(2, es.delivered.get()); + } + } + + @Test + public void flushBlockingWithTimeoutSucceedsWithNothingToSend() throws Exception { + MockEventSender es = new MockEventSender(); + try (DefaultEventProcessor ep = makeEventProcessor(baseConfig(es))) { + assertTrue(ep.flushBlocking(5000)); + es.expectNoRequests(50); + } + } + + @Test + public void flushBlockingWithTimeoutReturnsFalseAfterClose() throws Exception { + MockEventSender es = new MockEventSender(); + DefaultEventProcessor ep = makeEventProcessor(baseConfig(es)); + ep.close(); + assertFalse(ep.flushBlocking(1000)); + } + + @Test + public void defaultFlushBlockingWithTimeoutCallsFlushBlocking() { + AtomicInteger calls = new AtomicInteger(); + EventProcessor ep = new EventProcessor() { + public void sendEvent(Event e) {} + public void flushAsync() {} + public void flushBlocking() { + calls.incrementAndGet(); + } + public void setInBackground(boolean inBackground) {} + public void setOffline(boolean offline) {} + }; + assertTrue(ep.flushBlocking(10)); + assertEquals(1, calls.get()); + } }