Skip to content

Commit c468b2f

Browse files
authored
Make primary terms fields private in index shard (#38036)
This commit encapsulates the primary terms fields in index shard. This is a precursor to pushing the operation primary term down to the replication tracker.
1 parent 9782aaa commit c468b2f

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

server/src/main/java/org/elasticsearch/index/shard/IndexShard.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ public class IndexShard extends AbstractIndexShardComponent implements IndicesCl
198198

199199
protected volatile ShardRouting shardRouting;
200200
protected volatile IndexShardState state;
201-
protected volatile long pendingPrimaryTerm; // see JavaDocs for getPendingPrimaryTerm
202-
protected volatile long operationPrimaryTerm;
201+
private volatile long pendingPrimaryTerm; // see JavaDocs for getPendingPrimaryTerm
202+
private volatile long operationPrimaryTerm;
203203
protected final AtomicReference<Engine> currentEngineReference = new AtomicReference<>();
204204
final EngineFactory engineFactory;
205205

server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -956,18 +956,18 @@ private void finish() {
956956
// our operation should be blocked until the previous operations complete
957957
assertFalse(onResponse.get());
958958
assertNull(onFailure.get());
959-
assertThat(indexShard.operationPrimaryTerm, equalTo(primaryTerm));
959+
assertThat(indexShard.getOperationPrimaryTerm(), equalTo(primaryTerm));
960960
assertThat(TestTranslog.getCurrentTerm(getTranslog(indexShard)), equalTo(primaryTerm));
961961
Releasables.close(operation1);
962962
// our operation should still be blocked
963963
assertFalse(onResponse.get());
964964
assertNull(onFailure.get());
965-
assertThat(indexShard.operationPrimaryTerm, equalTo(primaryTerm));
965+
assertThat(indexShard.getOperationPrimaryTerm(), equalTo(primaryTerm));
966966
assertThat(TestTranslog.getCurrentTerm(getTranslog(indexShard)), equalTo(primaryTerm));
967967
Releasables.close(operation2);
968968
barrier.await();
969969
// now lock acquisition should have succeeded
970-
assertThat(indexShard.operationPrimaryTerm, equalTo(newPrimaryTerm));
970+
assertThat(indexShard.getOperationPrimaryTerm(), equalTo(newPrimaryTerm));
971971
assertThat(indexShard.getPendingPrimaryTerm(), equalTo(newPrimaryTerm));
972972
assertThat(TestTranslog.getCurrentTerm(getTranslog(indexShard)), equalTo(newPrimaryTerm));
973973
if (engineClosed) {
@@ -1008,7 +1008,7 @@ public void onFailure(Exception e) {
10081008
}
10091009
};
10101010

1011-
final long oldPrimaryTerm = indexShard.pendingPrimaryTerm - 1;
1011+
final long oldPrimaryTerm = indexShard.getPendingPrimaryTerm() - 1;
10121012
randomReplicaOperationPermitAcquisition(indexShard, oldPrimaryTerm, indexShard.getGlobalCheckpoint(),
10131013
randomNonNegativeLong(), onLockAcquired, "");
10141014
latch.await();
@@ -1030,7 +1030,7 @@ public void testAcquireReplicaPermitAdvanceMaxSeqNoOfUpdates() throws Exception
10301030

10311031
long newMaxSeqNoOfUpdates = randomLongBetween(SequenceNumbers.NO_OPS_PERFORMED, Long.MAX_VALUE);
10321032
PlainActionFuture<Releasable> fut = new PlainActionFuture<>();
1033-
randomReplicaOperationPermitAcquisition(replica, replica.operationPrimaryTerm, replica.getGlobalCheckpoint(),
1033+
randomReplicaOperationPermitAcquisition(replica, replica.getOperationPrimaryTerm(), replica.getGlobalCheckpoint(),
10341034
newMaxSeqNoOfUpdates, fut, "");
10351035
try (Releasable ignored = fut.actionGet()) {
10361036
assertThat(replica.getMaxSeqNoOfUpdatesOrDeletes(), equalTo(Math.max(currentMaxSeqNoOfUpdates, newMaxSeqNoOfUpdates)));
@@ -1181,7 +1181,7 @@ public void testRollbackReplicaEngineOnPromotion() throws IOException, Interrupt
11811181
final Engine beforeRollbackEngine = indexShard.getEngine();
11821182
final long newMaxSeqNoOfUpdates = randomLongBetween(indexShard.getMaxSeqNoOfUpdatesOrDeletes(), Long.MAX_VALUE);
11831183
randomReplicaOperationPermitAcquisition(indexShard,
1184-
indexShard.pendingPrimaryTerm + 1,
1184+
indexShard.getPendingPrimaryTerm() + 1,
11851185
globalCheckpoint,
11861186
newMaxSeqNoOfUpdates,
11871187
new ActionListener<Releasable>() {
@@ -2105,10 +2105,6 @@ public void testRecoverFromStoreRemoveStaleOperations() throws Exception {
21052105
new SourceToParse(indexName, "_doc", "doc-1", new BytesArray("{}"), XContentType.JSON));
21062106
flushShard(shard);
21072107
assertThat(getShardDocUIDs(shard), containsInAnyOrder("doc-0", "doc-1"));
2108-
// Here we try to increase term (i.e. a new primary is promoted) without rolling back a replica so we can keep stale operations
2109-
// in the index commit; then verify that a recovery from store (started with the safe commit) will remove all stale operations.
2110-
shard.pendingPrimaryTerm++;
2111-
shard.operationPrimaryTerm++;
21122108
shard.getEngine().rollTranslogGeneration();
21132109
shard.markSeqNoAsNoop(1, "test");
21142110
shard.applyIndexOperationOnReplica(2, 1, IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, false,
@@ -2118,11 +2114,20 @@ public void testRecoverFromStoreRemoveStaleOperations() throws Exception {
21182114
closeShard(shard, false);
21192115
// Recovering from store should discard doc #1
21202116
final ShardRouting replicaRouting = shard.routingEntry();
2121-
IndexShard newShard = reinitShard(shard,
2122-
newShardRouting(replicaRouting.shardId(), replicaRouting.currentNodeId(), true, ShardRoutingState.INITIALIZING,
2123-
RecoverySource.ExistingStoreRecoverySource.INSTANCE));
2124-
newShard.pendingPrimaryTerm++;
2125-
newShard.operationPrimaryTerm++;
2117+
final IndexMetaData newShardIndexMetadata = IndexMetaData.builder(shard.indexSettings().getIndexMetaData())
2118+
.primaryTerm(replicaRouting.shardId().id(), shard.getOperationPrimaryTerm() + 1)
2119+
.build();
2120+
closeShards(shard);
2121+
IndexShard newShard = newShard(
2122+
newShardRouting(replicaRouting.shardId(), replicaRouting.currentNodeId(), true, ShardRoutingState.INITIALIZING,
2123+
RecoverySource.ExistingStoreRecoverySource.INSTANCE),
2124+
shard.shardPath(),
2125+
newShardIndexMetadata,
2126+
null,
2127+
null,
2128+
shard.getEngineFactory(),
2129+
shard.getGlobalCheckpointSyncer(),
2130+
EMPTY_EVENT_LISTENER);
21262131
DiscoveryNode localNode = new DiscoveryNode("foo", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
21272132
newShard.markAsRecovering("store", new RecoveryState(newShard.routingEntry(), localNode, null));
21282133
assertTrue(newShard.recoverFromStore());

server/src/test/java/org/elasticsearch/index/shard/ShardGetServiceTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void testGetForUpdate() throws IOException {
8383
assertTrue(testGet1.getFields().containsKey(RoutingFieldMapper.NAME));
8484
assertEquals("foobar", testGet1.getFields().get(RoutingFieldMapper.NAME).getValue());
8585

86-
final long primaryTerm = primary.operationPrimaryTerm;
86+
final long primaryTerm = primary.getOperationPrimaryTerm();
8787
testGet1 = primary.getService().getForUpdate("test", "1", MATCH_ANY, VersionType.INTERNAL, test2.getSeqNo(), primaryTerm);
8888
assertEquals(new String(testGet1.source(), StandardCharsets.UTF_8), "{\"foo\" : \"baz\"}");
8989

0 commit comments

Comments
 (0)