Search in sources :

Example 11 with DarkClusterConfig

use of com.linkedin.d2.DarkClusterConfig in project rest.li by linkedin.

the class SimpleLoadBalancerTest method testClusterInfoProviderGetDarkClustersNoUris.

@Test
public void testClusterInfoProviderGetDarkClustersNoUris() throws InterruptedException, ExecutionException, ServiceUnavailableException {
    MockStore<ServiceProperties> serviceRegistry = new MockStore<>();
    MockStore<ClusterProperties> clusterRegistry = new MockStore<>();
    MockStore<UriProperties> uriRegistry = new MockStore<>();
    SimpleLoadBalancer loadBalancer = setupLoadBalancer(null, serviceRegistry, clusterRegistry, uriRegistry);
    DarkClusterConfig darkClusterConfig = new DarkClusterConfig().setMultiplier(1.0f).setDispatcherOutboundTargetRate(1).setDispatcherMaxRequestsToBuffer(1).setDispatcherBufferedRequestExpiryInSeconds(1);
    DarkClusterConfigMap darkClusterConfigMap = new DarkClusterConfigMap();
    darkClusterConfigMap.put(DARK_CLUSTER1_NAME, darkClusterConfig);
    clusterRegistry.put(CLUSTER1_NAME, new ClusterProperties(CLUSTER1_NAME, Collections.emptyList(), Collections.emptyMap(), Collections.emptySet(), NullPartitionProperties.getInstance(), Collections.emptyList(), DarkClustersConverter.toProperties(darkClusterConfigMap), false));
    loadBalancer.getDarkClusterConfigMap(CLUSTER1_NAME, new Callback<DarkClusterConfigMap>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("getDarkClusterConfigMap threw exception", e);
        }

        @Override
        public void onSuccess(DarkClusterConfigMap returnedDarkClusterConfigMap) {
            Assert.assertEquals(returnedDarkClusterConfigMap, darkClusterConfigMap, "dark cluster configs should be equal");
            Assert.assertEquals(returnedDarkClusterConfigMap.get(DARK_CLUSTER1_NAME).getMultiplier(), 1.0f, "multiplier should match");
        }
    });
}
Also used : MockStore(com.linkedin.d2.discovery.stores.mock.MockStore) DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) Test(org.testng.annotations.Test)

Example 12 with DarkClusterConfig

use of com.linkedin.d2.DarkClusterConfig in project rest.li by linkedin.

the class TestDarkClusterManager method testDarkGateKeeper.

@Test
public void testDarkGateKeeper() {
    DarkGateKeeper darkGateKeeper = new DarkGateKeeper() {

        @Override
        public boolean shouldDispatchToDark(RestRequest request, RequestContext requestContext, String darkClusterName) {
            return darkClusterName.equals(DARK_CLUSTER_NAME);
        }
    };
    MockClusterInfoProvider clusterInfoProvider = new MockClusterInfoProvider();
    Facilities facilities = new MockFacilities(clusterInfoProvider);
    MockStrategyFactory strategyFactory = new MockStrategyFactory();
    DarkClusterManager darkClusterManager = new DarkClusterManagerImpl(SOURCE_CLUSTER_NAME, facilities, strategyFactory, null, null, new DoNothingNotifier(), darkGateKeeper);
    strategyFactory.start();
    // This configuration will choose the RelativeTrafficMultiplierDarkClusterStrategy
    DarkClusterConfig darkClusterConfig = createRelativeTrafficMultiplierConfig(1.0f);
    clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME2, darkClusterConfig);
    RestRequest restRequest1 = new RestRequestBuilder(URI.create("/white")).setMethod(METHOD_SAFE).build();
    boolean whiteStatus = darkClusterManager.handleDarkRequest(restRequest1, new RequestContext());
    RestRequest restRequest2 = new RestRequestBuilder(URI.create("/black")).setMethod(METHOD_SAFE).build();
    boolean blackStatus = darkClusterManager.handleDarkRequest(restRequest2, new RequestContext());
    Assert.assertFalse(whiteStatus, "white uri requests not as expected");
    Assert.assertFalse(blackStatus, "black uri requests not as expected");
    Assert.assertEquals(strategyFactory.strategyGetOrCreateCount, 0, "unexpected strategy GetOrCreateCount");
    clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig);
    boolean whiteStatus1 = darkClusterManager.handleDarkRequest(restRequest1, new RequestContext());
    boolean blackStatus1 = darkClusterManager.handleDarkRequest(restRequest2, new RequestContext());
    Assert.assertTrue(whiteStatus1, "white uri requests not as expected");
    Assert.assertTrue(blackStatus1, "black uri requests not as expected");
    Assert.assertEquals(strategyFactory.strategyGetOrCreateCount, 2, "unexpected strategy GetOrCreateCount");
}
Also used : Facilities(com.linkedin.d2.balancer.Facilities) RestRequest(com.linkedin.r2.message.rest.RestRequest) DarkGateKeeper(com.linkedin.darkcluster.api.DarkGateKeeper) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) DarkClusterManagerImpl(com.linkedin.darkcluster.impl.DarkClusterManagerImpl) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) DarkClusterManager(com.linkedin.darkcluster.api.DarkClusterManager) Test(org.testng.annotations.Test)

Example 13 with DarkClusterConfig

use of com.linkedin.d2.DarkClusterConfig in project rest.li by linkedin.

the class TestDarkClusterManager method testWithDarkHeaders.

@Test
public void testWithDarkHeaders() {
    MockClusterInfoProvider clusterInfoProvider = new MockClusterInfoProvider();
    Facilities facilities = new MockFacilities(clusterInfoProvider);
    // This configuration will choose the RelativeTrafficMultiplierDarkClusterStrategy
    DarkClusterConfig darkClusterConfig = createRelativeTrafficMultiplierConfig(1.0f);
    clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig);
    DarkClusterStrategyFactory mockStrategyFactory = Mockito.mock(DarkClusterStrategyFactory.class);
    DarkClusterStrategy mockDarkStrategy = Mockito.mock(DarkClusterStrategy.class);
    DarkRequestHeaderGenerator darkRequestHeaderGenerator = Mockito.mock(DarkRequestHeaderGenerator.class);
    Mockito.when(mockStrategyFactory.get(DARK_CLUSTER_NAME)).thenReturn(mockDarkStrategy);
    Mockito.when(darkRequestHeaderGenerator.get(DARK_CLUSTER_NAME)).thenReturn(Optional.of(new DarkRequestHeaderGenerator.HeaderNameValuePair("header", "value")));
    RestRequest restRequest = new RestRequestBuilder(URI.create("/abc")).setMethod(METHOD_SAFE).build();
    RestRequest darkRequest = new RestRequestBuilder(URI.create("d2://" + DARK_CLUSTER_NAME + "/abc")).setMethod(METHOD_SAFE).setHeader("header", "value").build();
    RequestContext requestContext = new RequestContext();
    Mockito.when(mockDarkStrategy.handleRequest(restRequest, darkRequest, new RequestContext(requestContext))).thenReturn(true);
    DarkClusterManager darkClusterManager = new DarkClusterManagerImpl(SOURCE_CLUSTER_NAME, facilities, mockStrategyFactory, null, null, new DoNothingNotifier(), null, Collections.singletonList(darkRequestHeaderGenerator));
    boolean status = darkClusterManager.handleDarkRequest(restRequest, requestContext);
    Assert.assertTrue(status);
}
Also used : DarkClusterStrategyFactory(com.linkedin.darkcluster.api.DarkClusterStrategyFactory) DarkClusterStrategy(com.linkedin.darkcluster.api.DarkClusterStrategy) NoOpDarkClusterStrategy(com.linkedin.darkcluster.api.NoOpDarkClusterStrategy) Facilities(com.linkedin.d2.balancer.Facilities) DarkRequestHeaderGenerator(com.linkedin.darkcluster.api.DarkRequestHeaderGenerator) RestRequest(com.linkedin.r2.message.rest.RestRequest) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) DarkClusterManagerImpl(com.linkedin.darkcluster.impl.DarkClusterManagerImpl) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) DarkClusterManager(com.linkedin.darkcluster.api.DarkClusterManager) Test(org.testng.annotations.Test)

Example 14 with DarkClusterConfig

use of com.linkedin.d2.DarkClusterConfig in project rest.li by linkedin.

the class TestDarkClusterStrategyFactory method testStrategyFallThru.

@Test
public void testStrategyFallThru() {
    DarkClusterConfig darkClusterConfig1 = createRelativeTrafficMultiplierConfig(0.5f);
    DarkClusterStrategyNameArray darkClusterStrategyList = new DarkClusterStrategyNameArray();
    darkClusterStrategyList.addAll(Arrays.asList(CONSTANT_QPS, RELATIVE_TRAFFIC));
    darkClusterConfig1.setDarkClusterStrategyPrioritizedList(darkClusterStrategyList);
    _clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig1);
    _clusterInfoProvider.notifyListenersClusterAdded(SOURCE_CLUSTER_NAME);
    DarkClusterStrategy strategy = _strategyFactory.get(DARK_CLUSTER_NAME);
    // test that we didn't find a strategy corresponding to Constant QPS and fell through to Relative traffic
    Assert.assertTrue(strategy instanceof RelativeTrafficMultiplierDarkClusterStrategy);
    Assert.assertEquals(((RelativeTrafficMultiplierDarkClusterStrategy) strategy).getMultiplier(), 0.5f, "expected 0.5f multiplier");
}
Also used : RelativeTrafficMultiplierDarkClusterStrategy(com.linkedin.darkcluster.impl.RelativeTrafficMultiplierDarkClusterStrategy) DarkClusterStrategyNameArray(com.linkedin.d2.DarkClusterStrategyNameArray) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) NoOpDarkClusterStrategy(com.linkedin.darkcluster.api.NoOpDarkClusterStrategy) RelativeTrafficMultiplierDarkClusterStrategy(com.linkedin.darkcluster.impl.RelativeTrafficMultiplierDarkClusterStrategy) DarkClusterStrategy(com.linkedin.darkcluster.api.DarkClusterStrategy) Test(org.testng.annotations.Test)

Example 15 with DarkClusterConfig

use of com.linkedin.d2.DarkClusterConfig in project rest.li by linkedin.

the class TestDarkClusterStrategyFactory method testChangingStrategiesAfterStoppingListener.

@Test
public void testChangingStrategiesAfterStoppingListener() {
    DarkClusterConfig darkClusterConfig1 = createRelativeTrafficMultiplierConfig(0.5f);
    DarkClusterConfig darkClusterConfig2 = createRelativeTrafficMultiplierConfig(0.1f);
    _clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig1);
    _clusterInfoProvider.notifyListenersClusterAdded(SOURCE_CLUSTER_NAME);
    DarkClusterStrategy strategy = _strategyFactory.get(DARK_CLUSTER_NAME);
    Assert.assertTrue(strategy instanceof RelativeTrafficMultiplierDarkClusterStrategy);
    Assert.assertEquals(((RelativeTrafficMultiplierDarkClusterStrategy) strategy).getMultiplier(), 0.5f, "expected 0.5f multiplier");
    _strategyFactory.shutdown();
    // update the strategy.
    _clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig2);
    _clusterInfoProvider.notifyListenersClusterAdded(SOURCE_CLUSTER_NAME);
    // Nothing should have been changed, since we shutdown down the listener
    DarkClusterStrategy strategy2 = _strategyFactory.get(DARK_CLUSTER_NAME);
    Assert.assertTrue(strategy2 instanceof RelativeTrafficMultiplierDarkClusterStrategy);
    Assert.assertEquals(((RelativeTrafficMultiplierDarkClusterStrategy) strategy2).getMultiplier(), 0.5f, "expected 0.5f multiplier");
}
Also used : RelativeTrafficMultiplierDarkClusterStrategy(com.linkedin.darkcluster.impl.RelativeTrafficMultiplierDarkClusterStrategy) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) NoOpDarkClusterStrategy(com.linkedin.darkcluster.api.NoOpDarkClusterStrategy) RelativeTrafficMultiplierDarkClusterStrategy(com.linkedin.darkcluster.impl.RelativeTrafficMultiplierDarkClusterStrategy) DarkClusterStrategy(com.linkedin.darkcluster.api.DarkClusterStrategy) Test(org.testng.annotations.Test)

Aggregations

DarkClusterConfig (com.linkedin.d2.DarkClusterConfig)23 Test (org.testng.annotations.Test)20 DarkClusterConfigMap (com.linkedin.d2.DarkClusterConfigMap)10 NoOpDarkClusterStrategy (com.linkedin.darkcluster.api.NoOpDarkClusterStrategy)10 DarkClusterStrategyNameArray (com.linkedin.d2.DarkClusterStrategyNameArray)9 DarkClusterStrategy (com.linkedin.darkcluster.api.DarkClusterStrategy)9 RelativeTrafficMultiplierDarkClusterStrategy (com.linkedin.darkcluster.impl.RelativeTrafficMultiplierDarkClusterStrategy)8 RequestContext (com.linkedin.r2.message.RequestContext)5 RestRequest (com.linkedin.r2.message.rest.RestRequest)5 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)5 Facilities (com.linkedin.d2.balancer.Facilities)4 DarkClusterManager (com.linkedin.darkcluster.api.DarkClusterManager)4 DarkClusterManagerImpl (com.linkedin.darkcluster.impl.DarkClusterManagerImpl)4 D2TransportClientProperties (com.linkedin.d2.D2TransportClientProperties)2 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)2 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)2 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)2 MockStore (com.linkedin.d2.discovery.stores.mock.MockStore)2 DarkClusterStrategyFactoryImpl (com.linkedin.darkcluster.impl.DarkClusterStrategyFactoryImpl)2 ConstantQpsRateLimiter (com.linkedin.r2.transport.http.client.ConstantQpsRateLimiter)2