Example usage for java.util.concurrent Semaphore release

List of usage examples for java.util.concurrent Semaphore release

Introduction

In this page you can find the example usage for java.util.concurrent Semaphore release.

Prototype

public void release() 

Source Link

Document

Releases a permit, returning it to the semaphore.

Usage

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  w  w  w .  j a  v a2  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.commoncrawl.service.listcrawler.CrawlHistoryManager.java

private static void testWriteMapFileToHDFS(EventLoop eventLoop) {
    try {//from  w ww .  j  av  a  2s  . c  o  m
        // initialize log manager
        CrawlHistoryManager logManager = initializeTestLogManager(eventLoop, true);

        // initialize item list
        TreeMap<URLFP, ProxyCrawlHistoryItem> items = buildTestList(urlList1);
        final TreeMap<String, URLFP> urlToURLFPMap = new TreeMap<String, URLFP>();

        for (Map.Entry<URLFP, ProxyCrawlHistoryItem> item : items.entrySet()) {
            urlToURLFPMap.put(item.getValue().getOriginalURL(), item.getKey());
        }

        // add to local item map in log manager
        for (ProxyCrawlHistoryItem item : items.values()) {
            logManager.appendItemToLog(item);
        }
        // ok shutdown log manager ...
        logManager.shutdown();

        // restart - reload log file ...
        logManager = initializeTestLogManager(eventLoop, false);

        // write to 'hdfs'
        logManager.doCheckpoint();

        syncAndValidateItems(items, logManager);

        logManager.shutdown();

        // restart
        logManager = initializeTestLogManager(eventLoop, false);

        // tweak original items
        updateTestItemStates(items);

        // ok append items
        for (ProxyCrawlHistoryItem item : items.values()) {
            logManager.appendItemToLog(item);
        }

        syncAndValidateItems(items, logManager);

        // ok now checkpoint the items
        logManager.doCheckpoint();

        // ok now validate one last time
        syncAndValidateItems(items, logManager);

        // shutown
        logManager.shutdown();

        logManager = null;

        {
            // start from scratch ...
            final CrawlHistoryManager logManagerTest = initializeTestLogManager(eventLoop, true);

            // create a final version of the tree map reference
            final TreeMap<URLFP, ProxyCrawlHistoryItem> itemList = items;
            // create filename
            File urlInputFile = new File(logManagerTest.getLocalDataDir(),
                    "testURLS-" + System.currentTimeMillis());
            // ok create a crawl list from urls
            CrawlList.generateTestURLFile(urlInputFile, urlList1);
            long listId = logManagerTest.loadList(urlInputFile, 0);

            CrawlList listObject = logManagerTest.getList(listId);

            final Semaphore listCompletionSemaphore = new Semaphore(-(itemList.size() - 1));

            listObject.setEventListener(new CrawlList.CrawlListEvents() {

                @Override
                public void itemUpdated(URLFP itemFingerprint) {
                    // TODO Auto-generated method stub
                    listCompletionSemaphore.release();
                }
            });

            // ok start the appropriate threads
            logManagerTest.startLogWriterThread(0);
            logManagerTest.startListLoaderThread();
            logManagerTest.startQueueLoaderThread(new CrawlQueueLoader() {

                @Override
                public void queueURL(URLFP urlfp, String url) {
                    logManagerTest.crawlComplete(
                            proxyCrawlHitoryItemToCrawlURL(itemList.get(urlToURLFPMap.get(url))));
                }

                @Override
                public void flush() {
                    // TODO Auto-generated method stub

                }
            });

            LOG.info("Waiting for Release");

            // and wait for the finish
            listCompletionSemaphore.acquireUninterruptibly();

            LOG.info("Got Here");

        }

    } catch (IOException e) {
        LOG.error(CCStringUtils.stringifyException(e));
    }
}

From source file:org.openengsb.opencit.core.projectmanager.internal.ProjectManagerImplTest.java

@Test
@SuppressWarnings("unchecked")
public void build_shouldSuspendPoller() throws Exception {
    List<CommitRef> fakeCommits = new LinkedList<CommitRef>();
    fakeCommits.add(Mockito.mock(CommitRef.class));

    final Semaphore eventSync = new Semaphore(0);
    when(workflowService.startFlow(eq("ci"), any(Map.class))).thenReturn(1L);
    doAnswer(new Answer<Void>() {
        @Override//from w  w w. j  av a 2 s  .  co m
        public Void answer(InvocationOnMock invocation) throws Throwable {
            eventSync.acquire();
            return null;
        }
    }).when(workflowService).waitForFlowToFinish(eq(1L), anyLong());
    when(scmMock.update()).thenReturn(fakeCommits, (List<CommitRef>[]) null);

    scheduler.setPollInterval(100L);

    Project project = new Project("test");
    project.setState(State.OK);
    projectManager.createProject(project);
    Thread.sleep(200);
    assertThat(scheduler.isProjectBuilding("test"), is(true));
    Thread.sleep(200);

    verify(scmMock).update();

    eventSync.release();

    Thread.sleep(200);

    assertThat(scheduler.isProjectBuilding("test"), is(false));
    assertThat(scheduler.isProjectPolling("test"), is(true));
}

From source file:com.thoughtworks.go.agent.bootstrapper.AgentBootstrapperTest.java

@Test
public void shouldNotDieWhenCreationOfLauncherRaisesException() throws InterruptedException {
    final Semaphore waitForLauncherCreation = new Semaphore(1);
    waitForLauncherCreation.acquire();/*  ww  w.  ja va 2s .  c om*/
    final boolean[] reLaunchWaitIsCalled = new boolean[1];
    final AgentBootstrapper bootstrapper = new AgentBootstrapper() {
        @Override
        void waitForRelaunchTime() {
            assertThat(waitTimeBeforeRelaunch, is(0));
            reLaunchWaitIsCalled[0] = true;
            super.waitForRelaunchTime();
        }

        @Override
        AgentLauncherCreator getLauncherCreator() {
            return new AgentLauncherCreator() {
                public AgentLauncher createLauncher() {
                    try {
                        throw new RuntimeException("i bombed");
                    } finally {
                        if (waitForLauncherCreation.availablePermits() == 0) {
                            waitForLauncherCreation.release();
                        }
                    }
                }

                @Override
                public void close() {
                }
            };
        }
    };

    final AgentBootstrapper spyBootstrapper = stubJVMExit(bootstrapper);

    Thread stopLoopThd = new Thread(new Runnable() {
        public void run() {
            try {
                waitForLauncherCreation.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            ReflectionUtil.setField(spyBootstrapper, "loop", false);
        }
    });
    stopLoopThd.start();
    try {
        spyBootstrapper.go(true,
                new AgentBootstrapperArgs(new URL("http://" + "ghost-name" + ":" + 3518 + "/go"), null,
                        AgentBootstrapperArgs.SslMode.NONE));
        stopLoopThd.join();
    } catch (Exception e) {
        fail("should not have propagated exception thrown while creating launcher");
    }
    assertThat(reLaunchWaitIsCalled[0], is(true));
}

From source file:com.boundlessgeo.geoserver.api.controllers.ThumbnailController.java

/**
 * Clears any cached thumbnail information (called by {@link ThumbnailInvalidatingCatalogListener} when a layer is removed).
 * //  ww w .  j a  va  2 s  . c  om
 * @param layer
 * @throws InterruptedException 
 */
public void clearThumbnail(PublishedInfo layer) {
    Semaphore s = semaphores.get(layer);
    try {
        s.acquire();
    } catch (InterruptedException e) {
        LOG.finer("Unable to clear thumbnail for " + layer.prefixedName() + ":" + e.getMessage());
        return;
    }
    try {
        File loRes = config.cacheFile(thumbnailFilename(layer));
        if (loRes.exists()) {
            boolean removed = loRes.delete();
            if (!removed) {
                loRes.deleteOnExit();
            }
        }
        File hiRes = config.cacheFile(thumbnailFilename(layer, true));
        if (hiRes.exists()) {
            boolean removed = hiRes.delete();
            if (!removed) {
                hiRes.deleteOnExit();
            }
        }
    } finally {
        s.release();
    }
}

From source file:org.mahasen.node.MahasenNodeManager.java

/**
 * @param myContent// ww w  .  j  ava 2  s  .c  om
 */
public void insertPastContent(MahasenPropertyPastContent myContent) {

    //  String result = this.lookupDHT(resourceId);
    final Semaphore control = new Semaphore(0);

    mahasenPastTreeApp.insert(myContent, new Continuation<Boolean[], Exception>() {
        // the result is an Array of Booleans for each insert
        public void receiveResult(Boolean[] results) {

            int numSuccessfulStores = 0;

            for (int ctr = 0; ctr < results.length; ctr++) {
                if (results[ctr].booleanValue())
                    numSuccessfulStores++;
            }

            control.release();

            System.out.println(" PropertyTree successfully stored at " + +numSuccessfulStores + " locations.");
        }

        public void receiveException(Exception result) {
            System.out.println("Error storing ");
            result.printStackTrace();
            control.release();

        }
    });

    try {
        control.tryAcquire(1000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        log.error("Time out while trying to insert Property tree into DHT");
    }
}

From source file:com.bt.sdk.callcontrol.sip.util.EhCacheCollectionImpl.java

@SuppressWarnings("unchecked")
public T get(String infoId) {
    if (infoId == null)
        throw new IllegalArgumentException("Info id must not be null");

    Semaphore semaphore = (Semaphore) semaphoreCache.get(infoId).getObjectValue();
    if (semaphore == null) {
        log.debug(String.format("No info object for %s in %s, returning null ", infoId,
                this.getClass().getSimpleName()));
        return null;
    }/*from   ww  w  .  j a  v a 2  s.co  m*/

    try {
        semaphore.acquire();
    } catch (InterruptedException e) {
        log.error(String.format(FAILED_TO_READ_OBJECT_MESSAGE, infoId, this.getClass().getSimpleName(),
                e.getMessage()));
        throw new CollectionAccessInterruptedException(String.format(FAILED_TO_READ_OBJECT_MESSAGE, infoId,
                this.getClass().getSimpleName(), e.getMessage()), e);
    }
    try {
        T result = ((T) cache.get(infoId).getObjectValue()).cloneObject();

        log.debug(String.format("Retrieved info %s with version %s", infoId, result.getVersionId()));
        return (T) result;
    } finally {
        semaphore.release();
    }
}

From source file:com.pinterest.pinlater.client.PinLaterQueryIssuer.java

private void issueEnqueueRequests(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 PinLaterEnqueueRequest request = new PinLaterEnqueueRequest();
        request.setQueueName(queueName);
        for (int i = 0; i < batchSize; i++) {
            PinLaterJob job = new PinLaterJob(
                    ByteBuffer.wrap(new String("task_" + random.nextInt(Integer.MAX_VALUE)).getBytes()));
            job.setPriority(priority);/*from  w ww  .  j a v  a 2  s  .c  o m*/
            request.addToJobs(job);
        }
        final long startTimeNanos = System.nanoTime();
        queriesIssued.incrementAndGet();
        permits.acquire();
        iface.enqueueJobs(REQUEST_CONTEXT, request)
                .respond(new Function<Try<PinLaterEnqueueResponse>, BoxedUnit>() {
                    @Override
                    public BoxedUnit apply(Try<PinLaterEnqueueResponse> responseTry) {
                        permits.release();
                        statsLogger
                                .requestComplete(Duration.fromNanoseconds(System.nanoTime() - startTimeNanos));
                        if (responseTry.isThrow()) {
                            LOG.info("Exception for request: " + request + " : " + ((Throw) responseTry).e());
                        }
                        return BoxedUnit.UNIT;
                    }
                });
    }
    permits.acquire(concurrency);
    LOG.info("Enqueue queries issued: " + queriesIssued);
}

From source file:org.mahasen.node.MahasenNodeManager.java

/**
 * @param id//  w ww  .  ja v  a2 s  .  com
 */
public void deleteContent(final Id id) {

    final Semaphore control = new Semaphore(0);

    Continuation continuation = new Continuation() {

        public void receiveResult(Object result) {
            if (result instanceof MahasenPastContent) {
                System.out.println(" recieved result for " + ((MahasenPastContent) result).getId());

            }
        }

        public void receiveException(Exception result) {
            result.printStackTrace();
        }
    };

    mahasenPastApp.delete(id, continuation);
    control.release();
}

From source file:org.openengsb.opencit.core.projectmanager.internal.ProjectManagerImplTest.java

@Test
@SuppressWarnings("unchecked")
public void buildManually_shouldSuspendPoller() throws Exception {
    final Semaphore eventSync = new Semaphore(0);
    when(workflowService.startFlow(eq("ci"), any(Map.class))).thenReturn(1L);
    doAnswer(new Answer<Void>() {
        @Override//from  w w w .  j a  v a  2  s . co m
        public Void answer(InvocationOnMock invocation) throws Throwable {
            eventSync.acquire();
            return null;
        }
    }).when(workflowService).waitForFlowToFinish(eq(1L), anyLong());
    when(scmMock.update()).thenReturn(null);

    Project project = new Project("test");
    project.setState(State.OK);
    projectManager.createProject(project);
    Thread.sleep(200);
    scheduler.scheduleProjectForBuild("test", new TestBuild());
    assertThat(scheduler.isProjectBuilding("test"), is(true));
    assertThat(scheduler.isProjectPolling("test"), is(false));
    Thread.sleep(200);
    verify(scmMock).update();

    eventSync.release();
    Thread.sleep(200);

    assertThat(scheduler.isProjectBuilding("test"), is(false));
    assertThat(scheduler.isProjectPolling("test"), is(true));
}