use of com.linkedin.r2.transport.http.client.EvictingCircularBuffer in project rest.li by linkedin.
the class TestConstantQpsDarkClusterStrategy method testStrategy.
@Test(dataProvider = "qpsKeys")
public void testStrategy(int duration, float inboundQps, float outboundQps, int numSourceInstances, int numDarkInstances) {
IntStream.of(1, 1000, 1000000).forEach(capacity -> {
DarkClusterDispatcher darkClusterDispatcher = new DefaultDarkClusterDispatcher(new MockClient(false));
ClockedExecutor executor = new ClockedExecutor();
Supplier<ConstantQpsRateLimiter> uniqueRateLimiterSupplier = () -> {
EvictingCircularBuffer uniqueBuffer = TestConstantQpsDarkClusterStrategy.getBuffer(executor);
ConstantQpsRateLimiter limiter = new ConstantQpsRateLimiter(executor, executor, executor, uniqueBuffer);
limiter.setBufferCapacity(capacity);
limiter.setBufferTtl(Integer.MAX_VALUE, ChronoUnit.DAYS);
return limiter;
};
ConstantQpsRateLimiter sharedRateLimiter = uniqueRateLimiterSupplier.get();
Supplier<ConstantQpsRateLimiter> sharedRateLimiterSupplier = () -> sharedRateLimiter;
MockClusterInfoProvider mockClusterInfoProvider = new MockClusterInfoProvider();
mockClusterInfoProvider.putHttpsClusterCount(SOURCE_CLUSTER_NAME, numSourceInstances);
// dark cluster 1
BaseDarkClusterDispatcherImpl baseDispatcherOne = new BaseDarkClusterDispatcherImpl(DARK_CLUSTER_NAME_ONE, darkClusterDispatcher, new DoNothingNotifier(), new CountingVerifierManager());
mockClusterInfoProvider.putHttpsClusterCount(DARK_CLUSTER_NAME_ONE, numDarkInstances);
ConstantQpsRateLimiter rateLimiterOne = sharedRateLimiterSupplier.get();
// dark cluster 2
BaseDarkClusterDispatcherImpl baseDispatcherTwo = new BaseDarkClusterDispatcherImpl(DARK_CLUSTER_NAME_TWO, darkClusterDispatcher, new DoNothingNotifier(), new CountingVerifierManager());
mockClusterInfoProvider.putHttpsClusterCount(DARK_CLUSTER_NAME_TWO, numDarkInstances);
ConstantQpsRateLimiter rateLimiterTwo = sharedRateLimiterSupplier.get();
// dark cluster 3
BaseDarkClusterDispatcherImpl baseDispatcherThree = new BaseDarkClusterDispatcherImpl(DARK_CLUSTER_NAME_THREE, darkClusterDispatcher, new DoNothingNotifier(), new CountingVerifierManager());
mockClusterInfoProvider.putHttpsClusterCount(DARK_CLUSTER_NAME_THREE, numDarkInstances);
ConstantQpsRateLimiter rateLimiterThree = uniqueRateLimiterSupplier.get();
List<ConstantQpsDarkClusterStrategy> strategies = new ArrayList<>();
strategies.add(new ConstantQpsDarkClusterStrategy(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME_ONE, outboundQps, baseDispatcherOne, new DoNothingNotifier(), mockClusterInfoProvider, rateLimiterOne));
strategies.add(new ConstantQpsDarkClusterStrategy(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME_TWO, outboundQps, baseDispatcherTwo, new DoNothingNotifier(), mockClusterInfoProvider, rateLimiterTwo));
strategies.add(new ConstantQpsDarkClusterStrategy(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME_THREE, outboundQps, baseDispatcherThree, new DoNothingNotifier(), mockClusterInfoProvider, rateLimiterThree));
// simulate receiving the configured qps while dispatching over the duration
int msBetweenEachInboundRequest = (int) (1000 / inboundQps);
for (int runTime = 0; runTime < duration; runTime = runTime + msBetweenEachInboundRequest) {
RestRequest dummyRestRequest = new RestRequestBuilder(URI.create("foo")).build();
for (ConstantQpsDarkClusterStrategy strategy : strategies) {
strategy.handleRequest(dummyRestRequest, dummyRestRequest, new RequestContext());
}
executor.runFor(msBetweenEachInboundRequest);
}
double validation = ((duration == 0 ? 0 : duration / 1000.0) * outboundQps * numDarkInstances) / (double) (numSourceInstances);
// Cluster One and Two share a rate limiter, so their combined QPS should match the expected value.
int actualCountClusterOne = baseDispatcherOne.getRequestCount();
int actualCountClusterTwo = baseDispatcherTwo.getRequestCount();
int expectedCountClusterOneAndTwo = (int) validation;
Assert.assertEquals(actualCountClusterOne + actualCountClusterTwo, expectedCountClusterOneAndTwo, expectedCountClusterOneAndTwo * ERR_PCT, "count not within expected range");
// Cluster Three uses its own so it matches the expected value on its own.
int expectedCountClusterThree = (int) validation;
int actualCountClusterThree = baseDispatcherThree.getRequestCount();
Assert.assertEquals(actualCountClusterThree, expectedCountClusterThree, expectedCountClusterThree * ERR_PCT, "count not within expected range");
});
}
Aggregations