public void retain(IndexSet indexSet,
                       @Nullable Integer maxNumberOfIndices,
                       RetentionExecutor.RetentionAction action,
                       String actionName) {


        final Map<String, Set<String>> deflectorIndices = indexSet.getAllIndexAliases();
        final int indexCount = (int) deflectorIndices.keySet()
                .stream()
                .filter(indexName -> !indices.isReopened(indexName))
                .count();

        if (maxNumberOfIndices == null) {
            LOG.warn("No retention strategy configuration found, not running index retention!");
            return;
        }

        // Do we have more indices than the configured maximum?
        if (indexCount <= maxNumberOfIndices) {
            LOG.debug("Number of indices ({}) lower than limit ({}). Not performing any retention actions.",
                    indexCount, maxNumberOfIndices);
            return;
        }

        // We have more indices than the configured maximum! Remove as many as needed.
        final int removeCount = indexCount - maxNumberOfIndices;
        final String msg = "Number of indices (" + indexCount + ") higher than limit (" + maxNumberOfIndices + "). " +
                "Running retention for " + removeCount + " indices.";
        LOG.info(msg);
        activityWriter.write(new Activity(msg, CountBasedRetentionExecutor.class));

        retentionExecutor.runRetention(indexSet, removeCount, action, actionName);
    }
@Test
    public void shouldRetainOldestIndices() {
        underTest.retain(indexSet, 4, action, "action");

        verify(action, times(1)).retain(retainedIndexName.capture(), eq(indexSet));
        // Ensure that the oldest indices come first
        assertThat(retainedIndexName.getAllValues().get(0)).containsExactly("test_1", "test_2");

        verify(activityWriter, times(2)).write(any(Activity.class));
    }