use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestBackupRequestsClient method testRequestWithHint.
/**
* Backup Request should still work when a hint is given together with the flag indicating that the hint is only a preference, not requirement.
*/
// Appears to be flaky in CI
@Test(invocationCount = 3, dataProvider = "isD2Async", retryAnalyzer = SingleRetry.class)
public void testRequestWithHint(boolean isD2Async) throws Exception {
// 1s till response comes back
int responseDelayNano = 100000000;
// make backup request after 0.5 second
int backupDelayNano = 50000000;
Deque<URI> hostsReceivingRequest = new ConcurrentLinkedDeque<>();
BackupRequestsClient client = createAlwaysBackupClientWithHosts(Arrays.asList("http://test1.com:123", "http://test2.com:123"), hostsReceivingRequest, responseDelayNano, backupDelayNano, isD2Async);
URI uri = URI.create("d2://testService");
RestRequest restRequest = new RestRequestBuilder(uri).setEntity(CONTENT).build();
RequestContext context = new RequestContext();
context.putLocalAttr(R2Constants.OPERATION, "get");
// case 1: no hint, backup request should be made normally
RequestContext context1 = context.clone();
Future<RestResponse> response1 = client.restRequest(restRequest, context1);
assertEquals(response1.get().getStatus(), 200);
assertEquals(hostsReceivingRequest.size(), 2);
assertEquals(new HashSet<>(hostsReceivingRequest).size(), 2);
hostsReceivingRequest.clear();
// case 2: hint specified but won't accept other host, backup request will not be made.
RequestContext context2 = context.clone();
URI hint = new URI("http://test1.com:123");
KeyMapper.TargetHostHints.setRequestContextTargetHost(context2, hint);
Future<RestResponse> response2 = client.restRequest(restRequest, context2);
assertEquals(response2.get().getStatus(), 200);
assertEquals(hostsReceivingRequest.size(), 1);
assertEquals(hostsReceivingRequest.poll(), hint);
hostsReceivingRequest.clear();
// case 3: hint specified and set flag to accept other host, backup request will be made to a different host.
RequestContext context3 = context.clone();
KeyMapper.TargetHostHints.setRequestContextTargetHost(context3, hint);
KeyMapper.TargetHostHints.setRequestContextOtherHostAcceptable(context3, true);
Future<RestResponse> response3 = client.restRequest(restRequest, context3);
assertEquals(response3.get().getStatus(), 200);
assertEquals(hostsReceivingRequest.size(), 2);
// The first request should be made to the hinted host while the second should go to the other.
assertEquals(hostsReceivingRequest.toArray(), new URI[] { new URI("http://test1.com:123"), new URI("http://test2.com:123") });
assertEquals(new HashSet<>(hostsReceivingRequest).size(), 2);
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class DegraderTrackerClientTest method testCallTrackingRestRequest.
@Test
public void testCallTrackingRestRequest() throws Exception {
URI uri = URI.create("http://test.qa.com:1234/foo");
SettableClock clock = new SettableClock();
AtomicInteger action = new AtomicInteger(0);
TransportClient tc = new TransportClient() {
@Override
public void restRequest(RestRequest request, RequestContext requestContext, Map<String, String> wireAttrs, TransportCallback<RestResponse> callback) {
clock.addDuration(5);
switch(action.get()) {
// success
case 0:
callback.onResponse(TransportResponseImpl.success(RestResponseFactory.noResponse()));
break;
// fail with rest exception
case 1:
callback.onResponse(TransportResponseImpl.error(RestException.forError(500, "rest exception")));
break;
// fail with timeout exception
case 2:
callback.onResponse(TransportResponseImpl.error(new RemoteInvocationException(new TimeoutException())));
break;
// fail with other exception
default:
callback.onResponse(TransportResponseImpl.error(new RuntimeException()));
break;
}
}
@Override
public void shutdown(Callback<None> callback) {
}
};
DegraderTrackerClientImpl client = (DegraderTrackerClientImpl) createTrackerClient(tc, clock, uri);
CallTracker callTracker = client.getCallTracker();
CallTracker.CallStats stats;
DegraderControl degraderControl = client.getDegraderControl(DefaultPartitionAccessor.DEFAULT_PARTITION_ID);
client.restRequest(new RestRequestBuilder(uri).build(), new RequestContext(), new HashMap<>(), new TestTransportCallback<>());
clock.addDuration(5000);
stats = callTracker.getCallStats();
Assert.assertEquals(stats.getCallCount(), 1);
Assert.assertEquals(stats.getErrorCount(), 0);
Assert.assertEquals(stats.getCallCountTotal(), 1);
Assert.assertEquals(stats.getErrorCountTotal(), 0);
Assert.assertEquals(degraderControl.getCurrentComputedDropRate(), 0.0, 0.001);
action.set(1);
client.restRequest(new RestRequestBuilder(uri).build(), new RequestContext(), new HashMap<>(), new TestTransportCallback<>());
clock.addDuration(5000);
stats = callTracker.getCallStats();
Assert.assertEquals(stats.getCallCount(), 1);
Assert.assertEquals(stats.getErrorCount(), 1);
Assert.assertEquals(stats.getCallCountTotal(), 2);
Assert.assertEquals(stats.getErrorCountTotal(), 1);
Assert.assertEquals(degraderControl.getCurrentComputedDropRate(), 0.2, 0.001);
action.set(2);
client.restRequest(new RestRequestBuilder(uri).build(), new RequestContext(), new HashMap<>(), new TestTransportCallback<>());
clock.addDuration(5000);
stats = callTracker.getCallStats();
Assert.assertEquals(stats.getCallCount(), 1);
Assert.assertEquals(stats.getErrorCount(), 1);
Assert.assertEquals(stats.getCallCountTotal(), 3);
Assert.assertEquals(stats.getErrorCountTotal(), 2);
Assert.assertEquals(degraderControl.getCurrentComputedDropRate(), 0.4, 0.001);
action.set(3);
client.restRequest(new RestRequestBuilder(uri).build(), new RequestContext(), new HashMap<>(), new TestTransportCallback<>());
clock.addDuration(5000);
stats = callTracker.getCallStats();
Assert.assertEquals(stats.getCallCount(), 1);
Assert.assertEquals(stats.getErrorCount(), 1);
Assert.assertEquals(stats.getCallCountTotal(), 4);
Assert.assertEquals(stats.getErrorCountTotal(), 3);
Assert.assertEquals(degraderControl.getCurrentComputedDropRate(), 0.2, 0.001);
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class DegraderTrackerClientTest method testClientRestRequest.
@Test(groups = { "small", "back-end" })
public void testClientRestRequest() throws URISyntaxException {
URI uri = URI.create("http://test.qa.com:1234/foo");
double weight = 3d;
TestClient wrappedClient = new TestClient();
Clock clock = new SettableClock();
Map<Integer, PartitionData> partitionDataMap = createDefaultPartitionData(3d);
DegraderTrackerClient client = new DegraderTrackerClientImpl(uri, partitionDataMap, wrappedClient, clock, null);
Assert.assertEquals(client.getUri(), uri);
Double clientWeight = client.getPartitionWeight(DefaultPartitionAccessor.DEFAULT_PARTITION_ID);
Assert.assertEquals(clientWeight, weight);
Assert.assertEquals(client.getTransportClient(), wrappedClient);
RestRequest restRequest = new RestRequestBuilder(uri).build();
Map<String, String> restWireAttrs = new HashMap<>();
TestTransportCallback<RestResponse> restCallback = new TestTransportCallback<>();
client.restRequest(restRequest, new RequestContext(), restWireAttrs, restCallback);
Assert.assertFalse(restCallback.response.hasError());
Assert.assertEquals(wrappedClient.restRequest, restRequest);
Assert.assertEquals(wrappedClient.restWireAttrs, restWireAttrs);
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestDarkClusterFilter method testDarkClusterAssemblyNoDarkCluster.
@Test
public void testDarkClusterAssemblyNoDarkCluster() {
_darkClusterStrategyFactory.start();
RestRequest restRequest = new RestRequestBuilder(URI.create("/foo/1")).build();
// no dark clusters have been added, so no request should be sent.
_darkClusterFilter.onRestRequest(restRequest, new RequestContext(), new HashMap<>(), new DummyNextFilter());
Assert.assertEquals(_client.requestAuthorityMap.size(), 0, "expected zero requests to be sent because no dark clusters");
_darkClusterFilter.onRestError(new RuntimeException("test"), new RequestContext(), new HashMap<>(), new DummyNextFilter());
_darkClusterFilter.onRestResponse(new RestResponseBuilder().build(), new RequestContext(), new HashMap<>(), new DummyNextFilter());
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestDarkClusterStrategyFactory method testCreateStrategiesWithNoDarkClusters.
@Test
public void testCreateStrategiesWithNoDarkClusters() {
DarkClusterStrategy strategy = _strategyFactory.get(DARK_CLUSTER_NAME);
RestRequest dummyRestRequest = new RestRequestBuilder(URI.create("foo")).build();
boolean requestSent = strategy.handleRequest(dummyRestRequest, dummyRestRequest, new RequestContext());
Assert.assertTrue(strategy instanceof NoOpDarkClusterStrategy);
Assert.assertFalse(requestSent, "default empty strategy should not send request");
}
Aggregations