-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleServlet.java
448 lines (400 loc) · 15.7 KB
/
ExampleServlet.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
/* START COPY NOTICE
* MIT License
* Copyright (c) 2018 Bozeman Pass, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* END COPY NOTICE */
package com.bozemanpass.example.performance.instrumentation;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
/**
* An example to demonstrate how to use a {@link ConcurrentResourceUsageCounter}
* to track resource consumption across the lifetime of an operation in a
* multi-threaded, concurrent environment.
* <p>
* In response to each GET, the servlet discovers a random quantity
* of prime numbers. It may do this using a sieve (more memory intensive),
* by primality testing (more CPU intensive) or a mixture of both.
* <p>
* Rather than process each request linearly, new operations are placed into
* a work queue, processed by a thread pool. Random integers are tested for
* primality. After each test, if the tally is complete, the operation is
* closed and the response returned to the client, else the operation is
* placed back in the queue for more processing.
* <p>
* The operation can also be closed if any error is sent by the
* asynchronous Servlet API {@link AsyncListener#onError(AsyncEvent)}.
*/
@WebServlet(urlPatterns = "/example/*", asyncSupported = true)
public class ExampleServlet extends HttpServlet {
private final int NUM_PROCS = Runtime.getRuntime().availableProcessors();
private final ExecutorService executor = Executors.newCachedThreadPool();
/**
* Process an HTTP GET request.
* For this example, all operations begin here.
*
* @param req the HttpServletRequest
* @param resp the HttpServletResponse
* @throws ServletException
* @throws IOException
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//Switch the request to async processing.
AsyncContext ctx = req.startAsync();
//Initialize the operation.
MyOperation op = new MyOperation(ctx);
//Send it off to be worked on.
processOperation(op);
}
/**
* Submit the operation to a pool of workers to be processed.
*
* @param op MyOperation
*/
private void processOperation(MyOperation op) {
//this will submit as many jobs as we have processors (or at least 2), processing the
//operation. in a real application, we would put in safeguards to prevent creating
//too many threads, building up too long a queue of tasks, etc. but for this example,
//the goal is to get multiple threads concurrently processing each operation, which this
//accomplishes nicely
for (int i = 0; i < Math.max(2, NUM_PROCS); i++) {
executor.submit(() -> {
try {
//start the resource counter
//this is really the heart of the example
ResourceUsageCounter x = op.perf.start();
try {
//keep going until we are done or at least closed
while (!op.isClosed() && !op.isFull()) {
//could be anything, but for us, find some primes
op.doSomething();
//update the counter in the loop. we don't want
//to wait until it is all over, else the main counter
//may not have received anything from us before op.complete()
//is called by another thread.
op.perf.addLast(x.split());
}
} finally {
//halt the timer
op.perf.addLast(x.halt());
}
} catch (Throwable t) {
log("Error in worker!", t);
} finally {
//finish out the request. this is safe to call here, since one (and only one)
//thread will be granted permission to call AsyncContext.complete()
try {
op.complete();
} catch (Throwable t) {
log("Error completing operation!", t);
}
}
});
}
}
/**
* Initial servlet setup.
*
* @throws ServletException
*/
@Override
public void init() throws ServletException {
//Whether JVM measurement of CPU time and memory allocation is available or
//enabled by default is platform dependent. Though not required in most cases,
//attempting to enable it explicitly does not hurt.
try {
ResourceUsageCounter.enableMeasurementsInJVM();
} catch (Throwable t) {
log("Error enabling resource tracking in JVM!", t);
}
}
/**
* A simple class representing the operation. In this 'busy work' example, the
* operation is to discover a random quantity of prime numbers. It can
* use a sieve, primality testing on random numbers, or a mix of both.
*/
private class MyOperation implements AsyncListener, AutoCloseable {
//resource usage for the whole op
final ConcurrentResourceUsageCounter perf = new ConcurrentResourceUsageCounter();
//hits/misses and resource usage for finding primes
final PrimeStats sieveStats = new PrimeStats();
final PrimeStats trialStats = new PrimeStats();
final String id = UUID.randomUUID().toString();
final AsyncContext context;
final Set<Integer> results = Collections.synchronizedSet(new HashSet<Integer>());
final AtomicBoolean closed = new AtomicBoolean(false);
final AtomicBoolean full = new AtomicBoolean(false);
final AtomicBoolean ranSieve = new AtomicBoolean(false);
final Random random = new Random();
MyOperation(AsyncContext context) {
this.context = context;
//hook our onError, onComplete, and onTimeout handlers up to the async Servlet API
this.context.addListener(this);
}
/**
* Find some primes! Since the result set is concurrency-safe, this can be called
* by multiple threads. A more advanced implementation would split the prime-finding
* into segments, make use of wheels, etc. all allowing for better scaling. Our goal
* is neither speed nor efficiency though, we just want to use one memory-intensive
* way (the sieve) and one CPU-intensive way (the primality test) to give the counters
* something to count.
*/
void doSomething() throws Exception {
if (closed.get()) {
return;
}
//only run the sieve once. if we wanted to up the ante
//we would use the results of the first sieve for the next segment,
//but that is really beyond the scope of the example...
if (ranSieve.compareAndSet(false, true)) {
try (ResourceUsageCounter x = sieveStats.perf.auto()) {
//find some primes the sieve way (this needs a lot of memory)
int limit = random.nextInt(7 * 1024 * 1024);
List<Integer> primes = primeSieve(limit);
results.addAll(primes);
sieveStats.hits.addAndGet(primes.size());
sieveStats.misses.addAndGet(limit - primes.size());
}
} else {
try (ResourceUsageCounter x = trialStats.perf.auto()) {
//find some primes by testing random numbers (this needs a lot of CPU)
for (int candidate = random.nextInt(); ; candidate = random.nextInt()) {
if (isPrime(candidate)) {
results.add(candidate);
trialStats.hits.incrementAndGet();
break;
} else {
trialStats.misses.incrementAndGet();
}
}
}
}
if (results.size() > 300000) {
full.set(true);
}
}
/**
* Do we have all our results?
*
* @return true if we are done, else false
*/
boolean isFull() {
return full.get();
}
/**
* Have we been closed?
*
* @return true if closed, else false
*/
boolean isClosed() {
return closed.get();
}
/**
* Set status to SUCCESS and rite our primes (however many we've got) to the
* ServletResponse and close the AsyncContext.
*
* @throws IOException
*/
void complete() throws IOException {
complete(HttpServletResponse.SC_OK);
}
/**
* Set our status and write our primes (however many we've got) to the
* ServletResponse and close the AsyncContext.
*
* @param status the HTTP status code
* @throws IOException
*/
void complete(int status) throws IOException {
//we can only be closed once
if (!closed.compareAndSet(false, true)) {
return;
}
try {
HttpServletResponse response = (HttpServletResponse) context.getResponse();
if (!response.isCommitted()) {
response.setStatus(status);
}
PrintWriter w = response.getWriter();
w.write(results.toString());
w.write("\n\nTotal: [");
w.write(perf.toString());
w.write("]\n\tSieve: [");
w.write(sieveStats.toString());
w.write("]\n\tTrial: [");
w.write(trialStats.toString());
w.write("]");
} finally {
context.complete();
}
}
/**
* {@link AsyncListener#onComplete(AsyncEvent)} - called when the AsyncContext is completed.
*
* @param event
* @throws IOException
*/
@Override
public void onComplete(AsyncEvent event) throws IOException {
//this would happen if the container completed the context underneath us
if (!closed.getAndSet(true)) {
logResult("CLOSED BY CONTAINER");
return;
}
logResult("COMPLETE");
}
/**
* {@link AsyncListener#onError(AsyncEvent)} - called if there is any error
*
* @param event
* @throws IOException
*/
@Override
public void onError(AsyncEvent event) throws IOException {
//close out our part
try {
complete(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} catch (Throwable t) {
log("ERROR!", t);
}
//first log the cause
log("ERROR", event.getThrowable());
//now log us
logResult("ERROR");
}
/**
* {@link AsyncListener#onTimeout(AsyncEvent)} - called when a timeout is encountered
*
* @param event
* @throws IOException
*/
@Override
public void onTimeout(AsyncEvent event) throws IOException {
//close out our part
try {
complete(HttpServletResponse.SC_ACCEPTED);
} catch (Throwable t) {
log("ERROR!", t);
}
logResult("TIMEOUT");
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException {
//pass
}
/**
* Same as complete()
*
* @throws Exception
*/
@Override
public void close() throws Exception {
complete();
}
/**
* Log the result of our operation.
*
* @param tag prefix to our message
*/
private void logResult(String tag) {
log(String.format("%s: OP-%s :: Total: (%s)\n\tSieve: [%s]\n\tTrial: [%s]",
tag, id, perf, sieveStats, trialStats));
}
}
/**
* A class to contain some metrics about our prime finding endeavors.
*/
class PrimeStats {
final ConcurrentResourceUsageCounter perf = new ConcurrentResourceUsageCounter();
final AtomicInteger hits = new AtomicInteger();
final AtomicInteger misses = new AtomicInteger();
@Override
public String toString() {
return String.format("Hits: %d; Misses: %d :: (%s)", hits.get(), misses.get(), perf);
}
}
/**
* Find all primes <= n
*
* @param n
* @return
*/
private List<Integer> primeSieve(int n) {
boolean[] candidates = new boolean[n + 1];
int found = 0;
for (int i = 0; i < candidates.length; i++) {
if (candidates[i] = i >= 2) {
found++;
}
}
for (int i = 2; i < Math.sqrt(candidates.length); i++) {
if (candidates[i]) {
for (int j = 0; j < candidates.length; j++) {
int k = i * i + j * i;
if (k >= candidates.length) {
break;
}
if (candidates[k]) {
candidates[k] = false;
found--;
}
}
}
}
ArrayList<Integer> ret = new ArrayList<>(found);
for (int i = 0, j = 0; i < candidates.length; i++) {
if (candidates[i]) {
ret.add(i);
}
}
return ret;
}
/**
* A 6k ± 1 primality tester.
*/
private boolean isPrime(int n) {
if (n <= 1) {
return false;
} else if (n <= 3) {
return true;
} else if (0 == n % 2 || 0 == n % 3) {
return false;
}
for (int i = 5; i * i <= n; i += 6) {
if (0 == n % i || 0 == n % (i + 2)) {
return false;
}
}
return true;
}
}