use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestBackupRequestsClient method testStatsConsumerLatencyUpdate.
// @Test - Disabled due to flakiness. See SI-3077 to track and resolve this.
public void testStatsConsumerLatencyUpdate() throws Exception {
AtomicReference<ServiceProperties> serviceProperties = new AtomicReference<>();
TestBackupRequestsStrategyStatsConsumer statsConsumer = new TestBackupRequestsStrategyStatsConsumer();
serviceProperties.set(createServiceProperties(null));
BackupRequestsClient client = createClient(serviceProperties::get, statsConsumer, new ConstantResponseTimeDistribution(1, TimeUnit.NANOSECONDS), false);
URI uri = URI.create("d2://testService");
RestRequest restRequest = new RestRequestBuilder(uri).setEntity(CONTENT).build();
RequestContext requestContext = new RequestContext();
requestContext.putLocalAttr(R2Constants.OPERATION, "get");
Future<RestResponse> response = client.restRequest(restRequest, requestContext);
assertEquals(response.get().getStatus(), 200);
List<StatsConsumerEvent> events = statsConsumer.getEvents();
assertEquals(events.size(), 0);
for (int i = 0; i < Short.MAX_VALUE * 4; i++) {
requestContext = new RequestContext();
requestContext.putLocalAttr(R2Constants.OPERATION, "get");
response = client.restRequest(restRequest, requestContext);
assertEquals(response.get().getStatus(), 200);
}
assertEquals(statsConsumer.getLatencyWithBackup().size(), 0);
assertEquals(statsConsumer.getLatencyWithoutBackup().size(), 0);
serviceProperties.set(createServiceProperties(Arrays.asList(createBackupRequestsConfiguration(5, "get"))));
while (statsConsumer.getLatencyWithoutBackup().size() < 1) {
requestContext = new RequestContext();
requestContext.putLocalAttr(R2Constants.OPERATION, "get");
response = client.restRequest(restRequest, requestContext);
assertEquals(response.get().getStatus(), 200);
}
assertEquals(statsConsumer.getLatencyWithoutBackup().size(), 1);
assertEquals(statsConsumer.getLatencyWithBackup().size(), 1);
// allowing 1% imprecision
long expected = statsConsumer.getLatencyWithoutBackup().get(0).getTotalCount();
long actual = statsConsumer.getLatencyWithBackup().get(0).getTotalCount();
assertTrue(actual > expected * .99 && actual < expected * 1.01, "Expected: " + expected + "+-" + (expected * .01) + ", but actual: " + actual);
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class ProfileClientExample method sendTraffic.
private static void sendTraffic(Map<String, Map<String, Long>> trafficProportion, D2Client d2Client, Long delay) throws Exception {
for (Map.Entry<String, Map<String, Long>> d2Service : trafficProportion.entrySet()) {
for (Map.Entry<String, Long> partition : d2Service.getValue().entrySet()) {
final URI uri = new URI("d2://" + d2Service.getKey() + "?partitionId=" + partition.getKey());
RestRequestBuilder requestBuilder = new RestRequestBuilder(uri).setMethod("get");
if (delay != null) {
requestBuilder.setHeader("delay", delay.toString());
}
RestRequest request = requestBuilder.build();
Long queryPerSecond = partition.getValue();
for (int i = 0; i < queryPerSecond; i++) {
// we don't care about the result from the server after all,
// you can see the traffic hits the echo server from stdout
d2Client.restRequest(request, new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
System.err.println("URI = " + uri.toString() + " didn't get any response");
}
@Override
public void onSuccess(RestResponse result) {
System.out.println("URI = " + uri.toString() + " was served by " + result.getEntity().asString("UTF-8"));
}
});
}
}
}
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class ExampleD2Client method sendTraffic.
private static void sendTraffic(Map<String, Long> trafficProportion, D2Client d2Client) throws URISyntaxException {
for (Map.Entry<String, Long> entry : trafficProportion.entrySet()) {
URI uri = new URI("d2://" + entry.getKey());
RestRequest request = new RestRequestBuilder(uri).setMethod("get").build();
for (long i = 0; i < entry.getValue(); i++) {
// we don't care about the result from the server after all,
// you can see the traffic hits the echo server from stdout
d2Client.restRequest(request);
}
}
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class EmailClientExample method sendTraffic.
private static void sendTraffic(Map<String, Long> trafficProportion, D2Client d2Client) throws Exception {
for (Map.Entry<String, Long> proportion : trafficProportion.entrySet()) {
long queryPerSecond = proportion.getValue();
String serviceName = proportion.getKey();
Random random = new Random();
for (int i = 0; i < queryPerSecond; i++) {
final URI uri = new URI("d2://" + serviceName + "?user=" + random.nextInt());
RestRequestBuilder requestBuilder = new RestRequestBuilder(uri).setMethod("get");
RestRequest request = requestBuilder.build();
// we don't care about the result from the server after all,
// you can see the traffic hits the echo server from stdout
d2Client.restRequest(request, new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
System.err.println("URI = " + uri.toString() + " didn't get any response");
}
@Override
public void onSuccess(RestResponse result) {
System.out.println("URI = " + uri.toString() + " was served by " + result.getEntity().asString("UTF-8"));
}
});
}
}
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestChannelPoolBehavior method testChannelReuse.
@Test
public void testChannelReuse() throws Exception {
_client2.streamRequest(new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, NOT_FOUND_URI)).build(EntityStreams.newEntityStream(new SlowWriter())), new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
if (e instanceof StreamException) {
StreamException streamException = (StreamException) e;
streamException.getResponse().getEntityStream().setReader(new CancelingReader());
}
throw new RuntimeException(e);
}
@Override
public void onSuccess(StreamResponse result) {
result.getEntityStream().setReader(new DrainReader());
}
});
Future<RestResponse> responseFuture = _client2.restRequest(new RestRequestBuilder(Bootstrap.createHttpURI(PORT, NORMAL_URI)).build());
RestResponse response = responseFuture.get(WRITER_DELAY * 1000, TimeUnit.MILLISECONDS);
Assert.assertEquals(response.getStatus(), RestStatus.OK);
}
Aggregations