List of usage examples for java.util.concurrent Semaphore Semaphore
public Semaphore(int permits)
From source file:io.mindmaps.loader.DistributedLoader.java
public DistributedLoader(String graphNameInit, Collection<String> hosts) { ConfigProperties prop = ConfigProperties.getInstance(); batchSize = prop.getPropertyAsInt(ConfigProperties.BATCH_SIZE_PROPERTY); graphName = graphNameInit;//ww w. j a v a 2 s .com batch = new HashSet<>(); hostsArray = hosts.toArray(new String[hosts.size()]); currentHost = 0; pollingFrequency = 30000; threadsNumber = prop.getPropertyAsInt(ConfigProperties.NUM_THREADS_PROPERTY) * 3; // create availability map availability = new HashMap<>(); hosts.forEach(h -> availability.put(h, new Semaphore(threadsNumber))); jobsTerminated = new HashMap<>(); hosts.forEach(h -> jobsTerminated.put(h, 0)); }
From source file:edu.umn.msi.tropix.common.jobqueue.jobprocessors.BaseExecutableJobProcessorFactoryImpl.java
public void setMaxConcurrentProcessingJobs(final String maxConcurrentProcessingJobs) { if (StringUtils.hasText(maxConcurrentProcessingJobs) && !maxConcurrentProcessingJobs.startsWith("$")) { final int maxConcurrentProcessingJobsInt = Integer.parseInt(maxConcurrentProcessingJobs); LOG.info("Constructing job processing semaphore allowing " + maxConcurrentProcessingJobsInt + " concurrent jobs to preprocess."); final Semaphore semaphore = new Semaphore(maxConcurrentProcessingJobsInt); this.processingSemaphore = Optional.of(semaphore); }/*from www. j av a 2 s. c om*/ }
From source file:org.artifactory.storage.binstore.service.providers.FileCacheBinaryProviderImpl.java
@Override public void initialize() { super.initialize(); lruCache = Maps.newConcurrentMap();/*from w ww.j av a 2s. co m*/ totalSize = new AtomicLong(0); maxTotalSize = getLongParam("maxSize", getStorageProperties().getBinaryProviderCacheMaxSize()); cacheCleanerSemaphore = new Semaphore(1); syncCacheEntries(); }
From source file:com.netflix.curator.ensemble.exhibitor.TestExhibitorEnsembleProvider.java
@Test public void testExhibitorFailures() throws Exception { final AtomicReference<String> backupConnectionString = new AtomicReference<String>("backup1:1"); final AtomicReference<String> connectionString = new AtomicReference<String>( "count=1&port=2&server0=localhost"); Exhibitors exhibitors = new Exhibitors(Lists.newArrayList("foo", "bar"), 1000, new Exhibitors.BackupConnectionStringProvider() { @Override//from w ww. ja va 2 s. c o m public String getBackupConnectionString() { return backupConnectionString.get(); } }); ExhibitorRestClient mockRestClient = new ExhibitorRestClient() { @Override public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception { String localConnectionString = connectionString.get(); if (localConnectionString == null) { throw new IOException(); } return localConnectionString; } }; final Semaphore semaphore = new Semaphore(0); ExhibitorEnsembleProvider provider = new ExhibitorEnsembleProvider(exhibitors, mockRestClient, "/foo", 10, new RetryOneTime(1)) { @Override protected void poll() { super.poll(); semaphore.release(); } }; provider.pollForInitialEnsemble(); try { provider.start(); Assert.assertEquals(provider.getConnectionString(), "localhost:2"); connectionString.set(null); semaphore.drainPermits(); semaphore.acquire(); // wait for next poll Assert.assertEquals(provider.getConnectionString(), "backup1:1"); backupConnectionString.set("backup2:2"); semaphore.drainPermits(); semaphore.acquire(); // wait for next poll Assert.assertEquals(provider.getConnectionString(), "backup2:2"); connectionString.set("count=1&port=3&server0=localhost3"); semaphore.drainPermits(); semaphore.acquire(); // wait for next poll Assert.assertEquals(provider.getConnectionString(), "localhost3:3"); } finally { IOUtils.closeQuietly(provider); } }
From source file:org.apache.hadoop.hdfs.server.blockmanagement.TestBlockReportRateLimiting.java
@Test(timeout = 180000) public void testRateLimitingDuringDataNodeStartup() 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, 20L * 60L * 1000L); final Semaphore fbrSem = new Semaphore(0); final HashSet<DatanodeID> expectedFbrDns = new HashSet<>(); final HashSet<DatanodeID> fbrDns = new HashSet<>(); final AtomicReference<String> failure = new AtomicReference<String>(""); final BlockManagerFaultInjector injector = new BlockManagerFaultInjector() { private int numLeases = 0; @Override/*from w w w. j a va 2 s. c o m*/ public void incomingBlockReportRpc(DatanodeID nodeID, BlockReportContext context) throws IOException { LOG.info("Incoming full block report from " + nodeID + ". Lease ID = 0x" + Long.toHexString(context.getLeaseId())); if (context.getLeaseId() == 0) { setFailure(failure, "Got unexpected rate-limiting-" + "bypassing full block report RPC from " + nodeID); } fbrSem.acquireUninterruptibly(); synchronized (this) { fbrDns.add(nodeID); if (!expectedFbrDns.remove(nodeID)) { setFailure(failure, "Got unexpected full block report " + "RPC from " + nodeID + ". expectedFbrDns = " + Joiner.on(", ").join(expectedFbrDns)); } LOG.info("Proceeding with full block report from " + nodeID + ". Lease ID = 0x" + Long.toHexString(context.getLeaseId())); } } @Override public void requestBlockReportLease(DatanodeDescriptor node, long leaseId) { if (leaseId == 0) { return; } synchronized (this) { numLeases++; expectedFbrDns.add(node); LOG.info("requestBlockReportLease(node=" + node + ", leaseId=0x" + Long.toHexString(leaseId) + "). " + "expectedFbrDns = " + Joiner.on(", ").join(expectedFbrDns)); if (numLeases > 1) { setFailure(failure, "More than 1 lease was issued at once."); } } } @Override public void removeBlockReportLease(DatanodeDescriptor node, long leaseId) { LOG.info("removeBlockReportLease(node=" + node + ", leaseId=0x" + Long.toHexString(leaseId) + ")"); synchronized (this) { numLeases--; } } }; BlockManagerFaultInjector.instance = injector; final int NUM_DATANODES = 5; MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATANODES).build(); cluster.waitActive(); for (int n = 1; n <= NUM_DATANODES; n++) { LOG.info("Waiting for " + n + " datanode(s) to report in."); fbrSem.release(); Uninterruptibles.sleepUninterruptibly(20, TimeUnit.MILLISECONDS); final int currentN = n; GenericTestUtils.waitFor(new Supplier<Boolean>() { @Override public Boolean get() { synchronized (injector) { if (fbrDns.size() > currentN) { setFailure(failure, "Expected at most " + currentN + " datanodes to have sent a block report, but actually " + fbrDns.size() + " have."); } return (fbrDns.size() >= currentN); } } }, 25, 50000); } cluster.shutdown(); Assert.assertEquals("", failure.get()); }
From source file:com.taobao.gecko.service.impl.DefaultRemotingContext.java
public DefaultRemotingContext(final BaseConfig config, final CommandFactory commandFactory) { this.groupManager = new GroupManager(); this.config = config; if (commandFactory == null) { throw new IllegalArgumentException("CommandFactory"); }//from w w w .j a v a 2 s.c om this.commandFactory = commandFactory; this.callBackSemaphore = new Semaphore(this.config.getMaxCallBackCount()); MBeanUtils.registerMBeanWithIdPrefix(this, null); }
From source file:org.jbpm.EventCallback.java
private static Semaphore getEventSemaphore(String event) { synchronized (eventSemaphores) { Semaphore semaphore = eventSemaphores.get(event); if (semaphore == null) { semaphore = new Semaphore(0); eventSemaphores.put(event, semaphore); }// ww w . j a va 2 s. c o m return semaphore; } }
From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessSemaphoreCluster.java
@Test public void testKilledServerWithEnsembleProvider() throws Exception { final int CLIENT_QTY = 10; final Timing timing = new Timing(); final String PATH = "/foo/bar/lock"; ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_QTY); ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService); TestingCluster cluster = new TestingCluster(3); try {//from w w w . jav a 2 s. com cluster.start(); final AtomicReference<String> connectionString = new AtomicReference<String>( cluster.getConnectString()); final EnsembleProvider provider = new EnsembleProvider() { @Override public void start() throws Exception { } @Override public String getConnectionString() { return connectionString.get(); } @Override public void close() throws IOException { } }; final Semaphore acquiredSemaphore = new Semaphore(0); final AtomicInteger acquireCount = new AtomicInteger(0); final CountDownLatch suspendedLatch = new CountDownLatch(CLIENT_QTY); for (int i = 0; i < CLIENT_QTY; ++i) { completionService.submit(new Callable<Void>() { @Override public Void call() throws Exception { CuratorFramework client = CuratorFrameworkFactory.builder().ensembleProvider(provider) .sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()) .retryPolicy(new ExponentialBackoffRetry(100, 3)).build(); try { final Semaphore suspendedSemaphore = new Semaphore(0); client.getConnectionStateListenable().addListener(new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { if ((newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)) { suspendedLatch.countDown(); suspendedSemaphore.release(); } } }); client.start(); InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, PATH, 1); while (!Thread.currentThread().isInterrupted()) { Lease lease = null; try { lease = semaphore.acquire(); acquiredSemaphore.release(); acquireCount.incrementAndGet(); suspendedSemaphore.acquire(); } catch (Exception e) { // just retry } finally { if (lease != null) { acquireCount.decrementAndGet(); IOUtils.closeQuietly(lease); } } } } finally { IOUtils.closeQuietly(client); } return null; } }); } Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore)); Assert.assertEquals(1, acquireCount.get()); cluster.close(); timing.awaitLatch(suspendedLatch); timing.forWaiting().sleepABit(); Assert.assertEquals(0, acquireCount.get()); cluster = new TestingCluster(3); cluster.start(); connectionString.set(cluster.getConnectString()); timing.forWaiting().sleepABit(); Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore)); timing.forWaiting().sleepABit(); Assert.assertEquals(1, acquireCount.get()); } finally { executorService.shutdown(); executorService.awaitTermination(10, TimeUnit.SECONDS); executorService.shutdownNow(); IOUtils.closeQuietly(cluster); } }
From source file:com.alibaba.napoli.gecko.service.impl.DefaultRemotingContext.java
public DefaultRemotingContext(final BaseConfig config, final CommandFactory commandFactory) { this.groupManager = new GroupManager(); this.config = config; if (commandFactory == null) { throw new IllegalArgumentException("CommandFactory?"); }//from w ww .j a va2s.c o m this.commandFactory = commandFactory; this.callBackSemaphore = new Semaphore(this.config.getMaxCallBackCount()); MBeanUtils.registerMBeanWithIdPrefix(this, null); }
From source file:com.amazonaws.services.simpleworkflow.flow.worker.GenericActivityWorker.java
protected Semaphore createPollSemaphore() { return new Semaphore(taskExecutorThreadPoolSize); }