-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Fixing indexing regression and bug fixes for grouping criteria #20145
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
Open
RS146BIJAY
wants to merge
1
commit into
opensearch-project:main
Choose a base branch
from
RS146BIJAY:prod
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,15 +40,13 @@ | |
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.locks.Lock; | ||
| import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.opensearch.index.BucketedCompositeDirectory.CHILD_DIRECTORY_PREFIX; | ||
|
|
||
|
|
@@ -209,12 +207,12 @@ public CriteriaBasedIndexWriterLookup getLookupMap() { | |
| * each refresh cycle. | ||
| * | ||
| */ | ||
| public static final class CriteriaBasedIndexWriterLookup implements Closeable { | ||
| public static class CriteriaBasedIndexWriterLookup implements Closeable { | ||
| private final Map<String, DisposableIndexWriter> criteriaBasedIndexWriterMap; | ||
| private final Map<BytesRef, DeleteEntry> lastDeleteEntrySet; | ||
| private final Map<BytesRef, String> criteria; | ||
| private final ReentrantReadWriteLock mapLock; | ||
| private final CriteriaBasedWriterLock mapReadLock; | ||
| CriteriaBasedWriterLock mapReadLock; | ||
| private final ReleasableLock mapWriteLock; | ||
| private final long version; | ||
| private boolean closed; | ||
|
|
@@ -300,7 +298,7 @@ public boolean isClosed() { | |
| return closed; | ||
| } | ||
|
|
||
| private static final class CriteriaBasedWriterLock implements Releasable { | ||
| static class CriteriaBasedWriterLock implements Releasable { | ||
| private final Lock lock; | ||
| // a per-thread count indicating how many times the thread has entered the lock; only works if assertions are enabled | ||
| private final ThreadLocal<Integer> holdingThreads; | ||
|
|
@@ -405,9 +403,9 @@ public Term getTerm() { | |
| * | ||
| * @opensearch.internal | ||
| */ | ||
| final static class LiveIndexWriterDeletesMap { | ||
| static class LiveIndexWriterDeletesMap { | ||
| // All writes (adds and deletes) go into here: | ||
| final CriteriaBasedIndexWriterLookup current; | ||
| CriteriaBasedIndexWriterLookup current; | ||
|
|
||
| // Used while refresh is running, and to hold adds/deletes until refresh finishes. We read from both current and old on lookup: | ||
| final CriteriaBasedIndexWriterLookup old; | ||
|
|
@@ -468,16 +466,31 @@ String getCriteriaForDoc(BytesRef key) { | |
| DisposableIndexWriter computeIndexWriterIfAbsentForCriteria( | ||
| String criteria, | ||
| CheckedBiFunction<String, CriteriaBasedIndexWriterLookup, DisposableIndexWriter, IOException> indexWriterSupplier, | ||
| ShardId shardId | ||
| ShardId shardId, | ||
| int maxRetryOnLookupMapAcquisitionException | ||
| ) { | ||
| boolean success = false; | ||
| CriteriaBasedIndexWriterLookup current = null; | ||
| try { | ||
| current = getCurrentMap(); | ||
| int counter = 0; | ||
| while ((current == null || current.isClosed()) && counter < maxRetryOnLookupMapAcquisitionException) { | ||
| // This function acquires a first read lock on a map which does not have any write lock present. Current keeps | ||
| // on getting rotated during refresh, so there will be one current on which read lock can be obtained. | ||
| // Validate that no write lock is applied on the map and the map is not closed. Idea here is write lock was | ||
| // never applied on this map as write lock gets only during closing time. We are doing this instead of acquire, | ||
| // because acquire can also apply a read lock in case refresh completed and map is closed. | ||
| current = this.current.mapReadLock.tryAcquire(); | ||
| if (current != null && current.isClosed() == true) { | ||
| current.mapReadLock.close(); | ||
| current = null; | ||
| } | ||
|
|
||
| ++counter; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (current == null || current.isClosed()) { | ||
| throw new LookupMapLockAcquisitionException(shardId, "Unable to obtain lock on the current Lookup map", null); | ||
| } | ||
|
|
||
| DisposableIndexWriter writer = current.computeIndexWriterIfAbsentForCriteria(criteria, indexWriterSupplier); | ||
| success = true; | ||
| return writer; | ||
|
|
@@ -489,15 +502,6 @@ DisposableIndexWriter computeIndexWriterIfAbsentForCriteria( | |
| } | ||
| } | ||
|
|
||
| // This function acquires a first read lock on a map which does not have any write lock present. Current keeps | ||
| // on getting rotated during refresh, so there will be one current on which read lock can be obtained. | ||
| // Validate that no write lock is applied on the map and the map is not closed. Idea here is write lock was | ||
| // never applied on this map as write lock gets only during closing time. We are doing this instead of acquire, | ||
| // because acquire can also apply a read lock in case refresh completed and map is closed. | ||
| CriteriaBasedIndexWriterLookup getCurrentMap() { | ||
| return current.mapReadLock.tryAcquire(); | ||
| } | ||
|
|
||
| // Used for Test Case. | ||
| ReleasableLock acquireCurrentWriteLock() { | ||
| return current.mapWriteLock.acquire(); | ||
|
|
@@ -672,7 +676,8 @@ DisposableIndexWriter computeIndexWriterIfAbsentForCriteria( | |
| return currentLiveIndexWriterDeletesMap.computeIndexWriterIfAbsentForCriteria( | ||
| criteria, | ||
| indexWriterSupplier, | ||
| engineConfig.getShardId() | ||
| engineConfig.getShardId(), | ||
| engineConfig.getIndexSettings().getMaxRetryOnLookupMapAcquisitionException() | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -684,12 +689,8 @@ public Map<String, DisposableIndexWriter> getMarkForRefreshIndexWriterMap() { | |
| public long getFlushingBytes() { | ||
| ensureOpen(); | ||
| long flushingBytes = 0; | ||
| Collection<IndexWriter> currentWriterSet = liveIndexWriterDeletesMap.current.criteriaBasedIndexWriterMap.values() | ||
| .stream() | ||
| .map(DisposableIndexWriter::getIndexWriter) | ||
| .collect(Collectors.toSet()); | ||
| for (IndexWriter currentWriter : currentWriterSet) { | ||
| flushingBytes += currentWriter.getFlushingBytes(); | ||
| for (DisposableIndexWriter disposableIndexWriter : liveIndexWriterDeletesMap.current.criteriaBasedIndexWriterMap.values()) { | ||
| flushingBytes += disposableIndexWriter.getIndexWriter().getFlushingBytes(); | ||
| } | ||
|
|
||
| return flushingBytes + accumulatingIndexWriter.getFlushingBytes(); | ||
|
|
@@ -699,13 +700,8 @@ public long getFlushingBytes() { | |
| public long getPendingNumDocs() { | ||
| ensureOpen(); | ||
| long pendingNumDocs = 0; | ||
| Collection<IndexWriter> currentWriterSet = liveIndexWriterDeletesMap.current.criteriaBasedIndexWriterMap.values() | ||
| .stream() | ||
| .map(DisposableIndexWriter::getIndexWriter) | ||
| .collect(Collectors.toSet()); | ||
| ; | ||
| for (IndexWriter currentWriter : currentWriterSet) { | ||
| pendingNumDocs += currentWriter.getPendingNumDocs(); | ||
| for (DisposableIndexWriter disposableIndexWriter : liveIndexWriterDeletesMap.current.criteriaBasedIndexWriterMap.values()) { | ||
| pendingNumDocs += disposableIndexWriter.getIndexWriter().getPendingNumDocs(); | ||
| } | ||
|
|
||
| // TODO: Should we add docs for old writer as well? | ||
|
|
@@ -733,24 +729,17 @@ public boolean hasUncommittedChanges() { | |
|
|
||
| @Override | ||
| public Throwable getTragicException() { | ||
| Collection<IndexWriter> currentWriterSet = liveIndexWriterDeletesMap.current.criteriaBasedIndexWriterMap.values() | ||
| .stream() | ||
| .map(DisposableIndexWriter::getIndexWriter) | ||
| .collect(Collectors.toSet()); | ||
| for (IndexWriter writer : currentWriterSet) { | ||
| if (writer.isOpen() == false && writer.getTragicException() != null) { | ||
| return writer.getTragicException(); | ||
| for (DisposableIndexWriter disposableIndexWriter : liveIndexWriterDeletesMap.current.criteriaBasedIndexWriterMap.values()) { | ||
| if (disposableIndexWriter.getIndexWriter().isOpen() == false | ||
| && disposableIndexWriter.getIndexWriter().getTragicException() != null) { | ||
| return disposableIndexWriter.getIndexWriter().getTragicException(); | ||
| } | ||
| } | ||
|
|
||
| Collection<IndexWriter> oldWriterSet = liveIndexWriterDeletesMap.old.criteriaBasedIndexWriterMap.values() | ||
| .stream() | ||
| .map(DisposableIndexWriter::getIndexWriter) | ||
| .collect(Collectors.toSet()); | ||
| ; | ||
| for (IndexWriter writer : oldWriterSet) { | ||
| if (writer.isOpen() == false && writer.getTragicException() != null) { | ||
| return writer.getTragicException(); | ||
| for (DisposableIndexWriter disposableIndexWriter : liveIndexWriterDeletesMap.old.criteriaBasedIndexWriterMap.values()) { | ||
| if (disposableIndexWriter.getIndexWriter().isOpen() == false | ||
| && disposableIndexWriter.getIndexWriter().getTragicException() != null) { | ||
| return disposableIndexWriter.getIndexWriter().getTragicException(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -765,27 +754,19 @@ public Throwable getTragicException() { | |
| public final long ramBytesUsed() { | ||
| ensureOpen(); | ||
| long ramBytesUsed = 0; | ||
| Collection<IndexWriter> currentWriterSet = liveIndexWriterDeletesMap.current.criteriaBasedIndexWriterMap.values() | ||
| .stream() | ||
| .map(DisposableIndexWriter::getIndexWriter) | ||
| .collect(Collectors.toSet()); | ||
|
|
||
| try (ReleasableLock ignore = liveIndexWriterDeletesMap.current.mapWriteLock.acquire()) { | ||
| for (IndexWriter indexWriter : currentWriterSet) { | ||
| if (indexWriter.isOpen() == true) { | ||
| ramBytesUsed += indexWriter.ramBytesUsed(); | ||
| for (DisposableIndexWriter disposableIndexWriter : liveIndexWriterDeletesMap.current.criteriaBasedIndexWriterMap.values()) { | ||
| if (disposableIndexWriter.getIndexWriter().isOpen() == true) { | ||
| ramBytesUsed += disposableIndexWriter.getIndexWriter().ramBytesUsed(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Collection<IndexWriter> oldWriterSet = liveIndexWriterDeletesMap.old.criteriaBasedIndexWriterMap.values() | ||
| .stream() | ||
| .map(DisposableIndexWriter::getIndexWriter) | ||
| .collect(Collectors.toSet()); | ||
| try (ReleasableLock ignore = liveIndexWriterDeletesMap.old.mapWriteLock.acquire()) { | ||
| for (IndexWriter indexWriter : oldWriterSet) { | ||
| if (indexWriter.isOpen() == true) { | ||
| ramBytesUsed += indexWriter.ramBytesUsed(); | ||
| for (DisposableIndexWriter disposableIndexWriter : liveIndexWriterDeletesMap.old.criteriaBasedIndexWriterMap.values()) { | ||
| if (disposableIndexWriter.getIndexWriter().isOpen() == true) { | ||
| ramBytesUsed += disposableIndexWriter.getIndexWriter().ramBytesUsed(); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -813,24 +794,15 @@ public final synchronized Iterable<Map.Entry<String, String>> getLiveCommitData( | |
|
|
||
| public void rollback() throws IOException { | ||
| if (shouldClose()) { | ||
| Collection<IndexWriter> currentWriterSet = liveIndexWriterDeletesMap.current.criteriaBasedIndexWriterMap.values() | ||
| .stream() | ||
| .map(DisposableIndexWriter::getIndexWriter) | ||
| .collect(Collectors.toSet()); | ||
|
|
||
| for (IndexWriter indexWriter : currentWriterSet) { | ||
| if (indexWriter.isOpen() == true) { | ||
| indexWriter.rollback(); | ||
| for (DisposableIndexWriter disposableIndexWriter : liveIndexWriterDeletesMap.current.criteriaBasedIndexWriterMap.values()) { | ||
| if (disposableIndexWriter.getIndexWriter().isOpen() == true) { | ||
| disposableIndexWriter.getIndexWriter().rollback(); | ||
| } | ||
| } | ||
|
|
||
| Collection<IndexWriter> oldWriterSet = liveIndexWriterDeletesMap.old.criteriaBasedIndexWriterMap.values() | ||
| .stream() | ||
| .map(DisposableIndexWriter::getIndexWriter) | ||
| .collect(Collectors.toSet()); | ||
| for (IndexWriter indexWriter : oldWriterSet) { | ||
| if (indexWriter.isOpen() == true) { | ||
| indexWriter.rollback(); | ||
| for (DisposableIndexWriter disposableIndexWriter : liveIndexWriterDeletesMap.old.criteriaBasedIndexWriterMap.values()) { | ||
| if (disposableIndexWriter.getIndexWriter().isOpen() == true) { | ||
| disposableIndexWriter.getIndexWriter().rollback(); | ||
|
Comment on lines
+803
to
+805
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might not be thread-safe for instance the index writer might be closed while we are doing a rollback? |
||
| } | ||
| } | ||
|
|
||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we move this logic centrally in
close(). Also didn't quite understand why the close operation done as a part of write lock acquisition doesn't handle this logic?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This handles the scenario where try acquire succeded in obtaining the lock on the current writer but the map itself rotated and the writer got closed. In that case, we close the old writer as we retry to obtain lock again on the current. As this ensures the lock is correctly released on the old writer before we try acquiring lock on new writer.