-
Notifications
You must be signed in to change notification settings - Fork 995
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
Reduce contention in DocumentsWriterPerThreadPool. #12199
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
482fc3c
Reduce contention in DocumentsWriterPerThreadPool.
jpountz be8b8b8
Simplify a bit and add more comments about the workings of this pool.
jpountz 92a9fcb
Better style, approved by the policeman.
jpountz a560270
More tests.
jpountz c853888
tidy
jpountz 8418029
Merge branch 'main' into reduced_contention_dwpt_pool
jpountz 3fdd42b
CHANGES
jpountz 6461538
Extract lock to local variable.
jpountz ec760be
Check if assertions are enabled.
jpountz 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 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
150 changes: 150 additions & 0 deletions
150
lucene/core/src/java/org/apache/lucene/index/ConcurrentApproximatePriorityQueue.java
This file contains 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,150 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.lucene.index; | ||
|
||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Concurrent version of {@link ApproximatePriorityQueue}, which trades a bit more of ordering for | ||
* better concurrency by maintaining 8 sub {@link ApproximatePriorityQueue}s that are locked | ||
* independently. | ||
*/ | ||
final class ConcurrentApproximatePriorityQueue<T> { | ||
|
||
/** Keeping 8 queues should already help a lot compared to a single one. */ | ||
static final int CONCURRENCY = 8; | ||
|
||
private static final int MASK = 0x07; | ||
|
||
final Lock[] locks; | ||
final ApproximatePriorityQueue<T>[] queues; | ||
|
||
ConcurrentApproximatePriorityQueue() { | ||
locks = new Lock[CONCURRENCY]; | ||
@SuppressWarnings({"rawtypes", "unchecked"}) | ||
ApproximatePriorityQueue<T>[] queues = new ApproximatePriorityQueue[CONCURRENCY]; | ||
this.queues = queues; | ||
for (int i = 0; i < CONCURRENCY; ++i) { | ||
locks[i] = new ReentrantLock(); | ||
queues[i] = new ApproximatePriorityQueue<>(); | ||
} | ||
} | ||
|
||
void add(T entry, long weight) { | ||
// Seed the order in which to look at entries based on the current thread. This helps distribute | ||
// entries across queues and gives a bit of thread affinity between entries and threads, which | ||
// can't hurt. | ||
final int threadHash = Thread.currentThread().hashCode(); | ||
for (int i = 0; i < CONCURRENCY; ++i) { | ||
final int index = (threadHash + i) & MASK; | ||
final Lock lock = locks[index]; | ||
final ApproximatePriorityQueue<T> queue = queues[index]; | ||
if (lock.tryLock()) { | ||
try { | ||
queue.add(entry, weight); | ||
return; | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
} | ||
final int index = threadHash & MASK; | ||
final Lock lock = locks[index]; | ||
final ApproximatePriorityQueue<T> queue = queues[index]; | ||
lock.lock(); | ||
try { | ||
queue.add(entry, weight); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
T poll(Predicate<T> predicate) { | ||
final int threadHash = Thread.currentThread().hashCode(); | ||
for (int i = 0; i < CONCURRENCY; ++i) { | ||
final int index = (threadHash + i) & MASK; | ||
final Lock lock = locks[index]; | ||
final ApproximatePriorityQueue<T> queue = queues[index]; | ||
if (lock.tryLock()) { | ||
try { | ||
T entry = queue.poll(predicate); | ||
if (entry != null) { | ||
return entry; | ||
} | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
} | ||
for (int i = 0; i < CONCURRENCY; ++i) { | ||
final int index = (threadHash + i) & MASK; | ||
final Lock lock = locks[index]; | ||
final ApproximatePriorityQueue<T> queue = queues[index]; | ||
lock.lock(); | ||
try { | ||
T entry = queue.poll(predicate); | ||
if (entry != null) { | ||
return entry; | ||
} | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
// Only used for assertions | ||
boolean contains(Object o) { | ||
boolean assertionsAreEnabled = false; | ||
assert assertionsAreEnabled = true; | ||
if (assertionsAreEnabled == false) { | ||
throw new AssertionError("contains should only be used for assertions"); | ||
} | ||
|
||
for (int i = 0; i < CONCURRENCY; ++i) { | ||
final Lock lock = locks[i]; | ||
final ApproximatePriorityQueue<T> queue = queues[i]; | ||
lock.lock(); | ||
try { | ||
if (queue.contains(o)) { | ||
return true; | ||
} | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
boolean remove(Object o) { | ||
for (int i = 0; i < CONCURRENCY; ++i) { | ||
final Lock lock = locks[i]; | ||
final ApproximatePriorityQueue<T> queue = queues[i]; | ||
lock.lock(); | ||
try { | ||
if (queue.remove(o)) { | ||
return true; | ||
} | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains 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
89 changes: 89 additions & 0 deletions
89
lucene/core/src/test/org/apache/lucene/index/TestConcurrentApproximatePriorityQueue.java
This file contains 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,89 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.lucene.index; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import org.apache.lucene.tests.util.LuceneTestCase; | ||
import org.apache.lucene.util.ThreadInterruptedException; | ||
|
||
public class TestConcurrentApproximatePriorityQueue extends LuceneTestCase { | ||
|
||
public void testPollFromSameThread() { | ||
ConcurrentApproximatePriorityQueue<Integer> pq = new ConcurrentApproximatePriorityQueue<>(); | ||
pq.add(3, 3); | ||
pq.add(10, 10); | ||
pq.add(7, 7); | ||
assertEquals(Integer.valueOf(10), pq.poll(x -> true)); | ||
assertEquals(Integer.valueOf(7), pq.poll(x -> true)); | ||
assertEquals(Integer.valueOf(3), pq.poll(x -> true)); | ||
assertNull(pq.poll(x -> true)); | ||
} | ||
|
||
public void testPollFromDifferentThread() throws Exception { | ||
ConcurrentApproximatePriorityQueue<Integer> pq = new ConcurrentApproximatePriorityQueue<>(); | ||
pq.add(3, 3); | ||
pq.add(10, 10); | ||
pq.add(7, 7); | ||
Thread t = | ||
new Thread() { | ||
@Override | ||
public void run() { | ||
assertEquals(Integer.valueOf(10), pq.poll(x -> true)); | ||
assertEquals(Integer.valueOf(7), pq.poll(x -> true)); | ||
assertEquals(Integer.valueOf(3), pq.poll(x -> true)); | ||
assertNull(pq.poll(x -> true)); | ||
} | ||
}; | ||
t.start(); | ||
t.join(); | ||
} | ||
|
||
public void testCurrentLockIsBusy() throws Exception { | ||
ConcurrentApproximatePriorityQueue<Integer> pq = new ConcurrentApproximatePriorityQueue<>(); | ||
pq.add(3, 3); | ||
CountDownLatch takeLock = new CountDownLatch(1); | ||
CountDownLatch releaseLock = new CountDownLatch(1); | ||
Thread t = | ||
new Thread() { | ||
@Override | ||
public void run() { | ||
int queueIndex = -1; | ||
for (int i = 0; i < pq.queues.length; ++i) { | ||
if (pq.queues[i].isEmpty() == false) { | ||
queueIndex = i; | ||
break; | ||
} | ||
} | ||
assertTrue(pq.locks[queueIndex].tryLock()); | ||
takeLock.countDown(); | ||
try { | ||
releaseLock.await(); | ||
} catch (InterruptedException e) { | ||
throw new ThreadInterruptedException(e); | ||
} | ||
pq.locks[queueIndex].unlock(); | ||
} | ||
}; | ||
t.start(); | ||
takeLock.await(); | ||
pq.add(1, 1); // The lock is taken so this needs to go to a different queue | ||
assertEquals(Integer.valueOf(1), pq.poll(x -> true)); | ||
releaseLock.countDown(); | ||
assertEquals(Integer.valueOf(3), pq.poll(x -> true)); | ||
assertNull(pq.poll(x -> true)); | ||
} | ||
} |
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.
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.
nitpick can you add a check that assertions are enabled?