|
| 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 | +package org.elasticsearch.xpack.core.ilm; |
| 7 | + |
| 8 | +import com.carrotsearch.hppc.cursors.IntObjectCursor; |
| 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.AliasOrIndex; |
| 14 | +import org.elasticsearch.cluster.metadata.AliasOrIndex.Alias; |
| 15 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 16 | +import org.elasticsearch.cluster.routing.IndexRoutingTable; |
| 17 | +import org.elasticsearch.cluster.routing.IndexShardRoutingTable; |
| 18 | +import org.elasticsearch.common.ParseField; |
| 19 | +import org.elasticsearch.common.Strings; |
| 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.List; |
| 26 | +import java.util.Locale; |
| 27 | +import java.util.Objects; |
| 28 | + |
| 29 | +/** |
| 30 | + * After we performed the index rollover we wait for the the configured number of shards for the rolled over index (ie. newly created |
| 31 | + * index) to become available. |
| 32 | + */ |
| 33 | +public class WaitForActiveShardsStep extends ClusterStateWaitStep { |
| 34 | + |
| 35 | + public static final String NAME = "wait-for-active-shards"; |
| 36 | + |
| 37 | + private static final Logger logger = LogManager.getLogger(WaitForActiveShardsStep.class); |
| 38 | + |
| 39 | + WaitForActiveShardsStep(StepKey key, StepKey nextStepKey) { |
| 40 | + super(key, nextStepKey); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public boolean isRetryable() { |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Result isConditionMet(Index index, ClusterState clusterState) { |
| 50 | + IndexMetaData originalIndexMeta = clusterState.metaData().index(index); |
| 51 | + |
| 52 | + if (originalIndexMeta == null) { |
| 53 | + String errorMessage = String.format(Locale.ROOT, "[%s] lifecycle action for index [%s] executed but index no longer exists", |
| 54 | + getKey().getAction(), index.getName()); |
| 55 | + // Index must have been since deleted |
| 56 | + logger.debug(errorMessage); |
| 57 | + return new Result(false, new Info(errorMessage)); |
| 58 | + } |
| 59 | + |
| 60 | + boolean indexingComplete = LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE_SETTING.get(originalIndexMeta.getSettings()); |
| 61 | + if (indexingComplete) { |
| 62 | + String message = String.format(Locale.ROOT, "index [%s] has lifecycle complete set, skipping [%s]", |
| 63 | + originalIndexMeta.getIndex().getName(), WaitForActiveShardsStep.NAME); |
| 64 | + logger.trace(message); |
| 65 | + return new Result(true, new Info(message)); |
| 66 | + } |
| 67 | + |
| 68 | + String rolloverAlias = RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING.get(originalIndexMeta.getSettings()); |
| 69 | + if (Strings.isNullOrEmpty(rolloverAlias)) { |
| 70 | + throw new IllegalStateException("setting [" + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS |
| 71 | + + "] is not set on index [" + originalIndexMeta.getIndex().getName() + "]"); |
| 72 | + } |
| 73 | + |
| 74 | + AliasOrIndex aliasOrIndex = clusterState.metaData().getAliasAndIndexLookup().get(rolloverAlias); |
| 75 | + assert aliasOrIndex.isAlias() : rolloverAlias + " must be an alias but it is an index"; |
| 76 | + |
| 77 | + Alias alias = (Alias) aliasOrIndex; |
| 78 | + IndexMetaData aliasWriteIndex = alias.getWriteIndex(); |
| 79 | + final String rolledIndexName; |
| 80 | + final String waitForActiveShardsSettingValue; |
| 81 | + if (aliasWriteIndex != null) { |
| 82 | + rolledIndexName = aliasWriteIndex.getIndex().getName(); |
| 83 | + waitForActiveShardsSettingValue = aliasWriteIndex.getSettings().get("index.write.wait_for_active_shards"); |
| 84 | + } else { |
| 85 | + List<IndexMetaData> indices = alias.getIndices(); |
| 86 | + int maxIndexCounter = -1; |
| 87 | + IndexMetaData rolledIndexMeta = null; |
| 88 | + for (IndexMetaData indexMetaData : indices) { |
| 89 | + int indexNameCounter = parseIndexNameCounter(indexMetaData.getIndex().getName()); |
| 90 | + if (maxIndexCounter < indexNameCounter) { |
| 91 | + maxIndexCounter = indexNameCounter; |
| 92 | + rolledIndexMeta = indexMetaData; |
| 93 | + } |
| 94 | + } |
| 95 | + if (rolledIndexMeta == null) { |
| 96 | + String errorMessage = String.format(Locale.ROOT, |
| 97 | + "unable to find the index that was rolled over from [%s] as part of lifecycle action [%s]", index.getName(), |
| 98 | + getKey().getAction()); |
| 99 | + |
| 100 | + // Index must have been since deleted |
| 101 | + logger.debug(errorMessage); |
| 102 | + return new Result(false, new Info(errorMessage)); |
| 103 | + } |
| 104 | + rolledIndexName = rolledIndexMeta.getIndex().getName(); |
| 105 | + waitForActiveShardsSettingValue = rolledIndexMeta.getSettings().get("index.write.wait_for_active_shards"); |
| 106 | + } |
| 107 | + |
| 108 | + ActiveShardCount activeShardCount = ActiveShardCount.parseString(waitForActiveShardsSettingValue); |
| 109 | + boolean enoughShardsActive = activeShardCount.enoughShardsActive(clusterState, rolledIndexName); |
| 110 | + |
| 111 | + IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(rolledIndexName); |
| 112 | + int currentActiveShards = 0; |
| 113 | + for (final IntObjectCursor<IndexShardRoutingTable> shardRouting : indexRoutingTable.getShards()) { |
| 114 | + currentActiveShards += shardRouting.value.activeShards().size(); |
| 115 | + } |
| 116 | + return new Result(enoughShardsActive, new ActiveShardsInfo(currentActiveShards, activeShardCount.toString(), enoughShardsActive)); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Parses the number from the rolled over index name. It also supports the date-math format (ie. index name is wrapped in < and >) |
| 121 | + * <p> |
| 122 | + * Eg. |
| 123 | + * <p> |
| 124 | + * - For "logs-000002" it'll return 2 |
| 125 | + * - For "<logs-{now/d}-3>" it'll return 3 |
| 126 | + */ |
| 127 | + static int parseIndexNameCounter(String indexName) { |
| 128 | + int numberIndex = indexName.lastIndexOf("-"); |
| 129 | + if (numberIndex == -1) { |
| 130 | + throw new IllegalArgumentException("no - separator found in index name [" + indexName + "]"); |
| 131 | + } |
| 132 | + try { |
| 133 | + return Integer.parseInt(indexName.substring(numberIndex + 1, indexName.endsWith(">") ? indexName.length() - 1 : |
| 134 | + indexName.length())); |
| 135 | + } catch (NumberFormatException e) { |
| 136 | + throw new IllegalArgumentException("unable to parse the index name [" + indexName + "] to extract the counter", e); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + static final class ActiveShardsInfo implements ToXContentObject { |
| 141 | + |
| 142 | + private final long currentActiveShardsCount; |
| 143 | + private final String targetActiveShardsCount; |
| 144 | + private final boolean enoughShardsActive; |
| 145 | + private final String message; |
| 146 | + |
| 147 | + static final ParseField CURRENT_ACTIVE_SHARDS_COUNT = new ParseField("current_active_shards_count"); |
| 148 | + static final ParseField TARGET_ACTIVE_SHARDS_COUNT = new ParseField("target_active_shards_count"); |
| 149 | + static final ParseField ENOUGH_SHARDS_ACTIVE = new ParseField("enough_shards_active"); |
| 150 | + static final ParseField MESSAGE = new ParseField("message"); |
| 151 | + |
| 152 | + ActiveShardsInfo(long currentActiveShardsCount, String targetActiveShardsCount, boolean enoughShardsActive) { |
| 153 | + this.currentActiveShardsCount = currentActiveShardsCount; |
| 154 | + this.targetActiveShardsCount = targetActiveShardsCount; |
| 155 | + this.enoughShardsActive = enoughShardsActive; |
| 156 | + |
| 157 | + if (enoughShardsActive) { |
| 158 | + message = "the target of [" + targetActiveShardsCount + "] are active. Don't need to wait anymore"; |
| 159 | + } else { |
| 160 | + message = "waiting for [" + targetActiveShardsCount + "] shards to become active, but only [" + currentActiveShardsCount + |
| 161 | + "] are active"; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 167 | + builder.startObject(); |
| 168 | + builder.field(MESSAGE.getPreferredName(), message); |
| 169 | + builder.field(CURRENT_ACTIVE_SHARDS_COUNT.getPreferredName(), currentActiveShardsCount); |
| 170 | + builder.field(TARGET_ACTIVE_SHARDS_COUNT.getPreferredName(), targetActiveShardsCount); |
| 171 | + builder.field(ENOUGH_SHARDS_ACTIVE.getPreferredName(), enoughShardsActive); |
| 172 | + builder.endObject(); |
| 173 | + return builder; |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public boolean equals(Object o) { |
| 178 | + if (this == o) { |
| 179 | + return true; |
| 180 | + } |
| 181 | + if (o == null || getClass() != o.getClass()) { |
| 182 | + return false; |
| 183 | + } |
| 184 | + ActiveShardsInfo info = (ActiveShardsInfo) o; |
| 185 | + return currentActiveShardsCount == info.currentActiveShardsCount && |
| 186 | + enoughShardsActive == info.enoughShardsActive && |
| 187 | + Objects.equals(targetActiveShardsCount, info.targetActiveShardsCount) && |
| 188 | + Objects.equals(message, info.message); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public int hashCode() { |
| 193 | + return Objects.hash(currentActiveShardsCount, targetActiveShardsCount, enoughShardsActive, message); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + static final class Info implements ToXContentObject { |
| 198 | + |
| 199 | + private final String message; |
| 200 | + |
| 201 | + static final ParseField MESSAGE = new ParseField("message"); |
| 202 | + |
| 203 | + Info(String message) { |
| 204 | + this.message = message; |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 209 | + builder.startObject(); |
| 210 | + builder.field(MESSAGE.getPreferredName(), message); |
| 211 | + builder.endObject(); |
| 212 | + return builder; |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public boolean equals(Object o) { |
| 217 | + if (this == o) { |
| 218 | + return true; |
| 219 | + } |
| 220 | + if (o == null || getClass() != o.getClass()) { |
| 221 | + return false; |
| 222 | + } |
| 223 | + Info info = (Info) o; |
| 224 | + return Objects.equals(message, info.message); |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public int hashCode() { |
| 229 | + return Objects.hash(message); |
| 230 | + } |
| 231 | + } |
| 232 | +} |
0 commit comments