-
Notifications
You must be signed in to change notification settings - Fork 242
/
BatchingSharedElasticsearchWorkOrchestrator.java
252 lines (231 loc) · 7.95 KB
/
BatchingSharedElasticsearchWorkOrchestrator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.elasticsearch.processor.impl;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.Phaser;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.hibernate.search.elasticsearch.logging.impl.Log;
import org.hibernate.search.elasticsearch.work.impl.ElasticsearchWork;
import org.hibernate.search.exception.ErrorHandler;
import org.hibernate.search.util.impl.Closer;
import org.hibernate.search.util.impl.Executors;
import org.hibernate.search.util.impl.Futures;
import org.hibernate.search.util.logging.impl.LoggerFactory;
/**
* An orchestrator sharing context across multiple threads,
* allowing to batch together changesets from different threads,
* and thus to produce bigger bulk works.
* <p>
* More precisely, the submitted works are sent to a queue which is processed periodically
* in a separate thread.
* This allows to process more works when orchestrating, which allows to use bulk works
* more extensively.
*
* @author Yoann Rodiere
*/
class BatchingSharedElasticsearchWorkOrchestrator implements BarrierElasticsearchWorkOrchestrator, AutoCloseable {
private static final Log LOG = LoggerFactory.make( Log.class );
private final FlushableElasticsearchWorkOrchestrator delegate;
private final ErrorHandler errorHandler;
private final ScheduledExecutorService scheduler;
private final BlockingDeque<Changeset> changesetQueue;
private final AtomicBoolean processingScheduled;
private final Phaser phaser = new Phaser() {
@Override
protected boolean onAdvance(int phase, int registeredParties) {
// This phaser never terminates on its own, allowing re-use
return false;
}
};
public BatchingSharedElasticsearchWorkOrchestrator(String name,
FlushableElasticsearchWorkOrchestrator delegate,
ErrorHandler errorHandler) {
this.delegate = delegate;
this.errorHandler = errorHandler;
changesetQueue = new LinkedBlockingDeque<>();
scheduler = Executors.newScheduledThreadPool( name );
processingScheduled = new AtomicBoolean( false );
}
@Override
public CompletableFuture<Void> submit(Iterable<ElasticsearchWork<?>> works) {
CompletableFuture<Void> future = new CompletableFuture<>();
changesetQueue.add( new Changeset( works, future ) );
ensureProcessingScheduled();
return future;
}
private void ensureProcessingScheduled() {
// Set up worker if needed
if ( !processingScheduled.get() ) {
/*
* Register to the phaser exactly here:
* * registering after scheduling would mean running the risk
* of finishing the work processing before we even registered to the phaser,
* likely resulting in an exception when de-registering from the phaser;
* * registering after compareAndSet would mean running the risk
* of another thread calling this method just after we called compareAndSet,
* then moving on to a call to awaitCompletion() before we had the chance to
* register to the phaser. This other thread would thus believe that the submitted
* work was executed while in fact it wasn't.
*/
phaser.register();
try {
if ( processingScheduled.compareAndSet( false, true ) ) {
try {
scheduler.schedule( this::processChangesets, 100, TimeUnit.MILLISECONDS );
}
catch (Throwable e) {
/*
* Make sure a failure to schedule the processing
* doesn't leave other threads waiting indefinitely
*/
try {
processingScheduled.set( false );
}
catch (Throwable e2) {
e.addSuppressed( e2 );
}
throw e;
}
}
else {
/*
* Corner case: another thread scheduled processing
* just after we registered the phaser.
* Cancel our own registration.
*/
phaser.arriveAndDeregister();
}
}
catch (Throwable e) {
/*
* Make sure a failure to schedule the processing
* doesn't leave other threads waiting indefinitely
*/
try {
phaser.arriveAndDeregister();
}
catch (Throwable e2) {
e.addSuppressed( e2 );
}
throw e;
}
}
}
@Override
public void awaitCompletion() throws InterruptedException {
int phaseBeforeUnarrivedPartiesCheck = phaser.getPhase();
if ( phaser.getUnarrivedParties() > 0 ) {
phaser.awaitAdvanceInterruptibly( phaseBeforeUnarrivedPartiesCheck );
}
}
@Override
public void close() {
try ( Closer<RuntimeException> closer = new Closer<>() ) {
closer.push( () -> {
scheduler.shutdown();
try {
awaitCompletion();
}
catch (InterruptedException e) {
LOG.interruptedWhileWaitingForIndexActivity( e );
Thread.currentThread().interrupt();
}
} );
//It's possible that a task was successfully scheduled but had no chance to run,
//so we need to release waiting threads:
closer.push( phaser::forceTermination );
}
}
/**
* Takes changesets from the queue and processes them.
*/
private void processChangesets() {
try {
CompletableFuture<?> future;
try {
synchronized ( delegate ) {
delegate.reset();
for ( Changeset changeset = changesetQueue.poll();
changeset != null;
changeset = changesetQueue.poll() ) {
try {
delegate.submit( changeset.works )
.whenComplete( Futures.copyHandler( changeset.future ) );
}
catch (Throwable e) {
changeset.future.completeExceptionally( e );
throw e;
}
}
// Nothing more to do, flush and terminate
future = delegate.flush();
}
}
finally {
try {
/*
* Allow processing to be scheduled immediately,
* even if we didn't finish executing yet (see the join below).
* This won't lead to concurrent processing,
* since there's only one thread in the pool,
* but it will make sure the processing delay runs from one
* queue drain to the next, instead of from one batch to
* the next, allowing higher throughput when batches take more
* than 100ms to process.
*/
processingScheduled.set( false );
/*
* Just in case changesets were added to the queue between
* when we last checked the queue and the resetting of
* processingScheduled above.
* This must be executed before we arrive at the phaser to ensure that
* threads calling submit(), then awaitCompletion() will not be unblocked
* before we called ensureProcessingScheduled() below.
*/
if ( !changesetQueue.isEmpty() ) {
ensureProcessingScheduled();
}
}
catch (Throwable e) {
errorHandler.handleException(
"Error while ensuring the next submitted asynchronous Elasticsearch works will be processed",
e );
}
}
// Note: timeout is handled by the client, so this "join" will not last forever
future.join();
}
catch (Throwable e) {
errorHandler.handleException( "Error while processing Elasticsearch works", e );
}
finally {
/*
* Regardless of the outcome (exception or not),
* arrive at the phaser after all the works completed.
* Note that all works have a timeout, so this will be executed eventually.
*
* Also note this must be executed *after* the finally block above,
* so we are sure we won't arrive at the phaser before ensuring we're not
* in a situation where no processing is scheduled even though
* the queue is not empty.
*/
phaser.arriveAndDeregister();
}
}
private static class Changeset {
private final Iterable<ElasticsearchWork<?>> works;
private final CompletableFuture<Void> future;
public Changeset(Iterable<ElasticsearchWork<?>> works, CompletableFuture<Void> future) {
this.works = works;
this.future = future;
}
}
}