use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.
the class TestScatterGather method testSendGetKVSGRequests.
private static void testSendGetKVSGRequests(ScatterGatherBuilder<Greeting> sg, Long[] requestIds) throws ServiceUnavailableException, InterruptedException {
Collection<ScatterGatherBuilder.KVRequestInfo<Long, Greeting>> scatterGatherRequests = buildScatterGatherGetKVRequests(sg, requestIds);
final Map<Long, Greeting> results = new ConcurrentHashMap<Long, Greeting>();
final CountDownLatch latch = new CountDownLatch(scatterGatherRequests.size());
final List<Throwable> errors = new ArrayList<Throwable>();
final List<BatchKVResponse<Long, Greeting>> responses = new ArrayList<BatchKVResponse<Long, Greeting>>();
for (ScatterGatherBuilder.KVRequestInfo<Long, Greeting> requestInfo : scatterGatherRequests) {
Callback<Response<BatchKVResponse<Long, Greeting>>> cb = new Callback<Response<BatchKVResponse<Long, Greeting>>>() {
@Override
public void onSuccess(Response<BatchKVResponse<Long, Greeting>> response) {
results.putAll(response.getEntity().getResults());
synchronized (responses) {
responses.add(response.getEntity());
}
latch.countDown();
}
@Override
public void onError(Throwable e) {
synchronized (errors) {
errors.add(e);
}
latch.countDown();
}
};
REST_CLIENT.sendRequest(requestInfo.getRequest(), requestInfo.getRequestContext(), cb);
}
latch.await();
if (!errors.isEmpty()) {
Assert.fail("Errors in scatter/gather: " + errors.toString());
}
Assert.assertEquals(results.values().size(), requestIds.length);
Set<Set<Long>> responseIdSets = new HashSet<Set<Long>>();
Set<Long> responseIds = new HashSet<Long>();
for (BatchKVResponse<Long, Greeting> response : responses) {
Set<Long> theseIds = response.getResults().keySet();
//no duplicate requests
Assert.assertFalse(responseIdSets.contains(theseIds));
for (Long id : theseIds) {
//no duplicate ids
Assert.assertFalse(responseIds.contains(id));
responseIds.add(id);
}
responseIdSets.add(theseIds);
}
Assert.assertTrue(responseIds.containsAll(Arrays.asList(requestIds)));
Assert.assertEquals(responseIds.size(), requestIds.length);
}
use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.
the class TestScatterGather method generateCreate.
private static List<Greeting> generateCreate(int num) {
List<Greeting> creates = new ArrayList<Greeting>();
for (int i = 0; i < num; ++i) {
Greeting greeting = new Greeting();
greeting.setMessage("create message").setTone(Tone.FRIENDLY);
creates.add(greeting);
}
return creates;
}
use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.
the class TestScatterGather method testBuildSGGetEntityRequests.
private static void testBuildSGGetEntityRequests(int numEndpoints, ScatterGatherBuilder<Greeting> sg, Long[] ids) throws ServiceUnavailableException {
Collection<ScatterGatherBuilder.KVRequestInfo<Long, EntityResponse<Greeting>>> requests = buildScatterGatherGetEntityRequests(sg, ids);
Assert.assertEquals(requests.size(), numEndpoints);
Set<Set<String>> requestIdSets = new HashSet<Set<String>>();
Set<Long> requestIds = new HashSet<Long>();
for (ScatterGatherBuilder.KVRequestInfo<Long, EntityResponse<Greeting>> requestInfo : requests) {
//URI will be something like "greetings/?ids=21&ids=4&ids=53&ids=60&ids=66&ids=88&ids=93&foo=bar"
BatchRequest<BatchKVResponse<Long, EntityResponse<Greeting>>> request = requestInfo.getRequest();
Set<String> expectedParams = new HashSet<String>();
expectedParams.add(RestConstants.QUERY_BATCH_IDS_PARAM);
expectedParams.add("foo");
expectedParams.add(RestConstants.FIELDS_PARAM);
Set<PathSpec> expectedFields = Collections.singleton(new PathSpec("message"));
testGetEntityRequest(request, expectedParams, expectedFields, null, requestIdSets, requestIds);
}
Assert.assertTrue(requestIds.containsAll(Arrays.asList(ids)));
Assert.assertEquals(requestIds.size(), ids.length);
}
use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.
the class TestScatterGather method testScatterGatherLoadBalancerIntegration.
@Test(dataProvider = "requestBuilderDataProvider")
public static void testScatterGatherLoadBalancerIntegration(RootBuilderWrapper<Long, Greeting> builders) throws Exception {
SimpleLoadBalancer loadBalancer = MockLBFactory.createLoadBalancer();
KeyMapper keyMapper = new ConsistentHashKeyMapper(loadBalancer, new TestPartitionInfoProvider());
try {
keyMapper.mapKeysV2(URI.create("http://badurischeme/"), new HashSet<String>());
Assert.fail("keyMapper should reject non-D2 URI scheme");
} catch (IllegalArgumentException e) {
//expected
}
ScatterGatherBuilder<Greeting> sg = new ScatterGatherBuilder<Greeting>(keyMapper);
final int NUM_IDS = 20;
Long[] requestIds = generateIds(NUM_IDS);
Collection<ScatterGatherBuilder.RequestInfo<Greeting>> scatterGatherRequests = buildScatterGatherGetRequests(sg, requestIds);
}
use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.
the class TestScatterGather method testSendSGRequests.
@Test(dataProvider = "requestBuilderDataProvider")
public static void testSendSGRequests(RootBuilderWrapper<Long, Greeting> builders) throws URISyntaxException, InterruptedException, RemoteInvocationException {
final int NUM_ENDPOINTS = 4;
ConsistentHashKeyMapper mapper = getKeyToHostMapper(NUM_ENDPOINTS);
ScatterGatherBuilder<Greeting> sg = new ScatterGatherBuilder<Greeting>(mapper);
final int NUM_IDS = 20;
List<Greeting> entities = generateCreate(NUM_IDS);
Long[] requestIds = prepareData(entities, builders.getRequestOptions());
testSendGetSGRequests(sg, requestIds);
testSendGetKVSGRequests(sg, requestIds);
Map<Long, Greeting> input = generateUpdates(requestIds);
testSendSGUpdateRequests(sg, input, builders);
testSendSGDeleteRequests(sg, requestIds, builders);
}
Aggregations