/** * @deprecated use {@link #callSynchronous(JsonArray, OrangeContext)} instead and make sure to always pass the {@link OrangeContext} */ @Deprecated public String callSynchronous(JsonArray params) throws RpcCallException { return callSynchronous(params, null); }
public RESPONSE sendRequest(Message request, OrangeContext orangeContext) throws RpcCallException { ProtobufRpcRequest pbRequest = new ProtobufRpcRequest(client.getMethodName(), request); logger.info("Calling {} with {}", client.getMethodName(), ProtobufUtil. protobufToJson(pbRequest.getPayload())); client.getLoadBalancer().waitForServiceInstance(); RESPONSE response = null; response = (RESPONSE) client.callSynchronous(request, orangeContext); logger.info("Received response: {}", response); return response; }
private GoTimer getMethodTimer() { if (rpcClientMetrics == null) { return new GoTimer(""); } else { return rpcClientMetrics.getMethodTimer(client.getServiceName(), client.getMethodName()); } }
when(exception.isRetriable()).thenReturn(true); when(decoder.decodeException(any(ContentResponse.class))).thenReturn(exception); when(rpcClient.getRetryBackOffFunction()) .thenReturn(retryCounter -> Duration.ofMillis(10)); when(rpcClient.hasRetryBackOffFunction()) .thenReturn(true); verify(rpcClient, times(NUMBER_OF_RETRIES + 1)).getTimeout(); verify(rpcClient, times(8)).getRetries(); verify(rpcClient, times(8)).getMethodName(); verify(rpcClient, times(4)).getServiceName(); Assert.assertTrue(timeSpentOnRetries >= NUMBER_OF_RETRIES * 10); Assert.assertEquals(1, exceptionsCatchTimes);
span = tracer.buildSpan(client.getMethodName()).asChildOf(spanContext).start(); } else { span = tracer.buildSpan(client.getMethodName()).start(); span.setTag("rpc.call", client.getServiceMethodName()); if (orangeContext != null) { span.setTag("correlation_id", orangeContext.getCorrelationId()); retval = request.newRequest(httpClient).timeout(client.getTimeout(), TimeUnit.MILLISECONDS).send(); logger.debug(logMarker, "Http send completed"); if (tryCount < client.getRetries()) { if (client.hasRetryBackOffFunction()) { client.getRetryBackOffFunction().execute(tryCount); } while (request != null && tryCount <= client.getRetries());
@Before public void setup() throws InterruptedException, ExecutionException, TimeoutException { when(loadBalancer.getHealthyInstance()).thenReturn(createServiceEndpoint()); when(loadBalancer.getHealthyInstanceExclude(anyListOf(ServiceEndpoint.class))) .thenReturn(createServiceEndpoint()); when(rpcClient.getRetries()).thenReturn(NUMBER_OF_RETRIES); when(rpcClient.getTimeout()).thenReturn(0); httpClientWrapper.setLoadBalancer(loadBalancer); when(rpcClientMetrics.getMethodTimer(any(), any())).thenReturn(new GoTimer("timer")); when(tracer.buildSpan(any())).thenReturn(spanBuilder); when(spanBuilder.start()).thenReturn(span); when(httpClient.newRequest(any(URI.class))).thenReturn(request); when(httpClient.newRequest(any(String.class))).thenReturn(request); when(request.content(any(ContentProvider.class))).thenReturn(request); when(request.method(anyString())).thenReturn(request); when(request.timeout(anyLong(), any(TimeUnit.class))).thenReturn(request); when(request.send()).thenReturn(httpContentResponse); when(httpContentResponse.getStatus()).thenReturn(100); dependencyHealthCheck = mock(ServiceDependencyHealthCheck.class); }
public RpcClient<RESPONSE> build() { if (StringUtils.isBlank(serviceName)) { throw new IllegalStateException("RpcClientBuilder: Service name was not set"); } if (StringUtils.isBlank(methodName)) { throw new IllegalStateException("RpcClientBuilder: Method name was not set"); } LoadBalancerFactory lbFactory = injector.getInstance(LoadBalancerFactory.class); LoadBalancer loadBalancer = lbFactory.getLoadBalancer(serviceName); return new RpcClient<>(loadBalancer, serviceName, methodName, retries, timeout, retryBackOffFunction, responseClass); }
/** * @deprecated use {@link #callSynchronous(Message, OrangeContext)} instead and make sure to always pass the {@link OrangeContext} */ @Deprecated public RESPONSE callSynchronous(Message request) throws RpcCallException { return callSynchronous(request, null); }
@Override public CallAnotherServiceResponse handleRequest(CallAnotherServiceCommand command, OrangeContext ctx) throws RpcCallException { rpcClient.callSynchronous(CallAnotherServiceCommand.getDefaultInstance(), ctx); return CallAnotherServiceResponse.getDefaultInstance(); }
@Test public void noInstances() { //no service endpoints available int errorCount = 0; try { rpcClient.callSynchronous(FrameworkTest.Foobar.newBuilder().build(), new OrangeContext()); } catch (RpcCallException ex) { errorCount++; assertThat(ex.getCategory()).isEqualTo(RpcCallException.Category.InternalServerError); } assertThat(errorCount).isEqualTo(1); }
withRetries(0).withTimeout(LONG_POLL_TIMEOUT).build(); logger.debug("Calling configuration service, lastChangeIndex = {}", lastChangeIndex.get()); response = client.callSynchronous(configRequest, new OrangeContext()); logger.debug("Got configuration service response"); break;
@Test public void basicHappyPath() throws Exception { //Create 2 healthy endpoints and send 4 requests. //Each endpoint should have processed 2 requests each. loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20001", "dc1", dependencyHealthCheck)); loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20002", "dc1", dependencyHealthCheck)); for (int i = 0; i < 4; i++) { rpcClient.callSynchronous(FrameworkTest.Foobar.newBuilder().build(), new OrangeContext()); } assertThat(httpClient.verifyRequestsProcessed(2, "localhost:20001")).isTrue(); assertThat(httpClient.verifyRequestsProcessed(2, "localhost:20002")).isTrue(); }
@Test public void multipleServicesSingleRetry() { //Create 2 servers and have them always fail. Set retries to 1. Send request. //Verify each server got issued one request. loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20001", "dc1", dependencyHealthCheck)); loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20002", "dc1", dependencyHealthCheck)); httpClient.makeFailing(); int failureCount = 0; try { rpcClient.callSynchronous(FrameworkTest.Foobar.newBuilder().build(), new OrangeContext()); } catch (java.lang.Exception e) { failureCount++; } assertThat(failureCount).isEqualTo(1); assertThat(httpClient.verifyRequestsProcessed(1, "localhost:20001")).isTrue(); assertThat(httpClient.verifyRequestsProcessed(1, "localhost:20002")).isTrue(); }
@Test public void newServiceInstanceAppears() throws Exception { //Create 2 servers and send 4 requests. Each should have processed 2 requests each. //Add a new server. Send 3 more requests. Two servers should have processed 3 //and the third one just 1. loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20001", "dc1", dependencyHealthCheck)); loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20002", "dc1", dependencyHealthCheck)); for (int i = 0; i < 4; i++) { rpcClient.callSynchronous(FrameworkTest.Foobar.newBuilder().build(), new OrangeContext()); } loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20003", "dc1", dependencyHealthCheck)); for (int i = 0; i < 3; i++) { rpcClient.callSynchronous(FrameworkTest.Foobar.newBuilder().build(), new OrangeContext()); } assertThat(httpClient.verifyRequestsProcessed(3, "localhost:20001")).isTrue(); assertThat(httpClient.verifyRequestsProcessed(3, "localhost:20002")).isTrue(); assertThat(httpClient.verifyRequestsProcessed(1, "localhost:20003")).isTrue(); }
@Test public void allInstancesTimeOut() { //Create 2 servers and have both timeout. Set retries to 1. Send request. //The response from the 2nd should be returned. loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20001", "dc1", dependencyHealthCheck)); loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20002", "dc1", dependencyHealthCheck)); rpcClient = clientFactory.newClient(serviceName, "testing", FrameworkTest.Foobar.class). withRetries(1).build(); httpClient.makeRequestsTimeout(); int failureCount = 0; try { rpcClient.callSynchronous(FrameworkTest.Foobar.newBuilder().build(), new OrangeContext()); } catch (RpcCallException ex) { failureCount++; assertThat(ex.getCategory()).isEqualTo(RpcCallException.Category.RequestTimedOut); } assertThat(failureCount).isEqualTo(1); assertThat(httpClient.verifyRequestsProcessed(1, "localhost:20001")).isTrue(); assertThat(httpClient.verifyRequestsProcessed(1, "localhost:20002")).isTrue(); }
@Test public void retriableError() { //Create 2 servers and have both throw an exception that is retriable. //The exception should be thrown locally loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20001", "dc1", dependencyHealthCheck)); loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20002", "dc1", dependencyHealthCheck)); rpcClient = clientFactory.newClient(serviceName, "testing", FrameworkTest.Foobar.class). withRetries(1).build(); httpClient.setResponseException(new RpcCallException(RpcCallException.Category. InternalServerError, "test1234").withSource("testing567")); int failureCount = 0; try { rpcClient.callSynchronous(FrameworkTest.Foobar.newBuilder().build(), new OrangeContext()); } catch (RpcCallException ex) { failureCount++; assertThat(ex.getCategory()).isEqualTo(RpcCallException.Category.InternalServerError); assertThat(ex.getMessage()).isEqualTo("test1234"); assertThat(ex.getSource()).isEqualTo("testing567"); } assertThat(failureCount).isEqualTo(1); assertThat(httpClient.verifyRequestsProcessed(2)).isTrue(); }
@Test public void nonRetriableError() { //Create 2 servers and have the first throw an exception that is not retriable. //The exception should be thrown and not retried with the other node loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20001", "dc1", dependencyHealthCheck)); loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20002", "dc1", dependencyHealthCheck)); rpcClient = clientFactory.newClient(serviceName, "testing", FrameworkTest.Foobar.class). withRetries(1).build(); httpClient.setResponseException(new RpcCallException(RpcCallException.Category. InsufficientPermissions, "test").withSource("testing")); int failureCount = 0; try { rpcClient.callSynchronous(FrameworkTest.Foobar.newBuilder().build(), new OrangeContext()); } catch (RpcCallException ex) { failureCount++; assertThat(ex.getCategory()).isEqualTo(RpcCallException.Category.InsufficientPermissions); assertThat(ex.getMessage()).isEqualTo("test"); } assertThat(failureCount).isEqualTo(1); assertThat(httpClient.verifyRequestsProcessed(1)).isTrue(); }
@Test public void allFailingNoInstanceAvailable() { //Create 3 servers and have them always fail. Set retries to 4. Send request. //Each server should be tried once, then the request should fail. loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20001", "dc1", dependencyHealthCheck)); loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20002", "dc1", dependencyHealthCheck)); loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20003", "dc1", dependencyHealthCheck)); rpcClient = clientFactory.newClient(serviceName, "testing", FrameworkTest.Foobar.class). withRetries(4).build(); httpClient.makeFailing(); int failureCount = 0; try { rpcClient.callSynchronous(FrameworkTest.Foobar.newBuilder().build(), new OrangeContext()); } catch (java.lang.Exception e) { e.printStackTrace(); failureCount++; } assertThat(failureCount).isEqualTo(1); assertThat(httpClient.verifyRequestsProcessed(1, "localhost:20001")).isTrue(); assertThat(httpClient.verifyRequestsProcessed(1, "localhost:20002")).isTrue(); assertThat(httpClient.verifyRequestsProcessed(1, "localhost:20003")).isTrue(); }
@Test public void singleInstanceTimesOut() { //Create 2 servers and have one timeout. Set retries to 1. Send request. //The response from the 2nd should be returned. loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20001", "dc1", dependencyHealthCheck)); loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20002", "dc1", dependencyHealthCheck)); rpcClient = clientFactory.newClient(serviceName, "testing", FrameworkTest.Foobar.class). withRetries(1).build(); httpClient.makeFirstRequestTimeout(); try { rpcClient.callSynchronous(FrameworkTest.Foobar.newBuilder().build(), new OrangeContext()); } catch (java.lang.Exception e) { e.printStackTrace(); } assertThat(httpClient.verifyRequestsProcessed(1, "localhost:20001")).isTrue(); assertThat(httpClient.verifyRequestsProcessed(1, "localhost:20002")).isTrue(); }
@Test public void singleServiceZeroRetries() throws Exception { //Verify retry behavior. Create 1 server and have it always fail. //Set retries to 0. Send a request, and verify server got issued one request. loadBalancer.addServiceEndpoint(new ServiceEndpoint(executor, "localhost:20001", "dc1", dependencyHealthCheck)); rpcClient = clientFactory.newClient(serviceName, "testing", FrameworkTest.Foobar.class). withRetries(0).build(); rpcClient.callSynchronous(FrameworkTest.Foobar.newBuilder().build(), new OrangeContext()); assertThat(httpClient.verifyRequestsProcessed(1, "localhost:20001")).isTrue(); }