Search in sources :

Example 1 with NamedThreadFactory

use of org.apache.dubbo.common.utils.NamedThreadFactory in project dubbo by alibaba.

the class EagerThreadPoolExecutorTest method testEagerThreadPool.

/**
 * It print like this:
 * thread number in current pool:1,  task number in task queue:0 executor size: 1
 * thread number in current pool:2,  task number in task queue:0 executor size: 2
 * thread number in current pool:3,  task number in task queue:0 executor size: 3
 * thread number in current pool:4,  task number in task queue:0 executor size: 4
 * thread number in current pool:5,  task number in task queue:0 executor size: 5
 * thread number in current pool:6,  task number in task queue:0 executor size: 6
 * thread number in current pool:7,  task number in task queue:0 executor size: 7
 * thread number in current pool:8,  task number in task queue:0 executor size: 8
 * thread number in current pool:9,  task number in task queue:0 executor size: 9
 * thread number in current pool:10,  task number in task queue:0 executor size: 10
 * thread number in current pool:10,  task number in task queue:4 executor size: 10
 * thread number in current pool:10,  task number in task queue:3 executor size: 10
 * thread number in current pool:10,  task number in task queue:2 executor size: 10
 * thread number in current pool:10,  task number in task queue:1 executor size: 10
 * thread number in current pool:10,  task number in task queue:0 executor size: 10
 * <p>
 * We can see , when the core threads are in busy,
 * the thread pool create thread (but thread nums always less than max) instead of put task into queue.
 */
@Test
public void testEagerThreadPool() throws Exception {
    String name = "eager-tf";
    int queues = 5;
    int cores = 5;
    int threads = 10;
    // alive 1 second
    long alive = 1000;
    // init queue and executor
    TaskQueue<Runnable> taskQueue = new TaskQueue<Runnable>(queues);
    final EagerThreadPoolExecutor executor = new EagerThreadPoolExecutor(cores, threads, alive, TimeUnit.MILLISECONDS, taskQueue, new NamedThreadFactory(name, true), new AbortPolicyWithReport(name, URL));
    taskQueue.setExecutor(executor);
    for (int i = 0; i < 15; i++) {
        Thread.sleep(50);
        executor.execute(() -> {
            System.out.println("thread number in current pool:" + executor.getPoolSize() + ",  task number in task queue:" + executor.getQueue().size() + " executor size: " + executor.getPoolSize());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }
    Thread.sleep(5000);
    // cores theads are all alive.
    Assertions.assertEquals(executor.getPoolSize(), cores, "more than cores threads alive!");
}
Also used : NamedThreadFactory(org.apache.dubbo.common.utils.NamedThreadFactory) AbortPolicyWithReport(org.apache.dubbo.common.threadpool.support.AbortPolicyWithReport) Test(org.junit.jupiter.api.Test)

Example 2 with NamedThreadFactory

use of org.apache.dubbo.common.utils.NamedThreadFactory in project dubbo by alibaba.

the class HashedWheelTimerTest method createTaskTest.

@Test
public void createTaskTest() throws InterruptedException {
    HashedWheelTimer timer = new HashedWheelTimer(new NamedThreadFactory("dubbo-future-timeout", true), 10, TimeUnit.MILLISECONDS, 8, 8);
    Assertions.assertThrows(RuntimeException.class, () -> timer.newTimeout(null, 5, TimeUnit.SECONDS));
    Assertions.assertThrows(RuntimeException.class, () -> timer.newTimeout(new EmptyTask(), 5, null));
    Timeout timeout = timer.newTimeout(new ErrorTask(), 10, TimeUnit.MILLISECONDS);
    errorTaskCountDownLatch.await();
    Assertions.assertFalse(timeout.cancel());
    Assertions.assertFalse(timeout.isCancelled());
    Assertions.assertNotNull(timeout.toString());
    Assertions.assertEquals(timeout.timer(), timer);
    timeout = timer.newTimeout(new EmptyTask(), 1000, TimeUnit.SECONDS);
    timeout.cancel();
    Assertions.assertTrue(timeout.isCancelled());
    List<Timeout> timeouts = new LinkedList<>();
    for (; timer.pendingTimeouts() < 8; ) {
        // to trigger maxPendingTimeouts
        timeout = timer.newTimeout(new BlockTask(), -1, TimeUnit.MILLISECONDS);
        timeouts.add(timeout);
        Assertions.assertNotNull(timeout.toString());
    }
    Assertions.assertEquals(timer.pendingTimeouts(), 8);
    // this will throw an exception because of maxPendingTimeouts
    Assertions.assertThrows(RuntimeException.class, () -> timer.newTimeout(new BlockTask(), 1, TimeUnit.MILLISECONDS));
    timeout = timeouts.get(2);
    // wait until the task expired
    Thread.sleep(100);
    Assertions.assertTrue(timeout.isExpired());
    timer.stop();
}
Also used : NamedThreadFactory(org.apache.dubbo.common.utils.NamedThreadFactory) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 3 with NamedThreadFactory

use of org.apache.dubbo.common.utils.NamedThreadFactory in project dubbo by alibaba.

the class HashedWheelTimerTest method stopTaskTest.

@Test
public void stopTaskTest() throws InterruptedException {
    Timer timer = new HashedWheelTimer(new NamedThreadFactory("dubbo-future-timeout", true));
    timer.newTimeout(new TryStopTask(timer), 10, TimeUnit.MILLISECONDS);
    tryStopTaskCountDownLatch.await();
    for (int i = 0; i < 8; i++) {
        timer.newTimeout(new EmptyTask(), 0, TimeUnit.SECONDS);
    }
    // stop timer
    timer.stop();
    Assertions.assertTrue(timer.isStop());
    // this will throw an exception
    Assertions.assertThrows(RuntimeException.class, () -> timer.newTimeout(new EmptyTask(), 5, TimeUnit.SECONDS));
}
Also used : NamedThreadFactory(org.apache.dubbo.common.utils.NamedThreadFactory) Test(org.junit.jupiter.api.Test)

Example 4 with NamedThreadFactory

use of org.apache.dubbo.common.utils.NamedThreadFactory in project dubbo by alibaba.

the class MinaServer method doOpen.

@Override
protected void doOpen() throws Throwable {
    // set thread pool.
    acceptor = new SocketAcceptor(getUrl().getPositiveParameter(IO_THREADS_KEY, DEFAULT_IO_THREADS), Executors.newCachedThreadPool(new NamedThreadFactory("MinaServerWorker", true)));
    // config
    SocketAcceptorConfig cfg = acceptor.getDefaultConfig();
    cfg.setThreadModel(ThreadModel.MANUAL);
    // set codec.
    acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecAdapter(getCodec(), getUrl(), this)));
    acceptor.bind(getBindAddress(), new MinaHandler(getUrl(), this));
}
Also used : NamedThreadFactory(org.apache.dubbo.common.utils.NamedThreadFactory) SocketAcceptorConfig(org.apache.mina.transport.socket.nio.SocketAcceptorConfig) SocketAcceptor(org.apache.mina.transport.socket.nio.SocketAcceptor) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter)

Example 5 with NamedThreadFactory

use of org.apache.dubbo.common.utils.NamedThreadFactory in project dubbo by alibaba.

the class MinaClient method doOpen.

@Override
protected void doOpen() throws Throwable {
    connectorKey = getUrl().toFullString();
    SocketConnector c = CONNECTORS.get(connectorKey);
    if (c != null) {
        connector = c;
    } else {
        // set thread pool.
        connector = new SocketConnector(Constants.DEFAULT_IO_THREADS, Executors.newCachedThreadPool(new NamedThreadFactory("MinaClientWorker", true)));
        // config
        SocketConnectorConfig cfg = (SocketConnectorConfig) connector.getDefaultConfig();
        cfg.setThreadModel(ThreadModel.MANUAL);
        cfg.getSessionConfig().setTcpNoDelay(true);
        cfg.getSessionConfig().setKeepAlive(true);
        int timeout = getConnectTimeout();
        cfg.setConnectTimeout(timeout < 1000 ? 1 : timeout / 1000);
        // set codec.
        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecAdapter(getCodec(), getUrl(), this)));
        CONNECTORS.put(connectorKey, connector);
    }
}
Also used : NamedThreadFactory(org.apache.dubbo.common.utils.NamedThreadFactory) SocketConnectorConfig(org.apache.mina.transport.socket.nio.SocketConnectorConfig) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) SocketConnector(org.apache.mina.transport.socket.nio.SocketConnector)

Aggregations

NamedThreadFactory (org.apache.dubbo.common.utils.NamedThreadFactory)7 Test (org.junit.jupiter.api.Test)4 ProtocolCodecFilter (org.apache.mina.filter.codec.ProtocolCodecFilter)2 WeakReference (java.lang.ref.WeakReference)1 LinkedList (java.util.LinkedList)1 ExecutorService (java.util.concurrent.ExecutorService)1 AbortPolicyWithReport (org.apache.dubbo.common.threadpool.support.AbortPolicyWithReport)1 SocketAcceptor (org.apache.mina.transport.socket.nio.SocketAcceptor)1 SocketAcceptorConfig (org.apache.mina.transport.socket.nio.SocketAcceptorConfig)1 SocketConnector (org.apache.mina.transport.socket.nio.SocketConnector)1 SocketConnectorConfig (org.apache.mina.transport.socket.nio.SocketConnectorConfig)1 ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)1 ChannelFactory (org.jboss.netty.channel.ChannelFactory)1 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)1 ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)1 NioServerSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory)1