Search in sources :

Example 1 with PoolMetrics

use of io.vertx.core.spi.metrics.PoolMetrics in project vert.x by eclipse.

the class MetricsTest method testThreadPoolMetricsWithNamedExecuteBlocking.

@Test
public void testThreadPoolMetricsWithNamedExecuteBlocking() {
    // Close the instance automatically created
    vertx.close();
    vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(new FakeMetricsFactory())));
    WorkerExecutor workerExec = vertx.createSharedWorkerExecutor("my-pool", 10);
    Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();
    FakePoolMetrics metrics = (FakePoolMetrics) all.get("my-pool");
    assertThat(metrics.getPoolSize(), is(10));
    assertThat(metrics.numberOfIdleThreads(), is(10));
    Handler<Future<Void>> job = getSomeDumbTask();
    AtomicInteger counter = new AtomicInteger();
    AtomicBoolean hadWaitingQueue = new AtomicBoolean();
    AtomicBoolean hadIdle = new AtomicBoolean();
    AtomicBoolean hadRunning = new AtomicBoolean();
    for (int i = 0; i < 100; i++) {
        workerExec.executeBlocking(job, false, ar -> {
            if (metrics.numberOfWaitingTasks() > 0) {
                hadWaitingQueue.set(true);
            }
            if (metrics.numberOfIdleThreads() > 0) {
                hadIdle.set(true);
            }
            if (metrics.numberOfRunningTasks() > 0) {
                hadRunning.set(true);
            }
            if (counter.incrementAndGet() == 100) {
                testComplete();
            }
        });
    }
    await();
    assertEquals(metrics.numberOfSubmittedTask(), 100);
    assertEquals(metrics.numberOfCompletedTasks(), 100);
    assertTrue(hadIdle.get());
    assertTrue(hadWaitingQueue.get());
    assertTrue(hadRunning.get());
    assertEquals(metrics.numberOfIdleThreads(), 10);
    assertEquals(metrics.numberOfRunningTasks(), 0);
    assertEquals(metrics.numberOfWaitingTasks(), 0);
}
Also used : MetricsOptions(io.vertx.core.metrics.MetricsOptions) PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 2 with PoolMetrics

use of io.vertx.core.spi.metrics.PoolMetrics in project vert.x by eclipse.

the class MetricsTest method testWithWorkerVerticle.

private void testWithWorkerVerticle(DeploymentOptions options) {
    AtomicInteger counter = new AtomicInteger();
    Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();
    FakePoolMetrics metrics = (FakePoolMetrics) all.get("vert.x-worker-thread");
    assertThat(metrics.getPoolSize(), is(getOptions().getInternalBlockingPoolSize()));
    assertThat(metrics.numberOfIdleThreads(), is(getOptions().getWorkerPoolSize()));
    AtomicBoolean hadWaitingQueue = new AtomicBoolean();
    AtomicBoolean hadIdle = new AtomicBoolean();
    AtomicBoolean hadRunning = new AtomicBoolean();
    int count = 100;
    Verticle worker = new AbstractVerticle() {

        @Override
        public void start(Future<Void> done) throws Exception {
            vertx.eventBus().localConsumer("message", d -> {
                try {
                    Thread.sleep(10);
                    if (metrics.numberOfWaitingTasks() > 0) {
                        hadWaitingQueue.set(true);
                    }
                    if (metrics.numberOfIdleThreads() > 0) {
                        hadIdle.set(true);
                    }
                    if (metrics.numberOfRunningTasks() > 0) {
                        hadRunning.set(true);
                    }
                    if (counter.incrementAndGet() == count) {
                        testComplete();
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().isInterrupted();
                }
            });
            done.complete();
        }
    };
    vertx.deployVerticle(worker, options, s -> {
        for (int i = 0; i < count; i++) {
            vertx.eventBus().send("message", i);
        }
    });
    await();
    // The verticle deployment is also executed on the worker thread pool
    assertEquals(metrics.numberOfSubmittedTask(), count + 1);
    assertEquals(metrics.numberOfCompletedTasks(), count + 1);
    assertTrue(hadIdle.get());
    assertTrue(hadWaitingQueue.get());
    assertTrue(hadRunning.get());
    assertEquals(metrics.numberOfIdleThreads(), getOptions().getWorkerPoolSize());
    assertEquals(metrics.numberOfRunningTasks(), 0);
    assertEquals(metrics.numberOfWaitingTasks(), 0);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics)

Example 3 with PoolMetrics

use of io.vertx.core.spi.metrics.PoolMetrics in project vert.x by eclipse.

the class ContextImpl method executeBlocking.

static <T> Future<T> executeBlocking(ContextInternal context, Handler<Promise<T>> blockingCodeHandler, WorkerPool workerPool, TaskQueue queue) {
    PoolMetrics metrics = workerPool.metrics();
    Object queueMetric = metrics != null ? metrics.submitted() : null;
    Promise<T> promise = context.promise();
    Future<T> fut = promise.future();
    try {
        Runnable command = () -> {
            Object execMetric = null;
            if (metrics != null) {
                execMetric = metrics.begin(queueMetric);
            }
            context.dispatch(promise, f -> {
                try {
                    blockingCodeHandler.handle(promise);
                } catch (Throwable e) {
                    promise.tryFail(e);
                }
            });
            if (metrics != null) {
                metrics.end(execMetric, fut.succeeded());
            }
        };
        Executor exec = workerPool.executor();
        if (queue != null) {
            queue.execute(command, exec);
        } else {
            exec.execute(command);
        }
    } catch (RejectedExecutionException e) {
        // Pool is already shut down
        if (metrics != null) {
            metrics.rejected(queueMetric);
        }
        throw e;
    }
    return fut;
}
Also used : Logger(io.vertx.core.impl.logging.Logger) PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) io.vertx.core(io.vertx.core) Executor(java.util.concurrent.Executor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) VertxTracer(io.vertx.core.spi.tracing.VertxTracer) JsonObject(io.vertx.core.json.JsonObject) EventLoop(io.netty.channel.EventLoop) ConcurrentMap(java.util.concurrent.ConcurrentMap) Executor(java.util.concurrent.Executor) JsonObject(io.vertx.core.json.JsonObject) PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 4 with PoolMetrics

use of io.vertx.core.spi.metrics.PoolMetrics in project vert.x by eclipse.

the class VertxImpl method createSharedWorkerPool.

public synchronized SharedWorkerPool createSharedWorkerPool(String name, int poolSize, long maxExecuteTime, TimeUnit maxExecuteTimeUnit) {
    if (poolSize < 1) {
        throw new IllegalArgumentException("poolSize must be > 0");
    }
    if (maxExecuteTime < 1) {
        throw new IllegalArgumentException("maxExecuteTime must be > 0");
    }
    SharedWorkerPool sharedWorkerPool = namedWorkerPools.get(name);
    if (sharedWorkerPool == null) {
        ThreadFactory workerThreadFactory = createThreadFactory(maxExecuteTime, maxExecuteTimeUnit, name + "-", true);
        ExecutorService workerExec = executorServiceFactory.createExecutor(workerThreadFactory, poolSize, poolSize);
        PoolMetrics workerMetrics = metrics != null ? metrics.createPoolMetrics("worker", name, poolSize) : null;
        namedWorkerPools.put(name, sharedWorkerPool = new SharedWorkerPool(name, workerExec, workerMetrics));
    } else {
        sharedWorkerPool.refCount++;
    }
    return sharedWorkerPool;
}
Also used : VertxThreadFactory(io.vertx.core.spi.VertxThreadFactory) PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics)

Example 5 with PoolMetrics

use of io.vertx.core.spi.metrics.PoolMetrics in project vert.x by eclipse.

the class WorkerContext method run.

private <T> void run(ContextInternal ctx, TaskQueue queue, T value, Handler<T> task) {
    Objects.requireNonNull(task, "Task handler must not be null");
    PoolMetrics metrics = workerPool.metrics();
    Object queueMetric = metrics != null ? metrics.submitted() : null;
    queue.execute(() -> {
        Object execMetric = null;
        if (metrics != null) {
            execMetric = metrics.begin(queueMetric);
        }
        try {
            ctx.dispatch(value, task);
        } finally {
            if (metrics != null) {
                metrics.end(execMetric, true);
            }
        }
    }, workerPool.executor());
}
Also used : PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics)

Aggregations

PoolMetrics (io.vertx.core.spi.metrics.PoolMetrics)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Test (org.junit.Test)3 EventLoop (io.netty.channel.EventLoop)1 io.vertx.core (io.vertx.core)1 FileSystem (io.vertx.core.file.FileSystem)1 Logger (io.vertx.core.impl.logging.Logger)1 LoggerFactory (io.vertx.core.impl.logging.LoggerFactory)1 JsonObject (io.vertx.core.json.JsonObject)1 MetricsOptions (io.vertx.core.metrics.MetricsOptions)1 VertxThreadFactory (io.vertx.core.spi.VertxThreadFactory)1 VertxTracer (io.vertx.core.spi.tracing.VertxTracer)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Executor (java.util.concurrent.Executor)1 ExecutorService (java.util.concurrent.ExecutorService)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1