List of usage examples for java.util.concurrent Semaphore release
public void release()
From source file:org.commoncrawl.service.listcrawler.ProxyServlet.java
private static boolean checkCacheForURL(final String targetURL, final AsyncResponse responseData, final Semaphore completionSemaphore, final long timeoutInMS, final boolean skipHTTPFetch) { // normalize the url ... try {// ww w . j a va2 s . co m final String normalizedURL = URLUtils.canonicalizeURL(targetURL, true); final long urlFingerprint = URLFingerprint.generate64BitURLFPrint(normalizedURL); //1. check cache for data ProxyServer.getSingleton().getCache().checkCacheForItem(normalizedURL, urlFingerprint, new CacheItemCheckCallback() { @Override public void cacheItemAvailable(String url, CacheItem item) { // if redirected ... get redirected url ... if ((item.getFlags() & (CacheItem.Flags.Flag_IsPermanentRedirect | CacheItem.Flags.Flag_IsTemporaryRedirect)) != 0) { LOG.info("Redirect Detected for TargetURL:" + targetURL + " Checking Cache for Final URL:" + item.getFinalURL()); // resubmit the request to the cache if (!checkCacheForURL(item.getFinalURL(), responseData, completionSemaphore, timeoutInMS, skipHTTPFetch)) { // immediate failure detected ... responseData.setHttpErrorResponse(400, "Malformed Exception parsing Redirect URL:" + item.getFinalURL()); // release completion semaphore completionSemaphore.release(); } } // otherwise no redirects detected .. else { LOG.info("Servicing Response for URL:" + url + " via cache. Item Content Size is:" + item.getContent().getCount()); // if cached data is available ... // set the appropriate data member in the response object ... // and return to the calling thread (so that it can do the blocking io to service the request) responseData.setCacheItemRespone(item); // release completion semaphore completionSemaphore.release(); } } @Override public void cacheItemNotFound(String url) { // 2. time to hit the query master server (if available) if (false /*ProxyServer.getSingleton().isConnectedToQueryMaster()*/) { LOG.info("Query Master Online. Sending Request:" + targetURL + " to queryMaster"); queueQueryMasterURLRequest(targetURL, urlFingerprint, responseData, completionSemaphore, timeoutInMS, skipHTTPFetch); } else { LOG.info("Query Master Offline. Sending Request:" + targetURL + " directly to crawler"); // otherwise skip and go direct to crawler queue ... queueHighPriorityURLRequest(targetURL, urlFingerprint, responseData, completionSemaphore, timeoutInMS, skipHTTPFetch); } // 2. ok hit the query master if available // } }); // response will complete asynchronously ... return true; } catch (MalformedURLException e) { responseData.setHttpErrorResponse(400, "Malformed Exception parsing URL:" + targetURL); // immdediate response return false; } }
From source file:org.springframework.batch.core.jsr.launch.JsrJobOperator.java
/** * Creates a child {@link ApplicationContext} for the job being requested based upon * the /META-INF/batch.xml (if exists) and the /META-INF/batch-jobs/<jobName>.xml * configuration and restart the job.//w w w .ja v a2 s . c om * * @param executionId the database id of the job execution to be restarted. * @param params any job parameters to be used during the execution of this job. * @throws JobExecutionAlreadyCompleteException thrown if the requested job execution has * a status of COMPLETE * @throws NoSuchJobExecutionException throw if the requested job execution does not exist * in the repository * @throws JobExecutionNotMostRecentException thrown if the requested job execution is not * the most recent attempt for the job instance it's related to. * @throws JobRestartException thrown for any general errors during the job restart process */ @Override public long restart(long executionId, Properties params) throws JobExecutionAlreadyCompleteException, NoSuchJobExecutionException, JobExecutionNotMostRecentException, JobRestartException, JobSecurityException { org.springframework.batch.core.JobExecution previousJobExecution = jobExplorer.getJobExecution(executionId); if (previousJobExecution == null) { throw new NoSuchJobExecutionException("No JobExecution found for id: [" + executionId + "]"); } else if (previousJobExecution.getStatus().equals(BatchStatus.COMPLETED)) { throw new JobExecutionAlreadyCompleteException("The requested job has already completed"); } List<org.springframework.batch.core.JobExecution> previousExecutions = jobExplorer .getJobExecutions(previousJobExecution.getJobInstance()); for (org.springframework.batch.core.JobExecution jobExecution : previousExecutions) { if (jobExecution.getCreateTime().compareTo(previousJobExecution.getCreateTime()) > 0) { throw new JobExecutionNotMostRecentException( "The requested JobExecution to restart was not the most recently run"); } if (jobExecution.getStatus().equals(BatchStatus.ABANDONED)) { throw new JobRestartException("JobExecution ID: " + jobExecution.getId() + " is abandoned and attempted to be restarted."); } } final String jobName = previousJobExecution.getJobInstance().getJobName(); Properties jobRestartProperties = getJobRestartProperties(params, previousJobExecution); final JsrXmlApplicationContext batchContext = new JsrXmlApplicationContext(jobRestartProperties); batchContext.setValidating(false); Resource batchXml = new ClassPathResource("/META-INF/batch.xml"); Resource jobXml = new ClassPathResource(previousJobExecution.getJobConfigurationName()); if (batchXml.exists()) { batchContext.load(batchXml); } if (jobXml.exists()) { batchContext.load(jobXml); } AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder .genericBeanDefinition("org.springframework.batch.core.jsr.JsrJobContextFactoryBean") .getBeanDefinition(); beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON); batchContext.registerBeanDefinition(JSR_JOB_CONTEXT_BEAN_NAME, beanDefinition); batchContext.setParent(baseContext); try { batchContext.refresh(); } catch (BeanCreationException e) { throw new JobRestartException(e); } final org.springframework.batch.core.JobExecution jobExecution; try { JobParameters jobParameters = jobParametersConverter.getJobParameters(jobRestartProperties); jobExecution = jobRepository.createJobExecution(previousJobExecution.getJobInstance(), jobParameters, previousJobExecution.getJobConfigurationName()); } catch (Exception e) { throw new JobRestartException(e); } try { final Semaphore semaphore = new Semaphore(1); final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>()); semaphore.acquire(); taskExecutor.execute(new Runnable() { @Override public void run() { JsrJobContextFactoryBean factoryBean = null; try { factoryBean = (JsrJobContextFactoryBean) batchContext .getBean("&" + JSR_JOB_CONTEXT_BEAN_NAME); factoryBean.setJobExecution(jobExecution); final Job job = batchContext.getBean(Job.class); if (!job.isRestartable()) { throw new JobRestartException("Job " + jobName + " is not restartable"); } semaphore.release(); // Initialization of the JobExecution for job level dependencies jobRegistry.register(job, jobExecution); job.execute(jobExecution); jobRegistry.remove(jobExecution); } catch (Exception e) { exceptionHolder.add(e); } finally { if (factoryBean != null) { factoryBean.close(); } batchContext.close(); if (semaphore.availablePermits() == 0) { semaphore.release(); } } } }); semaphore.acquire(); if (exceptionHolder.size() > 0) { semaphore.release(); throw new JobRestartException(exceptionHolder.get(0)); } } catch (Exception e) { jobExecution.upgradeStatus(BatchStatus.FAILED); if (jobExecution.getExitStatus().equals(ExitStatus.UNKNOWN)) { jobExecution.setExitStatus(ExitStatus.FAILED.addExitDescription(e)); } jobRepository.update(jobExecution); if (batchContext.isActive()) { batchContext.close(); } throw new JobRestartException(e); } return jobExecution.getId(); }
From source file:functionaltests.RestSmartProxyTest.java
@Test(timeout = TEN_MINUTES) public void testInErrorEventsReception() throws Exception { TaskFlowJob job = createInErrorJob(); final Semaphore semaphore = new Semaphore(0); printJobXmlRepresentation(job);//from w ww.jav a2 s . co m final MutableBoolean taskHasBeenInError = new MutableBoolean(false); final MutableBoolean restartedFromErrorEventReceived = new MutableBoolean(false); SchedulerEventListenerExtended listener = new SchedulerEventListenerExtended() { @Override public void schedulerStateUpdatedEvent(SchedulerEvent eventType) { System.out.println("RestSmartProxyTest.schedulerStateUpdatedEvent " + eventType); } @Override public void jobSubmittedEvent(JobState job) { System.out.println("RestSmartProxyTest.jobSubmittedEvent"); } @Override public void jobStateUpdatedEvent(NotificationData<JobInfo> notification) { JobStatus status = notification.getData().getStatus(); System.out.println("RestSmartProxyTest.jobStateUpdatedEvent, eventType=" + notification.getEventType() + ", jobStatus=" + status); if (notification.getEventType() == SchedulerEvent.JOB_RESTARTED_FROM_ERROR) { restartedFromErrorEventReceived.setTrue(); } if (status == JobStatus.IN_ERROR) { semaphore.release(); } } @Override public void taskStateUpdatedEvent(NotificationData<TaskInfo> notification) { TaskStatus status = notification.getData().getStatus(); System.out.println("RestSmartProxyTest.taskStateUpdatedEvent, taskStatus=" + status); if (status == TaskStatus.WAITING_ON_ERROR || status == TaskStatus.IN_ERROR) { // IN_ERROR previously taskHasBeenInError.setTrue(); } } @Override public void usersUpdatedEvent(NotificationData<UserIdentification> notification) { System.out.println("RestSmartProxyTest.usersUpdatedEvent " + notification.getData()); } @Override public void pullDataFinished(String jobId, String taskName, String localFolderPath) { System.out.println("RestSmartProxyTest.pullDataFinished"); } @Override public void pullDataFailed(String jobId, String taskName, String remoteFolder_URL, Throwable t) { System.out.println("RestSmartProxyTest.pullDataFailed"); } @Override public void jobUpdatedFullDataEvent(JobState job) { System.out.println("RestSmartProxyTest.jobUpdatedFullDataEvent"); } }; restSmartProxy.addEventListener(listener); JobId jobId = restSmartProxy.submit(job, inputLocalFolder.getAbsolutePath(), pushUrl, outputLocalFolder.getAbsolutePath(), pullUrl, false, false); // the next line blocks until jobStateUpdatedEvent is called on the // listener // with job status set to IN_ERROR semaphore.acquire(); String jobIdAsString = jobId.value(); System.out.println("Restarting all In-Error tasks"); restSmartProxy.restartAllInErrorTasks(jobIdAsString); assertThat(restartedFromErrorEventReceived.booleanValue()).isTrue(); assertThat(taskHasBeenInError.booleanValue()).isTrue(); }
From source file:com.impetus.ankush2.ganglia.GangliaDeployer.java
@Override public boolean removeNode(final ClusterConfig conf, Collection<String> nodes) { try {//from ww w.java2s . c o m if (newClusterConf == null) { // setting clusterconf, componentconf and logger if (!initializeDataMembers(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()); } catch (Exception e) { addClusterError("Could not remove " + getComponentName(), e); return false; } return AnkushUtils.getStatus(conf.getNodes()); }
From source file:com.pinterest.pinlater.client.PinLaterQueryIssuer.java
private void issueDequeueAckRequests(final PinLater.ServiceIface iface) throws InterruptedException { Preconditions.checkNotNull(queueName, "Queue was not specified."); final AtomicLong queriesIssued = new AtomicLong(0); final Semaphore permits = new Semaphore(concurrency); while (numQueries == -1 || queriesIssued.get() < numQueries) { final PinLaterDequeueRequest request = new PinLaterDequeueRequest(); request.setQueueName(queueName); request.setLimit(batchSize);//from ww w .ja v a 2 s . co m final long startTimeNanos = System.nanoTime(); queriesIssued.incrementAndGet(); permits.acquire(); iface.dequeueJobs(REQUEST_CONTEXT, request) .flatMap(new Function<PinLaterDequeueResponse, Future<Void>>() { @Override public Future<Void> apply(PinLaterDequeueResponse response) { if (response.getJobsSize() == 0) { return Future.Void(); } PinLaterJobAckRequest jobAckRequest = new PinLaterJobAckRequest(queueName); for (String job : response.getJobs().keySet()) { if (random.nextInt(100) < dequeueSuccessPercent) { jobAckRequest.addToJobsSucceeded(new PinLaterJobAckInfo(job)); } else { jobAckRequest.addToJobsFailed(new PinLaterJobAckInfo(job)); } } return iface.ackDequeuedJobs(REQUEST_CONTEXT, jobAckRequest); } }).respond(new Function<Try<Void>, BoxedUnit>() { @Override public BoxedUnit apply(Try<Void> voidTry) { permits.release(); statsLogger .requestComplete(Duration.fromNanoseconds(System.nanoTime() - startTimeNanos)); if (voidTry.isThrow()) { LOG.info("Exception for request: " + request + " : " + ((Throw) voidTry).e()); } return BoxedUnit.UNIT; } }); } permits.acquire(concurrency); LOG.info("Dequeue/ack queries issued: " + queriesIssued); }
From source file:com.impetus.ankush2.cassandra.deployer.CassandraDeployer.java
/** * method to deploy Cassandra packages on all nodes * /* w ww . j a va 2 s . c om*/ * @param conf * {@link ClusterConfig} * @param nodeMap * {@link Map} * * @return <code>true</code>, if successful * */ private boolean deployNodes(final ClusterConfig conf, Set<String> nodeMap) throws AnkushException { try { // Node Deployment process ... final Semaphore semaphore = new Semaphore(nodeMap.size()); for (final String host : nodeMap) { semaphore.acquire(); AppStoreWrapper.getExecutor().execute(new Runnable() { @Override public void run() { conf.getNodes().get(host).setStatus(createNode(host)); if (semaphore != null) { semaphore.release(); } } }); } semaphore.acquire(nodeMap.size()); } catch (Exception e) { throw new AnkushException("Could not deploy " + getComponentName(), e); } return AnkushUtils.getStatus(conf.getNodes()); }
From source file:org.apache.bookkeeper.bookie.InterleavedLedgerStorageTest.java
@Test public void testConsistencyCheckConcurrentGC() throws Exception { final long signalDone = -1; final List<Exception> asyncErrors = new ArrayList<>(); final LinkedBlockingQueue<Long> toCompact = new LinkedBlockingQueue<>(); final Semaphore awaitingCompaction = new Semaphore(0); interleavedStorage.flush();// www . j ava 2 s . c om final long lastLogId = entryLogger.getLeastUnflushedLogId(); final MutableInt counter = new MutableInt(0); entryLogger.setCheckEntryTestPoint((ledgerId, entryId, entryLogId, pos) -> { if (entryLogId < lastLogId) { if (counter.intValue() % 100 == 0) { try { toCompact.put(entryLogId); awaitingCompaction.acquire(); } catch (InterruptedException e) { asyncErrors.add(e); } } counter.increment(); } }); Thread mutator = new Thread(() -> { EntryLogCompactor compactor = new EntryLogCompactor(conf, entryLogger, interleavedStorage, entryLogger::removeEntryLog); while (true) { Long next = null; try { next = toCompact.take(); if (next == null || next == signalDone) { break; } compactor.compact(entryLogger.getEntryLogMetadata(next)); } catch (BufferedChannelBase.BufferedChannelClosedException e) { // next was already removed, ignore } catch (Exception e) { asyncErrors.add(e); break; } finally { if (next != null) { awaitingCompaction.release(); } } } }); mutator.start(); List<LedgerStorage.DetectedInconsistency> inconsistencies = interleavedStorage .localConsistencyCheck(Optional.empty()); for (LedgerStorage.DetectedInconsistency e : inconsistencies) { LOG.error("Found: {}", e); } Assert.assertEquals(0, inconsistencies.size()); toCompact.offer(signalDone); mutator.join(); for (Exception e : asyncErrors) { throw e; } if (!conf.isEntryLogPerLedgerEnabled()) { Assert.assertNotEquals(0, statsProvider.getCounter(BOOKIE_SCOPE + "." + STORAGE_SCRUB_PAGE_RETRIES).get().longValue()); } }
From source file:com.impetus.ankush2.cassandra.deployer.CassandraDeployer.java
@Override public boolean register(final ClusterConfig conf) { try {// w w w.ja v a 2s . c om ComponentConfig compConfig = clusterConfig.getComponents().get(this.componentName); if (!AnkushUtils.isMonitoredByAnkush(compConfig)) { logger.info("Skipping " + getComponentName() + " registration for Level1", this.componentName); return true; } final String infoMsg = "Registering Cassandra"; logger.info(infoMsg); // Getting node map for cluster deployment Map<String, Map<String, Object>> nodeMap = new HashMap<String, Map<String, Object>>( componentConfig.getNodes()); // Node Registration process ... final Semaphore semaphore = new Semaphore(nodeMap.size()); for (final String host : nodeMap.keySet()) { semaphore.acquire(); AppStoreWrapper.getExecutor().execute(new Runnable() { @Override public void run() { conf.getNodes().get(host).setStatus(registerNode(host)); if (semaphore != null) { semaphore.release(); } } }); } semaphore.acquire(nodeMap.size()); // Return false if any of the node is not deployed. return AnkushUtils.getStatus(conf.getNodes()); } catch (Exception e) { return addClusterError("Could not register " + getComponentName(), e); } }
From source file:com.impetus.ankush2.cassandra.deployer.CassandraDeployer.java
@Override public boolean unregister(final ClusterConfig conf) { try {//ww w. java 2 s . co m ComponentConfig compConfig = clusterConfig.getComponents().get(this.componentName); if (!AnkushUtils.isMonitoredByAnkush(compConfig)) { logger.info("Skipping " + getComponentName() + " unregistration for Level1", this.componentName); return true; } if (!setClassVariables(conf)) { return false; } final String infoMsg = "Unregistering Cassandra"; logger.info(infoMsg, getComponentName()); // Getting node map for cluster deployment Map<String, Map<String, Object>> nodeMap = new HashMap<String, Map<String, Object>>( componentConfig.getNodes()); // Node Registration process ... final Semaphore semaphore = new Semaphore(nodeMap.size()); for (final String host : nodeMap.keySet()) { semaphore.acquire(); AppStoreWrapper.getExecutor().execute(new Runnable() { @Override public void run() { conf.getNodes().get(host).setStatus(unregisterNode(host)); if (semaphore != null) { semaphore.release(); } } }); } semaphore.acquire(nodeMap.size()); // Return false if any of the node is not deployed. return AnkushUtils.getStatus(conf.getNodes()); } catch (AnkushException e) { return addClusterError(e.getMessage(), e); } catch (Exception e) { return addClusterError("Could not unregister " + getComponentName(), e); } }
From source file:com.solace.samples.BasicRequestor.java
public void run(String... args) { System.out.println("BasicRequestor initializing..."); try {/* ww w . j a va2s .c om*/ // Create an Mqtt client final MqttClient mqttClient = new MqttClient("tcp://" + args[0], "HelloWorldBasicRequestor"); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(true); // Connect the client System.out.println("Connecting to Solace broker: tcp://" + args[0]); mqttClient.connect(connOpts); System.out.println("Connected"); // Semaphore used for synchronizing b/w threads final Semaphore latch = new Semaphore(0); // Topic the client will use to send request messages final String requestTopic = "T/GettingStarted/request"; // Callback - Anonymous inner-class for receiving the Reply-To topic from the Solace broker mqttClient.setCallback(new MqttCallback() { public void messageArrived(String topic, MqttMessage message) throws Exception { // If the topic is "$SYS/client/reply-to" then set our replyToTopic // to with the contents of the message payload received if (topic != null && topic.equals("$SYS/client/reply-to")) { replyToTopic = new String(message.getPayload()); System.out.println("\nReceived Reply-to topic from Solace for the MQTT client:" + "\n\tReply-To: " + replyToTopic + "\n"); } else { // Received a response to our request try { // Parse the response payload and convert to a JSONObject Object obj = parser.parse(new String(message.getPayload())); JSONObject jsonPayload = (JSONObject) obj; System.out.println("\nReceived a response!" + "\n\tCorrel. Id: " + (String) jsonPayload.get("correlationId") + "\n\tMessage: " + (String) jsonPayload.get("message") + "\n"); } catch (ParseException ex) { System.out.println("Exception parsing response message!"); ex.printStackTrace(); } } latch.release(); // unblock main thread } public void connectionLost(Throwable cause) { System.out.println("Connection to Solace broker lost!" + cause.getMessage()); latch.release(); } public void deliveryComplete(IMqttDeliveryToken token) { } }); // Subscribe client to the special Solace topic for requesting a unique // Reply-to destination for the MQTT client System.out.println("Requesting Reply-To topic from Solace..."); mqttClient.subscribe("$SYS/client/reply-to", 0); // Wait for till we have received the reply to Topic try { latch.acquire(); } catch (InterruptedException e) { System.out.println("I was awoken while waiting"); } // Check if we have a Reply-To topic if (replyToTopic == null || replyToTopic.isEmpty()) { System.out.println("Unable to request Reply-To from Solace. Exiting"); System.exit(0); } // Subscribe client to the Solace provide Reply-To topic with a QoS level of 0 System.out.println("Subscribing client to Solace provide Reply-To topic"); mqttClient.subscribe(replyToTopic, 0); // Create the request payload in JSON format JSONObject obj = new JSONObject(); obj.put("correlationId", UUID.randomUUID().toString()); obj.put("replyTo", replyToTopic); obj.put("message", "Sample Request"); String reqPayload = obj.toJSONString(); // Create a request message and set the request payload MqttMessage reqMessage = new MqttMessage(reqPayload.getBytes()); reqMessage.setQos(0); System.out.println("Sending request to: " + requestTopic); // Publish the request message mqttClient.publish(requestTopic, reqMessage); // Wait for till we have received a response try { latch.acquire(); // block here until message received } catch (InterruptedException e) { System.out.println("I was awoken while waiting"); } // Disconnect the client mqttClient.disconnect(); System.out.println("Exiting"); System.exit(0); } catch (MqttException me) { System.out.println("reason " + me.getReasonCode()); System.out.println("msg " + me.getMessage()); System.out.println("loc " + me.getLocalizedMessage()); System.out.println("cause " + me.getCause()); System.out.println("excep " + me); me.printStackTrace(); } }