/** * Creates a supplier such that the first call to {@link Supplier#get()} on it will take the maximum of the runtime * configuration and the persisted number of shards, and persist and memoize the result. Subsequent calls will * return the cached value until refreshTimeMillis has passed, at which point the next call will again perform the * check and set. * * @param runtimeConfig live reloadable runtime configuration for the number of shards * @param progress progress table persisting the number of shards * @param refreshTimeMillis timeout for caching the number of shards * @return supplier calculating and persisting the number of shards to use */ public static Supplier<Integer> createProgressUpdatingSupplier(Supplier<Integer> runtimeConfig, ShardProgress progress, long refreshTimeMillis) { return Suppliers.memoizeWithExpiration( () -> progress.updateNumberOfShards(runtimeConfig.get()), refreshTimeMillis, TimeUnit.MILLISECONDS); }
@Test public void attemptingToDecreaseNumberOfShardsIsNoop() { progress.updateNumberOfShards(64); progress.updateNumberOfShards(32); assertThat(progress.getNumberOfShards()).isEqualTo(64); }
@Test public void testProgressHigherValue() { when(runtimeConfigSupplier.get()).thenReturn(AtlasDbConstants.DEFAULT_SWEEP_QUEUE_SHARDS); progress.updateNumberOfShards(25); assertThat(numShardSupplier.get()).isEqualTo(25); }
@Test public void increasingNumberOfShardsAboveMaxThrows() { assertThatThrownBy(() -> progress.updateNumberOfShards(AtlasDbConstants.MAX_SWEEP_QUEUE_SHARDS + 1)) .isInstanceOf(IllegalArgumentException.class); }
@Test public void getAfterRefreshTimeChecksConfigAndUpdatesProgress() throws InterruptedException { assertThat(setRuntimeAndGetNumShards(50)).isEqualTo(50); verify(runtimeConfigSupplier, times(1)).get(); verify(progress, times(1)).updateNumberOfShards(50); waitToForceRefresh(); assertThat(setRuntimeAndGetNumShards(100)).isEqualTo(100); verify(runtimeConfigSupplier, times(2)).get(); verify(progress, times(1)).updateNumberOfShards(100); waitToForceRefresh(); assertThat(setRuntimeAndGetNumShards(75)).isEqualTo(100); verify(runtimeConfigSupplier, times(3)).get(); verify(progress, times(1)).updateNumberOfShards(75); verify(progress, times(3)).updateNumberOfShards(anyInt()); }
@Test public void cannotUpdateNumberOfShardsToZero() { progress.updateNumberOfShards(0); assertThat(progress.getNumberOfShards()).isEqualTo(AtlasDbConstants.DEFAULT_SWEEP_QUEUE_SHARDS); }
@Test public void canIncreaseNumberOfShardsToMax() { progress.updateNumberOfShards(AtlasDbConstants.MAX_SWEEP_QUEUE_SHARDS); assertThat(progress.getNumberOfShards()).isEqualTo(AtlasDbConstants.MAX_SWEEP_QUEUE_SHARDS); }
@Test public void canUpdateNumberOfShards() { progress.updateNumberOfShards(128); assertThat(progress.getNumberOfShards()).isEqualTo(128); }
@Test public void updatingTimestampsDoesNotAffectShardsAndViceVersa() { assertThat(progress.getNumberOfShards()).isEqualTo(AtlasDbConstants.DEFAULT_SWEEP_QUEUE_SHARDS); assertThat(progress.getLastSweptTimestamp(CONSERVATIVE_TEN)).isEqualTo(INITIAL_TIMESTAMP); assertThat(progress.getLastSweptTimestamp(THOROUGH_TEN)).isEqualTo(INITIAL_TIMESTAMP); progress.updateNumberOfShards(64); progress.updateLastSweptTimestamp(CONSERVATIVE_TEN, 32L); progress.updateLastSweptTimestamp(THOROUGH_TEN, 128L); assertThat(progress.getNumberOfShards()).isEqualTo(64); assertThat(progress.getLastSweptTimestamp(CONSERVATIVE_TEN)).isEqualTo(32L); assertThat(progress.getLastSweptTimestamp(THOROUGH_TEN)).isEqualTo(128L); }
@Test public void canUpgradeNumberOfShardsIfPersistedDefaultValue() { byte[] defaultValue = ShardProgress.createColumnValue(AtlasDbConstants.DEFAULT_SWEEP_QUEUE_SHARDS); CheckAndSetRequest request = progress.createNewCellRequest(ShardProgress.SHARD_COUNT_SAS, defaultValue); kvs.checkAndSet(request); progress.updateNumberOfShards(128); assertThat(progress.getNumberOfShards()).isEqualTo(128); }
/** * Creates a supplier such that the first call to {@link Supplier#get()} on it will take the maximum of the runtime * configuration and the persisted number of shards, and persist and memoize the result. Subsequent calls will * return the cached value until refreshTimeMillis has passed, at which point the next call will again perform the * check and set. * * @param runtimeConfig live reloadable runtime configuration for the number of shards * @param progress progress table persisting the number of shards * @param refreshTimeMillis timeout for caching the number of shards * @return supplier calculating and persisting the number of shards to use */ public static Supplier<Integer> createProgressUpdatingSupplier(Supplier<Integer> runtimeConfig, ShardProgress progress, long refreshTimeMillis) { return Suppliers.memoizeWithExpiration( () -> progress.updateNumberOfShards(runtimeConfig.get()), refreshTimeMillis, TimeUnit.MILLISECONDS); }