Skip to content

Commit fcf8b6d

Browse files
authored
Generalize repo analysis (#93899)
In #93825 we introduced a CAS operation on snapshot repositories. In due course we'll want to verify that this operation works as expected using the repository analysis API. This commit reworks the implementation slightly so as to simplify the change which will add this functionality.
1 parent 1b068aa commit fcf8b6d

File tree

3 files changed

+341
-65
lines changed

3 files changed

+341
-65
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.common.util.concurrent;
10+
11+
import org.elasticsearch.core.AbstractRefCounted;
12+
import org.elasticsearch.core.RefCounted;
13+
import org.elasticsearch.core.Releasable;
14+
import org.elasticsearch.core.Releasables;
15+
import org.elasticsearch.core.Strings;
16+
import org.elasticsearch.logging.LogManager;
17+
import org.elasticsearch.logging.Logger;
18+
19+
import java.util.Iterator;
20+
import java.util.Objects;
21+
import java.util.concurrent.Semaphore;
22+
import java.util.function.BiConsumer;
23+
24+
public class ThrottledIterator<T> implements Releasable {
25+
26+
private static final Logger logger = LogManager.getLogger(ThrottledIterator.class);
27+
28+
/**
29+
* Iterate through the given collection, performing an operation on each item which may fork background tasks, but with a limit on the
30+
* number of such background tasks running concurrently to avoid overwhelming the rest of the system (e.g. starving other work of access
31+
* to an executor).
32+
*
33+
* @param iterator The items to iterate. May be accessed by multiple threads, but accesses are all protected by synchronizing on itself.
34+
* @param itemConsumer The operation to perform on each item. Each operation receives a {@link RefCounted} which can be used to track
35+
* the execution of any background tasks spawned for this item. This operation may run on the thread which
36+
* originally called {@link #run}, if this method has not yet returned. Otherwise it will run on a thread on which a
37+
* background task previously called {@link RefCounted#decRef()} on its ref count. This operation should not throw
38+
* any exceptions.
39+
* @param maxConcurrency The maximum number of ongoing operations at any time.
40+
* @param onItemCompletion Executed when each item is completed, which can be used for instance to report on progress. Must not throw
41+
* exceptions.
42+
* @param onCompletion Executed when all items are completed.
43+
*/
44+
public static <T> void run(
45+
Iterator<T> iterator,
46+
BiConsumer<Releasable, T> itemConsumer,
47+
int maxConcurrency,
48+
Runnable onItemCompletion,
49+
Runnable onCompletion
50+
) {
51+
try (var throttledIterator = new ThrottledIterator<>(iterator, itemConsumer, maxConcurrency, onItemCompletion, onCompletion)) {
52+
throttledIterator.run();
53+
}
54+
}
55+
56+
private final RefCounted refs; // one ref for each running item, plus one for the iterator if incomplete
57+
private final Iterator<T> iterator;
58+
private final BiConsumer<Releasable, T> itemConsumer;
59+
private final Semaphore permits;
60+
private final Runnable onItemCompletion;
61+
62+
private ThrottledIterator(
63+
Iterator<T> iterator,
64+
BiConsumer<Releasable, T> itemConsumer,
65+
int maxConcurrency,
66+
Runnable onItemCompletion,
67+
Runnable onCompletion
68+
) {
69+
this.iterator = Objects.requireNonNull(iterator);
70+
this.itemConsumer = Objects.requireNonNull(itemConsumer);
71+
if (maxConcurrency <= 0) {
72+
throw new IllegalArgumentException("maxConcurrency must be positive");
73+
}
74+
this.permits = new Semaphore(maxConcurrency);
75+
this.onItemCompletion = Objects.requireNonNull(onItemCompletion);
76+
this.refs = AbstractRefCounted.of(onCompletion);
77+
}
78+
79+
private void run() {
80+
while (permits.tryAcquire()) {
81+
final T item;
82+
synchronized (iterator) {
83+
if (iterator.hasNext()) {
84+
item = iterator.next();
85+
} else {
86+
permits.release();
87+
return;
88+
}
89+
}
90+
try (var itemRefs = new ItemRefCounted()) {
91+
itemRefs.incRef();
92+
itemConsumer.accept(Releasables.releaseOnce(itemRefs::decRef), item);
93+
} catch (Exception e) {
94+
logger.error(Strings.format("exception when processing [%s] with [%s]", item, itemConsumer), e);
95+
assert false : e;
96+
}
97+
}
98+
}
99+
100+
@Override
101+
public void close() {
102+
refs.decRef();
103+
}
104+
105+
// A RefCounted for a single item, including protection against calling back into run() if it's created and closed within a single
106+
// invocation of run().
107+
private class ItemRefCounted extends AbstractRefCounted implements Releasable {
108+
private boolean isRecursive = true;
109+
110+
ItemRefCounted() {
111+
refs.incRef();
112+
}
113+
114+
@Override
115+
protected void closeInternal() {
116+
try {
117+
onItemCompletion.run();
118+
} catch (Exception e) {
119+
logger.error("exception in onItemCompletion", e);
120+
assert false : e;
121+
} finally {
122+
permits.release();
123+
try {
124+
// Someone must now pick up the next item. Here we might be called from the run() invocation which started processing
125+
// the just-completed item (via close() -> decRef()) if that item's processing didn't fork or all its forked tasks
126+
// finished first. If so, there's no need to call run() here, we can just return and the next iteration of the run()
127+
// loop will continue the processing; moreover calling run() in this situation could lead to a stack overflow. However
128+
// if we're not within that run() invocation then ...
129+
if (isRecursive() == false) {
130+
// ... we're not within any other run() invocation either, so it's safe (and necessary) to call run() here.
131+
run();
132+
}
133+
} finally {
134+
refs.decRef();
135+
}
136+
}
137+
}
138+
139+
// Note on blocking: we call both of these synchronized methods exactly once (and must enter close() before calling isRecursive()).
140+
// If close() releases the last ref and calls closeInternal(), and hence isRecursive(), then there's no other threads involved and
141+
// hence no blocking. In contrast if close() doesn't release the last ref then it exits immediately, so the call to isRecursive()
142+
// will proceed without delay in this case too.
143+
144+
private synchronized boolean isRecursive() {
145+
return isRecursive;
146+
}
147+
148+
@Override
149+
public synchronized void close() {
150+
decRef();
151+
isRecursive = false;
152+
}
153+
}
154+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.common.util.concurrent;
10+
11+
import org.apache.lucene.tests.util.LuceneTestCase;
12+
import org.elasticsearch.action.support.RefCountingRunnable;
13+
import org.elasticsearch.common.settings.Settings;
14+
import org.elasticsearch.core.TimeValue;
15+
import org.elasticsearch.test.ESTestCase;
16+
import org.elasticsearch.threadpool.FixedExecutorBuilder;
17+
import org.elasticsearch.threadpool.ScalingExecutorBuilder;
18+
import org.elasticsearch.threadpool.TestThreadPool;
19+
import org.elasticsearch.threadpool.ThreadPool;
20+
21+
import java.util.concurrent.CountDownLatch;
22+
import java.util.concurrent.Semaphore;
23+
import java.util.concurrent.TimeUnit;
24+
import java.util.concurrent.atomic.AtomicInteger;
25+
import java.util.function.BooleanSupplier;
26+
import java.util.stream.IntStream;
27+
28+
public class ThrottledIteratorTests extends ESTestCase {
29+
private static final String CONSTRAINED = "constrained";
30+
private static final String RELAXED = "relaxed";
31+
32+
public void testConcurrency() throws InterruptedException {
33+
final var maxConstrainedThreads = between(1, 3);
34+
final var maxRelaxedThreads = between(1, 100);
35+
final var constrainedQueue = between(3, 6);
36+
final var threadPool = new TestThreadPool(
37+
"test",
38+
new FixedExecutorBuilder(Settings.EMPTY, CONSTRAINED, maxConstrainedThreads, constrainedQueue, CONSTRAINED, false),
39+
new ScalingExecutorBuilder(RELAXED, 1, maxRelaxedThreads, TimeValue.timeValueSeconds(30), true)
40+
);
41+
try {
42+
final var items = between(1, 10000); // large enough that inadvertent recursion will trigger a StackOverflowError
43+
final var itemStartLatch = new CountDownLatch(items);
44+
final var completedItems = new AtomicInteger();
45+
final var maxConcurrency = between(1, (constrainedQueue + maxConstrainedThreads) * 2);
46+
final var itemPermits = new Semaphore(maxConcurrency);
47+
final var completionLatch = new CountDownLatch(1);
48+
final BooleanSupplier forkSupplier = randomFrom(
49+
() -> false,
50+
ESTestCase::randomBoolean,
51+
LuceneTestCase::rarely,
52+
LuceneTestCase::usually,
53+
() -> true
54+
);
55+
final var blockPermits = new Semaphore(between(0, Math.min(maxRelaxedThreads, maxConcurrency) - 1));
56+
57+
ThrottledIterator.run(IntStream.range(0, items).boxed().iterator(), (releasable, item) -> {
58+
try (var refs = new RefCountingRunnable(releasable::close)) {
59+
assertTrue(itemPermits.tryAcquire());
60+
if (forkSupplier.getAsBoolean()) {
61+
var ref = refs.acquire();
62+
final var executor = randomFrom(CONSTRAINED, RELAXED);
63+
threadPool.executor(executor).execute(new AbstractRunnable() {
64+
65+
@Override
66+
public void onRejection(Exception e) {
67+
assertEquals(CONSTRAINED, executor);
68+
itemStartLatch.countDown();
69+
}
70+
71+
@Override
72+
protected void doRun() {
73+
itemStartLatch.countDown();
74+
if (RELAXED.equals(executor) && randomBoolean() && blockPermits.tryAcquire()) {
75+
// simulate at most (maxConcurrency-1) long-running operations, to demonstrate that they don't
76+
// hold up the processing of the other operations
77+
try {
78+
assertTrue(itemStartLatch.await(30, TimeUnit.SECONDS));
79+
} catch (InterruptedException e) {
80+
throw new AssertionError("unexpected", e);
81+
} finally {
82+
blockPermits.release();
83+
}
84+
}
85+
}
86+
87+
@Override
88+
public void onAfter() {
89+
itemPermits.release();
90+
ref.close();
91+
}
92+
93+
@Override
94+
public void onFailure(Exception e) {
95+
throw new AssertionError("unexpected", e);
96+
}
97+
});
98+
} else {
99+
itemStartLatch.countDown();
100+
itemPermits.release();
101+
}
102+
}
103+
}, maxConcurrency, completedItems::incrementAndGet, completionLatch::countDown);
104+
105+
assertTrue(completionLatch.await(30, TimeUnit.SECONDS));
106+
assertEquals(items, completedItems.get());
107+
assertTrue(itemPermits.tryAcquire(maxConcurrency));
108+
assertTrue(itemStartLatch.await(0, TimeUnit.SECONDS));
109+
} finally {
110+
ThreadPool.terminate(threadPool, 30, TimeUnit.SECONDS);
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)