Search in sources :

Example 71 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project toolkit by googleapis.

the class Pubsub method grpc.

private static void grpc(final Settings settings) throws Exception {
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    ManagedChannel channel = NettyChannelBuilder.forTarget(settings.endpoint()).executor(executor).sslContext(GrpcSslContexts.forClient().trustManager(new File(settings.cert())).build()).build();
    final Semaphore semaphore = new Semaphore(settings.numWorkers());
    Metadata header = new Metadata();
    header.put(X_GOOG_HEADER_KEY, X_GOOG_HEADER_VALUE);
    final PublisherFutureStub stub = MetadataUtils.attachHeaders(PublisherGrpc.newFutureStub(channel), header);
    final AtomicLong resetTime = new AtomicLong();
    final AtomicLong numCalls = new AtomicLong();
    final AtomicLong numErrs = new AtomicLong();
    long endTime = System.nanoTime() + settings.warmDurNano() + settings.targetDurNano();
    Thread resetter = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(settings.warmDurNano() / MILLION);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            numCalls.set(0);
            numErrs.set(0);
            resetTime.set(System.nanoTime());
        }
    });
    resetter.start();
    while (System.nanoTime() < endTime) {
        semaphore.acquire(1);
        Futures.addCallback(stub.getTopic(GetTopicRequest.newBuilder().setTopic(TOPIC_NAME_STRING).build()), new FutureCallback<Topic>() {

            @Override
            public void onSuccess(Topic topic) {
                if (!topic.getName().equals(TOPIC_NAME_STRING)) {
                    numErrs.incrementAndGet();
                }
                both();
            }

            @Override
            public void onFailure(Throwable t) {
                numErrs.incrementAndGet();
                both();
            }

            void both() {
                numCalls.incrementAndGet();
                semaphore.release(1);
            }
        });
    }
    long nCalls = numCalls.get();
    long nErrs = numErrs.get();
    long runDurNano = System.nanoTime() - resetTime.get();
    System.out.println("errors: " + nErrs);
    System.out.println("calls: " + nCalls);
    System.out.println("time per call (ns): " + (runDurNano / nCalls));
    System.out.println("QPS: " + (nCalls * BILLION / runDurNano));
    channel.shutdown().awaitTermination(10, TimeUnit.SECONDS);
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Metadata(io.grpc.Metadata) Semaphore(java.util.concurrent.Semaphore) PublisherFutureStub(com.google.pubsub.v1.PublisherGrpc.PublisherFutureStub) AtomicLong(java.util.concurrent.atomic.AtomicLong) ManagedChannel(io.grpc.ManagedChannel) Topic(com.google.pubsub.v1.Topic) File(java.io.File)

Example 72 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project hive by apache.

the class SQLOperation method prepare.

/**
 * Compile the query and extract metadata
 *
 * @throws HiveSQLException
 */
public void prepare(QueryState queryState) throws HiveSQLException {
    setState(OperationState.RUNNING);
    try {
        driver = DriverFactory.newDriver(queryState, getParentSession().getUserName(), queryInfo);
        // queryTimeout == 0 means no timeout
        if (queryTimeout > 0) {
            timeoutExecutor = new ScheduledThreadPoolExecutor(1);
            Runnable timeoutTask = new Runnable() {

                @Override
                public void run() {
                    try {
                        String queryId = queryState.getQueryId();
                        LOG.info("Query timed out after: " + queryTimeout + " seconds. Cancelling the execution now: " + queryId);
                        SQLOperation.this.cancel(OperationState.TIMEDOUT);
                    } catch (HiveSQLException e) {
                        LOG.error("Error cancelling the query after timeout: " + queryTimeout + " seconds", e);
                    } finally {
                        // Stop
                        timeoutExecutor.shutdown();
                    }
                }
            };
            timeoutExecutor.schedule(timeoutTask, queryTimeout, TimeUnit.SECONDS);
        }
        queryInfo.setQueryDisplay(driver.getQueryDisplay());
        // set the operation handle information in Driver, so that thrift API users
        // can use the operation handle they receive, to lookup query information in
        // Yarn ATS
        String guid64 = Base64.encodeBase64URLSafeString(getHandle().getHandleIdentifier().toTHandleIdentifier().getGuid()).trim();
        driver.setOperationId(guid64);
        // In Hive server mode, we are not able to retry in the FetchTask
        // case, when calling fetch queries since execute() has returned.
        // For now, we disable the test attempts.
        response = driver.compileAndRespond(statement);
        if (0 != response.getResponseCode()) {
            throw toSQLException("Error while compiling statement", response);
        }
        mResultSchema = driver.getSchema();
        // "explain" is an exception for now
        if (driver.getPlan().getFetchTask() != null) {
            // Schema has to be set
            if (mResultSchema == null || !mResultSchema.isSetFieldSchemas()) {
                throw new HiveSQLException("Error compiling query: Schema and FieldSchema " + "should be set when query plan has a FetchTask");
            }
            resultSchema = new TableSchema(mResultSchema);
            setHasResultSet(true);
        } else {
            setHasResultSet(false);
        }
        // TODO explain should use a FetchTask for reading
        for (Task<? extends Serializable> task : driver.getPlan().getRootTasks()) {
            if (task.getClass() == ExplainTask.class) {
                resultSchema = new TableSchema(mResultSchema);
                setHasResultSet(true);
                break;
            }
        }
    } catch (HiveSQLException e) {
        setState(OperationState.ERROR);
        throw e;
    } catch (Throwable e) {
        setState(OperationState.ERROR);
        throw new HiveSQLException("Error running query: " + e.toString(), e);
    }
}
Also used : TableSchema(org.apache.hive.service.cli.TableSchema) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) HiveSQLException(org.apache.hive.service.cli.HiveSQLException)

Example 73 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project cxf by apache.

the class NotificatorService method start.

/**
 * Starts this NotificatorService. You MUST run this method on every instance
 * before starting to pass any events to it. Run it only once.
 */
public void start() {
    obtainManager().registerNotificator(this);
    service = new ScheduledThreadPoolExecutor(CORE_POOL_SIZE);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)

Example 74 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project tomee by apache.

the class ManagedScheduledExecutorServiceImplFactoryTest method createServiceTest.

@Test
public void createServiceTest() {
    final ManagedScheduledExecutorServiceImplFactory factory = new ManagedScheduledExecutorServiceImplFactory();
    factory.setThreadFactory(MyThreadFactory.class.getName());
    final ManagedScheduledExecutorServiceImpl executorService = factory.create();
    final ScheduledThreadPoolExecutor poolExecutor = (ScheduledThreadPoolExecutor) executorService.getDelegate();
    Assert.assertEquals(poolExecutor.getThreadFactory().getClass(), ManagedThreadFactoryImpl.class);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ManagedScheduledExecutorServiceImpl(org.apache.openejb.threads.impl.ManagedScheduledExecutorServiceImpl) ManagedScheduledExecutorServiceImplFactory(org.apache.openejb.resource.thread.ManagedScheduledExecutorServiceImplFactory) Test(org.junit.Test)

Example 75 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project springside4 by springside.

the class ThreadPoolUtilTest method wrapException.

@Test
public void wrapException() {
    ScheduledThreadPoolExecutor executor = ThreadPoolBuilder.scheduledPool().build();
    ExceptionTask task = new ExceptionTask();
    executor.scheduleAtFixedRate(task, 0, 100, TimeUnit.MILLISECONDS);
    ThreadUtil.sleep(500);
    // 线程第一次跑就被中断
    assertThat(task.counter.get()).isEqualTo(1);
    ThreadPoolUtil.gracefulShutdown(executor, 1000);
    // //////
    executor = ThreadPoolBuilder.scheduledPool().build();
    ExceptionTask newTask = new ExceptionTask();
    Runnable wrapTask = ThreadPoolUtil.safeRunnable(newTask);
    executor.scheduleAtFixedRate(wrapTask, 0, 100, TimeUnit.MILLISECONDS);
    ThreadUtil.sleep(500);
    assertThat(newTask.counter.get()).isGreaterThan(2);
    System.out.println("-------actual run:" + task.counter.get());
    ThreadPoolUtil.gracefulShutdown(executor, 1000);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Test(org.junit.Test)

Aggregations

ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)154 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)31 Test (org.junit.Test)26 ExecutorService (java.util.concurrent.ExecutorService)24 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)22 ThreadFactory (java.util.concurrent.ThreadFactory)15 Test (org.testng.annotations.Test)15 ArrayList (java.util.ArrayList)12 List (java.util.List)10 HashMap (java.util.HashMap)9 ServerInstance (com.linkedin.pinot.common.response.ServerInstance)8 NettyClientMetrics (com.linkedin.pinot.transport.metrics.NettyClientMetrics)8 MetricsRegistry (com.yammer.metrics.core.MetricsRegistry)8 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)8 HashedWheelTimer (io.netty.util.HashedWheelTimer)8 File (java.io.File)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)7 IOException (java.io.IOException)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7