JRTConfigRequester implements RequestWaiter { static long calculateFailedRequestDelay(ErrorType errorCode, int transientFailures, int fatalFailures, TimingValues timingValues, boolean configured) { long delay; if (configured) delay = timingValues.getConfiguredErrorDelay(); else delay = timingValues.getUnconfiguredDelay(); if (errorCode == ErrorType.TRANSIENT) { delay = delay * Math.min((transientFailures + 1), timingValues.getMaxDelayMultiplier()); } else { delay = timingValues.getFixedDelay() + (delay * Math.min(fatalFailures, timingValues.getMaxDelayMultiplier())); delay = timingValues.getPlusMinusFractionRandom(delay, randomFraction); } return delay; } JRTConfigRequester(ConfigSourceSet configSourceSet, ScheduledThreadPoolExecutor scheduler,
                       ConnectionPool connectionPool, TimingValues timingValues);  JRTConfigRequester(ConnectionPool connectionPool, TimingValues timingValues); static JRTConfigRequester create(ConfigSourceSet sourceSet, TimingValues timingValues); void request(JRTConfigSubscription<T> sub); @SuppressWarnings("unchecked") @Override void handleRequestDone(Request req); void close(); ConnectionPool getConnectionPool(); static final ConfigSourceSet defaultSourceSet; }
@Test public void testDelayCalculation() { TimingValues defaultTimingValues = new TimingValues(); Random random = new Random(0); TimingValues timingValues = new TimingValues(defaultTimingValues, random); int transientFailures = 0; int fatalFailures = 0; boolean configured = false; long delay = JRTConfigRequester.calculateFailedRequestDelay(ErrorType.TRANSIENT, transientFailures, fatalFailures, timingValues, configured); assertThat(delay, is(timingValues.getUnconfiguredDelay())); transientFailures = 5; delay = JRTConfigRequester.calculateFailedRequestDelay(ErrorType.TRANSIENT, transientFailures, fatalFailures, timingValues, configured); assertThat(delay, is((transientFailures + 1) * timingValues.getUnconfiguredDelay())); transientFailures = 0; delay = JRTConfigRequester.calculateFailedRequestDelay(ErrorType.FATAL, transientFailures, fatalFailures, timingValues, configured); assertTrue(delay > (1 - JRTConfigRequester.randomFraction) * timingValues.getFixedDelay()); assertTrue(delay < (1 + JRTConfigRequester.randomFraction) * timingValues.getFixedDelay()); assertThat(delay, is(5462L)); configured = true; delay = JRTConfigRequester.calculateFailedRequestDelay(ErrorType.TRANSIENT, transientFailures, fatalFailures, timingValues, configured); assertThat(delay, is(timingValues.getConfiguredErrorDelay())); delay = JRTConfigRequester.calculateFailedRequestDelay(ErrorType.FATAL, transientFailures, fatalFailures, timingValues, configured); assertTrue(delay > (1 - JRTConfigRequester.randomFraction) * timingValues.getFixedDelay()); assertTrue(delay < (1 + JRTConfigRequester.randomFraction) * timingValues.getFixedDelay()); assertThat(delay, is(5663L)); fatalFailures = 1; configured = false; delay = JRTConfigRequester.calculateFailedRequestDelay(ErrorType.TRANSIENT, transientFailures, fatalFailures, timingValues, configured); assertThat(delay, is(timingValues.getUnconfiguredDelay())); delay = JRTConfigRequester.calculateFailedRequestDelay(ErrorType.FATAL, transientFailures, fatalFailures, timingValues, configured); final long l = timingValues.getFixedDelay() + timingValues.getUnconfiguredDelay(); assertTrue(delay > (1 - JRTConfigRequester.randomFraction) * l); assertTrue(delay < (1 + JRTConfigRequester.randomFraction) * l); assertThat(delay, is(5377L)); fatalFailures = 1; configured = true; delay = JRTConfigRequester.calculateFailedRequestDelay(ErrorType.TRANSIENT, transientFailures, fatalFailures, timingValues, configured); assertThat(delay, is(timingValues.getConfiguredErrorDelay())); delay = JRTConfigRequester.calculateFailedRequestDelay(ErrorType.FATAL, transientFailures, fatalFailures, timingValues, configured); final long l1 = timingValues.getFixedDelay() + timingValues.getConfiguredErrorDelay(); assertTrue(delay > (1 - JRTConfigRequester.randomFraction) * l1); assertTrue(delay < (1 + JRTConfigRequester.randomFraction) * l1); assertThat(delay, is(20851L)); fatalFailures = timingValues.getMaxDelayMultiplier() + 1; configured = true; delay = JRTConfigRequester.calculateFailedRequestDelay(ErrorType.TRANSIENT, transientFailures, fatalFailures, timingValues, configured); assertThat(delay, is(timingValues.getConfiguredErrorDelay())); assertTrue(delay < timingValues.getMaxDelayMultiplier() * timingValues.getConfiguredErrorDelay()); delay = JRTConfigRequester.calculateFailedRequestDelay(ErrorType.FATAL, transientFailures, fatalFailures, timingValues, configured); final long l2 = timingValues.getFixedDelay() + timingValues.getMaxDelayMultiplier() * timingValues.getConfiguredErrorDelay(); assertTrue(delay > (1 - JRTConfigRequester.randomFraction) * l2); assertTrue(delay < (1 + JRTConfigRequester.randomFraction) * l2); assertThat(delay, is(163520L)); }
@Test public void testDelay() { TimingValues timingValues = new TimingValues(); int transientFailures = 0; int fatalFailures = 0; long delay = JRTConfigRequester.calculateFailedRequestDelay(ErrorType.TRANSIENT, transientFailures, fatalFailures, timingValues, true); assertThat(delay, is(timingValues.getConfiguredErrorDelay())); assertThat(delay, is((transientFailures + 1) * timingValues.getConfiguredErrorDelay())); }