Search in sources :

Example 6 with TrackingBackupRequestsStrategy

use of com.linkedin.d2.backuprequests.TrackingBackupRequestsStrategy 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)

Aggregations

ByteString (com.linkedin.data.ByteString)4 TrackingBackupRequestsStrategy (com.linkedin.d2.backuprequests.TrackingBackupRequestsStrategy)3 Callback (com.linkedin.common.callback.Callback)2 FutureCallback (com.linkedin.common.callback.FutureCallback)2 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)2 URI (java.net.URI)2 BackupRequestsStrategy (com.linkedin.d2.backuprequests.BackupRequestsStrategy)1 BackupRequestsStrategyFromConfig (com.linkedin.d2.backuprequests.BackupRequestsStrategyFromConfig)1 TestTrackingBackupRequestsStrategy (com.linkedin.d2.backuprequests.TestTrackingBackupRequestsStrategy)1 LoadBalancer (com.linkedin.d2.balancer.LoadBalancer)1 StaticLoadBalancerState (com.linkedin.d2.balancer.StaticLoadBalancerState)1 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)1 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)1 SimpleLoadBalancer (com.linkedin.d2.balancer.simple.SimpleLoadBalancer)1 RequestContext (com.linkedin.r2.message.RequestContext)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)1 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)1 StreamResponseBuilder (com.linkedin.r2.message.stream.StreamResponseBuilder)1 DrainReader (com.linkedin.r2.message.stream.entitystream.DrainReader)1