Example usage for java.util.concurrent Semaphore tryAcquire

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

Introduction

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

Prototype

public boolean tryAcquire() 

Source Link

Document

Acquires a permit from this semaphore, only if one is available at the time of invocation.

Usage

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

@Override
protected void handleLookup(CommandLookupTopic lookup) {
    final long requestId = lookup.getRequestId();
    final String topic = lookup.getTopic();
    if (log.isDebugEnabled()) {
        log.debug("[{}] Received Lookup from {} for {}", topic, remoteAddress, requestId);
    }//from  w ww  .jav  a 2s  . c  o m
    final Semaphore lookupSemaphore = service.getLookupRequestSemaphore();
    if (lookupSemaphore.tryAcquire()) {
        lookupDestinationAsync(getBrokerService().pulsar(), DestinationName.get(topic),
                lookup.getAuthoritative(), getRole(), lookup.getRequestId()).handle((lookupResponse, ex) -> {
                    if (ex == null) {
                        ctx.writeAndFlush(lookupResponse);
                    } else {
                        // it should never happen
                        log.warn("[{}] lookup failed with error {}, {}", remoteAddress, topic, ex.getMessage(),
                                ex);
                        ctx.writeAndFlush(
                                newLookupResponse(ServerError.ServiceNotReady, ex.getMessage(), requestId));
                    }
                    lookupSemaphore.release();
                    return null;
                });
    } else {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Failed 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.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);
    }//from  w ww  .jav a 2 s.  c o m
    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:org.apache.pulsar.broker.service.BrokerService.java

/**
 * It creates a topic async and returns CompletableFuture. It also throttles down configured max-concurrent topic
 * loading and puts them into queue once in-process topics are created.
 * /*from  w  w  w .jav a2  s .  c  o m*/
 * @param topic persistent-topic name
 * @return CompletableFuture<Topic>
 * @throws RuntimeException
 */
protected CompletableFuture<Topic> createPersistentTopic(final String topic) throws RuntimeException {
    checkTopicNsOwnership(topic);

    final CompletableFuture<Topic> topicFuture = new CompletableFuture<>();

    final Semaphore topicLoadSemaphore = topicLoadRequestSemaphore.get();

    if (topicLoadSemaphore.tryAcquire()) {
        createPersistentTopic(topic, topicFuture);
        topicFuture.handle((persistentTopic, ex) -> {
            // release permit and process pending topic
            topicLoadSemaphore.release();
            createPendingLoadTopic();
            return null;
        });
    } else {
        pendingTopicLoadingQueue.add(new ImmutablePair<String, CompletableFuture<Topic>>(topic, topicFuture));
        if (log.isDebugEnabled()) {
            log.debug("topic-loading for {} added into pending queue", topic);
        }
    }
    return topicFuture;
}

From source file:org.apache.pulsar.broker.service.BrokerService.java

/**
 * Create pending topic and on completion it picks the next one until processes all topics in
 * {@link #pendingTopicLoadingQueue}.<br/>
 * It also tries to acquire {@link #topicLoadRequestSemaphore} so throttle down newly incoming topics and release
 * permit if it was successful to acquire it.
 *//*from  w w  w .  java  2s  .c  o  m*/
private void createPendingLoadTopic() {
    Pair<String, CompletableFuture<Topic>> pendingTopic = pendingTopicLoadingQueue.poll();
    if (pendingTopic == null) {
        return;
    }

    final String topic = pendingTopic.getLeft();
    try {
        checkTopicNsOwnership(topic);
        CompletableFuture<Topic> pendingFuture = pendingTopic.getRight();
        final Semaphore topicLoadSemaphore = topicLoadRequestSemaphore.get();
        final boolean acquiredPermit = topicLoadSemaphore.tryAcquire();
        createPersistentTopic(topic, pendingFuture);
        pendingFuture.handle((persistentTopic, ex) -> {
            // release permit and process next pending topic
            if (acquiredPermit) {
                topicLoadSemaphore.release();
            }
            createPendingLoadTopic();
            return null;
        });
    } catch (RuntimeException re) {
        log.error("Failed to create pending topic {} {}", topic, re);
        pendingTopic.getRight().completeExceptionally(re.getCause());
        // schedule to process next pending topic
        inactivityMonitor.schedule(() -> createPendingLoadTopic(), 100, TimeUnit.MILLISECONDS);
    }

}

From source file:com.clustercontrol.monitor.run.factory.RunMonitor.java

/**
 * FacilityId????????/*from  ww w  .j  av a2  s .c o  m*/
 * 
 * @return ??????</code> true </code>
 * @throws FacilityNotFound
 * @throws MonitorNotFound
 * @throws InvalidRole
 * @throws EntityExistsException
 * @throws HinemosUnknown
 *
 */
private boolean runMonitorInfoAggregateByNode()
        throws FacilityNotFound, MonitorNotFound, InvalidRole, EntityExistsException, HinemosUnknown {

    if (!m_isMonitorJob) {
        // ?????????????????
        final RepositoryControllerBean repository = new RepositoryControllerBean();
        final NodeInfo targetNode = repository.getNode(m_facilityId);
        final Set<Integer> currentTask = NodeMonitorPollerController
                .calcCurrentExecMonitorIntervals(targetNode);

        final Set<Integer> plannedTask = getPlannedTasksForNodeAggregateMonitor(m_monitorTypeId, m_facilityId);
        synchronized (plannedTask) {
            plannedTask.addAll(currentTask);
        }

        final NodeToMonitorCache node2monitor = NodeToMonitorCache.getInstance(m_monitorTypeId);
        final Semaphore execSingleThreadSemaphore = getSemaphoreForNodeAggregateMonitor(m_monitorTypeId,
                m_facilityId);

        // ?try???????????1??????????
        // Semaphore??????????????????????
        // ?????????
        if (execSingleThreadSemaphore.tryAcquire() == false) {
            for (Map.Entry<Integer, Set<MonitorInfo>> entry : node2monitor
                    .getMonitorsWithCalendar(m_facilityId, currentTask).entrySet()) {

                // ??????????
                final Set<MonitorInfo> monitors = entry.getValue();
                for (MonitorInfo monitor : monitors) {
                    String notifyGroupId = monitor.getNotifyGroupId();

                    // 
                    OutputBasicInfo notifyInfo = new OutputBasicInfo();
                    notifyInfo.setPluginId(m_monitorTypeId);
                    notifyInfo.setMonitorId(monitor.getMonitorId());
                    notifyInfo.setApplication(monitor.getApplication());

                    String facilityPath = new RepositoryControllerBean().getFacilityPath(m_facilityId, null);
                    notifyInfo.setFacilityId(m_facilityId);
                    notifyInfo.setScopeText(facilityPath);

                    // ????
                    int priority = PriorityConstant.TYPE_UNKNOWN;

                    String message = MessageConstant.MESSAGE_MONITOR_UNCOMPLETED.getMessage();
                    String messageOrg = MessageConstant.MESSAGE_MONITOR_UNCOMPLETED.getMessage();
                    notifyInfo.setPriority(priority);
                    notifyInfo.setMessage(message);
                    notifyInfo.setMessageOrg(messageOrg);
                    notifyInfo.setGenerationDate(m_now.getTime());

                    // for debug
                    if (m_log.isDebugEnabled()) {
                        m_log.debug("notify() priority = " + priority + " , message = " + message
                                + " , messageOrg = " + messageOrg + ", generationDate = " + m_now);
                    }

                    // ?
                    if (m_log.isDebugEnabled()) {
                        m_log.debug("sending message" + " : priority=" + notifyInfo.getPriority()
                                + " generationDate=" + notifyInfo.getGenerationDate() + " pluginId="
                                + notifyInfo.getPluginId() + " monitorId=" + notifyInfo.getMonitorId()
                                + " facilityId=" + notifyInfo.getFacilityId() + " subKey="
                                + notifyInfo.getSubKey() + ")");
                    }

                    new NotifyControllerBean().notify(notifyInfo, notifyGroupId);
                }
            }

            // ?????????????????????true?
            return true;
        }

        // ?????1??????Semaphore???
        try {
            // ???????????????
            Set<Integer> execTargetInterval = null;
            synchronized (plannedTask) {
                execTargetInterval = new HashSet<>(plannedTask);
                plannedTask.clear();
            }

            // ??????preCollect???????????????preCollect????????
            final Object preCollectData = preCollect(execTargetInterval);

            // ??????RunMonitor???runMonitorList???????
            final List<RunMonitor> runMonitorList = new ArrayList<>();
            Set<MonitorInfo> monitorInfos = new HashSet<>();
            for (Set<MonitorInfo> addMonitors : node2monitor
                    .getMonitorsWithCalendar(m_facilityId, execTargetInterval).values()) {
                monitorInfos.addAll(addMonitors);
            }
            // ????????FacilityId????Map???????????1?????
            final Map<String, NodeInfo> nodeinfoMap = new HashMap<>();
            nodeinfoMap.put(m_facilityId, targetNode);
            for (final MonitorInfo monitorInfo : monitorInfos) {
                // ID?runMonitor?????
                RunMonitor runMonitor = this.createMonitorInstance();
                runMonitor.m_monitorTypeId = m_monitorTypeId;
                runMonitor.m_monitorId = monitorInfo.getMonitorId();
                runMonitor.m_now = m_now;
                if (runMonitor.setMonitorInfo(m_monitorTypeId, monitorInfo.getMonitorId())) {
                    runMonitorList.add(runMonitor);

                    // setMonitorInfo?????FacilityId???
                    // ??????????????????????
                    runMonitor.m_facilityId = m_facilityId;

                    // ????runMonitor?????runMonitorInfo??????
                    // TODO ?? m_priorityMap ???????????????????
                    runMonitor.setJudgementInfo();
                    runMonitor.nodeInfo = nodeinfoMap;
                    runMonitor.setCheckInfo();
                    runMonitor.m_isNode = true;
                }
            }

            // ??????
            checkMultiMonitorInfoData(preCollectData, runMonitorList);
        } finally {
            execSingleThreadSemaphore.release();
        }
    } else {
        // ?????
        final Object preCollectData = preCollect(null);

        // ID?runMonitor?????
        setJudgementInfo();
        final RepositoryControllerBean repository = new RepositoryControllerBean();
        final NodeInfo targetNode = repository.getNode(m_facilityId);
        final Map<String, NodeInfo> nodeinfoMap = new HashMap<>();
        nodeinfoMap.put(m_facilityId, targetNode);
        nodeInfo = nodeinfoMap;
        setCheckInfo();
        m_isNode = true;

        // ??????
        checkMultiMonitorInfoData(preCollectData, Arrays.asList(this));
    }
    return true;
}