Example usage for java.util.concurrent Semaphore release

List of usage examples for java.util.concurrent Semaphore release

Introduction

In this page you can find the example usage for java.util.concurrent Semaphore release.

Prototype

public void release() 

Source Link

Document

Releases a permit, returning it to the semaphore.

Usage

From source file:voldemort.store.routed.ThreadPoolRoutedStore.java

@Override
public void put(final ByteArray key, final Versioned<byte[]> versioned, final byte[] transforms)
        throws VoldemortException {
    long startNs = System.nanoTime();
    StoreUtils.assertValidKey(key);//from w  w  w . j a v  a  2  s  .c  om
    final List<Node> nodes = availableNodes(routingStrategy.routeRequest(key.get()));

    // quickly fail if there aren't enough nodes to meet the requirement
    final int numNodes = nodes.size();
    if (numNodes < this.storeDef.getRequiredWrites())
        throw new InsufficientOperationalNodesException("Only " + numNodes + " nodes in preference list, but "
                + this.storeDef.getRequiredWrites() + " writes required.");

    // A count of the number of successful operations
    final AtomicInteger successes = new AtomicInteger(0);

    // A list of thrown exceptions, indicating the number of failures
    final List<Exception> failures = Collections.synchronizedList(new ArrayList<Exception>(1));

    // If requiredWrites > 0 then do a single blocking write to the first
    // live node in the preference list if this node throws an
    // ObsoleteVersionException allow it to propagate
    Node master = null;
    int currentNode = 0;
    Versioned<byte[]> versionedCopy = null;
    for (; currentNode < numNodes; currentNode++) {
        Node current = nodes.get(currentNode);
        long startNsLocal = System.nanoTime();
        try {
            versionedCopy = incremented(versioned, current.getId());
            innerStores.get(current.getId()).put(key, versionedCopy, transforms);
            successes.getAndIncrement();
            recordSuccess(current, startNsLocal);
            master = current;
            break;
        } catch (UnreachableStoreException e) {
            recordException(current, startNsLocal, e);
            failures.add(e);
        } catch (VoldemortApplicationException e) {
            throw e;
        } catch (Exception e) {
            failures.add(e);
        }
    }

    if (successes.get() < 1)
        throw new InsufficientOperationalNodesException("No master node succeeded!",
                failures.size() > 0 ? failures.get(0) : null);
    else
        currentNode++;

    // A semaphore indicating the number of completed operations
    // Once inititialized all permits are acquired, after that
    // permits are released when an operation is completed.
    // semaphore.acquire(n) waits for n operations to complete
    final Versioned<byte[]> finalVersionedCopy = versionedCopy;
    final Semaphore semaphore = new Semaphore(0, false);
    // Add the operations to the pool
    int attempts = 0;
    for (; currentNode < numNodes; currentNode++) {
        attempts++;
        final Node node = nodes.get(currentNode);
        this.executor.execute(new Runnable() {

            @Override
            public void run() {
                long startNsLocal = System.nanoTime();
                try {
                    innerStores.get(node.getId()).put(key, finalVersionedCopy, transforms);
                    successes.incrementAndGet();
                    recordSuccess(node, startNsLocal);
                } catch (UnreachableStoreException e) {
                    recordException(node, startNsLocal, e);
                    failures.add(e);
                } catch (ObsoleteVersionException e) {
                    // ignore this completely here
                    // this means that a higher version was able
                    // to write on this node and should be termed as clean
                    // success.
                } catch (VoldemortApplicationException e) {
                    throw e;
                } catch (Exception e) {
                    logger.warn("Error in PUT on node " + node.getId() + "(" + node.getHost() + ")", e);
                    failures.add(e);
                } finally {
                    // signal that the operation is complete
                    semaphore.release();
                }
            }
        });
    }

    // Block until we get enough completions
    int blockCount = Math.min(storeDef.getPreferredWrites() - 1, attempts);
    boolean noTimeout = blockOnPut(startNs, semaphore, 0, blockCount, successes, storeDef.getPreferredWrites());

    if (successes.get() < storeDef.getRequiredWrites()) {
        /*
         * We don't have enough required writes, but we haven't timed out
         * yet, so block a little more if there are healthy nodes that can
         * help us achieve our target.
         */
        if (noTimeout) {
            int startingIndex = blockCount - 1;
            blockCount = Math.max(storeDef.getPreferredWrites() - 1, attempts);
            blockOnPut(startNs, semaphore, startingIndex, blockCount, successes, storeDef.getRequiredWrites());
        }
        if (successes.get() < storeDef.getRequiredWrites())
            throw new InsufficientOperationalNodesException(successes.get() + " writes succeeded, but "
                    + this.storeDef.getRequiredWrites() + " are required.", failures);
    }

    // Okay looks like it worked, increment the version for the caller
    VectorClock versionedClock = (VectorClock) versioned.getVersion();
    versionedClock.incrementVersion(master.getId(), time.getMilliseconds());
}

From source file:com.impetus.ankush2.cassandra.deployer.CassandraDeployer.java

/**
 * Function to update topology information on all nodes in cluster after add
 * or remove nodes//ww w.j a v  a  2s. c om
 */
private void updateTopologyFile() {
    Map<String, Map<String, Object>> nodes = new HashMap<String, Map<String, Object>>(
            componentConfig.getNodes());
    if (clusterConfig.getState().equals(Constant.Cluster.State.REMOVE_NODE)) {
        nodes = new HashMap<String, Map<String, Object>>(returnNodes());
    }

    final Semaphore semaphore = new Semaphore(nodes.size());
    try {
        for (final String host : nodes.keySet()) {
            semaphore.acquire();
            AppStoreWrapper.getExecutor().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        SSHExec connection = getConnection(host);
                        if (connection != null) {
                            editTopologyFile(connection, host);
                        }
                    } catch (AnkushException e) {
                        addClusterError(e.getMessage(), host, e);
                    } catch (Exception e) {
                        addClusterError("Could not update topology details.", host, e);
                    }

                    if (semaphore != null) {
                        semaphore.release();
                    }
                }
            });
        }
        semaphore.acquire(nodes.size());

    } catch (Exception e) {
        logger.error("Error in updating topology file.", e);
    }
}

From source file:voldemort.store.routed.RoutedStore.java

public void put(final ByteArray key, final Versioned<byte[]> versioned) throws VoldemortException {
    long startNs = System.nanoTime();
    StoreUtils.assertValidKey(key);// w  ww. ja v  a2 s  .co m
    final List<Node> nodes = availableNodes(routingStrategy.routeRequest(key.get()));

    // quickly fail if there aren't enough nodes to meet the requirement
    final int numNodes = nodes.size();
    if (numNodes < this.storeDef.getRequiredWrites())
        throw new InsufficientOperationalNodesException("Only " + numNodes + " nodes in preference list, but "
                + this.storeDef.getRequiredWrites() + " writes required.");

    // A count of the number of successful operations
    final AtomicInteger successes = new AtomicInteger(0);

    // A list of thrown exceptions, indicating the number of failures
    final List<Exception> failures = Collections.synchronizedList(new ArrayList<Exception>(1));

    // If requiredWrites > 0 then do a single blocking write to the first
    // live node in the preference list if this node throws an
    // ObsoleteVersionException allow it to propagate
    Node master = null;
    int currentNode = 0;
    Versioned<byte[]> versionedCopy = null;
    for (; currentNode < numNodes; currentNode++) {
        Node current = nodes.get(currentNode);
        long startNsLocal = System.nanoTime();
        try {
            versionedCopy = incremented(versioned, current.getId());
            innerStores.get(current.getId()).put(key, versionedCopy);
            successes.getAndIncrement();
            recordSuccess(current, startNsLocal);
            master = current;
            break;
        } catch (UnreachableStoreException e) {
            recordException(current, startNsLocal, e);
            failures.add(e);
        } catch (VoldemortApplicationException e) {
            throw e;
        } catch (Exception e) {
            failures.add(e);
        }
    }

    if (successes.get() < 1)
        throw new InsufficientOperationalNodesException("No master node succeeded!",
                failures.size() > 0 ? failures.get(0) : null);
    else
        currentNode++;

    // A semaphore indicating the number of completed operations
    // Once inititialized all permits are acquired, after that
    // permits are released when an operation is completed.
    // semaphore.acquire(n) waits for n operations to complete
    final Versioned<byte[]> finalVersionedCopy = versionedCopy;
    final Semaphore semaphore = new Semaphore(0, false);
    // Add the operations to the pool
    int attempts = 0;
    for (; currentNode < numNodes; currentNode++) {
        attempts++;
        final Node node = nodes.get(currentNode);
        this.executor.execute(new Runnable() {

            public void run() {
                long startNsLocal = System.nanoTime();
                try {
                    innerStores.get(node.getId()).put(key, finalVersionedCopy);
                    successes.incrementAndGet();
                    recordSuccess(node, startNsLocal);
                } catch (UnreachableStoreException e) {
                    recordException(node, startNsLocal, e);
                    failures.add(e);
                } catch (ObsoleteVersionException e) {
                    // ignore this completely here
                    // this means that a higher version was able
                    // to write on this node and should be termed as clean
                    // success.
                } catch (VoldemortApplicationException e) {
                    throw e;
                } catch (Exception e) {
                    logger.warn("Error in PUT on node " + node.getId() + "(" + node.getHost() + ")", e);
                    failures.add(e);
                } finally {
                    // signal that the operation is complete
                    semaphore.release();
                }
            }
        });
    }

    // Block until we get enough completions
    int blockCount = Math.min(storeDef.getPreferredWrites() - 1, attempts);
    boolean noTimeout = blockOnPut(startNs, semaphore, 0, blockCount, successes, storeDef.getPreferredWrites());

    if (successes.get() < storeDef.getRequiredWrites()) {
        /*
         * We don't have enough required writes, but we haven't timed out
         * yet, so block a little more if there are healthy nodes that can
         * help us achieve our target.
         */
        if (noTimeout) {
            int startingIndex = blockCount - 1;
            blockCount = Math.max(storeDef.getPreferredWrites() - 1, attempts);
            blockOnPut(startNs, semaphore, startingIndex, blockCount, successes, storeDef.getRequiredWrites());
        }
        if (successes.get() < storeDef.getRequiredWrites())
            throw new InsufficientOperationalNodesException(successes.get() + " writes succeeded, but "
                    + this.storeDef.getRequiredWrites() + " are required.", failures);
    }

    // Okay looks like it worked, increment the version for the caller
    VectorClock versionedClock = (VectorClock) versioned.getVersion();
    versionedClock.incrementVersion(master.getId(), time.getMilliseconds());
}

From source file:com.amazonaws.services.kinesis.clientlibrary.lib.worker.WorkerTest.java

/**
 * This test is testing the {@link Worker}'s shutdown behavior and by extension the behavior of
 * {@link ThreadPoolExecutor#shutdownNow()}. It depends on the thread pool sending an interrupt to the pool threads.
 * This behavior makes the test a bit racy, since we need to ensure a specific order of events.
 * //from  w  w  w  .j  av a 2 s .  c o  m
 * @throws Exception
 */
@Test
public final void testWorkerForcefulShutdown() throws Exception {
    final List<Shard> shardList = createShardListWithOneShard();
    final boolean callProcessRecordsForEmptyRecordList = true;
    final long failoverTimeMillis = 50L;
    final int numberOfRecordsPerShard = 10;

    final List<KinesisClientLease> initialLeases = new ArrayList<KinesisClientLease>();
    for (Shard shard : shardList) {
        KinesisClientLease lease = ShardSyncer.newKCLLease(shard);
        lease.setCheckpoint(ExtendedSequenceNumber.TRIM_HORIZON);
        initialLeases.add(lease);
    }

    final File file = KinesisLocalFileDataCreator.generateTempDataFile(shardList, numberOfRecordsPerShard,
            "normalShutdownUnitTest");
    final IKinesisProxy fileBasedProxy = new KinesisLocalFileProxy(file.getAbsolutePath());

    // Get executor service that will be owned by the worker, so we can get interrupts.
    ExecutorService executorService = getWorkerThreadPoolExecutor();

    // Make test case as efficient as possible.
    final CountDownLatch processRecordsLatch = new CountDownLatch(1);
    final AtomicBoolean recordProcessorInterrupted = new AtomicBoolean(false);
    when(v2RecordProcessorFactory.createProcessor()).thenReturn(v2RecordProcessor);
    final Semaphore actionBlocker = new Semaphore(1);
    final Semaphore shutdownBlocker = new Semaphore(1);

    actionBlocker.acquire();

    doAnswer(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            // Signal that record processor has started processing records.
            processRecordsLatch.countDown();

            // Block for some time now to test forceful shutdown. Also, check if record processor
            // was interrupted or not.
            final long startTimeMillis = System.currentTimeMillis();
            long elapsedTimeMillis = 0;

            LOG.info("Entering sleep @ " + startTimeMillis + " with elapsedMills: " + elapsedTimeMillis);
            shutdownBlocker.acquire();
            try {
                actionBlocker.acquire();
            } catch (InterruptedException e) {
                LOG.info("Sleep interrupted @ " + System.currentTimeMillis() + " elapsedMillis: "
                        + (System.currentTimeMillis() - startTimeMillis));
                recordProcessorInterrupted.getAndSet(true);
            }
            shutdownBlocker.release();
            elapsedTimeMillis = System.currentTimeMillis() - startTimeMillis;
            LOG.info(
                    "Sleep completed @ " + System.currentTimeMillis() + " elapsedMillis: " + elapsedTimeMillis);

            return null;
        }
    }).when(v2RecordProcessor).processRecords(any(ProcessRecordsInput.class));

    WorkerThread workerThread = runWorker(shardList, initialLeases, callProcessRecordsForEmptyRecordList,
            failoverTimeMillis, numberOfRecordsPerShard, fileBasedProxy, v2RecordProcessorFactory,
            executorService, nullMetricsFactory);

    // Only sleep for time that is required.
    processRecordsLatch.await();

    // Make sure record processor is initialized and processing records.
    verify(v2RecordProcessorFactory, times(1)).createProcessor();
    verify(v2RecordProcessor, times(1)).initialize(any(InitializationInput.class));
    verify(v2RecordProcessor, atLeast(1)).processRecords(any(ProcessRecordsInput.class));
    verify(v2RecordProcessor, times(0)).shutdown(any(ShutdownInput.class));

    workerThread.getWorker().shutdown();
    workerThread.join();

    Assert.assertTrue(workerThread.getState() == State.TERMINATED);
    // Shutdown should not be called in this case because record processor is blocked.
    verify(v2RecordProcessor, times(0)).shutdown(any(ShutdownInput.class));

    //
    // Release the worker thread
    //
    actionBlocker.release();
    //
    // Give the worker thread time to execute it's interrupted handler.
    //
    shutdownBlocker.tryAcquire(100, TimeUnit.MILLISECONDS);
    //
    // Now we can see if it was actually interrupted. It's possible it wasn't and this will fail.
    //
    assertThat(recordProcessorInterrupted.get(), equalTo(true));
}

From source file:com.impetus.ankush2.cassandra.deployer.CassandraDeployer.java

/**
 * Function to update seed node information after add or remove nodes on all
 * nodes in a cluster/*from w  w  w  . jav a 2 s. co m*/
 */
private void updateSeedNodeValue() {
    // Seeds update command
    String cassandraYaml = advanceConf.get(CassandraConstants.ClusterProperties.CONF_DIR)
            + CassandraConstants.Cassandra_Configuration_Files.CASSANDRA_YAML;
    final String command = "sed -i -e 's/- seeds: \".*$/- seeds: \"" + getSeedNodeString() + "\"/' "
            + cassandraYaml;

    Map<String, Map<String, Object>> nodes = new HashMap<String, Map<String, Object>>(
            componentConfig.getNodes());
    if (clusterConfig.getState().equals(Constant.Cluster.State.REMOVE_NODE)) {
        nodes = new HashMap<String, Map<String, Object>>(returnNodes());
    }

    final Semaphore semaphore = new Semaphore(nodes.size());
    try {
        for (final String host : nodes.keySet()) {
            semaphore.acquire();
            AppStoreWrapper.getExecutor().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        SSHExec connection = getConnection(host);
                        if (connection != null) {
                            execCustomtask(command, connection, host, "Could not update seeds value");
                        }
                    } catch (AnkushException e) {
                        addClusterError(e.getMessage(), host, e);
                    } catch (Exception e) {
                        addClusterError("Could not update seed node value in cassandra.yaml file.", host, e);
                    }
                    if (semaphore != null) {
                        semaphore.release();
                    }
                }
            });
        }
        semaphore.acquire(nodes.size());

    } catch (Exception e) {
        logger.error("Error in editing seeds values.", e);
    }
}

From source file:org.apache.sshd.common.forward.PortForwardingLoadTest.java

@Test
@SuppressWarnings("checkstyle:nestedtrydepth")
public void testLocalForwardingPayload() throws Exception {
    final int numIterations = 100;
    final String payloadTmpData = "This is significantly longer Test Data. This is significantly "
            + "longer Test Data. This is significantly longer Test Data. This is significantly "
            + "longer Test Data. This is significantly longer Test Data. This is significantly "
            + "longer Test Data. This is significantly longer Test Data. This is significantly "
            + "longer Test Data. This is significantly longer Test Data. This is significantly "
            + "longer Test Data. ";
    StringBuilder sb = new StringBuilder(payloadTmpData.length() * 1000);
    for (int i = 0; i < 1000; i++) {
        sb.append(payloadTmpData);/*from w  w  w .  j a  v  a2s.  c  o  m*/
    }
    final String payload = sb.toString();

    Session session = createSession();
    try (ServerSocket ss = new ServerSocket()) {
        ss.setReuseAddress(true);
        ss.bind(new InetSocketAddress((InetAddress) null, 0));
        int forwardedPort = ss.getLocalPort();
        int sinkPort = session.setPortForwardingL(0, TEST_LOCALHOST, forwardedPort);
        final AtomicInteger conCount = new AtomicInteger(0);
        final Semaphore iterationsSignal = new Semaphore(0);
        Thread tAcceptor = new Thread(getCurrentTestName() + "Acceptor") {
            @SuppressWarnings("synthetic-access")
            @Override
            public void run() {
                try {
                    byte[] buf = new byte[8192];
                    log.info("Started...");
                    for (int i = 0; i < numIterations; ++i) {
                        try (Socket s = ss.accept()) {
                            conCount.incrementAndGet();

                            try (InputStream sockIn = s.getInputStream();
                                    ByteArrayOutputStream baos = new ByteArrayOutputStream()) {

                                while (baos.size() < payload.length()) {
                                    int l = sockIn.read(buf);
                                    if (l < 0) {
                                        break;
                                    }
                                    baos.write(buf, 0, l);
                                }

                                assertEquals("Mismatched received data at iteration #" + i, payload,
                                        baos.toString());

                                try (InputStream inputCopy = new ByteArrayInputStream(baos.toByteArray());
                                        OutputStream sockOut = s.getOutputStream()) {

                                    while (true) {
                                        int l = sockIn.read(buf);
                                        if (l < 0) {
                                            break;
                                        }
                                        sockOut.write(buf, 0, l);
                                    }
                                }
                            }
                        }
                        log.info("Finished iteration {}", i);
                        iterationsSignal.release();
                    }
                    log.info("Done");
                } catch (Exception e) {
                    log.error("Failed to complete run loop", e);
                }
            }
        };
        tAcceptor.start();
        Thread.sleep(TimeUnit.SECONDS.toMillis(1L));

        byte[] buf = new byte[8192];
        byte[] bytes = payload.getBytes(StandardCharsets.UTF_8);
        for (int i = 0; i < numIterations; i++) {
            log.info("Iteration {}", i);
            try (Socket s = new Socket(TEST_LOCALHOST, sinkPort); OutputStream sockOut = s.getOutputStream()) {

                s.setSoTimeout((int) FactoryManager.DEFAULT_NIO2_MIN_WRITE_TIMEOUT);

                sockOut.write(bytes);
                sockOut.flush();

                try (InputStream sockIn = s.getInputStream();
                        ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length)) {
                    while (baos.size() < payload.length()) {
                        int l = sockIn.read(buf);
                        if (l < 0) {
                            break;
                        }
                        baos.write(buf, 0, l);
                    }
                    assertEquals("Mismatched payload at iteration #" + i, payload, baos.toString());
                }
            } catch (Exception e) {
                log.error("Error in iteration #" + i, e);
            }
        }

        try {
            assertTrue("Failed to await pending iterations=" + numIterations,
                    iterationsSignal.tryAcquire(numIterations, numIterations, TimeUnit.SECONDS));
        } finally {
            session.delPortForwardingL(sinkPort);
        }

        ss.close();
        tAcceptor.join(TimeUnit.SECONDS.toMillis(11L));
    } finally {
        session.disconnect();
    }
}

From source file:org.apache.pulsar.compaction.TwoPhaseCompactor.java

private void phaseTwoLoop(RawReader reader, MessageId to, Map<String, MessageId> latestForKey, LedgerHandle lh,
        Semaphore outstanding, CompletableFuture<Void> promise) {
    reader.readNextAsync().whenCompleteAsync((m, exception) -> {
        if (exception != null) {
            promise.completeExceptionally(exception);
            return;
        } else if (promise.isDone()) {
            return;
        }/*from   www  .jav  a 2s  . c o  m*/
        MessageId id = m.getMessageId();
        Optional<RawMessage> messageToAdd = Optional.empty();
        if (RawBatchConverter.isReadableBatch(m)) {
            try {
                messageToAdd = RawBatchConverter.rebatchMessage(m,
                        (key, subid) -> latestForKey.get(key).equals(subid));
            } catch (IOException ioe) {
                log.info("Error decoding batch for message {}. Whole batch will be included in output", id,
                        ioe);
                messageToAdd = Optional.of(m);
            }
        } else {
            Pair<String, Integer> keyAndSize = extractKeyAndSize(m);
            MessageId msg;
            if (keyAndSize == null) { // pass through messages without a key
                messageToAdd = Optional.of(m);
            } else if ((msg = latestForKey.get(keyAndSize.getLeft())) != null && msg.equals(id)) { // consider message only if present into latestForKey map
                if (keyAndSize.getRight() <= 0) {
                    promise.completeExceptionally(new IllegalArgumentException(
                            "Compaction phase found empty record from sorted key-map"));
                }
                messageToAdd = Optional.of(m);
            } else {
                m.close();
                // Reached to last-id and phase-one found it deleted-message while iterating on ledger so, not
                // present under latestForKey. Complete the compaction.
                if (to.equals(id)) {
                    promise.complete(null);
                }
            }
        }

        messageToAdd.ifPresent((toAdd) -> {
            try {
                outstanding.acquire();
                CompletableFuture<Void> addFuture = addToCompactedLedger(lh, toAdd)
                        .whenComplete((res, exception2) -> {
                            outstanding.release();
                            if (exception2 != null) {
                                promise.completeExceptionally(exception2);
                            }
                        });
                if (to.equals(id)) {
                    addFuture.whenComplete((res, exception2) -> {
                        if (exception2 == null) {
                            promise.complete(null);
                        }
                    });
                }
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                promise.completeExceptionally(ie);
            }
        });
        phaseTwoLoop(reader, to, latestForKey, lh, outstanding, promise);
    }, scheduler);
}

From source file:org.commoncrawl.service.queryserver.query.DomainListQuery.java

@Override
protected long executeRemote(final FileSystem fileSystem, final Configuration conf, EventLoop eventLoop,
        SlaveDatabaseIndex instanceIndex, File tempFirDir,
        QueryProgressCallback<DomainListQueryInfo, Text, SubDomainMetadata> progressCallback)
        throws IOException {

    int shardsProcessed = 0;

    // ok create a semaphore for the number of shard we are going to query ...
    final Semaphore semaphore = new Semaphore(-(getCommonQueryInfo().getRelevantShardIds().size() - 1));
    // and create a record count array 
    final long recordCounts[] = new long[getCommonQueryInfo().getRelevantShardIds().size()];
    final IOException exceptions[] = new IOException[getCommonQueryInfo().getRelevantShardIds().size()];

    int threadIdx = 0;
    // ok dispatch queries for each shard we are responsible for ... 
    for (int shardId : getCommonQueryInfo().getRelevantShardIds()) {

        final int currentShardId = shardId;
        final int currentThreadIdx = threadIdx++;

        Thread subQueryThread = new Thread(new Runnable() {

            @Override//from   w ww  . j  a v  a 2s .  c o  m
            public void run() {
                Path shardOutputPath = getHDFSQueryResultsFilePathForShard(currentShardId);

                LOG.info("Execute Remote for Query:" + getQueryId() + " for shardId:" + currentShardId
                        + "  Creating spill file @:" + shardOutputPath);

                try {
                    // create SequenceFile Spill Writer ... 
                    SequenceFileSpillWriter<Text, SubDomainMetadata> spillWriter = new SequenceFileSpillWriter<Text, SubDomainMetadata>(
                            fileSystem, conf, shardOutputPath, Text.class, SubDomainMetadata.class, null, true);
                    try {
                        LOG.info("Execute Remote for Query:" + getQueryId()
                                + " calling executeDomainListQuery on index");
                        // scan index for matching patterns ... spill into writer ...
                        recordCounts[currentThreadIdx] += _slaveDatabaseIndex.queryDomainsGivenPattern(
                                getQueryData().getSearchPattern(), currentShardId, spillWriter);
                        LOG.info("Execute Remote for Query:" + getQueryId()
                                + " executeDomainListQuery returned:" + recordCounts[currentThreadIdx]);
                    } finally {
                        spillWriter.close();
                        // increment semaphore count 
                        semaphore.release();
                    }
                } catch (IOException e) {
                    LOG.error("Execute Remote for Query:" + getQueryId()
                            + " executeDomainListQuery failed with error:"
                            + CCStringUtils.stringifyException(e));
                    exceptions[currentThreadIdx] = e;
                }
            }
        });
        subQueryThread.start();
    }

    // ok block until all queries are complete
    LOG.info("Query:" + getQueryId() + " Waiting on Worker Threads");
    semaphore.acquireUninterruptibly();
    LOG.info("Query:" + getQueryId() + " All Threads Compelted");

    for (IOException e : exceptions) {
        if (e != null) {
            LOG.error(
                    "Query:" + getQueryId() + " Failed with Exception:" + CCStringUtils.stringifyException(e));
            throw e;
        }
    }
    long cumilativeRecordCount = 0L;
    for (long recordCount : recordCounts)
        cumilativeRecordCount += recordCount;
    return cumilativeRecordCount;
}

From source file:android.webkit.cts.WebViewTest.java

private void doSaveWebArchive(String baseName, boolean autoName, final String expectName) throws Throwable {
    final Semaphore saving = new Semaphore(0);
    ValueCallback<String> callback = new ValueCallback<String>() {
        @Override/*  w w w  . j av a2  s  . c  o m*/
        public void onReceiveValue(String savedName) {
            assertEquals(expectName, savedName);
            saving.release();
        }
    };

    mOnUiThread.saveWebArchive(baseName, autoName, callback);
    assertTrue(saving.tryAcquire(TEST_TIMEOUT, TimeUnit.MILLISECONDS));
}

From source file:org.knime.al.nodes.score.novelty.localnoveltyscorer.LocalNoveltyScorer.java

public double[] calculateNoveltyScores() throws Exception {

    final ThreadPool pool = KNIMEConstants.GLOBAL_THREAD_POOL;
    final int procCount = (int) (Runtime.getRuntime().availableProcessors() * (2.0 / 3));
    final Semaphore semaphore = new Semaphore(procCount);

    final int numTestSamples = m_globalKernelMatrix.getColumnDimension();
    final NoveltyScoreCalculationCallable[] nct = new NoveltyScoreCalculationCallable[numTestSamples];
    for (int i = 0; i < numTestSamples; i++) {
        nct[i] = new NoveltyScoreCalculationCallable(i, semaphore, m_numNeighbors, m_trainingKernelMatrix,
                m_globalKernelMatrix, m_labels, m_normalize);
    }//from   www  . j  ava  2 s.co m
    final Future<?>[] scores = new Future<?>[numTestSamples];
    final KNIMETimer timer = KNIMETimer.getInstance();
    final TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            try {
                m_exec.checkCanceled();
            } catch (final CanceledExecutionException ce) {
                for (int i = 0; i < scores.length; i++) {
                    if (scores[i] != null) {
                        scores[i].cancel(true);
                    }
                }
                super.cancel();
            }

        }
    };
    timer.scheduleAtFixedRate(timerTask, 0, 3000);
    double progCounter = 0;
    for (int i = 0; i < numTestSamples; i++) {
        try {
            m_exec.checkCanceled();
        } catch (final Exception e) {
            for (int j = 0; j < i; j++) {
                if (scores[j] != null) {
                    scores[j].cancel(true);
                }
            }
            timerTask.cancel();
            throw e;
        }
        semaphore.acquire();
        scores[i] = pool.enqueue(nct[i]);
        m_exec.setProgress(progCounter / (2 * numTestSamples),
                "Local novelty score calculation started (" + i + "/" + numTestSamples + ")");
        progCounter += 1;
    }
    final double[] result = new double[numTestSamples];

    for (int i = 0; i < numTestSamples; i++) {
        semaphore.acquire();
        try {
            m_exec.checkCanceled();
            result[i] = (Double) scores[i].get();
            nct[i].ok();
        } catch (final Exception e) {
            for (int j = 0; j < scores.length; j++) {
                scores[j].cancel(true);
            }
            timerTask.cancel();
            throw e;

        }
        m_exec.setProgress(progCounter / (2 * numTestSamples),
                "Local novelty score calculated (" + i + "/" + numTestSamples + ")");
        progCounter += 1;
        semaphore.release();
    }

    timerTask.cancel();

    return result;
}