Search in sources :

Example 86 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project jetty.project by eclipse.

the class MBeanContainer method beanAdded.

@Override
public void beanAdded(Container parent, Object obj) {
    if (LOG.isDebugEnabled())
        LOG.debug("beanAdded {}->{}", parent, obj);
    // Is there an object name for the parent ?
    ObjectName parentObjectName = null;
    if (parent != null) {
        parentObjectName = findMBean(parent);
        if (parentObjectName == null) {
            // Create the parent bean.
            beanAdded(null, parent);
            parentObjectName = findMBean(parent);
        }
    }
    // Does the mbean already exist ?
    if (obj == null || _beans.containsKey(obj))
        return;
    try {
        // Create an MBean for the object.
        Object mbean = ObjectMBean.mbeanFor(obj);
        if (mbean == null)
            return;
        ObjectName objectName = null;
        if (mbean instanceof ObjectMBean) {
            ((ObjectMBean) mbean).setMBeanContainer(this);
            objectName = ((ObjectMBean) mbean).getObjectName();
        }
        // No override of the mbean's ObjectName, so make a generic one.
        if (objectName == null) {
            // If no explicit domain, create one.
            String domain = _domain;
            if (domain == null)
                domain = obj.getClass().getPackage().getName();
            String type = obj.getClass().getName().toLowerCase(Locale.ENGLISH);
            int dot = type.lastIndexOf('.');
            if (dot >= 0)
                type = type.substring(dot + 1);
            StringBuilder buf = new StringBuilder();
            String context = (mbean instanceof ObjectMBean) ? makeName(((ObjectMBean) mbean).getObjectContextBasis()) : null;
            if (context == null && parentObjectName != null)
                context = parentObjectName.getKeyProperty("context");
            if (context != null && context.length() > 1)
                buf.append("context=").append(context).append(",");
            buf.append("type=").append(type);
            String name = (mbean instanceof ObjectMBean) ? makeName(((ObjectMBean) mbean).getObjectNameBasis()) : context;
            if (name != null && name.length() > 1)
                buf.append(",").append("name=").append(name);
            String basis = buf.toString();
            AtomicInteger count = __unique.get(basis);
            if (count == null) {
                count = new AtomicInteger();
                AtomicInteger existing = __unique.putIfAbsent(basis, count);
                if (existing != null)
                    count = existing;
            }
            objectName = ObjectName.getInstance(domain + ":" + basis + ",id=" + count.getAndIncrement());
        }
        _mbeanServer.registerMBean(mbean, objectName);
        if (LOG.isDebugEnabled())
            LOG.debug("Registered {}", objectName);
        _beans.put(obj, objectName);
    } catch (Throwable x) {
        LOG.warn("bean: " + obj, x);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) ObjectName(javax.management.ObjectName)

Example 87 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project jetty.project by eclipse.

the class StressTest method doThreads.

private void doThreads(int threadCount, final int loops, final boolean persistent) throws Throwable {
    final Throwable[] throwables = new Throwable[threadCount];
    final Thread[] threads = new Thread[threadCount];
    try {
        for (int i = 0; i < threadCount; i++) {
            final int id = i;
            final String name = "T" + i;
            Thread.sleep(_random.nextInt(100));
            threads[i] = new Thread() {

                @Override
                public void run() {
                    try {
                        doLoops(id, name, loops, persistent);
                    } catch (Throwable th) {
                        th.printStackTrace();
                        throwables[id] = th;
                    }
                }
            };
        }
        _loops = new AtomicInteger[threadCount];
        for (int i = 0; i < threadCount; i++) {
            _loops[i] = new AtomicInteger(0);
            threads[i].start();
        }
        String last = null;
        int same = 0;
        while (true) {
            Thread.sleep(1000L);
            int finished = 0;
            int errors = 0;
            int min = loops;
            int max = 0;
            int total = 0;
            for (int i = 0; i < threadCount; i++) {
                int l = _loops[i].get();
                if (l < 0) {
                    errors++;
                    total -= l;
                } else {
                    if (l < min)
                        min = l;
                    if (l > max)
                        max = l;
                    total += l;
                    if (l == loops)
                        finished++;
                }
            }
            String status = "min/ave/max/target=" + min + "/" + (total / threadCount) + "/" + max + "/" + loops + " errors/finished/loops=" + errors + "/" + finished + "/" + threadCount + " idle/threads=" + (_threads.getIdleThreads()) + "/" + _threads.getThreads();
            if (status.equals(last)) {
                if (same++ > 5) {
                    System.err.println("STALLED!!!");
                    System.err.println(_server.getThreadPool().toString());
                    Thread.sleep(5000);
                    System.exit(1);
                }
            } else
                same = 0;
            last = status;
            LOG.info(_server.getThreadPool().toString() + " " + status);
            if ((finished + errors) == threadCount)
                break;
        }
        for (Thread thread : threads) thread.join();
        for (Throwable throwable : throwables) if (throwable != null)
            throw throwable;
        for (ConcurrentLinkedQueue _latency : _latencies) assertEquals(_handled.get(), _latency.size());
    } finally {
        // System.err.println();
        final int quantums = 48;
        final int[][] count = new int[_latencies.length][quantums];
        final int[] length = new int[_latencies.length];
        final int[] other = new int[_latencies.length];
        long total = 0;
        for (int i = 0; i < _latencies.length; i++) {
            Queue<Long> latencies = _latencies[i];
            length[i] = latencies.size();
            loop: for (long latency : latencies) {
                if (i == 4)
                    total += latency;
                for (int q = 0; q < quantums; q++) {
                    if (latency >= (q * 100) && latency < ((q + 1) * 100)) {
                        count[i][q]++;
                        continue loop;
                    }
                }
                other[i]++;
            }
        }
        System.out.println("           stage:\tbind\twrite\trecv\tdispatch\twrote\ttotal");
        for (int q = 0; q < quantums; q++) {
            System.out.printf("%02d00<=l<%02d00", q, (q + 1));
            for (int i = 0; i < _latencies.length; i++) System.out.print("\t" + count[i][q]);
            System.out.println();
        }
        System.out.print("other       ");
        for (int i = 0; i < _latencies.length; i++) System.out.print("\t" + other[i]);
        System.out.println();
        System.out.print("HANDLED     ");
        for (int i = 0; i < _latencies.length; i++) System.out.print("\t" + _handled.get());
        System.out.println();
        System.out.print("TOTAL       ");
        for (int i = 0; i < _latencies.length; i++) System.out.print("\t" + length[i]);
        System.out.println();
        long ave = total / _latencies[4].size();
        System.out.println("ave=" + ave);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 88 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class ClusteredEventBusTest method testSubsRemoved.

private void testSubsRemoved(Consumer<CountDownLatch> action) throws Exception {
    startNodes(3);
    CountDownLatch regLatch = new CountDownLatch(1);
    AtomicInteger cnt = new AtomicInteger();
    vertices[0].eventBus().consumer(ADDRESS1, msg -> {
        int c = cnt.getAndIncrement();
        assertEquals(msg.body(), "foo" + c);
        if (c == 9) {
            testComplete();
        }
        if (c > 9) {
            fail("too many messages");
        }
    }).completionHandler(onSuccess(v -> {
        vertices[1].eventBus().consumer(ADDRESS1, msg -> {
            fail("shouldn't get message");
        }).completionHandler(onSuccess(v2 -> {
            regLatch.countDown();
        }));
    }));
    awaitLatch(regLatch);
    CountDownLatch closeLatch = new CountDownLatch(1);
    action.accept(closeLatch);
    awaitLatch(closeLatch);
    // Allow time for kill to be propagate
    Thread.sleep(2000);
    vertices[2].runOnContext(v -> {
        EventBus ebSender = vertices[2].eventBus();
        for (int i = 0; i < 10; i++) {
            ebSender.send(ADDRESS1, "foo" + i);
        }
    });
    await();
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) Vertx(io.vertx.core.Vertx) VertxOptions(io.vertx.core.VertxOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) io.vertx.core.eventbus(io.vertx.core.eventbus) Test(org.junit.Test) FakeClusterManager(io.vertx.test.fakecluster.FakeClusterManager) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 89 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class ClusteredEventBusTestBase method testPublish.

@Override
protected <T> void testPublish(T val, Consumer<T> consumer) {
    int numNodes = 3;
    startNodes(numNodes);
    AtomicInteger count = new AtomicInteger();
    class MyHandler implements Handler<Message<T>> {

        @Override
        public void handle(Message<T> msg) {
            if (consumer == null) {
                assertFalse(msg.isSend());
                assertEquals(val, msg.body());
            } else {
                consumer.accept(msg.body());
            }
            if (count.incrementAndGet() == numNodes - 1) {
                testComplete();
            }
        }
    }
    AtomicInteger registerCount = new AtomicInteger(0);
    class MyRegisterHandler implements Handler<AsyncResult<Void>> {

        @Override
        public void handle(AsyncResult<Void> ar) {
            assertTrue(ar.succeeded());
            if (registerCount.incrementAndGet() == 2) {
                vertices[0].eventBus().publish(ADDRESS1, val);
            }
        }
    }
    MessageConsumer reg = vertices[2].eventBus().<T>consumer(ADDRESS1).handler(new MyHandler());
    reg.completionHandler(new MyRegisterHandler());
    reg = vertices[1].eventBus().<T>consumer(ADDRESS1).handler(new MyHandler());
    reg.completionHandler(new MyRegisterHandler());
    vertices[0].eventBus().publish(ADDRESS1, val);
    await();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Handler(io.vertx.core.Handler) AsyncResult(io.vertx.core.AsyncResult)

Example 90 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class ComplexHATest method deployRandomVerticles.

protected void deployRandomVerticles(Runnable runner) {
    int toDeploy = 0;
    AtomicInteger deployCount = new AtomicInteger();
    List<Integer> numbersToDeploy = new ArrayList<>();
    for (int i = 0; i < aliveNodes.size(); i++) {
        int numToDeploy = random.nextInt(maxVerticlesPerNode + 1);
        numbersToDeploy.add(numToDeploy);
        toDeploy += numToDeploy;
    }
    int index = 0;
    for (int pos : aliveNodes) {
        Vertx v = vertices[pos];
        int numToDeploy = numbersToDeploy.get(index);
        index++;
        for (int j = 0; j < numToDeploy; j++) {
            JsonObject config = new JsonObject();
            config.put("foo", TestUtils.randomAlphaString(100));
            DeploymentOptions options = new DeploymentOptions().setHa(true).setConfig(config);
            String verticleName = "java:io.vertx.test.core.HAVerticle" + (random.nextInt(3) + 1);
            v.deployVerticle(verticleName, options, ar -> {
                assertTrue(ar.succeeded());
                deployCount.incrementAndGet();
            });
        }
    }
    int ttoDeploy = toDeploy;
    eventLoopWaitUntil(() -> ttoDeploy == deployCount.get(), () -> {
        totDeployed += ttoDeploy;
        runner.run();
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeploymentOptions(io.vertx.core.DeploymentOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) JsonObject(io.vertx.core.json.JsonObject) Vertx(io.vertx.core.Vertx)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7986 Test (org.junit.Test)3775 CountDownLatch (java.util.concurrent.CountDownLatch)1072 ArrayList (java.util.ArrayList)1018 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)849 List (java.util.List)740 IOException (java.io.IOException)719 AtomicReference (java.util.concurrent.atomic.AtomicReference)574 HashMap (java.util.HashMap)499 Map (java.util.Map)460 Test (org.testng.annotations.Test)419 File (java.io.File)337 ExecutorService (java.util.concurrent.ExecutorService)337 Test (org.junit.jupiter.api.Test)334 AtomicLong (java.util.concurrent.atomic.AtomicLong)329 TimeUnit (java.util.concurrent.TimeUnit)323 HashSet (java.util.HashSet)315 Arrays (java.util.Arrays)308 Set (java.util.Set)284 Collections (java.util.Collections)266