/** * Create a strategy whose time periods increase exponentially. * * @param clock the clock used to determine if sufficient time has elapsed; may not be null * @param initialDelayInMilliseconds the initial delay; must be positive * @param maxDelayInMilliseconds the maximum delay; must be greater than the initial delay * @return the strategy; never null */ public static ElapsedTimeStrategy exponential(Clock clock, long initialDelayInMilliseconds, long maxDelayInMilliseconds) { return exponential(clock, initialDelayInMilliseconds, maxDelayInMilliseconds, 2.0); }
@Override protected void pollComplete(List<SourceRecord> batch) { // Record a bit about this batch ... int batchSize = batch.size(); recordCounter += batchSize; totalRecordCounter.addAndGet(batchSize); if (batchSize > 0) { SourceRecord lastRecord = batch.get(batchSize - 1); lastOffset = lastRecord.sourceOffset(); if (pollOutputDelay.hasElapsed()) { // We want to record the status ... long millisSinceLastOutput = clock.currentTimeInMillis() - previousOutputMillis; try { context.temporaryLoggingContext("binlog", () -> { logger.info("{} records sent during previous {}, last recorded offset: {}", recordCounter, Strings.duration(millisSinceLastOutput), lastOffset); }); } finally { recordCounter = 0; previousOutputMillis += millisSinceLastOutput; } } } }
delay = ElapsedTimeStrategy.step(clock, 10, step::get, 100);
@Test public void testConstantDelay() { clock.advanceTo(100); delay = ElapsedTimeStrategy.constant(clock, 10); // Initial call should always be true ... assertElapsed(); // next stop at 100+10=110 assertNotElapsed(); clock.advanceTo(109); assertNotElapsed(); clock.advanceTo(110); assertElapsed(); // next stop at 110+10=120 clock.advanceTo(119); assertNotElapsed(); clock.advanceTo(128); assertElapsed(); // next stop at 120+10=130 clock.advanceTo(130); assertElapsed(); // next stop at 130+10=140 clock.advanceTo(139); assertNotElapsed(); clock.advanceTo(140); assertElapsed(); // Advance many multiples should be instantaneous... clock.advanceTo(100000000000000L); assertElapsed(); }
@Test public void testLinearDelay() { clock.advanceTo(100); delay = ElapsedTimeStrategy.linear(clock, 100);
delay = ElapsedTimeStrategy.step(clock, 10, step::get, 100);
pollOutputDelay = ElapsedTimeStrategy.exponential(clock, INITIAL_POLL_PERIOD_IN_MILLIS, MAX_POLL_PERIOD_IN_MILLIS);
protected void assertNotElapsed() { for (int i=0; i!=5; ++i) { assertThat(delay.hasElapsed()).isFalse(); } }
/** * Create a strategy whose time periods increase exponentially. * * @param clock the clock used to determine if sufficient time has elapsed; may not be null * @param initialDelayInMilliseconds the initial delay; must be positive * @param maxDelayInMilliseconds the maximum delay; must be greater than the initial delay * @return the strategy; never null */ public static ElapsedTimeStrategy exponential(Clock clock, long initialDelayInMilliseconds, long maxDelayInMilliseconds) { return exponential(clock, initialDelayInMilliseconds, maxDelayInMilliseconds, 2.0); }
protected void assertElapsed() { assertThat(delay.hasElapsed()).isTrue(); assertNotElapsed(); }
@Test public void testExponentialDelay() { clock.advanceTo(100); delay = ElapsedTimeStrategy.exponential(clock, 100, 4000);
pollOutputDelay.hasElapsed(); previousOutputMillis = clock.currentTimeInMillis();