use of java.util.concurrent.ConcurrentLinkedQueue in project jetty.project by eclipse.
the class AsyncRestServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Long start = System.nanoTime();
// Do we have results yet?
Queue<Map<String, String>> results = (Queue<Map<String, String>>) request.getAttribute(RESULTS_ATTR);
// If no results, this must be the first dispatch, so send the REST request(s)
if (results == null) {
// define results data structures
final Queue<Map<String, String>> resultsQueue = new ConcurrentLinkedQueue<>();
request.setAttribute(RESULTS_ATTR, results = resultsQueue);
// suspend the request
// This is done before scheduling async handling to avoid race of
// dispatch before startAsync!
final AsyncContext async = request.startAsync();
async.setTimeout(30000);
// extract keywords to search for
String[] keywords = sanitize(request.getParameter(ITEMS_PARAM)).split(",");
final AtomicInteger outstanding = new AtomicInteger(keywords.length);
// Send request each keyword
for (final String item : keywords) {
_client.newRequest(restURL(item)).method(HttpMethod.GET).send(new AsyncRestRequest() {
@Override
void onAuctionFound(Map<String, String> auction) {
resultsQueue.add(auction);
}
@Override
void onComplete() {
if (outstanding.decrementAndGet() <= 0)
async.dispatch();
}
});
}
// save timing info and return
request.setAttribute(START_ATTR, start);
request.setAttribute(DURATION_ATTR, System.nanoTime() - start);
return;
}
// We have results!
// Generate the response
String thumbs = generateThumbs(results);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head>");
out.println(STYLE);
out.println("</head><body><small>");
long initial = (Long) request.getAttribute(DURATION_ATTR);
long start0 = (Long) request.getAttribute(START_ATTR);
long now = System.nanoTime();
long total = now - start0;
long generate = now - start;
long thread = initial + generate;
out.print("<b>Asynchronous: " + sanitize(request.getParameter(ITEMS_PARAM)) + "</b><br/>");
out.print("Total Time: " + ms(total) + "ms<br/>");
out.print("Thread held (<span class='red'>red</span>): " + ms(thread) + "ms (" + ms(initial) + " initial + " + ms(generate) + " generate )<br/>");
out.print("Async wait (<span class='green'>green</span>): " + ms(total - thread) + "ms<br/>");
out.println("<img border='0px' src='asyncrest/red.png' height='20px' width='" + width(initial) + "px'>" + "<img border='0px' src='asyncrest/green.png' height='20px' width='" + width(total - thread) + "px'>" + "<img border='0px' src='asyncrest/red.png' height='20px' width='" + width(generate) + "px'>");
out.println("<hr />");
out.println(thumbs);
out.println("</small>");
out.println("</body></html>");
out.close();
}
use of java.util.concurrent.ConcurrentLinkedQueue in project jetty.project by eclipse.
the class BlockingArrayQueueTest method testConcurrentAccess.
@Test
@Slow
public void testConcurrentAccess() throws Exception {
final int THREADS = 50;
final int LOOPS = 1000;
final BlockingArrayQueue<Integer> queue = new BlockingArrayQueue<>(1 + THREADS * LOOPS);
final ConcurrentLinkedQueue<Integer> produced = new ConcurrentLinkedQueue<>();
final ConcurrentLinkedQueue<Integer> consumed = new ConcurrentLinkedQueue<>();
final AtomicBoolean running = new AtomicBoolean(true);
// start consumers
final CyclicBarrier barrier0 = new CyclicBarrier(THREADS + 1);
for (int i = 0; i < THREADS; i++) {
new Thread() {
@Override
public void run() {
final Random random = new Random();
setPriority(getPriority() - 1);
try {
while (running.get()) {
int r = 1 + random.nextInt(10);
if (r % 2 == 0) {
Integer msg = queue.poll();
if (msg == null) {
Thread.sleep(1 + random.nextInt(10));
continue;
}
consumed.add(msg);
} else {
Integer msg = queue.poll(r, TimeUnit.MILLISECONDS);
if (msg != null)
consumed.add(msg);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
barrier0.await();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
// start producers
final CyclicBarrier barrier1 = new CyclicBarrier(THREADS + 1);
for (int i = 0; i < THREADS; i++) {
final int id = i;
new Thread() {
@Override
public void run() {
final Random random = new Random();
try {
for (int j = 0; j < LOOPS; j++) {
Integer msg = random.nextInt();
produced.add(msg);
if (!queue.offer(msg))
throw new Exception(id + " FULL! " + queue.size());
Thread.sleep(1 + random.nextInt(10));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
barrier1.await();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
barrier1.await();
int size = queue.size();
int last = size - 1;
while (size > 0 && size != last) {
last = size;
Thread.sleep(500);
size = queue.size();
}
running.set(false);
barrier0.await();
HashSet<Integer> prodSet = new HashSet<>(produced);
HashSet<Integer> consSet = new HashSet<>(consumed);
Assert.assertEquals(prodSet, consSet);
}
use of java.util.concurrent.ConcurrentLinkedQueue in project jetty.project by eclipse.
the class AsyncIOServletTest method testWriteFromOnDataAvailable.
@Test
public void testWriteFromOnDataAvailable() throws Exception {
Queue<Throwable> errors = new ConcurrentLinkedQueue<>();
CountDownLatch writeLatch = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AsyncContext asyncContext = request.startAsync();
request.getInputStream().setReadListener(new ReadListener() {
@Override
public void onDataAvailable() throws IOException {
ServletInputStream input = request.getInputStream();
ServletOutputStream output = response.getOutputStream();
while (input.isReady()) {
byte[] buffer = new byte[512];
int read = input.read(buffer);
if (read < 0) {
asyncContext.complete();
break;
}
if (output.isReady())
output.write(buffer, 0, read);
else
Assert.fail();
}
}
@Override
public void onAllDataRead() throws IOException {
asyncContext.complete();
}
@Override
public void onError(Throwable t) {
errors.offer(t);
}
});
response.getOutputStream().setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
writeLatch.countDown();
}
@Override
public void onError(Throwable t) {
errors.offer(t);
}
});
}
});
String content = "0123456789ABCDEF";
DeferredContentProvider contentProvider = new DeferredContentProvider();
contentProvider.offer(ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)));
CountDownLatch clientLatch = new CountDownLatch(1);
client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(contentProvider).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
if (result.isSucceeded()) {
Response response = result.getResponse();
assertThat(response.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
assertThat(getContentAsString(), Matchers.equalTo(content));
assertThat(errors, Matchers.hasSize(0));
clientLatch.countDown();
}
}
});
assertTrue(writeLatch.await(5, TimeUnit.SECONDS));
contentProvider.close();
assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
use of java.util.concurrent.ConcurrentLinkedQueue in project jetty.project by eclipse.
the class StressTest method doThreads.
private void doThreads(int threadCount, final int loops, final boolean persistent) throws Throwable {
final Throwable[] throwables = new Throwable[threadCount];
final Thread[] threads = new Thread[threadCount];
try {
for (int i = 0; i < threadCount; i++) {
final int id = i;
final String name = "T" + i;
Thread.sleep(_random.nextInt(100));
threads[i] = new Thread() {
@Override
public void run() {
try {
doLoops(id, name, loops, persistent);
} catch (Throwable th) {
th.printStackTrace();
throwables[id] = th;
}
}
};
}
_loops = new AtomicInteger[threadCount];
for (int i = 0; i < threadCount; i++) {
_loops[i] = new AtomicInteger(0);
threads[i].start();
}
String last = null;
int same = 0;
while (true) {
Thread.sleep(1000L);
int finished = 0;
int errors = 0;
int min = loops;
int max = 0;
int total = 0;
for (int i = 0; i < threadCount; i++) {
int l = _loops[i].get();
if (l < 0) {
errors++;
total -= l;
} else {
if (l < min)
min = l;
if (l > max)
max = l;
total += l;
if (l == loops)
finished++;
}
}
String status = "min/ave/max/target=" + min + "/" + (total / threadCount) + "/" + max + "/" + loops + " errors/finished/loops=" + errors + "/" + finished + "/" + threadCount + " idle/threads=" + (_threads.getIdleThreads()) + "/" + _threads.getThreads();
if (status.equals(last)) {
if (same++ > 5) {
System.err.println("STALLED!!!");
System.err.println(_server.getThreadPool().toString());
Thread.sleep(5000);
System.exit(1);
}
} else
same = 0;
last = status;
LOG.info(_server.getThreadPool().toString() + " " + status);
if ((finished + errors) == threadCount)
break;
}
for (Thread thread : threads) thread.join();
for (Throwable throwable : throwables) if (throwable != null)
throw throwable;
for (ConcurrentLinkedQueue _latency : _latencies) assertEquals(_handled.get(), _latency.size());
} finally {
// System.err.println();
final int quantums = 48;
final int[][] count = new int[_latencies.length][quantums];
final int[] length = new int[_latencies.length];
final int[] other = new int[_latencies.length];
long total = 0;
for (int i = 0; i < _latencies.length; i++) {
Queue<Long> latencies = _latencies[i];
length[i] = latencies.size();
loop: for (long latency : latencies) {
if (i == 4)
total += latency;
for (int q = 0; q < quantums; q++) {
if (latency >= (q * 100) && latency < ((q + 1) * 100)) {
count[i][q]++;
continue loop;
}
}
other[i]++;
}
}
System.out.println(" stage:\tbind\twrite\trecv\tdispatch\twrote\ttotal");
for (int q = 0; q < quantums; q++) {
System.out.printf("%02d00<=l<%02d00", q, (q + 1));
for (int i = 0; i < _latencies.length; i++) System.out.print("\t" + count[i][q]);
System.out.println();
}
System.out.print("other ");
for (int i = 0; i < _latencies.length; i++) System.out.print("\t" + other[i]);
System.out.println();
System.out.print("HANDLED ");
for (int i = 0; i < _latencies.length; i++) System.out.print("\t" + _handled.get());
System.out.println();
System.out.print("TOTAL ");
for (int i = 0; i < _latencies.length; i++) System.out.print("\t" + length[i]);
System.out.println();
long ave = total / _latencies[4].size();
System.out.println("ave=" + ave);
}
}
use of java.util.concurrent.ConcurrentLinkedQueue in project buck by facebook.
the class CachingBuildEngine method processBuildRule.
private ListenableFuture<BuildResult> processBuildRule(final BuildRule rule, final BuildEngineBuildContext buildContext, final ExecutionContext executionContext, final OnDiskBuildInfo onDiskBuildInfo, final BuildInfoRecorder buildInfoRecorder, final BuildableContext buildableContext, final ConcurrentLinkedQueue<ListenableFuture<Void>> asyncCallbacks) {
// If we've already seen a failure, exit early.
if (!buildContext.isKeepGoing() && firstFailure != null) {
return Futures.immediateFuture(BuildResult.canceled(rule, firstFailure));
}
final RuleKeyFactories ruleKeyFactory = ruleKeyFactories.apply(rule.getProjectFilesystem());
try (BuildRuleEvent.Scope scope = BuildRuleEvent.resumeSuspendScope(buildContext.getEventBus(), rule, buildRuleDurationTracker, ruleKeyFactory.getDefaultRuleKeyFactory())) {
// 1. Check if it's already built.
Optional<RuleKey> cachedRuleKey = onDiskBuildInfo.getRuleKey(BuildInfo.MetadataKey.RULE_KEY);
final RuleKey defaultRuleKey = ruleKeyFactory.getDefaultRuleKeyFactory().build(rule);
if (defaultRuleKey.equals(cachedRuleKey.orElse(null))) {
return Futures.transform(markRuleAsUsed(rule, buildContext.getEventBus()), Functions.constant(BuildResult.success(rule, BuildRuleSuccessType.MATCHING_RULE_KEY, CacheResult.localKeyUnchangedHit())));
}
// 2. Rule key cache lookup.
ListenableFuture<CacheResult> rulekeyCacheResult = cacheActivityService.submit(() -> {
CacheResult cacheResult = tryToFetchArtifactFromBuildCacheAndOverlayOnTopOfProjectFilesystem(rule, defaultRuleKey, buildContext.getArtifactCache(), // TODO(shs96c): This should be a shared between all tests, not one per cell
rule.getProjectFilesystem(), buildContext);
if (cacheResult.getType().isSuccess()) {
fillMissingBuildMetadataFromCache(cacheResult, buildInfoRecorder, BuildInfo.MetadataKey.INPUT_BASED_RULE_KEY, BuildInfo.MetadataKey.DEP_FILE_RULE_KEY, BuildInfo.MetadataKey.DEP_FILE);
}
return cacheResult;
}, CACHE_CHECK_RESOURCE_AMOUNTS);
return Futures.transformAsync(rulekeyCacheResult, ruleAsyncFunction(rule, buildContext.getEventBus(), (cacheResult) -> handleRuleKeyCacheResult(rule, buildContext, executionContext, onDiskBuildInfo, buildInfoRecorder, buildableContext, asyncCallbacks, ruleKeyFactory, cacheResult)), serviceByAdjustingDefaultWeightsTo(SCHEDULING_MORE_WORK_RESOURCE_AMOUNTS));
}
}
Aggregations