Search in sources :

Example 6 with DarkClusterConfigMap

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

the class SimpleLoadBalancerTest method testClusterInfoProviderGetDarkClusters.

@Test
public void testClusterInfoProviderGetDarkClusters() throws InterruptedException, ExecutionException, ServiceUnavailableException {
    int numHttp = 3;
    int numHttps = 4;
    int partitionIdForAdd = 0;
    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));
    populateUriRegistry(numHttp, numHttps, partitionIdForAdd, uriRegistry);
    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 7 with DarkClusterConfigMap

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

the class DarkClusterManagerImpl method handleDarkRequest.

@Override
public boolean handleDarkRequest(RestRequest originalRequest, RequestContext originalRequestContext) {
    String uri = originalRequest.getURI().toString();
    boolean darkRequestSent = false;
    try {
        final boolean whiteListed = _whiteListRegEx != null && _whiteListRegEx.matcher(uri).matches();
        final boolean blackedListed = _blackListRegEx != null && _blackListRegEx.matcher(uri).matches();
        // 4) custom dark gatekeeper returns true for the given request and requestContext
        if ((isSafe(originalRequest) || whiteListed) && !blackedListed) {
            // the request is already immutable, and a new requestContext will be created in BaseDarkClusterDispatcher.
            // We don't need to copy them here, but doing it just for safety.
            RestRequest reqCopy = originalRequest.builder().build();
            RequestContext newRequestContext = new RequestContext(originalRequestContext);
            DarkClusterConfigMap configMap = _facilities.getClusterInfoProvider().getDarkClusterConfigMap(_sourceClusterName);
            for (String darkClusterName : configMap.keySet()) {
                if (_darkGateKeeper.shouldDispatchToDark(originalRequest, originalRequestContext, darkClusterName)) {
                    RestRequest newD2Request = rewriteRequest(reqCopy, darkClusterName);
                    // now find the strategy appropriate for each dark cluster
                    DarkClusterStrategy strategy = _darkClusterStrategyFactory.get(darkClusterName);
                    darkRequestSent = strategy.handleRequest(reqCopy, newD2Request, newRequestContext);
                }
            }
        }
    } catch (Throwable e) {
        _notifier.notify(() -> new RuntimeException("DarkCanaryDispatcherFilter failed to send request: " + uri, e));
    }
    return darkRequestSent;
}
Also used : DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap) RestRequest(com.linkedin.r2.message.rest.RestRequest) DarkClusterStrategy(com.linkedin.darkcluster.api.DarkClusterStrategy) RequestContext(com.linkedin.r2.message.RequestContext)

Example 8 with DarkClusterConfigMap

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

the class MockClusterInfoProvider method removeDarkClusterConfig.

void removeDarkClusterConfig(String sourceClusterName, String darkClusterName) {
    DarkClusterConfigMap darkClusterConfigMap = (lookupMap.containsKey(sourceClusterName)) ? lookupMap.get(sourceClusterName) : new DarkClusterConfigMap();
    darkClusterConfigMap.remove(darkClusterName);
    lookupMap.put(sourceClusterName, darkClusterConfigMap);
}
Also used : DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap)

Example 9 with DarkClusterConfigMap

use of com.linkedin.d2.DarkClusterConfigMap 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 10 with DarkClusterConfigMap

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

the class MockClusterInfoProvider method addDarkClusterConfig.

/**
 * add the ability to add a dark cluster to a source cluster's darkClusterConfigMap
 */
void addDarkClusterConfig(String sourceClusterName, String darkClusterName, DarkClusterConfig darkClusterConfig) {
    DarkClusterConfigMap darkClusterConfigMap = (lookupMap.containsKey(sourceClusterName)) ? lookupMap.get(sourceClusterName) : new DarkClusterConfigMap();
    darkClusterConfigMap.put(darkClusterName, darkClusterConfig);
    lookupMap.put(sourceClusterName, darkClusterConfigMap);
}
Also used : DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap)

Aggregations

DarkClusterConfigMap (com.linkedin.d2.DarkClusterConfigMap)17 Test (org.testng.annotations.Test)10 DarkClusterConfig (com.linkedin.d2.DarkClusterConfig)8 DarkClusterStrategyNameArray (com.linkedin.d2.DarkClusterStrategyNameArray)5 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 D2TransportClientProperties (com.linkedin.d2.D2TransportClientProperties)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 Map (java.util.Map)2 FutureCallback (com.linkedin.common.callback.FutureCallback)1 DarkClusterStrategyName (com.linkedin.d2.DarkClusterStrategyName)1 DarkClusterStrategy (com.linkedin.darkcluster.api.DarkClusterStrategy)1 DataList (com.linkedin.data.DataList)1 RequestContext (com.linkedin.r2.message.RequestContext)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 TimeoutCallback (com.linkedin.r2.transport.http.client.TimeoutCallback)1 List (java.util.List)1