Search in sources :

Example 21 with LoadBalancer

use of com.linkedin.d2.balancer.LoadBalancer in project rest.li by linkedin.

the class WarmUpLoadBalancerTest method testDeletingFilesAfterShutdown.

@Test(timeOut = 10000, groups = { "ci-flaky" })
public void testDeletingFilesAfterShutdown() throws InterruptedException, ExecutionException, TimeoutException {
    createDefaultServicesIniFiles();
    TestLoadBalancer balancer = new TestLoadBalancer();
    List<String> allServicesBeforeShutdown = getAllDownstreamServices();
    List<String> partialServices = getPartialDownstreams();
    DownstreamServicesFetcher returnPartialDownstreams = callback -> callback.onSuccess(partialServices);
    LoadBalancer warmUpLoadBalancer = new WarmUpLoadBalancer(balancer, balancer, Executors.newSingleThreadScheduledExecutor(), _tmpdir.getAbsolutePath(), MY_SERVICES_FS, returnPartialDownstreams, WarmUpLoadBalancer.DEFAULT_SEND_REQUESTS_TIMEOUT_SECONDS, WarmUpLoadBalancer.DEFAULT_CONCURRENT_REQUESTS);
    FutureCallback<None> callback = new FutureCallback<>();
    warmUpLoadBalancer.start(callback);
    callback.get(5000, TimeUnit.MILLISECONDS);
    FutureCallback<None> shutdownCallback = new FutureCallback<>();
    warmUpLoadBalancer.shutdown(() -> shutdownCallback.onSuccess(None.none()));
    shutdownCallback.get(5000, TimeUnit.MILLISECONDS);
    List<String> allServicesAfterShutdown = getAllDownstreamServices();
    Assert.assertTrue(allServicesBeforeShutdown.size() > partialServices.size(), "After shutdown the unused services should have been deleted. Expected lower number of:" + allServicesBeforeShutdown.size() + ", actual " + partialServices.size());
    Assert.assertTrue(partialServices.containsAll(allServicesAfterShutdown) && allServicesAfterShutdown.containsAll(partialServices), "There should be just the services that were passed by the partial fetcher");
}
Also used : Arrays(java.util.Arrays) FutureCallback(com.linkedin.common.callback.FutureCallback) DownstreamServicesFetcher(com.linkedin.d2.balancer.util.downstreams.DownstreamServicesFetcher) URISyntaxException(java.net.URISyntaxException) BeforeMethod(org.testng.annotations.BeforeMethod) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) FSBasedDownstreamServicesFetcher(com.linkedin.d2.balancer.util.downstreams.FSBasedDownstreamServicesFetcher) File(java.io.File) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) RequestContext(com.linkedin.r2.message.RequestContext) Assert(org.testng.Assert) None(com.linkedin.common.util.None) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoadBalancer(com.linkedin.d2.balancer.LoadBalancer) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) Collections(java.util.Collections) DownstreamServicesFetcher(com.linkedin.d2.balancer.util.downstreams.DownstreamServicesFetcher) FSBasedDownstreamServicesFetcher(com.linkedin.d2.balancer.util.downstreams.FSBasedDownstreamServicesFetcher) LoadBalancer(com.linkedin.d2.balancer.LoadBalancer) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 22 with LoadBalancer

use of com.linkedin.d2.balancer.LoadBalancer in project rest.li by linkedin.

the class WarmUpLoadBalancerTest method testThrottlingUnlimitedRequests.

/**
 * Tests that if the requests are not throttled it makes a large amount of concurrent calls
 */
@Test(timeOut = 10000)
public void testThrottlingUnlimitedRequests() throws URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
    int NRequests = 500;
    createNServicesIniFiles(NRequests);
    int concurrentRequestsHugeNumber = 999999999;
    int concurrentRequestsCheckHigher = WarmUpLoadBalancer.DEFAULT_CONCURRENT_REQUESTS;
    TestLoadBalancer balancer = new TestLoadBalancer(50);
    AtomicInteger requestCount = balancer.getRequestCount();
    LoadBalancer warmUpLoadBalancer = new WarmUpLoadBalancer(balancer, balancer, Executors.newSingleThreadScheduledExecutor(), _tmpdir.getAbsolutePath(), MY_SERVICES_FS, _FSBasedDownstreamServicesFetcher, WarmUpLoadBalancer.DEFAULT_SEND_REQUESTS_TIMEOUT_SECONDS, concurrentRequestsHugeNumber);
    FutureCallback<None> callback = new FutureCallback<>();
    warmUpLoadBalancer.start(callback);
    boolean triggeredAtLeastOnce = false;
    while (!callback.isDone()) {
        int currentConcurrentRequests = balancer.getRequestCount().get() - balancer.getCompletedRequestCount().get();
        if (currentConcurrentRequests > concurrentRequestsCheckHigher) {
            triggeredAtLeastOnce = true;
        }
        Thread.sleep(50);
    }
    Assert.assertTrue(triggeredAtLeastOnce);
    Assert.assertEquals(NRequests, requestCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoadBalancer(com.linkedin.d2.balancer.LoadBalancer) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 23 with LoadBalancer

use of com.linkedin.d2.balancer.LoadBalancer in project rest.li by linkedin.

the class WarmUpLoadBalancerTest method testHitTimeout.

@Test(timeOut = 10000)
public void testHitTimeout() throws URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
    int NRequests = 5000;
    int warmUpTimeout = 2;
    int concurrentRequests = 5;
    int requestTime = 100;
    float requestsPerSecond = 1000 / requestTime * concurrentRequests;
    int expectedRequests = (int) (requestsPerSecond * warmUpTimeout);
    // we allow inaccuracies of 1s
    int deviation = (int) requestsPerSecond;
    createNServicesIniFiles(NRequests);
    TestLoadBalancer balancer = new TestLoadBalancer(requestTime);
    AtomicInteger requestCount = balancer.getRequestCount();
    LoadBalancer warmUpLoadBalancer = new WarmUpLoadBalancer(balancer, balancer, Executors.newSingleThreadScheduledExecutor(), _tmpdir.getAbsolutePath(), MY_SERVICES_FS, _FSBasedDownstreamServicesFetcher, warmUpTimeout, concurrentRequests);
    FutureCallback<None> callback = new FutureCallback<>();
    warmUpLoadBalancer.start(callback);
    callback.get();
    Assert.assertTrue(expectedRequests - deviation < requestCount.get() && expectedRequests + deviation > requestCount.get(), "Expected # of requests between " + expectedRequests + " +/-" + deviation + ", found:" + requestCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoadBalancer(com.linkedin.d2.balancer.LoadBalancer) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 24 with LoadBalancer

use of com.linkedin.d2.balancer.LoadBalancer in project rest.li by linkedin.

the class WarmUpLoadBalancerTest method testNotDeletingFilesGetClient.

/**
 * Since the list might from the fetcher might not be complete (update service, old data, etc.., and the user might
 * require additional services at runtime, we have to check that those services are not cleared from the cache
 * otherwise it would incur in a penalty at the next deployment
 */
@Test(timeOut = 10000)
public void testNotDeletingFilesGetClient() throws InterruptedException, ExecutionException, TimeoutException, ServiceUnavailableException {
    createDefaultServicesIniFiles();
    TestLoadBalancer balancer = new TestLoadBalancer();
    List<String> allServicesBeforeShutdown = getAllDownstreamServices();
    DownstreamServicesFetcher returnNoDownstreams = callback -> callback.onSuccess(Collections.emptyList());
    String pickOneService = allServicesBeforeShutdown.get(0);
    LoadBalancer warmUpLoadBalancer = new WarmUpLoadBalancer(balancer, balancer, Executors.newSingleThreadScheduledExecutor(), _tmpdir.getAbsolutePath(), MY_SERVICES_FS, returnNoDownstreams, WarmUpLoadBalancer.DEFAULT_SEND_REQUESTS_TIMEOUT_SECONDS, WarmUpLoadBalancer.DEFAULT_CONCURRENT_REQUESTS);
    FutureCallback<None> callback = new FutureCallback<>();
    warmUpLoadBalancer.start(callback);
    callback.get(5000, TimeUnit.MILLISECONDS);
    warmUpLoadBalancer.getClient(new URIRequest("d2://" + pickOneService), new RequestContext());
    FutureCallback<None> shutdownCallback = new FutureCallback<>();
    warmUpLoadBalancer.shutdown(() -> shutdownCallback.onSuccess(None.none()));
    shutdownCallback.get(5000, TimeUnit.MILLISECONDS);
    List<String> allServicesAfterShutdown = getAllDownstreamServices();
    Assert.assertEquals(1, allServicesAfterShutdown.size(), "After shutdown there should be just one service, the one that we 'get the client' on");
}
Also used : Arrays(java.util.Arrays) FutureCallback(com.linkedin.common.callback.FutureCallback) DownstreamServicesFetcher(com.linkedin.d2.balancer.util.downstreams.DownstreamServicesFetcher) URISyntaxException(java.net.URISyntaxException) BeforeMethod(org.testng.annotations.BeforeMethod) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) FSBasedDownstreamServicesFetcher(com.linkedin.d2.balancer.util.downstreams.FSBasedDownstreamServicesFetcher) File(java.io.File) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) RequestContext(com.linkedin.r2.message.RequestContext) Assert(org.testng.Assert) None(com.linkedin.common.util.None) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoadBalancer(com.linkedin.d2.balancer.LoadBalancer) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) Collections(java.util.Collections) DownstreamServicesFetcher(com.linkedin.d2.balancer.util.downstreams.DownstreamServicesFetcher) FSBasedDownstreamServicesFetcher(com.linkedin.d2.balancer.util.downstreams.FSBasedDownstreamServicesFetcher) LoadBalancer(com.linkedin.d2.balancer.LoadBalancer) RequestContext(com.linkedin.r2.message.RequestContext) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 25 with LoadBalancer

use of com.linkedin.d2.balancer.LoadBalancer in project rest.li by linkedin.

the class WarmUpLoadBalancerTest method testThrottling.

@Test(timeOut = 10000)
public void testThrottling() throws InterruptedException {
    int NRequests = 100;
    createNServicesIniFiles(NRequests);
    TestLoadBalancer balancer = new TestLoadBalancer(50);
    AtomicInteger requestCount = balancer.getRequestCount();
    LoadBalancer warmUpLoadBalancer = new WarmUpLoadBalancer(balancer, balancer, Executors.newSingleThreadScheduledExecutor(), _tmpdir.getAbsolutePath(), MY_SERVICES_FS, _FSBasedDownstreamServicesFetcher, WarmUpLoadBalancer.DEFAULT_SEND_REQUESTS_TIMEOUT_SECONDS, WarmUpLoadBalancer.DEFAULT_CONCURRENT_REQUESTS);
    FutureCallback<None> callback = new FutureCallback<>();
    warmUpLoadBalancer.start(callback);
    boolean triggeredAtLeastOnce = false;
    while (!callback.isDone()) {
        triggeredAtLeastOnce = true;
        int currentConcurrentRequests = balancer.getRequestCount().get() - balancer.getCompletedRequestCount().get();
        if (currentConcurrentRequests > WarmUpLoadBalancer.DEFAULT_CONCURRENT_REQUESTS) {
            Assert.fail("The concurrent requests (" + currentConcurrentRequests + ") are greater than the allowed (" + WarmUpLoadBalancer.DEFAULT_CONCURRENT_REQUESTS + ")");
        }
        Thread.sleep(50);
    }
    Assert.assertTrue(triggeredAtLeastOnce);
    Assert.assertEquals(NRequests, requestCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoadBalancer(com.linkedin.d2.balancer.LoadBalancer) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)22 None (com.linkedin.common.util.None)17 FutureCallback (com.linkedin.common.callback.FutureCallback)16 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)13 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)13 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)13 MockStore (com.linkedin.d2.discovery.stores.mock.MockStore)12 RequestContext (com.linkedin.r2.message.RequestContext)11 HashMap (java.util.HashMap)11 LoadBalancer (com.linkedin.d2.balancer.LoadBalancer)9 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)9 LoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory)9 RandomLoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory)9 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)9 ArrayList (java.util.ArrayList)9 DarkClusterConfigMap (com.linkedin.d2.DarkClusterConfigMap)8 DegraderLoadBalancerStrategyFactoryV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3)8 URI (java.net.URI)8 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)8 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)7