Example usage for java.util.concurrent.atomic AtomicInteger incrementAndGet

List of usage examples for java.util.concurrent.atomic AtomicInteger incrementAndGet

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicInteger incrementAndGet.

Prototype

public final int incrementAndGet() 

Source Link

Document

Atomically increments the current value, with memory effects as specified by VarHandle#getAndAdd .

Usage

From source file:co.cask.cdap.gateway.router.NettyRouterTestBase.java

@Test
public void testRouterAsync() throws Exception {
    int numElements = 123;
    AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();

    final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(
            new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());

    final CountDownLatch latch = new CountDownLatch(numElements);
    final AtomicInteger numSuccessfulRequests = new AtomicInteger(0);
    for (int i = 0; i < numElements; ++i) {
        final int elem = i;
        final Request request = new RequestBuilder("GET")
                .setUrl(resolveURI(DEFAULT_SERVICE, String.format("%s/%s-%d", "/v1/ping", "async", i))).build();
        asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {
            @Override/* w  ww . j  av  a  2 s.  c  o  m*/
            public Void onCompleted(Response response) throws Exception {
                latch.countDown();
                Assert.assertEquals(HttpResponseStatus.OK.getCode(), response.getStatusCode());
                numSuccessfulRequests.incrementAndGet();
                return null;
            }

            @Override
            public void onThrowable(Throwable t) {
                LOG.error("Got exception while posting {}", elem, t);
                latch.countDown();
            }
        });

        // Sleep so as not to overrun the server.
        TimeUnit.MILLISECONDS.sleep(1);
    }
    latch.await();
    asyncHttpClient.close();

    Assert.assertEquals(numElements, numSuccessfulRequests.get());
    // we use sticky endpoint strategy so the sum of requests from the two gateways should be NUM_ELEMENTS
    Assert.assertTrue(numElements == (defaultServer1.getNumRequests() + defaultServer2.getNumRequests()));
}

From source file:com.kixeye.chassis.transport.websocket.ActionInvokingWebSocket.java

public void onWebSocketBinary(byte[] payload, int offset, int length) {
    try {/*from ww w . ja  va  2 s  .c  om*/
        // don't accept empty frames
        if (payload == null || length < 1) {
            throw new WebSocketServiceException(new ServiceError("EMPTY_ENVELOPE", "Empty envelope!"),
                    "UNKNOWN", null);
        }

        // check if we need to do psk encryption
        byte[] processedPayload = pskFrameProcessor.processIncoming(payload, offset, length);

        if (processedPayload != payload) {
            payload = processedPayload;
            offset = 0;
            length = payload.length;
        }

        // get the envelope
        final WebSocketEnvelope envelope = new WebSocketEnvelope(
                serDe.deserialize(payload, offset, length, Envelope.class));

        // gets all the actions
        Collection<WebSocketAction> actions = mappingRegistry.getActionMethods(envelope.getAction());

        final AtomicInteger invokedActions = new AtomicInteger(0);

        // invokes them
        for (final WebSocketAction action : actions) {
            // get and validate type ID
            Class<?> messageClass = null;

            if (StringUtils.isNotBlank(envelope.getTypeId())) {
                messageClass = messageRegistry.getClassByTypeId(envelope.getTypeId());
            }

            // validate if action has a payload class that it needs
            if (action.getPayloadClass() != null && messageClass == null) {
                throw new WebSocketServiceException(new ServiceError("INVALID_TYPE_ID", "Unknown type ID!"),
                        envelope.getAction(), envelope.getTransactionId());
            }

            // invoke this action if allowed
            if (action.canInvoke(webSocketSession, messageClass)) {
                invokedActions.incrementAndGet();

                final Object handler = handlerCache.get(action.getHandlerClass().getName());
                final Class<?> finalMessageClass = messageClass;

                ListenableFuture<DeferredResult<?>> invocation = serviceExecutor
                        .submit(new Callable<DeferredResult<?>>() {
                            @Override
                            public DeferredResult<?> call() throws Exception {
                                // then invoke
                                return action.invoke(
                                        handler, new RawWebSocketMessage<>(envelope.getPayload(),
                                                finalMessageClass, messageValidator, serDe),
                                        envelope, webSocketSession);
                            }
                        });

                Futures.addCallback(invocation, new FutureCallback<DeferredResult<?>>() {
                    public void onSuccess(DeferredResult<?> result) {
                        if (result != null) {
                            result.setResultHandler(new DeferredResultHandler() {
                                @Override
                                public void handleResult(Object result) {
                                    if (result instanceof Exception) {
                                        onFailure((Exception) result);
                                        return;
                                    }

                                    sendResponse(result);
                                }
                            });
                        }
                    }

                    public void onFailure(Throwable t) {
                        if (t instanceof InvocationTargetException) {
                            t = ((InvocationTargetException) t).getTargetException();
                        }

                        ServiceError error = ExceptionServiceErrorMapper.mapException(t);

                        if (error != null
                                && !ExceptionServiceErrorMapper.VALIDATION_ERROR_CODE.equals(error.code)) {
                            logger.error("Unexpected exception throw while executing action [{}]",
                                    envelope.getAction(), t);
                        }

                        sendResponse(error);
                    }

                    public Future<Void> sendResponse(Object response) {
                        try {
                            return sendMessage(envelope.getAction(), envelope.getTransactionId(), response);
                        } catch (IOException | GeneralSecurityException e) {
                            logger.error("Unable to send message to channel", e);

                            return Futures.immediateFuture(null);
                        }
                    }

                }, responseExecutor);
            }
        }

        // make sure we actually invoked something
        if (invokedActions.get() < 1) {
            throw new WebSocketServiceException(
                    new ServiceError("INVALID_ACTION_MAPPING", "No actions invoked."), envelope.getAction(),
                    envelope.getTransactionId());
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.alfresco.bm.event.mongo.MongoResultServiceTest.java

/**
 * Test the case where the reporting period is smaller than the stats window
 *//*from w w w.j av  a 2  s . c o m*/
@Test
public void getResultsUsingHandler() {
    pumpRecords(10);
    final long firstEventTime = resultService.getFirstResult().getStartTime();
    final long lastEventTime = resultService.getLastResult().getStartTime();

    final AtomicInteger count = new AtomicInteger();
    final Set<String> names = new HashSet<String>(17);

    resultService.getResults(new ResultHandler() {
        @Override
        public boolean processResult(long fromTime, long toTime,
                Map<String, DescriptiveStatistics> statsByEventName, Map<String, Integer> failuresByEventName)
                throws Throwable {
            if (toTime <= firstEventTime) {
                fail("The window is before the first event.");
            }
            if (fromTime > lastEventTime) {
                fail("The window is past the last event.");
            }
            assertEquals("Window not rebased. ", 0L, fromTime % 10L); // Rebased on reporting period
            assertEquals("Window size incorrect", 20L, toTime - fromTime);

            // Record all the event names we got
            names.addAll(statsByEventName.keySet());

            // Increment
            count.incrementAndGet();

            return true;
        }
    }, 0L, 20L, 10L, false);

    // Check
    assertEquals(10, count.get());
    assertEquals(resultService.getEventNames().size(), names.size());
}

From source file:org.glassfish.jersey.examples.sseitemstore.ItemStoreResourceTest.java

/**
 * Test the item addition, addition event broadcasting and item retrieval from {@link ItemStoreResource}.
 *
 * @throws Exception in case of a test failure.
 *//*from  w  w  w .  j ava 2  s.  c  o m*/
@Test
public void testItemsStore() throws Exception {
    final List<String> items = Collections.unmodifiableList(Arrays.asList("foo", "bar", "baz"));
    final WebTarget itemsTarget = target("items");
    final CountDownLatch latch = new CountDownLatch(items.size() * MAX_LISTENERS * 2); // countdown on all events
    final List<Queue<Integer>> indexQueues = new ArrayList<Queue<Integer>>(MAX_LISTENERS);
    final EventSource[] sources = new EventSource[MAX_LISTENERS];
    final AtomicInteger sizeEventsCount = new AtomicInteger(0);

    for (int i = 0; i < MAX_LISTENERS; i++) {
        final int id = i;
        final EventSource es = EventSource.target(itemsTarget.path("events")).named("SOURCE " + id).build();
        sources[id] = es;

        final Queue<Integer> indexes = new ConcurrentLinkedQueue<Integer>();
        indexQueues.add(indexes);

        es.register(new EventListener() {
            @SuppressWarnings("MagicNumber")
            @Override
            public void onEvent(InboundEvent inboundEvent) {
                try {
                    if (inboundEvent.getName() == null) {
                        final String data = inboundEvent.readData();
                        LOGGER.info("[-i-] SOURCE " + id + ": Received event id=" + inboundEvent.getId()
                                + " data=" + data);
                        indexes.add(items.indexOf(data));
                    } else if ("size".equals(inboundEvent.getName())) {
                        sizeEventsCount.incrementAndGet();
                    }
                } catch (Exception ex) {
                    LOGGER.log(Level.SEVERE, "[-x-] SOURCE " + id + ": Error getting event data.", ex);
                    indexes.add(-999);
                } finally {
                    latch.countDown();
                }
            }
        });
    }

    try {
        open(sources);

        for (String item : items) {
            postItem(itemsTarget, item);
        }

        assertTrue("Waiting to receive all events has timed out.",
                latch.await(
                        (1000 + MAX_LISTENERS * EventSource.RECONNECT_DEFAULT) * getAsyncTimeoutMultiplier(),
                        TimeUnit.MILLISECONDS));

        // need to force disconnect on server in order for EventSource.close(...) to succeed with HttpUrlConnection
        sendCommand(itemsTarget, "disconnect");
    } finally {
        close(sources);
    }

    String postedItems = itemsTarget.request().get(String.class);
    for (String item : items) {
        assertTrue("Item '" + item + "' not stored on server.", postedItems.contains(item));
    }

    int queueId = 0;
    for (Queue<Integer> indexes : indexQueues) {
        for (int i = 0; i < items.size(); i++) {
            assertTrue("Event for '" + items.get(i) + "' not received in queue " + queueId,
                    indexes.contains(i));
        }
        assertEquals("Not received the expected number of events in queue " + queueId, items.size(),
                indexes.size());
        queueId++;
    }

    assertEquals("Number of received 'size' events does not match.", items.size() * MAX_LISTENERS,
            sizeEventsCount.get());
}

From source file:com.heliosdecompiler.helios.gui.controller.FileTreeController.java

@FXML
public void initialize() {
    this.rootItem = new TreeItem<>(new TreeNode("[root]"));
    this.root.setRoot(this.rootItem);
    this.root.setCellFactory(new TreeCellFactory<>(node -> {
        if (node.getParent() == null) {
            ContextMenu export = new ContextMenu();

            MenuItem exportItem = new MenuItem("Export");

            export.setOnAction(e -> {
                File file = messageHandler.chooseFile().withInitialDirectory(new File("."))
                        .withTitle(Message.GENERIC_CHOOSE_EXPORT_LOCATION_JAR.format())
                        .withExtensionFilter(new FileFilter(Message.FILETYPE_JAVA_ARCHIVE.format(), "*.jar"),
                                true)/*from  ww w  . ja va  2 s .c  o m*/
                        .promptSave();

                OpenedFile openedFile = (OpenedFile) node.getMetadata().get(OpenedFile.OPENED_FILE);

                Map<String, byte[]> clone = new HashMap<>(openedFile.getContents());

                backgroundTaskHelper.submit(
                        new BackgroundTask(Message.TASK_SAVING_FILE.format(node.getDisplayName()), true, () -> {
                            try {
                                if (!file.exists()) {
                                    if (!file.createNewFile()) {
                                        throw new IOException("Could not create export file");
                                    }
                                }

                                try (ZipOutputStream zipOutputStream = new ZipOutputStream(
                                        new FileOutputStream(file))) {
                                    for (Map.Entry<String, byte[]> ent : clone.entrySet()) {
                                        ZipEntry zipEntry = new ZipEntry(ent.getKey());
                                        zipOutputStream.putNextEntry(zipEntry);
                                        zipOutputStream.write(ent.getValue());
                                        zipOutputStream.closeEntry();
                                    }
                                }

                                messageHandler.handleMessage(Message.GENERIC_EXPORTED.format());
                            } catch (IOException ex) {
                                messageHandler.handleException(Message.ERROR_IOEXCEPTION_OCCURRED.format(), ex);
                            }
                        }));
            });

            export.getItems().add(exportItem);
            return export;
        }
        return null;
    }));

    root.addEventHandler(KeyEvent.KEY_RELEASED, event -> {
        if (event.getCode() == KeyCode.ENTER) {
            TreeItem<TreeNode> selected = this.root.getSelectionModel().getSelectedItem();
            if (selected != null) {
                if (selected.getChildren().size() != 0) {
                    selected.setExpanded(!selected.isExpanded());
                } else {
                    getParentController().getAllFilesViewerController().handleClick(selected.getValue());
                }
            }
        }
    });

    Tooltip tooltip = new Tooltip();
    StringBuilder search = new StringBuilder();

    List<TreeItem<TreeNode>> searchContext = new ArrayList<>();
    AtomicInteger searchIndex = new AtomicInteger();

    root.focusedProperty().addListener((observable, oldValue, newValue) -> {
        if (!newValue) {
            tooltip.hide();
            search.setLength(0);
        }
    });

    root.boundsInLocalProperty().addListener((observable, oldValue, newValue) -> {
        Bounds bounds = root.localToScreen(newValue);
        tooltip.setAnchorX(bounds.getMinX());
        tooltip.setAnchorY(bounds.getMinY());
    });

    root.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
        if (tooltip.isShowing() && event.getCode() == KeyCode.UP) {
            if (searchIndex.decrementAndGet() < 0) {
                searchIndex.set(searchContext.size() - 1);
            }
        } else if (tooltip.isShowing() && event.getCode() == KeyCode.DOWN) {
            if (searchIndex.incrementAndGet() >= searchContext.size()) {
                searchIndex.set(0);
            }
        } else {
            return;
        }
        event.consume();

        root.scrollTo(root.getRow(searchContext.get(searchIndex.get())));
        root.getSelectionModel().select(searchContext.get(searchIndex.get()));
    });

    root.addEventHandler(KeyEvent.KEY_TYPED, event -> {
        if (event.getCharacter().charAt(0) == '\b') {
            if (search.length() > 0) {
                search.setLength(search.length() - 1);
            }
        } else if (event.getCharacter().charAt(0) == '\u001B') { //esc
            tooltip.hide();
            search.setLength(0);
            return;
        } else if (search.length() > 0
                || (search.length() == 0 && StringUtils.isAlphanumeric(event.getCharacter()))) {
            search.append(event.getCharacter());
            if (!tooltip.isShowing()) {
                tooltip.show(root.getScene().getWindow());
            }
        }

        if (!tooltip.isShowing())
            return;

        String str = search.toString();
        tooltip.setText("Search for: " + str);

        searchContext.clear();

        ArrayDeque<TreeItem<TreeNode>> deque = new ArrayDeque<>();
        deque.addAll(rootItem.getChildren());

        while (!deque.isEmpty()) {
            TreeItem<TreeNode> item = deque.poll();
            if (item.getValue().getDisplayName().contains(str)) {
                searchContext.add(item);
            }
            if (item.isExpanded() && item.getChildren().size() > 0)
                deque.addAll(item.getChildren());
        }

        searchIndex.set(0);
        if (searchContext.size() > 0) {
            root.scrollTo(root.getRow(searchContext.get(0)));
            root.getSelectionModel().select(searchContext.get(0));
        }
    });

    openedFileController.loadedFiles().addListener((MapChangeListener<String, OpenedFile>) change -> {
        if (change.getValueAdded() != null) {
            updateTree(change.getValueAdded());
        }
        if (change.getValueRemoved() != null) {
            this.rootItem.getChildren()
                    .removeIf(ti -> ti.getValue().equals(change.getValueRemoved().getRoot()));
        }
    });
}

From source file:org.glassfish.jersey.apache.connector.HelloWorldTest.java

/**
 * JERSEY-2157 reproducer.//from w  w w .  j av  a2 s.com
 * <p>
 * The test ensures that entities of the error responses which cause
 * WebApplicationException being thrown by a JAX-RS client are buffered
 * and that the underlying input connections are automatically released
 * in such case.
 */
@Test
public void testConnectionClosingOnExceptionsForErrorResponses() {
    final BasicClientConnectionManager cm = new BasicClientConnectionManager();
    final AtomicInteger connectionCounter = new AtomicInteger(0);

    final ClientConfig config = new ClientConfig().property(ApacheClientProperties.CONNECTION_MANAGER,
            new ClientConnectionManager() {
                @Override
                public SchemeRegistry getSchemeRegistry() {
                    return cm.getSchemeRegistry();
                }

                @Override
                public ClientConnectionRequest requestConnection(final HttpRoute route, final Object state) {
                    connectionCounter.incrementAndGet();

                    final ClientConnectionRequest wrappedRequest = cm.requestConnection(route, state);

                    /**
                     * To explain the following long piece of code:
                     *
                     * All the code does is to just create a wrapper implementations
                     * for the AHC connection management interfaces.
                     *
                     * The only really important piece of code is the
                     * {@link org.apache.http.conn.ManagedClientConnection#releaseConnection()} implementation,
                     * where the connectionCounter is decremented when a managed connection instance
                     * is released by AHC runtime. In our test, this is expected to happen
                     * as soon as the exception is created for an error response
                     * (as the error response entity gets buffered in
                     * {@link org.glassfish.jersey.client.JerseyInvocation#convertToException(javax.ws.rs.core.Response)}).
                     */
                    return new ClientConnectionRequest() {
                        @Override
                        public ManagedClientConnection getConnection(long timeout, TimeUnit tunit)
                                throws InterruptedException, ConnectionPoolTimeoutException {

                            final ManagedClientConnection wrappedConnection = wrappedRequest
                                    .getConnection(timeout, tunit);

                            return new ManagedClientConnection() {
                                @Override
                                public boolean isSecure() {
                                    return wrappedConnection.isSecure();
                                }

                                @Override
                                public HttpRoute getRoute() {
                                    return wrappedConnection.getRoute();
                                }

                                @Override
                                public SSLSession getSSLSession() {
                                    return wrappedConnection.getSSLSession();
                                }

                                @Override
                                public void open(HttpRoute route, HttpContext context, HttpParams params)
                                        throws IOException {
                                    wrappedConnection.open(route, context, params);
                                }

                                @Override
                                public void tunnelTarget(boolean secure, HttpParams params) throws IOException {
                                    wrappedConnection.tunnelTarget(secure, params);
                                }

                                @Override
                                public void tunnelProxy(HttpHost next, boolean secure, HttpParams params)
                                        throws IOException {
                                    wrappedConnection.tunnelProxy(next, secure, params);
                                }

                                @Override
                                public void layerProtocol(HttpContext context, HttpParams params)
                                        throws IOException {
                                    wrappedConnection.layerProtocol(context, params);
                                }

                                @Override
                                public void markReusable() {
                                    wrappedConnection.markReusable();
                                }

                                @Override
                                public void unmarkReusable() {
                                    wrappedConnection.unmarkReusable();
                                }

                                @Override
                                public boolean isMarkedReusable() {
                                    return wrappedConnection.isMarkedReusable();
                                }

                                @Override
                                public void setState(Object state) {
                                    wrappedConnection.setState(state);
                                }

                                @Override
                                public Object getState() {
                                    return wrappedConnection.getState();
                                }

                                @Override
                                public void setIdleDuration(long duration, TimeUnit unit) {
                                    wrappedConnection.setIdleDuration(duration, unit);
                                }

                                @Override
                                public boolean isResponseAvailable(int timeout) throws IOException {
                                    return wrappedConnection.isResponseAvailable(timeout);
                                }

                                @Override
                                public void sendRequestHeader(HttpRequest request)
                                        throws HttpException, IOException {
                                    wrappedConnection.sendRequestHeader(request);
                                }

                                @Override
                                public void sendRequestEntity(HttpEntityEnclosingRequest request)
                                        throws HttpException, IOException {
                                    wrappedConnection.sendRequestEntity(request);
                                }

                                @Override
                                public HttpResponse receiveResponseHeader() throws HttpException, IOException {
                                    return wrappedConnection.receiveResponseHeader();
                                }

                                @Override
                                public void receiveResponseEntity(HttpResponse response)
                                        throws HttpException, IOException {
                                    wrappedConnection.receiveResponseEntity(response);
                                }

                                @Override
                                public void flush() throws IOException {
                                    wrappedConnection.flush();
                                }

                                @Override
                                public void close() throws IOException {
                                    wrappedConnection.close();
                                }

                                @Override
                                public boolean isOpen() {
                                    return wrappedConnection.isOpen();
                                }

                                @Override
                                public boolean isStale() {
                                    return wrappedConnection.isStale();
                                }

                                @Override
                                public void setSocketTimeout(int timeout) {
                                    wrappedConnection.setSocketTimeout(timeout);
                                }

                                @Override
                                public int getSocketTimeout() {
                                    return wrappedConnection.getSocketTimeout();
                                }

                                @Override
                                public void shutdown() throws IOException {
                                    wrappedConnection.shutdown();
                                }

                                @Override
                                public HttpConnectionMetrics getMetrics() {
                                    return wrappedConnection.getMetrics();
                                }

                                @Override
                                public InetAddress getLocalAddress() {
                                    return wrappedConnection.getLocalAddress();
                                }

                                @Override
                                public int getLocalPort() {
                                    return wrappedConnection.getLocalPort();
                                }

                                @Override
                                public InetAddress getRemoteAddress() {
                                    return wrappedConnection.getRemoteAddress();
                                }

                                @Override
                                public int getRemotePort() {
                                    return wrappedConnection.getRemotePort();
                                }

                                @Override
                                public void releaseConnection() throws IOException {
                                    connectionCounter.decrementAndGet();
                                    wrappedConnection.releaseConnection();
                                }

                                @Override
                                public void abortConnection() throws IOException {
                                    wrappedConnection.abortConnection();
                                }

                                @Override
                                public String getId() {
                                    return wrappedConnection.getId();
                                }

                                @Override
                                public void bind(Socket socket) throws IOException {
                                    wrappedConnection.bind(socket);
                                }

                                @Override
                                public Socket getSocket() {
                                    return wrappedConnection.getSocket();
                                }
                            };
                        }

                        @Override
                        public void abortRequest() {
                            wrappedRequest.abortRequest();
                        }
                    };
                }

                @Override
                public void releaseConnection(ManagedClientConnection conn, long keepalive, TimeUnit tunit) {
                    cm.releaseConnection(conn, keepalive, tunit);
                }

                @Override
                public void closeExpiredConnections() {
                    cm.closeExpiredConnections();
                }

                @Override
                public void closeIdleConnections(long idletime, TimeUnit tunit) {
                    cm.closeIdleConnections(idletime, tunit);
                }

                @Override
                public void shutdown() {
                    cm.shutdown();
                }
            });
    config.connectorProvider(new ApacheConnectorProvider());

    final Client client = ClientBuilder.newClient(config);
    final WebTarget rootTarget = client.target(getBaseUri()).path(ROOT_PATH);

    // Test that connection is getting closed properly for error responses.
    try {
        final String response = rootTarget.path("error").request().get(String.class);
        fail("Exception expected. Received: " + response);
    } catch (InternalServerErrorException isee) {
        // do nothing - connection should be closed properly by now
    }

    // Fail if the previous connection has not been closed automatically.
    assertEquals(0, connectionCounter.get());

    try {
        final String response = rootTarget.path("error2").request().get(String.class);
        fail("Exception expected. Received: " + response);
    } catch (InternalServerErrorException isee) {
        assertEquals("Received unexpected data.", "Error2.", isee.getResponse().readEntity(String.class));
        // Test buffering:
        // second read would fail if entity was not buffered
        assertEquals("Unexpected data in the entity buffer.", "Error2.",
                isee.getResponse().readEntity(String.class));
    }

    assertEquals(0, connectionCounter.get());
}

From source file:org.apache.hadoop.hbase.procedure2.TestProcedureSchedulerConcurrency.java

private void testConcurrentWaitWake(final boolean useWakeBatch) throws Exception {
    final int WAIT_THRESHOLD = 2500;
    final int NPROCS = 20;
    final int NRUNS = 500;

    final ProcedureScheduler sched = procSched;
    for (long i = 0; i < NPROCS; ++i) {
        sched.addBack(new TestProcedureWithEvent(i));
    }/*from   w w w.j a v  a2  s  .  c  o m*/

    final Thread[] threads = new Thread[4];
    final AtomicInteger waitCount = new AtomicInteger(0);
    final AtomicInteger wakeCount = new AtomicInteger(0);

    final ConcurrentSkipListSet<TestProcedureWithEvent> waitQueue = new ConcurrentSkipListSet<TestProcedureWithEvent>();
    threads[0] = new Thread() {
        @Override
        public void run() {
            long lastUpdate = 0;
            while (true) {
                final int oldWakeCount = wakeCount.get();
                if (useWakeBatch) {
                    ProcedureEvent[] ev = new ProcedureEvent[waitQueue.size()];
                    for (int i = 0; i < ev.length; ++i) {
                        ev[i] = waitQueue.pollFirst().getEvent();
                        LOG.debug("WAKE BATCH " + ev[i] + " total=" + wakeCount.get());
                    }
                    sched.wakeEvents(ev.length, ev);
                    wakeCount.addAndGet(ev.length);
                } else {
                    int size = waitQueue.size();
                    while (size-- > 0) {
                        ProcedureEvent ev = waitQueue.pollFirst().getEvent();
                        sched.wakeEvent(ev);
                        LOG.debug("WAKE " + ev + " total=" + wakeCount.get());
                        wakeCount.incrementAndGet();
                    }
                }
                if (wakeCount.get() != oldWakeCount) {
                    lastUpdate = System.currentTimeMillis();
                } else if (wakeCount.get() >= NRUNS
                        && (System.currentTimeMillis() - lastUpdate) > WAIT_THRESHOLD) {
                    break;
                }
                Threads.sleepWithoutInterrupt(25);
            }
        }
    };

    for (int i = 1; i < threads.length; ++i) {
        threads[i] = new Thread() {
            @Override
            public void run() {
                while (true) {
                    TestProcedureWithEvent proc = (TestProcedureWithEvent) sched.poll();
                    if (proc == null)
                        continue;

                    sched.suspendEvent(proc.getEvent());
                    waitQueue.add(proc);
                    sched.waitEvent(proc.getEvent(), proc);
                    LOG.debug("WAIT " + proc.getEvent());
                    if (waitCount.incrementAndGet() >= NRUNS) {
                        break;
                    }
                }
            }
        };
    }

    for (int i = 0; i < threads.length; ++i) {
        threads[i].start();
    }
    for (int i = 0; i < threads.length; ++i) {
        threads[i].join();
    }

    sched.clear();
}

From source file:org.apache.distributedlog.lock.TestZKSessionLock.java

/**
 * Test Session Expired Before Lock does locking. The lock should be closed since
 * all zookeeper operations would be failed.
 *
 * @param timeout//from  w  ww. j  a  va2  s .  c o m
 *          timeout to wait for the lock
 * @throws Exception
 */
private void testSessionExpiredBeforeLock(long timeout) throws Exception {
    String lockPath = "/test-session-expired-before-lock-" + timeout + "-" + System.currentTimeMillis();
    String clientId = "test-session-expired-before-lock-" + System.currentTimeMillis();

    createLockPath(zkc.get(), lockPath);
    final AtomicInteger expireCounter = new AtomicInteger(0);
    final CountDownLatch expiredLatch = new CountDownLatch(1);
    LockListener listener = new LockListener() {
        @Override
        public void onExpired() {
            expireCounter.incrementAndGet();
        }
    };
    final ZKSessionLock lock = new ZKSessionLock(zkc, lockPath, clientId, lockStateExecutor)
            .setLockListener(listener);
    // expire session
    ZooKeeperClientUtils.expireSession(zkc, zkServers, sessionTimeoutMs);
    // submit a runnable to lock state executor to ensure any state changes happened when session expired
    lockStateExecutor.executeOrdered(lockPath, () -> expiredLatch.countDown());
    expiredLatch.await();
    // no watcher was registered if never acquired lock successfully
    assertEquals(State.INIT, lock.getLockState());
    try {
        lock.tryLock(timeout, TimeUnit.MILLISECONDS);
        fail("Should fail locking using an expired lock");
    } catch (LockingException le) {
        assertTrue(le.getCause() instanceof KeeperException.SessionExpiredException);
    }
    assertEquals(State.CLOSED, lock.getLockState());
    List<String> children = getLockWaiters(zkc, lockPath);
    assertEquals(0, children.size());
}

From source file:org.apache.bookkeeper.bookie.CreateNewLogTest.java

@Test
public void testConcurrentEntryLogCreations() throws Exception {
    ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();

    // Creating a new configuration with a number of ledger directories.
    conf.setLedgerDirNames(ledgerDirs);// w  w w .  j  a v  a 2  s.  co  m
    // pre-allocation is enabled
    conf.setEntryLogFilePreAllocationEnabled(true);
    conf.setEntryLogPerLedgerEnabled(true);
    LedgerDirsManager ledgerDirsManager = new LedgerDirsManager(conf, conf.getLedgerDirs(),
            new DiskChecker(conf.getDiskUsageThreshold(), conf.getDiskUsageWarnThreshold()));
    EntryLogger entryLogger = new EntryLogger(conf, ledgerDirsManager);
    EntryLogManagerForEntryLogPerLedger entrylogManager = (EntryLogManagerForEntryLogPerLedger) entryLogger
            .getEntryLogManager();

    int numOfLedgers = 10;
    int numOfThreadsForSameLedger = 10;
    AtomicInteger createdEntryLogs = new AtomicInteger(0);
    CountDownLatch startLatch = new CountDownLatch(1);
    CountDownLatch createdLatch = new CountDownLatch(numOfLedgers * numOfThreadsForSameLedger);

    for (long i = 0; i < numOfLedgers; i++) {
        for (int j = 0; j < numOfThreadsForSameLedger; j++) {
            long ledgerId = i;
            new Thread(() -> {
                try {
                    startLatch.await();
                    entrylogManager.createNewLog(ledgerId);
                    createdEntryLogs.incrementAndGet();
                } catch (InterruptedException | IOException e) {
                    LOG.error("Got exception while trying to createNewLog for Ledger: " + ledgerId, e);
                } finally {
                    createdLatch.countDown();
                }
            }).start();
        }
    }

    startLatch.countDown();
    createdLatch.await(5, TimeUnit.SECONDS);
    Assert.assertEquals("Created EntryLogs", numOfLedgers * numOfThreadsForSameLedger, createdEntryLogs.get());
    Assert.assertEquals("Active currentlogs size", numOfLedgers, entrylogManager.getCopyOfCurrentLogs().size());
    Assert.assertEquals("Rotated entrylogs size", (numOfThreadsForSameLedger - 1) * numOfLedgers,
            entrylogManager.getRotatedLogChannels().size());
    /*
     * EntryLogFilePreAllocation is Enabled so
     * getPreviousAllocatedEntryLogId would be (numOfLedgers *
     * numOfThreadsForSameLedger) instead of (numOfLedgers *
     * numOfThreadsForSameLedger - 1)
     */
    Assert.assertEquals("PreviousAllocatedEntryLogId", numOfLedgers * numOfThreadsForSameLedger,
            entryLogger.getPreviousAllocatedEntryLogId());
}

From source file:com.netflix.spinnaker.igor.gitlabci.GitlabCiBuildMonitor.java

@Override
protected BuildPollingDelta generateDelta(PollContext ctx) {
    final String master = ctx.partitionName;

    log.info("Checking for new builds for {}", kv("master", master));
    final AtomicInteger updatedBuilds = new AtomicInteger();
    final GitlabCiService gitlabCiService = (GitlabCiService) buildMasters.getMap().get(master);
    long startTime = System.currentTimeMillis();

    final List<Project> projects = gitlabCiService.getProjects();
    log.info("Took {} ms to retrieve {} repositories (master: {})", System.currentTimeMillis() - startTime,
            projects.size(), kv("master", master));

    List<BuildDelta> delta = new ArrayList<>();
    projects.parallelStream().forEach(project -> {
        List<Pipeline> pipelines = filterOldPipelines(
                gitlabCiService.getPipelines(project, MAX_NUMBER_OF_PIPELINES));
        for (Pipeline pipeline : pipelines) {
            String branchedRepoSlug = GitlabCiPipelineUtis.getBranchedPipelineSlug(project, pipeline);

            boolean isPipelineRunning = GitlabCiResultConverter.running(pipeline.getStatus());
            int cachedBuildId = buildCache.getLastBuild(master, branchedRepoSlug, isPipelineRunning);
            // In case of Gitlab CI the pipeline ids are increasing so we can use it for ordering
            if (pipeline.getId() > cachedBuildId) {
                updatedBuilds.incrementAndGet();
                delta.add(new BuildDelta(branchedRepoSlug, project, pipeline, isPipelineRunning));
            }// www  .  ja va  2s. c  om
        }
    });

    if (!delta.isEmpty()) {
        log.info("Found {} new builds (master: {})", updatedBuilds.get(), kv("master", master));
    }

    return new BuildPollingDelta(delta, master, startTime);
}