Search in sources :

Example 1 with ClusterInfoProvider

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

the class TestDarkClusterStrategyFactory method testRemoveDarkClusters.

@Test
public void testRemoveDarkClusters() {
    DarkClusterConfig darkClusterConfig1 = createRelativeTrafficMultiplierConfig(0.5f);
    DarkClusterConfig darkClusterConfig2 = createRelativeTrafficMultiplierConfig(0.1f);
    _clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig1);
    _clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME2, darkClusterConfig2);
    // now trigger a refresh on the source cluster.
    _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");
    DarkClusterStrategy strategy2 = _strategyFactory.get(DARK_CLUSTER_NAME2);
    Assert.assertTrue(strategy2 instanceof RelativeTrafficMultiplierDarkClusterStrategy);
    Assert.assertEquals(((RelativeTrafficMultiplierDarkClusterStrategy) strategy2).getMultiplier(), 0.1f, "expected 0.1f multiplier");
    // update the clusterInfoProvider, and refresh the strategyMap
    _clusterInfoProvider.removeDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME2);
    _clusterInfoProvider.notifyListenersClusterAdded(SOURCE_CLUSTER_NAME);
    DarkClusterStrategy strategy3 = _strategyFactory.get(DARK_CLUSTER_NAME);
    Assert.assertTrue(strategy3 instanceof RelativeTrafficMultiplierDarkClusterStrategy);
    // there should be no strategy entry for DARK_CLUSTER_NAME2, so it should return the NO_OP strategy
    DarkClusterStrategy strategy4 = _strategyFactory.get(DARK_CLUSTER_NAME2);
    Assert.assertSame(strategy4, NO_OP_DARK_CLUSTER_STRATEGY);
}
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)

Example 2 with ClusterInfoProvider

use of com.linkedin.d2.balancer.util.ClusterInfoProvider 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 3 with ClusterInfoProvider

use of com.linkedin.d2.balancer.util.ClusterInfoProvider 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 4 with ClusterInfoProvider

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

the class TestDarkClusterManager method testBasic.

@Test(dataProvider = "provideKeys")
public void testBasic(String whitelist, String blacklist, String httpMethod, DarkGateKeeper darkGateKeeper, int expectedWhiteCount, int expectedBlackCount) {
    MockClusterInfoProvider clusterInfoProvider = new MockClusterInfoProvider();
    Facilities facilities = new MockFacilities(clusterInfoProvider);
    MockStrategyFactory strategyFactory = new MockStrategyFactory();
    DarkClusterManager darkClusterManager = new DarkClusterManagerImpl(SOURCE_CLUSTER_NAME, facilities, strategyFactory, whitelist, blacklist, new DoNothingNotifier(), darkGateKeeper);
    strategyFactory.start();
    // This configuration will choose the RelativeTrafficMultiplierDarkClusterStrategy
    DarkClusterConfig darkClusterConfig = createRelativeTrafficMultiplierConfig(1.0f);
    clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig);
    RestRequest restRequest1 = new RestRequestBuilder(URI.create("/white")).setMethod(httpMethod).build();
    boolean whiteStatus = darkClusterManager.handleDarkRequest(restRequest1, new RequestContext());
    RestRequest restRequest2 = new RestRequestBuilder(URI.create("/black")).setMethod(httpMethod).build();
    boolean blackStatus = darkClusterManager.handleDarkRequest(restRequest2, new RequestContext());
    Assert.assertEquals(whiteStatus, expectedWhiteCount > 0, "white uri requests not as expected");
    Assert.assertEquals(blackStatus, expectedBlackCount > 0, "black uri requests not as expected");
    Assert.assertEquals(strategyFactory.strategyGetOrCreateCount, expectedWhiteCount + expectedBlackCount, "unexpected strategy GetOrCreateCount");
}
Also used : 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) Facilities(com.linkedin.d2.balancer.Facilities) Test(org.testng.annotations.Test)

Example 5 with ClusterInfoProvider

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

the class TestDarkClusterFilter method testDarkClusterAssemblyWithDarkCluster.

@Test
public void testDarkClusterAssemblyWithDarkCluster() {
    // we need to have a Mock clusterInfoProvider in order to set up a dark cluster.
    MockClusterInfoProvider clusterInfoProvider = new MockClusterInfoProvider();
    _facilities = new MockFacilities(clusterInfoProvider);
    _darkClusterStrategyFactory = new DarkClusterStrategyFactoryImpl(_facilities, SOURCE_CLUSTER_NAME, _darkClusterDispatcher, _notifier, _random, _verifierManager, _rateLimiterSupplier);
    _darkClusterStrategyFactory.start();
    DarkClusterManager darkClusterManager = new DarkClusterManagerImpl(SOURCE_CLUSTER_NAME, _facilities, _darkClusterStrategyFactory, "", "", _notifier);
    _darkClusterFilter = new DarkClusterFilter(darkClusterManager, _verifierManager);
    // set the multiplier to 1 so that traffic gets sent.
    DarkClusterConfig darkClusterConfig = createRelativeTrafficMultiplierConfig(1.0f);
    clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig);
    clusterInfoProvider.notifyListenersClusterAdded(SOURCE_CLUSTER_NAME);
    // send the request, expecting it to make it all the way down to the client
    RestRequest restRequest = new RestRequestBuilder(URI.create("foo")).build();
    _darkClusterFilter.onRestRequest(restRequest, new RequestContext(), new HashMap<>(), new DummyNextFilter());
    Assert.assertEquals(_client.requestAuthorityMap.size(), 1, "expected 1 request to be sent");
    _darkClusterFilter.onRestError(new RuntimeException("test"), new RequestContext(), new HashMap<>(), new DummyNextFilter());
    _darkClusterFilter.onRestResponse(new RestResponseBuilder().build(), new RequestContext(), new HashMap<>(), new DummyNextFilter());
}
Also used : DarkClusterFilter(com.linkedin.darkcluster.filter.DarkClusterFilter) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) DarkClusterStrategyFactoryImpl(com.linkedin.darkcluster.impl.DarkClusterStrategyFactoryImpl) 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)

Aggregations

DarkClusterConfig (com.linkedin.d2.DarkClusterConfig)5 Test (org.testng.annotations.Test)5 DarkClusterManager (com.linkedin.darkcluster.api.DarkClusterManager)4 DarkClusterManagerImpl (com.linkedin.darkcluster.impl.DarkClusterManagerImpl)4 RequestContext (com.linkedin.r2.message.RequestContext)4 RestRequest (com.linkedin.r2.message.rest.RestRequest)4 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)4 Facilities (com.linkedin.d2.balancer.Facilities)3 DarkClusterStrategy (com.linkedin.darkcluster.api.DarkClusterStrategy)2 NoOpDarkClusterStrategy (com.linkedin.darkcluster.api.NoOpDarkClusterStrategy)2 DarkClusterStrategyFactory (com.linkedin.darkcluster.api.DarkClusterStrategyFactory)1 DarkGateKeeper (com.linkedin.darkcluster.api.DarkGateKeeper)1 DarkRequestHeaderGenerator (com.linkedin.darkcluster.api.DarkRequestHeaderGenerator)1 DarkClusterFilter (com.linkedin.darkcluster.filter.DarkClusterFilter)1 DarkClusterStrategyFactoryImpl (com.linkedin.darkcluster.impl.DarkClusterStrategyFactoryImpl)1 RelativeTrafficMultiplierDarkClusterStrategy (com.linkedin.darkcluster.impl.RelativeTrafficMultiplierDarkClusterStrategy)1 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)1