Search in sources :

Example 21 with Closeable

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList)

Example 22 with Closeable

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();
            }
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) Closeable(java.io.Closeable) AsyncContext(javax.servlet.AsyncContext) ExecutionException(java.util.concurrent.ExecutionException)

Example 23 with Closeable

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);
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ZenDiscovery.shouldIgnoreOrRejectNewClusterState(org.elasticsearch.discovery.zen.ZenDiscovery.shouldIgnoreOrRejectNewClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) Closeable(java.io.Closeable) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Discovery(org.elasticsearch.discovery.Discovery) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) ArrayDeque(java.util.ArrayDeque) ClusterServiceUtils.createClusterService(org.elasticsearch.test.ClusterServiceUtils.createClusterService) ClusterService(org.elasticsearch.cluster.service.ClusterService) AssertingAckListener(org.elasticsearch.discovery.zen.PublishClusterStateActionTests.AssertingAckListener) ClusterName(org.elasticsearch.cluster.ClusterName) Settings(org.elasticsearch.common.settings.Settings)

Example 24 with Closeable

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);
}
Also used : BigArrays(org.elasticsearch.common.util.BigArrays) TransportService(org.elasticsearch.transport.TransportService) LifecycleComponent(org.elasticsearch.common.component.LifecycleComponent) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList)

Example 25 with Closeable

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;
}
Also used : Closeable(java.io.Closeable) StringReader(java.io.StringReader)

Aggregations

Closeable (java.io.Closeable)216 IOException (java.io.IOException)88 Test (org.junit.Test)56 ArrayList (java.util.ArrayList)29 File (java.io.File)26 HashMap (java.util.HashMap)12 VirtualFile (org.jboss.vfs.VirtualFile)12 URL (java.net.URL)9 Path (java.nio.file.Path)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Map (java.util.Map)7 ISE (io.druid.java.util.common.ISE)6 InputStream (java.io.InputStream)6 MountHandle (org.jboss.as.server.deployment.module.MountHandle)6 ResourceRoot (org.jboss.as.server.deployment.module.ResourceRoot)6 ProgramController (co.cask.cdap.app.runtime.ProgramController)5 ProgramType (co.cask.cdap.proto.ProgramType)4 ProgramId (co.cask.cdap.proto.id.ProgramId)4 Pair (io.druid.java.util.common.Pair)4 FileOutputStream (java.io.FileOutputStream)4