|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.core.indexlifecycle; |
| 8 | + |
| 9 | +import org.apache.logging.log4j.LogManager; |
| 10 | +import org.apache.logging.log4j.Logger; |
| 11 | +import org.elasticsearch.action.support.ActiveShardCount; |
| 12 | +import org.elasticsearch.cluster.ClusterState; |
| 13 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 14 | +import org.elasticsearch.cluster.routing.IndexRoutingTable; |
| 15 | +import org.elasticsearch.cluster.routing.ShardRouting; |
| 16 | +import org.elasticsearch.cluster.routing.ShardRoutingState; |
| 17 | +import org.elasticsearch.common.ParseField; |
| 18 | +import org.elasticsearch.common.Strings; |
| 19 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 20 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 21 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 22 | +import org.elasticsearch.index.Index; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.Locale; |
| 26 | +import java.util.Objects; |
| 27 | + |
| 28 | +/** |
| 29 | + * This step is used prior to running a shrink step in order to ensure that the index being shrunk |
| 30 | + * has a copy of each shard allocated on one particular node (the node used by the require |
| 31 | + * parameter) and that the shards are not relocating. |
| 32 | + */ |
| 33 | +public class CheckShrinkReadyStep extends ClusterStateWaitStep { |
| 34 | + public static final String NAME = "check-shrink-allocation"; |
| 35 | + |
| 36 | + private static final Logger logger = LogManager.getLogger(CheckShrinkReadyStep.class); |
| 37 | + |
| 38 | + CheckShrinkReadyStep(StepKey key, StepKey nextStepKey) { |
| 39 | + super(key, nextStepKey); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public Result isConditionMet(Index index, ClusterState clusterState) { |
| 44 | + IndexMetaData idxMeta = clusterState.metaData().index(index); |
| 45 | + |
| 46 | + if (idxMeta == null) { |
| 47 | + // Index must have been since deleted, ignore it |
| 48 | + logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", |
| 49 | + getKey().getAction(), index.getName()); |
| 50 | + return new Result(false, null); |
| 51 | + } |
| 52 | + |
| 53 | + // How many shards the node should have |
| 54 | + int expectedShardCount = idxMeta.getNumberOfShards(); |
| 55 | + |
| 56 | + if (ActiveShardCount.ALL.enoughShardsActive(clusterState, index.getName()) == false) { |
| 57 | + logger.debug("[{}] shrink action for [{}] cannot make progress because not all shards are active", |
| 58 | + getKey().getAction(), index.getName()); |
| 59 | + return new Result(false, new CheckShrinkReadyStep.Info("", expectedShardCount, -1)); |
| 60 | + } |
| 61 | + |
| 62 | + // The id of the node the shards should be on |
| 63 | + final String idShardsShouldBeOn = idxMeta.getSettings().get(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_PREFIX + "._id"); |
| 64 | + if (idShardsShouldBeOn == null) { |
| 65 | + throw new IllegalStateException("Cannot check shrink allocation as there are no allocation rules by _id"); |
| 66 | + } |
| 67 | + |
| 68 | + final IndexRoutingTable routingTable = clusterState.getRoutingTable().index(index); |
| 69 | + int foundShards = 0; |
| 70 | + for (ShardRouting shard : routingTable.shardsWithState(ShardRoutingState.STARTED)) { |
| 71 | + final String currentNodeId = shard.currentNodeId(); |
| 72 | + if (idShardsShouldBeOn.equals(currentNodeId) && shard.relocating() == false) { |
| 73 | + foundShards++; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + logger.trace("{} checking for shrink readiness on [{}], found {} shards and need {}", |
| 78 | + index, idShardsShouldBeOn, foundShards, expectedShardCount); |
| 79 | + |
| 80 | + if (foundShards == expectedShardCount) { |
| 81 | + logger.trace("{} successfully found {} allocated shards for shrink readiness on node [{}] ({})", |
| 82 | + index, expectedShardCount, idShardsShouldBeOn, getKey().getAction()); |
| 83 | + return new Result(true, null); |
| 84 | + } else { |
| 85 | + logger.trace("{} failed to find {} allocated shards (found {}) on node [{}] for shrink readiness ({})", |
| 86 | + index, expectedShardCount, foundShards, idShardsShouldBeOn, getKey().getAction()); |
| 87 | + return new Result(false, new CheckShrinkReadyStep.Info(idShardsShouldBeOn, expectedShardCount, |
| 88 | + expectedShardCount - foundShards)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public int hashCode() { |
| 94 | + return 612; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public boolean equals(Object obj) { |
| 99 | + if (obj == null) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + if (getClass() != obj.getClass()) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + return super.equals(obj); |
| 106 | + } |
| 107 | + |
| 108 | + public static final class Info implements ToXContentObject { |
| 109 | + |
| 110 | + private final String nodeId; |
| 111 | + private final long actualReplicas; |
| 112 | + private final long numberShardsLeftToAllocate; |
| 113 | + private final String message; |
| 114 | + |
| 115 | + static final ParseField NODE_ID = new ParseField("node_id"); |
| 116 | + static final ParseField EXPECTED_SHARDS = new ParseField("expected_shards"); |
| 117 | + static final ParseField SHARDS_TO_ALLOCATE = new ParseField("shards_left_to_allocate"); |
| 118 | + static final ParseField MESSAGE = new ParseField("message"); |
| 119 | + static final ConstructingObjectParser<CheckShrinkReadyStep.Info, Void> PARSER = new ConstructingObjectParser<>( |
| 120 | + "check_shrink_ready_step_info", a -> new CheckShrinkReadyStep.Info((String) a[0], (long) a[1], (long) a[2])); |
| 121 | + static { |
| 122 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), NODE_ID); |
| 123 | + PARSER.declareLong(ConstructingObjectParser.constructorArg(), EXPECTED_SHARDS); |
| 124 | + PARSER.declareLong(ConstructingObjectParser.constructorArg(), SHARDS_TO_ALLOCATE); |
| 125 | + PARSER.declareString((i, s) -> {}, MESSAGE); |
| 126 | + } |
| 127 | + |
| 128 | + public Info(String nodeId, long expectedShards, long numberShardsLeftToAllocate) { |
| 129 | + this.nodeId = nodeId; |
| 130 | + this.actualReplicas = expectedShards; |
| 131 | + this.numberShardsLeftToAllocate = numberShardsLeftToAllocate; |
| 132 | + if (numberShardsLeftToAllocate < 0) { |
| 133 | + this.message = "Waiting for all shards to become active"; |
| 134 | + } else { |
| 135 | + this.message = String.format(Locale.ROOT, "Waiting for node [%s] to contain [%d] shards, found [%d], remaining [%d]", |
| 136 | + nodeId, expectedShards, expectedShards - numberShardsLeftToAllocate, numberShardsLeftToAllocate); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 142 | + builder.startObject(); |
| 143 | + builder.field(MESSAGE.getPreferredName(), message); |
| 144 | + builder.field(NODE_ID.getPreferredName(), nodeId); |
| 145 | + builder.field(SHARDS_TO_ALLOCATE.getPreferredName(), numberShardsLeftToAllocate); |
| 146 | + builder.field(EXPECTED_SHARDS.getPreferredName(), actualReplicas); |
| 147 | + builder.endObject(); |
| 148 | + return builder; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public int hashCode() { |
| 153 | + return Objects.hash(nodeId, actualReplicas, numberShardsLeftToAllocate); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public boolean equals(Object obj) { |
| 158 | + if (obj == null) { |
| 159 | + return false; |
| 160 | + } |
| 161 | + if (getClass() != obj.getClass()) { |
| 162 | + return false; |
| 163 | + } |
| 164 | + CheckShrinkReadyStep.Info other = (CheckShrinkReadyStep.Info) obj; |
| 165 | + return Objects.equals(actualReplicas, other.actualReplicas) && |
| 166 | + Objects.equals(numberShardsLeftToAllocate, other.numberShardsLeftToAllocate) && |
| 167 | + Objects.equals(nodeId, other.nodeId); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public String toString() { |
| 172 | + return Strings.toString(this); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments