Example usage for java.util.concurrent Semaphore Semaphore

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

Introduction

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

Prototype

public Semaphore(int permits) 

Source Link

Document

Creates a Semaphore with the given number of permits and nonfair fairness setting.

Usage

From source file:org.apache.kylin.storage.hbase.util.HbaseStreamingInput.java

public static void randomScan(String tableName) throws IOException {

    final Semaphore semaphore = new Semaphore(0);
    new Thread(new Runnable() {
        @Override//from   w  w w .j ava2  s .  c  om
        public void run() {
            scheduleJob(semaphore, 60000);//1 minutes a batch
        }
    }).start();

    while (true) {
        try {
            semaphore.acquire();
            int waiting = semaphore.drainPermits();
            if (waiting > 0) {
                logger.warn("Too many queries to handle! Blocking " + waiting + " sets of scan requests");
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }

        Random r = new Random();
        Connection conn = getConnection();
        Table table = conn.getTable(TableName.valueOf(tableName));

        long leftBound = getFirstKeyTime(table);
        long rightBound = System.currentTimeMillis();

        for (int t = 0; t < 5; ++t) {
            long start = (long) (leftBound + r.nextDouble() * (rightBound - leftBound));
            long end = start + 600000;//a period of 10 minutes
            logger.info("A scan from " + formatTime(start) + " to " + formatTime(end));

            Scan scan = new Scan();
            scan.setStartRow(Bytes.toBytes(start));
            scan.setStopRow(Bytes.toBytes(end));
            scan.addFamily(CF);
            ResultScanner scanner = table.getScanner(scan);
            long hash = 0;
            int rowCount = 0;
            for (Result result : scanner) {
                Cell cell = result.getColumnLatestCell(CF, QN);
                byte[] value = cell.getValueArray();
                if (cell.getValueLength() != CELL_SIZE) {
                    logger.error("value size invalid!!!!!");
                }

                hash += Arrays.hashCode(Arrays.copyOfRange(value, cell.getValueOffset(),
                        cell.getValueLength() + cell.getValueOffset()));
                rowCount++;
            }
            scanner.close();
            logger.info("Scanned " + rowCount + " rows, the (meaningless) hash for the scan is " + hash);
        }
        table.close();
        conn.close();
    }
}

From source file:ai.grakn.engine.loader.client.LoaderClient.java

/**
 * Set the size of the queue- this is equivalent to the size of the semaphore.
 * @param size the size of the queue/*  w  w  w .ja  va  2 s.  c om*/
 */
public Loader setQueueSize(int size) {
    // create availability map
    availability = new HashMap<>();
    hosts.forEach(h -> availability.put(h, new Semaphore(size)));
    return this;
}

From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessMutexBase.java

@Test
public void testKilledSession() throws Exception {
    final Timing timing = new Timing();

    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(),
            timing.connection(), new RetryOneTime(1));
    client.start();/*from   w ww  .j av  a 2  s  .  c  o  m*/
    try {
        final InterProcessLock mutex1 = makeLock(client);
        final InterProcessLock mutex2 = makeLock(client);

        final Semaphore semaphore = new Semaphore(0);
        ExecutorCompletionService<Object> service = new ExecutorCompletionService<Object>(
                Executors.newFixedThreadPool(2));
        service.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                mutex1.acquire();
                semaphore.release();
                Thread.sleep(1000000);
                return null;
            }
        });

        service.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                mutex2.acquire();
                semaphore.release();
                Thread.sleep(1000000);
                return null;
            }
        });

        Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
        KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
        Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
    } finally {
        client.close();
    }
}

From source file:lockstep.LockstepServer.java

public LockstepServer(int tcpPort, int clientsNumber, int tickrate, int maxUDPPayloadLength,
        int connectionTimeout) {
    //late fail left to Socket class
    this.tcpPort = tcpPort;

    if (clientsNumber <= 1)
        throw new IllegalArgumentException("clientsNumber must be at least 2");
    else//w w w.  j  a v  a 2 s.c om
        this.clientsNumber = clientsNumber;

    if (tickrate <= 0)
        throw new IllegalArgumentException("Tickrate must be an integer greater than 0");
    else
        this.tickrate = tickrate;

    if (maxUDPPayloadLength <= 0)
        throw new IllegalArgumentException("Max UDP payload length must be an integer greater than 0");
    else
        this.maxUDPPayloadLength = maxUDPPayloadLength;

    if (connectionTimeout < 0)
        throw new IllegalArgumentException("Connection timeout must be greater or equal than zero");
    else
        this.connectionTimeout = connectionTimeout;

    receivers = new HashMap<>();
    transmitters = new HashMap<>();

    executionSemaphore = new Semaphore(0);
    receivingQueues = new ConcurrentHashMap<>();
    transmissionFrameQueueTree = new ConcurrentHashMap<>();
    ackQueues = new HashMap<>();
    clientIDs = new ConcurrentSkipListSet<>();
    openSockets = new ArrayList<>();
}

From source file:com.gooddata.http.client.GoodDataHttpClientIntegrationTest.java

License:asdf

@Test
public void shouldRefreshTTConcurrent() throws Exception {
    mock401OnProjects();/*from ww w.  ja  va2s  .  co 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:org.onosproject.loadtest.DistributedConsensusLoadTest.java

private void startTest() {
    stopped.set(false);/*from www.  j  a  va2 s .  com*/
    RateLimiter limiter = RateLimiter.create(rate);
    Semaphore s = new Semaphore(100);
    while (!stopped.get()) {
        limiter.acquire();
        s.acquireUninterruptibly();
        counters.get(RandomUtils.nextInt(TOTAL_COUNTERS)).incrementAndGet().whenComplete((r, e) -> {
            s.release();
            if (e == null) {
                increments.incrementAndGet();
            }
        });
    }
}

From source file:com.parse.ParseAnalyticsTest.java

@Test
public void testTrackEventInBackgroundNormalCallback() throws Exception {
    final Map<String, String> dimensions = new HashMap<>();
    dimensions.put("key", "value");
    final Semaphore done = new Semaphore(0);
    ParseAnalytics.trackEventInBackground("test", dimensions, new SaveCallback() {
        @Override//from   ww  w. ja va 2s.  c  o  m
        public void done(ParseException e) {
            assertNull(e);
            done.release();
        }
    });

    // Make sure the callback is called
    assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
    verify(controller, times(1)).trackEventInBackground(eq("test"), eq(dimensions), isNull(String.class));

    final Semaphore doneAgain = new Semaphore(0);
    ParseAnalytics.trackEventInBackground("test", new SaveCallback() {
        @Override
        public void done(ParseException e) {
            assertNull(e);
            doneAgain.release();
        }
    });

    // Make sure the callback is called
    assertTrue(doneAgain.tryAcquire(1, 10, TimeUnit.SECONDS));
    verify(controller, times(1)).trackEventInBackground(eq("test"), Matchers.<Map<String, String>>eq(null),
            isNull(String.class));
}

From source file:org.hyperic.lather.server.LatherServlet.java

private ConnManager getConnManager(ServletConfig cfg) throws ServletException {
    @SuppressWarnings("unchecked")
    final Enumeration<String> paramNames = cfg.getInitParameterNames();
    final Map<String, Semaphore> maxConnMap = new HashMap<String, Semaphore>();
    while (paramNames.hasMoreElements()) {
        final String name = paramNames.nextElement();
        if (!name.startsWith(PROP_PREFIX) || name.contains(PROP_EXECTIMEOUT)) {
            continue;
        }/*from  w  w w  . ja  v  a  2  s  .  c o m*/
        final String param = cfg.getInitParameter(name);
        try {
            final int value = Integer.parseInt(param);
            maxConnMap.put(name.replace(PROP_PREFIX, ""), new Semaphore(value));
        } catch (NumberFormatException e) {
            log.error("could not initialize max conn setting for " + name + " value=" + param);
        }
    }
    if (!maxConnMap.containsKey(PROP_MAXCONNS)) {
        throw new ServletException("init-params do not contain key=" + PROP_MAXCONNS + ")");
    }
    return new ConnManager(maxConnMap);
}

From source file:ai.grakn.client.LoaderClient.java

/**
 * Number of active tasks running on the server at any one time.
 * Consider this a safeguard on system load.
 *
 * The Loader {@link #add(InsertQuery)} method will block on the value of this field.
 *
 * @param size number of tasks to allow to run at any given time
 *//*from  w w w. j  av a  2  s  .com*/
public LoaderClient setNumberActiveTasks(int size) {
    this.blockerSize = size;
    this.blocker = new Semaphore(size);
    return this;
}

From source file:io.hops.erasure_coding.ParallelStreamReader.java

/**
 * Reads data from multiple streams in parallel and puts the data in a queue.
 *
 * @param streams/*ww w  .  jav  a2s  . co m*/
 *     The input streams to read from.
 * @param bufSize
 *     The amount of data to read from each stream in each go.
 * @param numThreads
 *     Number of threads to use for parallelism.
 * @param boundedBuffer
 *     The queue to place the results in.
 */
public ParallelStreamReader(Progressable reporter, InputStream[] streams, int bufSize, int numThreads,
        int boundedBufferCapacity, long maxBytesPerStream) throws IOException {
    this.reporter = reporter;
    this.streams = new InputStream[streams.length];
    this.endOffsets = new long[streams.length];
    for (int i = 0; i < streams.length; i++) {
        this.streams[i] = streams[i];
        if (this.streams[i] instanceof DFSDataInputStream) {
            DFSDataInputStream stream = (DFSDataInputStream) this.streams[i];
            // in directory raiding, the block size for each input stream 
            // might be different, so we need to determine the endOffset of
            // each stream by their own block size.
            List<LocatedBlock> blocks = stream.getAllBlocks();
            if (blocks.size() == 0) {
                this.endOffsets[i] = Long.MAX_VALUE;
            } else {
                this.endOffsets[i] = stream.getPos() + blocks.get(0).getBlockSize();
            }
        } else {
            this.endOffsets[i] = Long.MAX_VALUE;
        }
        streams[i] = null; // Take over ownership of streams.
    }
    this.bufSize = bufSize;
    this.boundedBuffer = new ArrayBlockingQueue<ReadResult>(boundedBufferCapacity);
    if (numThreads > streams.length) {
        this.numThreads = streams.length;
    } else {
        this.numThreads = numThreads;
    }
    this.remainingBytesPerStream = maxBytesPerStream;
    this.slots = new Semaphore(this.numThreads);
    this.readPool = Executors.newFixedThreadPool(this.numThreads);
    this.mainThread = new MainThread();
}