From 3a806b4b583c68c0fd6fe27e0fd967ae9aa3c634 Mon Sep 17 00:00:00 2001 From: Artem Prigoda Date: Fri, 27 Oct 2023 17:56:22 +0200 Subject: [PATCH] Add methods for adding generation listeners with primary term (#100899) Provide a facility to supply the primary term for the engine for a more precise comparison of generations. Deprecate existing `addSegmentGenerationListener` and `waitForSegmentGeneration` methods Prerequisite for #99752 --- docs/changelog/100899.yaml | 5 +++++ .../java/org/elasticsearch/index/engine/Engine.java | 12 ++++++++++++ .../org/elasticsearch/index/shard/IndexShard.java | 13 ++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 docs/changelog/100899.yaml diff --git a/docs/changelog/100899.yaml b/docs/changelog/100899.yaml new file mode 100644 index 0000000000000..988546bb22cbe --- /dev/null +++ b/docs/changelog/100899.yaml @@ -0,0 +1,5 @@ +pr: 100899 +summary: Add methods for adding generation listeners with primary term +area: Store +type: enhancement +issues: [] diff --git a/server/src/main/java/org/elasticsearch/index/engine/Engine.java b/server/src/main/java/org/elasticsearch/index/engine/Engine.java index 7f896c352d958..85ede932f231a 100644 --- a/server/src/main/java/org/elasticsearch/index/engine/Engine.java +++ b/server/src/main/java/org/elasticsearch/index/engine/Engine.java @@ -111,6 +111,7 @@ public abstract class Engine implements Closeable { public static final String SEARCH_SOURCE = "search"; // TODO: Make source of search enum? public static final String CAN_MATCH_SEARCH_SOURCE = "can_match"; protected static final String DOC_STATS_SOURCE = "doc_stats"; + public static final long UNKNOWN_PRIMARY_TERM = -1L; protected final ShardId shardId; protected final Logger logger; @@ -2114,8 +2115,19 @@ public final EngineConfig getEngineConfig() { /** * Allows registering a listener for when the index shard is on a segment generation >= minGeneration. + * + * @deprecated use {@link #addPrimaryTermAndGenerationListener(long, long, ActionListener)} instead. */ + @Deprecated public void addSegmentGenerationListener(long minGeneration, ActionListener listener) { + addPrimaryTermAndGenerationListener(UNKNOWN_PRIMARY_TERM, minGeneration, listener); + } + + /** + * Allows registering a listener for when the index shard is on a primary term >= minPrimaryTerm + * and a segment generation >= minGeneration. + */ + public void addPrimaryTermAndGenerationListener(long minPrimaryTerm, long minGeneration, ActionListener listener) { throw new UnsupportedOperationException(); } diff --git a/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java b/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java index 376724d978768..f4812f280f917 100644 --- a/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java +++ b/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java @@ -4173,7 +4173,18 @@ public String toString() { return "IndexShard(shardRouting=" + shardRouting + ")"; } + /** + * @deprecated use {@link #waitForPrimaryTermAndGeneration(long, long, ActionListener)} instead. + */ + @Deprecated public void waitForSegmentGeneration(long segmentGeneration, ActionListener listener) { - getEngine().addSegmentGenerationListener(segmentGeneration, listener); + waitForPrimaryTermAndGeneration(getOperationPrimaryTerm(), segmentGeneration, listener); + } + + /** + * Registers a listener for an event when the shard advances to the provided primary term and segment generation + */ + public void waitForPrimaryTermAndGeneration(long primaryTerm, long segmentGeneration, ActionListener listener) { + getEngine().addPrimaryTermAndGenerationListener(primaryTerm, segmentGeneration, listener); } }