Skip to content

Commit b364cf5

Browse files
committed
Introduce Primary Terms
Every shard group in Elasticsearch has a selected copy called a primary. When a primary shard fails a new primary would be selected from the existing replica copies. This PR introduces `primary terms` to track the number of times this has happened. This will allow us, as follow up work and among other things, to identify operations that come from old stale primaries. It is also the first step in road towards sequence numbers. Relates to #10708 Closes #14062
1 parent 75cedca commit b364cf5

38 files changed

+895
-376
lines changed

core/src/main/java/org/elasticsearch/cluster/ClusterState.java

+14-17
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import com.carrotsearch.hppc.cursors.ObjectCursor;
2323
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
24-
2524
import org.elasticsearch.cluster.DiffableUtils.KeyedReader;
2625
import org.elasticsearch.cluster.block.ClusterBlock;
2726
import org.elasticsearch.cluster.block.ClusterBlocks;
@@ -31,12 +30,7 @@
3130
import org.elasticsearch.cluster.metadata.MetaData;
3231
import org.elasticsearch.cluster.node.DiscoveryNode;
3332
import org.elasticsearch.cluster.node.DiscoveryNodes;
34-
import org.elasticsearch.cluster.routing.IndexRoutingTable;
35-
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
36-
import org.elasticsearch.cluster.routing.RoutingNode;
37-
import org.elasticsearch.cluster.routing.RoutingNodes;
38-
import org.elasticsearch.cluster.routing.RoutingTable;
39-
import org.elasticsearch.cluster.routing.ShardRouting;
33+
import org.elasticsearch.cluster.routing.*;
4034
import org.elasticsearch.cluster.routing.allocation.RoutingAllocation;
4135
import org.elasticsearch.cluster.service.InternalClusterService;
4236
import org.elasticsearch.common.Nullable;
@@ -57,11 +51,7 @@
5751
import org.elasticsearch.discovery.zen.publish.PublishClusterStateAction;
5852

5953
import java.io.IOException;
60-
import java.util.EnumSet;
61-
import java.util.HashMap;
62-
import java.util.Locale;
63-
import java.util.Map;
64-
import java.util.Set;
54+
import java.util.*;
6555

6656
/**
6757
* Represents the current state of the cluster.
@@ -137,7 +127,7 @@ public static <T extends Custom> T lookupPrototype(String type) {
137127

138128
public static <T extends Custom> T lookupPrototypeSafe(String type) {
139129
@SuppressWarnings("unchecked")
140-
T proto = (T)customPrototypes.get(type);
130+
T proto = (T) customPrototypes.get(type);
141131
if (proto == null) {
142132
throw new IllegalArgumentException("No custom state prototype registered for type [" + type + "]");
143133
}
@@ -478,6 +468,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
478468
}
479469
builder.endArray();
480470

471+
builder.startObject("primary_terms");
472+
for (int shard = 0; shard < indexMetaData.getNumberOfShards(); shard++) {
473+
builder.field(Integer.toString(shard), indexMetaData.primaryTerm(shard));
474+
}
475+
builder.endObject();
476+
481477
builder.endObject();
482478
}
483479
builder.endObject();
@@ -593,6 +589,7 @@ public Builder nodes(DiscoveryNodes nodes) {
593589

594590
public Builder routingResult(RoutingAllocation.Result routingResult) {
595591
this.routingTable = routingResult.routingTable();
592+
this.metaData = routingResult.metaData();
596593
return this;
597594
}
598595

@@ -673,16 +670,16 @@ public static byte[] toBytes(ClusterState state) throws IOException {
673670
}
674671

675672
/**
676-
* @param data input bytes
677-
* @param localNode used to set the local node in the cluster state.
673+
* @param data input bytes
674+
* @param localNode used to set the local node in the cluster state.
678675
*/
679676
public static ClusterState fromBytes(byte[] data, DiscoveryNode localNode) throws IOException {
680677
return readFrom(StreamInput.wrap(data), localNode);
681678
}
682679

683680
/**
684-
* @param in input stream
685-
* @param localNode used to set the local node in the cluster state. can be null.
681+
* @param in input stream
682+
* @param localNode used to set the local node in the cluster state. can be null.
686683
*/
687684
public static ClusterState readFrom(StreamInput in, @Nullable DiscoveryNode localNode) throws IOException {
688685
return PROTO.readFrom(in, localNode);

0 commit comments

Comments
 (0)