Search in sources :

Example 96 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project stanbol by apache.

the class EntityhubDereferenceEngine method activate.

@Activate
@SuppressWarnings("unchecked")
protected void activate(ComponentContext ctx) throws ConfigurationException {
    Dictionary<String, Object> properties = ctx.getProperties();
    bundleContext = ctx.getBundleContext();
    log.info("> activate {}", getClass().getSimpleName());
    //get the metadata later set to the enhancement engine
    DereferenceEngineConfig engineConfig = new DereferenceEngineConfig(properties, prefixService);
    log.debug(" - engineName: {}", engineConfig.getEngineName());
    //parse the Entityhub Site used for dereferencing
    Object value = properties.get(SITE_ID);
    //init the EntitySource
    if (value == null) {
        //all referenced sites
        siteName = "*";
    } else {
        siteName = value.toString();
    }
    if (siteName.isEmpty()) {
        siteName = "*";
    }
    log.debug(" - siteName: {}", siteName);
    final boolean sharedPoolState;
    value = properties.get(SHARED_THREAD_POOL_STATE);
    if (value instanceof Boolean) {
        sharedPoolState = ((Boolean) value).booleanValue();
    } else if (value != null && !StringUtils.isBlank(value.toString())) {
        sharedPoolState = Boolean.parseBoolean(value.toString());
    } else {
        sharedPoolState = DEFAULT_SHARED_THREAD_POOL_STATE;
    }
    final ExecutorServiceProvider esProvider;
    log.debug(" - shared thread pool state: {}", sharedPoolState);
    if (sharedPoolState) {
        esProvider = new SharedExecutorServiceProvider(ctx.getBundleContext());
    } else {
        //we need to create our own ExecutorService
        value = properties.get(THREAD_POOL_SIZE);
        if (value instanceof Number) {
            this.threadPoolSize = ((Number) value).intValue();
        } else if (value != null) {
            try {
                this.threadPoolSize = Integer.parseInt(value.toString());
            } catch (NumberFormatException e) {
                throw new ConfigurationException(THREAD_POOL_SIZE, "Value '" + value + "'(type: " + value.getClass().getName() + ") can not be parsed " + "as Integer");
            }
        } else {
            this.threadPoolSize = DEFAULT_THREAD_POOL_SIZE;
        }
        if (threadPoolSize > 0) {
            String namePattern = getClass().getSimpleName() + "-" + engineConfig.getEngineName() + "-thread-%s";
            ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(namePattern).setDaemon(true).build();
            log.debug(" - create Threadpool(namePattern='{}' | size='{}')", namePattern, threadPoolSize);
            executorService = Executors.newFixedThreadPool(threadPoolSize, threadFactory);
        } else {
            log.debug(" - no thread pool configured (poolSize: {})", threadPoolSize);
            executorService = null;
        }
        esProvider = new StaticExecutorServiceProvider(executorService);
    }
    //init the tracking entity searcher
    trackedServiceCount = 0;
    if (Entityhub.ENTITYHUB_IDS.contains(siteName.toLowerCase())) {
        log.info("  ... init Entityhub dereferencer");
        entityDereferencer = new EntityhubDereferencer(bundleContext, this, esProvider);
    } else if (siteName.equals("*")) {
        log.info("  ... init dereferencer for all referenced sites");
        entityDereferencer = new SitesDereferencer(bundleContext, this, esProvider);
    } else {
        log.info(" ... init dereferencer for referenced site {}", siteName);
        entityDereferencer = new SiteDereferencer(bundleContext, siteName, this, esProvider);
    }
    //set the namespace prefix service to the dereferencer
    entityDereferencer.setNsPrefixService(prefixService);
    //now parse dereference field config
    entityDereferencer.setDereferencedFields(engineConfig.getDereferenceFields());
    entityDereferencer.setLdPath(engineConfig.getLdPathProgram());
    entityDereferenceEngine = new EntityDereferenceEngine(entityDereferencer, engineConfig, new //we want to use our own DereferenceContext impl
    DereferenceContextFactory() {

        @Override
        public DereferenceContext createContext(EntityDereferenceEngine engine, Map<String, Object> enhancementProperties) throws DereferenceConfigurationException {
            return new EntityhubDereferenceContext(engine, enhancementProperties);
        }
    });
    //NOTE: registration of this instance as OSGI service is done as soon as the
    //      entityhub service backing the entityDereferencer is available.
    //finally start tracking
    entityDereferencer.open();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) DereferenceContextFactory(org.apache.stanbol.enhancer.engines.dereference.DereferenceContextFactory) DereferenceEngineConfig(org.apache.stanbol.enhancer.engines.dereference.DereferenceEngineConfig) SharedExecutorServiceProvider(org.apache.stanbol.enhancer.engines.dereference.entityhub.shared.SharedExecutorServiceProvider) EntityDereferenceEngine(org.apache.stanbol.enhancer.engines.dereference.EntityDereferenceEngine) SharedExecutorServiceProvider(org.apache.stanbol.enhancer.engines.dereference.entityhub.shared.SharedExecutorServiceProvider) ConfigurationException(org.osgi.service.cm.ConfigurationException) DereferenceConfigurationException(org.apache.stanbol.enhancer.engines.dereference.DereferenceConfigurationException) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Map(java.util.Map) Activate(org.apache.felix.scr.annotations.Activate)

Example 97 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project opennms by OpenNMS.

the class StressCommand method doExecute.

@Override
protected Void doExecute() {
    // Apply sane lower bounds to all of the configurable options
    intervalInSeconds = Math.max(1, intervalInSeconds);
    numberOfNodes = Math.max(1, numberOfNodes);
    numberOfInterfacesPerNode = Math.max(1, numberOfInterfacesPerNode);
    numberOfGroupsPerInterface = Math.max(1, numberOfGroupsPerInterface);
    numberOfNumericAttributesPerGroup = Math.max(0, numberOfNumericAttributesPerGroup);
    numberOfStringAttributesPerGroup = Math.max(0, numberOfStringAttributesPerGroup);
    reportIntervalInSeconds = Math.max(1, reportIntervalInSeconds);
    numberOfGeneratorThreads = Math.max(1, numberOfGeneratorThreads);
    stringVariationFactor = Math.max(0, stringVariationFactor);
    if (stringVariationFactor > 0) {
        stringAttributesVaried = metrics.meter("string-attributes-varied");
    }
    // Display the effective settings and rates
    double attributesPerSecond = (1 / (double) intervalInSeconds) * numberOfGroupsPerInterface * numberOfInterfacesPerNode * numberOfNodes;
    System.out.printf("Generating collection sets every %d seconds\n", intervalInSeconds);
    System.out.printf("\t for %d nodes\n", numberOfNodes);
    System.out.printf("\t with %d interfaces\n", numberOfInterfacesPerNode);
    System.out.printf("\t with %d attribute groups\n", numberOfGroupsPerInterface);
    System.out.printf("\t with %d numeric attributes\n", numberOfNumericAttributesPerGroup);
    System.out.printf("\t with %d string attributes\n", numberOfStringAttributesPerGroup);
    System.out.printf("Across %d threads\n", numberOfGeneratorThreads);
    if (stringVariationFactor > 0) {
        System.out.printf("With string variation factor %d\n", stringVariationFactor);
    }
    System.out.printf("Which will yield an effective\n");
    System.out.printf("\t %.2f numeric attributes per second\n", numberOfNumericAttributesPerGroup * attributesPerSecond);
    System.out.printf("\t %.2f string attributes per second\n", numberOfStringAttributesPerGroup * attributesPerSecond);
    ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
    // Setup the executor
    ThreadFactory threadFactoy = new ThreadFactoryBuilder().setNameFormat("Metrics Stress Tool Generator #%d").build();
    ExecutorService executor = Executors.newFixedThreadPool(numberOfGeneratorThreads, threadFactoy);
    // Setup auxiliary objects needed by the persister
    ServiceParameters params = new ServiceParameters(Collections.emptyMap());
    RrdRepository repository = new RrdRepository();
    repository.setStep(Math.max(intervalInSeconds, 1));
    repository.setHeartBeat(repository.getStep() * 2);
    if (rras != null && rras.size() > 0) {
        repository.setRraList(rras);
    } else {
        repository.setRraList(Lists.newArrayList(// Use the default list of RRAs we provide in our stock configuration files
        "RRA:AVERAGE:0.5:1:2016", "RRA:AVERAGE:0.5:12:1488", "RRA:AVERAGE:0.5:288:366", "RRA:MAX:0.5:288:366", "RRA:MIN:0.5:288:366"));
    }
    repository.setRrdBaseDir(Paths.get(System.getProperty("opennms.home"), "share", "rrd", "snmp").toFile());
    // Calculate how we fast we should insert the collection sets
    int sleepTimeInMillisBetweenNodes = 0;
    int sleepTimeInSecondsBetweenIterations = 0;
    System.out.printf("Sleeping for\n");
    if (burst) {
        sleepTimeInSecondsBetweenIterations = intervalInSeconds;
        System.out.printf("\t %d seconds between batches\n", sleepTimeInSecondsBetweenIterations);
    } else {
        // We want to "stream" the collection sets
        sleepTimeInMillisBetweenNodes = Math.round((((float) intervalInSeconds * 1000) / numberOfNodes) * numberOfGeneratorThreads);
        System.out.printf("\t %d milliseconds between nodes\n", sleepTimeInMillisBetweenNodes);
    }
    // Start generating, and keep generating until we're interrupted
    try {
        reporter.start(reportIntervalInSeconds, TimeUnit.SECONDS);
        while (true) {
            final Context context = batchTimer.time();
            try {
                // Split the tasks up among the threads
                List<Future<Void>> futures = new ArrayList<>();
                for (int generatorThreadId = 0; generatorThreadId < numberOfGeneratorThreads; generatorThreadId++) {
                    futures.add(executor.submit(generateAndPersistCollectionSets(params, repository, generatorThreadId, sleepTimeInMillisBetweenNodes)));
                }
                // Wait for all the tasks to complete before starting others
                for (Future<Void> future : futures) {
                    future.get();
                }
            } catch (InterruptedException | ExecutionException e) {
                break;
            } finally {
                context.stop();
            }
            try {
                Thread.sleep(sleepTimeInSecondsBetweenIterations * 1000);
            } catch (InterruptedException e) {
                break;
            }
        }
    } finally {
        reporter.stop();
        abort.set(true);
        executor.shutdownNow();
    }
    return null;
}
Also used : Context(com.codahale.metrics.Timer.Context) ThreadFactory(java.util.concurrent.ThreadFactory) ConsoleReporter(com.codahale.metrics.ConsoleReporter) ArrayList(java.util.ArrayList) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Future(java.util.concurrent.Future) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) ExecutionException(java.util.concurrent.ExecutionException)

Example 98 with ThreadFactory

use of java.util.concurrent.ThreadFactory 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 99 with ThreadFactory

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

the class QueueManagerImpl method start.

public void start(ScheduledExecutorService background) {
    try {
        blackList.start(background);
        endpointManager.addListener(endpointListener);
        // Use a separate timer for queue management tasks
        // We don't want primary recovery (and therefore user threads) to wait for
        // things like pinging connections for health checks.
        // this.background = background;
        final String name = "queueTimer-" + this.pool.getName();
        this.recoveryThread = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {

            public Thread newThread(Runnable r) {
                Thread result = new Thread(r, name);
                result.setDaemon(true);
                return result;
            }
        });
        recoveryThread.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
        // TODO - use yet another Timer or the like for these tasks? We know
        // we don't want them in the recoveryThread, because the ThreadIdToSequenceIdExpiryTask
        // will wait for primary recovery.
        getState().start(background, getPool().getSubscriptionAckInterval());
        // initialize connections
        initializeConnections();
        scheduleRedundancySatisfierIfNeeded(redundancyRetryInterval);
        // When a server is removed from the blacklist, try again
        // to establish redundancy (if we need to)
        BlackListListener blackListListener = new BlackListListenerAdapter() {

            @Override
            public void serverRemoved(ServerLocation location) {
                QueueManagerImpl.this.scheduleRedundancySatisfierIfNeeded(0);
            }
        };
        blackList.addListener(blackListListener);
        factory.getBlackList().addListener(blackListListener);
    } finally {
        initializedLatch.countDown();
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) BlackListListener(org.apache.geode.cache.client.internal.ServerBlackList.BlackListListener) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) BlackListListenerAdapter(org.apache.geode.cache.client.internal.ServerBlackList.BlackListListenerAdapter)

Example 100 with ThreadFactory

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

the class PoolImpl method start.

private void start() {
    if (this.startDisabled)
        return;
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (isDebugEnabled) {
        List locators = getLocators();
        if (!locators.isEmpty()) {
            logger.debug("PoolImpl - starting pool with locators: {}", locators);
        } else {
            logger.debug("PoolImpl -starting pool with servers: {}", getServers());
        }
    }
    final String timerName = "poolTimer-" + getName() + "-";
    backgroundProcessor = new ScheduledThreadPoolExecutorWithKeepAlive(BACKGROUND_TASK_POOL_SIZE, BACKGROUND_TASK_POOL_KEEP_ALIVE, TimeUnit.MILLISECONDS, new ThreadFactory() {

        AtomicInteger threadNum = new AtomicInteger();

        public Thread newThread(final Runnable r) {
            Thread result = new Thread(r, timerName + threadNum.incrementAndGet());
            result.setDaemon(true);
            return result;
        }
    });
    ((ScheduledThreadPoolExecutorWithKeepAlive) backgroundProcessor).setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
    ((ScheduledThreadPoolExecutorWithKeepAlive) backgroundProcessor).setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    source.start(this);
    connectionFactory.start(backgroundProcessor);
    endpointManager.addListener(new InstantiatorRecoveryListener(backgroundProcessor, this));
    endpointManager.addListener(new DataSerializerRecoveryListener(backgroundProcessor, this));
    if (Boolean.getBoolean(ON_DISCONNECT_CLEAR_PDXTYPEIDS)) {
        endpointManager.addListener(new PdxRegistryRecoveryListener(this));
    }
    endpointManager.addListener(new LiveServerPinger(this));
    manager.start(backgroundProcessor);
    if (queueManager != null) {
        if (isDebugEnabled) {
            logger.debug("starting queueManager");
        }
        queueManager.start(backgroundProcessor);
    }
    if (isDebugEnabled) {
        logger.debug("scheduling pings every {} milliseconds", pingInterval);
    }
    if (this.statisticInterval > 0 && this.dsys.getConfig().getStatisticSamplingEnabled()) {
        backgroundProcessor.scheduleWithFixedDelay(new PublishClientStatsTask(), statisticInterval, statisticInterval, TimeUnit.MILLISECONDS);
    }
    // LOG: changed from config to info
    logger.info(LocalizedMessage.create(LocalizedStrings.PoolImpl_POOL_0_STARTED_WITH_MULTIUSER_SECURE_MODE_ENABLED_1, new Object[] { this.name, this.multiuserSecureModeEnabled }));
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ScheduledThreadPoolExecutorWithKeepAlive(org.apache.geode.internal.ScheduledThreadPoolExecutorWithKeepAlive) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ThreadFactory (java.util.concurrent.ThreadFactory)250 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)47 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)46 ExecutorService (java.util.concurrent.ExecutorService)45 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)21 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)19 Test (org.junit.Test)17 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)16 Future (java.util.concurrent.Future)15 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)13 ArrayList (java.util.ArrayList)13 LoggingThreadGroup (org.apache.geode.internal.logging.LoggingThreadGroup)12 IOException (java.io.IOException)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 ExecutionException (java.util.concurrent.ExecutionException)9 Executor (java.util.concurrent.Executor)9 ChannelFuture (io.netty.channel.ChannelFuture)8 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)8 SynchronousQueue (java.util.concurrent.SynchronousQueue)7