@Test public void testRotateFailed() throws Exception { when(indices.getIndexStats("name")).thenReturn(null); when(deflector.getNewestTargetName()).thenReturn("name"); when(clusterConfigService.get(SizeBasedRotationStrategyConfig.class)) .thenReturn(SizeBasedRotationStrategyConfig.create(100)); final SizeBasedRotationStrategy strategy = new SizeBasedRotationStrategy( indices, deflector, clusterConfigService, nodeId, auditEventSender); strategy.rotate(); verify(deflector, never()).cycle(); reset(deflector); }
@Nullable @Override protected Result shouldRotate(final String index) { final SizeBasedRotationStrategyConfig config = clusterConfigService.get(SizeBasedRotationStrategyConfig.class); if (config == null) { LOG.warn("No rotation strategy configuration found, not running index rotation!"); return null; } final IndexStatistics indexStats = indices.getIndexStats(index); if (indexStats == null) { return null; } final long sizeInBytes = indexStats.primaries().getStore().getSizeInBytes(); final boolean shouldRotate = sizeInBytes > config.maxSize(); return new Result() { public final MessageFormat ROTATE = new MessageFormat( "Storage size for index <{0}> is {1} bytes, exceeding the maximum of {2} bytes. Rotating index.", Locale.ENGLISH); public final MessageFormat NOT_ROTATE = new MessageFormat( "Storage size for index <{0}> is {1} bytes, below the maximum of {2} bytes. Not doing anything.", Locale.ENGLISH); @Override public String getDescription() { MessageFormat format = shouldRotate() ? ROTATE : NOT_ROTATE; return format.format(new Object[] {index, sizeInBytes, config.maxSize()}); } @Override public boolean shouldRotate() { return shouldRotate; } }; }
@Test public void testDontRotate() throws Exception { final CommonStats commonStats = new CommonStats(); commonStats.store = new StoreStats(1000, 0); final IndexStatistics stats = IndexStatistics.create( "name", commonStats, commonStats, Collections.<ShardRouting>emptyList()); when(indices.getIndexStats("name")).thenReturn(stats); when(deflector.getNewestTargetName()).thenReturn("name"); when(clusterConfigService.get(SizeBasedRotationStrategyConfig.class)) .thenReturn(SizeBasedRotationStrategyConfig.create(100000L)); final SizeBasedRotationStrategy strategy = new SizeBasedRotationStrategy( indices, deflector, clusterConfigService, nodeId, auditEventSender); strategy.rotate(); verify(deflector, never()).cycle(); reset(deflector); }