List of usage examples for java.util.concurrent Semaphore release
public void release()
From source file:com.impetus.ankush2.ganglia.GangliaDeployer.java
@Override public boolean start(final ClusterConfig conf, Collection<String> nodes) { final Semaphore semaphore = new Semaphore(nodes.size()); String gangliaMaster = (String) advanceConf.get(GangliaConstants.ClusterProperties.GMETAD_HOST); try {/*from w ww. ja v a 2s. c om*/ // starting Ganglia master during deployment case if (newClusterConf == null) { // starting ganglia master first and then applying a 1 minute // sleep // before starting gmond if (!startGangliaMaster(gangliaMaster)) { return false; } } // starting service in each cluster node for (final String host : nodes) { semaphore.acquire(); AppStoreWrapper.getExecutor().execute(new Runnable() { @Override public void run() { conf.getNodes().get(host).setStatus(startGmond(host)); if (semaphore != null) { semaphore.release(); } } }); } semaphore.acquire(nodes.size()); } catch (Exception e) { logger.warn("There is some exception while stating " + getComponentName() + " services.", getComponentName(), e); } // Return false if any of the node service is not started. // return AnkushUtils.getStatus(conf.getNodes()); return true; }
From source file:voldemort.store.routed.ThreadPoolRoutedStore.java
@Override public boolean delete(final ByteArray key, final Version version) throws VoldemortException { StoreUtils.assertValidKey(key);//from w w w . j av a 2 s . co m final List<Node> nodes = availableNodes(routingStrategy.routeRequest(key.get())); // quickly fail if there aren't enough live nodes to meet the // requirements 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); final AtomicBoolean deletedSomething = new AtomicBoolean(false); // A list of thrown exceptions, indicating the number of failures final List<Exception> failures = Collections.synchronizedList(new LinkedList<Exception>()); // 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 Semaphore semaphore = new Semaphore(0, false); // Add the operations to the pool for (final Node node : nodes) { this.executor.execute(new Runnable() { @Override public void run() { long startNs = System.nanoTime(); try { boolean deleted = innerStores.get(node.getId()).delete(key, version); successes.incrementAndGet(); deletedSomething.compareAndSet(false, deleted); recordSuccess(node, startNs); } catch (UnreachableStoreException e) { failures.add(e); recordException(node, startNs, e); } catch (VoldemortApplicationException e) { throw e; } catch (Exception e) { failures.add(e); logger.warn("Error in DELETE on node " + node.getId() + "(" + node.getHost() + ")", e); } finally { // signal that the operation is complete semaphore.release(); } } }); } int attempts = Math.min(storeDef.getPreferredWrites(), numNodes); if (this.storeDef.getPreferredWrites() <= 0) { return true; } else { for (int i = 0; i < numNodes; i++) { try { long timeoutMs = timeoutConfig.getOperationTimeout(VoldemortOpCode.DELETE_OP_CODE); boolean acquired = semaphore.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS); if (!acquired) logger.warn("Delete operation timed out waiting for operation " + i + " to complete after waiting " + timeoutMs + " ms."); // okay, at least the required number of operations have // completed, were they successful? if (successes.get() >= attempts) return deletedSomething.get(); } catch (InterruptedException e) { throw new InsufficientOperationalNodesException("Delete operation interrupted!", e); } } } // If we get to here, that means we couldn't hit the preferred number // of writes, throw an exception if you can't even hit the required // number if (successes.get() < storeDef.getRequiredWrites()) throw new InsufficientOperationalNodesException( this.storeDef.getRequiredWrites() + " deletes required, but " + successes.get() + " succeeded.", failures); else return deletedSomething.get(); }
From source file:com.impetus.ankush2.cassandra.deployer.CassandraDeployer.java
@Override public boolean stop(final ClusterConfig conf, Collection<String> nodes) { // Stop services only if cluster is not in REMOVING_NODES if (!conf.getState().equals(Constant.Cluster.State.REMOVE_NODE)) { final Semaphore semaphore = new Semaphore(nodes.size()); try {//from w w w. j a v a 2 s .co m // stopping service on each of the cluster nodes for (final String host : nodes) { semaphore.acquire(); AppStoreWrapper.getExecutor().execute(new Runnable() { @Override public void run() { conf.getNodes().get(host).setStatus(stopNode(host)); if (semaphore != null) { semaphore.release(); } } }); } semaphore.acquire(nodes.size()); } catch (Exception e) { logger.error(e.getMessage()); } } return true; }
From source file:com.impetus.ankush2.cassandra.deployer.CassandraDeployer.java
/** * Perform asynchronous operations nodes. * /*from w ww . ja v a 2s.co m*/ * @param nodeList * {@link Collection} * @return <code>true</code>, if successful */ private boolean validate(Collection<String> nodeList) throws AnkushException { try { // Create semaphore to join threads final Semaphore semaphore = new Semaphore(nodeList.size()); for (final String host : nodeList) { final NodeConfig nodeConfig = clusterConfig.getNodes().get(host); semaphore.acquire(); AppStoreWrapper.getExecutor().execute(new Runnable() { @Override public void run() { nodeConfig.setStatus(new CassandraValidator(clusterConfig, nodeConfig).validate()); if (semaphore != null) { semaphore.release(); } } }); } semaphore.acquire(nodeList.size()); } catch (Exception e) { throw new AnkushException(getComponentName() + " validation failed."); } return AnkushUtils.getStatus(clusterConfig, nodeList); }
From source file:com.impetus.ankush.common.framework.ClusterPreValidator.java
public Map validate(final Map params) { final LinkedHashMap result = new LinkedHashMap(); if (notContainsKey(params, "nodePorts", result)) { return result; }/* w w w .jav a 2 s . c om*/ if (params.containsKey(Constant.Keys.CLUSTERID)) { // Get cluster manager GenericManager<Cluster, Long> clusterManager = AppStoreWrapper.getManager(Constant.Manager.CLUSTER, Cluster.class); // get cluster id string String clusterIdStr = (String) params.get(Constant.Keys.CLUSTERID); // convert cluster id string into long value. Long clusterId = ParserUtil.getLongValue(clusterIdStr, 0); // Get the cluster object from database. Cluster cluster = clusterManager.get(clusterId); ClusterConfig clusterConfig = cluster.getClusterConfig(); // set username params.put(Constant.Keys.USERNAME, clusterConfig.getAuthConf().getUsername()); String pass = clusterConfig.getAuthConf().getPassword(); if (pass != null && !pass.isEmpty()) { params.put(Constant.Keys.PASSWORD, pass); } else { params.put(Constant.Keys.PRIVATEKEY, clusterConfig.getAuthConf().getPrivateKey()); } params.put(Constant.Agent.Key.AGENT_INSTALL_DIR, clusterConfig.getAgentInstallDir()); } else { if (notContainsKey(params, Constant.Keys.USERNAME, result)) { return result; } if (notContainsKey(params, Constant.Keys.PASSWORD, result)) { return result; } if (notContainsKey(params, Constant.Keys.PRIVATEKEY, result)) { return result; } } final String username = (String) params.get(Constant.Keys.USERNAME); final String password = (String) params.get(Constant.Keys.PASSWORD); final String privateKey = (String) params.get(Constant.Keys.PRIVATEKEY); final Map nodePorts = (Map) params.get("nodePorts"); Set<String> nodes = nodePorts.keySet(); final boolean authUsingPassword = (password != null && !password.isEmpty()); final String authInfo = authUsingPassword ? password : privateKey; try { final Semaphore semaphore = new Semaphore(nodes.size()); for (final String hostname : nodes) { semaphore.acquire(); AppStoreWrapper.getExecutor().execute(new Runnable() { @Override public void run() { SSHExec conn = null; LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>(); try { conn = SSHUtils.connectToNode(hostname, username, password, privateKey); map = getValidationMap(params, username, password, nodePorts, authUsingPassword, authInfo, hostname, conn); } catch (Exception e) { map.put("error", new Status("Error", "Unable to perform validations", CRITICAL)); logger.error(e.getMessage(), e); } finally { // setting overall status. map.put("status", getOverAllStatus((Collection) map.values())); // putting map against node. result.put(hostname, map); if (semaphore != null) { semaphore.release(); } if (conn != null) { conn.disconnect(); } } } }); } semaphore.acquire(nodes.size()); result.put("status", true); } catch (Exception e) { String message = e.getMessage(); if (message == null) { message = "Unable to validate nodes"; } result.put("error", Collections.singletonList(message)); result.put("status", false); logger.error(e.getMessage(), e); } return result; }
From source file:voldemort.store.routed.RoutedStore.java
public boolean delete(final ByteArray key, final Version version) throws VoldemortException { StoreUtils.assertValidKey(key);/*from w ww . ja v a 2s.c o m*/ final List<Node> nodes = availableNodes(routingStrategy.routeRequest(key.get())); // quickly fail if there aren't enough live nodes to meet the // requirements 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); final AtomicBoolean deletedSomething = new AtomicBoolean(false); // A list of thrown exceptions, indicating the number of failures final List<Exception> failures = Collections.synchronizedList(new LinkedList<Exception>()); // 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 Semaphore semaphore = new Semaphore(0, false); // Add the operations to the pool for (final Node node : nodes) { this.executor.execute(new Runnable() { public void run() { long startNs = System.nanoTime(); try { boolean deleted = innerStores.get(node.getId()).delete(key, version); successes.incrementAndGet(); deletedSomething.compareAndSet(false, deleted); recordSuccess(node, startNs); } catch (UnreachableStoreException e) { failures.add(e); recordException(node, startNs, e); } catch (VoldemortApplicationException e) { throw e; } catch (Exception e) { failures.add(e); logger.warn("Error in DELETE on node " + node.getId() + "(" + node.getHost() + ")", e); } finally { // signal that the operation is complete semaphore.release(); } } }); } int attempts = Math.min(storeDef.getPreferredWrites(), numNodes); if (this.storeDef.getPreferredWrites() <= 0) { return true; } else { for (int i = 0; i < numNodes; i++) { try { boolean acquired = semaphore.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS); if (!acquired) logger.warn("Delete operation timed out waiting for operation " + i + " to complete after waiting " + timeoutMs + " ms."); // okay, at least the required number of operations have // completed, were they successful? if (successes.get() >= attempts) return deletedSomething.get(); } catch (InterruptedException e) { throw new InsufficientOperationalNodesException("Delete operation interrupted!", e); } } } // If we get to here, that means we couldn't hit the preferred number // of writes, throw an exception if you can't even hit the required // number if (successes.get() < storeDef.getRequiredWrites()) throw new InsufficientOperationalNodesException( this.storeDef.getRequiredWrites() + " deletes required, but " + successes.get() + " succeeded.", failures); else return deletedSomething.get(); }
From source file:com.impetus.ankush2.ganglia.GangliaDeployer.java
/** * Perform asynchronous operations nodes. * // ww w .j ava 2s . c om * @param nodeList * {@link Collection} * @return <code>true</code>, if successful */ private boolean validate(Collection<String> nodeList) throws AnkushException { try { // Create semaphore to join threads final Semaphore semaphore = new Semaphore(nodeList.size()); for (final String host : nodeList) { final NodeConfig nodeConf = clusterConf.getNodes().get(host); semaphore.acquire(); AppStoreWrapper.getExecutor().execute(new Runnable() { @Override public void run() { try { nodeConf.setStatus(new GangliaValidator(clusterConf, nodeConf).validate()); } catch (AnkushException e) { addClusterError(e.getMessage(), host, e); } catch (Exception e) { addClusterError("There is some exception while validating " + host + " for " + getComponentName() + " deployment. " + GangliaConstants.EXCEPTION_STRING, host, e); } finally { if (semaphore != null) { semaphore.release(); } } } }); } semaphore.acquire(nodeList.size()); } catch (Exception e) { throw new AnkushException("There is some exception while validating nodes for " + getComponentName() + " deployment." + GangliaConstants.EXCEPTION_STRING, e); } return AnkushUtils.getStatus(clusterConf, nodeList); }
From source file:com.impetus.ankush2.cassandra.deployer.CassandraDeployer.java
@Override public boolean start(final ClusterConfig conf, Collection<String> nodes) { try {/* www . j av a 2 s. co m*/ // Causing the thread to sleep for two minutes during add nodes case // to // update ring topology details if (this.clusterConfig.getState().equals(Constant.Cluster.State.ADD_NODE)) { // starting service on all newly added nodes for (final String host : nodes) { // setting cluster conf nodes status conf.getNodes().get(host).setStatus(startNode(host)); // Wait for two minutes try { logger.info("Waiting for two minutes...", getComponentName(), host); logger.debug("Wait for two minutes.", host); Thread.sleep(120000); } catch (InterruptedException e) { logger.debug(e.getMessage()); } } } else { final Semaphore semaphore = new Semaphore(nodes.size()); // starting service on each node in cluster for (final String host : nodes) { semaphore.acquire(); AppStoreWrapper.getExecutor().execute(new Runnable() { @Override public void run() { // setting cluster conf nodes status conf.getNodes().get(host).setStatus(startNode(host)); if (semaphore != null) { semaphore.release(); } } }); } semaphore.acquire(nodes.size()); } // Return false if any of the node is not deployed. return AnkushUtils.getStatus(conf.getNodes()); } catch (Exception e) { return addClusterError("Could not start " + getComponentName() + " services.", e); } }
From source file:org.commoncrawl.util.shared.S3Downloader.java
public void shutdown() { if (_callback == null) { throw new RuntimeException("Invalid State - stop called on already inactive downloader"); }// w ww. j a va2 s . com _freezeDownloads = true; Thread eventThread = (_ownsEventLoop) ? _eventLoop.getEventThread() : null; final Semaphore shutdownSemaphore = new Semaphore(0); _eventLoop.setTimer(new Timer(1, false, new Timer.Callback() { // shutdown within the context of the async thread ... public void timerFired(Timer timer) { try { // fail any active connections for (NIOHttpConnection connection : Lists.newArrayList(_activeConnections)) { S3DownloadItem item = (S3DownloadItem) connection.getContext(); if (item != null) { failDownload(item, NIOHttpConnection.ErrorType.UNKNOWN, connection, false); } } _activeConnections.clear(); // next, fail all queued items for (S3DownloadItem item : _queuedItems) { failDownload(item, NIOHttpConnection.ErrorType.UNKNOWN, null, false); } _queuedItems.clear(); _freezeDownloads = false; _callback = null; if (_ownsEventLoop) { //System.out.println("Stopping Event Loop"); _eventLoop.stop(); } _eventLoop = null; _ownsEventLoop = false; } finally { //System.out.println("Releasing Semaphore"); shutdownSemaphore.release(); } } })); //System.out.println("Acquiring Shutdown Semaphore"); shutdownSemaphore.acquireUninterruptibly(); //System.out.println("Acquired Shutdown Semaphore"); try { if (eventThread != null) { eventThread.join(); } } catch (InterruptedException e) { } }
From source file:org.commoncrawl.service.listcrawler.DataTransferAgent.java
static Thread startTransferThread(final int threadIndex, final CCBridgeServerMapping mapping, final File shutdownFile, final FileSystem fs, final Configuration conf, final LinkedBlockingDeque<ProxyTransferItem> itemQueue, final EventLoop eventLoop, final Semaphore shutdownSemaphore) { Thread thread = new Thread(new Runnable() { @Override// w ww.j a v a 2s . c om public void run() { try { while (true) { if (shutdownFile.exists()) { LOG.info("Exiting due to shutdown file existense!"); break; } ProxyTransferItem item = itemQueue.take(); if (item.hdfsFilePath == null) { LOG.info("Transfer Thread:" + Thread.currentThread().getId() + " Exiting"); } else { try { LOG.info("Transfer Thread:" + threadIndex + " for Host:" + mapping._internalName + " Transferring File:" + item.hdfsFilePath); int result = uploadSingeFile(mapping, fs, conf, item.hdfsFilePath, item.uploadName, eventLoop); if (result == 200) { LOG.info("Transfer Thread:" + threadIndex + "for Host:" + mapping._internalName + " Done Transferring File:" + item.hdfsFilePath); //item.logFilePath.createNewFile(); } else if (result == 409) { LOG.info("Transfer Thread:" + threadIndex + "for Host:" + mapping._internalName + " File Already Exists for Path:" + item.hdfsFilePath); //item.logFilePath.createNewFile(); } else { LOG.error("Transfer Thread:" + threadIndex + "for Host:" + mapping._internalName + " File Transfer Failed with Error:" + result + " for Path:" + item.hdfsFilePath); itemQueue.putFirst(item); } } catch (IOException e) { LOG.error("Transfer Failed for Thread:" + threadIndex + "Host:" + mapping._internalName + " File: " + item.hdfsFilePath); LOG.fatal(CCStringUtils.stringifyException(e)); } } } } catch (InterruptedException e) { } finally { shutdownSemaphore.release(); } } }); thread.start(); return thread; }