List of usage examples for java.util.concurrent Semaphore acquire
public void acquire() throws InterruptedException
From source file:org.knime.al.nodes.score.novelty.localnoveltyscorer.LocalNoveltyScorer.java
public double[] calculateNoveltyScores() throws Exception { final ThreadPool pool = KNIMEConstants.GLOBAL_THREAD_POOL; final int procCount = (int) (Runtime.getRuntime().availableProcessors() * (2.0 / 3)); final Semaphore semaphore = new Semaphore(procCount); final int numTestSamples = m_globalKernelMatrix.getColumnDimension(); final NoveltyScoreCalculationCallable[] nct = new NoveltyScoreCalculationCallable[numTestSamples]; for (int i = 0; i < numTestSamples; i++) { nct[i] = new NoveltyScoreCalculationCallable(i, semaphore, m_numNeighbors, m_trainingKernelMatrix, m_globalKernelMatrix, m_labels, m_normalize); }/*from w w w .j av a 2s .c o m*/ final Future<?>[] scores = new Future<?>[numTestSamples]; final KNIMETimer timer = KNIMETimer.getInstance(); final TimerTask timerTask = new TimerTask() { @Override public void run() { try { m_exec.checkCanceled(); } catch (final CanceledExecutionException ce) { for (int i = 0; i < scores.length; i++) { if (scores[i] != null) { scores[i].cancel(true); } } super.cancel(); } } }; timer.scheduleAtFixedRate(timerTask, 0, 3000); double progCounter = 0; for (int i = 0; i < numTestSamples; i++) { try { m_exec.checkCanceled(); } catch (final Exception e) { for (int j = 0; j < i; j++) { if (scores[j] != null) { scores[j].cancel(true); } } timerTask.cancel(); throw e; } semaphore.acquire(); scores[i] = pool.enqueue(nct[i]); m_exec.setProgress(progCounter / (2 * numTestSamples), "Local novelty score calculation started (" + i + "/" + numTestSamples + ")"); progCounter += 1; } final double[] result = new double[numTestSamples]; for (int i = 0; i < numTestSamples; i++) { semaphore.acquire(); try { m_exec.checkCanceled(); result[i] = (Double) scores[i].get(); nct[i].ok(); } catch (final Exception e) { for (int j = 0; j < scores.length; j++) { scores[j].cancel(true); } timerTask.cancel(); throw e; } m_exec.setProgress(progCounter / (2 * numTestSamples), "Local novelty score calculated (" + i + "/" + numTestSamples + ")"); progCounter += 1; semaphore.release(); } timerTask.cancel(); return result; }
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 w ww.j a v a2s .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 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 w w .j a v a2s. 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:com.netflix.curator.ensemble.exhibitor.TestExhibitorEnsembleProvider.java
@Test public void testChanging() throws Exception { TestingServer secondServer = new TestingServer(); try {/*from w ww. ja v a2 s . c o m*/ String mainConnectionString = "count=1&port=" + server.getPort() + "&server0=localhost"; String secondConnectionString = "count=1&port=" + secondServer.getPort() + "&server0=localhost"; final Semaphore semaphore = new Semaphore(0); final AtomicReference<String> connectionString = new AtomicReference<String>(mainConnectionString); Exhibitors exhibitors = new Exhibitors(Lists.newArrayList("foo", "bar"), 1000, dummyConnectionStringProvider); ExhibitorRestClient mockRestClient = new ExhibitorRestClient() { @Override public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception { semaphore.release(); return connectionString.get(); } }; ExhibitorEnsembleProvider provider = new ExhibitorEnsembleProvider(exhibitors, mockRestClient, "/foo", 10, new RetryOneTime(1)); provider.pollForInitialEnsemble(); Timing timing = new Timing().multiple(4); final CuratorZookeeperClient client = new CuratorZookeeperClient(provider, timing.session(), timing.connection(), null, new RetryOneTime(2)); client.start(); try { RetryLoop.callWithRetry(client, new Callable<Object>() { @Override public Object call() throws Exception { client.getZooKeeper().create("/test", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return null; } }); connectionString.set(secondConnectionString); semaphore.drainPermits(); semaphore.acquire(); server.stop(); // create situation where the current zookeeper gets a sys-disconnected Stat stat = RetryLoop.callWithRetry(client, new Callable<Stat>() { @Override public Stat call() throws Exception { return client.getZooKeeper().exists("/test", false); } }); Assert.assertNull(stat); // it's a different server so should be null } finally { client.close(); } } finally { IOUtils.closeQuietly(secondServer); } }
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 {//w ww . j av a 2 s . c o m 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:org.apache.hadoop.hdfs.server.blockmanagement.TestBlockReportRateLimiting.java
/** * Start a 2-node cluster with only one block report lease. When the * first datanode gets a lease, kill it. Then wait for the lease to * expire, and the second datanode to send a full block report. *///from ww w . j a va2 s.co m @Test(timeout = 180000) public void testLeaseExpiration() throws Exception { Configuration conf = new Configuration(); conf.setInt(DFS_NAMENODE_MAX_FULL_BLOCK_REPORT_LEASES, 1); conf.setLong(DFS_NAMENODE_FULL_BLOCK_REPORT_LEASE_LENGTH_MS, 100L); final Semaphore gotFbrSem = new Semaphore(0); final AtomicReference<String> failure = new AtomicReference<>(); final AtomicReference<MiniDFSCluster> cluster = new AtomicReference<>(); final AtomicReference<String> datanodeToStop = new AtomicReference<>(); final BlockManagerFaultInjector injector = new BlockManagerFaultInjector() { @Override public void incomingBlockReportRpc(DatanodeID nodeID, BlockReportContext context) throws IOException { if (context.getLeaseId() == 0) { setFailure(failure, "Got unexpected rate-limiting-" + "bypassing full block report RPC from " + nodeID); } if (nodeID.getXferAddr().equals(datanodeToStop.get())) { throw new IOException("Injecting failure into block " + "report RPC for " + nodeID); } gotFbrSem.release(); } @Override public void requestBlockReportLease(DatanodeDescriptor node, long leaseId) { if (leaseId == 0) { return; } datanodeToStop.compareAndSet(null, node.getXferAddr()); } @Override public void removeBlockReportLease(DatanodeDescriptor node, long leaseId) { } }; try { BlockManagerFaultInjector.instance = injector; cluster.set(new MiniDFSCluster.Builder(conf).numDataNodes(2).build()); cluster.get().waitActive(); Assert.assertNotNull(cluster.get().stopDataNode(datanodeToStop.get())); gotFbrSem.acquire(); Assert.assertNull(failure.get()); } finally { if (cluster.get() != null) { cluster.get().shutdown(); } } }
From source file:org.knime.al.util.noveltydetection.kernel.KernelCalculator.java
public RealMatrix calculateKernelMatrix(final double[][] training, final double[][] test, final ExecutionMonitor progMon) throws Exception { final ThreadPool pool = KNIMEConstants.GLOBAL_THREAD_POOL; final int procCount = (int) (Runtime.getRuntime().availableProcessors() * (2.0 / 3)); final Semaphore semaphore = new Semaphore(procCount); RealMatrix result = null;//from w ww . java 2 s. c om try { result = pool.runInvisible(new Callable<RealMatrix>() { @Override public RealMatrix call() throws Exception { final double[][] resultArrayMatrix = new double[training.length][test.length]; final CalculateKernelValuesRunnable[] kct = new CalculateKernelValuesRunnable[test.length]; final int numberOfRunnables = kct.length; for (int i = 0; i < numberOfRunnables; i++) { kct[i] = new CalculateKernelValuesRunnable(0, training.length, i, i + 1, training, test, resultArrayMatrix, m_kernelFunction, semaphore); } final Future<?>[] threads = new Future<?>[numberOfRunnables]; double progCounter = 0; for (int i = 0; i < numberOfRunnables; i++) { try { progMon.checkCanceled(); } catch (final Exception e) { for (int j = 0; j < i; j++) { if (threads[j] != null) { threads[j].cancel(true); } } throw e; } semaphore.acquire(); threads[i] = pool.enqueue(kct[i]); progMon.setProgress(progCounter / (2 * numberOfRunnables), "Kernel calculation started (" + i + "/" + numberOfRunnables + ")"); progCounter += 1; } for (int i = 0; i < numberOfRunnables; i++) { try { progMon.checkCanceled(); } catch (final Exception e) { for (int j = 0; j < numberOfRunnables; j++) { if (threads[j] != null) { threads[j].cancel(true); } } throw e; } semaphore.acquire(); threads[i].get(); semaphore.release(); progMon.setProgress(progCounter / (2 * numberOfRunnables), "Kernel calculation finished (" + i + "/" + numberOfRunnables + ")"); progCounter += 1; } return MatrixUtils.createRealMatrix(resultArrayMatrix); } }); } catch (final Exception e) { throw e; } return result; }
From source file:com.arpnetworking.metrics.impl.ApacheHttpSinkTest.java
@Test public void testBatchesRequests() throws InterruptedException { _wireMockRule.stubFor(WireMock.requestMatching(new RequestValueMatcher(r -> { // Annotations Assert.assertEquals(0, r.getAnnotationsCount()); // Dimensions Assert.assertEquals(0, r.getDimensionsCount()); // Samples assertSample(r.getTimersList(), "timer", 7d); assertSample(r.getCountersList(), "counter", 8d); assertSample(r.getGaugesList(), "gauge", 9d); })).willReturn(WireMock.aResponse().withStatus(200))); final AtomicBoolean assertionResult = new AtomicBoolean(false); final Semaphore semaphore = new Semaphore(0); final Sink sink = new ApacheHttpSink.Builder() .setUri(URI.create("http://localhost:" + _wireMockRule.port() + PATH)).setMaxBatchSize(10) .setParallelism(1).setEmptyQueueInterval(Duration.ofMillis(1000)) .setEventHandler(new AttemptCompletedAssertionHandler(assertionResult, 3, 210, true, new CompletionHandler(semaphore))) .build();//from w w w .j a v a2 s. c om final TsdEvent event = new TsdEvent(Collections.emptyMap(), createQuantityMap("timer", TsdQuantity.newInstance(7d, null)), createQuantityMap("counter", TsdQuantity.newInstance(8d, null)), createQuantityMap("gauge", TsdQuantity.newInstance(9d, null))); for (int x = 0; x < 3; x++) { sink.record(event); } semaphore.acquire(); // Ensure expected handler was invoked Assert.assertTrue(assertionResult.get()); // Request matcher final RequestPatternBuilder requestPattern = WireMock.postRequestedFor(WireMock.urlEqualTo(PATH)) .withHeader("Content-Type", WireMock.equalTo("application/octet-stream")); // Assert that data was sent _wireMockRule.verify(1, requestPattern); Assert.assertTrue(_wireMockRule.findUnmatchedRequests().getRequests().isEmpty()); }
From source file:com.gooddata.http.client.GoodDataHttpClientIntegrationTest.java
License:asdf
@Test public void shouldRefreshTTConcurrent() throws Exception { mock401OnProjects();/*from w w w . j av a 2 s .c o m*/ // this serves to block second thread, until the first one gets 401 on projects, which causes TT refresh // the test aims to test the second thread is not cycling on 401 and cumulating wrong TT headers final Semaphore semaphore = new Semaphore(1); requestOnPath(GDC_PROJECTS_PATH, "TT1").respondUsing(new Responder() { boolean first = true; @Override public StubResponse nextResponse(Request request) { if (first) { first = false; return StubResponse.builder().status(200).body(BODY_PROJECTS, CHARSET) .header(CONTENT_HEADER, CONTENT_TYPE_JSON_UTF).build(); } else { semaphore.release(); return StubResponse.builder().status(401).body(BODY_401, CHARSET) .header(CONTENT_HEADER, CONTENT_TYPE_JSON_UTF) .header(WWW_AUTHENTICATE_HEADER, GOODDATA_REALM + " " + TT_COOKIE) .delay(5, TimeUnit.SECONDS).build(); } } }); mock200OnProjects("TT2"); mock401OnPath(GDC_PROJECTS2_PATH, "TT1"); mock200OnPath(GDC_PROJECTS2_PATH, "TT2"); mock401OnToken(); respond200OnToken(mock200OnToken("TT1").thenRespond(), "TT2"); mockLogin(); final HttpClient client = createGoodDataClient(jadlerLogin, jadlerPassword, jadlerHost); // one get at the beginning causing successful login performGet(client, jadlerHost, GDC_PROJECTS_PATH, 200); // to be able to finish when both threads finished final CountDownLatch countDown = new CountDownLatch(2); final ExecutorService executor = Executors.newFixedThreadPool(2); semaphore.acquire(); // will be released in jadler executor.submit(new PerformGetWithCountDown(client, GDC_PROJECTS_PATH, countDown)); semaphore.acquire(); // causes waiting executor.submit(new PerformGetWithCountDown(client, GDC_PROJECTS2_PATH, countDown)); countDown.await(10, TimeUnit.SECONDS); verifyThatRequest().havingMethodEqualTo("GET").havingPathEqualTo(GDC_TOKEN_PATH) .havingHeaderEqualTo(SST_HEADER, "SST") // if received more than twice, it means the second thread didn't wait, while the first was refreshing TT .receivedTimes(2); verifyThatRequest().havingMethodEqualTo("GET").havingPathEqualTo(GDC_PROJECTS2_PATH) .havingHeaderEqualTo(TT_HEADER, "TT1") // the second thread should try only once with expired TT1 .receivedOnce(); verifyThatRequest().havingMethodEqualTo("GET").havingPathEqualTo(GDC_PROJECTS2_PATH) .havingHeaderEqualTo(TT_HEADER, "TT1").havingHeaderEqualTo(TT_HEADER, "TT2") // the second thread should not set more than one X-GDC-AuthTT header .receivedNever(); }
From source file:io.pravega.client.stream.impl.ControllerImplTest.java
@Test public void testParallelCreateStream() throws Exception { final ExecutorService executorService = Executors.newFixedThreadPool(10); Semaphore createCount = new Semaphore(-19); AtomicBoolean success = new AtomicBoolean(true); for (int i = 0; i < 10; i++) { executorService.submit(() -> { for (int j = 0; j < 2; j++) { try { CompletableFuture<Boolean> createStreamStatus; createStreamStatus = controllerClient .createStream(StreamConfiguration.builder().streamName("streamparallel") .scope("scope1").scalingPolicy(ScalingPolicy.fixed(1)).build()); log.info("{}", createStreamStatus.get()); assertTrue(createStreamStatus.get()); createCount.release(); } catch (Exception e) { log.error("Exception when creating stream: {}", e); // Don't wait for other threads to complete. success.set(false);/*from ww w. ja v a 2s . co m*/ createCount.release(20); } } }); } createCount.acquire(); executorService.shutdownNow(); assertTrue(success.get()); }