Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
import org.elasticsearch.client.indexlifecycle.AllocateAction;
import org.elasticsearch.client.indexlifecycle.DeleteAction;
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.ExplainLifecycleRequest;
import org.elasticsearch.client.indexlifecycle.ExplainLifecycleResponse;
import org.elasticsearch.client.indexlifecycle.ForceMergeAction;
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyResponse;
import org.elasticsearch.client.indexlifecycle.IndexLifecycleExplainResponse;
import org.elasticsearch.client.indexlifecycle.LifecycleAction;
import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest;
import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusResponse;
Expand All @@ -39,16 +42,13 @@
import org.elasticsearch.client.indexlifecycle.PhaseExecutionInfo;
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.RolloverAction;
import org.elasticsearch.client.indexlifecycle.ShrinkAction;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.client.indexlifecycle.ExplainLifecycleRequest;
import org.elasticsearch.client.indexlifecycle.ExplainLifecycleResponse;
import org.elasticsearch.client.indexlifecycle.IndexLifecycleExplainResponse;
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyResponse;
import org.elasticsearch.client.indexlifecycle.ShrinkAction;
import org.elasticsearch.client.indexlifecycle.StartILMRequest;
import org.elasticsearch.client.indexlifecycle.StopILMRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.hamcrest.Matchers;

import java.io.IOException;
Expand Down Expand Up @@ -167,14 +167,6 @@ public void testExplainLifecycle() throws Exception {
.put("index.lifecycle.rollover_alias", "baz-alias").build(), "", "\"baz-alias\" : {}");

createIndex("squash", Settings.EMPTY);
assertBusy(() -> {
GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices("foo-01", "baz-01");
GetSettingsResponse settingsResponse = highLevelClient().indices().getSettings(getSettingsRequest, RequestOptions.DEFAULT);
assertThat(settingsResponse.getSetting("foo-01", "index.lifecycle.name"), equalTo(policy.getName()));
assertThat(settingsResponse.getSetting("baz-01", "index.lifecycle.name"), equalTo(policy.getName()));
assertThat(settingsResponse.getSetting("foo-01", "index.lifecycle.phase"), equalTo("hot"));
assertThat(settingsResponse.getSetting("baz-01", "index.lifecycle.phase"), equalTo("hot"));
});

ExplainLifecycleRequest req = new ExplainLifecycleRequest();
req.indices("foo-01", "baz-01", "squash");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,11 @@ public Builder putCustom(String type, Map<String, String> customIndexMetaData) {
return this;
}

public Builder removeCustom(String type) {
this.customMetaData.remove(type);
return this;
}

public Set<String> getInSyncAllocationIds(int shardId) {
return inSyncAllocationIds.get(shardId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.index.Index;

import java.util.Objects;

import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY;

/**
* Copies the execution state data from one index to another, typically after a
* new index has been created. Useful for actions such as shrink.
*/
public class CopyExecutionStateStep extends ClusterStateActionStep {
public static final String NAME = "copy_execution_state";
private String shrunkIndexPrefix;

public CopyExecutionStateStep(StepKey key, StepKey nextStepKey, String shrunkIndexPrefix) {
super(key, nextStepKey);
this.shrunkIndexPrefix = shrunkIndexPrefix;
}

String getShrunkIndexPrefix() {
return shrunkIndexPrefix;
}

@Override
public ClusterState performAction(Index index, ClusterState clusterState) {
IndexMetaData indexMetaData = clusterState.metaData().index(index);
// get source index
String indexName = indexMetaData.getIndex().getName();
// get target shrink index
String targetIndexName = shrunkIndexPrefix + indexName;
IndexMetaData targetIndexMetaData = clusterState.metaData().index(targetIndexName);

LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetaData);
String phase = lifecycleState.getPhase();
String action = lifecycleState.getAction();
long lifecycleDate = lifecycleState.getLifecycleDate();

LifecycleExecutionState.Builder relevantTargetCustomData = LifecycleExecutionState.builder();
relevantTargetCustomData.setIndexCreationDate(lifecycleDate);
relevantTargetCustomData.setPhase(phase);
relevantTargetCustomData.setAction(action);
relevantTargetCustomData.setStep(ShrunkenIndexCheckStep.NAME);

MetaData.Builder newMetaData = MetaData.builder(clusterState.getMetaData())
.put(IndexMetaData.builder(targetIndexMetaData)
.putCustom(ILM_CUSTOM_METADATA_KEY, relevantTargetCustomData.build().asMap()));

return ClusterState.builder(clusterState).metaData(newMetaData).build();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
CopyExecutionStateStep that = (CopyExecutionStateStep) o;
return Objects.equals(shrunkIndexPrefix, that.shrunkIndexPrefix);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), shrunkIndexPrefix);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public class IndexLifecycleExplainResponse implements ToXContentObject, Writeabl
(boolean) a[1],
(String) a[2],
(boolean) (a[3] == null ? false: a[3]),
(long) (a[4] == null ? -1L: a[4]),
(Long) (a[4]),
(String) a[5],
(String) a[6],
(String) a[7],
(String) a[8],
(long) (a[9] == null ? -1L: a[9]),
(long) (a[10] == null ? -1L: a[10]),
(long) (a[11] == null ? -1L: a[11]),
(Long) (a[9]),
(Long) (a[10]),
(Long) (a[11]),
(BytesReference) a[12],
(PhaseExecutionInfo) a[13]));
static {
Expand Down Expand Up @@ -86,36 +86,36 @@ public class IndexLifecycleExplainResponse implements ToXContentObject, Writeabl
private final String action;
private final String step;
private final String failedStep;
private final long lifecycleDate;
private final long phaseTime;
private final long actionTime;
private final long stepTime;
private final Long lifecycleDate;
private final Long phaseTime;
private final Long actionTime;
private final Long stepTime;
private final boolean skip;
private final boolean managedByILM;
private final BytesReference stepInfo;
private final PhaseExecutionInfo phaseExecutionInfo;

public static IndexLifecycleExplainResponse newManagedIndexResponse(String index, String policyName, boolean skip, long lifecycleDate,
String phase, String action, String step, String failedStep, long phaseTime, long actionTime, long stepTime,
public static IndexLifecycleExplainResponse newManagedIndexResponse(String index, String policyName, boolean skip, Long lifecycleDate,
String phase, String action, String step, String failedStep, Long phaseTime, Long actionTime, Long stepTime,
BytesReference stepInfo, PhaseExecutionInfo phaseExecutionInfo) {
return new IndexLifecycleExplainResponse(index, true, policyName, skip, lifecycleDate, phase, action, step, failedStep, phaseTime,
actionTime, stepTime, stepInfo, phaseExecutionInfo);
}

public static IndexLifecycleExplainResponse newUnmanagedIndexResponse(String index) {
return new IndexLifecycleExplainResponse(index, false, null, false, -1L, null, null, null, null, -1L, -1L, -1L, null, null);
return new IndexLifecycleExplainResponse(index, false, null, false, null, null, null, null, null, null, null, null, null, null);
}

private IndexLifecycleExplainResponse(String index, boolean managedByILM, String policyName, boolean skip, long lifecycleDate,
String phase, String action, String step, String failedStep, long phaseTime, long actionTime,
long stepTime, BytesReference stepInfo, PhaseExecutionInfo phaseExecutionInfo) {
private IndexLifecycleExplainResponse(String index, boolean managedByILM, String policyName, boolean skip, Long lifecycleDate,
String phase, String action, String step, String failedStep, Long phaseTime, Long actionTime,
Long stepTime, BytesReference stepInfo, PhaseExecutionInfo phaseExecutionInfo) {
if (managedByILM) {
if (policyName == null) {
throw new IllegalArgumentException("[" + POLICY_NAME_FIELD.getPreferredName() + "] cannot be null for managed index");
}
} else {
if (policyName != null || lifecycleDate >= 0 || phase != null || action != null || step != null || failedStep != null
|| phaseTime >= 0 || actionTime >= 0 || stepTime >= 0 || stepInfo != null || phaseExecutionInfo != null) {
if (policyName != null || lifecycleDate != null || phase != null || action != null || step != null || failedStep != null
|| phaseTime != null || actionTime != null || stepTime != null || stepInfo != null || phaseExecutionInfo != null) {
throw new IllegalArgumentException(
"Unmanaged index response must only contain fields: [" + MANAGED_BY_ILM_FIELD + ", " + INDEX_FIELD + "]");
}
Expand All @@ -142,27 +142,27 @@ public IndexLifecycleExplainResponse(StreamInput in) throws IOException {
if (managedByILM) {
policyName = in.readString();
skip = in.readBoolean();
lifecycleDate = in.readZLong();
phase = in.readString();
action = in.readString();
step = in.readString();
lifecycleDate = in.readOptionalLong();
phase = in.readOptionalString();
action = in.readOptionalString();
step = in.readOptionalString();
failedStep = in.readOptionalString();
phaseTime = in.readZLong();
actionTime = in.readZLong();
stepTime = in.readZLong();
phaseTime = in.readOptionalLong();
actionTime = in.readOptionalLong();
stepTime = in.readOptionalLong();
stepInfo = in.readOptionalBytesReference();
phaseExecutionInfo = in.readOptionalWriteable(PhaseExecutionInfo::new);
} else {
policyName = null;
skip = false;
lifecycleDate = -1L;
lifecycleDate = null;
phase = null;
action = null;
step = null;
failedStep = null;
phaseTime = -1L;
actionTime = -1L;
stepTime = -1L;
phaseTime = null;
actionTime = null;
stepTime = null;
stepInfo = null;
phaseExecutionInfo = null;
}
Expand All @@ -175,14 +175,14 @@ public void writeTo(StreamOutput out) throws IOException {
if (managedByILM) {
out.writeString(policyName);
out.writeBoolean(skip);
out.writeZLong(lifecycleDate);
out.writeString(phase);
out.writeString(action);
out.writeString(step);
out.writeOptionalLong(lifecycleDate);
out.writeOptionalString(phase);
out.writeOptionalString(action);
out.writeOptionalString(step);
out.writeOptionalString(failedStep);
out.writeZLong(phaseTime);
out.writeZLong(actionTime);
out.writeZLong(stepTime);
out.writeOptionalLong(phaseTime);
out.writeOptionalLong(actionTime);
out.writeOptionalLong(stepTime);
out.writeOptionalBytesReference(stepInfo);
out.writeOptionalWriteable(phaseExecutionInfo);
}
Expand All @@ -204,31 +204,31 @@ public boolean skip() {
return skip;
}

public long getLifecycleDate() {
public Long getLifecycleDate() {
return lifecycleDate;
}

public String getPhase() {
return phase;
}

public long getPhaseTime() {
public Long getPhaseTime() {
return phaseTime;
}

public String getAction() {
return action;
}

public long getActionTime() {
public Long getActionTime() {
return actionTime;
}

public String getStep() {
return step;
}

public long getStepTime() {
public Long getStepTime() {
return stepTime;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
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 static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY;

public final class InitializePolicyContextStep extends ClusterStateActionStep {
public static final String INITIALIZATION_PHASE = "new";
public static final StepKey KEY = new StepKey(INITIALIZATION_PHASE, "init", "init");
Expand All @@ -30,18 +31,19 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
// Index must have been since deleted, ignore it
return clusterState;
}
Settings settings = indexMetaData.getSettings();
if (settings.hasValue(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE)) {
LifecycleExecutionState lifecycleState = LifecycleExecutionState
.fromIndexMetadata(indexMetaData);
if (lifecycleState.getLifecycleDate() != null) {
return clusterState;
}

ClusterState.Builder newClusterStateBuilder = ClusterState.builder(clusterState);
IndexMetaData idxMeta = clusterState.getMetaData().index(index);
Settings.Builder indexSettings = Settings.builder().put(idxMeta.getSettings())
.put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, idxMeta.getCreationDate());

LifecycleExecutionState.Builder newCustomData = LifecycleExecutionState.builder(lifecycleState);
newCustomData.setIndexCreationDate(indexMetaData.getCreationDate());
newClusterStateBuilder.metaData(MetaData.builder(clusterState.getMetaData()).put(IndexMetaData
.builder(clusterState.getMetaData().index(index))
.settings(indexSettings)));
.builder(indexMetaData)
.putCustom(ILM_CUSTOM_METADATA_KEY, newCustomData.build().asMap())));
return newClusterStateBuilder.build();
}
}
Loading