Search in sources :

Example 31 with AbstractRunnable

use of org.elasticsearch.common.util.concurrent.AbstractRunnable in project elasticsearch by elastic.

the class LocalCheckpointTrackerTests method testConcurrentReplica.

public void testConcurrentReplica() throws InterruptedException {
    Thread[] threads = new Thread[randomIntBetween(2, 5)];
    final int opsPerThread = randomIntBetween(10, 20);
    final int maxOps = opsPerThread * threads.length;
    // make sure we always index the last seqNo to simplify maxSeq checks
    final long unFinishedSeq = randomIntBetween(0, maxOps - 2);
    Set<Integer> seqNos = IntStream.range(0, maxOps).boxed().collect(Collectors.toSet());
    final Integer[][] seqNoPerThread = new Integer[threads.length][];
    for (int t = 0; t < threads.length - 1; t++) {
        int size = Math.min(seqNos.size(), randomIntBetween(opsPerThread - 4, opsPerThread + 4));
        seqNoPerThread[t] = randomSubsetOf(size, seqNos).toArray(new Integer[size]);
        seqNos.removeAll(Arrays.asList(seqNoPerThread[t]));
    }
    seqNoPerThread[threads.length - 1] = seqNos.toArray(new Integer[seqNos.size()]);
    logger.info("--> will run [{}] threads, maxOps [{}], unfinished seq no [{}]", threads.length, maxOps, unFinishedSeq);
    final CyclicBarrier barrier = new CyclicBarrier(threads.length);
    for (int t = 0; t < threads.length; t++) {
        final int threadId = t;
        threads[t] = new Thread(new AbstractRunnable() {

            @Override
            public void onFailure(Exception e) {
                throw new ElasticsearchException("failure in background thread", e);
            }

            @Override
            protected void doRun() throws Exception {
                barrier.await();
                Integer[] ops = seqNoPerThread[threadId];
                for (int seqNo : ops) {
                    if (seqNo != unFinishedSeq) {
                        tracker.markSeqNoAsCompleted(seqNo);
                        logger.info("[t{}] completed [{}]", threadId, seqNo);
                    }
                }
            }
        }, "testConcurrentReplica_" + threadId);
        threads[t].start();
    }
    for (Thread thread : threads) {
        thread.join();
    }
    assertThat(tracker.getMaxSeqNo(), equalTo(maxOps - 1L));
    assertThat(tracker.getCheckpoint(), equalTo(unFinishedSeq - 1L));
    tracker.markSeqNoAsCompleted(unFinishedSeq);
    assertThat(tracker.getCheckpoint(), equalTo(maxOps - 1L));
    assertThat(tracker.firstProcessedSeqNo, equalTo(((long) maxOps / SMALL_CHUNK_SIZE) * SMALL_CHUNK_SIZE));
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 32 with AbstractRunnable

use of org.elasticsearch.common.util.concurrent.AbstractRunnable 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 33 with AbstractRunnable

use of org.elasticsearch.common.util.concurrent.AbstractRunnable in project elasticsearch by elastic.

the class MockTcpTransport method bind.

@Override
protected MockChannel bind(final String name, InetSocketAddress address) throws IOException {
    MockServerSocket socket = new MockServerSocket();
    socket.bind(address);
    socket.setReuseAddress(TCP_REUSE_ADDRESS.get(settings));
    ByteSizeValue tcpReceiveBufferSize = TCP_RECEIVE_BUFFER_SIZE.get(settings);
    if (tcpReceiveBufferSize.getBytes() > 0) {
        socket.setReceiveBufferSize(tcpReceiveBufferSize.bytesAsInt());
    }
    MockChannel serverMockChannel = new MockChannel(socket, name);
    CountDownLatch started = new CountDownLatch(1);
    executor.execute(new AbstractRunnable() {

        @Override
        public void onFailure(Exception e) {
            try {
                onException(serverMockChannel, e);
            } catch (IOException ex) {
                logger.warn("failed on handling exception", ex);
            }
        }

        @Override
        protected void doRun() throws Exception {
            started.countDown();
            serverMockChannel.accept(executor);
        }
    });
    try {
        started.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    return serverMockChannel;
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) MockServerSocket(org.elasticsearch.mocksocket.MockServerSocket) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) SocketException(java.net.SocketException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Aggregations

AbstractRunnable (org.elasticsearch.common.util.concurrent.AbstractRunnable)33 IOException (java.io.IOException)19 ExecutionException (java.util.concurrent.ExecutionException)11 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)10 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)9 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 CyclicBarrier (java.util.concurrent.CyclicBarrier)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 TimeValue (org.elasticsearch.common.unit.TimeValue)8 ElasticsearchException (org.elasticsearch.ElasticsearchException)7 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)6 Supplier (org.apache.logging.log4j.util.Supplier)6 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)6 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)5 UnknownHostException (java.net.UnknownHostException)4 ArrayList (java.util.ArrayList)4 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)4 ClusterState (org.elasticsearch.cluster.ClusterState)4 NotMasterException (org.elasticsearch.cluster.NotMasterException)4