refactor a collection of step-related things + shrink_step
- added ShrinkStep/Tests - AsyncActionStep now passes in IndexMetaData instead of Index - Delete usage of ClusterStateActionStep - with ClusterStateActionStep gone, InitializePolicyContextStep is the only other ClusterState-nonWait step - Migrate setting-updates to UpdateSettingsStep
This commit is contained in:
parent
8d91f197d4
commit
ef34f982f1
|
@ -6,10 +6,12 @@
|
|||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
|
@ -113,10 +115,14 @@ public class AllocateAction implements LifecycleAction {
|
|||
|
||||
@Override
|
||||
public List<Step> toSteps(Client client, String phase, StepKey nextStepKey) {
|
||||
StepKey allocateKey = new StepKey(phase, NAME, UpdateAllocationSettingsStep.NAME);
|
||||
StepKey allocateKey = new StepKey(phase, NAME, NAME);
|
||||
StepKey allocationRoutedKey = new StepKey(phase, NAME, AllocationRoutedStep.NAME);
|
||||
UpdateAllocationSettingsStep allocateStep = new UpdateAllocationSettingsStep(allocateKey, allocationRoutedKey, client, include,
|
||||
exclude, require);
|
||||
|
||||
Settings.Builder newSettings = Settings.builder();
|
||||
include.forEach((key, value) -> newSettings.put(IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + key, value));
|
||||
exclude.forEach((key, value) -> newSettings.put(IndexMetaData.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + key, value));
|
||||
require.forEach((key, value) -> newSettings.put(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + key, value));
|
||||
UpdateSettingsStep allocateStep = new UpdateSettingsStep(allocateKey, allocationRoutedKey, client, newSettings.build());
|
||||
AllocationRoutedStep routedCheckStep = new AllocationRoutedStep(allocationRoutedKey, nextStepKey);
|
||||
return Arrays.asList(allocateStep, routedCheckStep);
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.index.Index;
|
||||
|
||||
public abstract class AsyncActionStep extends Step {
|
||||
|
@ -25,7 +26,7 @@ public abstract class AsyncActionStep extends Step {
|
|||
return true;
|
||||
}
|
||||
|
||||
public abstract void performAction(Index index, Listener listener);
|
||||
public abstract void performAction(IndexMetaData indexMetaData, Listener listener);
|
||||
|
||||
public interface Listener {
|
||||
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.cluster.ClusterState;
|
||||
import org.elasticsearch.index.Index;
|
||||
|
||||
public abstract class ClusterStateActionStep extends Step {
|
||||
|
||||
public ClusterStateActionStep(StepKey key, StepKey nextStepKey) {
|
||||
super(key, nextStepKey);
|
||||
}
|
||||
|
||||
public abstract ClusterState performAction(Index index, ClusterState clusterState);
|
||||
|
||||
}
|
|
@ -8,7 +8,7 @@ package org.elasticsearch.xpack.core.indexlifecycle;
|
|||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.index.Index;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
|
||||
public class DeleteStep extends AsyncActionStep {
|
||||
|
||||
|
@ -17,9 +17,9 @@ public class DeleteStep extends AsyncActionStep {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void performAction(Index index, Listener listener) {
|
||||
public void performAction(IndexMetaData indexMetaData, Listener listener) {
|
||||
getClient().admin().indices()
|
||||
.delete(new DeleteIndexRequest(index.getName()),
|
||||
.delete(new DeleteIndexRequest(indexMetaData.getIndex().getName()),
|
||||
ActionListener.wrap(response -> listener.onResponse(true) , listener::onFailure));
|
||||
}
|
||||
|
||||
|
|
|
@ -10,9 +10,12 @@ import org.elasticsearch.common.ParseField;
|
|||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.index.codec.CodecService;
|
||||
import org.elasticsearch.index.engine.EngineConfig;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -91,13 +94,16 @@ public class ForceMergeAction implements LifecycleAction {
|
|||
|
||||
@Override
|
||||
public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey) {
|
||||
StepKey updateCompressionKey = new StepKey(phase, NAME, UpdateBestCompressionSettingsStep.NAME);
|
||||
StepKey updateCompressionKey = new StepKey(phase, NAME, "best_compression");
|
||||
StepKey forceMergeKey = new StepKey(phase, NAME, ForceMergeStep.NAME);
|
||||
StepKey countKey = new StepKey(phase, NAME, SegmentCountStep.NAME);
|
||||
ForceMergeStep forceMergeStep = new ForceMergeStep(forceMergeKey, countKey, client, maxNumSegments);
|
||||
SegmentCountStep segmentCountStep = new SegmentCountStep(countKey, nextStepKey, client, maxNumSegments, bestCompression);
|
||||
if (bestCompression) {
|
||||
UpdateBestCompressionSettingsStep updateBestCompression = new UpdateBestCompressionSettingsStep(updateCompressionKey, forceMergeKey);
|
||||
Settings compressionSettings = Settings.builder()
|
||||
.put(EngineConfig.INDEX_CODEC_SETTING.getKey(), CodecService.BEST_COMPRESSION_CODEC).build();
|
||||
UpdateSettingsStep updateBestCompression = new UpdateSettingsStep(updateCompressionKey,
|
||||
forceMergeKey, client, compressionSettings);
|
||||
return Arrays.asList(updateBestCompression, forceMergeStep, segmentCountStep);
|
||||
}
|
||||
return Arrays.asList(forceMergeStep, segmentCountStep);
|
||||
|
|
|
@ -8,6 +8,7 @@ package org.elasticsearch.xpack.core.indexlifecycle;
|
|||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.index.Index;
|
||||
|
||||
import java.util.Objects;
|
||||
|
@ -26,8 +27,8 @@ public class ForceMergeStep extends AsyncActionStep {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void performAction(Index index, Listener listener) {
|
||||
ForceMergeRequest request = new ForceMergeRequest();
|
||||
public void performAction(IndexMetaData indexMetaData, Listener listener) {
|
||||
ForceMergeRequest request = new ForceMergeRequest(indexMetaData.getIndex().getName());
|
||||
request.maxNumSegments(maxNumSegments);
|
||||
getClient().admin().indices()
|
||||
.forceMerge(request, ActionListener.wrap(response -> listener.onResponse(true),
|
||||
|
|
|
@ -11,13 +11,12 @@ import org.elasticsearch.cluster.metadata.MetaData;
|
|||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.index.Index;
|
||||
|
||||
public class InitializePolicyContextStep extends ClusterStateActionStep {
|
||||
public class InitializePolicyContextStep extends Step {
|
||||
|
||||
InitializePolicyContextStep(Step.StepKey key, StepKey nextStepKey) {
|
||||
public InitializePolicyContextStep(Step.StepKey key, StepKey nextStepKey) {
|
||||
super(key, nextStepKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClusterState performAction(Index index, ClusterState clusterState) {
|
||||
Settings settings = clusterState.metaData().index(index).getSettings();
|
||||
if (settings.hasValue(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE)) {
|
||||
|
|
|
@ -6,9 +6,11 @@
|
|||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.ObjectParser;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
|
@ -54,8 +56,9 @@ public class ReadOnlyAction implements LifecycleAction {
|
|||
|
||||
@Override
|
||||
public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey) {
|
||||
Step.StepKey key = new Step.StepKey(phase, NAME, ReadOnlyStep.NAME);
|
||||
return Collections.singletonList(new ReadOnlyStep(key, nextStepKey));
|
||||
Step.StepKey key = new Step.StepKey(phase, NAME, NAME);
|
||||
Settings readOnlySettings = Settings.builder().put(IndexMetaData.SETTING_BLOCKS_WRITE, true).build();
|
||||
return Collections.singletonList(new UpdateSettingsStep(key, nextStepKey, client, readOnlySettings));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.cluster.ClusterState;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.cluster.metadata.MetaData;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.index.Index;
|
||||
|
||||
public class ReadOnlyStep extends ClusterStateActionStep {
|
||||
public static final String NAME = "read-only";
|
||||
|
||||
ReadOnlyStep(StepKey key, StepKey nextStepKey) {
|
||||
super(key, nextStepKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClusterState performAction(Index index, ClusterState clusterState) {
|
||||
return ClusterState.builder(clusterState).metaData(MetaData.builder(clusterState.metaData())
|
||||
.updateSettings(Settings.builder().put(IndexMetaData.SETTING_BLOCKS_WRITE, true).build(),
|
||||
index.getName())).build();
|
||||
}
|
||||
}
|
|
@ -6,10 +6,12 @@
|
|||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
|
@ -73,9 +75,10 @@ public class ReplicasAction implements LifecycleAction {
|
|||
|
||||
@Override
|
||||
public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey) {
|
||||
StepKey updateReplicasKey = new StepKey(phase, NAME, UpdateReplicaSettingsStep.NAME);
|
||||
StepKey updateReplicasKey = new StepKey(phase, NAME, UpdateSettingsStep.NAME);
|
||||
StepKey enoughKey = new StepKey(phase, NAME, ReplicasAllocatedStep.NAME);
|
||||
return Arrays.asList(new UpdateReplicaSettingsStep(updateReplicasKey, enoughKey, client, numberOfReplicas),
|
||||
Settings replicaSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, numberOfReplicas).build();
|
||||
return Arrays.asList(new UpdateSettingsStep(updateReplicasKey, enoughKey, client, replicaSettings),
|
||||
new ReplicasAllocatedStep(enoughKey, nextStepKey, numberOfReplicas));
|
||||
}
|
||||
|
||||
|
|
|
@ -8,9 +8,9 @@ package org.elasticsearch.xpack.core.indexlifecycle;
|
|||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.common.unit.ByteSizeValue;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.index.Index;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
|
@ -32,7 +32,8 @@ public class RolloverStep extends AsyncActionStep {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void performAction(Index index, Listener listener) {
|
||||
public void performAction(IndexMetaData indexMetaData, Listener listener) {
|
||||
// TODO(talevy): shouldn't we double-check that this alias is managed by us/this-index?
|
||||
RolloverRequest rolloverRequest = new RolloverRequest(alias, null);
|
||||
if (maxAge != null) {
|
||||
rolloverRequest.addMaxIndexAgeCondition(maxAge);
|
||||
|
|
|
@ -150,6 +150,17 @@ public class ShrinkAction implements LifecycleAction {
|
|||
// return clusterState;
|
||||
// });
|
||||
|
||||
// UpdateSettingsStep allocateStep = new UpdateSettingsStep();
|
||||
// UpdateSettingsStep waitForAllocation = new UpdateSettingsStep();
|
||||
// UpdateSettingsStep allocateStep = new UpdateSettingsStep();
|
||||
// Step.StepKey allocateKey = new StepKey(phase, NAME, NAME);
|
||||
// StepKey allocationRoutedKey = new StepKey(phase, NAME, AllocationRoutedStep.NAME);
|
||||
//
|
||||
// Settings.Builder newSettings = Settings.builder();
|
||||
// newSettings.put(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_name", value));
|
||||
// UpdateSettingsStep allocateStep = new UpdateSettingsStep(allocateKey, allocationRoutedKey, client, newSettings.build());
|
||||
// AllocationRoutedStep routedCheckStep = new AllocationRoutedStep(allocationRoutedKey, nextStepKey);
|
||||
// return Arrays.asList(allocateStep, routedCheckStep);
|
||||
return Arrays.asList();
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.admin.indices.alias.Alias;
|
||||
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class ShrinkStep extends AsyncActionStep {
|
||||
public static final String NAME = "shrink";
|
||||
|
||||
private String shrunkenIndexName;
|
||||
|
||||
public ShrinkStep(StepKey key, StepKey nextStepKey, Client client, String shrunkenIndexName) {
|
||||
super(key, nextStepKey, client);
|
||||
this.shrunkenIndexName = shrunkenIndexName;
|
||||
}
|
||||
|
||||
public String getShrunkenIndexName() {
|
||||
return shrunkenIndexName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performAction(IndexMetaData indexMetaData, Listener listener) {
|
||||
Long lifecycleDate = LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE_SETTING.get(indexMetaData.getSettings());
|
||||
if (lifecycleDate == null) {
|
||||
throw new IllegalStateException("source index[" + indexMetaData.getIndex().getName() +
|
||||
"] is missing setting[" + LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE);
|
||||
}
|
||||
|
||||
Settings relevantTargetSettings = Settings.builder()
|
||||
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, indexMetaData.getNumberOfShards())
|
||||
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, indexMetaData.getNumberOfReplicas())
|
||||
.put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, lifecycleDate)
|
||||
.build();
|
||||
ResizeRequest resizeRequest = new ResizeRequest(shrunkenIndexName, indexMetaData.getIndex().getName());
|
||||
indexMetaData.getAliases().values().spliterator().forEachRemaining(aliasMetaDataObjectCursor -> {
|
||||
resizeRequest.getTargetIndexRequest().alias(new Alias(aliasMetaDataObjectCursor.value.alias()));
|
||||
});
|
||||
resizeRequest.getTargetIndexRequest().settings(relevantTargetSettings);
|
||||
|
||||
getClient().admin().indices().resizeIndex(resizeRequest, ActionListener.wrap(response -> {
|
||||
// TODO(talevy): when is this not acknowledged?
|
||||
listener.onResponse(response.isAcknowledged());
|
||||
}, listener::onFailure));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), shrunkenIndexName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
ShrinkStep other = (ShrinkStep) obj;
|
||||
return super.equals(obj) && Objects.equals(shrunkenIndexName, other.shrunkenIndexName);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,76 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.index.Index;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class UpdateAllocationSettingsStep extends AsyncActionStep {
|
||||
public static final String NAME = "update-allocation";
|
||||
|
||||
private final Map<String, String> include;
|
||||
private final Map<String, String> exclude;
|
||||
private final Map<String, String> require;
|
||||
|
||||
public UpdateAllocationSettingsStep(StepKey key, StepKey nextStepKey, Client client, Map<String, String> include,
|
||||
Map<String, String> exclude, Map<String, String> require) {
|
||||
super(key, nextStepKey, client);
|
||||
this.include = include;
|
||||
this.exclude = exclude;
|
||||
this.require = require;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performAction(Index index, Listener listener) {
|
||||
Settings.Builder newSettings = Settings.builder();
|
||||
include.forEach((key, value) -> newSettings.put(IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + key, value));
|
||||
exclude.forEach((key, value) -> newSettings.put(IndexMetaData.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + key, value));
|
||||
require.forEach((key, value) -> newSettings.put(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + key, value));
|
||||
|
||||
UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(index.getName()).settings(newSettings);
|
||||
getClient().admin().indices().updateSettings(updateSettingsRequest,
|
||||
ActionListener.wrap(response -> listener.onResponse(true), listener::onFailure));
|
||||
}
|
||||
|
||||
Map<String, String> getInclude() {
|
||||
return include;
|
||||
}
|
||||
|
||||
Map<String, String> getExclude() {
|
||||
return exclude;
|
||||
}
|
||||
|
||||
Map<String, String> getRequire() {
|
||||
return require;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), include, exclude, require);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
UpdateAllocationSettingsStep other = (UpdateAllocationSettingsStep) obj;
|
||||
return super.equals(obj) &&
|
||||
Objects.equals(include, other.include) &&
|
||||
Objects.equals(exclude, other.exclude) &&
|
||||
Objects.equals(require, other.require);
|
||||
}
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.cluster.ClusterState;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.cluster.metadata.MetaData;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.index.Index;
|
||||
import org.elasticsearch.index.codec.CodecService;
|
||||
import org.elasticsearch.index.engine.EngineConfig;
|
||||
|
||||
public class UpdateBestCompressionSettingsStep extends ClusterStateActionStep {
|
||||
public static final String NAME = "update-best-compression";
|
||||
|
||||
public UpdateBestCompressionSettingsStep(StepKey key, StepKey nextStepKey) {
|
||||
super(key, nextStepKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClusterState performAction(Index index, ClusterState clusterState) {
|
||||
IndexMetaData idxMeta = clusterState.metaData().index(index);
|
||||
if (idxMeta == null) {
|
||||
return clusterState;
|
||||
}
|
||||
Settings.Builder newSettings = Settings.builder()
|
||||
.put(EngineConfig.INDEX_CODEC_SETTING.getKey(), CodecService.BEST_COMPRESSION_CODEC);
|
||||
|
||||
return ClusterState.builder(clusterState)
|
||||
.metaData(MetaData.builder(clusterState.metaData())
|
||||
.updateSettings(newSettings.build(), index.getName())).build();
|
||||
}
|
||||
}
|
|
@ -14,31 +14,30 @@ import org.elasticsearch.index.Index;
|
|||
|
||||
import java.util.Objects;
|
||||
|
||||
public class UpdateReplicaSettingsStep extends AsyncActionStep {
|
||||
public static final String NAME = "update-replicas";
|
||||
public class UpdateSettingsStep extends AsyncActionStep {
|
||||
public static final String NAME = "update-settings";
|
||||
|
||||
private int numberOfReplicas;
|
||||
private final Settings settings;
|
||||
|
||||
public UpdateReplicaSettingsStep(StepKey key, StepKey nextStepKey, Client client, int numberOfReplicas) {
|
||||
public UpdateSettingsStep(StepKey key, StepKey nextStepKey, Client client, Settings settings) {
|
||||
super(key, nextStepKey, client);
|
||||
this.numberOfReplicas = numberOfReplicas;
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performAction(Index index, Listener listener) {
|
||||
UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(index.getName())
|
||||
.settings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, numberOfReplicas));
|
||||
public void performAction(IndexMetaData indexMetaData, Listener listener) {
|
||||
UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indexMetaData.getIndex().getName()).settings(settings);
|
||||
getClient().admin().indices().updateSettings(updateSettingsRequest,
|
||||
ActionListener.wrap(response -> listener.onResponse(true), listener::onFailure));
|
||||
}
|
||||
|
||||
public int getNumberOfReplicas() {
|
||||
return numberOfReplicas;
|
||||
public Settings getSettings() {
|
||||
return settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), numberOfReplicas);
|
||||
return Objects.hash(super.hashCode(), settings);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -49,8 +48,8 @@ public class UpdateReplicaSettingsStep extends AsyncActionStep {
|
|||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
UpdateReplicaSettingsStep other = (UpdateReplicaSettingsStep) obj;
|
||||
UpdateSettingsStep other = (UpdateSettingsStep) obj;
|
||||
return super.equals(obj) &&
|
||||
Objects.equals(numberOfReplicas, other.numberOfReplicas);
|
||||
Objects.equals(settings, other.settings);
|
||||
}
|
||||
}
|
|
@ -5,7 +5,9 @@
|
|||
*/
|
||||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.common.io.stream.Writeable.Reader;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.test.AbstractSerializingTestCase;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
|
||||
|
@ -15,6 +17,8 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
public class AllocateActionTests extends AbstractSerializingTestCase<AllocateAction> {
|
||||
|
||||
@Override
|
||||
|
@ -104,16 +108,21 @@ public class AllocateActionTests extends AbstractSerializingTestCase<AllocateAct
|
|||
randomAlphaOfLengthBetween(1, 10));
|
||||
List<Step> steps = action.toSteps(null, phase, nextStepKey);
|
||||
assertNotNull(steps);
|
||||
assertEquals(3, steps.size());
|
||||
StepKey expectedFirstStepKey = new StepKey(phase, AllocateAction.NAME, UpdateAllocationSettingsStep.NAME);
|
||||
assertEquals(2, steps.size());
|
||||
StepKey expectedFirstStepKey = new StepKey(phase, AllocateAction.NAME, AllocateAction.NAME);
|
||||
StepKey expectedSecondStepKey = new StepKey(phase, AllocateAction.NAME, AllocationRoutedStep.NAME);
|
||||
UpdateAllocationSettingsStep firstStep = (UpdateAllocationSettingsStep) steps.get(1);
|
||||
UpdateSettingsStep firstStep = (UpdateSettingsStep) steps.get(0);
|
||||
assertEquals(expectedFirstStepKey, firstStep.getKey());
|
||||
assertEquals(expectedSecondStepKey, firstStep.getNextStepKey());
|
||||
assertEquals(action.getInclude(), firstStep.getInclude());
|
||||
assertEquals(action.getExclude(), firstStep.getExclude());
|
||||
assertEquals(action.getRequire(), firstStep.getRequire());
|
||||
AllocationRoutedStep secondStep = (AllocationRoutedStep) steps.get(2);
|
||||
Settings.Builder expectedSettings = Settings.builder();
|
||||
action.getInclude().forEach(
|
||||
(key, value) -> expectedSettings.put(IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + key, value));
|
||||
action.getExclude().forEach(
|
||||
(key, value) -> expectedSettings.put(IndexMetaData.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + key, value));
|
||||
action.getRequire().forEach(
|
||||
(key, value) -> expectedSettings.put(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + key, value));
|
||||
assertThat(firstStep.getSettings(), equalTo(expectedSettings.build()));
|
||||
AllocationRoutedStep secondStep = (AllocationRoutedStep) steps.get(1);
|
||||
assertEquals(expectedSecondStepKey, secondStep.getKey());
|
||||
assertEquals(nextStepKey, secondStep.getNextStepKey());
|
||||
}
|
||||
|
|
|
@ -7,12 +7,14 @@ package org.elasticsearch.xpack.core.indexlifecycle;
|
|||
|
||||
|
||||
import org.apache.lucene.util.SetOnce;
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
||||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
|
||||
import org.elasticsearch.client.AdminClient;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.client.IndicesAdminClient;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.index.Index;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.mockito.Mockito;
|
||||
|
@ -28,7 +30,8 @@ public class DeleteStepTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testDeleted() {
|
||||
Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
|
||||
Client client = Mockito.mock(Client.class);
|
||||
AdminClient adminClient = Mockito.mock(AdminClient.class);
|
||||
|
@ -42,7 +45,7 @@ public class DeleteStepTests extends ESTestCase {
|
|||
ActionListener<DeleteIndexResponse> listener = (ActionListener<DeleteIndexResponse>) invocation.getArguments()[1];
|
||||
assertNotNull(request);
|
||||
assertEquals(1, request.indices().length);
|
||||
assertEquals(index.getName(), request.indices()[0]);
|
||||
assertEquals(indexMetaData.getIndex().getName(), request.indices()[0]);
|
||||
listener.onResponse(null);
|
||||
return null;
|
||||
}).when(indicesClient).delete(Mockito.any(), Mockito.any());
|
||||
|
@ -50,7 +53,7 @@ public class DeleteStepTests extends ESTestCase {
|
|||
SetOnce<Boolean> actionCompleted = new SetOnce<>();
|
||||
|
||||
DeleteStep step = new DeleteStep(null, null, client);
|
||||
step.performAction(index, new AsyncActionStep.Listener() {
|
||||
step.performAction(indexMetaData, new AsyncActionStep.Listener() {
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
||||
actionCompleted.set(complete);
|
||||
|
@ -70,8 +73,8 @@ public class DeleteStepTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testExceptionThrown() {
|
||||
|
||||
Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Exception exception = new RuntimeException();
|
||||
|
||||
Client client = Mockito.mock(Client.class);
|
||||
|
@ -89,7 +92,7 @@ public class DeleteStepTests extends ESTestCase {
|
|||
ActionListener<DeleteIndexResponse> listener = (ActionListener<DeleteIndexResponse>) invocation.getArguments()[1];
|
||||
assertNotNull(request);
|
||||
assertEquals(1, request.indices().length);
|
||||
assertEquals(index.getName(), request.indices()[0]);
|
||||
assertEquals(indexMetaData.getIndex().getName(), request.indices()[0]);
|
||||
listener.onFailure(exception);
|
||||
return null;
|
||||
}
|
||||
|
@ -98,7 +101,7 @@ public class DeleteStepTests extends ESTestCase {
|
|||
|
||||
SetOnce<Boolean> exceptionThrown = new SetOnce<>();
|
||||
DeleteStep step = new DeleteStep(null, null, client);
|
||||
step.performAction(index, new AsyncActionStep.Listener() {
|
||||
step.performAction(indexMetaData, new AsyncActionStep.Listener() {
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
||||
throw new AssertionError("Unexpected method call");
|
||||
|
|
|
@ -7,6 +7,7 @@ package org.elasticsearch.xpack.core.indexlifecycle;
|
|||
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.io.stream.Writeable.Reader;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.DeprecationHandler;
|
||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
|
@ -68,17 +69,19 @@ public class ForceMergeActionTests extends AbstractSerializingTestCase<ForceMerg
|
|||
StepKey nextStepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
List<Step> steps = instance.toSteps(null, phase, nextStepKey);
|
||||
assertNotNull(steps);
|
||||
int segmentCountIndex = 1;
|
||||
int nextFirstIndex = 0;
|
||||
if (instance.isBestCompression()) {
|
||||
Settings expectedSettings = Settings.builder().put("index.codec", "best_compression").build();
|
||||
assertEquals(3, steps.size());
|
||||
UpdateBestCompressionSettingsStep firstStep = (UpdateBestCompressionSettingsStep) steps.get(0);
|
||||
assertThat(firstStep.getKey(), equalTo(new StepKey(phase, ForceMergeAction.NAME, UpdateBestCompressionSettingsStep.NAME)));
|
||||
segmentCountIndex = 2;
|
||||
UpdateSettingsStep firstStep = (UpdateSettingsStep) steps.get(0);
|
||||
assertThat(firstStep.getKey(), equalTo(new StepKey(phase, ForceMergeAction.NAME, "best_compression")));
|
||||
assertThat(firstStep.getSettings(), equalTo(expectedSettings));
|
||||
nextFirstIndex = 1;
|
||||
} else {
|
||||
assertEquals(2, steps.size());
|
||||
}
|
||||
ForceMergeStep firstStep = (ForceMergeStep) steps.get(0);
|
||||
SegmentCountStep secondStep = (SegmentCountStep) steps.get(segmentCountIndex);
|
||||
ForceMergeStep firstStep = (ForceMergeStep) steps.get(nextFirstIndex);
|
||||
SegmentCountStep secondStep = (SegmentCountStep) steps.get(nextFirstIndex + 1);
|
||||
assertThat(firstStep.getKey(), equalTo(new StepKey(phase, ForceMergeAction.NAME, ForceMergeStep.NAME)));
|
||||
assertThat(firstStep.getNextStepKey(), equalTo(secondStep.getKey()));
|
||||
assertThat(secondStep.getKey(), equalTo(new StepKey(phase, ForceMergeAction.NAME, SegmentCountStep.NAME)));
|
||||
|
|
|
@ -7,12 +7,14 @@ package org.elasticsearch.xpack.core.indexlifecycle;
|
|||
|
||||
|
||||
import org.apache.lucene.util.SetOnce;
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
|
||||
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
|
||||
import org.elasticsearch.client.AdminClient;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.client.IndicesAdminClient;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.index.Index;
|
||||
import org.elasticsearch.rest.RestStatus;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
@ -65,6 +67,8 @@ public class ForceMergeStepTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testPerformActionComplete() {
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Step.StepKey stepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
StepKey nextStepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
int maxNumSegments = randomIntBetween(1, 10);
|
||||
|
@ -85,9 +89,8 @@ public class ForceMergeStepTests extends ESTestCase {
|
|||
}).when(indicesClient).forceMerge(any(), any());
|
||||
|
||||
ForceMergeStep step = new ForceMergeStep(stepKey, nextStepKey, client, maxNumSegments);
|
||||
Index index = new Index(randomAlphaOfLength(5), randomAlphaOfLength(5));
|
||||
SetOnce<Boolean> completed = new SetOnce<>();
|
||||
step.performAction(index, new AsyncActionStep.Listener() {
|
||||
step.performAction(indexMetaData, new AsyncActionStep.Listener() {
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
||||
completed.set(complete);
|
||||
|
@ -102,6 +105,8 @@ public class ForceMergeStepTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testPerformActionThrowsException() {
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Exception exception = new RuntimeException("error");
|
||||
Step.StepKey stepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
StepKey nextStepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
|
@ -115,6 +120,8 @@ public class ForceMergeStepTests extends ESTestCase {
|
|||
Mockito.when(forceMergeResponse.getStatus()).thenReturn(RestStatus.OK);
|
||||
Mockito.doAnswer(invocationOnMock -> {
|
||||
ForceMergeRequest request = (ForceMergeRequest) invocationOnMock.getArguments()[0];
|
||||
assertThat(request.indices().length, equalTo(1));
|
||||
assertThat(request.indices()[0], equalTo(indexMetaData.getIndex().getName()));
|
||||
assertThat(request.maxNumSegments(), equalTo(maxNumSegments));
|
||||
@SuppressWarnings("unchecked")
|
||||
ActionListener<ForceMergeResponse> listener = (ActionListener<ForceMergeResponse>) invocationOnMock.getArguments()[1];
|
||||
|
@ -123,9 +130,8 @@ public class ForceMergeStepTests extends ESTestCase {
|
|||
}).when(indicesClient).forceMerge(any(), any());
|
||||
|
||||
ForceMergeStep step = new ForceMergeStep(stepKey, nextStepKey, client, maxNumSegments);
|
||||
Index index = new Index(randomAlphaOfLength(5), randomAlphaOfLength(5));
|
||||
SetOnce<Boolean> exceptionThrown = new SetOnce<>();
|
||||
step.performAction(index, new AsyncActionStep.Listener() {
|
||||
step.performAction(indexMetaData, new AsyncActionStep.Listener() {
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
||||
throw new AssertionError("unexpected method call");
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.common.io.stream.Writeable.Reader;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.test.AbstractSerializingTestCase;
|
||||
|
@ -39,10 +40,12 @@ public class ReadOnlyActionTests extends AbstractSerializingTestCase<ReadOnlyAct
|
|||
List<Step> steps = action.toSteps(null, phase, nextStepKey);
|
||||
assertNotNull(steps);
|
||||
assertEquals(1, steps.size());
|
||||
StepKey expectedFirstStepKey = new StepKey(phase, ReadOnlyAction.NAME, ReadOnlyStep.NAME);
|
||||
ReadOnlyStep firstStep = (ReadOnlyStep) steps.get(0);
|
||||
StepKey expectedFirstStepKey = new StepKey(phase, ReadOnlyAction.NAME, ReadOnlyAction.NAME);
|
||||
UpdateSettingsStep firstStep = (UpdateSettingsStep) steps.get(0);
|
||||
assertThat(firstStep.getKey(), equalTo(expectedFirstStepKey));
|
||||
assertThat(firstStep.getNextStepKey(), equalTo(nextStepKey));
|
||||
assertThat(firstStep.getSettings().size(), equalTo(1));
|
||||
assertTrue(IndexMetaData.INDEX_BLOCKS_WRITE_SETTING.get(firstStep.getSettings()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.cluster.ClusterName;
|
||||
import org.elasticsearch.cluster.ClusterState;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.cluster.metadata.MetaData;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.index.Index;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.test.EqualsHashCodeTestUtils;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
public class ReadOnlyStepTests extends ESTestCase {
|
||||
|
||||
public ReadOnlyStep createRandomInstance() {
|
||||
StepKey stepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
StepKey nextStepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
|
||||
return new ReadOnlyStep(stepKey, nextStepKey);
|
||||
}
|
||||
public void testHashcodeAndEquals() {
|
||||
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createRandomInstance(),
|
||||
instance -> new ReadOnlyStep(instance.getKey(), instance.getNextStepKey()));
|
||||
}
|
||||
|
||||
public void testPerformAction() {
|
||||
Settings.Builder indexSettingsBuilder = settings(Version.CURRENT);
|
||||
if (randomBoolean()) {
|
||||
indexSettingsBuilder.put(IndexMetaData.SETTING_BLOCKS_WRITE, randomBoolean());
|
||||
}
|
||||
IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLength(5))
|
||||
.settings(settings(Version.CURRENT))
|
||||
.numberOfShards(1).numberOfReplicas(0).build();
|
||||
MetaData metaData = MetaData.builder()
|
||||
.persistentSettings(settings(Version.CURRENT).build())
|
||||
.put(IndexMetaData.builder(indexMetadata))
|
||||
.build();
|
||||
Index index = indexMetadata.getIndex();
|
||||
ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).build();
|
||||
ReadOnlyStep step = new ReadOnlyStep(null, null);
|
||||
ClusterState newState = step.performAction(index, clusterState);
|
||||
assertThat(newState.metaData().index(index).getSettings().get(IndexMetaData.SETTING_BLOCKS_WRITE), equalTo("true"));
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.common.io.stream.Writeable.Reader;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.test.AbstractSerializingTestCase;
|
||||
|
@ -49,12 +50,13 @@ public class ReplicasActionTests extends AbstractSerializingTestCase<ReplicasAct
|
|||
List<Step> steps = action.toSteps(null, phase, nextStepKey);
|
||||
assertNotNull(steps);
|
||||
assertEquals(2, steps.size());
|
||||
StepKey expectedFirstStepKey = new StepKey(phase, ReplicasAction.NAME, UpdateReplicaSettingsStep.NAME);
|
||||
StepKey expectedFirstStepKey = new StepKey(phase, ReplicasAction.NAME, UpdateSettingsStep.NAME);
|
||||
StepKey expectedSecondStepKey = new StepKey(phase, ReplicasAction.NAME, ReplicasAllocatedStep.NAME);
|
||||
UpdateReplicaSettingsStep firstStep = (UpdateReplicaSettingsStep) steps.get(0);
|
||||
UpdateSettingsStep firstStep = (UpdateSettingsStep) steps.get(0);
|
||||
assertEquals(expectedFirstStepKey, firstStep.getKey());
|
||||
assertEquals(expectedSecondStepKey, firstStep.getNextStepKey());
|
||||
assertEquals(action.getNumberOfReplicas(), firstStep.getNumberOfReplicas());
|
||||
assertEquals(1, firstStep.getSettings().size());
|
||||
assertEquals(action.getNumberOfReplicas(), IndexMetaData.INDEX_NUMBER_OF_REPLICAS_SETTING.get(firstStep.getSettings()).intValue());
|
||||
ReplicasAllocatedStep secondStep = (ReplicasAllocatedStep) steps.get(1);
|
||||
assertEquals(expectedSecondStepKey, secondStep.getKey());
|
||||
assertEquals(nextStepKey, secondStep.getNextStepKey());
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
import org.apache.lucene.util.SetOnce;
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.admin.indices.rollover.Condition;
|
||||
import org.elasticsearch.action.admin.indices.rollover.MaxAgeCondition;
|
||||
|
@ -17,10 +18,10 @@ import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
|
|||
import org.elasticsearch.client.AdminClient;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.client.IndicesAdminClient;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.common.unit.ByteSizeUnit;
|
||||
import org.elasticsearch.common.unit.ByteSizeValue;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.index.Index;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.test.EqualsHashCodeTestUtils;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.AsyncActionStep.Listener;
|
||||
|
@ -101,7 +102,8 @@ public class RolloverStepTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testPerformAction() throws Exception {
|
||||
Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
|
||||
RolloverStep step = createRandomInstance();
|
||||
|
||||
|
@ -135,7 +137,7 @@ public class RolloverStepTests extends ESTestCase {
|
|||
}).when(indicesClient).rolloverIndex(Mockito.any(), Mockito.any());
|
||||
|
||||
SetOnce<Boolean> actionCompleted = new SetOnce<>();
|
||||
step.performAction(index, new Listener() {
|
||||
step.performAction(indexMetaData, new Listener() {
|
||||
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
||||
|
@ -156,8 +158,8 @@ public class RolloverStepTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testPerformActionNotComplete() throws Exception {
|
||||
Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
|
||||
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
RolloverStep step = createRandomInstance();
|
||||
|
||||
AdminClient adminClient = Mockito.mock(AdminClient.class);
|
||||
|
@ -190,7 +192,7 @@ public class RolloverStepTests extends ESTestCase {
|
|||
}).when(indicesClient).rolloverIndex(Mockito.any(), Mockito.any());
|
||||
|
||||
SetOnce<Boolean> actionCompleted = new SetOnce<>();
|
||||
step.performAction(index, new Listener() {
|
||||
step.performAction(indexMetaData, new Listener() {
|
||||
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
||||
|
@ -211,7 +213,8 @@ public class RolloverStepTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testPerformActionFailure() throws Exception {
|
||||
Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Exception exception = new RuntimeException();
|
||||
RolloverStep step = createRandomInstance();
|
||||
|
||||
|
@ -245,7 +248,7 @@ public class RolloverStepTests extends ESTestCase {
|
|||
}).when(indicesClient).rolloverIndex(Mockito.any(), Mockito.any());
|
||||
|
||||
SetOnce<Boolean> exceptionThrown = new SetOnce<>();
|
||||
step.performAction(index, new Listener() {
|
||||
step.performAction(indexMetaData, new Listener() {
|
||||
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
||||
|
|
|
@ -0,0 +1,241 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
import org.apache.lucene.util.SetOnce;
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.admin.indices.alias.Alias;
|
||||
import org.elasticsearch.action.admin.indices.rollover.Condition;
|
||||
import org.elasticsearch.action.admin.indices.rollover.MaxAgeCondition;
|
||||
import org.elasticsearch.action.admin.indices.rollover.MaxDocsCondition;
|
||||
import org.elasticsearch.action.admin.indices.rollover.MaxSizeCondition;
|
||||
import org.elasticsearch.action.admin.indices.rollover.RolloverIndexTestHelper;
|
||||
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
|
||||
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
|
||||
import org.elasticsearch.action.admin.indices.shrink.ResizeAction;
|
||||
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
|
||||
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
|
||||
import org.elasticsearch.client.AdminClient;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.client.IndicesAdminClient;
|
||||
import org.elasticsearch.cluster.metadata.AliasMetaData;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.unit.ByteSizeUnit;
|
||||
import org.elasticsearch.common.unit.ByteSizeValue;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.test.EqualsHashCodeTestUtils;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.AsyncActionStep.Listener;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
|
||||
import org.junit.Before;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
public class ShrinkStepTests extends ESTestCase {
|
||||
|
||||
private Client client;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
client = Mockito.mock(Client.class);
|
||||
}
|
||||
|
||||
public ShrinkStep createRandomInstance() {
|
||||
StepKey stepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
StepKey nextStepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
String shrunkenIndexName = randomAlphaOfLengthBetween(1, 20);
|
||||
return new ShrinkStep(stepKey, nextStepKey, client, shrunkenIndexName);
|
||||
}
|
||||
|
||||
public ShrinkStep mutateInstance(ShrinkStep instance) {
|
||||
StepKey key = instance.getKey();
|
||||
StepKey nextKey = instance.getNextStepKey();
|
||||
String shrunkenIndexName = instance.getShrunkenIndexName();
|
||||
|
||||
switch (between(0, 2)) {
|
||||
case 0:
|
||||
key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5));
|
||||
break;
|
||||
case 1:
|
||||
nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5));
|
||||
break;
|
||||
case 2:
|
||||
shrunkenIndexName = shrunkenIndexName + randomAlphaOfLengthBetween(1, 5);
|
||||
break;
|
||||
default:
|
||||
throw new AssertionError("Illegal randomisation branch");
|
||||
}
|
||||
|
||||
return new ShrinkStep(key, nextKey, instance.getClient(), shrunkenIndexName);
|
||||
}
|
||||
|
||||
public void testHashcodeAndEquals() {
|
||||
EqualsHashCodeTestUtils
|
||||
.checkEqualsAndHashCode(createRandomInstance(),
|
||||
instance -> new ShrinkStep(instance.getKey(), instance.getNextStepKey(), instance.getClient(),
|
||||
instance.getShrunkenIndexName()),
|
||||
this::mutateInstance);
|
||||
}
|
||||
|
||||
public void testPerformAction() throws Exception {
|
||||
long creationDate = randomNonNegativeLong();
|
||||
IndexMetaData sourceIndexMetaData = IndexMetaData.builder(randomAlphaOfLength(10))
|
||||
.settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, creationDate))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5))
|
||||
.putAlias(AliasMetaData.builder("my_alias"))
|
||||
.build();
|
||||
|
||||
ShrinkStep step = createRandomInstance();
|
||||
|
||||
AdminClient adminClient = Mockito.mock(AdminClient.class);
|
||||
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
|
||||
|
||||
Mockito.when(client.admin()).thenReturn(adminClient);
|
||||
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
|
||||
Mockito.doAnswer(new Answer<Void>() {
|
||||
|
||||
@Override
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
ResizeRequest request = (ResizeRequest) invocation.getArguments()[0];
|
||||
@SuppressWarnings("unchecked")
|
||||
ActionListener<ResizeResponse> listener = (ActionListener<ResizeResponse>) invocation.getArguments()[1];
|
||||
assertThat(request.getSourceIndex(), equalTo(sourceIndexMetaData.getIndex().getName()));
|
||||
assertThat(request.getTargetIndexRequest().aliases(), equalTo(Collections.singleton(new Alias("my_alias"))));
|
||||
assertThat(request.getTargetIndexRequest().settings(), equalTo(Settings.builder()
|
||||
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, sourceIndexMetaData.getNumberOfShards())
|
||||
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, sourceIndexMetaData.getNumberOfReplicas())
|
||||
.put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, creationDate).build()));
|
||||
assertThat(request.getTargetIndexRequest().index(), equalTo(step.getShrunkenIndexName()));
|
||||
ResizeResponse resizeResponse = ResizeAction.INSTANCE.newResponse();
|
||||
resizeResponse.readFrom(StreamInput.wrap(new byte[] { 1, 1, 1, 1, 1 }));
|
||||
listener.onResponse(resizeResponse);
|
||||
return null;
|
||||
}
|
||||
|
||||
}).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any());
|
||||
|
||||
SetOnce<Boolean> actionCompleted = new SetOnce<>();
|
||||
step.performAction(sourceIndexMetaData, new Listener() {
|
||||
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
||||
actionCompleted.set(complete);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Exception e) {
|
||||
throw new AssertionError("Unexpected method call", e);
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(true, actionCompleted.get());
|
||||
|
||||
Mockito.verify(client, Mockito.only()).admin();
|
||||
Mockito.verify(adminClient, Mockito.only()).indices();
|
||||
Mockito.verify(indicesClient, Mockito.only()).resizeIndex(Mockito.any(), Mockito.any());
|
||||
}
|
||||
|
||||
public void testPerformActionNotComplete() throws Exception {
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
ShrinkStep step = createRandomInstance();
|
||||
|
||||
AdminClient adminClient = Mockito.mock(AdminClient.class);
|
||||
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
|
||||
|
||||
Mockito.when(client.admin()).thenReturn(adminClient);
|
||||
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
|
||||
Mockito.doAnswer(new Answer<Void>() {
|
||||
|
||||
@Override
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
@SuppressWarnings("unchecked")
|
||||
ActionListener<ResizeResponse> listener = (ActionListener<ResizeResponse>) invocation.getArguments()[1];
|
||||
ResizeResponse resizeResponse = ResizeAction.INSTANCE.newResponse();
|
||||
resizeResponse.readFrom(StreamInput.wrap(new byte[] { 0, 0, 0, 0, 0 }));
|
||||
listener.onResponse(resizeResponse);
|
||||
return null;
|
||||
}
|
||||
|
||||
}).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any());
|
||||
|
||||
SetOnce<Boolean> actionCompleted = new SetOnce<>();
|
||||
step.performAction(indexMetaData, new Listener() {
|
||||
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
||||
actionCompleted.set(complete);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Exception e) {
|
||||
throw new AssertionError("Unexpected method call", e);
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(false, actionCompleted.get());
|
||||
|
||||
Mockito.verify(client, Mockito.only()).admin();
|
||||
Mockito.verify(adminClient, Mockito.only()).indices();
|
||||
Mockito.verify(indicesClient, Mockito.only()).resizeIndex(Mockito.any(), Mockito.any());
|
||||
}
|
||||
|
||||
public void testPerformActionFailure() throws Exception {
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Exception exception = new RuntimeException();
|
||||
ShrinkStep step = createRandomInstance();
|
||||
|
||||
AdminClient adminClient = Mockito.mock(AdminClient.class);
|
||||
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
|
||||
|
||||
Mockito.when(client.admin()).thenReturn(adminClient);
|
||||
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
|
||||
Mockito.doAnswer(new Answer<Void>() {
|
||||
|
||||
@Override
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
@SuppressWarnings("unchecked")
|
||||
ActionListener<RolloverResponse> listener = (ActionListener<RolloverResponse>) invocation.getArguments()[1];
|
||||
listener.onFailure(exception);
|
||||
return null;
|
||||
}
|
||||
|
||||
}).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any());
|
||||
|
||||
SetOnce<Boolean> exceptionThrown = new SetOnce<>();
|
||||
step.performAction(indexMetaData, new Listener() {
|
||||
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
||||
throw new AssertionError("Unexpected method call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Exception e) {
|
||||
assertSame(exception, e);
|
||||
exceptionThrown.set(true);
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(true, exceptionThrown.get());
|
||||
|
||||
Mockito.verify(client, Mockito.only()).admin();
|
||||
Mockito.verify(adminClient, Mockito.only()).indices();
|
||||
Mockito.verify(indicesClient, Mockito.only()).resizeIndex(Mockito.any(), Mockito.any());
|
||||
}
|
||||
|
||||
}
|
|
@ -1,197 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
|
||||
import org.apache.lucene.util.SetOnce;
|
||||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
|
||||
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse;
|
||||
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsTestHelper;
|
||||
import org.elasticsearch.client.AdminClient;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.client.IndicesAdminClient;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.index.Index;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.test.EqualsHashCodeTestUtils;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.AsyncActionStep.Listener;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
|
||||
import org.junit.Before;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class UpdateAllocationSettingsStepTests extends ESTestCase {
|
||||
|
||||
private Client client;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
client = Mockito.mock(Client.class);
|
||||
}
|
||||
|
||||
public UpdateAllocationSettingsStep createRandomInstance() {
|
||||
StepKey stepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
StepKey nextStepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
Map<String, String> include = AllocateActionTests.randomMap(0, 10);
|
||||
Map<String, String> exclude = AllocateActionTests.randomMap(0, 10);
|
||||
Map<String, String> require = AllocateActionTests.randomMap(0, 10);
|
||||
|
||||
return new UpdateAllocationSettingsStep(stepKey, nextStepKey, client, include, exclude, require);
|
||||
}
|
||||
|
||||
public UpdateAllocationSettingsStep mutateInstance(UpdateAllocationSettingsStep instance) {
|
||||
StepKey key = instance.getKey();
|
||||
StepKey nextKey = instance.getNextStepKey();
|
||||
Map<String, String> include = instance.getInclude();
|
||||
Map<String, String> exclude = instance.getExclude();
|
||||
Map<String, String> require = instance.getRequire();
|
||||
|
||||
switch (between(0, 4)) {
|
||||
case 0:
|
||||
key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5));
|
||||
break;
|
||||
case 1:
|
||||
nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5));
|
||||
break;
|
||||
case 2:
|
||||
include = new HashMap<>(include);
|
||||
include.put(randomAlphaOfLengthBetween(11, 15), randomAlphaOfLengthBetween(1, 20));
|
||||
break;
|
||||
case 3:
|
||||
exclude = new HashMap<>(exclude);
|
||||
exclude.put(randomAlphaOfLengthBetween(11, 15), randomAlphaOfLengthBetween(1, 20));
|
||||
break;
|
||||
case 4:
|
||||
require = new HashMap<>(require);
|
||||
require.put(randomAlphaOfLengthBetween(11, 15), randomAlphaOfLengthBetween(1, 20));
|
||||
break;
|
||||
default:
|
||||
throw new AssertionError("Illegal randomisation branch");
|
||||
}
|
||||
|
||||
return new UpdateAllocationSettingsStep(key, nextKey, client, include, exclude, require);
|
||||
}
|
||||
|
||||
public void testHashcodeAndEquals() {
|
||||
EqualsHashCodeTestUtils
|
||||
.checkEqualsAndHashCode(
|
||||
createRandomInstance(), instance -> new UpdateAllocationSettingsStep(instance.getKey(), instance.getNextStepKey(),
|
||||
instance.getClient(), instance.getInclude(), instance.getExclude(), instance.getRequire()),
|
||||
this::mutateInstance);
|
||||
}
|
||||
|
||||
public void testPerformAction() {
|
||||
Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
|
||||
|
||||
UpdateAllocationSettingsStep step = createRandomInstance();
|
||||
|
||||
AdminClient adminClient = Mockito.mock(AdminClient.class);
|
||||
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
|
||||
|
||||
Mockito.when(client.admin()).thenReturn(adminClient);
|
||||
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
|
||||
Mockito.doAnswer(new Answer<Void>() {
|
||||
|
||||
@Override
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0];
|
||||
@SuppressWarnings("unchecked")
|
||||
ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1];
|
||||
Settings.Builder expectedSettings = Settings.builder();
|
||||
step.getInclude().forEach(
|
||||
(key, value) -> expectedSettings.put(IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + key, value));
|
||||
step.getExclude().forEach(
|
||||
(key, value) -> expectedSettings.put(IndexMetaData.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + key, value));
|
||||
step.getRequire().forEach(
|
||||
(key, value) -> expectedSettings.put(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + key, value));
|
||||
UpdateSettingsTestHelper.assertSettingsRequest(request, expectedSettings.build(), index.getName());
|
||||
listener.onResponse(UpdateSettingsTestHelper.createMockResponse(true));
|
||||
return null;
|
||||
}
|
||||
|
||||
}).when(indicesClient).updateSettings(Mockito.any(), Mockito.any());
|
||||
|
||||
SetOnce<Boolean> actionCompleted = new SetOnce<>();
|
||||
|
||||
step.performAction(index, new Listener() {
|
||||
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
||||
actionCompleted.set(complete);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Exception e) {
|
||||
throw new AssertionError("Unexpected method call", e);
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(true, actionCompleted.get());
|
||||
|
||||
Mockito.verify(client, Mockito.only()).admin();
|
||||
Mockito.verify(adminClient, Mockito.only()).indices();
|
||||
Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any());
|
||||
}
|
||||
|
||||
public void testPerformActionFailure() {
|
||||
Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
|
||||
Exception exception = new RuntimeException();
|
||||
UpdateAllocationSettingsStep step = createRandomInstance();
|
||||
|
||||
AdminClient adminClient = Mockito.mock(AdminClient.class);
|
||||
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
|
||||
|
||||
Mockito.when(client.admin()).thenReturn(adminClient);
|
||||
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
|
||||
Mockito.doAnswer(new Answer<Void>() {
|
||||
|
||||
@Override
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0];
|
||||
@SuppressWarnings("unchecked")
|
||||
ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1];
|
||||
Settings.Builder expectedSettings = Settings.builder();
|
||||
step.getInclude().forEach(
|
||||
(key, value) -> expectedSettings.put(IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + key, value));
|
||||
step.getExclude().forEach(
|
||||
(key, value) -> expectedSettings.put(IndexMetaData.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + key, value));
|
||||
step.getRequire().forEach(
|
||||
(key, value) -> expectedSettings.put(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + key, value));
|
||||
UpdateSettingsTestHelper.assertSettingsRequest(request, expectedSettings.build(), index.getName());
|
||||
listener.onFailure(exception);
|
||||
return null;
|
||||
}
|
||||
|
||||
}).when(indicesClient).updateSettings(Mockito.any(), Mockito.any());
|
||||
|
||||
SetOnce<Boolean> exceptionThrown = new SetOnce<>();
|
||||
step.performAction(index, new Listener() {
|
||||
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
||||
throw new AssertionError("Unexpected method call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Exception e) {
|
||||
assertSame(exception, e);
|
||||
exceptionThrown.set(true);
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(true, exceptionThrown.get());
|
||||
|
||||
Mockito.verify(client, Mockito.only()).admin();
|
||||
Mockito.verify(adminClient, Mockito.only()).indices();
|
||||
Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any());
|
||||
}
|
||||
}
|
|
@ -1,68 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.core.indexlifecycle;
|
||||
|
||||
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.cluster.ClusterName;
|
||||
import org.elasticsearch.cluster.ClusterState;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.cluster.metadata.MetaData;
|
||||
import org.elasticsearch.index.Index;
|
||||
import org.elasticsearch.index.codec.CodecService;
|
||||
import org.elasticsearch.index.engine.EngineConfig;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.test.EqualsHashCodeTestUtils;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
|
||||
|
||||
public class UpdateBestCompressionSettingsStepTests extends ESTestCase {
|
||||
|
||||
public UpdateBestCompressionSettingsStep createRandomInstance() {
|
||||
StepKey stepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
StepKey nextStepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
|
||||
return new UpdateBestCompressionSettingsStep(stepKey, nextStepKey);
|
||||
}
|
||||
|
||||
|
||||
public void testHashcodeAndEquals() {
|
||||
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createRandomInstance(),
|
||||
instance -> new UpdateBestCompressionSettingsStep(instance.getKey(), instance.getNextStepKey()));
|
||||
}
|
||||
|
||||
public void testPerformAction() {
|
||||
IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLength(5))
|
||||
.settings(settings(Version.CURRENT))
|
||||
.numberOfShards(1)
|
||||
.numberOfReplicas(0).build();
|
||||
MetaData metaData = MetaData.builder()
|
||||
.persistentSettings(settings(Version.CURRENT).build())
|
||||
.put(IndexMetaData.builder(indexMetadata))
|
||||
.build();
|
||||
Index index = indexMetadata.getIndex();
|
||||
ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).build();
|
||||
|
||||
UpdateBestCompressionSettingsStep step = createRandomInstance();
|
||||
ClusterState newState = step.performAction(index, clusterState);
|
||||
assertNotSame(clusterState, newState);
|
||||
IndexMetaData newIndexMetadata = newState.metaData().index(index);
|
||||
assertNotNull(newIndexMetadata);
|
||||
assertNotSame(indexMetadata, newIndexMetadata);
|
||||
assertTrue(EngineConfig.INDEX_CODEC_SETTING.exists(newIndexMetadata.getSettings()));
|
||||
assertTrue(CodecService.BEST_COMPRESSION_CODEC.equals(
|
||||
newIndexMetadata.getSettings().get(EngineConfig.INDEX_CODEC_SETTING.getKey())));
|
||||
}
|
||||
|
||||
public void testPerformActionNoIndex() {
|
||||
MetaData metaData = MetaData.builder().persistentSettings(settings(Version.CURRENT).build()).build();
|
||||
Index index = new Index("invalid_index", "invalid_index_id");
|
||||
ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).build();
|
||||
|
||||
UpdateBestCompressionSettingsStep step = createRandomInstance();
|
||||
ClusterState newState = step.performAction(index, clusterState);
|
||||
assertSame(clusterState, newState);
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@ package org.elasticsearch.xpack.core.indexlifecycle;
|
|||
|
||||
|
||||
import org.apache.lucene.util.SetOnce;
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
|
||||
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse;
|
||||
|
@ -26,7 +27,7 @@ import org.mockito.Mockito;
|
|||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
public class UpdateReplicaSettingsStepTests extends ESTestCase {
|
||||
public class UpdateSettingsStepTests extends ESTestCase {
|
||||
|
||||
private Client client;
|
||||
|
||||
|
@ -35,17 +36,18 @@ public class UpdateReplicaSettingsStepTests extends ESTestCase {
|
|||
client = Mockito.mock(Client.class);
|
||||
}
|
||||
|
||||
public UpdateReplicaSettingsStep createRandomInstance() {
|
||||
public UpdateSettingsStep createRandomInstance() {
|
||||
StepKey stepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
StepKey nextStepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
|
||||
Settings settings = Settings.builder().put(randomAlphaOfLength(10), randomAlphaOfLength(10)).build();
|
||||
|
||||
return new UpdateReplicaSettingsStep(stepKey, nextStepKey, client, randomIntBetween(0, 100));
|
||||
return new UpdateSettingsStep(stepKey, nextStepKey, client, settings);
|
||||
}
|
||||
|
||||
public UpdateReplicaSettingsStep mutateInstance(UpdateReplicaSettingsStep instance) {
|
||||
public UpdateSettingsStep mutateInstance(UpdateSettingsStep instance) {
|
||||
StepKey key = instance.getKey();
|
||||
StepKey nextKey = instance.getNextStepKey();
|
||||
int replicas = instance.getNumberOfReplicas();
|
||||
Settings settings = instance.getSettings();
|
||||
|
||||
switch (between(0, 2)) {
|
||||
case 0:
|
||||
|
@ -55,24 +57,25 @@ public class UpdateReplicaSettingsStepTests extends ESTestCase {
|
|||
nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5));
|
||||
break;
|
||||
case 2:
|
||||
replicas += 1;
|
||||
settings = Settings.builder().put(settings).put(randomAlphaOfLength(10), randomInt()).build();
|
||||
break;
|
||||
default:
|
||||
throw new AssertionError("Illegal randomisation branch");
|
||||
}
|
||||
|
||||
return new UpdateReplicaSettingsStep(key, nextKey, client, replicas);
|
||||
return new UpdateSettingsStep(key, nextKey, client, settings);
|
||||
}
|
||||
|
||||
public void testHashcodeAndEquals() {
|
||||
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createRandomInstance(), instance -> new UpdateReplicaSettingsStep(instance.getKey(),
|
||||
instance.getNextStepKey(), instance.getClient(), instance.getNumberOfReplicas()), this::mutateInstance);
|
||||
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createRandomInstance(), instance -> new UpdateSettingsStep(instance.getKey(),
|
||||
instance.getNextStepKey(), instance.getClient(), instance.getSettings()), this::mutateInstance);
|
||||
}
|
||||
|
||||
public void testPerformAction() {
|
||||
Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
|
||||
UpdateReplicaSettingsStep step = createRandomInstance();
|
||||
UpdateSettingsStep step = createRandomInstance();
|
||||
|
||||
AdminClient adminClient = Mockito.mock(AdminClient.class);
|
||||
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
|
||||
|
@ -86,9 +89,7 @@ public class UpdateReplicaSettingsStepTests extends ESTestCase {
|
|||
UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0];
|
||||
@SuppressWarnings("unchecked")
|
||||
ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1];
|
||||
Settings expectedSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, step.getNumberOfReplicas())
|
||||
.build();
|
||||
UpdateSettingsTestHelper.assertSettingsRequest(request, expectedSettings, index.getName());
|
||||
UpdateSettingsTestHelper.assertSettingsRequest(request, step.getSettings(), indexMetaData.getIndex().getName());
|
||||
listener.onResponse(UpdateSettingsTestHelper.createMockResponse(true));
|
||||
return null;
|
||||
}
|
||||
|
@ -97,7 +98,7 @@ public class UpdateReplicaSettingsStepTests extends ESTestCase {
|
|||
|
||||
SetOnce<Boolean> actionCompleted = new SetOnce<>();
|
||||
|
||||
step.performAction(index, new Listener() {
|
||||
step.performAction(indexMetaData, new Listener() {
|
||||
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
||||
|
@ -118,9 +119,10 @@ public class UpdateReplicaSettingsStepTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testPerformActionFailure() {
|
||||
Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Exception exception = new RuntimeException();
|
||||
UpdateReplicaSettingsStep step = createRandomInstance();
|
||||
UpdateSettingsStep step = createRandomInstance();
|
||||
|
||||
AdminClient adminClient = Mockito.mock(AdminClient.class);
|
||||
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
|
||||
|
@ -134,9 +136,7 @@ public class UpdateReplicaSettingsStepTests extends ESTestCase {
|
|||
UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0];
|
||||
@SuppressWarnings("unchecked")
|
||||
ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1];
|
||||
Settings expectedSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, step.getNumberOfReplicas())
|
||||
.build();
|
||||
UpdateSettingsTestHelper.assertSettingsRequest(request, expectedSettings, index.getName());
|
||||
UpdateSettingsTestHelper.assertSettingsRequest(request, step.getSettings(), indexMetaData.getIndex().getName());
|
||||
listener.onFailure(exception);
|
||||
return null;
|
||||
}
|
||||
|
@ -144,7 +144,7 @@ public class UpdateReplicaSettingsStepTests extends ESTestCase {
|
|||
}).when(indicesClient).updateSettings(Mockito.any(), Mockito.any());
|
||||
|
||||
SetOnce<Boolean> exceptionThrown = new SetOnce<>();
|
||||
step.performAction(index, new Listener() {
|
||||
step.performAction(indexMetaData, new Listener() {
|
||||
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
|
@ -10,8 +10,8 @@ import org.elasticsearch.cluster.ClusterState;
|
|||
import org.elasticsearch.cluster.ClusterStateUpdateTask;
|
||||
import org.elasticsearch.common.logging.ESLoggerFactory;
|
||||
import org.elasticsearch.index.Index;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateActionStep;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateWaitStep;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.InitializePolicyContextStep;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.Step;
|
||||
|
||||
public class ExecuteStepsUpdateTask extends ClusterStateUpdateTask {
|
||||
|
@ -50,12 +50,12 @@ public class ExecuteStepsUpdateTask extends ClusterStateUpdateTask {
|
|||
// We can do cluster state steps all together until we
|
||||
// either get to a step that isn't a cluster state step or a
|
||||
// cluster state wait step returns not completed
|
||||
while (currentStep instanceof ClusterStateActionStep || currentStep instanceof ClusterStateWaitStep) {
|
||||
if (currentStep instanceof ClusterStateActionStep) {
|
||||
while (currentStep instanceof InitializePolicyContextStep || currentStep instanceof ClusterStateWaitStep) {
|
||||
if (currentStep instanceof InitializePolicyContextStep) {
|
||||
// cluster state action step so do the action and
|
||||
// move
|
||||
// the cluster state to the next step
|
||||
currentState = ((ClusterStateActionStep) currentStep).performAction(index, currentState);
|
||||
currentState = ((InitializePolicyContextStep) currentStep).performAction(index, currentState);
|
||||
if (currentStep.getNextStepKey() == null) {
|
||||
return currentState;
|
||||
}
|
||||
|
|
|
@ -17,8 +17,8 @@ import org.elasticsearch.common.settings.Settings.Builder;
|
|||
import org.elasticsearch.index.Index;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.AsyncActionStep;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.AsyncWaitStep;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateActionStep;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateWaitStep;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.InitializePolicyContextStep;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.Step;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
|
||||
|
@ -34,22 +34,22 @@ public class IndexLifecycleRunner {
|
|||
this.clusterService = clusterService;
|
||||
}
|
||||
|
||||
public void runPolicy(String policy, Index index, Settings indexSettings, boolean fromClusterStateChange) {
|
||||
public void runPolicy(String policy, IndexMetaData indexMetaData, Settings indexSettings, boolean fromClusterStateChange) {
|
||||
Step currentStep = getCurrentStep(stepRegistry, policy, indexSettings);
|
||||
logger.warn("running policy with current-step[" + currentStep.getKey() + "]");
|
||||
if (currentStep instanceof TerminalPolicyStep) {
|
||||
logger.debug("policy [" + policy + "] for index [" + index.getName() + "] complete, skipping execution");
|
||||
} else if (currentStep instanceof ClusterStateActionStep || currentStep instanceof ClusterStateWaitStep) {
|
||||
executeClusterStateSteps(index, policy, currentStep);
|
||||
logger.debug("policy [" + policy + "] for index [" + indexMetaData.getIndex().getName() + "] complete, skipping execution");
|
||||
} else if (currentStep instanceof InitializePolicyContextStep || currentStep instanceof ClusterStateWaitStep) {
|
||||
executeClusterStateSteps(indexMetaData.getIndex(), policy, currentStep);
|
||||
} else if (currentStep instanceof AsyncWaitStep) {
|
||||
if (fromClusterStateChange == false) {
|
||||
((AsyncWaitStep) currentStep).evaluateCondition(index, new AsyncWaitStep.Listener() {
|
||||
((AsyncWaitStep) currentStep).evaluateCondition(indexMetaData.getIndex(), new AsyncWaitStep.Listener() {
|
||||
|
||||
@Override
|
||||
public void onResponse(boolean conditionMet) {
|
||||
logger.error("cs-change-async-wait-callback. current-step:" + currentStep.getKey());
|
||||
if (conditionMet) {
|
||||
moveToStep(index, policy, currentStep.getKey(), currentStep.getNextStepKey());
|
||||
moveToStep(indexMetaData.getIndex(), policy, currentStep.getKey(), currentStep.getNextStepKey());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -62,13 +62,13 @@ public class IndexLifecycleRunner {
|
|||
}
|
||||
} else if (currentStep instanceof AsyncActionStep) {
|
||||
if (fromClusterStateChange == false) {
|
||||
((AsyncActionStep) currentStep).performAction(index, new AsyncActionStep.Listener() {
|
||||
((AsyncActionStep) currentStep).performAction(indexMetaData, new AsyncActionStep.Listener() {
|
||||
|
||||
@Override
|
||||
public void onResponse(boolean complete) {
|
||||
logger.error("cs-change-async-action-callback. current-step:" + currentStep.getKey());
|
||||
if (complete && ((AsyncActionStep) currentStep).indexSurvives()) {
|
||||
moveToStep(index, policy, currentStep.getKey(), currentStep.getNextStepKey());
|
||||
moveToStep(indexMetaData.getIndex(), policy, currentStep.getKey(), currentStep.getNextStepKey());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,15 +84,14 @@ public class IndexLifecycleRunner {
|
|||
}
|
||||
}
|
||||
|
||||
private void runPolicy(Index index, ClusterState clusterState) {
|
||||
IndexMetaData indexMetaData = clusterState.getMetaData().index(index);
|
||||
private void runPolicy(IndexMetaData indexMetaData) {
|
||||
Settings indexSettings = indexMetaData.getSettings();
|
||||
String policy = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexSettings);
|
||||
runPolicy(policy, index, indexSettings, false);
|
||||
runPolicy(policy, indexMetaData, indexSettings, false);
|
||||
}
|
||||
|
||||
private void executeClusterStateSteps(Index index, String policy, Step step) {
|
||||
assert step instanceof ClusterStateActionStep || step instanceof ClusterStateWaitStep;
|
||||
assert step instanceof InitializePolicyContextStep || step instanceof ClusterStateWaitStep;
|
||||
clusterService.submitStateUpdateTask("ILM", new ExecuteStepsUpdateTask(policy, index, step, stepRegistry));
|
||||
}
|
||||
|
||||
|
@ -144,6 +143,6 @@ public class IndexLifecycleRunner {
|
|||
logger.error("moveToStep[" + policy + "] [" + index.getName() + "]" + currentStepKey + " -> "
|
||||
+ nextStepKey);
|
||||
clusterService.submitStateUpdateTask("ILM", new MoveToNextStepUpdateTask(index, policy, currentStepKey,
|
||||
nextStepKey, newState -> runPolicy(index, newState)));
|
||||
nextStepKey, newState -> runPolicy(newState.getMetaData().index(index))));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -149,7 +149,7 @@ public class IndexLifecycleService extends AbstractComponent
|
|||
clusterState.metaData().indices().valuesIt().forEachRemaining((idxMeta) -> {
|
||||
String policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxMeta.getSettings());
|
||||
if (Strings.isNullOrEmpty(policyName) == false) {
|
||||
lifecycleRunner.runPolicy(policyName, idxMeta.getIndex(), idxMeta.getSettings(), fromClusterStateChange);
|
||||
lifecycleRunner.runPolicy(policyName, idxMeta, idxMeta.getSettings(), fromClusterStateChange);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ import org.elasticsearch.xpack.core.indexlifecycle.Step;
|
|||
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.TerminalPolicyStep;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.TestLifecycleType;
|
||||
import static org.elasticsearch.xpack.indexlifecycle.IndexLifecycleRunnerTests.MockClusterStateActionStep;
|
||||
import static org.elasticsearch.xpack.indexlifecycle.IndexLifecycleRunnerTests.MockInitializePolicyContextStep;
|
||||
import static org.elasticsearch.xpack.indexlifecycle.IndexLifecycleRunnerTests.MockClusterStateWaitStep;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
|
@ -48,14 +48,14 @@ public class ExecuteStepsUpdateTaskTests extends ESTestCase {
|
|||
private String mixedPolicyName;
|
||||
private String allClusterPolicyName;
|
||||
private Index index;
|
||||
private MockClusterStateActionStep firstStep;
|
||||
private MockInitializePolicyContextStep firstStep;
|
||||
private MockClusterStateWaitStep secondStep;
|
||||
private MockClusterStateWaitStep allClusterSecondStep;
|
||||
private MockStep thirdStep;
|
||||
|
||||
@Before
|
||||
public void prepareState() {
|
||||
firstStep = new MockClusterStateActionStep(firstStepKey, secondStepKey);
|
||||
firstStep = new MockInitializePolicyContextStep(firstStepKey, secondStepKey);
|
||||
secondStep = new MockClusterStateWaitStep(secondStepKey, thirdStepKey);
|
||||
secondStep.setWillComplete(true);
|
||||
allClusterSecondStep = new MockClusterStateWaitStep(secondStepKey, TerminalPolicyStep.KEY);
|
||||
|
|
|
@ -16,8 +16,8 @@ import org.elasticsearch.index.Index;
|
|||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.AsyncActionStep;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.AsyncWaitStep;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateActionStep;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateWaitStep;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.InitializePolicyContextStep;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings;
|
||||
import org.elasticsearch.xpack.core.indexlifecycle.MockStep;
|
||||
|
@ -51,10 +51,11 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step);
|
||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
||||
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService);
|
||||
Index index = new Index("my_index", "my_index_id");
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Settings indexSettings = Settings.builder().build();
|
||||
|
||||
runner.runPolicy(policyName, index, indexSettings, false);
|
||||
runner.runPolicy(policyName, indexMetaData, indexSettings, false);
|
||||
|
||||
Mockito.verifyZeroInteractions(clusterService);
|
||||
}
|
||||
|
@ -62,17 +63,18 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
public void testRunPolicyClusterStateActionStep() {
|
||||
String policyName = "cluster_state_action_policy";
|
||||
StepKey stepKey = new StepKey("phase", "action", "cluster_state_action_step");
|
||||
MockClusterStateActionStep step = new MockClusterStateActionStep(stepKey, null);
|
||||
MockInitializePolicyContextStep step = new MockInitializePolicyContextStep(stepKey, null);
|
||||
PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step);
|
||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
||||
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService);
|
||||
Index index = new Index("my_index", "my_index_id");
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Settings indexSettings = Settings.builder().build();
|
||||
|
||||
runner.runPolicy(policyName, index, indexSettings, randomBoolean());
|
||||
runner.runPolicy(policyName, indexMetaData, indexSettings, randomBoolean());
|
||||
|
||||
Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM"),
|
||||
Mockito.argThat(new ExecuteStepsUpdateTaskMatcher(index, policyName, step)));
|
||||
Mockito.argThat(new ExecuteStepsUpdateTaskMatcher(indexMetaData.getIndex(), policyName, step)));
|
||||
Mockito.verifyNoMoreInteractions(clusterService);
|
||||
}
|
||||
|
||||
|
@ -84,13 +86,14 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step);
|
||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
||||
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService);
|
||||
Index index = new Index("my_index", "my_index_id");
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Settings indexSettings = Settings.builder().build();
|
||||
|
||||
runner.runPolicy(policyName, index, indexSettings, randomBoolean());
|
||||
runner.runPolicy(policyName, indexMetaData, indexSettings, randomBoolean());
|
||||
|
||||
Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM"),
|
||||
Mockito.argThat(new ExecuteStepsUpdateTaskMatcher(index, policyName, step)));
|
||||
Mockito.argThat(new ExecuteStepsUpdateTaskMatcher(indexMetaData.getIndex(), policyName, step)));
|
||||
Mockito.verifyNoMoreInteractions(clusterService);
|
||||
}
|
||||
|
||||
|
@ -102,14 +105,15 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step);
|
||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
||||
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService);
|
||||
Index index = new Index("my_index", "my_index_id");
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Settings indexSettings = Settings.builder().build();
|
||||
|
||||
runner.runPolicy(policyName, index, indexSettings, false);
|
||||
runner.runPolicy(policyName, indexMetaData, indexSettings, false);
|
||||
|
||||
assertEquals(1, step.getExecuteCount());
|
||||
Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM"),
|
||||
Mockito.argThat(new MoveToNextStepUpdateTaskMatcher(index, policyName, stepKey, null)));
|
||||
Mockito.argThat(new MoveToNextStepUpdateTaskMatcher(indexMetaData.getIndex(), policyName, stepKey, null)));
|
||||
Mockito.verifyNoMoreInteractions(clusterService);
|
||||
}
|
||||
|
||||
|
@ -122,10 +126,11 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step);
|
||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
||||
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService);
|
||||
Index index = new Index("my_index", "my_index_id");
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Settings indexSettings = Settings.builder().build();
|
||||
|
||||
runner.runPolicy(policyName, index, indexSettings, false);
|
||||
runner.runPolicy(policyName, indexMetaData, indexSettings, false);
|
||||
|
||||
assertEquals(1, step.getExecuteCount());
|
||||
Mockito.verifyZeroInteractions(clusterService);
|
||||
|
@ -139,10 +144,11 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step);
|
||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
||||
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService);
|
||||
Index index = new Index("my_index", "my_index_id");
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Settings indexSettings = Settings.builder().build();
|
||||
|
||||
runner.runPolicy(policyName, index, indexSettings, false);
|
||||
runner.runPolicy(policyName, indexMetaData, indexSettings, false);
|
||||
|
||||
assertEquals(1, step.getExecuteCount());
|
||||
Mockito.verifyZeroInteractions(clusterService);
|
||||
|
@ -157,11 +163,12 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step);
|
||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
||||
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService);
|
||||
Index index = new Index("my_index", "my_index_id");
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Settings indexSettings = Settings.builder().build();
|
||||
|
||||
RuntimeException exception = expectThrows(RuntimeException.class,
|
||||
() -> runner.runPolicy(policyName, index, indexSettings, false));
|
||||
() -> runner.runPolicy(policyName, indexMetaData, indexSettings, false));
|
||||
|
||||
assertSame(expectedException, exception.getCause());
|
||||
assertEquals(1, step.getExecuteCount());
|
||||
|
@ -177,10 +184,11 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step);
|
||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
||||
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService);
|
||||
Index index = new Index("my_index", "my_index_id");
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Settings indexSettings = Settings.builder().build();
|
||||
|
||||
runner.runPolicy(policyName, index, indexSettings, true);
|
||||
runner.runPolicy(policyName, indexMetaData, indexSettings, true);
|
||||
|
||||
assertEquals(0, step.getExecuteCount());
|
||||
Mockito.verifyZeroInteractions(clusterService);
|
||||
|
@ -194,14 +202,15 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step);
|
||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
||||
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService);
|
||||
Index index = new Index("my_index", "my_index_id");
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Settings indexSettings = Settings.builder().build();
|
||||
|
||||
runner.runPolicy(policyName, index, indexSettings, false);
|
||||
runner.runPolicy(policyName, indexMetaData, indexSettings, false);
|
||||
|
||||
assertEquals(1, step.getExecuteCount());
|
||||
Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM"),
|
||||
Mockito.argThat(new MoveToNextStepUpdateTaskMatcher(index, policyName, stepKey, null)));
|
||||
Mockito.argThat(new MoveToNextStepUpdateTaskMatcher(indexMetaData.getIndex(), policyName, stepKey, null)));
|
||||
Mockito.verifyNoMoreInteractions(clusterService);
|
||||
}
|
||||
|
||||
|
@ -213,10 +222,11 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step);
|
||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
||||
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService);
|
||||
Index index = new Index("my_index", "my_index_id");
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Settings indexSettings = Settings.builder().build();
|
||||
|
||||
runner.runPolicy(policyName, index, indexSettings, false);
|
||||
runner.runPolicy(policyName, indexMetaData, indexSettings, false);
|
||||
|
||||
assertEquals(1, step.getExecuteCount());
|
||||
Mockito.verifyZeroInteractions(clusterService);
|
||||
|
@ -231,11 +241,12 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step);
|
||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
||||
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService);
|
||||
Index index = new Index("my_index", "my_index_id");
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Settings indexSettings = Settings.builder().build();
|
||||
|
||||
RuntimeException exception = expectThrows(RuntimeException.class,
|
||||
() -> runner.runPolicy(policyName, index, indexSettings, false));
|
||||
() -> runner.runPolicy(policyName, indexMetaData, indexSettings, false));
|
||||
|
||||
assertSame(expectedException, exception.getCause());
|
||||
assertEquals(1, step.getExecuteCount());
|
||||
|
@ -251,10 +262,11 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step);
|
||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
||||
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService);
|
||||
Index index = new Index("my_index", "my_index_id");
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Settings indexSettings = Settings.builder().build();
|
||||
|
||||
runner.runPolicy(policyName, index, indexSettings, true);
|
||||
runner.runPolicy(policyName, indexMetaData, indexSettings, true);
|
||||
|
||||
assertEquals(0, step.getExecuteCount());
|
||||
Mockito.verifyZeroInteractions(clusterService);
|
||||
|
@ -267,11 +279,12 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step);
|
||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
||||
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService);
|
||||
Index index = new Index("my_index", "my_index_id");
|
||||
IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT))
|
||||
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
|
||||
Settings indexSettings = Settings.builder().build();
|
||||
|
||||
IllegalStateException exception = expectThrows(IllegalStateException.class,
|
||||
() -> runner.runPolicy(policyName, index, indexSettings, randomBoolean()));
|
||||
() -> runner.runPolicy(policyName, indexMetaData, indexSettings, randomBoolean()));
|
||||
assertEquals("Step with key [" + stepKey + "] is not a recognised type: [" + step.getClass().getName() + "]",
|
||||
exception.getMessage());
|
||||
Mockito.verifyZeroInteractions(clusterService);
|
||||
|
@ -518,7 +531,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void performAction(Index index, Listener listener) {
|
||||
public void performAction(IndexMetaData indexMetaData, Listener listener) {
|
||||
executeCount++;
|
||||
if (exception == null) {
|
||||
listener.onResponse(willComplete);
|
||||
|
@ -563,12 +576,12 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
|
||||
}
|
||||
|
||||
static class MockClusterStateActionStep extends ClusterStateActionStep {
|
||||
static class MockInitializePolicyContextStep extends InitializePolicyContextStep {
|
||||
|
||||
private RuntimeException exception;
|
||||
private long executeCount = 0;
|
||||
|
||||
MockClusterStateActionStep(StepKey key, StepKey nextStepKey) {
|
||||
MockInitializePolicyContextStep(StepKey key, StepKey nextStepKey) {
|
||||
super(key, nextStepKey);
|
||||
}
|
||||
|
||||
|
@ -588,7 +601,6 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
|
|||
}
|
||||
return clusterState;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class MockClusterStateWaitStep extends ClusterStateWaitStep {
|
||||
|
|
Loading…
Reference in New Issue