Search in sources :

Example 11 with Semaphore

use of java.util.concurrent.Semaphore in project weave by continuuity.

the class ZKServiceDecoratorTest method testStateTransition.

@Test
public void testStateTransition() throws InterruptedException, ExecutionException, TimeoutException {
    InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
    zkServer.startAndWait();
    try {
        final String namespace = Joiner.on('/').join("/weave", RunIds.generate(), "runnables", "Runner1");
        final ZKClientService zkClient = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
        zkClient.startAndWait();
        zkClient.create(namespace, null, CreateMode.PERSISTENT).get();
        try {
            JsonObject content = new JsonObject();
            content.addProperty("containerId", "container-123");
            content.addProperty("host", "localhost");
            RunId runId = RunIds.generate();
            final Semaphore semaphore = new Semaphore(0);
            ZKServiceDecorator service = new ZKServiceDecorator(ZKClients.namespace(zkClient, namespace), runId, Suppliers.ofInstance(content), new AbstractIdleService() {

                @Override
                protected void startUp() throws Exception {
                    Preconditions.checkArgument(semaphore.tryAcquire(5, TimeUnit.SECONDS), "Fail to start");
                }

                @Override
                protected void shutDown() throws Exception {
                    Preconditions.checkArgument(semaphore.tryAcquire(5, TimeUnit.SECONDS), "Fail to stop");
                }
            });
            final String runnablePath = namespace + "/" + runId.getId();
            final AtomicReference<String> stateMatch = new AtomicReference<String>("STARTING");
            watchDataChange(zkClient, runnablePath + "/state", semaphore, stateMatch);
            Assert.assertEquals(Service.State.RUNNING, service.start().get(5, TimeUnit.SECONDS));
            stateMatch.set("STOPPING");
            Assert.assertEquals(Service.State.TERMINATED, service.stop().get(5, TimeUnit.SECONDS));
        } finally {
            zkClient.stopAndWait();
        }
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : ZKServiceDecorator(com.continuuity.weave.internal.ZKServiceDecorator) ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) JsonObject(com.google.gson.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) RunId(com.continuuity.weave.api.RunId) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 12 with Semaphore

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

the class SearchServiceTests method testSearchWhileIndexDeleted.

public void testSearchWhileIndexDeleted() throws IOException, InterruptedException {
    createIndex("index");
    client().prepareIndex("index", "type", "1").setSource("field", "value").setRefreshPolicy(IMMEDIATE).get();
    SearchService service = getInstanceFromNode(SearchService.class);
    IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    IndexService indexService = indicesService.indexServiceSafe(resolveIndex("index"));
    IndexShard indexShard = indexService.getShard(0);
    AtomicBoolean running = new AtomicBoolean(true);
    CountDownLatch startGun = new CountDownLatch(1);
    Semaphore semaphore = new Semaphore(Integer.MAX_VALUE);
    final Thread thread = new Thread() {

        @Override
        public void run() {
            startGun.countDown();
            while (running.get()) {
                service.afterIndexRemoved(indexService.index(), indexService.getIndexSettings(), DELETED);
                if (randomBoolean()) {
                    // context in a non-sane way.
                    try {
                        semaphore.acquire();
                    } catch (InterruptedException e) {
                        throw new AssertionError(e);
                    }
                    client().prepareIndex("index", "type").setSource("field", "value").setRefreshPolicy(randomFrom(WriteRequest.RefreshPolicy.values())).execute(new ActionListener<IndexResponse>() {

                        @Override
                        public void onResponse(IndexResponse indexResponse) {
                            semaphore.release();
                        }

                        @Override
                        public void onFailure(Exception e) {
                            semaphore.release();
                        }
                    });
                }
            }
        }
    };
    thread.start();
    startGun.await();
    try {
        final int rounds = scaledRandomIntBetween(100, 10000);
        for (int i = 0; i < rounds; i++) {
            try {
                QuerySearchResultProvider querySearchResultProvider = service.executeQueryPhase(new ShardSearchLocalRequest(indexShard.shardId(), 1, SearchType.DEFAULT, new SearchSourceBuilder(), new String[0], false, new AliasFilter(null, Strings.EMPTY_ARRAY), 1.0f), new SearchTask(123L, "", "", "", null));
                IntArrayList intCursors = new IntArrayList(1);
                intCursors.add(0);
                ShardFetchRequest req = new ShardFetchRequest(querySearchResultProvider.id(), intCursors, null);
                service.executeFetchPhase(req, new SearchTask(123L, "", "", "", null));
            } catch (AlreadyClosedException ex) {
                throw ex;
            } catch (IllegalStateException ex) {
                assertEquals("search context is already closed can't increment refCount current count [0]", ex.getMessage());
            } catch (SearchContextMissingException ex) {
            // that's fine
            }
        }
    } finally {
        running.set(false);
        thread.join();
        semaphore.acquire(Integer.MAX_VALUE);
    }
}
Also used : AliasFilter(org.elasticsearch.search.internal.AliasFilter) ShardSearchLocalRequest(org.elasticsearch.search.internal.ShardSearchLocalRequest) IndexService(org.elasticsearch.index.IndexService) Semaphore(java.util.concurrent.Semaphore) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchTask(org.elasticsearch.action.search.SearchTask) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) ShardFetchRequest(org.elasticsearch.search.fetch.ShardFetchRequest) CountDownLatch(java.util.concurrent.CountDownLatch) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) ExecutionException(java.util.concurrent.ExecutionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexResponse(org.elasticsearch.action.index.IndexResponse) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 13 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 14 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 15 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)

Aggregations

Semaphore (java.util.concurrent.Semaphore)447 Test (org.junit.Test)167 IOException (java.io.IOException)52 Context (android.content.Context)35 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)31 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)29 File (java.io.File)27 CountDownLatch (java.util.concurrent.CountDownLatch)25 ExecutionException (java.util.concurrent.ExecutionException)25 Intent (android.content.Intent)24 Handler (android.os.Handler)24 List (java.util.List)24 Map (java.util.Map)24 InvocationOnMock (org.mockito.invocation.InvocationOnMock)23 HazelcastInstance (com.hazelcast.core.HazelcastInstance)21 BroadcastReceiver (android.content.BroadcastReceiver)20 IntentFilter (android.content.IntentFilter)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 TimeoutException (java.util.concurrent.TimeoutException)19