Skip to content

Commit 21d2654

Browse files
committed
move primaryTerm to a long
1 parent 3d5afcb commit 21d2654

12 files changed

+63
-62
lines changed

core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package org.elasticsearch.cluster.metadata;
2121

22-
import com.carrotsearch.hppc.IntArrayList;
22+
import com.carrotsearch.hppc.LongArrayList;
2323
import com.carrotsearch.hppc.cursors.ObjectCursor;
2424
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
2525
import org.elasticsearch.Version;
@@ -173,7 +173,7 @@ public static State fromString(String state) {
173173

174174
private final String index;
175175
private final long version;
176-
private final int[] primaryTerms;
176+
private final long[] primaryTerms;
177177

178178
private final State state;
179179

@@ -195,7 +195,7 @@ public static State fromString(String state) {
195195
private final Version indexUpgradedVersion;
196196
private final org.apache.lucene.util.Version minimumCompatibleLuceneVersion;
197197

198-
private IndexMetaData(String index, long version, int[] primaryTerms, State state, Settings settings, ImmutableOpenMap<String, MappingMetaData> mappings, ImmutableOpenMap<String, AliasMetaData> aliases, ImmutableOpenMap<String, Custom> customs) {
198+
private IndexMetaData(String index, long version, long[] primaryTerms, State state, Settings settings, ImmutableOpenMap<String, MappingMetaData> mappings, ImmutableOpenMap<String, AliasMetaData> aliases, ImmutableOpenMap<String, Custom> customs) {
199199
Integer maybeNumberOfShards = settings.getAsInt(SETTING_NUMBER_OF_SHARDS, null);
200200
if (maybeNumberOfShards == null) {
201201
throw new IllegalArgumentException("must specify numberOfShards for index [" + index + "]");
@@ -288,7 +288,7 @@ public long getVersion() {
288288
* a primary shard is assigned after a full cluster restart (see {@link ShardRouting#initialize(java.lang.String, long)}
289289
* or a replica shard is promoted to a primary (see {@link ShardRouting#moveToPrimary()}).
290290
**/
291-
public int primaryTerm(int shardId) {
291+
public long primaryTerm(int shardId) {
292292
return this.primaryTerms[shardId];
293293
}
294294

@@ -471,7 +471,7 @@ private static class IndexMetaDataDiff implements Diff<IndexMetaData> {
471471

472472
private final String index;
473473
private final long version;
474-
private final int[] primaryTerms;
474+
private final long[] primaryTerms;
475475
private final State state;
476476
private final Settings settings;
477477
private final Diff<ImmutableOpenMap<String, MappingMetaData>> mappings;
@@ -494,7 +494,7 @@ public IndexMetaDataDiff(StreamInput in) throws IOException {
494494
version = in.readLong();
495495
state = State.fromId(in.readByte());
496496
settings = Settings.readSettingsFromStream(in);
497-
primaryTerms = in.readVIntArray();
497+
primaryTerms = in.readVLongArray();
498498
mappings = DiffableUtils.readImmutableOpenMapDiff(in, MappingMetaData.PROTO);
499499
aliases = DiffableUtils.readImmutableOpenMapDiff(in, AliasMetaData.PROTO);
500500
customs = DiffableUtils.readImmutableOpenMapDiff(in, new DiffableUtils.KeyedReader<Custom>() {
@@ -516,7 +516,7 @@ public void writeTo(StreamOutput out) throws IOException {
516516
out.writeLong(version);
517517
out.writeByte(state.id);
518518
Settings.writeSettingsToStream(settings, out);
519-
out.writeVIntArray(primaryTerms);
519+
out.writeVLongArray(primaryTerms);
520520
mappings.writeTo(out);
521521
aliases.writeTo(out);
522522
customs.writeTo(out);
@@ -542,7 +542,7 @@ public IndexMetaData readFrom(StreamInput in) throws IOException {
542542
builder.version(in.readLong());
543543
builder.state(State.fromId(in.readByte()));
544544
builder.settings(readSettingsFromStream(in));
545-
builder.primaryTerms(in.readVIntArray());
545+
builder.primaryTerms(in.readVLongArray());
546546
int mappingsSize = in.readVInt();
547547
for (int i = 0; i < mappingsSize; i++) {
548548
MappingMetaData mappingMd = MappingMetaData.PROTO.readFrom(in);
@@ -568,7 +568,7 @@ public void writeTo(StreamOutput out) throws IOException {
568568
out.writeLong(version);
569569
out.writeByte(state.id());
570570
writeSettingsToStream(settings, out);
571-
out.writeVIntArray(primaryTerms);
571+
out.writeVLongArray(primaryTerms);
572572
out.writeVInt(mappings.size());
573573
for (ObjectCursor<MappingMetaData> cursor : mappings.values()) {
574574
cursor.value.writeTo(out);
@@ -597,7 +597,7 @@ public static class Builder {
597597
private String index;
598598
private State state = State.OPEN;
599599
private long version = 1;
600-
private int[] primaryTerms = null;
600+
private long[] primaryTerms = null;
601601
private Settings settings = Settings.Builder.EMPTY_SETTINGS;
602602
private final ImmutableOpenMap.Builder<String, MappingMetaData> mappings;
603603
private final ImmutableOpenMap.Builder<String, AliasMetaData> aliases;
@@ -739,7 +739,7 @@ public Builder version(long version) {
739739
* returns the primary term for the given shard.
740740
* See {@link IndexMetaData#primaryTerm(int)} for more information.
741741
*/
742-
public int primaryTerm(int shardId) {
742+
public long primaryTerm(int shardId) {
743743
if (primaryTerms == null) {
744744
initializePrimaryTerms();
745745
}
@@ -750,15 +750,15 @@ public int primaryTerm(int shardId) {
750750
* sets the primary term for the given shard.
751751
* See {@link IndexMetaData#primaryTerm(int)} for more information.
752752
*/
753-
public Builder primaryTerm(int shardId, int primaryTerm) {
753+
public Builder primaryTerm(int shardId, long primaryTerm) {
754754
if (primaryTerms == null) {
755755
initializePrimaryTerms();
756756
}
757757
this.primaryTerms[shardId] = primaryTerm;
758758
return this;
759759
}
760760

761-
private void primaryTerms(int[] primaryTerms) {
761+
private void primaryTerms(long[] primaryTerms) {
762762
this.primaryTerms = primaryTerms;
763763
}
764764

@@ -767,7 +767,7 @@ private void initializePrimaryTerms() {
767767
if (numberOfShards() < 0) {
768768
throw new IllegalStateException("you must set the number of shards before setting/reading primary terms");
769769
}
770-
primaryTerms = new int[numberOfShards()];
770+
primaryTerms = new long[numberOfShards()];
771771
}
772772

773773

@@ -912,10 +912,10 @@ public static IndexMetaData fromXContent(XContentParser parser) throws IOExcepti
912912
}
913913
}
914914
} else if (fieldEquals(Fields.PRIMARY_TERMS, currentFieldName)) {
915-
IntArrayList list = new IntArrayList();
915+
LongArrayList list = new LongArrayList();
916916
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
917917
if (token == XContentParser.Token.VALUE_NUMBER) {
918-
list.add(parser.intValue());
918+
list.add(parser.longValue());
919919
} else {
920920
throw new IllegalStateException("found a non-numeric value under [" + Fields.PRIMARY_TERMS.underscore() + "]");
921921
}

core/src/main/java/org/elasticsearch/cluster/routing/IndexRoutingTable.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ private Builder initializeAsRestore(IndexMetaData indexMetaData, RestoreSource r
425425
for (int shardId = 0; shardId < indexMetaData.getNumberOfShards(); shardId++) {
426426
IndexShardRoutingTable.Builder indexShardRoutingBuilder = new IndexShardRoutingTable.Builder(new ShardId(indexMetaData.getIndex(), shardId));
427427
for (int i = 0; i <= indexMetaData.getNumberOfReplicas(); i++) {
428-
final int primaryTerm = indexMetaData.primaryTerm(shardId);
428+
final long primaryTerm = indexMetaData.primaryTerm(shardId);
429429
if (asNew && ignoreShards.contains(shardId)) {
430430
// This shards wasn't completely snapshotted - restore it as new shard
431431
indexShardRoutingBuilder.addShard(ShardRouting.newUnassigned(index, shardId, null, primaryTerm, i == 0, unassignedInfo));
@@ -446,7 +446,7 @@ private Builder initializeEmpty(IndexMetaData indexMetaData, UnassignedInfo unas
446446
throw new IllegalStateException("trying to initialize an index with fresh shards, but already has shards created");
447447
}
448448
for (int shardId = 0; shardId < indexMetaData.getNumberOfShards(); shardId++) {
449-
final int primaryTerm = indexMetaData.primaryTerm(shardId);
449+
final long primaryTerm = indexMetaData.primaryTerm(shardId);
450450
IndexShardRoutingTable.Builder indexShardRoutingBuilder = new IndexShardRoutingTable.Builder(new ShardId(indexMetaData.getIndex(), shardId));
451451
for (int i = 0; i <= indexMetaData.getNumberOfReplicas(); i++) {
452452
indexShardRoutingBuilder.addShard(ShardRouting.newUnassigned(index, shardId, null,primaryTerm, i == 0, unassignedInfo));

core/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ public IndexShardRoutingTable normalizeVersionsAndPrimaryTerms() {
124124
return this;
125125
}
126126
long highestVersion = shards.get(0).version();
127-
int highestPrimaryTerm = shards.get(0).primaryTerm();
127+
long highestPrimaryTerm = shards.get(0).primaryTerm();
128128
boolean requiresNormalization = false;
129129
for (int i = 1; i < shards.size(); i++) {
130130
final long version = shards.get(i).version();
131-
final int primaryTerm = shards.get(i).primaryTerm();
131+
final long primaryTerm = shards.get(i).primaryTerm();
132132
if (highestVersion != version || highestPrimaryTerm != primaryTerm) {
133133
requiresNormalization = true;
134134
}

core/src/main/java/org/elasticsearch/cluster/routing/ShardRouting.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public final class ShardRouting implements Streamable, ToXContent {
4747
private String currentNodeId;
4848
private String relocatingNodeId;
4949
private boolean primary;
50-
private int primaryTerm;
50+
private long primaryTerm;
5151
private ShardRoutingState state;
5252
private long version;
5353
private RestoreSource restoreSource;
@@ -70,7 +70,7 @@ public ShardRouting(ShardRouting copy, long version) {
7070
this(copy, version, copy.primaryTerm());
7171
}
7272

73-
public ShardRouting(ShardRouting copy, long version, int primaryTerm) {
73+
public ShardRouting(ShardRouting copy, long version, long primaryTerm) {
7474
this(copy.index(), copy.id(), copy.currentNodeId(), copy.relocatingNodeId(), copy.restoreSource(), primaryTerm, copy.primary(), copy.state(), version, copy.unassignedInfo(), copy.allocationId(), true, copy.getExpectedShardSize());
7575
}
7676

@@ -79,7 +79,7 @@ public ShardRouting(ShardRouting copy, long version, int primaryTerm) {
7979
* by either this class or tests. Visible for testing.
8080
*/
8181
ShardRouting(String index, int shardId, String currentNodeId,
82-
String relocatingNodeId, RestoreSource restoreSource, int primaryTerm, boolean primary, ShardRoutingState state, long version,
82+
String relocatingNodeId, RestoreSource restoreSource, long primaryTerm, boolean primary, ShardRoutingState state, long version,
8383
UnassignedInfo unassignedInfo, AllocationId allocationId, boolean internal, long expectedShardSize) {
8484
this.index = index;
8585
this.shardId = shardId;
@@ -109,7 +109,7 @@ public ShardRouting(ShardRouting copy, long version, int primaryTerm) {
109109
/**
110110
* Creates a new unassigned shard.
111111
*/
112-
public static ShardRouting newUnassigned(String index, int shardId, RestoreSource restoreSource, int primaryTerm, boolean primary, UnassignedInfo unassignedInfo) {
112+
public static ShardRouting newUnassigned(String index, int shardId, RestoreSource restoreSource, long primaryTerm, boolean primary, UnassignedInfo unassignedInfo) {
113113
return new ShardRouting(index, shardId, null, null, restoreSource, primaryTerm, primary, ShardRoutingState.UNASSIGNED, 0, unassignedInfo, null, true, UNAVAILABLE_EXPECTED_SHARD_SIZE);
114114
}
115115

@@ -261,7 +261,7 @@ public boolean primary() {
261261
*
262262
* See {@link org.elasticsearch.cluster.metadata.IndexMetaData#primaryTerm(int)} for more info.
263263
*/
264-
public int primaryTerm() {
264+
public long primaryTerm() {
265265
return this.primaryTerm;
266266
}
267267

@@ -334,7 +334,7 @@ public void readFromThin(StreamInput in) throws IOException {
334334
}
335335

336336
primary = in.readBoolean();
337-
primaryTerm = in.readVInt();
337+
primaryTerm = in.readVLong();
338338
state = ShardRoutingState.fromValue(in.readByte());
339339

340340
restoreSource = RestoreSource.readOptionalRestoreSource(in);
@@ -380,7 +380,7 @@ public void writeToThin(StreamOutput out) throws IOException {
380380
}
381381

382382
out.writeBoolean(primary);
383-
out.writeVInt(primaryTerm);
383+
out.writeVLong(primaryTerm);
384384
out.writeByte(state.value());
385385

386386
if (restoreSource != null) {
@@ -684,7 +684,8 @@ public int hashCode() {
684684
result = 31 * result + (currentNodeId != null ? currentNodeId.hashCode() : 0);
685685
result = 31 * result + (relocatingNodeId != null ? relocatingNodeId.hashCode() : 0);
686686
result = 31 * result + (primary ? 1 : 0);
687-
result = 31 * result + primaryTerm;
687+
result = 31 * result + (int) (primaryTerm ^ (primaryTerm >>> 32));
688+
;
688689
result = 31 * result + (state != null ? state.hashCode() : 0);
689690
result = 31 * result + (int) (version ^ (version >>> 32));
690691
result = 31 * result + (restoreSource != null ? restoreSource.hashCode() : 0);

core/src/test/java/org/elasticsearch/cluster/metadata/ToAndFromJsonMetaDataTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void testSimpleJsonFromAndTo() throws IOException {
154154
MetaData parsedMetaData = MetaData.Builder.fromXContent(XContentFactory.xContent(XContentType.JSON).createParser(metaDataSource));
155155

156156
IndexMetaData indexMetaData = parsedMetaData.index("test1");
157-
assertThat(indexMetaData.primaryTerm(0), equalTo(1));
157+
assertThat(indexMetaData.primaryTerm(0), equalTo(1l));
158158
assertThat(indexMetaData.getNumberOfShards(), equalTo(1));
159159
assertThat(indexMetaData.getNumberOfReplicas(), equalTo(2));
160160
assertThat(indexMetaData.getCreationDate(), equalTo(-1l));
@@ -164,8 +164,8 @@ public void testSimpleJsonFromAndTo() throws IOException {
164164
indexMetaData = parsedMetaData.index("test2");
165165
assertThat(indexMetaData.getNumberOfShards(), equalTo(2));
166166
assertThat(indexMetaData.getNumberOfReplicas(), equalTo(3));
167-
assertThat(indexMetaData.primaryTerm(0), equalTo(2));
168-
assertThat(indexMetaData.primaryTerm(1), equalTo(2));
167+
assertThat(indexMetaData.primaryTerm(0), equalTo(2l));
168+
assertThat(indexMetaData.primaryTerm(1), equalTo(2l));
169169
assertThat(indexMetaData.getCreationDate(), equalTo(-1l));
170170
assertThat(indexMetaData.getSettings().getAsMap().size(), equalTo(5));
171171
assertThat(indexMetaData.getSettings().get("setting1"), equalTo("value1"));

core/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class RoutingTableTests extends ESAllocationTestCase {
5656
.build());
5757
private ClusterState clusterState;
5858

59-
private final Map<String, int[]> primaryTermsPerIndex = new HashMap<>();
59+
private final Map<String, long[]> primaryTermsPerIndex = new HashMap<>();
6060
private final Map<String, long[]> versionsPerIndex = new HashMap<>();
6161

6262
@Override
@@ -114,7 +114,7 @@ private void incrementVersion(String index, int shard) {
114114
}
115115

116116
private void incrementPrimaryTerm(String index) {
117-
final int[] primaryTerms = primaryTermsPerIndex.get(index);
117+
final long[] primaryTerms = primaryTermsPerIndex.get(index);
118118
for (int i = 0; i < primaryTerms.length; i++) {
119119
primaryTerms[i]++;
120120
}
@@ -167,7 +167,7 @@ private void failSomePrimaries(String index) {
167167
}
168168

169169
private IndexMetaData.Builder createIndexMetaData(String indexName) {
170-
primaryTermsPerIndex.put(indexName, new int[numberOfShards]);
170+
primaryTermsPerIndex.put(indexName, new long[numberOfShards]);
171171
final IndexMetaData.Builder builder = new IndexMetaData.Builder(indexName)
172172
.settings(DEFAULT_SETTINGS)
173173
.numberOfReplicas(this.numberOfReplicas)
@@ -185,7 +185,7 @@ private void assertAllVersionAndPrimaryTerm() {
185185

186186
private void assertVersionAndPrimaryTerm(String index) {
187187
final long[] versions = versionsPerIndex.get(index);
188-
final int[] terms = primaryTermsPerIndex.get(index);
188+
final long[] terms = primaryTermsPerIndex.get(index);
189189
final IndexMetaData indexMetaData = clusterState.metaData().index(index);
190190
for (IndexShardRoutingTable shardRoutingTable : this.testRoutingTable.index(index)) {
191191
final int shard = shardRoutingTable.shardId().id();

core/src/test/java/org/elasticsearch/cluster/routing/ShardRoutingTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
public class ShardRoutingTests extends ESTestCase {
3636

3737
public void testFrozenAfterRead() throws IOException {
38-
int term = randomInt(200);
38+
long term = randomInt(200);
3939
ShardRouting routing = TestShardRouting.newShardRouting("foo", 1, "node_1", null, null, term, false, ShardRoutingState.INITIALIZING, 1);
4040
routing.moveToPrimary();
4141
assertTrue(routing.primary());
@@ -53,7 +53,7 @@ public void testFrozenAfterRead() throws IOException {
5353
}
5454

5555
public void testPrimaryTermIncrementOnPromotion() {
56-
int term = randomInt(200);
56+
long term = randomInt(200);
5757
ShardRouting routing = TestShardRouting.newShardRouting("foo", 1, "node_1", null, null, term, false, ShardRoutingState.STARTED, 1);
5858
routing.moveToPrimary();
5959
assertTrue(routing.primary());
@@ -64,7 +64,7 @@ public void testPrimaryTermIncrementOnPromotion() {
6464
}
6565

6666
public void testIsSameAllocation() {
67-
int term = randomInt(200);
67+
long term = randomInt(200);
6868
ShardRouting unassignedShard0 = TestShardRouting.newShardRouting("test", 0, null, term, false, ShardRoutingState.UNASSIGNED, 1);
6969
ShardRouting unassignedShard1 = TestShardRouting.newShardRouting("test", 1, null, term, false, ShardRoutingState.UNASSIGNED, 1);
7070
ShardRouting initializingShard0 = TestShardRouting.newShardRouting("test", 0, "1", term, randomBoolean(), ShardRoutingState.INITIALIZING, 1);

0 commit comments

Comments
 (0)