Skip to content

Fix subscription replay after leader failover - #19535

Open
Caideyipi wants to merge 3 commits into
apache:masterfrom
Caideyipi:fix/subscription-leader-failover-progress
Open

Caideyipi wants to merge 3 commits into
apache:masterfrom
Caideyipi:fix/subscription-leader-failover-progress

Conversation

@Caideyipi

Copy link
Copy Markdown
Collaborator

Summary

  • Refresh authoritative subscription progress and the replay cursor when an active region's preferred writer changes.
  • Add unit coverage and a table-cluster integration test that force-stops the region leader and verifies delivery resumes with queued and post-failover records.

Tests

  • ConsensusPrefetchingQueueSeekTest: 10 tests passed.
  • IoTDBConsensusSubscriptionLeaderFailoverTableIT#testSubscriptionContinuesAfterKill9RegionLeaderFailover: 1 test passed. Local validation temporarily enabled the subscription feature gate; that verification-only source change is not included.
  • Spotless formatting passed for the changed modules.

regionLeaders.entrySet().iterator().next();
final int targetRegionId = targetRegionLeader.getKey();
final DataNodeWrapper oldLeader = targetRegionLeader.getValue();
oldLeader.stopForcibly();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor integration-test/src/test/java/org/apache/iotdb/subscription/it/consensus/local/tablemodel/IoTDBConsensusSubscriptionLeaderFailoverTableIT.java:129 — New failover IT never reaches the preferredWriterChanged reset branch

The future leader's queue is bound with initialActive = (lastKnownPreferredWriter == myNodeId), i.e. false, so the first state carrying preferredWriterNodeId == itself already satisfies the pre-existing !isActive term and runs the same authoritative refresh/seek. With only one leader change, the new preferredWriterChanged term is never exercised, so this IT passes with or without the queue change.

Fix: Add a second leader change (e.g. also kill the new leader, or re-elect back to the surviving replica) so an already-active queue observes a preferred-writer change, or drive applyRuntimeState twice on an active queue as the unit test does.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a second forced DataRegion leader failover. The delivery assertion now covers rows queued before that transition and rows written afterward. Spotless and the integration-test selection check pass; the targeted IT was not run locally.

@Caideyipi

Copy link
Copy Markdown
Collaborator Author

Addressed the review feedback by adding a second forced DataRegion leader failover and asserting delivery of rows queued before and written after that transition. Spotless and the integration-test selection check passed. The targeted integration test was not run locally.

&& commitManager.isConfigNodeProgressAvailable(
consumerGroupId, topicName, consensusGroupId);
final boolean requiresAuthoritativeActivation =
runtimeState.isActive() && (!isActive || preferredWriterChanged);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

major iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueue.java:6104 — preferredWriterChanged can never be true for an active queue, so failover replay is unchanged

isActive only becomes true by applying a state whose active flag is true, and all producers set active = (preferredWriterNodeId == local node id) (SetupHandler:273/1310, ConfigNodeProcedureEnv:1043), storing that same id at line 6118. Any later active state therefore already carries the stored preferredWriterNodeId, so this term never fires; the CN push after SubscriptionHandleLeaderChangeProcedure (same leader, newer runtimeVersion, fresher checkpoint) still does not refresh.

Fix: Refresh on progress instead of writer identity: force the authoritative activation when runtimeState.getRuntimeVersion() > this.runtimeVersion and ConfigNode progress is available.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for identifying the unreachable condition. The refresh trigger now performs authoritative activation when an active queue receives a strictly newer runtimeVersion and ConfigNode progress is available, even when the preferred writer ID is unchanged. The unit test now asserts this active-queue case.

ids.getDatabase(), survivingNode, targetRegionId, oldLeader);
final Set<String> rowsAfterFailover =
insertRows(newLeader, ids.getDatabase(), tableName, 3_000L, 32);
newLeader.stopForcibly();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor integration-test/src/test/java/org/apache/iotdb/subscription/it/consensus/local/tablemodel/IoTDBConsensusSubscriptionLeaderFailoverTableIT.java:142 — IT kills a leader right after ACKed inserts and relies on async WAL replication

IoTConsensusServerImpl.write returns after the local state-machine write and replicates asynchronously afterwards. rowsAfterFailover (line 141) and rowsBeforeFailover are part of expectedRows, yet stopForcibly() runs immediately after insertRows returns, so entries not yet shipped to a surviving replica are lost and the final containsAll assertion fails intermittently.

Fix: Wait until a surviving replica has applied those entries (e.g. poll its subscription progress/search index) before stopForcibly(), or flush and pause before killing.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a bounded wait before each forced stop. It compares the leader searchIndex with the per-peer currentSyncIndex for every surviving replica, so each pre-stop batch is replicated before the next failover. The targeted 1C3D integration test passes.

@Caideyipi

Copy link
Copy Markdown
Collaborator Author

Review-comment handling summary\n\n- Active queues now refresh authoritative progress for a newer runtimeVersion when ConfigNode progress is available, including when the preferred writer ID is unchanged.\n- The 1C3D failover IT enables Prometheus metrics, waits for surviving replicas to catch up before both forced stops, and verifies queued-row delivery after the second failover.\n- Verification passed: Spotless, IT selection check (710 classes), DataNode unit test (10/10), and targeted TableClusterIT (1/1). A temporary subscription-enabled source override was used only for verification and reverted; it is not in the commit.\n- Reviewed PR head: db8527a. Final local and confirmed remote head: acabee2.

&& commitManager.isConfigNodeProgressAvailable(
consumerGroupId, topicName, consensusGroupId);
final boolean requiresAuthoritativeActivation =
runtimeState.isActive() && (!isActive || shouldRefreshAuthoritativeProgress);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

major iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueue.java:6098 — Active-queue refresh rescans WAL for every region on any node status change

shouldRefreshAuthoritativeProgress fires on any newer runtimeVersion, and handleNodeStatisticsChange bumps that version for every DataRegion when any DataNode toggles mayBeOffline. Each push then runs setActive(false), a sync ConfigNode fetch and locateReplayStartForRegionProgress, which scans the WAL from Long.MIN_VALUE and rolls the WAL file, per region x topic, though nothing changed for that region.

Fix: Only tear down and re-seek when the fetched ConfigNode progress for this (group, topic, region) actually exceeds the queue's committed frontier, so unrelated node-status pushes do not deactivate and fully rescan every active queue.

@luoluoyuyu

Copy link
Copy Markdown
Member

minor iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueue.java:6103 — Active-queue refresh aborts an in-flight user seek

Because the refresh now calls setActive(false) on an already active queue, a concurrent seekToBeginning/seekToRegionProgress call has its pendingSeekRequest failed by setActiveUnderRuntimeLock and awaitCompletion rethrows it, so the client gets IllegalStateException "runtime stopped before seek" and the requested position is discarded while the queue resumes at the ConfigNode frontier.

Fix: Fail pending user seeks only when the incoming runtime state is inactive/closing; for active->active version refreshes let the explicit seek win, since seekGeneration already fences the older lookup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants