@@ -202,7 +202,6 @@ public class IndexShard extends AbstractIndexShardComponent implements IndicesCl
202
202
protected volatile ShardRouting shardRouting ;
203
203
protected volatile IndexShardState state ;
204
204
private volatile long pendingPrimaryTerm ; // see JavaDocs for getPendingPrimaryTerm
205
- private volatile long operationPrimaryTerm ;
206
205
protected final AtomicReference <Engine > currentEngineReference = new AtomicReference <>();
207
206
final EngineFactory engineFactory ;
208
207
@@ -310,17 +309,21 @@ public IndexShard(
310
309
}
311
310
this .translogConfig = new TranslogConfig (shardId , shardPath ().resolveTranslog (), indexSettings , bigArrays );
312
311
final String aId = shardRouting .allocationId ().getId ();
312
+ final long primaryTerm = indexSettings .getIndexMetaData ().primaryTerm (shardId .id ());
313
+ this .pendingPrimaryTerm = primaryTerm ;
313
314
this .globalCheckpointListeners =
314
315
new GlobalCheckpointListeners (shardId , threadPool .executor (ThreadPool .Names .LISTENER ), threadPool .scheduler (), logger );
315
- this . replicationTracker =
316
+ final ReplicationTracker replicationTracker =
316
317
new ReplicationTracker (
317
318
shardId ,
318
319
aId ,
319
320
indexSettings ,
321
+ primaryTerm ,
320
322
UNASSIGNED_SEQ_NO ,
321
323
globalCheckpointListeners ::globalCheckpointUpdated ,
322
324
threadPool ::absoluteTimeInMillis ,
323
325
retentionLeaseSyncer );
326
+ this .replicationTracker = replicationTracker ;
324
327
325
328
// the query cache is a node-level thing, however we want the most popular filters
326
329
// to be computed on a per-shard basis
@@ -340,8 +343,6 @@ public boolean shouldCache(Query query) {
340
343
}
341
344
indexShardOperationPermits = new IndexShardOperationPermits (shardId , logger , threadPool );
342
345
searcherWrapper = indexSearcherWrapper ;
343
- pendingPrimaryTerm = indexSettings .getIndexMetaData ().primaryTerm (shardId .id ());
344
- operationPrimaryTerm = pendingPrimaryTerm ;
345
346
refreshListeners = buildRefreshListeners ();
346
347
persistMetadata (path , indexSettings , shardRouting , null , logger );
347
348
}
@@ -402,7 +403,7 @@ public long getPendingPrimaryTerm() {
402
403
403
404
/** Returns the primary term that is currently being used to assign to operations */
404
405
public long getOperationPrimaryTerm () {
405
- return this . operationPrimaryTerm ;
406
+ return replicationTracker . getOperationPrimaryTerm () ;
406
407
}
407
408
408
409
/**
@@ -529,7 +530,7 @@ public void updateShardState(final ShardRouting newRouting,
529
530
assert pendingPrimaryTerm == newPrimaryTerm :
530
531
"shard term changed on primary. expected [" + newPrimaryTerm + "] but was [" + pendingPrimaryTerm + "]" +
531
532
", current routing: " + currentRouting + ", new routing: " + newRouting ;
532
- assert operationPrimaryTerm == newPrimaryTerm ;
533
+ assert getOperationPrimaryTerm () == newPrimaryTerm ;
533
534
try {
534
535
replicationTracker .activatePrimaryMode (getLocalCheckpoint ());
535
536
/*
@@ -731,23 +732,23 @@ private IndexShardState changeState(IndexShardState newState, String reason) {
731
732
public Engine .IndexResult applyIndexOperationOnPrimary (long version , VersionType versionType , SourceToParse sourceToParse ,
732
733
long ifSeqNo , long ifPrimaryTerm , long autoGeneratedTimestamp ,
733
734
boolean isRetry ) throws IOException {
734
- return applyIndexOperation (getEngine (), SequenceNumbers .UNASSIGNED_SEQ_NO , operationPrimaryTerm , version , versionType ,
735
+ return applyIndexOperation (getEngine (), SequenceNumbers .UNASSIGNED_SEQ_NO , getOperationPrimaryTerm () , version , versionType ,
735
736
ifSeqNo , ifPrimaryTerm , autoGeneratedTimestamp , isRetry , Engine .Operation .Origin .PRIMARY , sourceToParse );
736
737
}
737
738
738
739
public Engine .IndexResult applyIndexOperationOnReplica (long seqNo , long version , VersionType versionType ,
739
740
long autoGeneratedTimeStamp , boolean isRetry , SourceToParse sourceToParse )
740
741
throws IOException {
741
- return applyIndexOperation (getEngine (), seqNo , operationPrimaryTerm , version , versionType , UNASSIGNED_SEQ_NO , 0 ,
742
+ return applyIndexOperation (getEngine (), seqNo , getOperationPrimaryTerm () , version , versionType , UNASSIGNED_SEQ_NO , 0 ,
742
743
autoGeneratedTimeStamp , isRetry , Engine .Operation .Origin .REPLICA , sourceToParse );
743
744
}
744
745
745
746
private Engine .IndexResult applyIndexOperation (Engine engine , long seqNo , long opPrimaryTerm , long version , VersionType versionType ,
746
747
long ifSeqNo , long ifPrimaryTerm ,
747
748
long autoGeneratedTimeStamp , boolean isRetry , Engine .Operation .Origin origin ,
748
749
SourceToParse sourceToParse ) throws IOException {
749
- assert opPrimaryTerm <= this . operationPrimaryTerm : "op term [ " + opPrimaryTerm + " ] > shard term [" + this . operationPrimaryTerm
750
- + "]" ;
750
+ assert opPrimaryTerm <= getOperationPrimaryTerm ()
751
+ : "op term [ " + opPrimaryTerm + " ] > shard term [" + getOperationPrimaryTerm () + "]" ;
751
752
assert versionType .validateVersionForWrites (version );
752
753
ensureWriteAllowed (origin );
753
754
Engine .Index operation ;
@@ -811,13 +812,13 @@ private Engine.IndexResult index(Engine engine, Engine.Index index) throws IOExc
811
812
}
812
813
813
814
public Engine .NoOpResult markSeqNoAsNoop (long seqNo , String reason ) throws IOException {
814
- return markSeqNoAsNoop (getEngine (), seqNo , operationPrimaryTerm , reason , Engine .Operation .Origin .REPLICA );
815
+ return markSeqNoAsNoop (getEngine (), seqNo , getOperationPrimaryTerm () , reason , Engine .Operation .Origin .REPLICA );
815
816
}
816
817
817
818
private Engine .NoOpResult markSeqNoAsNoop (Engine engine , long seqNo , long opPrimaryTerm , String reason ,
818
819
Engine .Operation .Origin origin ) throws IOException {
819
- assert opPrimaryTerm <= this . operationPrimaryTerm : "op term [ " + opPrimaryTerm + " ] > shard term [" + this . operationPrimaryTerm
820
- + "]" ;
820
+ assert opPrimaryTerm <= getOperationPrimaryTerm ()
821
+ : "op term [ " + opPrimaryTerm + " ] > shard term [" + getOperationPrimaryTerm () + "]" ;
821
822
long startTime = System .nanoTime ();
822
823
ensureWriteAllowed (origin );
823
824
final Engine .NoOp noOp = new Engine .NoOp (seqNo , opPrimaryTerm , origin , startTime , reason );
@@ -833,31 +834,33 @@ private Engine.NoOpResult noOp(Engine engine, Engine.NoOp noOp) {
833
834
}
834
835
835
836
public Engine .IndexResult getFailedIndexResult (Exception e , long version ) {
836
- return new Engine .IndexResult (e , version , operationPrimaryTerm );
837
+ return new Engine .IndexResult (e , version , getOperationPrimaryTerm () );
837
838
}
838
839
839
840
public Engine .DeleteResult getFailedDeleteResult (Exception e , long version ) {
840
- return new Engine .DeleteResult (e , version , operationPrimaryTerm );
841
+ return new Engine .DeleteResult (e , version , getOperationPrimaryTerm () );
841
842
}
842
843
843
844
public Engine .DeleteResult applyDeleteOperationOnPrimary (long version , String type , String id , VersionType versionType ,
844
845
long ifSeqNo , long ifPrimaryTerm )
845
846
throws IOException {
846
- return applyDeleteOperation (getEngine (), SequenceNumbers .UNASSIGNED_SEQ_NO , operationPrimaryTerm , version , type , id , versionType ,
847
- ifSeqNo , ifPrimaryTerm , Engine .Operation .Origin .PRIMARY );
847
+ return applyDeleteOperation (
848
+ getEngine (), SequenceNumbers .UNASSIGNED_SEQ_NO , getOperationPrimaryTerm (), version , type , id , versionType , ifSeqNo ,
849
+ ifPrimaryTerm , Engine .Operation .Origin .PRIMARY );
848
850
}
849
851
850
852
public Engine .DeleteResult applyDeleteOperationOnReplica (long seqNo , long version , String type , String id ,
851
853
VersionType versionType ) throws IOException {
852
- return applyDeleteOperation (getEngine (), seqNo , operationPrimaryTerm , version , type , id , versionType , UNASSIGNED_SEQ_NO , 0 ,
854
+ return applyDeleteOperation (getEngine (), seqNo , getOperationPrimaryTerm () , version , type , id , versionType , UNASSIGNED_SEQ_NO , 0 ,
853
855
Engine .Operation .Origin .REPLICA );
856
+
854
857
}
855
858
856
859
private Engine .DeleteResult applyDeleteOperation (Engine engine , long seqNo , long opPrimaryTerm , long version , String type , String id ,
857
860
VersionType versionType , long ifSeqNo , long ifPrimaryTerm ,
858
861
Engine .Operation .Origin origin ) throws IOException {
859
- assert opPrimaryTerm <= this . operationPrimaryTerm : "op term [ " + opPrimaryTerm + " ] > shard term [" + this . operationPrimaryTerm
860
- + "]" ;
862
+ assert opPrimaryTerm <= getOperationPrimaryTerm ()
863
+ : "op term [ " + opPrimaryTerm + " ] > shard term [" + getOperationPrimaryTerm () + "]" ;
861
864
assert versionType .validateVersionForWrites (version );
862
865
ensureWriteAllowed (origin );
863
866
if (indexSettings ().isSingleType ()) {
@@ -874,7 +877,7 @@ private Engine.DeleteResult applyDeleteOperation(Engine engine, long seqNo, long
874
877
return new Engine .DeleteResult (update );
875
878
}
876
879
} catch (MapperParsingException | IllegalArgumentException | TypeMissingException e ) {
877
- return new Engine .DeleteResult (e , version , operationPrimaryTerm , seqNo , false );
880
+ return new Engine .DeleteResult (e , version , getOperationPrimaryTerm () , seqNo , false );
878
881
}
879
882
}
880
883
final Term uid = extractUidForDelete (type , id );
@@ -1304,7 +1307,7 @@ public void prepareForIndexRecovery() {
1304
1307
}
1305
1308
1306
1309
public void trimOperationOfPreviousPrimaryTerms (long aboveSeqNo ) {
1307
- getEngine ().trimOperationsFromTranslog (operationPrimaryTerm , aboveSeqNo );
1310
+ getEngine ().trimOperationsFromTranslog (getOperationPrimaryTerm () , aboveSeqNo );
1308
1311
}
1309
1312
1310
1313
/**
@@ -2421,7 +2424,7 @@ private EngineConfig newEngineConfig() {
2421
2424
Collections .singletonList (refreshListeners ),
2422
2425
Collections .singletonList (new RefreshMetricUpdater (refreshMetric )),
2423
2426
indexSort , circuitBreakerService , replicationTracker , replicationTracker ::getRetentionLeases ,
2424
- () -> operationPrimaryTerm , tombstoneDocSupplier ());
2427
+ () -> getOperationPrimaryTerm () , tombstoneDocSupplier ());
2425
2428
}
2426
2429
2427
2430
/**
@@ -2501,7 +2504,7 @@ private <E extends Exception> void bumpPrimaryTerm(final long newPrimaryTerm,
2501
2504
@ Nullable ActionListener <Releasable > combineWithAction ) {
2502
2505
assert Thread .holdsLock (mutex );
2503
2506
assert newPrimaryTerm > pendingPrimaryTerm || (newPrimaryTerm >= pendingPrimaryTerm && combineWithAction != null );
2504
- assert operationPrimaryTerm <= pendingPrimaryTerm ;
2507
+ assert getOperationPrimaryTerm () <= pendingPrimaryTerm ;
2505
2508
final CountDownLatch termUpdated = new CountDownLatch (1 );
2506
2509
asyncBlockOperations (new ActionListener <Releasable >() {
2507
2510
@ Override
@@ -2527,12 +2530,12 @@ private void innerFail(final Exception e) {
2527
2530
public void onResponse (final Releasable releasable ) {
2528
2531
final RunOnce releaseOnce = new RunOnce (releasable ::close );
2529
2532
try {
2530
- assert operationPrimaryTerm <= pendingPrimaryTerm ;
2533
+ assert getOperationPrimaryTerm () <= pendingPrimaryTerm ;
2531
2534
termUpdated .await ();
2532
2535
// indexShardOperationPermits doesn't guarantee that async submissions are executed
2533
2536
// in the order submitted. We need to guard against another term bump
2534
- if (operationPrimaryTerm < newPrimaryTerm ) {
2535
- operationPrimaryTerm = newPrimaryTerm ;
2537
+ if (getOperationPrimaryTerm () < newPrimaryTerm ) {
2538
+ replicationTracker . setOperationPrimaryTerm ( newPrimaryTerm ) ;
2536
2539
onBlocked .run ();
2537
2540
}
2538
2541
} catch (final Exception e ) {
@@ -2618,14 +2621,14 @@ private void innerAcquireReplicaOperationPermit(final long opPrimaryTerm,
2618
2621
final ActionListener <Releasable > operationListener = new ActionListener <Releasable >() {
2619
2622
@ Override
2620
2623
public void onResponse (final Releasable releasable ) {
2621
- if (opPrimaryTerm < operationPrimaryTerm ) {
2624
+ if (opPrimaryTerm < getOperationPrimaryTerm () ) {
2622
2625
releasable .close ();
2623
2626
final String message = String .format (
2624
2627
Locale .ROOT ,
2625
2628
"%s operation primary term [%d] is too old (current [%d])" ,
2626
2629
shardId ,
2627
2630
opPrimaryTerm ,
2628
- operationPrimaryTerm );
2631
+ getOperationPrimaryTerm () );
2629
2632
onPermitAcquired .onFailure (new IllegalStateException (message ));
2630
2633
} else {
2631
2634
assert assertReplicationTarget ();
@@ -2686,7 +2689,7 @@ public void onFailure(final Exception e) {
2686
2689
}
2687
2690
2688
2691
private boolean requirePrimaryTermUpdate (final long opPrimaryTerm , final boolean allPermits ) {
2689
- return (opPrimaryTerm > pendingPrimaryTerm ) || (allPermits && opPrimaryTerm > operationPrimaryTerm );
2692
+ return (opPrimaryTerm > pendingPrimaryTerm ) || (allPermits && opPrimaryTerm > getOperationPrimaryTerm () );
2690
2693
}
2691
2694
2692
2695
public int getActiveOperationsCount () {
0 commit comments