diff --git a/dorado/dorado-common/src/main/java/com/meituan/dorado/common/thread/ExecutorUtil.java b/dorado/dorado-common/src/main/java/com/meituan/dorado/common/thread/ExecutorUtil.java index 4b8271c..3802709 100644 --- a/dorado/dorado-common/src/main/java/com/meituan/dorado/common/thread/ExecutorUtil.java +++ b/dorado/dorado-common/src/main/java/com/meituan/dorado/common/thread/ExecutorUtil.java @@ -16,11 +16,12 @@ package com.meituan.dorado.common.thread; import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; public class ExecutorUtil { + private static final long DEFAULT_THREAD_KEEP_ALIVE_TIME = 30L; + public static void shutdownExecutors(List executors, int timeoutMillis) { if (executors == null) { return; @@ -43,4 +44,30 @@ public static void shutdownExecutor(ExecutorService executor, int timeoutMillis) executor.shutdownNow(); } } + + public static ThreadPoolExecutor getThreadPool(int corePoolSize, int maximumPoolSize, int workQueueSize, + BlockingQueue workQueue, DefaultThreadFactory threadFactory) { + if (workQueue != null) { + return new ThreadPoolExecutor(corePoolSize, + maximumPoolSize, + DEFAULT_THREAD_KEEP_ALIVE_TIME, + TimeUnit.SECONDS, + workQueue, + threadFactory); + } else if (workQueueSize > 0) { + return new ThreadPoolExecutor(corePoolSize, + maximumPoolSize, + DEFAULT_THREAD_KEEP_ALIVE_TIME, + TimeUnit.SECONDS, + new LinkedBlockingQueue(workQueueSize), + threadFactory); + } else { + return new ThreadPoolExecutor(corePoolSize, + maximumPoolSize, + DEFAULT_THREAD_KEEP_ALIVE_TIME, + TimeUnit.SECONDS, + new SynchronousQueue(), + threadFactory); + } + } } diff --git a/dorado/dorado-core-default/src/main/java/com/meituan/dorado/transport/meta/DefaultResponse.java b/dorado/dorado-core-default/src/main/java/com/meituan/dorado/transport/meta/DefaultResponse.java index 2462be3..138a91e 100644 --- a/dorado/dorado-core-default/src/main/java/com/meituan/dorado/transport/meta/DefaultResponse.java +++ b/dorado/dorado-core-default/src/main/java/com/meituan/dorado/transport/meta/DefaultResponse.java @@ -28,6 +28,8 @@ public class DefaultResponse implements Response { private Long seq; + private String serviceName; + private Class serviceInterface; private byte statusCode; @@ -78,6 +80,7 @@ private void initField(DefaultRequest request) { throw new TimeoutException("Request has removed, cause Timeout happened earlier."); } this.seq = request.getSeq(); + this.serviceName = request.getServiceName(); this.serviceInterface = request.getServiceInterface(); this.messageType = request.getMessageType(); this.doChecksum = request.getDoChecksum(); @@ -301,7 +304,6 @@ public void setThriftMsgInfo(ThriftMessageInfo thriftMsgInfo) { @Override public String toString() { - String serviceInterfaceName = serviceInterface == null ? "Unknown" : serviceInterface.getName(); - return "Response(" + serviceInterfaceName + ")"; + return "Response(" + serviceName + ")"; } } diff --git a/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ProviderConfig.java b/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ProviderConfig.java index ece9cc3..fb54c0d 100644 --- a/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ProviderConfig.java +++ b/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ProviderConfig.java @@ -21,13 +21,18 @@ import com.meituan.dorado.registry.RegistryFactory; import com.meituan.dorado.rpc.handler.filter.Filter; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.BlockingQueue; public class ProviderConfig { + private static final Logger LOGGER = LoggerFactory.getLogger(ProviderConfig.class); + protected String appkey; // mns, zookeeper://address?k=v&k=v; 没配置则从SPI中获取 private String registry; @@ -40,6 +45,11 @@ public class ProviderConfig { private int port = Constants.DEFAULT_SERVER_PORT; // 网络IO线程数 private int ioWorkerThreadCount = Constants.DEFAULT_IO_WORKER_THREAD_COUNT; + // 业务线程池配置1: 参数 + private int bizCoreWorkerThreadCount = Constants.DEFAULT_BIZ_CORE_WORKER_THREAD_COUNT; + private int bizMaxWorkerThreadCount = Constants.DEFAULT_BIZ_MAX_WORKER_THREAD_COUNT; + private int bizWorkerQueueSize = Constants.DEFAULT_BIZ_WORKER_QUEUES; + private BlockingQueue threadPoolQueue; // 单端口单服务 private ServiceConfig serviceConfig; @@ -61,6 +71,7 @@ public class ProviderConfig { private volatile boolean destroyed; public void init() { + check(); if (StringUtils.isBlank(registry)) { RegistryFactory registryFactory = ExtensionLoader.getExtension(RegistryFactory.class); registry = registryFactory.getName(); @@ -76,6 +87,14 @@ public void init() { ServicePublisher.publishService(this); } + protected void check() { + if (bizCoreWorkerThreadCount > bizMaxWorkerThreadCount) { + throw new IllegalArgumentException("bizCoreWorkerThreadCount must less than bizMaxWorkerThreadCount"); + } else if (bizCoreWorkerThreadCount == bizMaxWorkerThreadCount) { + LOGGER.warn("bizCoreWorkerThreadCount equals to bizMaxWorkerThreadCount, it may cause many idle thread kept in pool."); + } + } + public synchronized void destroy() { if (destroyed) { return; @@ -199,6 +218,38 @@ public void setEnv(String env) { this.env = env; } + public int getBizCoreWorkerThreadCount() { + return bizCoreWorkerThreadCount; + } + + public void setBizCoreWorkerThreadCount(int bizCoreWorkerThreadCount) { + this.bizCoreWorkerThreadCount = bizCoreWorkerThreadCount; + } + + public int getBizMaxWorkerThreadCount() { + return bizMaxWorkerThreadCount; + } + + public void setBizMaxWorkerThreadCount(int bizMaxWorkerThreadCount) { + this.bizMaxWorkerThreadCount = bizMaxWorkerThreadCount; + } + + public int getBizWorkerQueueSize() { + return bizWorkerQueueSize; + } + + public void setBizWorkerQueueSize(int bizWorkerQueueSize) { + this.bizWorkerQueueSize = bizWorkerQueueSize; + } + + public BlockingQueue getThreadPoolQueue() { + return threadPoolQueue; + } + + public void setThreadPoolQueue(BlockingQueue threadPoolQueue) { + this.threadPoolQueue = threadPoolQueue; + } + class ShutDownHook extends Thread { private ProviderConfig config; diff --git a/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ServiceConfig.java b/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ServiceConfig.java index 452f15c..6303f13 100644 --- a/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ServiceConfig.java +++ b/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ServiceConfig.java @@ -29,10 +29,6 @@ public class ServiceConfig extends AbstractConfig { // 接口实现 必配项 private T serviceImpl; - // 业务线程池配置1: 参数 - private int bizCoreWorkerThreadCount = Constants.DEFAULT_BIZ_CORE_WORKER_THREAD_COUNT; - private int bizMaxWorkerThreadCount = Constants.DEFAULT_BIZ_MAX_WORKER_THREAD_COUNT; - private int bizWorkerQueueSize = Constants.DEFAULT_BIZ_WORKER_QUEUES; // 业务线程池配置2: 线程池对象 private ExecutorService bizWorkerExecutor; // 业务线程池配置3: 方法粒度线程池对象 @@ -52,11 +48,6 @@ protected void check() { } serviceInterface = interfaces[0]; } - if (bizCoreWorkerThreadCount > bizMaxWorkerThreadCount) { - throw new IllegalArgumentException("bizCoreWorkerThreadCount must less than bizMaxWorkerThreadCount"); - } else if (bizCoreWorkerThreadCount == bizMaxWorkerThreadCount) { - LOGGER.warn("bizCoreWorkerThreadCount equals to bizMaxWorkerThreadCount, it may cause many idle thread keep in pool."); - } serviceName = serviceInterface.getName(); if (serviceName.indexOf(Constants.LINK_SUB_CLASS_SYMBOL) > 0) { @@ -86,30 +77,6 @@ public void setServiceImpl(T serviceImpl) { this.serviceImpl = serviceImpl; } - public int getBizCoreWorkerThreadCount() { - return bizCoreWorkerThreadCount; - } - - public void setBizCoreWorkerThreadCount(int bizCoreWorkerThreadCount) { - this.bizCoreWorkerThreadCount = bizCoreWorkerThreadCount; - } - - public int getBizMaxWorkerThreadCount() { - return bizMaxWorkerThreadCount; - } - - public void setBizMaxWorkerThreadCount(int bizMaxWorkerThreadCount) { - this.bizMaxWorkerThreadCount = bizMaxWorkerThreadCount; - } - - public int getBizWorkerQueueSize() { - return bizWorkerQueueSize; - } - - public void setBizWorkerQueueSize(int bizWorkerQueueSize) { - this.bizWorkerQueueSize = bizWorkerQueueSize; - } - public ExecutorService getBizWorkerExecutor() { return bizWorkerExecutor; } diff --git a/dorado/dorado-core/src/main/java/com/meituan/dorado/rpc/AsyncContext.java b/dorado/dorado-core/src/main/java/com/meituan/dorado/rpc/AsyncContext.java index 6458bf7..d42eee4 100644 --- a/dorado/dorado-core/src/main/java/com/meituan/dorado/rpc/AsyncContext.java +++ b/dorado/dorado-core/src/main/java/com/meituan/dorado/rpc/AsyncContext.java @@ -46,11 +46,7 @@ public ResponseFuture asyncCall(Callable callable) { try { try { setAttachment(Constants.ASYNC, Boolean.TRUE); - final T o = callable.call(); - if (o != null) { - logger.warn("Do async call but actual action is sync"); - return new MockFuture(o); - } + callable.call(); } catch (Exception e) { throw new RpcException(e); } finally { diff --git a/dorado/dorado-core/src/main/java/com/meituan/dorado/rpc/DefaultFuture.java b/dorado/dorado-core/src/main/java/com/meituan/dorado/rpc/DefaultFuture.java index 17bf727..52875a0 100644 --- a/dorado/dorado-core/src/main/java/com/meituan/dorado/rpc/DefaultFuture.java +++ b/dorado/dorado-core/src/main/java/com/meituan/dorado/rpc/DefaultFuture.java @@ -205,7 +205,7 @@ public void run() { } }); } - } else if (value != null) { + } else { if (executor == null) { callback.onComplete(value); } else { diff --git a/dorado/dorado-core/src/main/java/com/meituan/dorado/rpc/handler/invoker/AbstractInvokerInvokeHandler.java b/dorado/dorado-core/src/main/java/com/meituan/dorado/rpc/handler/invoker/AbstractInvokerInvokeHandler.java index 6be9375..0f46380 100644 --- a/dorado/dorado-core/src/main/java/com/meituan/dorado/rpc/handler/invoker/AbstractInvokerInvokeHandler.java +++ b/dorado/dorado-core/src/main/java/com/meituan/dorado/rpc/handler/invoker/AbstractInvokerInvokeHandler.java @@ -30,6 +30,7 @@ import com.meituan.dorado.trace.meta.TraceTimeline; import com.meituan.dorado.transport.meta.Request; import com.meituan.dorado.transport.meta.Response; +import com.meituan.dorado.util.MethodUtil; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; @@ -60,7 +61,9 @@ private Response handleAsync(Request request, ResponseFuture future) { ServiceInvocationRepository.addTimeoutTask(request, future); AsyncContext.getContext().setFuture(future); Response response = buildResponse(request); - response.setResult(new RpcResult()); + RpcResult result = new RpcResult(); + result.setReturnVal(MethodUtil.getDefaultResult(request.getData().getMethod().getReturnType())); + response.setResult(result); return response; } diff --git a/dorado/dorado-core/src/main/java/com/meituan/dorado/transport/support/ProviderChannelHandler.java b/dorado/dorado-core/src/main/java/com/meituan/dorado/transport/support/ProviderChannelHandler.java index df77669..4ef4e25 100644 --- a/dorado/dorado-core/src/main/java/com/meituan/dorado/transport/support/ProviderChannelHandler.java +++ b/dorado/dorado-core/src/main/java/com/meituan/dorado/transport/support/ProviderChannelHandler.java @@ -17,7 +17,6 @@ import com.meituan.dorado.common.Constants; import com.meituan.dorado.common.RpcRole; -import com.meituan.dorado.common.exception.ProtocolException; import com.meituan.dorado.common.exception.RpcException; import com.meituan.dorado.common.exception.ServiceException; import com.meituan.dorado.common.extension.ExtensionLoader; @@ -45,7 +44,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.ThreadPoolExecutor; /** * 每个端口服务的channel handler @@ -64,7 +65,7 @@ public class ProviderChannelHandler implements ChannelHandler { private final Map> serviceInterfaceMap = new HashMap<>(); private final Map serviceImplMap = new HashMap<>(); - private final ExecutorService defaultExecutor; + private ThreadPoolExecutor defaultExecutor; private final Map serviceExecutorMap = new HashMap<>(); private final Map> methodExecutorMap = new HashMap<>(); @@ -75,18 +76,22 @@ public ProviderChannelHandler(ProviderConfig providerConfig) { serviceImplMap.put(serviceConfig.getServiceName(), serviceConfig.getServiceImpl()); initThreadPoolExecutor(serviceConfig); } - - defaultExecutor = new ThreadPoolExecutor(Constants.DEFAULT_BIZ_CORE_WORKER_THREAD_COUNT, - Constants.DEFAULT_BIZ_MAX_WORKER_THREAD_COUNT, - Constants.IDLE_THREAD_KEEP_ALIVE_SECONDS, - TimeUnit.SECONDS, - new LinkedBlockingQueue(), - new DefaultThreadFactory(genServerBizThreadPoolName(providerConfig))); + initDefaultThreadPool(providerConfig); FilterHandler actualHandler = buildActualInvokeHandler(); filterHandler = InvokeChainBuilder.initInvokeChain(actualHandler, providerConfig.getFilters(), RpcRole.PROVIDER); } + private void initDefaultThreadPool(ProviderConfig providerConfig) { + DefaultThreadFactory threadFactory = new DefaultThreadFactory(genServerBizThreadPoolName(providerConfig)); + defaultExecutor = ExecutorUtil.getThreadPool(providerConfig.getBizCoreWorkerThreadCount(), + providerConfig.getBizMaxWorkerThreadCount(), + providerConfig.getBizWorkerQueueSize(), + providerConfig.getThreadPoolQueue(), + threadFactory); + defaultExecutor.prestartAllCoreThreads(); + } + private String genServerBizThreadPoolName(ProviderConfig providerConfig) { String bizThreadPoolName = "DoradoServerBizWorker-"; if (providerConfig.getServiceConfigList().size() == 1) { diff --git a/dorado/dorado-core/src/main/java/com/meituan/dorado/util/MethodUtil.java b/dorado/dorado-core/src/main/java/com/meituan/dorado/util/MethodUtil.java index 0128247..89ad919 100644 --- a/dorado/dorado-core/src/main/java/com/meituan/dorado/util/MethodUtil.java +++ b/dorado/dorado-core/src/main/java/com/meituan/dorado/util/MethodUtil.java @@ -48,4 +48,31 @@ private static String parameterTypesToString(Class[] parameterTypes) { buf.append(")"); return buf.toString(); } + + public static Object getDefaultResult(Class returnType) { + if (returnType.isPrimitive()) { + String returnTypeName = returnType.getSimpleName(); + if ("boolean".equals(returnTypeName)) { + return false; + } else if ("char".equals(returnTypeName)) { + return '0'; + } else if ("byte".equals(returnTypeName)) { + return (byte) 0; + } else if ("short".equals(returnTypeName)) { + return (short) 0; + } else if ("int".equals(returnTypeName)) { + return 0; + } else if ("long".equals(returnTypeName)) { + return (long) 0; + } else if ("float".equals(returnTypeName)) { + return (float) 0; + } else if ("double".equals(returnTypeName)) { + return (double) 0; + } else { + return null; + } + } else { + return null; + } + } } diff --git a/dorado/dorado-core/src/test/java/com/meituan/dorado/rpc/AsyncContextTest.java b/dorado/dorado-core/src/test/java/com/meituan/dorado/rpc/AsyncContextTest.java index 0183815..062f6ca 100644 --- a/dorado/dorado-core/src/test/java/com/meituan/dorado/rpc/AsyncContextTest.java +++ b/dorado/dorado-core/src/test/java/com/meituan/dorado/rpc/AsyncContextTest.java @@ -39,8 +39,9 @@ public Integer call() throws Exception { Assert.assertNull(context.getAttachment(Constants.ASYNC)); try { - Assert.assertTrue(result.get() == 10); + Assert.assertTrue(result == null); } catch (Exception e) { + e.printStackTrace(); Assert.fail(); } diff --git a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/apiway/ThriftConsumer.java b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/apiway/ThriftConsumer.java index 2c05e49..fe2eafb 100644 --- a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/apiway/ThriftConsumer.java +++ b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/apiway/ThriftConsumer.java @@ -15,10 +15,8 @@ */ package com.meituan.dorado.demo.thrift.apiway; -import com.meituan.dorado.bootstrap.ServiceBootstrap; import com.meituan.dorado.config.service.ReferenceConfig; import com.meituan.dorado.test.thrift.api.HelloService; -import org.apache.thrift.TException; import org.junit.Assert; public class ThriftConsumer { diff --git a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/apiway/ThriftProvider.java b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/apiway/ThriftProvider.java index 2a1bf5c..acbf30a 100644 --- a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/apiway/ThriftProvider.java +++ b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/apiway/ThriftProvider.java @@ -15,7 +15,6 @@ */ package com.meituan.dorado.demo.thrift.apiway; -import com.meituan.dorado.bootstrap.ServiceBootstrap; import com.meituan.dorado.config.service.ProviderConfig; import com.meituan.dorado.config.service.ServiceConfig; import com.meituan.dorado.test.thrift.api.HelloService; diff --git a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/async/AsyncConsumer.java b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/async/AsyncConsumer.java index 4f80ba8..1eff6f6 100644 --- a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/async/AsyncConsumer.java +++ b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/async/AsyncConsumer.java @@ -15,7 +15,6 @@ */ package com.meituan.dorado.demo.thrift.async; -import com.meituan.dorado.bootstrap.ServiceBootstrap; import com.meituan.dorado.rpc.AsyncContext; import com.meituan.dorado.rpc.ResponseCallback; import com.meituan.dorado.rpc.ResponseFuture; diff --git a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/multiport/ThriftConsumerMutilApi.java b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/multiport/ThriftConsumerMultiApi.java similarity index 86% rename from dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/multiport/ThriftConsumerMutilApi.java rename to dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/multiport/ThriftConsumerMultiApi.java index ad8dc99..74b2ae4 100644 --- a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/multiport/ThriftConsumerMutilApi.java +++ b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/multiport/ThriftConsumerMultiApi.java @@ -15,18 +15,18 @@ */ package com.meituan.dorado.demo.thrift.multiport; -import com.meituan.dorado.bootstrap.ServiceBootstrap; import com.meituan.dorado.test.thrift.api.Echo; import com.meituan.dorado.test.thrift.api.HelloService; import org.springframework.context.support.ClassPathXmlApplicationContext; -public class ThriftConsumerMutilApi { +public class ThriftConsumerMultiApi { + public static void main(String[] args) { try { ClassPathXmlApplicationContext beanFactory = new ClassPathXmlApplicationContext("thrift/multiport/thrift-consumer-multiapi.xml"); - HelloService.Iface userservice = (HelloService.Iface) beanFactory.getBean("helloService"); - System.out.println(userservice.sayHello("Emma")); + HelloService.Iface userService = (HelloService.Iface) beanFactory.getBean("helloService"); + System.out.println(userService.sayHello("Emma")); Echo.Iface echo = (Echo.Iface) beanFactory.getBean("echoService"); System.out.println(echo.echo("Hello world")); diff --git a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/multiport/ThriftProviderMutilPort.java b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/multiport/ThriftProviderMultiPort.java similarity index 96% rename from dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/multiport/ThriftProviderMutilPort.java rename to dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/multiport/ThriftProviderMultiPort.java index 33ab1d4..2bbee02 100644 --- a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/multiport/ThriftProviderMutilPort.java +++ b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/multiport/ThriftProviderMultiPort.java @@ -19,7 +19,8 @@ import com.meituan.dorado.demo.ConsoleCommandProcessor; import org.springframework.context.support.ClassPathXmlApplicationContext; -public class ThriftProviderMutilPort { +public class ThriftProviderMultiPort { + public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext beanFactory = new ClassPathXmlApplicationContext("thrift/multiport/thrift-provider-multiport.xml"); diff --git a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/simple/ThriftConsumer.java b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/simple/ThriftConsumer.java index 9ca5395..16d1c0e 100644 --- a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/simple/ThriftConsumer.java +++ b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/simple/ThriftConsumer.java @@ -15,7 +15,6 @@ */ package com.meituan.dorado.demo.thrift.simple; -import com.meituan.dorado.bootstrap.ServiceBootstrap; import com.meituan.dorado.test.thrift.api.HelloService; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -25,8 +24,8 @@ public static void main(String[] args) { try { ClassPathXmlApplicationContext beanFactory = new ClassPathXmlApplicationContext("thrift/simple/thrift-consumer.xml"); - HelloService.Iface userservice = (HelloService.Iface) beanFactory.getBean("helloService"); - System.out.println(userservice.sayHello("OCTO")); + HelloService.Iface userService = (HelloService.Iface) beanFactory.getBean("helloService"); + System.out.println(userService.sayHello("OCTO")); beanFactory.destroy(); System.exit(0); diff --git a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/zkmock/ThriftConsumer.java b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/zkmock/ThriftConsumer.java index 05d9718..4e3c8da 100644 --- a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/zkmock/ThriftConsumer.java +++ b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/zkmock/ThriftConsumer.java @@ -15,7 +15,6 @@ */ package com.meituan.dorado.demo.thrift.zkmock; -import com.meituan.dorado.bootstrap.ServiceBootstrap; import com.meituan.dorado.test.thrift.api.HelloService; import org.springframework.context.support.ClassPathXmlApplicationContext; diff --git a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/zkregistry/ThriftConsumer.java b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/zkregistry/ThriftConsumer.java index dc9eca0..a5381c4 100644 --- a/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/zkregistry/ThriftConsumer.java +++ b/dorado/dorado-demo/src/test/java/com/meituan/dorado/demo/thrift/zkregistry/ThriftConsumer.java @@ -15,7 +15,6 @@ */ package com.meituan.dorado.demo.thrift.zkregistry; -import com.meituan.dorado.bootstrap.ServiceBootstrap; import com.meituan.dorado.test.thrift.api.HelloService; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -28,8 +27,8 @@ public static void main(String[] args) { try { ClassPathXmlApplicationContext beanFactory = new ClassPathXmlApplicationContext("thrift/zkregistry/thrift-consumer.xml"); - HelloService.Iface userservice = (HelloService.Iface) beanFactory.getBean("helloService"); - System.out.println(userservice.sayHello("Emma")); + HelloService.Iface userService = (HelloService.Iface) beanFactory.getBean("helloService"); + System.out.println(userService.sayHello("Emma")); beanFactory.destroy(); System.exit(0); diff --git a/dorado/dorado-registry/dorado-registry-zookeeper/src/test/java/com/meituan/dorado/registry/zookeeper/RegistryTest.java b/dorado/dorado-registry/dorado-registry-zookeeper/src/test/java/com/meituan/dorado/registry/zookeeper/RegistryTest.java index 8b36b53..1dc6689 100644 --- a/dorado/dorado-registry/dorado-registry-zookeeper/src/test/java/com/meituan/dorado/registry/zookeeper/RegistryTest.java +++ b/dorado/dorado-registry/dorado-registry-zookeeper/src/test/java/com/meituan/dorado/registry/zookeeper/RegistryTest.java @@ -47,7 +47,6 @@ public static void initZkServer() throws Exception { public class MockNotifyListener implements ProviderListener { private ConcurrentMap providers = new ConcurrentHashMap<>(); - ; @Override public void notify(List list) { diff --git a/dorado/dorado-registry/dorado-registry-zookeeper/src/test/java/com/meituan/dorado/registry/zookeeper/util/ZooKeeperNodeInfoTest.java b/dorado/dorado-registry/dorado-registry-zookeeper/src/test/java/com/meituan/dorado/registry/zookeeper/util/ZooKeeperNodeInfoTest.java index c5454bd..06b85e6 100644 --- a/dorado/dorado-registry/dorado-registry-zookeeper/src/test/java/com/meituan/dorado/registry/zookeeper/util/ZooKeeperNodeInfoTest.java +++ b/dorado/dorado-registry/dorado-registry-zookeeper/src/test/java/com/meituan/dorado/registry/zookeeper/util/ZooKeeperNodeInfoTest.java @@ -39,7 +39,6 @@ public void testNodeInfo() { } catch (IOException e) { Assert.fail(); } - } public SubscribeInfo genDiffServiceNameSubscribeInfo() { diff --git a/dorado/dorado-test/dorado-test-integration/src/test/java/com/meituan/dorado/test/thrift/async/AsyncTest.java b/dorado/dorado-test/dorado-test-integration/src/test/java/com/meituan/dorado/test/thrift/async/AsyncTest.java new file mode 100644 index 0000000..4b0ab10 --- /dev/null +++ b/dorado/dorado-test/dorado-test-integration/src/test/java/com/meituan/dorado/test/thrift/async/AsyncTest.java @@ -0,0 +1,330 @@ +/* + * Copyright 2018 Meituan Dianping. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.meituan.dorado.test.thrift.async; + +import com.meituan.dorado.rpc.AsyncContext; +import com.meituan.dorado.rpc.ResponseCallback; +import com.meituan.dorado.rpc.ResponseFuture; +import com.sankuai.mtthrift.testSuite.idlTest.Tweet; +import com.sankuai.mtthrift.testSuite.idlTest.TweetSearchResult; +import com.sankuai.mtthrift.testSuite.idlTest.Twitter; +import com.sankuai.mtthrift.testSuite.idlTest.TwitterUnavailable; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +public class AsyncTest { + + private static ClassPathXmlApplicationContext clientBeanFactory; + private static ClassPathXmlApplicationContext serverBeanFactory; + + private static Twitter.Iface nettyClient; + + @BeforeClass + public static void start() { + serverBeanFactory = new ClassPathXmlApplicationContext("thrift/twitterAsync/thrift-provider.xml"); + clientBeanFactory = new ClassPathXmlApplicationContext("thrift/twitterAsync/thrift-consumer.xml"); + + nettyClient = (Twitter.Iface) clientBeanFactory.getBean("nettyClientProxy"); + } + + @AfterClass + public static void stop() { + clientBeanFactory.destroy(); + serverBeanFactory.destroy(); + } + + @Test + public void testFuture() { + try { + ResponseFuture future = AsyncContext.getContext().asyncCall(new Callable() { + @Override + public String call() throws Exception { + return nettyClient.testString("Emma async"); + } + }); + Assert.assertEquals("Emma async", future.get()); + } catch (Exception e) { + Assert.fail(e.getMessage()); + } + + try { + final ByteBuffer b = ByteBuffer.wrap("test".getBytes()); + ResponseFuture future = AsyncContext.getContext().asyncCall(new Callable() { + @Override + public ByteBuffer call() throws Exception { + return nettyClient.testBinary(b); + } + }); + Assert.assertEquals(b, future.get()); + } catch (Exception e) { + Assert.fail(e.getMessage()); + } + + try { + ResponseFuture future = AsyncContext.getContext().asyncCall(new Callable() { + @Override + public Short call() throws Exception { + return nettyClient.testI16((short) 1); + } + }); + Assert.assertTrue((short)1 == future.get()); + } catch (Exception e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + + try { + ResponseFuture future = AsyncContext.getContext().asyncCall(new Callable() { + @Override + public Double call() throws Exception { + return nettyClient.testDouble(2.0); + } + }); + double result = future.get(); + Assert.assertTrue(Double.compare(2.0, result) == 0); + } catch (Exception e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + + try { + ResponseFuture future = AsyncContext.getContext().asyncCall(new Callable() { + @Override + public Boolean call() throws Exception { + return nettyClient.testBool(false); + } + }); + Assert.assertEquals(false, future.get()); + } catch (Exception e) { + Assert.fail(e.getMessage()); + } + + try { + ResponseFuture future = AsyncContext.getContext().asyncCall(new Callable() { + @Override + public Long call() throws Exception { + return nettyClient.testI64(2L); + } + }); + Assert.assertTrue(2L == future.get()); + } catch (Exception e) { + Assert.fail(e.getMessage()); + } + + try { + ResponseFuture future = AsyncContext.getContext().asyncCall(new Callable() { + @Override + public Integer call() throws Exception { + return nettyClient.testI32(2); + } + }); + Assert.assertTrue(2 == future.get()); + } catch (Exception e) { + Assert.fail(e.getMessage()); + } + + try { + final byte b = 10; + ResponseFuture future = AsyncContext.getContext().asyncCall(new Callable() { + @Override + public Byte call() throws Exception { + return nettyClient.testByte(b); + } + }); + Assert.assertTrue(b == future.get()); + } catch (Exception e) { + Assert.fail(e.getMessage()); + } + } + + @Test + public void testCallback() throws InterruptedException { + //3. 异步回调 + ResponseFuture future2 = AsyncContext.getContext().asyncCall(new Callable() { + @Override + public String call() throws Exception { + return nettyClient.testString("Emma async callback"); + } + }); + final CountDownLatch countDownLatch = new CountDownLatch(1); + future2.setCallback(new ResponseCallback() { + @Override + public void onComplete(String result) { + if ("Emma async callback".equals(result)) { + countDownLatch.countDown(); + } + } + + @Override + public void onError(Throwable e) { + e.printStackTrace(); + } + }); + boolean result = countDownLatch.await(5000, TimeUnit.MILLISECONDS); + Assert.assertTrue(result); + + + ResponseFuture future1 = AsyncContext.getContext().asyncCall(new Callable() { + @Override + public Integer call() throws Exception { + return nettyClient.testI32(2); + } + }); + final CountDownLatch countDownLatch1 = new CountDownLatch(1); + future1.setCallback(new ResponseCallback() { + @Override + public void onComplete(Integer result) { + if (2 == result) { + countDownLatch1.countDown(); + } + } + + @Override + public void onError(Throwable e) { + e.printStackTrace(); + } + }); + result = countDownLatch1.await(5000, TimeUnit.MILLISECONDS); + Assert.assertTrue(result); + } + + @Test + public void callbackAsyncOtherTest() throws InterruptedException { + final int timeout = 5000; + final CountDownLatch countDownLatch = new CountDownLatch(1); + try { + ResponseFuture future2 = AsyncContext.getContext().asyncCall(new Callable() { + @Override + public Void call() throws Exception { + nettyClient.testVoid(); + return null; + } + }); + future2.setCallback(new ResponseCallback() { + @Override + public void onComplete(Void result) { + countDownLatch.countDown(); + } + @Override + public void onError(Throwable e) { + e.printStackTrace(); + } + }); + } catch (Exception e) { + Assert.fail(e.getMessage()); + } + boolean result = countDownLatch.await(timeout, TimeUnit.MILLISECONDS); + Assert.assertEquals(true, result); + + + final CountDownLatch countDownLatch1 = new CountDownLatch(1); + try { + ResponseFuture future2 = AsyncContext.getContext().asyncCall(new Callable() { + @Override + public String call() throws Exception { + return nettyClient.testReturnNull(); + } + }); + future2.setCallback(new ResponseCallback() { + @Override + public void onComplete(String result) { + if (result == null) { + countDownLatch1.countDown(); + } + } + @Override + public void onError(Throwable e) { + e.printStackTrace(); + } + }); + } catch (Exception e) { + Assert.fail(e.getMessage()); + } + result = countDownLatch1.await(timeout, TimeUnit.MILLISECONDS); + Assert.assertEquals(true, result); + + + final CountDownLatch countDownLatch2 = new CountDownLatch(1); + List tweets = new ArrayList(); + tweets.add(new Tweet(1, "1", "1")); + tweets.add(new Tweet(2, "2", "2")); + tweets.add(new Tweet(3, "3", "3")); + final TweetSearchResult tweetSearchResult = new TweetSearchResult(tweets); + try { + ResponseFuture future2 = AsyncContext.getContext().asyncCall(new Callable() { + @Override + public TweetSearchResult call() throws Exception { + return nettyClient.testStruct("test"); + } + }); + future2.setCallback(new ResponseCallback() { + @Override + public void onComplete(TweetSearchResult result) { + if (tweetSearchResult.equals(result)) { + countDownLatch2.countDown(); + } + } + @Override + public void onError(Throwable e) { + e.printStackTrace(); + } + }); + } catch (Exception e) { + Assert.fail(e.getMessage()); + } + result = countDownLatch2.await(timeout, TimeUnit.MILLISECONDS); + Assert.assertEquals(true, result); + + + final CountDownLatch countDownLatch3 = new CountDownLatch(1); + try { + ResponseFuture future2 = AsyncContext.getContext().asyncCall(new Callable() { + @Override + public String call() throws Exception { + return nettyClient.testException(new Tweet(1, "1", "1")); + } + }); + future2.setCallback(new ResponseCallback() { + @Override + public void onComplete(String result) { + + } + @Override + public void onError(Throwable e) { + e.printStackTrace(); + if (e.getCause() instanceof TwitterUnavailable) { + countDownLatch3.countDown(); + } + } + }); + } catch (Exception e) { + Assert.fail(e.getMessage()); + } + result = countDownLatch3.await(timeout, TimeUnit.MILLISECONDS); + Assert.assertEquals(true, result); + } + +} \ No newline at end of file diff --git a/dorado/dorado-test/dorado-test-integration/src/test/java/com/meituan/dorado/test/thrift/async/ClientAsyncTest.java b/dorado/dorado-test/dorado-test-integration/src/test/java/com/meituan/dorado/test/thrift/async/ClientAsyncTest.java index f28541a..dd545df 100644 --- a/dorado/dorado-test/dorado-test-integration/src/test/java/com/meituan/dorado/test/thrift/async/ClientAsyncTest.java +++ b/dorado/dorado-test/dorado-test-integration/src/test/java/com/meituan/dorado/test/thrift/async/ClientAsyncTest.java @@ -16,7 +16,6 @@ package com.meituan.dorado.test.thrift.async; -import com.meituan.dorado.bootstrap.ServiceBootstrap; import com.meituan.dorado.rpc.AsyncContext; import com.meituan.dorado.rpc.ResponseCallback; import com.meituan.dorado.rpc.ResponseFuture; @@ -54,7 +53,6 @@ public static void stop() { @Test public void asyncTest() { - try { //1. 同步调用 Assert.assertEquals("Hello Emma", client1.sayHello("Emma")); diff --git a/dorado/dorado-test/dorado-test-integration/src/test/java/com/meituan/dorado/test/thrift/exception/client/EmptyChannelTest.java b/dorado/dorado-test/dorado-test-integration/src/test/java/com/meituan/dorado/test/thrift/exception/client/EmptyChannelTest.java deleted file mode 100644 index f90a97a..0000000 --- a/dorado/dorado-test/dorado-test-integration/src/test/java/com/meituan/dorado/test/thrift/exception/client/EmptyChannelTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2018 Meituan Dianping. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.meituan.dorado.test.thrift.exception.client; - -import com.meituan.dorado.test.thrift.apitwitter.Twitter; -import org.junit.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -public class EmptyChannelTest { - - private static final Logger logger = LoggerFactory.getLogger(EmptyChannelTest.class); - - private static ClassPathXmlApplicationContext clientBeanFactory; - private static ClassPathXmlApplicationContext serverBeanFactory; - private static Twitter.Iface client; - - @BeforeClass - public static void start() { - serverBeanFactory = new ClassPathXmlApplicationContext("thrift/exception/emptyChannel/thrift-provider.xml"); - clientBeanFactory = new ClassPathXmlApplicationContext("thrift/exception/emptyChannel/thrift-consumer.xml"); - client = (Twitter.Iface) clientBeanFactory.getBean("clientProxy"); - } - - @AfterClass - public static void stop() { - clientBeanFactory.destroy(); - serverBeanFactory.destroy(); - } - - // 测试需要修改代码返回的channel为null - @Test - @Ignore - public void testEmptyChannel() { - try { - client.testString("test string."); - } catch (Exception e) { - Assert.fail(); - } - } - -} diff --git a/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/methodTimeout/thrift-provider.xml b/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/methodTimeout/thrift-provider.xml index 999591f..0d0ced9 100644 --- a/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/methodTimeout/thrift-provider.xml +++ b/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/methodTimeout/thrift-provider.xml @@ -25,11 +25,11 @@ + - \ No newline at end of file diff --git a/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/port2multiService/thrift-provider.xml b/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/port2multiService/thrift-provider.xml index e0d58f3..1b6f1f8 100644 --- a/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/port2multiService/thrift-provider.xml +++ b/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/port2multiService/thrift-provider.xml @@ -25,6 +25,7 @@ + @@ -36,12 +37,10 @@ - - \ No newline at end of file diff --git a/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/timeout/thrift-provider.xml b/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/timeout/thrift-provider.xml index 7ffdec4..ee4ec85 100644 --- a/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/timeout/thrift-provider.xml +++ b/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/timeout/thrift-provider.xml @@ -25,12 +25,12 @@ + - \ No newline at end of file diff --git a/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/twitterAsync/thrift-consumer.xml b/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/twitterAsync/thrift-consumer.xml new file mode 100644 index 0000000..dc9bcf0 --- /dev/null +++ b/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/twitterAsync/thrift-consumer.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + diff --git a/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/twitterAsync/thrift-provider.xml b/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/twitterAsync/thrift-provider.xml new file mode 100644 index 0000000..9ca20bc --- /dev/null +++ b/dorado/dorado-test/dorado-test-integration/src/test/resources/thrift/twitterAsync/thrift-provider.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + diff --git a/dorado/dorado-trace/dorado-trace-cat/src/test/java/com/meituan/dorado/trace/cat/CatInvokeTraceTest.java b/dorado/dorado-trace/dorado-trace-cat/src/test/java/com/meituan/dorado/trace/cat/CatInvokeTraceTest.java index 94a1e2b..236d770 100644 --- a/dorado/dorado-trace/dorado-trace-cat/src/test/java/com/meituan/dorado/trace/cat/CatInvokeTraceTest.java +++ b/dorado/dorado-trace/dorado-trace-cat/src/test/java/com/meituan/dorado/trace/cat/CatInvokeTraceTest.java @@ -72,7 +72,7 @@ public void testCatTraceWithTimeoutException() { Assert.assertEquals("echo: this is a message", message); } catch (Exception e) { e.printStackTrace(); - Assert.assertTrue(e.getCause() instanceof TimeoutException); + Assert.assertTrue(e instanceof TimeoutException); } } } diff --git a/dorado/dorado-trace/dorado-trace-cat/src/test/resources/thrift/thrift-provider.xml b/dorado/dorado-trace/dorado-trace-cat/src/test/resources/thrift/thrift-provider.xml index 6df564b..d76d87b 100644 --- a/dorado/dorado-trace/dorado-trace-cat/src/test/resources/thrift/thrift-provider.xml +++ b/dorado/dorado-trace/dorado-trace-cat/src/test/resources/thrift/thrift-provider.xml @@ -25,12 +25,12 @@ + - \ No newline at end of file