Search in sources :

Example 1 with NamedThreadFactory

use of com.alipay.sofa.rpc.common.struct.NamedThreadFactory in project sofa-rpc by sofastack.

the class Http2ClientMultipleMain method main.

public static void main(String[] args) {
    ApplicationConfig application = new ApplicationConfig().setAppName("test-client");
    ConsumerConfig<ProtoService> consumerConfig = new ConsumerConfig<ProtoService>().setApplication(application).setInterfaceId(ProtoService.class.getName()).setProtocol("h2c").setDirectUrl("h2c://127.0.0.1:12300").setSerialization("protobuf").setTimeout(3000);
    final ProtoService protoService = consumerConfig.refer();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    final int threads = 50;
    final AtomicLong cnt = new AtomicLong(0);
    final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), // 无队列
    new NamedThreadFactory("client-"));
    for (int i = 0; i < threads; i++) {
        service1.execute(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("12345678").build();
                        EchoResponse s = protoService.echoObj(request);
                        cnt.incrementAndGet();
                    } catch (Throwable e) {
                        LOGGER.error("", e);
                    }
                }
            }
        });
    }
    Thread thread = new Thread(new Runnable() {

        private long last = 0;

        @Override
        public void run() {
            while (true) {
                long count = cnt.get();
                long tps = count - last;
                LOGGER.error("last 1s invoke: {}, queue: {}", tps, service1.getQueue().size());
                last = count;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }, "Print-tps-THREAD");
    thread.start();
}
Also used : EchoResponse(com.alipay.sofa.rpc.protobuf.EchoResponse) NamedThreadFactory(com.alipay.sofa.rpc.common.struct.NamedThreadFactory) EchoRequest(com.alipay.sofa.rpc.protobuf.EchoRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProtoService(com.alipay.sofa.rpc.protobuf.ProtoService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 2 with NamedThreadFactory

use of com.alipay.sofa.rpc.common.struct.NamedThreadFactory in project sofa-rpc by sofastack.

the class RestClientMultipleMain method main.

public static void main(String[] args) {
    ApplicationConfig application = new ApplicationConfig().setAppName("test-client");
    ConsumerConfig<RestService> consumerConfig = new ConsumerConfig<RestService>().setApplication(application).setInterfaceId(RestService.class.getName()).setProtocol("rest").setBootstrap("rest").setDirectUrl("rest://127.0.0.1:8888").setTimeout(3000);
    final RestService helloService = consumerConfig.refer();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    final int threads = 50;
    final AtomicLong cnt = new AtomicLong(0);
    final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), // 无队列
    new NamedThreadFactory("client-"));
    for (int i = 0; i < threads; i++) {
        service1.execute(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        String s = helloService.get("1234567890");
                        cnt.incrementAndGet();
                    } catch (Exception e) {
                        LOGGER.error("", e);
                    }
                }
            }
        });
    }
    Thread thread = new Thread(new Runnable() {

        private long last = 0;

        @Override
        public void run() {
            while (true) {
                long count = cnt.get();
                long tps = count - last;
                LOGGER.error("last 1s invoke: {}, queue: {}", tps, service1.getQueue().size());
                last = count;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }, "Print-tps-THREAD");
    thread.start();
}
Also used : NamedThreadFactory(com.alipay.sofa.rpc.common.struct.NamedThreadFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RestService(com.alipay.sofa.rpc.rest.RestService)

Example 3 with NamedThreadFactory

use of com.alipay.sofa.rpc.common.struct.NamedThreadFactory in project sofa-rpc by sofastack.

the class ProtobufServiceClientMultipleMain method main.

public static void main(String[] args) {
    ApplicationConfig application = new ApplicationConfig().setAppName("test-client");
    ConsumerConfig<ProtoService> consumerConfig = new ConsumerConfig<ProtoService>().setInterfaceId(// 指定接口
    ProtoService.class.getName()).setProtocol(// 指定协议
    "bolt").setApplication(application).setDirectUrl(// 指定直连地址
    "bolt://127.0.0.1:12200").setSerialization(// 指定序列化协议,默认为hessian
    "protobuf").setConnectTimeout(10 * 1000);
    final ProtoService helloService = consumerConfig.refer();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    final int threads = 50;
    final AtomicLong cnt = new AtomicLong(0);
    final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), // 无队列
    new NamedThreadFactory("client-"));
    for (int i = 0; i < threads; i++) {
        service1.execute(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("12345678").build();
                        EchoResponse s = helloService.echoObj(request);
                        cnt.incrementAndGet();
                    } catch (Throwable e) {
                        LOGGER.error("", e);
                    }
                }
            }
        });
    }
    Thread thread = new Thread(new Runnable() {

        private long last = 0;

        @Override
        public void run() {
            while (true) {
                long count = cnt.get();
                long tps = count - last;
                LOGGER.error("last 1s invoke: {}, queue: {}", tps, service1.getQueue().size());
                last = count;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }, "Print-tps-THREAD");
    thread.start();
}
Also used : NamedThreadFactory(com.alipay.sofa.rpc.common.struct.NamedThreadFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 4 with NamedThreadFactory

use of com.alipay.sofa.rpc.common.struct.NamedThreadFactory in project sofa-rpc by sofastack.

the class AbstractHttpServer method initThreadPool.

protected ThreadPoolExecutor initThreadPool(ServerConfig serverConfig) {
    ThreadPoolExecutor threadPool = BusinessPool.initPool(serverConfig);
    threadPool.setThreadFactory(new NamedThreadFactory("SEV-" + serverConfig.getProtocol().toUpperCase() + "-BIZ-" + serverConfig.getPort(), serverConfig.isDaemon()));
    threadPool.setRejectedExecutionHandler(new SofaRejectedExecutionHandler());
    if (serverConfig.isPreStartCore()) {
        // 初始化核心线程池
        threadPool.prestartAllCoreThreads();
    }
    return threadPool;
}
Also used : SofaRejectedExecutionHandler(com.alipay.sofa.rpc.server.SofaRejectedExecutionHandler) NamedThreadFactory(com.alipay.sofa.rpc.common.struct.NamedThreadFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 5 with NamedThreadFactory

use of com.alipay.sofa.rpc.common.struct.NamedThreadFactory in project sofa-rpc by sofastack.

the class AllConnectConnectionHolder method addNode.

protected void addNode(List<ProviderInfo> providerInfoList) {
    // first update last all providers
    lastAddresses.addAll(providerInfoList);
    final String interfaceId = consumerConfig.getInterfaceId();
    int providerSize = providerInfoList.size();
    String appName = consumerConfig.getAppName();
    if (LOGGER.isInfoEnabled(appName)) {
        LOGGER.infoWithApp(appName, "Add provider of {}, size is : {}", interfaceId, providerSize);
    }
    if (providerSize > 0) {
        // 多线程建立连接
        // 最大10个
        int threads = Math.min(10, providerSize);
        final CountDownLatch latch = new CountDownLatch(providerSize);
        ThreadPoolExecutor initPool = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(providerInfoList.size()), new NamedThreadFactory("CLI-CONN-" + interfaceId, true));
        int connectTimeout = consumerConfig.getConnectTimeout();
        for (final ProviderInfo providerInfo : providerInfoList) {
            initClientRunnable(initPool, latch, providerInfo);
        }
        try {
            int totalTimeout = ((providerSize % threads == 0) ? (providerSize / threads) : ((providerSize / threads) + 1)) * connectTimeout + 500;
            // 一直等到子线程都结束
            latch.await(totalTimeout, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            LOGGER.errorWithApp(appName, LogCodes.getLog(LogCodes.ERROR_UPDATE_PROVIDERS, consumerConfig.getInterfaceId(), ""), e);
        } finally {
            // 关闭线程池
            initPool.shutdown();
        }
    }
}
Also used : NamedThreadFactory(com.alipay.sofa.rpc.common.struct.NamedThreadFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

NamedThreadFactory (com.alipay.sofa.rpc.common.struct.NamedThreadFactory)25 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)19 ThreadFactory (java.util.concurrent.ThreadFactory)7 Test (org.junit.Test)7 SynchronousQueue (java.util.concurrent.SynchronousQueue)6 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)4 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)4 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)3 SofaRejectedExecutionHandler (com.alipay.sofa.rpc.server.SofaRejectedExecutionHandler)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ClientTransport (com.alipay.sofa.rpc.transport.ClientTransport)2 EventLoopGroup (io.grpc.netty.shaded.io.netty.channel.EventLoopGroup)2 EpollEventLoopGroup (io.grpc.netty.shaded.io.netty.channel.epoll.EpollEventLoopGroup)2 NioEventLoopGroup (io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoopGroup)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 Map (java.util.Map)2