Skip to content

Commit

Permalink
Reuse Info in lifecycle step (#89419)
Browse files Browse the repository at this point in the history
We have a `SingleMessageFiledInfo` defined in
`org/elasticsearch/xpack/core/ilm/step/info` to provide single message
info for `AsyncWaitStep` and `ClusterStateWaitStep`.But there are still
some steps like `CheckNotDataStreamWriteIndexStep` defining their own
single message info. This pr removes the duplicated info defination in
these steps and use `SingleMessageFiledInfo` instand.
  • Loading branch information
mushao999 authored Aug 18, 2022
1 parent 58ddca3 commit 22e1150
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 189 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/89419.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 89419
summary: Reuse Info in lifecycle step
area: ILM+SLM
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.index.Index;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo;

import java.io.IOException;
import java.util.Locale;
import java.util.Objects;

/**
* Some actions cannot be executed on a data stream's write index (eg. `searchable-snapshot`). This step checks if the managed index is
Expand Down Expand Up @@ -57,7 +53,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
);
// Index must have been since deleted
logger.debug(errorMessage);
return new Result(false, new Info(errorMessage));
return new Result(false, new SingleMessageFieldInfo(errorMessage));
}

String policyName = indexMetadata.getLifecyclePolicyName();
Expand All @@ -77,50 +73,10 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
policyName
);
logger.debug(errorMessage);
return new Result(false, new Info(errorMessage));
return new Result(false, new SingleMessageFieldInfo(errorMessage));
}
}

return new Result(true, null);
}

static final class Info implements ToXContentObject {

private final String message;

static final ParseField MESSAGE = new ParseField("message");

Info(String message) {
this.message = message;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(MESSAGE.getPreferredName(), message);
builder.endObject();
return builder;
}

public String getMessage() {
return message;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Info info = (Info) o;
return Objects.equals(message, info.message);
}

@Override
public int hashCode() {
return Objects.hash(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -61,7 +62,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
);
// Index must have been since deleted
logger.debug(errorMessage);
return new Result(false, new Info(errorMessage));
return new Result(false, new SingleMessageFieldInfo(errorMessage));
}

boolean indexingComplete = LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE_SETTING.get(originalIndexMeta.getSettings());
Expand All @@ -73,7 +74,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
WaitForActiveShardsStep.NAME
);
logger.trace(message);
return new Result(true, new Info(message));
return new Result(true, new SingleMessageFieldInfo(message));
}

IndexAbstraction indexAbstraction = metadata.getIndicesLookup().get(index.getName());
Expand Down Expand Up @@ -149,7 +150,7 @@ private static Result getErrorResultOnNullMetadata(StepKey key, Index originalIn

// Index must have been since deleted
logger.debug(errorMessage);
return new Result(false, new Info(errorMessage));
return new Result(false, new SingleMessageFieldInfo(errorMessage));
}

static final class ActiveShardsInfo implements ToXContentObject {
Expand Down Expand Up @@ -211,40 +212,4 @@ public int hashCode() {
return Objects.hash(currentActiveShardsCount, targetActiveShardsCount, enoughShardsActive, message);
}
}

static final class Info implements ToXContentObject {

private final String message;

static final ParseField MESSAGE = new ParseField("message");

Info(String message) {
this.message = message;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(MESSAGE.getPreferredName(), message);
builder.endObject();
return builder;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Info info = (Info) o;
return Objects.equals(message, info.message);
}

@Override
public int hashCode() {
return Objects.hash(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.Index;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo;

import java.io.IOException;
import java.util.Locale;
import java.util.Objects;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -100,7 +97,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
indexName
);
logger.debug(errorMessage);
return new Result(false, new Info(errorMessage));
return new Result(false, new SingleMessageFieldInfo(errorMessage));
}

IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(indexMetadata.getIndex());
Expand All @@ -119,79 +116,39 @@ public boolean isRetryable() {

private static Result waitForRed(IndexRoutingTable indexRoutingTable) {
if (indexRoutingTable == null) {
return new Result(true, new Info("index is red"));
return new Result(true, new SingleMessageFieldInfo("index is red"));
}
return new Result(false, new Info("index is not red"));
return new Result(false, new SingleMessageFieldInfo("index is not red"));
}

private static Result waitForYellow(IndexRoutingTable indexRoutingTable) {
if (indexRoutingTable == null) {
return new Result(false, new Info("index is red; no indexRoutingTable"));
return new Result(false, new SingleMessageFieldInfo("index is red; no indexRoutingTable"));
}

boolean indexIsAtLeastYellow = indexRoutingTable.allPrimaryShardsActive();
if (indexIsAtLeastYellow) {
return new Result(true, null);
} else {
return new Result(false, new Info("index is red; not all primary shards are active"));
return new Result(false, new SingleMessageFieldInfo("index is red; not all primary shards are active"));
}
}

private static Result waitForGreen(IndexRoutingTable indexRoutingTable) {
if (indexRoutingTable == null) {
return new Result(false, new Info("index is red; no indexRoutingTable"));
return new Result(false, new SingleMessageFieldInfo("index is red; no indexRoutingTable"));
}

if (indexRoutingTable.allPrimaryShardsActive()) {
for (int i = 0; i < indexRoutingTable.size(); i++) {
boolean replicaIndexIsGreen = indexRoutingTable.shard(i).replicaShards().stream().allMatch(ShardRouting::active);
if (replicaIndexIsGreen == false) {
return new Result(false, new Info("index is yellow; not all replica shards are active"));
return new Result(false, new SingleMessageFieldInfo("index is yellow; not all replica shards are active"));
}
}
return new Result(true, null);
}

return new Result(false, new Info("index is not green; not all shards are active"));
}

static final class Info implements ToXContentObject {

static final ParseField MESSAGE_FIELD = new ParseField("message");

private final String message;

Info(String message) {
this.message = message;
}

String getMessage() {
return message;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(MESSAGE_FIELD.getPreferredName(), message);
builder.endObject();
return builder;
}

@Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (getClass() != o.getClass()) {
return false;
}
Info info = (Info) o;
return Objects.equals(getMessage(), info.getMessage());
}

@Override
public int hashCode() {
return Objects.hash(getMessage());
}
return new Result(false, new SingleMessageFieldInfo("index is not green; not all shards are active"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.Index;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;

/**
Expand All @@ -39,6 +35,8 @@ public class WaitForNoFollowersStep extends AsyncWaitStep {

static final String NAME = "wait-for-shard-history-leases";
static final String CCR_LEASE_KEY = "ccr";
private static final String WAIT_MESSAGE = "this index is a leader index; waiting for all following indices to cease "
+ "following before proceeding";

WaitForNoFollowersStep(StepKey key, StepKey nextStepKey, Client client) {
super(key, nextStepKey, client);
Expand Down Expand Up @@ -73,48 +71,10 @@ public void evaluateCondition(Metadata metadata, Index index, Listener listener,
.anyMatch(lease -> lease.isPresent() && lease.get().anyMatch(l -> CCR_LEASE_KEY.equals(l.source())));

if (isCurrentlyLeaderIndex) {
listener.onResponse(false, new Info());
listener.onResponse(false, new SingleMessageFieldInfo(WAIT_MESSAGE));
} else {
listener.onResponse(true, null);
}
}, listener::onFailure));
}

static final class Info implements ToXContentObject {

static final ParseField MESSAGE_FIELD = new ParseField("message");

private static final String message = "this index is a leader index; waiting for all following indices to cease "
+ "following before proceeding";

Info() {}

static String getMessage() {
return message;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(MESSAGE_FIELD.getPreferredName(), message);
builder.endObject();
return builder;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
return true;
}

@Override
public int hashCode() {
return Objects.hash(getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.index.Index;
import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo;

import java.util.List;

Expand Down Expand Up @@ -80,7 +81,7 @@ public void testStepIncompleteIfIndexIsTheDataStreamWriteIndex() {

ClusterStateWaitStep.Result result = createRandomInstance().isConditionMet(indexMetadata.getIndex(), clusterState);
assertThat(result.isComplete(), is(false));
CheckNotDataStreamWriteIndexStep.Info info = (CheckNotDataStreamWriteIndexStep.Info) result.getInfomationContext();
SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.getInfomationContext();
assertThat(
info.getMessage(),
is(
Expand Down
Loading

0 comments on commit 22e1150

Please sign in to comment.