You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Verify primary mode usage with assertions (#32667)
Primary terms were introduced as part of the sequence-number effort (#10708) and added in ES
5.0. Subsequent work introduced the replication tracker which lets the primary own its replication
group (#25692) to coordinate recovery and replication. The replication tracker explicitly exposes
whether it is operating in primary mode or replica mode, independent of the ShardRouting object
that's associated with a shard. During a primary relocation, for example, the primary mode is
transferred between the primary relocation source and the primary relocation target. After
transferring this so-called primary context, the old primary becomes a replication target and the
new primary the replication source, reflected in the replication tracker on both nodes. With the
most recent PR in this area (#32442), we finally have a clean transition between a shard that's
operating as a primary and issuing sequence numbers and a shard that's serving as a replication
target. The transition from one state to the other is enforced through the operation-permit system,
where we block permit acquisition during such changes and perform the transition under this
operation block, ensuring that there are no operations in progress while the transition is being
performed. This finally allows us to turn the best-effort checks that were put in place to prevent
shards from being used in the wrong way (i.e. primary as replica, or replica as primary) into hard
assertions, making it easier to catch any bugs in this area.
if (writeAllowedStates.contains(state) == false) {
1455
1455
thrownewIllegalIndexShardStateException(shardId, state, "operation only allowed when shard state is one of " + writeAllowedStates + ", origin [" + origin + "]");
1456
1456
}
1457
1457
}
1458
1458
}
1459
1459
1460
-
privatevoidverifyPrimary() {
1461
-
if (shardRouting.primary() == false) {
1462
-
thrownewIllegalStateException("shard " + shardRouting + " is not a primary");
1463
-
}
1460
+
privatebooleanassertPrimaryMode() {
1461
+
assertshardRouting.primary() && replicationTracker.isPrimaryMode() : "shard " + shardRouting + " is not a primary shard in primary mode";
1462
+
returntrue;
1464
1463
}
1465
1464
1466
-
privatevoidverifyReplicationTarget() {
1467
-
finalIndexShardStatestate = state();
1468
-
if (shardRouting.primary() && shardRouting.active() && replicationTracker.isPrimaryMode()) {
1469
-
// must use exception that is not ignored by replication logic. See TransportActions.isShardNotAvailableException
1470
-
thrownewIllegalStateException("active primary shard " + shardRouting + " cannot be a replication target before " +
1471
-
"relocation hand off, state is [" + state + "]");
1472
-
}
1465
+
privatebooleanassertReplicationTarget() {
1466
+
assertreplicationTracker.isPrimaryMode() == false : "shard " + shardRouting + " in primary mode cannot be a replication target";
0 commit comments