use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class BroadcastClusterInvoker method doInvoke.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Result doInvoke(final Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
checkInvokers(invokers, invocation);
RpcContext.getContext().setInvokers((List) invokers);
RpcException exception = null;
Result result = null;
URL url = getUrl();
// The value range of broadcast.fail.threshold must be 0~100.
// 100 means that an exception will be thrown last, and 0 means that as long as an exception occurs, it will be thrown.
// see https://github.com/apache/dubbo/pull/7174
int broadcastFailPercent = url.getParameter(BROADCAST_FAIL_PERCENT_KEY, MAX_BROADCAST_FAIL_PERCENT);
if (broadcastFailPercent < MIN_BROADCAST_FAIL_PERCENT || broadcastFailPercent > MAX_BROADCAST_FAIL_PERCENT) {
logger.info(String.format("The value corresponding to the broadcast.fail.percent parameter must be between 0 and 100. " + "The current setting is %s, which is reset to 100.", broadcastFailPercent));
broadcastFailPercent = MAX_BROADCAST_FAIL_PERCENT;
}
int failThresholdIndex = invokers.size() * broadcastFailPercent / MAX_BROADCAST_FAIL_PERCENT;
int failIndex = 0;
for (Invoker<T> invoker : invokers) {
try {
result = invoker.invoke(invocation);
if (null != result && result.hasException()) {
Throwable resultException = result.getException();
if (null != resultException) {
exception = getRpcException(result.getException());
logger.warn(exception.getMessage(), exception);
if (failIndex == failThresholdIndex) {
break;
} else {
failIndex++;
}
}
}
} catch (Throwable e) {
exception = getRpcException(e);
logger.warn(exception.getMessage(), exception);
if (failIndex == failThresholdIndex) {
break;
} else {
failIndex++;
}
}
}
if (exception != null) {
if (failIndex == failThresholdIndex) {
logger.debug(String.format("The number of BroadcastCluster call failures has reached the threshold %s", failThresholdIndex));
} else {
logger.debug(String.format("The number of BroadcastCluster call failures has not reached the threshold %s, fail size is %s", failThresholdIndex, failIndex));
}
throw exception;
}
return result;
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class FutureFilterTest method testSyncCallback.
@Test
public void testSyncCallback() {
@SuppressWarnings("unchecked") Invoker<DemoService> invoker = mock(Invoker.class);
given(invoker.isAvailable()).willReturn(true);
given(invoker.getInterface()).willReturn(DemoService.class);
AppResponse result = new AppResponse();
result.setValue("High");
given(invoker.invoke(invocation)).willReturn(result);
URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
given(invoker.getUrl()).willReturn(url);
Result filterResult = eventFilter.invoke(invoker, invocation);
assertEquals("High", filterResult.getValue());
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class InjvmInvoker method doInvoke.
@Override
public Result doInvoke(Invocation invocation) throws Throwable {
Exporter<?> exporter = InjvmProtocol.getExporter(delegateExporterMap, getUrl());
if (exporter == null) {
throw new RpcException("Service [" + key + "] not found.");
}
RpcContext.getContext().setRemoteAddress(LOCALHOST_VALUE, 0);
// Solve local exposure, the server opens the token, and the client call fails.
URL serverURL = exporter.getInvoker().getUrl();
boolean serverHasToken = serverURL.hasParameter(Constants.TOKEN_KEY);
if (serverHasToken) {
invocation.setAttachment(Constants.TOKEN_KEY, serverURL.getParameter(Constants.TOKEN_KEY));
}
if (isAsync(exporter.getInvoker().getUrl(), getUrl())) {
((RpcInvocation) invocation).setInvokeMode(InvokeMode.ASYNC);
// use consumer executor
ExecutorService executor = executorRepository.createExecutorIfAbsent(getUrl());
CompletableFuture<AppResponse> appResponseFuture = CompletableFuture.supplyAsync(() -> {
Result result = exporter.getInvoker().invoke(invocation);
if (result.hasException()) {
return new AppResponse(result.getException());
} else {
return new AppResponse(result.getValue());
}
}, executor);
// 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;
} else {
return exporter.getInvoker().invoke(invocation);
}
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class TraceFilterTest method testInvoke.
@Test
public void testInvoke() throws Exception {
String method = "sayHello";
Class<?> type = DemoService.class;
String key = type.getName() + "." + method;
// add tracer
TraceFilter.addTracer(type, method, mockChannel, 2);
Invoker<DemoService> mockInvoker = mock(Invoker.class);
Invocation mockInvocation = mock(Invocation.class);
Result mockResult = mock(Result.class);
TraceFilter filter = new TraceFilter();
given(mockInvoker.getInterface()).willReturn(DemoService.class);
given(mockInvocation.getMethodName()).willReturn(method);
given(mockInvocation.getArguments()).willReturn(new Object[0]);
given(mockInvoker.invoke(mockInvocation)).willReturn(mockResult);
given(mockResult.getValue()).willReturn("result");
// test invoke
filter.invoke(mockInvoker, mockInvocation);
String message = listToString(mockChannel.getReceivedObjects());
String expectMessage = "org.apache.dubbo.rpc.protocol.dubbo.support.DemoService.sayHello([]) -> \"result\"";
System.out.println("actual message: " + message);
Assertions.assertTrue(message.contains(expectMessage));
Assertions.assertTrue(message.contains("elapsed:"));
AtomicInteger traceCount = (AtomicInteger) mockChannel.getAttribute(TRACE_COUNT);
Assertions.assertEquals(1, traceCount.get());
// test remove channel when count >= max - 1
filter.invoke(mockInvoker, mockInvocation);
Field tracers = TraceFilter.class.getDeclaredField(TRACERS_FIELD_NAME);
tracers.setAccessible(true);
ConcurrentHashMap<String, Set<Channel>> o = (ConcurrentHashMap<String, Set<Channel>>) tracers.get(new ConcurrentHashMap<String, Set<Channel>>());
Assertions.assertTrue(o.containsKey(key));
Set<Channel> channels = o.get(key);
Assertions.assertNotNull(channels);
Assertions.assertFalse(channels.contains(mockChannel));
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class MonitorFilterTest method testFilter.
@Test
public void testFilter() throws Exception {
MonitorFilter monitorFilter = new MonitorFilter();
monitorFilter.setMonitorFactory(monitorFactory);
Invocation invocation = new RpcInvocation("aaa", MonitorService.class.getName(), "", new Class<?>[0], new Object[0]);
RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
Result result = monitorFilter.invoke(serviceInvoker, invocation);
result.whenCompleteWithContext((r, t) -> {
if (t == null) {
monitorFilter.onResponse(r, serviceInvoker, invocation);
} else {
monitorFilter.onError(t, serviceInvoker, invocation);
}
});
while (lastStatistics == null) {
Thread.sleep(10);
}
Assertions.assertEquals("abc", lastStatistics.getParameter(MonitorService.APPLICATION));
Assertions.assertEquals(MonitorService.class.getName(), lastStatistics.getParameter(MonitorService.INTERFACE));
Assertions.assertEquals("aaa", lastStatistics.getParameter(MonitorService.METHOD));
Assertions.assertEquals(NetUtils.getLocalHost() + ":20880", lastStatistics.getParameter(MonitorService.PROVIDER));
Assertions.assertEquals(NetUtils.getLocalHost(), lastStatistics.getAddress());
Assertions.assertNull(lastStatistics.getParameter(MonitorService.CONSUMER));
Assertions.assertEquals(1, lastStatistics.getParameter(MonitorService.SUCCESS, 0));
Assertions.assertEquals(0, lastStatistics.getParameter(MonitorService.FAILURE, 0));
Assertions.assertEquals(1, lastStatistics.getParameter(MonitorService.CONCURRENT, 0));
Assertions.assertEquals(invocation, lastInvocation);
}
Aggregations