Search in sources :

Example 16 with GovernanceRequest

use of org.apache.servicecomb.governance.marker.GovernanceRequest 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);
    });
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) Exceptions.toConsumerResponse(org.apache.servicecomb.core.exception.Exceptions.toConsumerResponse) GovernanceRequest(org.apache.servicecomb.governance.marker.GovernanceRequest) DecorateCompletionStage(io.github.resilience4j.decorators.Decorators.DecorateCompletionStage) CompletionStage(java.util.concurrent.CompletionStage)

Example 17 with GovernanceRequest

use of org.apache.servicecomb.governance.marker.GovernanceRequest in project java-chassis by ServiceComb.

the class InvokerUtils method invoke.

/**
 * This method is used in new Filter implementation to replace Handler
 * NOTE: this method should never throw exception directly
 */
public static CompletableFuture<Response> invoke(Invocation invocation) {
    Supplier<CompletionStage<Response>> next = invokeImpl(invocation);
    DecorateCompletionStage<Response> dcs = Decorators.ofCompletionStage(next);
    GovernanceRequest request = MatchType.createGovHttpRequest(invocation);
    try {
        ServiceCombInvocationContext.setInvocationContext(invocation);
        decorateReactiveRetry(invocation, dcs, request);
    } finally {
        ServiceCombInvocationContext.removeInvocationContext();
    }
    CompletableFuture<Response> result = new CompletableFuture<>();
    dcs.get().whenComplete((r, e) -> {
        if (e == null) {
            result.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);
        result.complete(response);
    });
    return result;
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) Exceptions.toConsumerResponse(org.apache.servicecomb.core.exception.Exceptions.toConsumerResponse) CompletableFuture(java.util.concurrent.CompletableFuture) GovernanceRequest(org.apache.servicecomb.governance.marker.GovernanceRequest) DecorateCompletionStage(io.github.resilience4j.decorators.Decorators.DecorateCompletionStage) CompletionStage(java.util.concurrent.CompletionStage)

Example 18 with GovernanceRequest

use of org.apache.servicecomb.governance.marker.GovernanceRequest in project java-chassis by ServiceComb.

the class OperatorTest method test_prefix_api_path_not_match_null.

@Test
public void test_prefix_api_path_not_match_null() {
    GovernanceRequest request = new GovernanceRequest();
    request.setUri("/bulkhead/hello");
    Matcher matcher = new Matcher();
    RawOperator apiPath = new RawOperator();
    apiPath.put("prefix", null);
    matcher.setApiPath(apiPath);
    Assert.assertFalse(requestProcessor.match(request, matcher));
}
Also used : GovernanceRequest(org.apache.servicecomb.governance.marker.GovernanceRequest) Matcher(org.apache.servicecomb.governance.marker.Matcher) RawOperator(org.apache.servicecomb.governance.marker.operator.RawOperator) Test(org.junit.Test)

Example 19 with GovernanceRequest

use of org.apache.servicecomb.governance.marker.GovernanceRequest in project java-chassis by ServiceComb.

the class OperatorTest method test_exact_api_path_match_header_match.

@Test
public void test_exact_api_path_match_header_match() {
    GovernanceRequest request = new GovernanceRequest();
    request.setUri("/bulkhead");
    request.setMethod("GET");
    Map<String, String> reqHeaders = new HashMap<>();
    reqHeaders.put("header1", "value1");
    request.setHeaders(reqHeaders);
    Matcher matcher = new Matcher();
    RawOperator apiPath = new RawOperator();
    apiPath.put("exact", "/bulkhead");
    matcher.setApiPath(apiPath);
    matcher.setMethod(Arrays.asList("GET"));
    Map<String, RawOperator> headers = new HashMap<>();
    RawOperator header1 = new RawOperator();
    header1.put("exact", "value1");
    headers.put("header1", header1);
    matcher.setHeaders(headers);
    Assert.assertTrue(requestProcessor.match(request, matcher));
}
Also used : GovernanceRequest(org.apache.servicecomb.governance.marker.GovernanceRequest) HashMap(java.util.HashMap) Matcher(org.apache.servicecomb.governance.marker.Matcher) RawOperator(org.apache.servicecomb.governance.marker.operator.RawOperator) Test(org.junit.Test)

Example 20 with GovernanceRequest

use of org.apache.servicecomb.governance.marker.GovernanceRequest in project java-chassis by ServiceComb.

the class OperatorTest method test_compare_header_match.

@Test
public void test_compare_header_match() {
    GovernanceRequest request = new GovernanceRequest();
    Map<String, String> reqHeaders = new HashMap<>();
    reqHeaders.put("header1", "100");
    request.setHeaders(reqHeaders);
    Matcher matcher = new Matcher();
    Map<String, RawOperator> headers = new HashMap<>();
    RawOperator header1 = new RawOperator();
    header1.put("compare", ">10");
    headers.put("header1", header1);
    matcher.setHeaders(headers);
    Assert.assertTrue(requestProcessor.match(request, matcher));
    header1 = new RawOperator();
    header1.put("compare", ">=10");
    headers.put("header1", header1);
    matcher.setHeaders(headers);
    Assert.assertTrue(requestProcessor.match(request, matcher));
    header1 = new RawOperator();
    header1.put("compare", "<1000");
    headers.put("header1", header1);
    matcher.setHeaders(headers);
    Assert.assertTrue(requestProcessor.match(request, matcher));
    header1 = new RawOperator();
    header1.put("compare", "<=1000");
    headers.put("header1", header1);
    matcher.setHeaders(headers);
    Assert.assertTrue(requestProcessor.match(request, matcher));
    header1 = new RawOperator();
    header1.put("compare", "=100");
    headers.put("header1", header1);
    matcher.setHeaders(headers);
    Assert.assertTrue(requestProcessor.match(request, matcher));
}
Also used : GovernanceRequest(org.apache.servicecomb.governance.marker.GovernanceRequest) HashMap(java.util.HashMap) Matcher(org.apache.servicecomb.governance.marker.Matcher) RawOperator(org.apache.servicecomb.governance.marker.operator.RawOperator) Test(org.junit.Test)

Aggregations

GovernanceRequest (org.apache.servicecomb.governance.marker.GovernanceRequest)34 Test (org.junit.Test)26 Matcher (org.apache.servicecomb.governance.marker.Matcher)24 RawOperator (org.apache.servicecomb.governance.marker.operator.RawOperator)24 HashMap (java.util.HashMap)10 DecorateCompletionStage (io.github.resilience4j.decorators.Decorators.DecorateCompletionStage)6 CompletionStage (java.util.concurrent.CompletionStage)6 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)6 Response (org.apache.servicecomb.swagger.invocation.Response)6 RequestNotPermitted (io.github.resilience4j.ratelimiter.RequestNotPermitted)4 Exceptions.toConsumerResponse (org.apache.servicecomb.core.exception.Exceptions.toConsumerResponse)4 BulkheadFullException (io.github.resilience4j.bulkhead.BulkheadFullException)2 CallNotPermittedException (io.github.resilience4j.circuitbreaker.CallNotPermittedException)2 RateLimiter (io.github.resilience4j.ratelimiter.RateLimiter)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)2 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)2