use of org.apache.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.
the class DubboProtocol method initClient.
/**
* Create new connection
*
* @param url
*/
private ExchangeClient initClient(URL url) {
// client type setting.
String str = url.getParameter(CLIENT_KEY, url.getParameter(SERVER_KEY, DEFAULT_REMOTING_CLIENT));
url = url.addParameter(CODEC_KEY, DubboCodec.NAME);
// enable heartbeat by default
url = url.addParameterIfAbsent(HEARTBEAT_KEY, String.valueOf(DEFAULT_HEARTBEAT));
// BIO is not allowed since it has severe performance issue.
if (str != null && str.length() > 0 && !ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str)) {
throw new RpcException("Unsupported client type: " + str + "," + " supported client type is " + StringUtils.join(ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions(), " "));
}
ExchangeClient client;
try {
// connection should be lazy
if (url.getParameter(LAZY_CONNECT_KEY, false)) {
client = new LazyConnectExchangeClient(url, requestHandler);
} else {
client = Exchangers.connect(url, requestHandler);
}
} catch (RemotingException e) {
throw new RpcException("Fail to create remoting client for service(" + url + "): " + e.getMessage(), e);
}
return client;
}
use of org.apache.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.
the class DubboInvokerAvailableTest method getClients.
private ExchangeClient[] getClients(DubboInvoker<?> invoker) throws Exception {
Field field = DubboInvoker.class.getDeclaredField("clients");
ReflectUtils.makeAccessible(field);
ExchangeClient[] clients = (ExchangeClient[]) field.get(invoker);
Assertions.assertEquals(1, clients.length);
return clients;
}
use of org.apache.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.
the class ReferenceCountExchangeClientTest method getReferenceClientList.
private List<ReferenceCountExchangeClient> getReferenceClientList(Invoker<?> invoker) {
List<ExchangeClient> invokerClientList = getInvokerClientList(invoker);
List<ReferenceCountExchangeClient> referenceCountExchangeClientList = new ArrayList<>(invokerClientList.size());
for (ExchangeClient exchangeClient : invokerClientList) {
Assertions.assertTrue(exchangeClient instanceof ReferenceCountExchangeClient);
referenceCountExchangeClientList.add((ReferenceCountExchangeClient) exchangeClient);
}
return referenceCountExchangeClientList;
}
use of org.apache.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.
the class ThriftInvoker method doInvoke.
@Override
protected Result doInvoke(Invocation invocation) throws Throwable {
RpcInvocation inv = (RpcInvocation) invocation;
final String methodName;
methodName = invocation.getMethodName();
inv.setAttachment(PATH_KEY, getUrl().getPath());
// for thrift codec
inv.setAttachment(ThriftCodec.PARAMETER_CLASS_NAME_GENERATOR, getUrl().getParameter(ThriftCodec.PARAMETER_CLASS_NAME_GENERATOR, DubboClassNameGenerator.NAME));
ExchangeClient currentClient;
if (clients.length == 1) {
currentClient = clients[0];
} else {
currentClient = clients[index.getAndIncrement() % clients.length];
}
try {
int timeout = getUrl().getMethodParameter(methodName, TIMEOUT_KEY, DEFAULT_TIMEOUT);
ExecutorService executor = getCallbackExecutor(getUrl(), inv);
CompletableFuture<AppResponse> appResponseFuture = currentClient.request(inv, timeout, executor).thenApply(obj -> (AppResponse) obj);
// save for 2.6.x compatibility, for example, TraceFilter in Zipkin uses com.alibaba.xxx.FutureAdapter
FutureContext.getContext().setCompatibleFuture(appResponseFuture);
AsyncRpcResult result = new AsyncRpcResult(appResponseFuture, invocation);
result.setExecutor(executor);
return result;
} catch (TimeoutException e) {
throw new RpcException(RpcException.TIMEOUT_EXCEPTION, e.getMessage(), e);
} catch (RemotingException e) {
throw new RpcException(RpcException.NETWORK_EXCEPTION, e.getMessage(), e);
}
}
use of org.apache.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.
the class PerformanceClientTest method testClient.
@Test
@SuppressWarnings("unchecked")
public void testClient() throws Throwable {
// read server info from property
if (PerformanceUtils.getProperty("server", null) == null) {
logger.warn("Please set -Dserver=127.0.0.1:9911");
return;
}
final String server = System.getProperty("server", "127.0.0.1:9911");
final String transporter = PerformanceUtils.getProperty(Constants.TRANSPORTER_KEY, Constants.DEFAULT_TRANSPORTER);
final String serialization = PerformanceUtils.getProperty(Constants.SERIALIZATION_KEY, Constants.DEFAULT_REMOTING_SERIALIZATION);
final int timeout = PerformanceUtils.getIntProperty(TIMEOUT_KEY, DEFAULT_TIMEOUT);
final int length = PerformanceUtils.getIntProperty("length", 1024);
final int connections = PerformanceUtils.getIntProperty(CONNECTIONS_KEY, 1);
final int concurrent = PerformanceUtils.getIntProperty("concurrent", 100);
int r = PerformanceUtils.getIntProperty("runs", 10000);
final int runs = r > 0 ? r : Integer.MAX_VALUE;
final String onerror = PerformanceUtils.getProperty("onerror", "continue");
final String url = "exchange://" + server + "?transporter=" + transporter + "&serialization=" + serialization + "&timeout=" + timeout;
// Create clients and build connections
final ExchangeClient[] exchangeClients = new ExchangeClient[connections];
for (int i = 0; i < connections; i++) {
// exchangeClients[i] = Exchangers.connect(url,handler);
exchangeClients[i] = Exchangers.connect(url);
}
List<String> serverEnvironment = (List<String>) exchangeClients[0].request("environment").get();
List<String> serverScene = (List<String>) exchangeClients[0].request("scene").get();
// Create some data for test
StringBuilder buf = new StringBuilder(length);
for (int i = 0; i < length; i++) {
buf.append("A");
}
final String data = buf.toString();
// counters
final AtomicLong count = new AtomicLong();
final AtomicLong error = new AtomicLong();
final AtomicLong time = new AtomicLong();
final AtomicLong all = new AtomicLong();
// Start multiple threads
final CountDownLatch latch = new CountDownLatch(concurrent);
for (int i = 0; i < concurrent; i++) {
new Thread(new Runnable() {
public void run() {
try {
AtomicInteger index = new AtomicInteger();
long init = System.currentTimeMillis();
for (int i = 0; i < runs; i++) {
try {
count.incrementAndGet();
ExchangeClient client = exchangeClients[index.getAndIncrement() % connections];
long start = System.currentTimeMillis();
String result = (String) client.request(data).get();
long end = System.currentTimeMillis();
if (!data.equals(result)) {
throw new IllegalStateException("Invalid result " + result);
}
time.addAndGet(end - start);
} catch (Exception e) {
error.incrementAndGet();
e.printStackTrace();
if ("exit".equals(onerror)) {
System.exit(-1);
} else if ("break".equals(onerror)) {
break;
} else if ("sleep".equals(onerror)) {
try {
Thread.sleep(30000);
} catch (InterruptedException e1) {
}
}
}
}
all.addAndGet(System.currentTimeMillis() - init);
} finally {
latch.countDown();
}
}
}).start();
}
// Output, tps is not for accuracy, but it reflects the situation to a certain extent.
new Thread(new Runnable() {
public void run() {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
long lastCount = count.get();
long sleepTime = 2000;
long elapsd = sleepTime / 1000;
boolean bfirst = true;
while (latch.getCount() > 0) {
long c = count.get() - lastCount;
if (// The first time is inaccurate.
!bfirst)
System.out.println("[" + dateFormat.format(new Date()) + "] count: " + count.get() + ", error: " + error.get() + ",tps:" + (c / elapsd));
bfirst = false;
lastCount = count.get();
Thread.sleep(sleepTime);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
latch.await();
for (ExchangeClient client : exchangeClients) {
if (client.isConnected()) {
client.close();
}
}
long total = count.get();
long failed = error.get();
long succeeded = total - failed;
long elapsed = time.get();
long allElapsed = all.get();
long clientElapsed = allElapsed - elapsed;
long art = 0;
long qps = 0;
long throughput = 0;
if (elapsed > 0) {
art = elapsed / succeeded;
qps = concurrent * succeeded * 1000 / elapsed;
throughput = concurrent * succeeded * length * 2 * 1000 / elapsed;
}
PerformanceUtils.printBorder();
PerformanceUtils.printHeader("Dubbo Remoting Performance Test Report");
PerformanceUtils.printBorder();
PerformanceUtils.printHeader("Test Environment");
PerformanceUtils.printSeparator();
for (String item : serverEnvironment) {
PerformanceUtils.printBody("Server " + item);
}
PerformanceUtils.printSeparator();
List<String> clientEnvironment = PerformanceUtils.getEnvironment();
for (String item : clientEnvironment) {
PerformanceUtils.printBody("Client " + item);
}
PerformanceUtils.printSeparator();
PerformanceUtils.printHeader("Test Scene");
PerformanceUtils.printSeparator();
for (String item : serverScene) {
PerformanceUtils.printBody("Server " + item);
}
PerformanceUtils.printBody("Client Transporter: " + transporter);
PerformanceUtils.printBody("Serialization: " + serialization);
PerformanceUtils.printBody("Response Timeout: " + timeout + " ms");
PerformanceUtils.printBody("Data Length: " + length + " bytes");
PerformanceUtils.printBody("Client Shared Connections: " + connections);
PerformanceUtils.printBody("Client Concurrent Threads: " + concurrent);
PerformanceUtils.printBody("Run Times Per Thread: " + runs);
PerformanceUtils.printSeparator();
PerformanceUtils.printHeader("Test Result");
PerformanceUtils.printSeparator();
PerformanceUtils.printBody("Succeeded Requests: " + DecimalFormat.getIntegerInstance().format(succeeded));
PerformanceUtils.printBody("Failed Requests: " + failed);
PerformanceUtils.printBody("Client Elapsed Time: " + clientElapsed + " ms");
PerformanceUtils.printBody("Average Response Time: " + art + " ms");
PerformanceUtils.printBody("Requests Per Second: " + qps + "/s");
PerformanceUtils.printBody("Throughput Per Second: " + DecimalFormat.getIntegerInstance().format(throughput) + " bytes/s");
PerformanceUtils.printBorder();
}
Aggregations