Search in sources :

Example 1 with Semaphore

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

the class AbstractSimpleTransportTestCase method testTimeoutSendExceptionWithDelayedResponse.

public void testTimeoutSendExceptionWithDelayedResponse() throws Exception {
    CountDownLatch waitForever = new CountDownLatch(1);
    CountDownLatch doneWaitingForever = new CountDownLatch(1);
    Semaphore inFlight = new Semaphore(Integer.MAX_VALUE);
    serviceA.registerRequestHandler("sayHelloTimeoutDelayedResponse", StringMessageRequest::new, ThreadPool.Names.GENERIC, new TransportRequestHandler<StringMessageRequest>() {

        @Override
        public void messageReceived(StringMessageRequest request, TransportChannel channel) throws InterruptedException {
            String message = request.message;
            inFlight.acquireUninterruptibly();
            try {
                if ("forever".equals(message)) {
                    waitForever.await();
                } else {
                    TimeValue sleep = TimeValue.parseTimeValue(message, null, "sleep");
                    Thread.sleep(sleep.millis());
                }
                try {
                    channel.sendResponse(new StringMessageResponse("hello " + request.message));
                } catch (IOException e) {
                    logger.error("Unexpected failure", e);
                    fail(e.getMessage());
                }
            } finally {
                inFlight.release();
                if ("forever".equals(message)) {
                    doneWaitingForever.countDown();
                }
            }
        }
    });
    final CountDownLatch latch = new CountDownLatch(1);
    TransportFuture<StringMessageResponse> res = serviceB.submitRequest(nodeA, "sayHelloTimeoutDelayedResponse", new StringMessageRequest("forever"), TransportRequestOptions.builder().withTimeout(100).build(), new TransportResponseHandler<StringMessageResponse>() {

        @Override
        public StringMessageResponse newInstance() {
            return new StringMessageResponse();
        }

        @Override
        public String executor() {
            return ThreadPool.Names.GENERIC;
        }

        @Override
        public void handleResponse(StringMessageResponse response) {
            latch.countDown();
            fail("got response instead of exception");
        }

        @Override
        public void handleException(TransportException exp) {
            latch.countDown();
            assertThat(exp, instanceOf(ReceiveTimeoutTransportException.class));
        }
    });
    try {
        res.txGet();
        fail("exception should be thrown");
    } catch (Exception e) {
        assertThat(e, instanceOf(ReceiveTimeoutTransportException.class));
    }
    latch.await();
    List<Runnable> assertions = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        final int counter = i;
        // now, try and send another request, this times, with a short timeout
        TransportFuture<StringMessageResponse> result = serviceB.submitRequest(nodeA, "sayHelloTimeoutDelayedResponse", new StringMessageRequest(counter + "ms"), TransportRequestOptions.builder().withTimeout(3000).build(), new TransportResponseHandler<StringMessageResponse>() {

            @Override
            public StringMessageResponse newInstance() {
                return new StringMessageResponse();
            }

            @Override
            public String executor() {
                return ThreadPool.Names.GENERIC;
            }

            @Override
            public void handleResponse(StringMessageResponse response) {
                assertThat("hello " + counter + "ms", equalTo(response.message));
            }

            @Override
            public void handleException(TransportException exp) {
                logger.error("Unexpected failure", exp);
                fail("got exception instead of a response for " + counter + ": " + exp.getDetailedMessage());
            }
        });
        assertions.add(() -> {
            StringMessageResponse message = result.txGet();
            assertThat(message.message, equalTo("hello " + counter + "ms"));
        });
    }
    for (Runnable runnable : assertions) {
        runnable.run();
    }
    waitForever.countDown();
    doneWaitingForever.await();
    assertTrue(inFlight.tryAcquire(Integer.MAX_VALUE, 10, TimeUnit.SECONDS));
}
Also used : ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ElasticsearchException(org.elasticsearch.ElasticsearchException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ExecutionException(java.util.concurrent.ExecutionException) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 2 with Semaphore

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

the class QoSFilter method init.

public void init(FilterConfig filterConfig) {
    int max_priority = __DEFAULT_MAX_PRIORITY;
    if (filterConfig.getInitParameter(MAX_PRIORITY_INIT_PARAM) != null)
        max_priority = Integer.parseInt(filterConfig.getInitParameter(MAX_PRIORITY_INIT_PARAM));
    _queues = new Queue[max_priority + 1];
    _listeners = new AsyncListener[_queues.length];
    for (int p = 0; p < _queues.length; ++p) {
        _queues[p] = new ConcurrentLinkedQueue<>();
        _listeners[p] = new QoSAsyncListener(p);
    }
    int maxRequests = __DEFAULT_PASSES;
    if (filterConfig.getInitParameter(MAX_REQUESTS_INIT_PARAM) != null)
        maxRequests = Integer.parseInt(filterConfig.getInitParameter(MAX_REQUESTS_INIT_PARAM));
    _passes = new Semaphore(maxRequests, true);
    _maxRequests = maxRequests;
    long wait = __DEFAULT_WAIT_MS;
    if (filterConfig.getInitParameter(MAX_WAIT_INIT_PARAM) != null)
        wait = Integer.parseInt(filterConfig.getInitParameter(MAX_WAIT_INIT_PARAM));
    _waitMs = wait;
    long suspend = __DEFAULT_TIMEOUT_MS;
    if (filterConfig.getInitParameter(SUSPEND_INIT_PARAM) != null)
        suspend = Integer.parseInt(filterConfig.getInitParameter(SUSPEND_INIT_PARAM));
    _suspendMs = suspend;
    ServletContext context = filterConfig.getServletContext();
    if (context != null && Boolean.parseBoolean(filterConfig.getInitParameter(MANAGED_ATTR_INIT_PARAM)))
        context.setAttribute(filterConfig.getFilterName(), this);
}
Also used : ServletContext(javax.servlet.ServletContext) Semaphore(java.util.concurrent.Semaphore)

Example 3 with Semaphore

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

the class QoSFilter method setMaxRequests.

/**
     * Set the maximum number of requests allowed to be processed
     * at the same time.
     *
     * @param value the number of requests
     */
public void setMaxRequests(int value) {
    _passes = new Semaphore((value - getMaxRequests() + _passes.availablePermits()), true);
    _maxRequests = value;
}
Also used : Semaphore(java.util.concurrent.Semaphore)

Example 4 with Semaphore

use of java.util.concurrent.Semaphore in project che by eclipse.

the class MavenTaskExecutor method waitForEndAllTasks.

public void waitForEndAllTasks() {
    if (!isWorking) {
        return;
    }
    Semaphore semaphore = new Semaphore(1);
    try {
        semaphore.acquire();
        submitTask(semaphore::release);
        while (true) {
            if (!isWorking || semaphore.tryAcquire(1, TimeUnit.SECONDS)) {
                return;
            }
        }
    } catch (InterruptedException e) {
        LOG.debug(e.getMessage(), e);
    }
}
Also used : Semaphore(java.util.concurrent.Semaphore)

Example 5 with Semaphore

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

the class IndexShardTests method testAsyncFsync.

public void testAsyncFsync() throws InterruptedException, IOException {
    IndexShard shard = newStartedShard();
    Semaphore semaphore = new Semaphore(Integer.MAX_VALUE);
    Thread[] thread = new Thread[randomIntBetween(3, 5)];
    CountDownLatch latch = new CountDownLatch(thread.length);
    for (int i = 0; i < thread.length; i++) {
        thread[i] = new Thread() {

            @Override
            public void run() {
                try {
                    latch.countDown();
                    latch.await();
                    for (int i = 0; i < 10000; i++) {
                        semaphore.acquire();
                        shard.sync(TranslogTests.randomTranslogLocation(), (ex) -> semaphore.release());
                    }
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        };
        thread[i].start();
    }
    for (int i = 0; i < thread.length; i++) {
        thread[i].join();
    }
    assertTrue(semaphore.tryAcquire(Integer.MAX_VALUE, 10, TimeUnit.SECONDS));
    closeShards(shard);
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) Versions(org.elasticsearch.common.lucene.uid.Versions) Arrays(java.util.Arrays) Releasables(org.elasticsearch.common.lease.Releasables) Term(org.apache.lucene.index.Term) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) TranslogTests(org.elasticsearch.index.translog.TranslogTests) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ParseContext(org.elasticsearch.index.mapper.ParseContext) EMPTY_REPO_GEN(org.elasticsearch.repositories.RepositoryData.EMPTY_REPO_GEN) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) UidFieldMapper(org.elasticsearch.index.mapper.UidFieldMapper) IOContext(org.apache.lucene.store.IOContext) Path(java.nio.file.Path) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) UUIDs(org.elasticsearch.common.UUIDs) Set(java.util.Set) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) RecoverySource(org.elasticsearch.cluster.routing.RecoverySource) SeqNoFieldMapper(org.elasticsearch.index.mapper.SeqNoFieldMapper) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) Logger(org.apache.logging.log4j.Logger) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.containsString(org.hamcrest.Matchers.containsString) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) IndexCommit(org.apache.lucene.index.IndexCommit) FieldDataStats(org.elasticsearch.index.fielddata.FieldDataStats) SnapshotId(org.elasticsearch.snapshots.SnapshotId) SnapshotShardFailure(org.elasticsearch.snapshots.SnapshotShardFailure) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) ArrayList(java.util.ArrayList) BytesArray(org.elasticsearch.common.bytes.BytesArray) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) SequenceNumbersService(org.elasticsearch.index.seqno.SequenceNumbersService) Store(org.elasticsearch.index.store.Store) BiConsumer(java.util.function.BiConsumer) Matchers.hasSize(org.hamcrest.Matchers.hasSize) EngineException(org.elasticsearch.index.engine.EngineException) TopDocs(org.apache.lucene.search.TopDocs) EMPTY_PARAMS(org.elasticsearch.common.xcontent.ToXContent.EMPTY_PARAMS) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) ExecutionException(java.util.concurrent.ExecutionException) Lucene.cleanLuceneIndex(org.elasticsearch.common.lucene.Lucene.cleanLuceneIndex) CommonStatsFlags(org.elasticsearch.action.admin.indices.stats.CommonStatsFlags) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) ConcurrentCollections(org.elasticsearch.common.util.concurrent.ConcurrentCollections) IndicesQueryCache(org.elasticsearch.indices.IndicesQueryCache) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) VersionType(org.elasticsearch.index.VersionType) Matchers.hasKey(org.hamcrest.Matchers.hasKey) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) Settings(org.elasticsearch.common.settings.Settings) ShardRoutingHelper(org.elasticsearch.cluster.routing.ShardRoutingHelper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) ThreadPool(org.elasticsearch.threadpool.ThreadPool) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Releasable(org.elasticsearch.common.lease.Releasable) CyclicBarrier(java.util.concurrent.CyclicBarrier) DirectoryReader(org.apache.lucene.index.DirectoryReader) BytesReference(org.elasticsearch.common.bytes.BytesReference) Collectors(java.util.stream.Collectors) Engine(org.elasticsearch.index.engine.Engine) List(java.util.List) Version(org.elasticsearch.Version) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState) Matchers.equalTo(org.hamcrest.Matchers.equalTo) CommonStats(org.elasticsearch.action.admin.indices.stats.CommonStats) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) RepositoryData(org.elasticsearch.repositories.RepositoryData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) IntStream(java.util.stream.IntStream) XContentType(org.elasticsearch.common.xcontent.XContentType) PRIMARY(org.elasticsearch.index.engine.Engine.Operation.Origin.PRIMARY) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) IndexId(org.elasticsearch.repositories.IndexId) AtomicReference(java.util.concurrent.atomic.AtomicReference) ForceMergeRequest(org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest) HashSet(java.util.HashSet) ShardStats(org.elasticsearch.action.admin.indices.stats.ShardStats) Mapping(org.elasticsearch.index.mapper.Mapping) Collections.emptyMap(java.util.Collections.emptyMap) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) Repository(org.elasticsearch.repositories.Repository) Uid(org.elasticsearch.index.mapper.Uid) Collections.emptySet(java.util.Collections.emptySet) Semaphore(java.util.concurrent.Semaphore) RecoveryTarget(org.elasticsearch.indices.recovery.RecoveryTarget) AbstractLifecycleComponent(org.elasticsearch.common.component.AbstractLifecycleComponent) FieldMaskingReader(org.elasticsearch.test.FieldMaskingReader) VersionUtils(org.elasticsearch.test.VersionUtils) TimeUnit(java.util.concurrent.TimeUnit) TermQuery(org.apache.lucene.search.TermQuery) FlushRequest(org.elasticsearch.action.admin.indices.flush.FlushRequest) Constants(org.apache.lucene.util.Constants) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) Field(org.apache.lucene.document.Field) StreamInput(org.elasticsearch.common.io.stream.StreamInput) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) Translog(org.elasticsearch.index.translog.Translog) AllocationId(org.elasticsearch.cluster.routing.AllocationId) DummyShardLock(org.elasticsearch.test.DummyShardLock) Snapshot(org.elasticsearch.snapshots.Snapshot) Collections(java.util.Collections) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) EngineException(org.elasticsearch.index.engine.EngineException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ExecutionException(java.util.concurrent.ExecutionException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException)

Aggregations

Semaphore (java.util.concurrent.Semaphore)971 Test (org.junit.Test)422 IOException (java.io.IOException)104 ArrayList (java.util.ArrayList)104 CountDownLatch (java.util.concurrent.CountDownLatch)73 AtomicReference (java.util.concurrent.atomic.AtomicReference)72 InvocationOnMock (org.mockito.invocation.InvocationOnMock)70 HashMap (java.util.HashMap)68 List (java.util.List)66 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)64 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)63 Map (java.util.Map)50 TimeUnit (java.util.concurrent.TimeUnit)46 File (java.io.File)45 ExecutionException (java.util.concurrent.ExecutionException)45 ExecutorService (java.util.concurrent.ExecutorService)44 Context (android.content.Context)43 Test (org.testng.annotations.Test)43 TimeoutException (java.util.concurrent.TimeoutException)38 HashSet (java.util.HashSet)33