introduce random timeseries lifecycle policy util method (#32852)

It is useful to have a random TimeseriesLifecycleType-backed LifecyclePolicy
for testing. This PR exposes a helper method to create one and use it for serialization tests
in LifecyclePolicyTests
This commit is contained in:
Tal Levy 2018-08-14 12:57:43 -07:00 committed by GitHub
parent a84b3239c3
commit b218b1c68d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 93 additions and 13 deletions

View File

@ -27,6 +27,10 @@ public class AllocateActionTests extends AbstractActionTestCase<AllocateAction>
@Override
protected AllocateAction createTestInstance() {
return randomInstance();
}
static AllocateAction randomInstance() {
boolean hasAtLeastOneMap = false;
Map<String, String> includes;
if (randomBoolean()) {
@ -52,6 +56,7 @@ public class AllocateActionTests extends AbstractActionTestCase<AllocateAction>
return new AllocateAction(numberOfReplicas, includes, excludes, requires);
}
@Override
protected Reader<AllocateAction> instanceReader() {
return AllocateAction::new;

View File

@ -28,6 +28,10 @@ public class ForceMergeActionTests extends AbstractActionTestCase<ForceMergeActi
@Override
protected ForceMergeAction createTestInstance() {
return randomInstance();
}
static ForceMergeAction randomInstance() {
return new ForceMergeAction(randomIntBetween(1, 100));
}

View File

@ -59,7 +59,7 @@ public class LifecyclePolicyMetadataTests extends AbstractSerializingTestCase<Li
for (int i = 0; i < numberHeaders; i++) {
headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10));
}
return new LifecyclePolicyMetadata(LifecyclePolicyTests.randomLifecyclePolicy(lifecycleName), headers);
return new LifecyclePolicyMetadata(LifecyclePolicyTests.randomTestLifecyclePolicy(lifecycleName), headers);
}
@Override

View File

@ -28,6 +28,8 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.function.LongSupplier;
import java.util.stream.Collectors;
@ -47,26 +49,87 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
@Override
protected NamedWriteableRegistry getNamedWriteableRegistry() {
return new NamedWriteableRegistry(
Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, MockAction.NAME, MockAction::new),
new NamedWriteableRegistry.Entry(LifecycleType.class, TestLifecycleType.TYPE, (in) -> TestLifecycleType.INSTANCE)));
Arrays.asList(
new NamedWriteableRegistry.Entry(LifecycleAction.class, MockAction.NAME, MockAction::new),
new NamedWriteableRegistry.Entry(LifecycleType.class, TimeseriesLifecycleType.TYPE,
(in) -> TimeseriesLifecycleType.INSTANCE),
new NamedWriteableRegistry.Entry(LifecycleAction.class, AllocateAction.NAME, AllocateAction::new),
new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new),
new NamedWriteableRegistry.Entry(LifecycleAction.class, ForceMergeAction.NAME, ForceMergeAction::new),
new NamedWriteableRegistry.Entry(LifecycleAction.class, ReadOnlyAction.NAME, ReadOnlyAction::new),
new NamedWriteableRegistry.Entry(LifecycleAction.class, RolloverAction.NAME, RolloverAction::new),
new NamedWriteableRegistry.Entry(LifecycleAction.class, ShrinkAction.NAME, ShrinkAction::new)
));
}
@Override
protected NamedXContentRegistry xContentRegistry() {
List<NamedXContentRegistry.Entry> entries = new ArrayList<>(ClusterModule.getNamedXWriteables());
entries.add(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MockAction.NAME), MockAction::parse));
entries.add(new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TestLifecycleType.TYPE),
(p) -> TestLifecycleType.INSTANCE));
entries.addAll(Arrays.asList(
new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TimeseriesLifecycleType.TYPE),
(p) -> TimeseriesLifecycleType.INSTANCE),
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse),
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse),
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse),
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse),
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse),
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse)
));
return new NamedXContentRegistry(entries);
}
@Override
protected LifecyclePolicy createTestInstance() {
lifecycleName = randomAlphaOfLength(5);
return randomLifecyclePolicy(lifecycleName);
return randomTimeseriesLifecyclePolicy(lifecycleName);
}
public static LifecyclePolicy randomLifecyclePolicy(@Nullable String lifecycleName) {
public static LifecyclePolicy randomTimeseriesLifecyclePolicy(@Nullable String lifecycleName) {
List<String> phaseNames = randomSubsetOf(TimeseriesLifecycleType.VALID_PHASES);
Map<String, Phase> phases = new HashMap<>(phaseNames.size());
Function<String, Set<String>> validActions = (phase) -> {
switch (phase) {
case "hot":
return TimeseriesLifecycleType.VALID_HOT_ACTIONS;
case "warm":
return TimeseriesLifecycleType.VALID_WARM_ACTIONS;
case "cold":
return TimeseriesLifecycleType.VALID_COLD_ACTIONS;
case "delete":
return TimeseriesLifecycleType.VALID_DELETE_ACTIONS;
default:
throw new IllegalArgumentException("invalid phase [" + phase + "]");
}};
Function<String, LifecycleAction> randomAction = (action) -> {
switch (action) {
case AllocateAction.NAME:
return AllocateActionTests.randomInstance();
case DeleteAction.NAME:
return new DeleteAction();
case ForceMergeAction.NAME:
return ForceMergeActionTests.randomInstance();
case ReadOnlyAction.NAME:
return new ReadOnlyAction();
case RolloverAction.NAME:
return RolloverActionTests.randomInstance();
case ShrinkAction.NAME:
return ShrinkActionTests.randomInstance();
default:
throw new IllegalArgumentException("invalid action [" + action + "]");
}};
for (String phase : phaseNames) {
TimeValue after = TimeValue.parseTimeValue(randomTimeValue(0, 1000000000, "s", "m", "h", "d"), "test_after");
Map<String, LifecycleAction> actions = new HashMap<>();
List<String> actionNames = randomSubsetOf(validActions.apply(phase));
for (String action : actionNames) {
actions.put(action, randomAction.apply(action));
}
phases.put(phase, new Phase(phase, after, actions));
}
return new LifecyclePolicy(TimeseriesLifecycleType.INSTANCE, lifecycleName, phases);
}
public static LifecyclePolicy randomTestLifecyclePolicy(@Nullable String lifecycleName) {
int numberPhases = randomInt(5);
Map<String, Phase> phases = new HashMap<>(numberPhases);
for (int i = 0; i < numberPhases; i++) {

View File

@ -24,12 +24,16 @@ public class RolloverActionTests extends AbstractActionTestCase<RolloverAction>
@Override
protected RolloverAction createTestInstance() {
return randomInstance();
}
static RolloverAction randomInstance() {
ByteSizeUnit maxSizeUnit = randomFrom(ByteSizeUnit.values());
ByteSizeValue maxSize = randomBoolean() ? null : new ByteSizeValue(randomNonNegativeLong() / maxSizeUnit.toBytes(1), maxSizeUnit);
Long maxDocs = randomBoolean() ? null : randomNonNegativeLong();
TimeValue maxAge = (maxDocs == null && maxSize == null || randomBoolean())
? TimeValue.parseTimeValue(randomPositiveTimeValue(), "rollover_action_test")
: null;
? TimeValue.parseTimeValue(randomPositiveTimeValue(), "rollover_action_test")
: null;
return new RolloverAction(maxSize, maxAge, maxDocs);
}

View File

@ -23,6 +23,10 @@ public class ShrinkActionTests extends AbstractActionTestCase<ShrinkAction> {
@Override
protected ShrinkAction createTestInstance() {
return randomInstance();
}
static ShrinkAction randomInstance() {
return new ShrinkAction(randomIntBetween(1, 100));
}

View File

@ -96,7 +96,7 @@ public class PolicyStepsRegistryTests extends ESTestCase {
public void testUpdateFromNothingToSomethingToNothing() {
Client client = Mockito.mock(Client.class);
Mockito.when(client.settings()).thenReturn(Settings.EMPTY);
LifecyclePolicy newPolicy = LifecyclePolicyTests.randomLifecyclePolicy(randomAlphaOfLength(5));
LifecyclePolicy newPolicy = LifecyclePolicyTests.randomTestLifecyclePolicy(randomAlphaOfLength(5));
List<Step> policySteps = newPolicy.toSteps(client, () -> 0L);
Map<String, String> headers = new HashMap<>();
if (randomBoolean()) {
@ -162,7 +162,7 @@ public class PolicyStepsRegistryTests extends ESTestCase {
Client client = Mockito.mock(Client.class);
Mockito.when(client.settings()).thenReturn(Settings.EMPTY);
String policyName = randomAlphaOfLengthBetween(5, 10);
LifecyclePolicy newPolicy = LifecyclePolicyTests.randomLifecyclePolicy(policyName);
LifecyclePolicy newPolicy = LifecyclePolicyTests.randomTestLifecyclePolicy(policyName);
Map<String, String> headers = new HashMap<>();
if (randomBoolean()) {
headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10));
@ -188,7 +188,7 @@ public class PolicyStepsRegistryTests extends ESTestCase {
registry.update(lifecycleMetadata, client, () -> 0L);
// swap out policy
newPolicy = LifecyclePolicyTests.randomLifecyclePolicy(policyName);
newPolicy = LifecyclePolicyTests.randomTestLifecyclePolicy(policyName);
lifecycleMetadata = new IndexLifecycleMetadata(Collections.singletonMap(policyName,
new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap())), OperationMode.RUNNING);
currentState = ClusterState.builder(currentState)