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:com.impetus.ankush2.cassandra.monitor.CassandraClusterMonitor.java

private void deleteConfigFileParam(final Parameter parameter, final String fileName, final String fileType,
        boolean editNodeParam) {
    final String propertyName = parameter.getName();
    final String propertyFilePath = advanceConf.get(CassandraConstants.ClusterProperties.CONF_DIR) + fileName;

    if (editNodeParam) {
        String host = (String) parameterMap.get(Constant.Keys.HOST);
        deleteParameter(host, propertyName, propertyFilePath, fileType, fileName);
    } else {/*from  w  ww  . j a v a2  s.c o  m*/
        final Semaphore semaphore = new Semaphore(componentConfig.getNodes().size());
        try {
            for (final String host : componentConfig.getNodes().keySet()) {
                semaphore.acquire();
                AppStoreWrapper.getExecutor().execute(new Runnable() {
                    @Override
                    public void run() {
                        deleteParameter(host, propertyName, propertyFilePath, fileType, fileName);
                        if (semaphore != null) {
                            semaphore.release();
                        }

                    }
                });
            }
            semaphore.acquire(componentConfig.getNodes().size());
        } catch (Exception e) {
            addAndLogError("Error in updating config file params...");
        }
    }
}

From source file:mitm.common.tools.SendMail.java

private void sendMultiThreaded(final MailTransport mailSender, final MimeMessage message,
        final Address[] recipients) throws InterruptedException {
    ExecutorService threadPool = Executors.newCachedThreadPool();

    final Semaphore semaphore = new Semaphore(threads, true);

    final long startTime = System.currentTimeMillis();

    for (int i = 1; i <= count; i++) {
        long threadStart = System.currentTimeMillis();

        semaphore.acquireUninterruptibly();

        threadPool.execute(new Runnable() {
            @Override/*from   w  w w .  j  a  va2 s  .  c o m*/
            public void run() {
                try {
                    MimeMessage clone = MailUtils.cloneMessage(message);

                    int sent = sentCount.incrementAndGet();

                    if (uniqueFrom) {
                        Address[] froms = clone.getFrom();

                        if (froms != null && froms.length > 0) {
                            clone.setFrom(
                                    new InternetAddress(sent + EmailAddressUtils.getEmailAddress(froms[0])));
                        }
                    }

                    mailSender.sendMessage(clone, recipients);

                    long timePassed = DateTimeUtils
                            .millisecondsToSeconds(System.currentTimeMillis() - startTime);

                    StrBuilder sb = new StrBuilder();

                    sb.append("Message\t" + sent + "\tsent.");

                    if (timePassed > 0) {
                        float msgPerSec = (float) sent / timePassed;

                        sb.append("\tmessages/second\t" + String.format("%.2f", msgPerSec));
                    }

                    logger.info(sb.toString());
                } catch (MessagingException e) {
                    logger.error("Error sending message.", e);
                } finally {
                    semaphore.release();
                }
            }
        });

        if (forceQuit.get()) {
            break;
        }

        if (throtllingSemaphore != null) {
            /* for throttling the sending of emails */
            throtllingSemaphore.acquire();
        } else {
            /* no throttling so use delay */
            long sleepTime = delay - (System.currentTimeMillis() - threadStart);

            if (sleepTime > 0) {
                Thread.sleep(sleepTime);
            }
        }
    }

    threadPool.shutdown();
    threadPool.awaitTermination(30, TimeUnit.SECONDS);

    waitForReceiveThreads();

    logger.info("Total sent: " + sentCount.intValue() + ". Total time: "
            + DateTimeUtils.millisecondsToSeconds(System.currentTimeMillis() - startTime) + " (sec.)");
}

From source file:com.impetus.ankush2.cassandra.monitor.CassandraClusterMonitor.java

private void editConfigFileParam(final Parameter parameter, final String fileName, final String loggedUser,
        final String fileType, boolean editNodeParam) {
    final String propertyName = parameter.getName();
    final String newValue = parameter.getValue();
    final String propertyFilePath = advanceConf.get(CassandraConstants.ClusterProperties.CONF_DIR) + fileName;
    if (editNodeParam) {
        String host = (String) parameterMap.get(Constant.Keys.HOST);
        editParameter(host, propertyName, newValue, propertyFilePath, fileType, loggedUser, fileName);
    } else {/*from   ww w.  j a v  a  2  s  .c  o  m*/
        final Semaphore semaphore = new Semaphore(componentConfig.getNodes().size());
        try {
            for (final String host : componentConfig.getNodes().keySet()) {
                semaphore.acquire();
                AppStoreWrapper.getExecutor().execute(new Runnable() {
                    @Override
                    public void run() {
                        editParameter(host, propertyName, newValue, propertyFilePath, fileType, loggedUser,
                                fileName);
                        if (semaphore != null) {
                            semaphore.release();
                        }
                    }
                });
            }
            semaphore.acquire(componentConfig.getNodes().size());
        } catch (Exception e) {
            addAndLogError("Error in updating config file params...");
        }
    }
}

From source file:com.impetus.ankush2.cassandra.monitor.CassandraClusterMonitor.java

private void addConfigFileParam(final Parameter parameter, final String fileName, final String loggedUser,
        final String fileType, boolean editNodeParam) {

    final String propertyName = parameter.getName();
    final String propertyValue = parameter.getValue();
    // get server.properties file path
    final String propertyFilePath = advanceConf.get(CassandraConstants.ClusterProperties.CONF_DIR) + fileName;
    if (editNodeParam) {
        String host = (String) parameterMap.get(Constant.Keys.HOST);
        addParameter(host, propertyName, propertyValue, propertyFilePath, fileType, loggedUser, fileName);
    } else {/*from   w  ww  . j  av  a  2  s  .c om*/
        final Semaphore semaphore = new Semaphore(componentConfig.getNodes().size());
        try {
            // iterate over all the nodes.
            for (final String host : componentConfig.getNodes().keySet()) {
                semaphore.acquire();
                AppStoreWrapper.getExecutor().execute(new Runnable() {
                    @Override
                    public void run() {
                        addParameter(host, propertyName, propertyValue, propertyFilePath, fileType, loggedUser,
                                fileName);
                        if (semaphore != null) {
                            semaphore.release();
                        }

                    }
                });
            }
            semaphore.acquire(componentConfig.getNodes().size());
        } catch (Exception e) {
            addAndLogError("Error in updating config file params...");
        }
    }
}

From source file:com.yahoo.pulsar.broker.service.ServerCnx.java

@Override
protected void handlePartitionMetadataRequest(CommandPartitionedTopicMetadata partitionMetadata) {
    final long requestId = partitionMetadata.getRequestId();
    final String topic = partitionMetadata.getTopic();
    if (log.isDebugEnabled()) {
        log.debug("[{}] Received PartitionMetadataLookup from {} for {}", topic, remoteAddress, requestId);
    }/*w  w  w. j  a v  a2s  .c  om*/
    final Semaphore lookupSemaphore = service.getLookupRequestSemaphore();
    if (lookupSemaphore.tryAcquire()) {
        getPartitionedTopicMetadata(getBrokerService().pulsar(), getRole(), DestinationName.get(topic))
                .handle((metadata, ex) -> {
                    if (ex == null) {
                        int partitions = metadata.partitions;
                        ctx.writeAndFlush(Commands.newPartitionMetadataResponse(partitions, requestId));
                    } else {
                        if (ex instanceof PulsarClientException) {
                            log.warn("Failed to authorize {} at [{}] on topic {} : {}", getRole(),
                                    remoteAddress, topic, ex.getMessage());
                            ctx.writeAndFlush(Commands.newPartitionMetadataResponse(
                                    ServerError.AuthorizationError, ex.getMessage(), requestId));
                        } else {
                            log.warn("Failed to get Partitioned Metadata [{}] {}: {}", remoteAddress, topic,
                                    ex.getMessage(), ex);
                            ctx.writeAndFlush(Commands.newPartitionMetadataResponse(ServerError.ServiceNotReady,
                                    ex.getMessage(), requestId));
                        }
                    }
                    lookupSemaphore.release();
                    return null;
                });
    } else {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Failed Partition-Metadata lookup due to too many lookup-requets {}", remoteAddress,
                    topic);
        }
        ctx.writeAndFlush(newLookupResponse(ServerError.TooManyRequests,
                "Failed due to too many pending lookup requests", requestId));
    }
}

From source file:com.alibaba.napoli.client.benchmark.NapoliNormalQueueTest.java

@Test
public void sendMessageWithSenderStoreEnableTest() throws Exception {

    log.info("start to execute sendMessageWithSenderStoreEnableTest");
    long beginQueueSize = JmxUtil.getQueueSize(sendConnector.getAddress(), queueName);

    qSender = new DefaultAsyncSender();
    qSender.setConnector(sendConnector);
    qSender.setName(queueName);//from  w w  w. j av  a  2 s . c om

    qSender.setStoreEnable(true);
    qSender.setReprocessInterval(10000 * 1000 * 1000);

    qSender.init();

    int tc = 10;

    log.info("yanny requestcount = " + System.getProperty("requestCount") + ", begin queue size is "
            + beginQueueSize);
    final int tp = Integer.parseInt(System.getProperty("requestCount", "20"));
    final Semaphore semaphore = new Semaphore(tc);
    final AtomicInteger sumCount = new AtomicInteger();

    final AtomicInteger requestCount = new AtomicInteger();
    long startTime = System.currentTimeMillis();
    log.info("Yanny start send request " + startTime);

    for (int i = 0; i < tc; i++) {
        Thread t = new Thread("thread--" + i) {
            public void run() {
                try {
                    //?tringap??Serializable
                    semaphore.acquire();
                    Person person = new Person();

                    person.setLoginName("superman");
                    person.setEmail("sm@1.com");
                    person.setPenName("pname");
                    person.setStatus(PersonStatus.ENABLED);

                    for (int j = 0; j < tp; j++) {
                        //      log.info("hello");
                        int id = requestCount.incrementAndGet();
                        person.setPersonId("" + id);

                        //?? ??true???alse                        
                        boolean result = qSender.send(person);
                        if (!result) {
                            log.info("----------------send to queue " + "result is false. personid=" + j);
                        } else {
                            sumCount.incrementAndGet();
                        }
                    }
                } catch (Throwable t) {
                    t.printStackTrace();
                } finally {
                    semaphore.release();
                }
            }
        };
        t.start();
    }

    while (semaphore.availablePermits() != tc) {
        Thread.sleep(100);
    }

    int totalRequest = tc * tp;

    long endTime = System.currentTimeMillis();
    log.info("yanny: send " + totalRequest + " message, take " + (endTime - startTime) + " milseconds");

    JmxUtil.waitTillQueueSizeAsTarget(sendConnector.getAddress(), queueName, beginQueueSize);

    endTime = System.currentTimeMillis();

    String errorMessage = "";

    long qBdbCount = NapoliTestUtil.getStoreSize(sendConnector.getSenderKVStore(qSender.getName()));

    log.info("yanny totalRequest " + totalRequest + " send queue success " + sumCount + " local store count:"
            + qBdbCount + " queue received " + qWorker.getAccessNum() + " take " + (endTime - startTime)
            + " milseconds");

    log.info(initConsumeMessage);

    log.info("NapoliNormalQueueTest's success=" + qWorker.getAccessNum() + " bdb's size=" + qBdbCount);

    //with store enabled, all send should succeed.
    if (qSender.getStoreEnable()) {
        if (sumCount.get() != totalRequest) {
            errorMessage += ";with store enabled, all send should return success, but not equal now. send succeed "
                    + sumCount.get() + "; total request:" + totalRequest;
        }
    } else {
        if (sumCount.get() < totalRequest * 0.95) {
            errorMessage += ";with store disabled, expected more than 95% message send succeed, total request:"
                    + totalRequest + "; send succeed " + sumCount.get();
        }
    }

    if (sumCount.get() < qWorker.getAccessNum()) {
        errorMessage += ";queue should not have success messages more than send succeed" + sumCount.get()
                + " (success " + qWorker.getAccessNum() + ")";
    }

    if ((sumCount.get() - qBdbCount) > qWorker.getAccessNum()) {
        errorMessage += ";queue received message (" + qWorker.getAccessNum()
                + ") less than send succeed - local stored message, message lost "
                + (sumCount.get() - qBdbCount);
    }

    int allowedDiff = (int) Math.round(sumCount.get() * 0.001);

    if (((qWorker.getAccessNum() + qBdbCount) - sumCount.get()) > allowedDiff) {
        errorMessage += "queue received message should not have more than send succeed + " + allowedDiff
                + " than allowed (0.1%), gap " + ((qWorker.getAccessNum() + qBdbCount) - sumCount.get());
    }

    assertTrue(errorMessage, errorMessage.equals(""));

    verify(napoliSenderStat, atMost(qWorker.getAccessNum())).sendSuccess(anyLong(), anyLong());
    verify(napoliSenderStat, atLeast((int) (sumCount.get() - qBdbCount))).sendSuccess(anyLong(), anyLong());
    verify(napoliSenderStat, times((int) qBdbCount)).sendFailure(anyLong(), anyLong());

    verify(napoliReceiverStat, times((int) qWorker.getAccessNum())).receiveSuccess(anyLong(), anyLong());
}

From source file:com.alibaba.napoli.client.benchmark.NapoliNormalQueueTest.java

@Test
public void sendMessageWithSenderStoreDisabledTest() throws Exception {

    log.info("start to execute sendMessageWithSenderStoreDisabledTest");

    long beginQueueSize = JmxUtil.getQueueSize(sendConnector.getAddress(), queueName);

    qSender = new DefaultAsyncSender();
    qSender.setConnector(sendConnector);
    qSender.setName(queueName);//from   w  w  w.  jav a2s.c  o m

    qSender.setStoreEnable(false);
    qSender.setReprocessInterval(10000 * 1000 * 1000);

    qSender.init();

    int tc = 10;
    log.info("yanny requestcount = " + System.getProperty("requestCount"));
    final int tp = Integer.parseInt(System.getProperty("requestCount", "20"));
    final Semaphore semaphore = new Semaphore(tc);
    final AtomicInteger sumCount = new AtomicInteger();

    final AtomicInteger requestCount = new AtomicInteger();
    long startTime = System.currentTimeMillis();
    log.info("Yanny start send request " + startTime);

    for (int i = 0; i < tc; i++) {
        Thread t = new Thread("thread--" + i) {
            public void run() {
                try {
                    //?tringap??Serializable
                    semaphore.acquire();
                    Person person = new Person();

                    person.setLoginName("superman");
                    person.setEmail("sm@1.com");
                    person.setPenName("pname");
                    person.setStatus(PersonStatus.ENABLED);

                    for (int j = 0; j < tp; j++) {
                        //      log.info("hello");
                        int id = requestCount.incrementAndGet();
                        person.setPersonId("" + id);

                        //?? ??true???alse                        
                        boolean result = qSender.send(person);
                        if (!result) {
                            log.info("----------------send to queue " + "result is false. personid=" + j);
                        } else {
                            sumCount.incrementAndGet();
                        }
                    }
                } catch (Throwable t) {
                    t.printStackTrace();
                } finally {
                    semaphore.release();
                }
            }
        };
        t.start();
    }

    while (semaphore.availablePermits() != tc) {
        Thread.sleep(100);
    }
    int totalRequest = tc * tp;

    long endTime = System.currentTimeMillis();
    log.info("yanny: send " + totalRequest + " message, take " + (endTime - startTime) + " milseconds");

    JmxUtil.waitTillQueueSizeAsTarget(sendConnector.getAddress(), queueName, beginQueueSize);

    endTime = System.currentTimeMillis();

    String errorMessage = "";

    long qBdbCount = NapoliTestUtil.getStoreSize(sendConnector.getSenderKVStore(qSender.getName()));
    log.info("yanny totalRequest " + totalRequest + " send queue success " + sumCount + " local store count:"
            + qBdbCount + " queue received " + qWorker.getAccessNum() + " take " + (endTime - startTime)
            + " milseconds");

    log.info(initConsumeMessage);

    log.info("NapoliNormalQueueTest's success=" + qWorker.getAccessNum() + " bdb's size=" + qBdbCount);

    if (qBdbCount > 0) {
        errorMessage += ";with store disabled, local store count should be empty, but is " + qBdbCount;
    }

    //with store enabled, all send should succeed.
    if (qSender.getStoreEnable()) {
        if (sumCount.get() != totalRequest) {
            errorMessage += ";with store enabled, all send should return success, but not equal now. send succeed "
                    + sumCount.get() + "; total request:" + totalRequest;
        }
    } else {
        if (sumCount.get() < totalRequest * 0.95) {
            errorMessage += ";with store disabled, expected more than 95% message send succeed, total request:"
                    + totalRequest + "; send succeed " + sumCount.get();
        }
    }

    //?????otalRequest,??>=sum
    if (totalRequest < qWorker.getAccessNum()) {
        errorMessage += ";queue should not have success messages more than send succeed" + sumCount.get()
                + " (success " + qWorker.getAccessNum() + ")";
    }

    //?qBdbCount?
    if ((sumCount.get() - qBdbCount) > qWorker.getAccessNum()) {
        errorMessage += ";queue received message (" + qWorker.getAccessNum()
                + ") less than send succeed - local stored message, message lost "
                + (sumCount.get() - qBdbCount);
    }

    int allowedDiff = (int) Math.round(sumCount.get() * 0.001);

    if (((qWorker.getAccessNum() + qBdbCount) - sumCount.get()) > allowedDiff) {
        errorMessage += "queue received message should not have more than send succeed + " + allowedDiff
                + " than allowed (0.1%), gap " + ((qWorker.getAccessNum() + qBdbCount) - sumCount.get());
    }

    assertTrue(errorMessage, errorMessage.equals(""));

    verify(napoliSenderStat, atMost(qWorker.getAccessNum())).sendSuccess(anyLong(), anyLong());
    verify(napoliSenderStat, atLeast((int) (sumCount.get() - qBdbCount))).sendSuccess(anyLong(), anyLong());
    verify(napoliSenderStat, times(totalRequest - sumCount.get())).sendFalse(anyLong(), anyLong());

    verify(napoliSenderStat, times((int) qBdbCount)).sendFailure(anyLong(), anyLong());

    verify(napoliReceiverStat, times((int) qWorker.getAccessNum())).receiveSuccess(anyLong(), anyLong());
}

From source file:org.commoncrawl.service.listcrawler.ProxyServlet.java

private static void queueQueryMasterURLRequest(final String targetURL, final long urlFingerprint,
        final AsyncResponse responseData, final Semaphore completionSemaphore, final long timeoutInMS,
        final boolean skipHTTPFetch) {
    ContentQueryRPCInfo rpcQueryInfo = new ContentQueryRPCInfo();
    //TODO:UNFORTUNATE HACK 
    GoogleURL canonicalURL = new GoogleURL(targetURL);
    rpcQueryInfo.setUrl(canonicalURL.getCanonicalURL());

    try {//from   w ww .  j  ava2s .c  om
        ProxyServer.getSingleton().getQueryMasterStub().doContentQuery(rpcQueryInfo,
                new AsyncRequest.Callback<ContentQueryRPCInfo, ContentQueryRPCResult>() {

                    @Override
                    public void requestComplete(
                            AsyncRequest<ContentQueryRPCInfo, ContentQueryRPCResult> request) {
                        if (request.getStatus() == AsyncRequest.Status.Success
                                && request.getOutput().getSuccess()) {
                            if (request.getOutput().getArcFileResult().getContent()
                                    .getCount() == (CrawlEnvironment.ORIGINAL_CONTENT_SIZE_LIMIT + 1)) {
                                LOG.error(
                                        "RPC to QueryMaster Successfull BUT content size is 131072. Suspecting truncation. REJECTING S3 Data for targetURL:"
                                                + targetURL);
                                queueHighPriorityURLRequest(targetURL, urlFingerprint, responseData,
                                        completionSemaphore, timeoutInMS, skipHTTPFetch);
                            } else {
                                LOG.info("RPC to QueryMaster Successfull. Servicing request for targetURL:"
                                        + targetURL + " via s3 cache");
                                // cache the http result 
                                cacheS3ItemResult(request.getOutput().getArcFileResult(), targetURL,
                                        urlFingerprint);
                                // set the result data .. 
                                responseData.setS3ItemResponse(request.getOutput().getArcFileResult());
                                // and set the completion semaphore ... 
                                completionSemaphore.release();
                            }
                        } else {
                            LOG.info("RPC to QueryMaster Failed. Servicing request for targetURL:" + targetURL
                                    + " via crawler");
                            queueHighPriorityURLRequest(targetURL, urlFingerprint, responseData,
                                    completionSemaphore, timeoutInMS, skipHTTPFetch);
                        }
                    }
                });
    } catch (RPCException e) {
        LOG.error("RPC to Query Master for targetURL:" + targetURL + " Failed with Exception:"
                + CCStringUtils.stringifyException(e));
        // queue it up for direct service via crawler ... 
        queueHighPriorityURLRequest(targetURL, urlFingerprint, responseData, completionSemaphore, timeoutInMS,
                skipHTTPFetch);
    }
}

From source file:com.qwazr.search.index.IndexInstance.java

final synchronized BackupStatus backup(Integer keepLastCount) throws IOException, InterruptedException {
    Semaphore sem = schema.acquireReadSemaphore();
    try {/*from   w ww. j  a  va  2 s  . c o m*/
        File backupdir = null;
        final IndexCommit commit = snapshotDeletionPolicy.snapshot();
        try {
            int files_count = 0;
            long bytes_size = 0;
            if (!fileSet.backupDirectory.exists())
                fileSet.backupDirectory.mkdir();
            backupdir = new File(fileSet.backupDirectory, Long.toString(commit.getGeneration()));
            if (!backupdir.exists())
                backupdir.mkdir();
            if (!backupdir.exists())
                throw new IOException("Cannot create the backup directory: " + backupdir);
            for (String fileName : commit.getFileNames()) {
                File sourceFile = new File(fileSet.dataDirectory, fileName);
                File targetFile = new File(backupdir, fileName);
                files_count++;
                bytes_size += sourceFile.length();
                if (targetFile.exists() && targetFile.length() == sourceFile.length()
                        && targetFile.lastModified() == sourceFile.lastModified())
                    continue;
                FileUtils.copyFile(sourceFile, targetFile, true);
            }
            purgeBackups(keepLastCount);
            return new BackupStatus(commit.getGeneration(), backupdir.lastModified(), bytes_size, files_count);
        } catch (IOException e) {
            if (backupdir != null)
                FileUtils.deleteQuietly(backupdir);
            throw e;
        } finally {
            snapshotDeletionPolicy.release(commit);
        }
    } finally {
        if (sem != null)
            sem.release();
    }
}

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

@Override
public boolean removeNode(final ClusterConfig conf, Collection<String> nodes) {
    logger.info("Deleting Cassandra packages...", getComponentName());
    try {/*from  w w w  . j  av  a2s  . c  o m*/
        if (newClusterConf == null) {
            // setting clusterconf, componentconf and logger
            if (!setClassVariables(conf)) {
                return false;
            }
        }
        final Semaphore semaphore = new Semaphore(nodes.size());
        // undeploying package from each node
        for (final String host : nodes) {
            semaphore.acquire();
            AppStoreWrapper.getExecutor().execute(new Runnable() {
                @Override
                public void run() {
                    // setting nodestatus default value to false
                    boolean nodestatus = false;
                    // if service stopped successfully, then removing
                    // component from node
                    if (stopNode(host)) {
                        nodestatus = removeNode(host);
                    }
                    conf.getNodes().get(host).setStatus(nodestatus);
                    if (semaphore != null) {
                        semaphore.release();
                    }
                }
            });
        }
        semaphore.acquire(nodes.size());
        return AnkushUtils.getStatus(conf.getNodes());
    } catch (Exception e) {
        return addClusterError("Could not remove " + getComponentName(), e);
    }
}