Search in sources :

Example 66 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project spring-data-mongodb by spring-projects.

the class ReactiveMongoRepositoryTests method shouldUseTailableCursor.

// DATAMONGO-1444
@Test
public void shouldUseTailableCursor() throws Exception {
    StepVerifier.create(// 
    template.dropCollection(Capped.class).then(// 
    template.createCollection(// 
    Capped.class, // 
    CollectionOptions.empty().size(1000).maxDocuments(100).capped()))).expectNextCount(// 
    1).verifyComplete();
    StepVerifier.create(template.insert(new Capped("value", Math.random()))).expectNextCount(1).verifyComplete();
    BlockingQueue<Capped> documents = new LinkedBlockingDeque<>(100);
    Disposable disposable = cappedRepository.findByKey("value").doOnNext(documents::add).subscribe();
    assertThat(documents.poll(5, TimeUnit.SECONDS), is(notNullValue()));
    StepVerifier.create(template.insert(new Capped("value", Math.random()))).expectNextCount(1).verifyComplete();
    assertThat(documents.poll(5, TimeUnit.SECONDS), is(notNullValue()));
    assertThat(documents.isEmpty(), is(true));
    disposable.dispose();
}
Also used : Disposable(reactor.core.Disposable) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) Test(org.junit.Test)

Example 67 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project tomee by apache.

the class JNDIContext method newExecutor.

public static ThreadPoolExecutor newExecutor(final int threads, final BlockingQueue<Runnable> blockingQueue) {
    /**
     *         This thread pool starts with 3 core threads and can grow to the limit defined by 'threads'.
     *         If a pool thread is idle for more than 1 minute it will be discarded, unless the core size is reached.
     *         It can accept upto the number of processes defined by 'queue'.
     *         If the queue is full then an attempt is made to add the process to the queue for 10 seconds.
     *         Failure to add to the queue in this time will either result in a logged rejection, or if 'block'
     *         is true then a final attempt is made to run the process in the current thread (the service thread).
     */
    final ThreadPoolExecutor executorService = new ThreadPoolExecutor(3, (threads < 3 ? 3 : threads), 1, TimeUnit.MINUTES, blockingQueue == null ? new LinkedBlockingDeque<Runnable>(Integer.parseInt(getProperty(null, POOL_QUEUE_SIZE, "2"))) : blockingQueue);
    executorService.setThreadFactory(new ThreadFactory() {

        private final AtomicInteger i = new AtomicInteger(0);

        @Override
        public Thread newThread(final Runnable r) {
            final Thread t = new Thread(r, "OpenEJB.Client." + i.incrementAndGet());
            t.setDaemon(true);
            t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

                @Override
                public void uncaughtException(final Thread t, final Throwable e) {
                    Logger.getLogger(EJBObjectHandler.class.getName()).log(Level.SEVERE, "Uncaught error in: " + t.getName(), e);
                }
            });
            return t;
        }
    });
    executorService.setRejectedExecutionHandler(new RejectedExecutionHandler() {

        @Override
        public void rejectedExecution(final Runnable r, final ThreadPoolExecutor tpe) {
            if (null == r || null == tpe || tpe.isShutdown() || tpe.isTerminated() || tpe.isTerminating()) {
                return;
            }
            final Logger log = Logger.getLogger(EJBObjectHandler.class.getName());
            if (log.isLoggable(Level.WARNING)) {
                log.log(Level.WARNING, "EJBObjectHandler ExecutorService at capicity for process: " + r);
            }
            boolean offer = false;
            try {
                offer = tpe.getQueue().offer(r, 10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
            // Ignore
            }
            if (!offer) {
                log.log(Level.SEVERE, "EJBObjectHandler ExecutorService failed to run asynchronous process: " + r);
            }
        }
    });
    return executorService;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Logger(java.util.logging.Logger)

Example 68 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project jetty.project by eclipse.

the class AsyncIOServletTest method testWriteListenerFromOtherThread.

@Test
public void testWriteListenerFromOtherThread() throws Exception {
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            AsyncContext asyncContext = request.startAsync();
            asyncContext.setTimeout(0);
            request.getInputStream().setReadListener(new Listener(asyncContext));
        }
    });
    int cores = 4;
    int iterations = 10;
    CountDownLatch latch = new CountDownLatch(cores * iterations);
    Deque<Throwable> failures = new LinkedBlockingDeque<>();
    for (int i = 0; i < cores; ++i) {
        client.getExecutor().execute(() -> {
            for (int j = 0; j < iterations; ++j) {
                try {
                    ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(new InputStreamContentProvider(new ByteArrayInputStream(new byte[16 * 1024]) {

                        @Override
                        public int read(byte[] b, int off, int len) {
                            sleep(5);
                            return super.read(b, off, Math.min(len, 4242));
                        }
                    })).send();
                    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
                    latch.countDown();
                } catch (Throwable x) {
                    failures.offer(x);
                }
            }
        });
    }
    Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
    Assert.assertTrue(failures.isEmpty());
}
Also used : BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) WriteListener(javax.servlet.WriteListener) ReadListener(javax.servlet.ReadListener) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 69 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project elasticsearch by elastic.

the class MockTransportService method addUnresponsiveRule.

/**
     * Adds a rule that will cause ignores each send request, simulating an unresponsive node
     * and failing to connect once the rule was added.
     *
     * @param duration the amount of time to delay sending and connecting.
     */
public void addUnresponsiveRule(TransportAddress transportAddress, final TimeValue duration) {
    final long startTime = System.currentTimeMillis();
    addDelegate(transportAddress, new ClearableTransport(original) {

        private final Queue<Runnable> requestsToSendWhenCleared = new LinkedBlockingDeque<Runnable>();

        private boolean cleared = false;

        TimeValue getDelay() {
            return new TimeValue(duration.millis() - (System.currentTimeMillis() - startTime));
        }

        @Override
        public void connectToNode(DiscoveryNode node, ConnectionProfile connectionProfile, CheckedBiConsumer<Connection, ConnectionProfile, IOException> connectionValidator) throws ConnectTransportException {
            if (original.nodeConnected(node)) {
                // connecting to an already connected node is a no-op
                return;
            }
            TimeValue delay = getDelay();
            if (delay.millis() <= 0) {
                original.connectToNode(node, connectionProfile, connectionValidator);
                return;
            }
            // TODO: Replace with proper setting
            TimeValue connectingTimeout = NetworkService.TcpSettings.TCP_CONNECT_TIMEOUT.getDefault(Settings.EMPTY);
            try {
                if (delay.millis() < connectingTimeout.millis()) {
                    Thread.sleep(delay.millis());
                    original.connectToNode(node, connectionProfile, connectionValidator);
                } else {
                    Thread.sleep(connectingTimeout.millis());
                    throw new ConnectTransportException(node, "UNRESPONSIVE: simulated");
                }
            } catch (InterruptedException e) {
                throw new ConnectTransportException(node, "UNRESPONSIVE: simulated");
            }
        }

        @Override
        protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException {
            // delayed sending - even if larger then the request timeout to simulated a potential late response from target node
            TimeValue delay = getDelay();
            if (delay.millis() <= 0) {
                connection.sendRequest(requestId, action, request, options);
                return;
            }
            // poor mans request cloning...
            RequestHandlerRegistry reg = MockTransportService.this.getRequestHandler(action);
            BytesStreamOutput bStream = new BytesStreamOutput();
            request.writeTo(bStream);
            final TransportRequest clonedRequest = reg.newRequest();
            clonedRequest.readFrom(bStream.bytes().streamInput());
            Runnable runnable = new AbstractRunnable() {

                AtomicBoolean requestSent = new AtomicBoolean();

                @Override
                public void onFailure(Exception e) {
                    logger.debug("failed to send delayed request", e);
                }

                @Override
                protected void doRun() throws IOException {
                    if (requestSent.compareAndSet(false, true)) {
                        connection.sendRequest(requestId, action, clonedRequest, options);
                    }
                }
            };
            // store the request to send it once the rule is cleared.
            synchronized (this) {
                if (cleared) {
                    runnable.run();
                } else {
                    requestsToSendWhenCleared.add(runnable);
                    threadPool.schedule(delay, ThreadPool.Names.GENERIC, runnable);
                }
            }
        }

        @Override
        public void clearRule() {
            synchronized (this) {
                assert cleared == false;
                cleared = true;
                requestsToSendWhenCleared.forEach(Runnable::run);
            }
        }
    });
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) TransportRequest(org.elasticsearch.transport.TransportRequest) ConnectionProfile(org.elasticsearch.transport.ConnectionProfile) IOException(java.io.IOException) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) TransportException(org.elasticsearch.transport.TransportException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) RequestHandlerRegistry(org.elasticsearch.transport.RequestHandlerRegistry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 70 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project pinot by linkedin.

the class ScatterGatherPerfClient method setup.

private void setup() {
    MetricsRegistry registry = new MetricsRegistry();
    _timedExecutor = new ScheduledThreadPoolExecutor(1);
    _service = new ThreadPoolExecutor(10, 10, 10, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>());
    _eventLoopGroup = new NioEventLoopGroup(10);
    _timer = new HashedWheelTimer();
    NettyClientMetrics clientMetrics = new NettyClientMetrics(registry, "client_");
    PooledNettyClientResourceManager rm = new PooledNettyClientResourceManager(_eventLoopGroup, _timer, clientMetrics);
    _pool = new KeyedPoolImpl<ServerInstance, NettyClientConnection>(1, _maxActiveConnections, 300000, 10, rm, _timedExecutor, MoreExecutors.sameThreadExecutor(), registry);
    rm.setPool(_pool);
    _scatterGather = new ScatterGatherImpl(_pool, _service);
    for (AsyncReader r : _readerThreads) {
        r.start();
    }
}
Also used : MetricsRegistry(com.yammer.metrics.core.MetricsRegistry) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) NettyClientMetrics(com.linkedin.pinot.transport.metrics.NettyClientMetrics) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) PooledNettyClientResourceManager(com.linkedin.pinot.transport.netty.PooledNettyClientResourceManager) HashedWheelTimer(io.netty.util.HashedWheelTimer) NettyClientConnection(com.linkedin.pinot.transport.netty.NettyClientConnection) ScatterGatherImpl(com.linkedin.pinot.transport.scattergather.ScatterGatherImpl) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ServerInstance(com.linkedin.pinot.common.response.ServerInstance) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)119 Test (org.junit.Test)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 NoSuchElementException (java.util.NoSuchElementException)8 ArrayList (java.util.ArrayList)7 Iterator (java.util.Iterator)7 IOException (java.io.IOException)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 ExecutorService (java.util.concurrent.ExecutorService)5 BlockingDeque (java.util.concurrent.BlockingDeque)4 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)4 ByteBuffer (java.nio.ByteBuffer)3 HashMap (java.util.HashMap)3 IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)2 AmqpsMessage (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage)2 AmqpsTransport (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport)2 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2