Search in sources :

Example 16 with DegraderTrackerClientImpl

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

the class SimpleLoadBalancerStateTest method testRefreshWithConcurrentGetTC.

// This test is to verify a fix for a specific bug, where the d2 client receives a zookeeper
// update and concurrent getTrackerClient requests. In that case, all but the first concurrent
// requests got a null tracker client because the degraderLoadBalancerState was not fully initialized
// (hashring was empty), and this continued until the first request had atomically swamped a
// fully initialized state for other requests to use. This test failed on pre-fix code, it now
// succeeds.
@Test(groups = { "small", "back-end" })
public void testRefreshWithConcurrentGetTC() throws URISyntaxException, InterruptedException {
    reset();
    LinkedList<String> strategyList = new LinkedList<>();
    URI uri = URI.create("http://cluster-1/test");
    final List<String> schemes = new ArrayList<>();
    schemes.add("http");
    strategyList.add("degraderV3");
    // set up state
    _state.listenToService("service-1", new NullStateListenerCallback());
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    assertNull(_state.getStrategy("service-1", "http"));
    // Use the _clusterRegistry.put to populate the _state.clusterProperties, used by
    // _state.refreshServiceStrategies
    _clusterRegistry.put("cluster-1", new ClusterProperties("cluster-1"));
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", strategyList, Collections.<String, Object>emptyMap(), Collections.<String, Object>emptyMap(), Collections.<String, String>emptyMap(), schemes, Collections.<URI>emptySet()));
    LoadBalancerStrategy strategy = _state.getStrategy("service-1", "http");
    assertNotNull(strategy, "got null strategy in setup");
    // test serial to make sure things are working before concurrent test
    TransportClient resultTC = _state.getClient("service-1", "http");
    assertNotNull(resultTC, "got null tracker client in non-concurrent env");
    ExecutorService myExecutor = Executors.newCachedThreadPool();
    ArrayList<TcCallable> cArray = new ArrayList<>();
    List<TrackerClient> clients = new ArrayList<>();
    Map<Integer, PartitionData> partitionDataMap = new HashMap<>(2);
    partitionDataMap.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1d));
    clients.add(new DegraderTrackerClientImpl(uri, partitionDataMap, new DegraderLoadBalancerTest.TestLoadBalancerClient(uri), SystemClock.instance(), null));
    for (int i = 0; i < 20; i++) {
        cArray.add(i, new TcCallable(clients, _state));
    }
    Runnable refreshTask = new Runnable() {

        @Override
        public void run() {
            while (true) {
                List<String> myStrategyList = new LinkedList<>();
                myStrategyList.add("degraderV3");
                _state.refreshServiceStrategies(new ServiceProperties("service-1", "cluster-1", "/test", myStrategyList, Collections.<String, Object>emptyMap(), Collections.<String, Object>emptyMap(), Collections.<String, String>emptyMap(), schemes, Collections.<URI>emptySet()));
                if (Thread.interrupted()) {
                    return;
                }
            }
        }
    };
    myExecutor.execute(refreshTask);
    Integer badResults = 0;
    ArrayList<Future<Integer>> myList = new ArrayList<>();
    for (int i = 0; i < cArray.size(); i++) {
        @SuppressWarnings("unchecked") Callable<Integer> c = (Callable) cArray.get(i);
        myList.add(i, myExecutor.submit(c));
    }
    try {
        for (int i = 0; i < cArray.size(); i++) {
            badResults += myList.get(i).get();
        }
    } catch (ExecutionException e) {
        Assert.assertFalse(true, "got ExecutionException");
    } finally {
        try {
            // call shutdownNow() to send an interrupt to the refreshTask
            myExecutor.shutdownNow();
            boolean status = myExecutor.awaitTermination(5, TimeUnit.SECONDS);
            if (status == false) {
                Assert.assertFalse(true, "failed to shutdown threads correctly");
            }
        } catch (InterruptedException ie) {
            // this thread was interrupted
            myExecutor.shutdownNow();
        }
    }
    Assert.assertTrue(badResults == 0, "getTrackerClients returned null");
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URI(java.net.URI) Callable(java.util.concurrent.Callable) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) ExecutionException(java.util.concurrent.ExecutionException) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) RelativeLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) RandomLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy) LinkedList(java.util.LinkedList) NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) SynchronousExecutorService(com.linkedin.d2.discovery.event.SynchronousExecutorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) Future(java.util.concurrent.Future) DegraderTrackerClientImpl(com.linkedin.d2.balancer.clients.DegraderTrackerClientImpl) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 17 with DegraderTrackerClientImpl

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

the class LoadBalancerStrategyTestRunnerBuilder method buildDegraderStrategy.

private LoadBalancerStrategyTestRunner buildDegraderStrategy() {
    if (_serviceProperties == null) {
        setDegraderStrategies(new HashMap<>(), new HashMap<>());
    }
    _strategy = new DegraderLoadBalancerStrategyFactoryV3().newLoadBalancer(_serviceProperties);
    _transportClients = _uris.stream().map(uri -> new MockTransportClient(_clockedExecutor, _latencyManager, _errorCountManager, uri, INTERVAL_IN_MILLIS, _currentErrorCountMap, _lastRequestCountMap, _callCountMap, _latencySumMap)).collect(Collectors.toList());
    Map<URI, TrackerClient> trackerClientMap = _transportClients.stream().map(transportClient -> {
        // If partition map is not specified, by default we only support one partition
        Map<Integer, PartitionData> partitionDataMap = _partitionDataMap.getOrDefault(transportClient.getUri(), DEFAULT_PARTITION_DATA_MAP);
        return new DegraderTrackerClientImpl(transportClient.getUri(), partitionDataMap, transportClient, _clockedExecutor, DegraderConfigFactory.toDegraderConfig(_serviceProperties.getDegraderProperties()));
    }).collect(Collectors.toMap(TrackerClient::getUri, trackerClient -> trackerClient));
    return buildInternal(trackerClientMap);
}
Also used : Arrays(java.util.Arrays) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) Callback(com.linkedin.common.callback.Callback) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) HashMap(java.util.HashMap) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) RestResponse(com.linkedin.r2.message.rest.RestResponse) ArrayList(java.util.ArrayList) ClockedExecutor(com.linkedin.test.util.ClockedExecutor) Map(java.util.Map) DegraderConfigFactory(com.linkedin.d2.balancer.strategies.degrader.DegraderConfigFactory) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) URI(java.net.URI) HttpStatus(org.eclipse.jetty.http.HttpStatus) RelativeLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategyFactory) RestException(com.linkedin.r2.message.rest.RestException) RestRequest(com.linkedin.r2.message.rest.RestRequest) com.linkedin.d2.loadBalancerStrategyType(com.linkedin.d2.loadBalancerStrategyType) Set(java.util.Set) DegraderTrackerClientImpl(com.linkedin.d2.balancer.clients.DegraderTrackerClientImpl) TrackerClientImpl(com.linkedin.d2.balancer.clients.TrackerClientImpl) PropertyKeys(com.linkedin.d2.balancer.properties.PropertyKeys) Collectors(java.util.stream.Collectors) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) TransportResponseImpl(com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl) RequestContext(com.linkedin.r2.message.RequestContext) None(com.linkedin.common.util.None) RelativeStrategyPropertiesConverter(com.linkedin.d2.balancer.config.RelativeStrategyPropertiesConverter) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) DegraderTrackerClientImpl(com.linkedin.d2.balancer.clients.DegraderTrackerClientImpl) URI(java.net.URI) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with DegraderTrackerClientImpl

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

the class TestBackupRequestsClient method createAlwaysBackupClientWithHosts.

private BackupRequestsClient createAlwaysBackupClientWithHosts(List<String> uris, Deque<URI> hostsReceivingRequestList, int responseDelayNano, int backupDelayNano, boolean isD2Async) throws IOException {
    Map<URI, Map<Integer, PartitionData>> partitionDescriptions = new HashMap<>();
    uris.forEach(uri -> partitionDescriptions.put(URI.create(uri), Collections.singletonMap(0, new PartitionData(1))));
    StaticLoadBalancerState LbState = new StaticLoadBalancerState() {

        @Override
        public TrackerClient getClient(String serviceName, URI uri) {
            return new DegraderTrackerClientImpl(uri, partitionDescriptions.get(uri), null, SystemClock.instance(), null) {

                @Override
                public void restRequest(RestRequest request, RequestContext requestContext, Map<String, String> wireAttrs, TransportCallback<RestResponse> callback) {
                    // whenever a trackerClient is used to make request, record down it's hostname
                    hostsReceivingRequestList.add(uri);
                    // delay response to allow backup request to happen
                    _executor.schedule(() -> callback.onResponse(TransportResponseImpl.success(new RestResponseBuilder().build())), responseDelayNano, TimeUnit.NANOSECONDS);
                }

                @Override
                public void streamRequest(StreamRequest request, RequestContext requestContext, Map<String, String> wireAttrs, TransportCallback<StreamResponse> callback) {
                    // whenever a trackerClient is used to make request, record down it's hostname
                    hostsReceivingRequestList.add(uri);
                    if (null != requestContext.getLocalAttr(R2Constants.BACKUP_REQUEST_BUFFERED_BODY)) {
                        callback.onResponse(TransportResponseImpl.success(new StreamResponseBuilder().setHeader(BUFFERED_HEADER, String.valueOf(requestContext.getLocalAttr(R2Constants.BACKUP_REQUEST_BUFFERED_BODY) != null)).build(EntityStreams.emptyStream())));
                        return;
                    }
                    request.getEntityStream().setReader(new DrainReader() {

                        public void onDone() {
                            // delay response to allow backup request to happen
                            _executor.schedule(() -> callback.onResponse(TransportResponseImpl.success(new StreamResponseBuilder().setHeader(BUFFERED_HEADER, String.valueOf(requestContext.getLocalAttr(R2Constants.BACKUP_REQUEST_BUFFERED_BODY) != null)).build(EntityStreams.emptyStream()))), responseDelayNano, TimeUnit.NANOSECONDS);
                        }
                    });
                }
            };
        }
    };
    LbState.TEST_URIS_PARTITIONDESCRIPTIONS.putAll(partitionDescriptions);
    LbState.TEST_SERVICE_BACKUP_REQUEST_PROPERTIES.add(createBackupRequestsConfiguration(5, "get"));
    LbState.refreshDefaultProperties();
    LoadBalancer loadBalancer = new SimpleLoadBalancer(LbState, _executor);
    DynamicClient dynamicClient = new DynamicClient(loadBalancer, null);
    return new BackupRequestsClient(dynamicClient, loadBalancer, _executor, null, 10, TimeUnit.SECONDS, isD2Async) {

        @Override
        Optional<TrackingBackupRequestsStrategy> getStrategyAfterUpdate(final String serviceName, final String operation) {
            // constantly enable backup request after backupDelayNano time.
            BackupRequestsStrategy alwaysBackup = new TestTrackingBackupRequestsStrategy.MockBackupRequestsStrategy(() -> Optional.of((long) backupDelayNano), () -> true);
            return Optional.of(new TrackingBackupRequestsStrategy(alwaysBackup));
        }
    };
}
Also used : TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) HashMap(java.util.HashMap) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) LoadBalancer(com.linkedin.d2.balancer.LoadBalancer) ByteString(com.linkedin.data.ByteString) TrackingBackupRequestsStrategy(com.linkedin.d2.backuprequests.TrackingBackupRequestsStrategy) TestTrackingBackupRequestsStrategy(com.linkedin.d2.backuprequests.TestTrackingBackupRequestsStrategy) URI(java.net.URI) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RestRequest(com.linkedin.r2.message.rest.RestRequest) BackupRequestsStrategy(com.linkedin.d2.backuprequests.BackupRequestsStrategy) TrackingBackupRequestsStrategy(com.linkedin.d2.backuprequests.TrackingBackupRequestsStrategy) TestTrackingBackupRequestsStrategy(com.linkedin.d2.backuprequests.TestTrackingBackupRequestsStrategy) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) StaticLoadBalancerState(com.linkedin.d2.balancer.StaticLoadBalancerState) RequestContext(com.linkedin.r2.message.RequestContext) Map(java.util.Map) HashMap(java.util.HashMap)

Example 19 with DegraderTrackerClientImpl

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

the class LoadBalancerTestState method getClient.

@Override
public TrackerClient getClient(String clusterName, URI uri) {
    Map<Integer, PartitionData> partitionDataMap = new HashMap<>(2);
    partitionDataMap.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1));
    return (getClient) ? new DegraderTrackerClientImpl(uri, partitionDataMap, new TestClient(), SystemClock.instance(), null) : null;
}
Also used : PartitionData(com.linkedin.d2.balancer.properties.PartitionData) HashMap(java.util.HashMap) TestClient(com.linkedin.d2.balancer.clients.TestClient) DegraderTrackerClientImpl(com.linkedin.d2.balancer.clients.DegraderTrackerClientImpl)

Example 20 with DegraderTrackerClientImpl

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

the class DegraderTrackerClientTest method testClientStreamRequest.

@Test(groups = { "small", "back-end" })
public void testClientStreamRequest() throws URISyntaxException {
    URI uri = URI.create("http://test.qa.com:1234/foo");
    double weight = 3d;
    TestClient wrappedClient = new TestClient(true);
    Clock clock = new SettableClock();
    Map<Integer, PartitionData> partitionDataMap = createDefaultPartitionData(3d);
    DegraderTrackerClient client = new DegraderTrackerClientImpl(uri, partitionDataMap, wrappedClient, clock, null);
    Assert.assertEquals(client.getUri(), uri);
    Double clientWeight = client.getPartitionWeight(DefaultPartitionAccessor.DEFAULT_PARTITION_ID);
    Assert.assertEquals(clientWeight, weight);
    Assert.assertEquals(client.getTransportClient(), wrappedClient);
    StreamRequest streamRequest = new StreamRequestBuilder(uri).build(EntityStreams.emptyStream());
    Map<String, String> restWireAttrs = new HashMap<>();
    TestTransportCallback<StreamResponse> restCallback = new TestTransportCallback<>();
    client.streamRequest(streamRequest, new RequestContext(), restWireAttrs, restCallback);
    Assert.assertFalse(restCallback.response.hasError());
    Assert.assertSame(wrappedClient.streamRequest, streamRequest);
    Assert.assertEquals(wrappedClient.restWireAttrs, restWireAttrs);
}
Also used : HashMap(java.util.HashMap) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) Clock(com.linkedin.util.clock.Clock) SettableClock(com.linkedin.util.clock.SettableClock) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) SettableClock(com.linkedin.util.clock.SettableClock) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Aggregations

URI (java.net.URI)20 DegraderTrackerClientImpl (com.linkedin.d2.balancer.clients.DegraderTrackerClientImpl)19 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)15 RequestContext (com.linkedin.r2.message.RequestContext)15 Test (org.testng.annotations.Test)15 DegraderTrackerClient (com.linkedin.d2.balancer.clients.DegraderTrackerClient)14 HashMap (java.util.HashMap)13 ArrayList (java.util.ArrayList)12 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)11 DegraderTrackerClientTest (com.linkedin.d2.balancer.clients.DegraderTrackerClientTest)10 DegraderControl (com.linkedin.util.degrader.DegraderControl)9 CallCompletion (com.linkedin.util.degrader.CallCompletion)7 URIRequest (com.linkedin.d2.balancer.util.URIRequest)5 DegraderImpl (com.linkedin.util.degrader.DegraderImpl)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 SettableClock (com.linkedin.util.clock.SettableClock)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ByteString (com.linkedin.data.ByteString)3 RestRequest (com.linkedin.r2.message.rest.RestRequest)3 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)2