use of java.io.Closeable in project druid by druid-io.
the class ReferenceCountingResourceHolderTest method runIdiomaticUsage.
private void runIdiomaticUsage() {
final AtomicBoolean released = new AtomicBoolean(false);
final ReferenceCountingResourceHolder<Closeable> resourceHolder = makeReleasingHandler(released);
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Thread thread = new Thread() {
@Override
public void run() {
try (Releaser r = resourceHolder.increment()) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
};
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
Assert.assertFalse(released.get());
resourceHolder.close();
Assert.assertTrue(released.get());
}
use of java.io.Closeable in project jetty.project by eclipse.
the class ThreadLimitHandler method handle.
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// Allow ThreadLimit to be enabled dynamically without restarting server
if (!_enabled) {
// if disabled, handle normally
super.handle(target, baseRequest, request, response);
} else {
// Get the remote address of the request
Remote remote = getRemote(baseRequest);
if (remote == null) {
// if remote is not known, handle normally
super.handle(target, baseRequest, request, response);
} else {
// Do we already have a future permit from a previous invocation?
Closeable permit = (Closeable) baseRequest.getAttribute(PERMIT);
try {
if (permit != null) {
// Yes, remove it from any future async cycles.
baseRequest.removeAttribute(PERMIT);
} else {
// No, then lets try to acquire one
CompletableFuture<Closeable> future_permit = remote.acquire();
// Did we get a permit?
if (future_permit.isDone()) {
// yes
permit = future_permit.get();
} else {
if (LOG.isDebugEnabled())
LOG.debug("Threadlimited {} {}", remote, target);
// No, lets asynchronously suspend the request
AsyncContext async = baseRequest.startAsync();
// let's never timeout the async. If this is a DOS, then good to make them wait, if this is not
// then give them maximum time to get a thread.
async.setTimeout(0);
// dispatch the request when we do eventually get a pass
future_permit.thenAccept(c -> {
baseRequest.setAttribute(PERMIT, c);
async.dispatch();
});
return;
}
}
// Use the permit
super.handle(target, baseRequest, request, response);
} catch (InterruptedException | ExecutionException e) {
throw new ServletException(e);
} finally {
if (permit != null)
permit.close();
}
}
}
}
use of java.io.Closeable in project elasticsearch by elastic.
the class ZenDiscoveryUnitTests method testPendingCSQueueIsClearedWhenClusterStatePublished.
public void testPendingCSQueueIsClearedWhenClusterStatePublished() throws Exception {
ThreadPool threadPool = new TestThreadPool(getClass().getName());
// randomly make minimum_master_nodes a value higher than we have nodes for, so it will force failure
int minMasterNodes = randomBoolean() ? 3 : 1;
Settings settings = Settings.builder().put(DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), Integer.toString(minMasterNodes)).build();
ArrayDeque<Closeable> toClose = new ArrayDeque<>();
try {
final MockTransportService masterTransport = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null);
masterTransport.start();
DiscoveryNode masterNode = masterTransport.getLocalNode();
toClose.addFirst(masterTransport);
ClusterState state = ClusterStateCreationUtils.state(masterNode, null, masterNode);
// build the zen discovery and cluster service
ClusterService masterClusterService = createClusterService(threadPool, masterNode);
toClose.addFirst(masterClusterService);
state = ClusterState.builder(masterClusterService.getClusterName()).nodes(state.nodes()).build();
setState(masterClusterService, state);
ZenDiscovery masterZen = buildZenDiscovery(settings, masterTransport, masterClusterService, threadPool);
toClose.addFirst(masterZen);
masterTransport.acceptIncomingRequests();
// inject a pending cluster state
masterZen.pendingClusterStatesQueue().addPending(ClusterState.builder(new ClusterName("foreign")).build());
// a new cluster state with a new discovery node (we will test if the cluster state
// was updated by the presence of this node in NodesFaultDetection)
ClusterState newState = ClusterState.builder(masterClusterService.state()).incrementVersion().nodes(DiscoveryNodes.builder(masterClusterService.state().nodes()).masterNodeId(masterNode.getId())).build();
try {
// publishing a new cluster state
ClusterChangedEvent clusterChangedEvent = new ClusterChangedEvent("testing", newState, state);
AssertingAckListener listener = new AssertingAckListener(newState.nodes().getSize() - 1);
masterZen.publish(clusterChangedEvent, listener);
listener.await(1, TimeUnit.HOURS);
// publish was a success, check that queue as cleared
assertThat(masterZen.pendingClusterStates(), emptyArray());
} catch (Discovery.FailedToCommitClusterStateException e) {
// not successful, so the pending queue should stay
assertThat(masterZen.pendingClusterStates(), arrayWithSize(1));
assertThat(masterZen.pendingClusterStates()[0].getClusterName().value(), equalTo("foreign"));
}
} finally {
IOUtils.close(toClose);
terminate(threadPool);
}
}
use of java.io.Closeable in project elasticsearch by elastic.
the class TransportClient method close.
/**
* Closes the client.
*/
@Override
public void close() {
List<Closeable> closeables = new ArrayList<>();
closeables.add(nodesService);
closeables.add(injector.getInstance(TransportService.class));
for (LifecycleComponent plugin : pluginLifecycleComponents) {
closeables.add(plugin);
}
closeables.add(() -> ThreadPool.terminate(injector.getInstance(ThreadPool.class), 10, TimeUnit.SECONDS));
closeables.add(injector.getInstance(BigArrays.class));
IOUtils.closeWhileHandlingException(closeables);
}
use of java.io.Closeable in project buck by facebook.
the class SimpleProc method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Closeable closeable = new StringReader("example");
Closeables.closeQuietly(closeable);
return false;
}
Aggregations