List of usage examples for java.util.concurrent Semaphore Semaphore
public Semaphore(int permits, boolean fair)
From source file:org.apache.pulsar.broker.service.BrokerService.java
public BrokerService(PulsarService pulsar) throws Exception { this.pulsar = pulsar; this.managedLedgerFactory = pulsar.getManagedLedgerFactory(); this.port = new URI(pulsar.getBrokerServiceUrl()).getPort(); this.tlsPort = new URI(pulsar.getBrokerServiceUrlTls()).getPort(); this.topics = new ConcurrentOpenHashMap<>(); this.replicationClients = new ConcurrentOpenHashMap<>(); this.keepAliveIntervalSeconds = pulsar.getConfiguration().getKeepAliveIntervalSeconds(); this.configRegisteredListeners = new ConcurrentOpenHashMap<>(); this.pendingTopicLoadingQueue = Queues.newConcurrentLinkedQueue(); this.multiLayerTopicsMap = new ConcurrentOpenHashMap<>(); this.pulsarStats = new PulsarStats(pulsar); this.offlineTopicStatCache = new ConcurrentOpenHashMap<>(); final DefaultThreadFactory acceptorThreadFactory = new DefaultThreadFactory("pulsar-acceptor"); final DefaultThreadFactory workersThreadFactory = new DefaultThreadFactory("pulsar-io"); final int numThreads = Runtime.getRuntime().availableProcessors() * 2; log.info("Using {} threads for broker service IO", numThreads); EventLoopGroup acceptorEventLoop, workersEventLoop; if (SystemUtils.IS_OS_LINUX) { try {/*from w ww.j a v a 2s. c om*/ acceptorEventLoop = new EpollEventLoopGroup(1, acceptorThreadFactory); workersEventLoop = new EpollEventLoopGroup(numThreads, workersThreadFactory); } catch (UnsatisfiedLinkError e) { acceptorEventLoop = new NioEventLoopGroup(1, acceptorThreadFactory); workersEventLoop = new NioEventLoopGroup(numThreads, workersThreadFactory); } } else { acceptorEventLoop = new NioEventLoopGroup(1, acceptorThreadFactory); workersEventLoop = new NioEventLoopGroup(numThreads, workersThreadFactory); } this.acceptorGroup = acceptorEventLoop; this.workerGroup = workersEventLoop; this.statsUpdater = Executors .newSingleThreadScheduledExecutor(new DefaultThreadFactory("pulsar-stats-updater")); if (pulsar.getConfiguration().isAuthorizationEnabled()) { this.authorizationManager = new AuthorizationManager(pulsar.getConfiguration(), pulsar.getConfigurationCache()); } if (pulsar.getConfigurationCache() != null) { pulsar.getConfigurationCache().policiesCache().registerListener(this); } this.inactivityMonitor = Executors .newSingleThreadScheduledExecutor(new DefaultThreadFactory("pulsar-inactivity-monitor")); this.messageExpiryMonitor = Executors .newSingleThreadScheduledExecutor(new DefaultThreadFactory("pulsar-msg-expiry-monitor")); this.backlogQuotaManager = new BacklogQuotaManager(pulsar); this.backlogQuotaChecker = Executors .newSingleThreadScheduledExecutor(new DefaultThreadFactory("pulsar-backlog-quota-checker")); this.authenticationService = new AuthenticationService(pulsar.getConfiguration()); this.dynamicConfigurationCache = new ZooKeeperDataCache<Map<String, String>>(pulsar().getLocalZkCache()) { @Override public Map<String, String> deserialize(String key, byte[] content) throws Exception { return ObjectMapperFactory.getThreadLocal().readValue(content, HashMap.class); } }; this.blockedDispatchers = new ConcurrentOpenHashSet<>(); // update dynamic configuration and register-listener updateConfigurationAndRegisterListeners(); this.lookupRequestSemaphore = new AtomicReference<Semaphore>( new Semaphore(pulsar.getConfiguration().getMaxConcurrentLookupRequest(), false)); this.topicLoadRequestSemaphore = new AtomicReference<Semaphore>( new Semaphore(pulsar.getConfiguration().getMaxConcurrentTopicLoadRequest(), false)); if (pulsar.getConfiguration().getMaxUnackedMessagesPerBroker() > 0 && pulsar.getConfiguration().getMaxUnackedMessagesPerSubscriptionOnBrokerBlocked() > 0.0) { this.maxUnackedMessages = pulsar.getConfiguration().getMaxUnackedMessagesPerBroker(); this.maxUnackedMsgsPerDispatcher = (int) ((maxUnackedMessages * pulsar.getConfiguration().getMaxUnackedMessagesPerSubscriptionOnBrokerBlocked()) / 100); log.info("Enabling per-broker unack-message limit {} and dispatcher-limit {} on blocked-broker", maxUnackedMessages, maxUnackedMsgsPerDispatcher); // block misbehaving dispatcher by checking periodically pulsar.getExecutor().scheduleAtFixedRate(() -> checkUnAckMessageDispatching(), 600, 30, TimeUnit.SECONDS); } else { this.maxUnackedMessages = 0; this.maxUnackedMsgsPerDispatcher = 0; log.info( "Disabling per broker unack-msg blocking due invalid unAckMsgSubscriptionPercentageLimitOnBrokerBlocked {} ", pulsar.getConfiguration().getMaxUnackedMessagesPerSubscriptionOnBrokerBlocked()); } PersistentReplicator.setReplicatorQueueSize(pulsar.getConfiguration().getReplicationProducerQueueSize()); }
From source file:com.ericsson.eif.hansoft.HansoftManager.java
/** * @return Hansoft SDK session/* w w w .j a v a 2 s .c om*/ * @throws HPMSdkException * @throws HPMSdkJavaException */ public static HPMSdkSession getMainSession() throws HPMSdkException, HPMSdkJavaException { if (hsSession != null) { return hsSession; } // Change to EHPMSdkDebugMode.Debug to get memory leak info and // debug output. Note: this is expensive. EHPMSdkDebugMode debugMode = EHPMSdkDebugMode.Off; // The Semaphore's release() is called on each callback, so process // - and check for case where the session is invalid = exception // with EHPMError_ConnectionLost. @SuppressWarnings("serial") Semaphore hsSemaphore = new Semaphore(1, true) { @Override public void release() { if (hsSession != null) { try { hsSession.SessionProcess(); } catch (HPMSdkException e) { logger.warn("Error from Hansoft Adapter: " + e.ErrorAsStr(), e); if (e.GetError().equals(EHPMError.ConnectionLost)) { clearSession(); } } catch (HPMSdkJavaException e) { logger.warn("Error from Hansoft Adapter: " + e.ErrorAsStr(), e); } } super.release(); } }; // Callbacks will be received as result of calling SessionProcess(). HPMSdkCallbacks hsCallbacks = new HansoftCallbacks(); hsSession = HPMSdkSession.SessionOpen(hansoftServer, // Address to // connect to hansoftPort, // Port of the server to connect to hansoftDatabase, // The database of the server to connect to hansoftSDK, // The name of the resource (SDK user) to log in // as hansoftSDKPassword, // Password for SDK user hsCallbacks, // Callback if using Semaphore it will be // called on SessionProcess() hsSemaphore, // Semaphore true, // Block on operations debugMode, // Debug mode 0, // Number of Sessions. Each session will give a in memory // copy of Hansoft DB, likely not needed. hansoftWorkingDir, // Working directory where db cache etc // is stored hansoftLib, // Path to the Hansoft library (HPMSdk.x64.dylib // on mac os) null); // Certificate settings return hsSession; }
From source file:org.apache.brooklyn.location.jclouds.JcloudsLocation.java
@Override @Deprecated//from www . j a v a2 s . c o m public JcloudsLocation configure(Map<?, ?> properties) { super.configure(properties); if (config().getLocalBag().containsKey("providerLocationId")) { LOG.warn("Using deprecated 'providerLocationId' key in " + this); if (!config().getLocalBag().containsKey(CLOUD_REGION_ID)) config().addToLocalBag(MutableMap.of(CLOUD_REGION_ID.getName(), (String) config().getLocalBag().getStringKey("providerLocationId"))); } if (isDisplayNameAutoGenerated() || !groovyTruth(getDisplayName())) { setDisplayName(elvis(getProvider(), "unknown") + (groovyTruth(getRegion()) ? ":" + getRegion() : "") + (groovyTruth(getEndpoint()) ? ":" + getEndpoint() : "")); } setCreationString(config().getLocalBag()); if (getConfig(MACHINE_CREATION_SEMAPHORE) == null) { Integer maxConcurrent = getConfig(MAX_CONCURRENT_MACHINE_CREATIONS); if (maxConcurrent == null || maxConcurrent < 1) { throw new IllegalStateException( MAX_CONCURRENT_MACHINE_CREATIONS.getName() + " must be >= 1, but was " + maxConcurrent); } config().set(MACHINE_CREATION_SEMAPHORE, new Semaphore(maxConcurrent, true)); } return this; }
From source file:org.ut.biolab.medsavant.MedSavantServlet.java
private void loadConfiguration() throws ServletException { String host = null;/* www . j a v a2s . com*/ String uname = null; String pass = null; String dbase = null; String maxSimultaneousUploadsStr = null; int p = -1; try { Properties props = new Properties(); InputStream in = getConfigInputStream(); props.load(in); in.close(); host = props.getProperty("host", ""); uname = props.getProperty("username", ""); pass = props.getProperty("password", ""); dbase = props.getProperty("db", ""); maxSimultaneousUploadsStr = props.getProperty("max_simultaneous_uploads", "").trim(); String portStr = props.getProperty("port", "-1").trim(); if (StringUtils.isBlank(portStr) || !NumberUtils.isNumber(portStr)) { LOG.error("No port specified in configuration, cannot continue"); } if (maxSimultaneousUploadsStr == null) { throw new ServletException("No maximum number of simultaneous uploads specified. Cannot continue"); } p = Integer.parseInt(portStr); if (p <= 0) { throw new ServletException( "Illegal port specified in configuration: " + portStr + ", cannot continue."); } maxSimultaneousUploads = Integer.parseInt(maxSimultaneousUploadsStr); if (maxSimultaneousUploads <= 0) { throw new ServletException("Illegal number of maximum simultaneous uploads in configuration: " + maxSimultaneousUploadsStr + ", cannot continue."); } uploadSem = new Semaphore(maxSimultaneousUploads, true); if (StringUtils.isBlank(uname)) { throw new ServletException("No username specified in configuration file, cannot continue."); } if (StringUtils.isBlank(pass)) { throw new ServletException("No password specified in configuration file, cannot continue."); } if (StringUtils.isBlank(dbase)) { throw new ServletException("No database specified in configuration file, cannot continue."); } if (StringUtils.isBlank(host)) { throw new ServletException("No host specified in configuration file, cannot continue."); } } catch (IOException iex) { throw new ServletException("IO Exception reading config file, cannot continue: " + iex.getMessage()); } this.medSavantServerHost = host; this.medSavantServerPort = p; this.username = uname; this.password = pass; this.db = dbase; LOG.info("Configured with:"); LOG.info("Host = " + host); LOG.info("Port = " + p); LOG.info("Username = " + uname); LOG.info("Database = " + this.db); }
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);//ww w .j av a 2s. c o 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, 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:voldemort.store.routed.RoutedStore.java
public void put(final ByteArray key, final Versioned<byte[]> versioned) throws VoldemortException { long startNs = System.nanoTime(); StoreUtils.assertValidKey(key);//from ww w. java2 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); 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:org.apache.pulsar.broker.service.BrokerService.java
/** * Update dynamic-ServiceConfiguration with value present into zk-configuration-map and register listeners on * dynamic-ServiceConfiguration field to take appropriate action on change of zk-configuration-map. *///from w ww. j a v a 2 s. c o m private void updateConfigurationAndRegisterListeners() { // update ServiceConfiguration value by reading zk-configuration-map updateDynamicServiceConfiguration(); // add listener on "maxConcurrentLookupRequest" value change registerConfigurationListener("maxConcurrentLookupRequest", (maxConcurrentLookupRequest) -> lookupRequestSemaphore .set(new Semaphore((int) maxConcurrentLookupRequest, false))); // add listener on "maxConcurrentTopicLoadRequest" value change registerConfigurationListener("maxConcurrentTopicLoadRequest", (maxConcurrentTopicLoadRequest) -> topicLoadRequestSemaphore .set(new Semaphore((int) maxConcurrentTopicLoadRequest, false))); registerConfigurationListener("loadManagerClassName", className -> { try { final LoadManager newLoadManager = LoadManager.create(pulsar); log.info("Created load manager: {}", className); pulsar.getLoadManager().get().stop(); newLoadManager.start(); pulsar.getLoadManager().set(newLoadManager); } catch (Exception ex) { log.warn("Failed to change load manager due to {}", ex); } }); // add more listeners here }