use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project java-chassis by ServiceComb.
the class TestLoadbalanceHandler method send_failed.
@Test
public void send_failed(@Injectable LoadBalancer loadBalancer) {
MicroserviceInstance instance1 = new MicroserviceInstance();
instance1.setInstanceId("1234");
CacheEndpoint cacheEndpoint = new CacheEndpoint("rest://localhost:8080", instance1);
ServiceCombServer server = new ServiceCombServer(null, restTransport, cacheEndpoint);
LoadBalancerStats stats = new LoadBalancerStats("test");
new Expectations(loadBalancer) {
{
loadBalancer.chooseServer(invocation);
result = server;
loadBalancer.getLoadBalancerStats();
result = stats;
}
};
sendResponse = Response.consumerFailResp(new SocketException());
Holder<Throwable> result = new Holder<>();
Deencapsulation.invoke(handler, "send", invocation, (AsyncResponse) resp -> {
result.value = (Throwable) resp.getResult();
}, loadBalancer);
Assert.assertEquals(1, loadBalancer.getLoadBalancerStats().getSingleServerStat(server).getSuccessiveConnectionFailureCount());
Assert.assertEquals("InvocationException: code=490;msg=CommonExceptionData [message=Unexpected consumer error, please check logs for details]", result.value.getMessage());
}
use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project java-chassis by ServiceComb.
the class RestClientInvocation method invoke.
public void invoke(Invocation invocation, AsyncResponse asyncResp) throws Exception {
this.invocation = invocation;
this.asyncResp = asyncResp;
OperationMeta operationMeta = invocation.getOperationMeta();
restOperationMeta = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
String path = this.createRequestPath(restOperationMeta);
IpPort ipPort = (IpPort) invocation.getEndpoint().getAddress();
Future<HttpClientRequest> requestFuture = createRequest(ipPort, path);
invocation.getInvocationStageTrace().startGetConnection();
requestFuture.compose(clientRequest -> {
invocation.getInvocationStageTrace().finishGetConnection();
this.clientRequest = clientRequest;
clientRequest.putHeader(org.apache.servicecomb.core.Const.TARGET_MICROSERVICE, invocation.getMicroserviceName());
RestClientRequestImpl restClientRequest = new RestClientRequestImpl(clientRequest, httpClientWithContext.context(), asyncResp, throwableHandler);
invocation.getHandlerContext().put(RestConst.INVOCATION_HANDLER_REQUESTCLIENT, restClientRequest);
Buffer requestBodyBuffer;
try {
requestBodyBuffer = restClientRequest.getBodyBuffer();
} catch (Exception e) {
return Future.failedFuture(e);
}
HttpServletRequestEx requestEx = new VertxClientRequestToHttpServletRequest(clientRequest, requestBodyBuffer);
invocation.getInvocationStageTrace().startClientFiltersRequest();
for (HttpClientFilter filter : httpClientFilters) {
if (filter.enabled()) {
filter.beforeSendRequest(invocation, requestEx);
}
}
// 从业务线程转移到网络线程中去发送
invocation.onStartSendRequest();
httpClientWithContext.runOnContext(httpClient -> {
clientRequest.setTimeout(operationMeta.getConfig().getMsRequestTimeout());
clientRequest.response().onComplete(asyncResult -> {
if (asyncResult.failed()) {
fail(asyncResult.cause());
return;
}
handleResponse(asyncResult.result());
});
processServiceCombHeaders(invocation, operationMeta);
restClientRequest.end().onComplete((t) -> invocation.getInvocationStageTrace().finishWriteToBuffer(System.nanoTime()));
});
return Future.succeededFuture();
}).onFailure(failure -> {
invocation.getTraceIdLogger().error(LOGGER, "Failed to send request, alreadyFailed:{}, local:{}, remote:{}, message={}.", alreadyFailed, getLocalAddress(), ipPort.getSocketAddress(), ExceptionUtils.getExceptionMessageWithoutTrace(failure));
throwableHandler.handle(failure);
});
}
use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project java-chassis by ServiceComb.
the class InvokerUtils method reactiveInvoke.
/**
* This is an internal API, caller make sure already invoked SCBEngine.ensureStatusUp
*/
public static void reactiveInvoke(Invocation invocation, AsyncResponse asyncResp) {
invocation.setSync(false);
Supplier<CompletionStage<Response>> next = reactiveInvokeImpl(invocation);
DecorateCompletionStage<Response> dcs = Decorators.ofCompletionStage(next);
GovernanceRequest request = MatchType.createGovHttpRequest(invocation);
try {
ServiceCombInvocationContext.setInvocationContext(invocation);
decorateReactiveRetry(invocation, dcs, request);
} finally {
ServiceCombInvocationContext.removeInvocationContext();
}
dcs.get().whenComplete((r, e) -> {
if (e == null) {
asyncResp.complete(r);
return;
}
String message = String.format("invoke failed, operation %s, trace id %s", invocation.getMicroserviceQualifiedName(), invocation.getTraceId());
LOGGER.error(message, e);
Response response = Response.createConsumerFail(e, message);
invocation.onFinish(response);
asyncResp.complete(response);
});
}
use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project java-chassis by ServiceComb.
the class LoadbalanceHandler method handle.
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
AsyncResponse response = asyncResp;
asyncResp = async -> {
ServiceCombServerStats.checkAndReleaseTryingChance(invocation);
response.handle(async);
};
if (handleSuppliedEndpoint(invocation, asyncResp)) {
invocation.addLocalContext(RetryContext.RETRY_LOAD_BALANCE, false);
return;
}
invocation.addLocalContext(RetryContext.RETRY_LOAD_BALANCE, true);
String strategy = Configuration.INSTANCE.getRuleStrategyName(invocation.getMicroserviceName());
if (!Objects.equals(strategy, this.strategy)) {
// 配置变化,需要重新生成所有的lb实例
synchronized (lock) {
clearLoadBalancer();
}
}
this.strategy = strategy;
LoadBalancer loadBalancer = getOrCreateLoadBalancer(invocation);
send(invocation, asyncResp, loadBalancer);
}
Aggregations