Search in sources :

Example 96 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project voltdb by VoltDB.

the class LatencyLogger method main.

public static void main(String[] args) throws Exception {
    final String hostname;
    if (args.length == 1) {
        readHistogramFromFile(args[0]);
        return;
    }
    if (args.length != 3) {
        if (args.length == 4) {
            hostname = args[3];
        } else {
            System.out.println(usage());
            return;
        }
    } else {
        hostname = args[0];
    }
    final String server = args[0];
    int dur = 0;
    try {
        dur = Integer.valueOf(args[2]);
        if (dur < 1) {
            throw new NumberFormatException();
        }
    } catch (NumberFormatException e) {
        System.out.println("reportIntervalSeconds should be greater than or equal to 1");
        System.out.println(usage());
        System.exit(0);
    }
    final int duration = dur;
    // start with an empty password
    String username = "";
    String password = "";
    // if credentials set in env, use them
    if (System.getenv().containsKey("VOLTDBUSER")) {
        username = System.getenv("VOLTDBUSER");
    }
    if (System.getenv().containsKey("VOLTDBPASSWORD")) {
        password = System.getenv("VOLTDBPASSWORD");
    }
    // create the client with our credentials
    ClientConfig clientConfig = new ClientConfig(username, password);
    final Client c = ClientFactory.createClient(clientConfig);
    int port = 0;
    try {
        port = Integer.valueOf(args[1]);
    } catch (NumberFormatException e) {
        System.out.println("Failed to parse port number.");
        System.out.println("Usage server port reportIntervalSeconds");
        System.exit(0);
    }
    System.out.println("Connecting to " + server + " port " + port);
    c.createConnection(args[0], port);
    System.out.printf("%12s, %10s, %10s, %10s, %10s, %10s, %10s, %10s\n", "TIMESTAMP", "COUNT", "TPS", "95", "99", "99.9", "99.99", "99.999");
    final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
    ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
    ses.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            VoltTable table = null;
            try {
                table = c.callProcedure("@Statistics", "LATENCY_HISTOGRAM", 0).getResults()[0];
            } catch (IOException | ProcCallException e) {
                System.out.println("Failed to get statistics:");
                e.printStackTrace();
                System.exit(0);
            }
            List<String> hostnames = new ArrayList<String>();
            String tableHostname = "";
            while (!hostname.equalsIgnoreCase(tableHostname)) {
                if (!table.advanceRow()) {
                    System.out.println("Server host name " + server + " not found. Valid host names are " + hostnames.toString());
                    System.exit(0);
                }
                tableHostname = table.getString(2);
                hostnames.add(tableHostname);
            }
            Date now = new Date(table.getLong(0));
            Histogram newHistogram = AbstractHistogram.fromCompressedBytes(table.getVarbinary(4), CompressionStrategySnappy.INSTANCE);
            Histogram diffHistogram;
            if (m_histogramData == null) {
                diffHistogram = newHistogram;
            } else {
                diffHistogram = Histogram.diff(newHistogram, m_histogramData);
            }
            long totalCount = diffHistogram.getTotalCount();
            if (totalCount > 0) {
                System.out.printf("%12s, %10d, %10.0f, %8.2fms, %8.2fms, %8.2fms, %8.2fms, %8.2fms\n", sdf.format(now), totalCount, ((double) totalCount / duration), (diffHistogram.getValueAtPercentile(95.0D) / 1000.0D), (diffHistogram.getValueAtPercentile(99) / 1000.0D), (diffHistogram.getValueAtPercentile(99.9) / 1000.0D), (diffHistogram.getValueAtPercentile(99.99) / 1000.0D), (diffHistogram.getValueAtPercentile(99.999) / 1000.0D));
            } else {
                System.out.printf("%12s, %10d, %10d, %8.2fms, %8.2fms, %8.2fms, %8.2fms, %8.2fms\n", sdf.format(now), totalCount, 0, 0D, 0D, 0D, 0D, 0D);
            }
            m_histogramData = AbstractHistogram.fromCompressedBytes(table.getVarbinary(4), CompressionStrategySnappy.INSTANCE);
            ;
        }
    }, 0, duration, TimeUnit.SECONDS);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AbstractHistogram(org.HdrHistogram_voltpatches.AbstractHistogram) Histogram(org.HdrHistogram_voltpatches.Histogram) VoltTable(org.voltdb.VoltTable) Date(java.util.Date) ArrayList(java.util.ArrayList) List(java.util.List) ClientConfig(org.voltdb.client.ClientConfig) Client(org.voltdb.client.Client) SimpleDateFormat(java.text.SimpleDateFormat)

Example 97 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project Gadgetbridge by Freeyourgadget.

the class LiveActivityFragment method startActivityPulse.

private ScheduledExecutorService startActivityPulse() {
    ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
    service.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            FragmentActivity activity = LiveActivityFragment.this.getActivity();
            if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        pulse();
                    }
                });
            }
        }
    }, 0, getPulseIntervalMillis(), TimeUnit.MILLISECONDS);
    return service;
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService)

Example 98 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project graylog2-server by Graylog2.

the class SchedulerBindings method configure.

@Override
protected void configure() {
    // TODO Add instrumentation to ExecutorService and ThreadFactory
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(SCHEDULED_THREADS_POOL_SIZE, new ThreadFactoryBuilder().setNameFormat("scheduled-%d").setDaemon(false).setUncaughtExceptionHandler(new Tools.LogUncaughtExceptionHandler(LOG)).build());
    bind(ScheduledExecutorService.class).annotatedWith(Names.named("scheduler")).toInstance(scheduler);
    // TODO Add instrumentation to ExecutorService and ThreadFactory
    final ScheduledExecutorService daemonScheduler = Executors.newScheduledThreadPool(SCHEDULED_THREADS_POOL_SIZE, new ThreadFactoryBuilder().setNameFormat("scheduled-daemon-%d").setDaemon(true).setUncaughtExceptionHandler(new Tools.LogUncaughtExceptionHandler(LOG)).build());
    bind(ScheduledExecutorService.class).annotatedWith(Names.named("daemonScheduler")).toInstance(daemonScheduler);
    bind(Periodicals.class).toInstance(new Periodicals(scheduler, daemonScheduler));
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Tools(org.graylog2.plugin.Tools) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Periodicals(org.graylog2.periodical.Periodicals)

Example 99 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project geode by apache.

the class CallbackSampler method start.

public void start(StatisticsManager statisticsManager, ThreadGroup threadGroup, int sampleInterval, TimeUnit timeUnit) {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(runnable -> {
        Thread thread = new Thread(threadGroup, runnable, "CallbackSampler");
        thread.setDaemon(true);
        return thread;
    });
    start(executor, statisticsManager, sampleInterval, timeUnit);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService)

Example 100 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project geode by apache.

the class OneTaskOnlyDecoratorJUnitTest method testExecuteOnlyOnce.

/**
   * Test to make sure we only execute the task once no matter how many times we schedule it.
   */
@Test
public void testExecuteOnlyOnce() throws Exception {
    ScheduledExecutorService ex = Executors.newScheduledThreadPool(1);
    MyConflationListener listener = new MyConflationListener();
    OneTaskOnlyExecutor decorator = new OneTaskOnlyExecutor(ex, listener);
    final CountDownLatch latch = new CountDownLatch(1);
    ex.submit(new Callable() {

        public Object call() throws Exception {
            latch.await();
            return null;
        }
    });
    final AtomicInteger counter = new AtomicInteger();
    Runnable increment = new Runnable() {

        public void run() {
            counter.incrementAndGet();
        }
    };
    for (int i = 0; i < 50; i++) {
        decorator.schedule(increment, 0, TimeUnit.SECONDS);
    }
    assertEquals(0, counter.get());
    latch.countDown();
    ex.shutdown();
    ex.awaitTermination(60, TimeUnit.SECONDS);
    assertEquals(1, counter.get());
    assertEquals(49, listener.getDropCount());
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)285 Test (org.junit.Test)81 ExecutorService (java.util.concurrent.ExecutorService)37 Test (org.testng.annotations.Test)35 CountDownLatch (java.util.concurrent.CountDownLatch)32 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)28 IOException (java.io.IOException)27 ArrayList (java.util.ArrayList)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 HashMap (java.util.HashMap)23 Map (java.util.Map)20 List (java.util.List)16 None (com.linkedin.common.util.None)15 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)15 DefaultStatisticsProvider (org.apache.jackrabbit.oak.stats.DefaultStatisticsProvider)15 URI (java.net.URI)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)14 HashSet (java.util.HashSet)13 ThreadFactory (java.util.concurrent.ThreadFactory)13 TimeUnit (java.util.concurrent.TimeUnit)13