/
RequestController.java
478 lines (422 loc) · 16.6 KB
/
RequestController.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.wildfly.extension.requestcontroller;
import org.jboss.as.server.suspend.CountingRequestCountCallback;
import org.jboss.as.server.suspend.ServerActivity;
import org.jboss.as.server.suspend.ServerActivityCallback;
import org.jboss.as.server.suspend.SuspendController;
import org.jboss.msc.service.Service;
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;
import org.wildfly.extension.requestcontroller.logging.RequestControllerLogger;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
/**
* A controller that manages the active requests that are running in the container.
* <p/>
* There are two main use cases for this:
* <p/>
* 1) Graceful shutdown - When the number of active request reaches zero then the container can be gracefully shut down
* 2) Request limiting - This allows the total number of requests that are active to be limited.
* <p/>
*
* @author Stuart Douglas
*/
public class RequestController implements Service<RequestController>, ServerActivity {
public static final ServiceName SERVICE_NAME = ServiceName.JBOSS.append("server", "global-request-controller");
private static final AtomicIntegerFieldUpdater<RequestController> activeRequestCountUpdater = AtomicIntegerFieldUpdater.newUpdater(RequestController.class, "activeRequestCount");
private static final AtomicReferenceFieldUpdater<RequestController, ServerActivityCallback> listenerUpdater = AtomicReferenceFieldUpdater.newUpdater(RequestController.class, ServerActivityCallback.class, "listener");
private volatile int maxRequestCount = -1;
private volatile int activeRequestCount = 0;
private volatile boolean paused = false;
private final Map<ControlPointIdentifier, ControlPoint> entryPoints = new HashMap<>();
private final InjectedValue<SuspendController> shutdownControllerInjectedValue = new InjectedValue<>();
@SuppressWarnings("unused")
private volatile ServerActivityCallback listener = null;
@Override
public void preSuspend(ServerActivityCallback listener) {
listener.done();
}
private Timer timer;
private final Deque<QueuedTask> taskQueue = new LinkedBlockingDeque<>();
/**
* Pause the controller. All existing requests will have a chance to finish, and once all requests are
* finished the provided listener will be invoked.
* <p/>
* While the container is paused no new requests will be accepted.
*
* @param requestCountListener The listener that will be notified when all requests are done
*/
public synchronized void suspended(ServerActivityCallback requestCountListener) {
this.paused = true;
listenerUpdater.set(this, requestCountListener);
if (activeRequestCountUpdater.get(this) == 0) {
if (listenerUpdater.compareAndSet(this, requestCountListener, null)) {
requestCountListener.done();
}
}
}
/**
* Unpause the server, allowing it to resume normal operations
*/
@Override
public synchronized void resume() {
this.paused = false;
ServerActivityCallback listener = listenerUpdater.get(this);
if (listener != null) {
listenerUpdater.compareAndSet(this, listener, null);
}
while (!taskQueue.isEmpty() && (activeRequestCount < maxRequestCount || maxRequestCount < 0)) {
runQueuedTask(false);
}
}
/**
* Pauses a given deployment
*
* @param deployment The deployment to pause
* @param listener The listener that will be notified when the pause is complete
*/
public synchronized void pauseDeployment(final String deployment, ServerActivityCallback listener) {
final List<ControlPoint> eps = new ArrayList<ControlPoint>();
for (ControlPoint ep : entryPoints.values()) {
if (ep.getDeployment().equals(deployment)) {
if(!ep.isPaused()) {
eps.add(ep);
}
}
}
CountingRequestCountCallback realListener = new CountingRequestCountCallback(eps.size(), listener);
for (ControlPoint ep : eps) {
ep.pause(realListener);
}
}
/**
* resumed a given deployment
*
* @param deployment The deployment to resume
*/
public synchronized void resumeDeployment(final String deployment) {
for (ControlPoint ep : entryPoints.values()) {
if (ep.getDeployment().equals(deployment)) {
ep.resume();
}
}
}
/**
* Pauses a given entry point. This can be used to stop all requests though a given mechanism, e.g. all web requests
*
* @param controlPoint The control point
* @param listener The listener
*/
public synchronized void pauseControlPoint(final String controlPoint, ServerActivityCallback listener) {
final List<ControlPoint> eps = new ArrayList<ControlPoint>();
for (ControlPoint ep : entryPoints.values()) {
if (ep.getEntryPoint().equals(controlPoint)) {
if(!ep.isPaused()) {
eps.add(ep);
}
}
}
if(eps.isEmpty()) {
if(listener != null) {
listener.done();
}
}
CountingRequestCountCallback realListener = new CountingRequestCountCallback(eps.size(), listener);
for (ControlPoint ep : eps) {
ep.pause(realListener);
}
}
/**
* Resumes a given entry point type;
*
* @param entryPoint The entry point
*/
public synchronized void resumeControlPoint(final String entryPoint) {
for (ControlPoint ep : entryPoints.values()) {
if (ep.getEntryPoint().equals(entryPoint)) {
ep.resume();
}
}
}
public synchronized RequestControllerState getState() {
final List<RequestControllerState.EntryPointState> eps = new ArrayList<>();
for (ControlPoint controlPoint : entryPoints.values()) {
eps.add(new RequestControllerState.EntryPointState(controlPoint.getDeployment(), controlPoint.getEntryPoint(), controlPoint.isPaused(), controlPoint.getActiveRequestCount()));
}
return new RequestControllerState(paused, activeRequestCount, maxRequestCount, eps);
}
RunResult beginRequest() {
int maxRequests = maxRequestCount;
int active = activeRequestCountUpdater.get(this);
boolean success = false;
while ((maxRequests <= 0 || active < maxRequests) && !paused) {
if (activeRequestCountUpdater.compareAndSet(this, active, active + 1)) {
success = true;
break;
}
active = activeRequestCountUpdater.get(this);
}
if (success) {
//re-check the paused state
//this is necessary because there is a race between checking paused and updating active requests
//if this happens we just call requestComplete(), as the listener can only be invoked once it does not
//matter if it has already been invoked
if(paused) {
requestComplete();
return RunResult.REJECTED;
}
return RunResult.RUN;
} else {
return RunResult.REJECTED;
}
}
void requestComplete() {
runQueuedTask(true);
}
private void decrementRequestCount() {
int result = activeRequestCountUpdater.decrementAndGet(this);
if (paused) {
if (paused && result == 0) {
ServerActivityCallback listener = listenerUpdater.get(this);
if (listener != null) {
if (listenerUpdater.compareAndSet(this, listener, null)) {
listener.done();
}
}
}
}
}
/**
* Gets an entry point for the given deployment. If one does not exist it will be created. If the request controller is disabled
* this will return null.
*
* Entry points are reference counted. If this method is called n times then {@link #removeControlPoint(ControlPoint)}
* must also be called n times to clean up the entry points.
*
* @param deploymentName The top level deployment name
* @param entryPointName The entry point name
* @return The entry point, or null if the request controller is disabled
*/
public synchronized ControlPoint getControlPoint(final String deploymentName, final String entryPointName) {
ControlPointIdentifier id = new ControlPointIdentifier(deploymentName, entryPointName);
ControlPoint ep = entryPoints.get(id);
if (ep == null) {
ep = new ControlPoint(this, deploymentName, entryPointName);
entryPoints.put(id, ep);
}
ep.increaseReferenceCount();
return ep;
}
/**
* Removes the specified entry point
*
* @param controlPoint The entry point
*/
public synchronized void removeControlPoint(ControlPoint controlPoint) {
if (controlPoint.decreaseReferenceCount() == 0) {
ControlPointIdentifier id = new ControlPointIdentifier(controlPoint.getDeployment(), controlPoint.getEntryPoint());
entryPoints.remove(id);
}
}
/**
* @return The maximum number of requests that can be active at a time
*/
public int getMaxRequestCount() {
return maxRequestCount;
}
/**
* Sets the maximum number of requests that can be active at a time.
* <p/>
* If this is higher that the number of currently running requests the no new requests
* will be able to run until the number of active requests has dropped below this level.
*
* @param maxRequestCount The max request count
*/
public void setMaxRequestCount(int maxRequestCount) {
this.maxRequestCount = maxRequestCount;
while (!taskQueue.isEmpty() && (activeRequestCount < maxRequestCount || maxRequestCount < 0)) {
runQueuedTask(false);
}
}
/**
* @return <code>true</code> If the server is currently pause
*/
public boolean isPaused() {
return paused;
}
@Override
public void start(StartContext startContext) throws StartException {
shutdownControllerInjectedValue.getValue().registerActivity(this);
timer = new Timer();
}
@Override
public void stop(StopContext stopContext) {
shutdownControllerInjectedValue.getValue().unRegisterActivity(this);
timer.cancel();
timer = null;
while (!taskQueue.isEmpty()) {
QueuedTask t = taskQueue.poll();
if(t != null) {
t.run();
}
}
}
@Override
public RequestController getValue() throws IllegalStateException, IllegalArgumentException {
return this;
}
public InjectedValue<SuspendController> getShutdownControllerInjectedValue() {
return shutdownControllerInjectedValue;
}
public int getActiveRequestCount() {
return activeRequestCount;
}
void queueTask(ControlPoint controlPoint, Runnable task, Executor taskExecutor, long timeout, Runnable timeoutTask, boolean rejectOnSuspend) {
if(paused) {
if(rejectOnSuspend) {
taskExecutor.execute(timeoutTask);
return;
}
}
QueuedTask queuedTask = new QueuedTask(taskExecutor, task, timeoutTask, controlPoint);
taskQueue.add(queuedTask);
runQueuedTask(false);
if(queuedTask.isQueued()) {
if(timeout > 0) {
timer.schedule(queuedTask, timeout);
}
}
}
/**
* Runs a queued task, if the queue is not already empty.
*
* Note that this will decrement the request count if there are no queued tasks to be run
*
* @param hasPermit If the caller has already called {@link #beginRequest()}
*/
private void runQueuedTask(boolean hasPermit) {
if(!hasPermit) {
if(beginRequest() == RunResult.REJECTED) {
return;
}
}
QueuedTask task = taskQueue.poll();
if(task != null) {
if(!task.runRequest()) {
decrementRequestCount();
}
} else {
decrementRequestCount();
}
}
private static final class ControlPointIdentifier {
private final String deployment, name;
private ControlPointIdentifier(String deployment, String name) {
this.deployment = deployment;
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ControlPointIdentifier that = (ControlPointIdentifier) o;
if (deployment != null ? !deployment.equals(that.deployment) : that.deployment != null) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
return true;
}
@Override
public int hashCode() {
int result = deployment != null ? deployment.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
@Override
public String toString() {
return super.toString();
}
}
private static final class QueuedTask extends TimerTask {
private final Executor executor;
private final Runnable task;
private final Runnable cancelTask;
private final ControlPoint controlPoint;
//0 == queued
//1 == run
//2 == cancelled
private final AtomicInteger state = new AtomicInteger(0);
private QueuedTask(Executor executor, Runnable task, Runnable cancelTask, ControlPoint controlPoint) {
this.executor = executor;
this.task = task;
this.cancelTask = cancelTask;
this.controlPoint = controlPoint;
}
@Override
public void run() {
if(state.compareAndSet(0, 2)) {
if(cancelTask != null) {
try {
executor.execute(cancelTask);
} catch (Exception e) {
//should only happen if the server is shutting down
RequestControllerLogger.ROOT_LOGGER.failedToCancelTask(cancelTask, e);
}
}
}
}
public boolean runRequest() {
if(state.compareAndSet(0, 1)) {
cancel();
executor.execute(new Runnable() {
@Override
public void run() {
try {
controlPoint.beginExistingRequest();
task.run();
} finally {
controlPoint.requestComplete();
}
}
});
return true;
} else {
return false;
}
}
boolean isQueued() {
return state.get() == 0;
}
}
}