Search in sources :

Example 56 with ThreadFactoryBuilder

use of com.google.common.util.concurrent.ThreadFactoryBuilder in project opennms by OpenNMS.

the class StressCommand method doExecute.

@Override
protected Object doExecute() {
    // Apply sane lower bounds to all of the configurable options
    eventsPerSecondPerThread = Math.max(1, eventsPerSecondPerThread);
    numberOfThreads = Math.max(1, numberOfThreads);
    numSeconds = Math.max(1, numSeconds);
    reportIntervalInSeconds = Math.max(1, reportIntervalInSeconds);
    batchSize = Math.max(1, batchSize);
    boolean useJexl = jexlExpressions != null && jexlExpressions.size() > 0;
    // Display the effective settings and rates
    double eventsPerSecond = eventsPerSecondPerThread * numberOfThreads;
    System.out.printf("Generating %d events per second accross %d threads for %d seconds\n", eventsPerSecondPerThread, numberOfThreads, numSeconds);
    System.out.printf("\t with UEI: %s\n", eventUei);
    System.out.printf("\t with batch size: %d\n", batchSize);
    System.out.printf("\t with synchronous calls: %s\n", isSynchronous);
    System.out.printf("Which will yield an effective\n");
    System.out.printf("\t %.2f events per second\n", eventsPerSecond);
    System.out.printf("\t %.2f total events\n", eventsPerSecond * numSeconds);
    if (useJexl) {
        System.out.printf("Using JEXL expressions:\n");
        for (String jexlExpression : jexlExpressions) {
            System.out.printf("\t%s\n", jexlExpression);
        }
    }
    // Setup the reporter
    ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
    // Setup the executor
    final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Event Generator #%d").build();
    final ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads, threadFactory);
    System.out.println("Starting.");
    try {
        reporter.start(reportIntervalInSeconds, TimeUnit.SECONDS);
        for (int i = 0; i < numberOfThreads; i++) {
            final EventGenerator eventGenerator = useJexl ? new JexlEventGenerator(jexlExpressions) : new EventGenerator();
            executor.execute(eventGenerator);
        }
        System.out.println("Started.");
        // Wait until we timeout or get interrupted
        try {
            Thread.sleep(SECONDS.toMillis(numSeconds));
        } catch (InterruptedException e) {
        }
        // Stop!
        try {
            System.out.println("Stopping.");
            executor.shutdownNow();
            if (!executor.awaitTermination(2, TimeUnit.MINUTES)) {
                System.err.println("The threads did not stop in time.");
            } else {
                System.out.println("Stopped.");
            }
        } catch (InterruptedException e) {
        }
    } finally {
        // Make sure we always stop the reporter
        reporter.stop();
    }
    // And display one last report...
    reporter.report();
    return null;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ConsoleReporter(com.codahale.metrics.ConsoleReporter) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

Example 57 with ThreadFactoryBuilder

use of com.google.common.util.concurrent.ThreadFactoryBuilder in project opennms by OpenNMS.

the class StressCommand method execute.

@Override
public Object execute() {
    // Apply sane lower bounds to all of the configurable options
    eventsPerSecondPerThread = Math.max(1, eventsPerSecondPerThread);
    numberOfThreads = Math.max(1, numberOfThreads);
    numSeconds = Math.max(1, numSeconds);
    reportIntervalInSeconds = Math.max(1, reportIntervalInSeconds);
    batchSize = Math.max(1, batchSize);
    boolean useJexl = jexlExpressions != null && jexlExpressions.size() > 0;
    // Display the effective settings and rates
    double eventsPerSecond = (double) eventsPerSecondPerThread * (double) numberOfThreads;
    System.out.printf("Generating %d events per second accross %d threads for %d seconds\n", eventsPerSecondPerThread, numberOfThreads, numSeconds);
    System.out.printf("\t with UEI: %s\n", eventUei);
    System.out.printf("\t with batch size: %d\n", batchSize);
    System.out.printf("\t with synchronous calls: %s\n", isSynchronous);
    System.out.printf("Which will yield an effective\n");
    System.out.printf("\t %.2f events per second\n", eventsPerSecond);
    System.out.printf("\t %.2f total events\n", eventsPerSecond * numSeconds);
    if (useJexl) {
        System.out.printf("Using JEXL expressions:\n");
        for (String jexlExpression : jexlExpressions) {
            System.out.printf("\t%s\n", jexlExpression);
        }
    }
    // Setup the reporter
    ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
    // Setup the executor
    final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Event Generator #%d").build();
    final ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads, threadFactory);
    System.out.println("Starting.");
    try {
        reporter.start(reportIntervalInSeconds, TimeUnit.SECONDS);
        for (int i = 0; i < numberOfThreads; i++) {
            final EventGenerator eventGenerator = useJexl ? new JexlEventGenerator(jexlExpressions) : new EventGenerator();
            executor.execute(eventGenerator);
        }
        System.out.println("Started.");
        // Wait until we timeout or get interrupted
        try {
            Thread.sleep(SECONDS.toMillis(numSeconds));
        } catch (InterruptedException e) {
        }
        // Stop!
        try {
            System.out.println("Stopping.");
            executor.shutdownNow();
            if (!executor.awaitTermination(2, TimeUnit.MINUTES)) {
                System.err.println("The threads did not stop in time.");
            } else {
                System.out.println("Stopped.");
            }
        } catch (InterruptedException e) {
        }
    } finally {
        // Make sure we always stop the reporter
        reporter.stop();
    }
    // And display one last report...
    reporter.report();
    return null;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ConsoleReporter(com.codahale.metrics.ConsoleReporter) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

Example 58 with ThreadFactoryBuilder

use of com.google.common.util.concurrent.ThreadFactoryBuilder in project metacat by Netflix.

the class AbstractThriftServer method start.

/**
 * Server initialization.
 *
 * @throws Exception error
 */
public void start() throws Exception {
    log.info("initializing thrift server {}", getServerName());
    final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(threadPoolNameFormat).setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception in thread: {}", t.getName(), e)).build();
    final ExecutorService executorService = new ThreadPoolExecutor(Math.min(2, config.getThriftServerMaxWorkerThreads()), config.getThriftServerMaxWorkerThreads(), 60L, TimeUnit.SECONDS, new SynchronousQueue<>(), threadFactory);
    RegistryUtil.registerThreadPool(registry, threadPoolNameFormat, (ThreadPoolExecutor) executorService);
    final int timeout = config.getThriftServerSocketClientTimeoutInSeconds() * 1000;
    final TServerTransport serverTransport = new TServerSocket(portNumber, timeout);
    startServing(executorService, serverTransport);
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Getter(lombok.Getter) SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) NonNull(lombok.NonNull) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TServer(org.apache.thrift.server.TServer) RegistryUtil(com.netflix.metacat.common.server.util.RegistryUtil) TServerTransport(org.apache.thrift.transport.TServerTransport) TimeUnit(java.util.concurrent.TimeUnit) TServerSocket(org.apache.thrift.transport.TServerSocket) Slf4j(lombok.extern.slf4j.Slf4j) TServerEventHandler(org.apache.thrift.server.TServerEventHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Registry(com.netflix.spectator.api.Registry) TThreadPoolServer(org.apache.thrift.server.TThreadPoolServer) ThreadFactory(java.util.concurrent.ThreadFactory) Config(com.netflix.metacat.common.server.properties.Config) ExecutorService(java.util.concurrent.ExecutorService) TProcessor(org.apache.thrift.TProcessor) TServerSocket(org.apache.thrift.transport.TServerSocket) ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) TServerTransport(org.apache.thrift.transport.TServerTransport)

Example 59 with ThreadFactoryBuilder

use of com.google.common.util.concurrent.ThreadFactoryBuilder in project metacat by Netflix.

the class SNSNotificationsConfig method amazonSNS.

/**
 * If SNS notifications are desired and no existing client has been created elsewhere
 * in the application create a default client here.
 * @param config       The system configuration abstraction to use
 * @param registry     registry for spectator
 * @return The configured SNS client
 */
// TODO: See what spring-cloud-aws would provide automatically...
@Bean
@ConditionalOnMissingBean(AmazonSNSAsync.class)
public AmazonSNSAsync amazonSNS(final Config config, final Registry registry) {
    final ExecutorService executor = Executors.newFixedThreadPool(config.getSnsClientThreadCount(), new ThreadFactoryBuilder().setNameFormat("metacat-sns-pool-%d").build());
    RegistryUtil.registerThreadPool(registry, "metacat-sns-pool", (ThreadPoolExecutor) executor);
    return new AmazonSNSAsyncClient(DefaultAWSCredentialsProviderChain.getInstance(), executor);
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) AmazonSNSAsyncClient(com.amazonaws.services.sns.AmazonSNSAsyncClient) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 60 with ThreadFactoryBuilder

use of com.google.common.util.concurrent.ThreadFactoryBuilder in project hive by apache.

the class PartitionExport method write.

void write(final ReplicationSpec forReplicationSpec) throws InterruptedException {
    ExecutorService producer = Executors.newFixedThreadPool(1);
    producer.submit(() -> {
        for (Partition partition : partitionIterable) {
            try {
                queue.put(partition);
            } catch (InterruptedException e) {
                throw new RuntimeException("Error while queuing up the partitions for export of data files", e);
            }
        }
    });
    producer.shutdown();
    ThreadFactory namingThreadFactory = new ThreadFactoryBuilder().setNameFormat("partition-dump-thread-%d").build();
    ExecutorService consumer = Executors.newFixedThreadPool(nThreads, namingThreadFactory);
    while (!producer.isTerminated() || !queue.isEmpty()) {
        /*
      This is removed using a poll because there can be a case where there partitions iterator is empty
      but because both the producer and consumer are started simultaneously the while loop will execute
      because producer is not terminated but it wont produce anything so queue will be empty and then we
      should only wait for a specific time before continuing, as the next loop cycle will fail.
       */
        Partition partition = queue.poll(1, TimeUnit.SECONDS);
        if (partition == null) {
            continue;
        }
        LOG.debug("scheduling partition dump {}", partition.getName());
        consumer.submit(() -> {
            String partitionName = partition.getName();
            String threadName = Thread.currentThread().getName();
            LOG.debug("Thread: {}, start partition dump {}", threadName, partitionName);
            Path fromPath = partition.getDataLocation();
            try {
                // this the data copy
                Path rootDataDumpDir = paths.partitionExportDir(partitionName);
                new FileOperations(fromPath, rootDataDumpDir, distCpDoAsUser, hiveConf).export(forReplicationSpec);
                LOG.debug("Thread: {}, finish partition dump {}", threadName, partitionName);
            } catch (Exception e) {
                throw new RuntimeException("Error while export of data files", e);
            }
        });
    }
    consumer.shutdown();
    // may be drive this via configuration as well.
    consumer.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
}
Also used : Path(org.apache.hadoop.fs.Path) Partition(org.apache.hadoop.hive.ql.metadata.Partition) ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) FileOperations(org.apache.hadoop.hive.ql.parse.repl.dump.io.FileOperations) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

Aggregations

ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)143 ExecutorService (java.util.concurrent.ExecutorService)49 ThreadFactory (java.util.concurrent.ThreadFactory)46 IOException (java.io.IOException)23 Future (java.util.concurrent.Future)19 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)19 ExecutionException (java.util.concurrent.ExecutionException)17 ArrayList (java.util.ArrayList)15 Callable (java.util.concurrent.Callable)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 HashMap (java.util.HashMap)11 Path (org.apache.hadoop.fs.Path)11 LinkedList (java.util.LinkedList)10 Map (java.util.Map)10 HashSet (java.util.HashSet)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)9 Test (org.junit.Test)9 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)8 Before (org.junit.Before)8