slight changes

This commit is contained in:
Tal Levy 2018-03-27 11:41:38 -07:00
parent 9972710e9e
commit ce4248ec59
3 changed files with 56 additions and 72 deletions

View File

@ -13,6 +13,7 @@ import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.Index;
import java.util.Objects;
import java.util.function.LongSupplier;
/**
@ -107,4 +108,46 @@ public class Step {
.metaData(MetaData.builder(currentState.metaData())
.updateSettings(newLifecyclePhaseSettings, index.getName())).build();
}
public static class StepKey {
private final String phase;
private final String action;
private final String name;
public StepKey(String phase, String action, String name) {
this.phase = phase;
this.action = action;
this.name = name;
}
public String getPhase() {
return phase;
}
public String getAction() {
return action;
}
public String getName() {
return name;
}
@Override
public int hashCode() {
return Objects.hash(phase, action, name);
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
StepKey other = (StepKey) obj;
return Objects.equals(phase, other.phase) && Objects.equals(action, other.action) && Objects.equals(name, other.name);
}
}
}

View File

@ -6,17 +6,12 @@
package org.elasticsearch.xpack.indexlifecycle;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.FormattedMessage;
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.client.Client;
import org.elasticsearch.cluster.ClusterChangedEvent;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateListener;
import org.elasticsearch.cluster.ClusterStateUpdateTask;
import org.elasticsearch.cluster.DiffableUtils;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.service.ClusterService;
@ -36,14 +31,7 @@ import org.elasticsearch.xpack.core.scheduler.SchedulerEngine;
import java.io.Closeable;
import java.time.Clock;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.concurrent.CompletableFuture;
import java.util.function.LongSupplier;
import java.util.stream.Collectors;
/**
* A service which runs the {@link LifecyclePolicy}s associated with indexes.
@ -94,7 +82,7 @@ public class IndexLifecycleService extends AbstractComponent
boolean pollIntervalSettingChanged = !pollInterval.equals(previousPollInterval);
if (lifecycleMetadata != null) {
if (lifecycleMetadata != null && event.changedCustomMetaDataSet().contains(IndexLifecycleMetadata.TYPE)) {
// update policy steps registry
policyRegistry.update(event.state());
}
@ -149,7 +137,7 @@ public class IndexLifecycleService extends AbstractComponent
String stepName = currentState.metaData().settings().get(LifecycleSettings.LIFECYCLE_STEP);
// returns current step to execute. If settings are null, then the first step to be executed in
// this policy is returned.
Step currentStep = policyRegistry.getStep(policyName, phase, action, stepName);
Step currentStep = policyRegistry.getStep(policyName, new Step.StepKey(phase, action, stepName));
return executeStepUntilAsync(currentStep, clusterState, client, nowSupplier, idxMeta.getIndex());
}

View File

@ -8,16 +8,14 @@ package org.elasticsearch.xpack.indexlifecycle;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.Diff;
import org.elasticsearch.cluster.DiffableUtils;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy;
import org.elasticsearch.xpack.core.indexlifecycle.Step;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
@ -28,8 +26,8 @@ public class PolicyStepsRegistry {
SortedMap<String, LifecyclePolicy> lifecyclePolicyMap;
// keeps track of what the first step in a policy is
Map<String, Step> firstStepMap;
// keeps track of a mapping from step-name to respective Step
Map<StepKey, Step> stepMap;
// keeps track of a mapping from policy/step-name to respective Step
Map<String, Map<Step.StepKey, Step>> stepMap;
public PolicyStepsRegistry() {
this.lifecyclePolicyMap = new TreeMap<>();
@ -48,19 +46,18 @@ public class PolicyStepsRegistry {
List<Step> policyAsSteps = policy.toSteps();
if (policyAsSteps.isEmpty() == false) {
firstStepMap.put(policy.getName(), policyAsSteps.get(0));
Map<Step.StepKey, Step> stepMapForPolicy = stepMap.put(policy.getName(), new HashMap<>());
for (Step step : policyAsSteps) {
stepMap.put(new StepKey(step.getPhase(), step.getAction(), step.getName()), step);
stepMapForPolicy.put(new Step.StepKey(step.getPhase(), step.getAction(), step.getName()), step);
}
}
}
}
for (String deletedPolicyName : mapDiff.getDeletes()) {
LifecyclePolicy policy = lifecyclePolicyMap.remove(deletedPolicyName);
Step next = firstStepMap.remove(deletedPolicyName);
while (next.hasNextStep()) {
next = stepMap.remove(next.getNextStep());
}
lifecyclePolicyMap.remove(deletedPolicyName);
firstStepMap.remove(deletedPolicyName);
stepMap.remove(deletedPolicyName);
}
}
@ -70,13 +67,11 @@ public class PolicyStepsRegistry {
* readers that know the current policy and step by name
* as String values in the cluster state.
* @param policy the policy from which to fetch the associated steps from
* @param phase the phase the requested step is run in
* @param action the action the requested step is run in
* @param name the name of the requested step
* @param stepKey the key to the requested {@link Step}
* @return
*/
public Step getStep(String policy, @Nullable String phase, @Nullable String action, @Nullable String name) {
Step step = stepMap.get(new StepKey(phase, action, name));
public Step getStep(String policy, Step.StepKey stepKey) {
Step step = stepMap.getOrDefault(policy, Collections.emptyMap()).get(stepKey);
if (step == null) {
step = firstStepMap.get(policy);
}
@ -84,7 +79,6 @@ public class PolicyStepsRegistry {
}
@Override
public int hashCode() {
return Objects.hash(lifecyclePolicyMap, firstStepMap, stepMap);
@ -103,45 +97,4 @@ public class PolicyStepsRegistry {
&& Objects.equals(firstStepMap, other.firstStepMap) && Objects.equals(stepMap, other.stepMap);
}
public class StepKey {
private final String phase;
private final String action;
private final String name;
public StepKey(String phase, String action, String name) {
this.phase = phase;
this.action = action;
this.name = name;
}
public String getPhase() {
return phase;
}
public String getAction() {
return action;
}
public String getName() {
return name;
}
@Override
public int hashCode() {
return Objects.hash(phase, action, name);
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
StepKey other = (StepKey) obj;
return Objects.equals(phase, other.phase) && Objects.equals(action, other.action) && Objects.equals(name, other.name);
}
}
}