/** * compute a compensation time defined as the actual time this task was executed since the prev iteration, * vs the configured amount of time for execution. This is useful for cases where changes in time (due to * clock skew or gc for example) causes the actual eviction task to execute later than the desired time * according to the configured cycle. */ long getCompensationTimeMs() { long currNanos = getCurrentTimeNano(); long lastNanos = lastExecutionNanosRef.getAndSet(currNanos); if (lastNanos == 0l) { return 0l; } long elapsedMs = TimeUnit.NANOSECONDS.toMillis(currNanos - lastNanos); long compensationTime = elapsedMs - serverConfig.getEvictionIntervalTimerInMs(); return compensationTime <= 0l ? 0l : compensationTime; }
@Test public void testEvictionTaskCompensationTime() throws Exception { long evictionTaskPeriodNanos = serverConfig.getEvictionIntervalTimerInMs() * 1000000; AbstractInstanceRegistry.EvictionTask testTask = spy(registry.new EvictionTask()); when(testTask.getCurrentTimeNano()) .thenReturn(1l) // less than the period .thenReturn(1l + evictionTaskPeriodNanos) // exactly 1 period .thenReturn(1l + evictionTaskPeriodNanos*2 + 10000000l) // 10ms longer than 1 period .thenReturn(1l + evictionTaskPeriodNanos*3 - 1l); // less than 1 period assertThat(testTask.getCompensationTimeMs(), is(0l)); assertThat(testTask.getCompensationTimeMs(), is(0l)); assertThat(testTask.getCompensationTimeMs(), is(10l)); assertThat(testTask.getCompensationTimeMs(), is(0l)); } }
/** * compute a compensation time defined as the actual time this task was executed since the prev iteration, * vs the configured amount of time for execution. This is useful for cases where changes in time (due to * clock skew or gc for example) causes the actual eviction task to execute later than the desired time * according to the configured cycle. */ long getCompensationTimeMs() { long currNanos = getCurrentTimeNano(); long lastNanos = lastExecutionNanosRef.getAndSet(currNanos); if (lastNanos == 0l) { return 0l; } long elapsedMs = TimeUnit.NANOSECONDS.toMillis(currNanos - lastNanos); long compensationTime = elapsedMs - serverConfig.getEvictionIntervalTimerInMs(); return compensationTime <= 0l ? 0l : compensationTime; }