-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Add interface for the Multi format merge flow #20908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mgodwan
merged 1 commit into
opensearch-project:main
from
darjisagar7:merge_asbtraction
Mar 30, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
server/src/main/java/org/opensearch/index/engine/dataformat/merge/MergeHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.index.engine.dataformat.merge; | ||
|
|
||
| import org.apache.logging.log4j.Logger; | ||
| import org.apache.logging.log4j.message.ParameterizedMessage; | ||
| import org.opensearch.common.annotation.ExperimentalApi; | ||
| import org.opensearch.common.concurrent.GatedCloseable; | ||
| import org.opensearch.common.logging.Loggers; | ||
| import org.opensearch.core.index.shard.ShardId; | ||
| import org.opensearch.index.engine.dataformat.MergeResult; | ||
| import org.opensearch.index.engine.exec.CatalogSnapshot; | ||
| import org.opensearch.index.engine.exec.Indexer; | ||
| import org.opensearch.index.engine.exec.Segment; | ||
|
|
||
| import java.util.ArrayDeque; | ||
| import java.util.Collection; | ||
| import java.util.Deque; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Abstract handler responsible for managing segment merge operations. | ||
| * <p> | ||
| * Subclasses define the merge policy by implementing {@link #findMerges()} and | ||
| * {@link #findForceMerges(int)}, while this base class manages the pending merge | ||
| * queue and lifecycle callbacks. | ||
| * | ||
| * @opensearch.experimental | ||
| */ | ||
| @ExperimentalApi | ||
| public abstract class MergeHandler { | ||
|
|
||
| private final Deque<OneMerge> mergingSegments = new ArrayDeque<>(); | ||
| private final Set<Segment> currentlyMergingSegments = new HashSet<>(); | ||
| private final Indexer indexer; | ||
| private final Logger logger; | ||
|
|
||
| public MergeHandler(Indexer indexer, ShardId shardId) { | ||
| this.logger = Loggers.getLogger(getClass(), shardId); | ||
| this.indexer = indexer; | ||
| } | ||
|
|
||
| /** | ||
| * Finds merges that should be executed based on the current segment state. | ||
| * | ||
| * @return a collection of merges to execute, or an empty collection if none are needed | ||
| */ | ||
| public abstract Collection<OneMerge> findMerges(); | ||
|
|
||
| /** | ||
| * Finds merges required to reduce the number of segments to at most {@code maxSegmentCount}. | ||
| * | ||
| * @param maxSegmentCount the maximum number of segments allowed after merging | ||
| * @return a collection of merges to execute | ||
| */ | ||
| public abstract Collection<OneMerge> findForceMerges(int maxSegmentCount); | ||
|
|
||
| /** | ||
| * Updates the set of pending merges. Called to refresh the merge queue | ||
| * when the segment state changes. | ||
| */ | ||
| public synchronized void updatePendingMerges() { | ||
| Collection<OneMerge> oneMerges = findMerges(); | ||
| for (OneMerge oneMerge : oneMerges) { | ||
| boolean isValidMerge = true; | ||
| for (Segment segment : oneMerge.getSegmentsToMerge()) { | ||
| if (currentlyMergingSegments.contains(segment)) { | ||
| isValidMerge = false; | ||
| break; | ||
| } | ||
| } | ||
| if (isValidMerge) { | ||
| registerMerge(oneMerge); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Registers a merge to be executed. | ||
| * | ||
| * @param merge the merge to register | ||
| */ | ||
| public synchronized void registerMerge(OneMerge merge) { | ||
| try (GatedCloseable<CatalogSnapshot> catalogSnapshotReleasableRef = indexer.acquireSnapshot()) { | ||
| // Validate segments exist in catalog | ||
| List<Segment> catalogSegments = catalogSnapshotReleasableRef.get().getSegments(); | ||
| for (Segment mergeSegment : merge.getSegmentsToMerge()) { | ||
| if (!catalogSegments.contains(mergeSegment)) { | ||
| return; | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| logger.warn("Failed to acquire snapshots", e); | ||
| throw new RuntimeException(e); | ||
| } | ||
| mergingSegments.add(merge); | ||
| currentlyMergingSegments.addAll(merge.getSegmentsToMerge()); | ||
| logger.debug(() -> new ParameterizedMessage("Registered merge [{}], mergingSegments: [{}]", merge, mergingSegments)); | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether there are any pending merges in the queue. | ||
| * | ||
| * @return {@code true} if there are pending merges | ||
| */ | ||
| public synchronized boolean hasPendingMerges() { | ||
| return !mergingSegments.isEmpty(); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves and removes the next pending merge from the queue. | ||
| * | ||
| * @return the next merge to execute, or {@code null} if the queue is empty | ||
| */ | ||
| public synchronized OneMerge getNextMerge() { | ||
| if (mergingSegments.isEmpty()) { | ||
| return null; | ||
| } | ||
| return mergingSegments.removeFirst(); | ||
| } | ||
|
|
||
| /** | ||
| * Callback invoked when a merge completes successfully. | ||
| * | ||
| * @param oneMerge the merge that finished | ||
| */ | ||
| public synchronized void onMergeFinished(OneMerge oneMerge) { | ||
| removeMergingSegments(oneMerge); | ||
| updatePendingMerges(); | ||
| } | ||
|
|
||
| /** | ||
| * Callback invoked when a merge fails. | ||
| * | ||
| * @param oneMerge the merge that failed | ||
| */ | ||
| public synchronized void onMergeFailure(OneMerge oneMerge) { | ||
| removeMergingSegments(oneMerge); | ||
| logger.warn(() -> new ParameterizedMessage("Merge failed for OneMerge [{}]", oneMerge)); | ||
| } | ||
|
|
||
| /** | ||
| * Executes the given merge operation. | ||
| * | ||
| * @param oneMerge the merge to execute | ||
| * @return the result of the merge | ||
| */ | ||
| public abstract MergeResult doMerge(OneMerge oneMerge); | ||
|
|
||
| private synchronized void removeMergingSegments(OneMerge oneMerge) { | ||
| mergingSegments.remove(oneMerge); | ||
| oneMerge.getSegmentsToMerge().forEach(currentlyMergingSegments::remove); | ||
| } | ||
|
|
||
| } | ||
133 changes: 133 additions & 0 deletions
133
server/src/main/java/org/opensearch/index/engine/dataformat/merge/MergeScheduler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.index.engine.dataformat.merge; | ||
|
|
||
| import org.apache.logging.log4j.Logger; | ||
| import org.apache.logging.log4j.message.ParameterizedMessage; | ||
| import org.opensearch.common.annotation.ExperimentalApi; | ||
| import org.opensearch.common.logging.Loggers; | ||
| import org.opensearch.core.index.shard.ShardId; | ||
| import org.opensearch.index.IndexSettings; | ||
| import org.opensearch.index.MergeSchedulerConfig; | ||
| import org.opensearch.index.merge.MergeStats; | ||
|
|
||
| /** | ||
| * Schedules and coordinates segment merge operations for a shard. | ||
| * <p> | ||
| * This scheduler delegates merge selection to a {@link MergeHandler} and controls | ||
| * concurrency via configurable thread and merge count limits sourced from | ||
| * {@link MergeSchedulerConfig}. | ||
| * | ||
| * @opensearch.experimental | ||
| */ | ||
| @ExperimentalApi | ||
| public class MergeScheduler { | ||
|
|
||
| private final Logger logger; | ||
| private volatile int maxConcurrentMerges; | ||
| private volatile int maxMergeCount; | ||
| private final MergeSchedulerConfig mergeSchedulerConfig; | ||
|
|
||
| /** true if we should rate-limit writes for each merge */ | ||
| private boolean doAutoIOThrottle = false; | ||
|
|
||
| /** Initial value for IO write rate limit when doAutoIOThrottle is true */ | ||
| private static final double START_MB_PER_SEC = 20.0; | ||
|
|
||
| /** Current IO writes throttle rate */ | ||
| protected double targetMBPerSec = START_MB_PER_SEC; | ||
|
|
||
| /** | ||
| * Creates a new merge scheduler. | ||
| * | ||
| * @param mergeHandler the handler that selects and executes merges | ||
| * @param shardId the shard this scheduler is associated with | ||
| * @param indexSettings the index settings providing merge scheduler configuration | ||
| */ | ||
| public MergeScheduler(MergeHandler mergeHandler, ShardId shardId, IndexSettings indexSettings) { | ||
| logger = Loggers.getLogger(getClass(), shardId); | ||
| this.mergeSchedulerConfig = indexSettings.getMergeSchedulerConfig(); | ||
| refreshConfig(); | ||
| } | ||
|
|
||
| /** | ||
| * Refreshes the max concurrent merge thread count and max merge count from | ||
| * the current {@link MergeSchedulerConfig}. No-op if the values have not changed. | ||
| */ | ||
| public synchronized void refreshConfig() { | ||
| int newMaxThreadCount = mergeSchedulerConfig.getMaxThreadCount(); | ||
| int newMaxMergeCount = mergeSchedulerConfig.getMaxMergeCount(); | ||
|
|
||
| if (newMaxThreadCount == this.maxConcurrentMerges && newMaxMergeCount == this.maxMergeCount) { | ||
| return; | ||
| } | ||
|
|
||
| logger.info( | ||
| () -> new ParameterizedMessage( | ||
| "Updating from merge scheduler config: maxThreadCount {} -> {}, " + "maxMergeCount {} -> {}", | ||
| this.maxConcurrentMerges, | ||
| newMaxThreadCount, | ||
| this.maxMergeCount, | ||
| newMaxMergeCount | ||
| ) | ||
| ); | ||
|
|
||
| this.maxConcurrentMerges = newMaxThreadCount; | ||
| this.maxMergeCount = newMaxMergeCount; | ||
| } | ||
|
|
||
| /** | ||
| * Triggers pending merge operations. Merges are selected by the | ||
| * underlying {@link MergeHandler} and executed up to the configured | ||
| * concurrency limits. | ||
| */ | ||
| public void triggerMerges() { | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Forces a merge down to at most {@code maxNumSegment} segments. | ||
| * | ||
| * @param maxNumSegment the maximum number of segments after the force merge | ||
| */ | ||
| public void forceMerge(int maxNumSegment) { | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Turn on dynamic IO throttling, to adaptively rate limit writes bytes/sec to the minimal rate | ||
| * necessary so merges do not fall behind. By default, this is disabled and writes are not | ||
| * rate-limited. | ||
| */ | ||
| public synchronized void enableAutoIOThrottle() { | ||
| doAutoIOThrottle = true; | ||
| targetMBPerSec = START_MB_PER_SEC; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the currently set per-merge IO writes rate limit, if {@link #enableAutoIOThrottle} was | ||
| * called, else {@code Double.POSITIVE_INFINITY}. | ||
| */ | ||
| public synchronized double getIORateLimitMBPerSec() { | ||
| if (doAutoIOThrottle) { | ||
| return targetMBPerSec; | ||
| } | ||
|
|
||
| return Double.POSITIVE_INFINITY; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the current merge statistics for this scheduler. | ||
| * | ||
| * @return the merge stats | ||
| */ | ||
| public MergeStats stats() { | ||
| return new MergeStats(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.