use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class ProviderAuthFilterTest method testAuthFailed.
@Test
void testAuthFailed() {
URL url = URL.valueOf("dubbo://10.10.10.10:2181").addParameter(Constants.ACCESS_KEY_ID_KEY, "ak").addParameter(Constants.SECRET_ACCESS_KEY_KEY, "sk").addParameter(CommonConstants.APPLICATION_KEY, "test").addParameter(Constants.SERVICE_AUTH, true);
Invoker invoker = mock(Invoker.class);
Invocation invocation = mock(Invocation.class);
when(invocation.getAttachment(Constants.REQUEST_SIGNATURE_KEY)).thenReturn(null);
when(invoker.getUrl()).thenReturn(url);
ProviderAuthFilter providerAuthFilter = new ProviderAuthFilter();
Result result = providerAuthFilter.invoke(invoker, invocation);
assertTrue(result.hasException());
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class ProviderAuthFilterTest method testAuthFailedWhenNoSignature.
@Test
void testAuthFailedWhenNoSignature() {
URL url = URL.valueOf("dubbo://10.10.10.10:2181").addParameter(Constants.ACCESS_KEY_ID_KEY, "ak").addParameter(Constants.SECRET_ACCESS_KEY_KEY, "sk").addParameter(CommonConstants.APPLICATION_KEY, "test").addParameter(Constants.SERVICE_AUTH, true);
Invoker invoker = mock(Invoker.class);
Invocation invocation = mock(Invocation.class);
when(invocation.getAttachment(Constants.REQUEST_SIGNATURE_KEY)).thenReturn(null);
when(invoker.getUrl()).thenReturn(url);
ProviderAuthFilter providerAuthFilter = new ProviderAuthFilter();
Result result = providerAuthFilter.invoke(invoker, invocation);
assertTrue(result.hasException());
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class ProviderAuthFilterTest method testAuthFailedWhenNoAccessKeyPair.
@Test
void testAuthFailedWhenNoAccessKeyPair() {
URL url = URL.valueOf("dubbo://10.10.10.10:2181").addParameter(CommonConstants.APPLICATION_KEY, "test-provider").addParameter(Constants.SERVICE_AUTH, true);
Invoker invoker = mock(Invoker.class);
Invocation invocation = mock(Invocation.class);
when(invocation.getObjectAttachment(Constants.REQUEST_SIGNATURE_KEY)).thenReturn("dubbo");
when(invocation.getObjectAttachment(Constants.AK_KEY)).thenReturn("ak");
when(invocation.getObjectAttachment(CommonConstants.CONSUMER)).thenReturn("test-consumer");
when(invocation.getObjectAttachment(Constants.REQUEST_TIMESTAMP_KEY)).thenReturn(System.currentTimeMillis());
when(invoker.getUrl()).thenReturn(url);
ProviderAuthFilter providerAuthFilter = new ProviderAuthFilter();
Result result = providerAuthFilter.invoke(invoker, invocation);
assertTrue(result.hasException());
assertTrue(result.getException() instanceof RpcAuthenticationException);
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class ForkingClusterInvoker method doInvoke.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Result doInvoke(final Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
try {
checkInvokers(invokers, invocation);
final List<Invoker<T>> selected;
final int forks = getUrl().getParameter(FORKS_KEY, DEFAULT_FORKS);
final int timeout = getUrl().getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
if (forks <= 0 || forks >= invokers.size()) {
selected = invokers;
} else {
selected = new ArrayList<>(forks);
while (selected.size() < forks) {
Invoker<T> invoker = select(loadbalance, invocation, invokers, selected);
if (!selected.contains(invoker)) {
// Avoid add the same invoker several times.
selected.add(invoker);
}
}
}
RpcContext.getContext().setInvokers((List) selected);
final AtomicInteger count = new AtomicInteger();
final BlockingQueue<Object> ref = new LinkedBlockingQueue<>();
for (final Invoker<T> invoker : selected) {
executor.execute(() -> {
try {
Result result = invoker.invoke(invocation);
ref.offer(result);
} catch (Throwable e) {
int value = count.incrementAndGet();
if (value >= selected.size()) {
ref.offer(e);
}
}
});
}
try {
Object ret = ref.poll(timeout, TimeUnit.MILLISECONDS);
if (ret instanceof Throwable) {
Throwable e = (Throwable) ret;
throw new RpcException(e instanceof RpcException ? ((RpcException) e).getCode() : 0, "Failed to forking invoke provider " + selected + ", but no luck to perform the invocation. Last error is: " + e.getMessage(), e.getCause() != null ? e.getCause() : e);
}
return (Result) ret;
} catch (InterruptedException e) {
throw new RpcException("Failed to forking invoke provider " + selected + ", but no luck to perform the invocation. Last error is: " + e.getMessage(), e);
}
} finally {
// clear attachments which is binding to current thread.
RpcContext.getContext().clearAttachments();
}
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class BroadcastCluster2Invoker method getCallables.
private List<Callable<BroadcastResult>> getCallables(List<Invoker<T>> invokers, Invocation invocation) {
List<Callable<BroadcastResult>> tasks = invokers.stream().map(it -> (Callable<BroadcastResult>) () -> {
BroadcastResult br = new BroadcastResult(it.getUrl().getIp(), it.getUrl().getPort());
Result result = null;
try {
result = it.invoke(invocation);
if (null != result && result.hasException()) {
Throwable resultException = result.getException();
if (null != resultException) {
RpcException exception = getRpcException(result.getException());
br.setExceptionMsg(exception.getMessage());
br.setException(exception);
logger.warn(exception.getMessage(), exception);
}
} else if (null != result) {
br.setData(result.getValue());
br.setResult(result);
}
} catch (Throwable ex) {
RpcException exception = getRpcException(result.getException());
br.setExceptionMsg(exception.getMessage());
br.setException(exception);
logger.warn(exception.getMessage(), exception);
}
return br;
}).collect(Collectors.toList());
return tasks;
}
Aggregations