Skip to content

Fix FollowingEngine#lookupPrimaryTerm when sequence numbers are disabled#143935

Merged
tlrx merged 8 commits intoelastic:mainfrom
tlrx:2026/03/10-fix-followengine-lookupprimaryterm
Mar 12, 2026
Merged

Fix FollowingEngine#lookupPrimaryTerm when sequence numbers are disabled#143935
tlrx merged 8 commits intoelastic:mainfrom
tlrx:2026/03/10-fix-followengine-lookupprimaryterm

Conversation

@tlrx
Copy link
Copy Markdown
Member

@tlrx tlrx commented Mar 10, 2026

lookupPrimaryTerm is called by FollowingEngine when a duplicate operation is detected on the primary (i.e., an operation with a seq_no that was already processed). It retrieves the primary term of the existing operation on the follower primary shard so that TransportBulkShardOperationsAction can rewrite the duplicate with the correct term before replicating it to followers replicas, ensuring consistency between primary and replicas.

But the method currently only fetch the _seq_no indexed with SeqNoIndexOptions.POINTS_AND_DOC_VALUES option, not DOC_VALUES_ONLY.

This is now fixed.

Also, if the _seq_no cannot be fetched because it has been merged away on the follower primary, the method now returns OptionalLong.empty() as it would for duplicate that are past the global checkpoint. Since _seq_no should be retained for operations beyond the global checkpoint this is not a production issue, rather a safety net to avoid throwing the IllegalStateException.

The only test we have is FollowEnginTests.testProcessOnPrimary but making it work for the DOC_VALUES_ONLY is already a pain, even more with DISABLE_SEQUENCE_NUMBERS.

lookupPrimaryTerm is called by FollowingEngine when a duplicate
operation is detected on the primary (i.e., an operation with a seq_no
that was already processed). It retrieves the primary term of the
existing operation on the follower primary shard so that
TransportBulkShardOperationsAction can rewrite the duplicate with the
correct term before replicating it to followers replicas, ensuring
consistency between primary and replicas.

But the method currently only fetch the _seq_no indexed with
SeqNoIndexOptions.POINTS_AND_DOC_VALUES option, not DOC_VALUES_ONLY.

This is now fixed.

Also, if the _seq_no cannot be fetched because it has been merged away
on the follower primary, the method now returns `OptionalLong.empty()`
as it would for duplicate that are past the global checkpoint. Since
_seq_no should be retained for operations below the global checkpoint
this is not a production issue, rather a safety net to avoid throwing
the IllegalStateException.
@tlrx tlrx added >bug :Distributed/CCR Issues around the Cross Cluster State Replication features v9.4.0 labels Mar 10, 2026
@elasticsearchmachine elasticsearchmachine added the Team:Distributed Meta label for distributed team. label Mar 10, 2026
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-distributed (Team:Distributed)

@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Hi @tlrx, I've created a changelog YAML for you.

@tlrx tlrx requested a review from fcofdez March 10, 2026 11:02
@fcofdez
Copy link
Copy Markdown
Contributor

fcofdez commented Mar 10, 2026

Since _seq_no should be retained for operations below the global checkpoint

did you meant "beyond" instead of below? Also, this manifests in tests only, right?

@tlrx
Copy link
Copy Markdown
Member Author

tlrx commented Mar 10, 2026

did you meant "beyond" instead of below?

Yes, beyond.

Also, this manifests in tests only, right?

I think it can manifest in production too, throwing an illegal state exception on time-series/logsdb follower primary shard and therefore failing the engine.

@fcofdez
Copy link
Copy Markdown
Contributor

fcofdez commented Mar 10, 2026

I think it can manifest in production too, throwing an illegal state exception on time-series/logsdb follower primary shard and therefore failing the engine.

But in that case we shouldn't be pruning sequence numbers beyond the global checkpoint? I think that I'm missing something here

Copy link
Copy Markdown
Contributor

@fcofdez fcofdez left a comment

Choose a reason for hiding this comment

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

LGTM

@tlrx tlrx added the auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) label Mar 11, 2026
@tlrx tlrx merged commit 63c8f6a into elastic:main Mar 12, 2026
34 of 36 checks passed
@tlrx tlrx deleted the 2026/03/10-fix-followengine-lookupprimaryterm branch March 12, 2026 08:04
@tlrx
Copy link
Copy Markdown
Member Author

tlrx commented Mar 12, 2026

Thanks Francisco

michalborek pushed a commit to michalborek/elasticsearch that referenced this pull request Mar 23, 2026
…led (elastic#143935)

lookupPrimaryTerm is called by FollowingEngine when a duplicate operation is detected on the primary (i.e., an operation with a seq_no that was already processed). It retrieves the primary term of the existing operation on the follower primary shard so that TransportBulkShardOperationsAction can rewrite the duplicate with the correct term before replicating it to followers replicas, ensuring consistency between primary and replicas.

But the method currently only fetch the _seq_no indexed with SeqNoIndexOptions.POINTS_AND_DOC_VALUES option, not DOC_VALUES_ONLY.

This is now fixed.

Also, if the _seq_no cannot be fetched because it has been merged away on the follower primary, the method now returns OptionalLong.empty() as it would for duplicate that are past the global checkpoint. Since _seq_no should be retained for operations beyond the global checkpoint this is not a production issue, rather a safety net to avoid throwing the IllegalStateException.

The only test we have is FollowEnginTests.testProcessOnPrimary but making it work for the DOC_VALUES_ONLY is already a pain, even more with DISABLE_SEQUENCE_NUMBERS.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) >bug :Distributed/CCR Issues around the Cross Cluster State Replication features Team:Distributed Meta label for distributed team. v9.4.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants