Search in sources :

Example 1 with BackupRequestsStrategy

use of com.linkedin.d2.backuprequests.BackupRequestsStrategy in project rest.li by linkedin.

the class TestBackupRequestsStrategyFactory method testBoundedCostBackupRequestsWithDefaultsDeser.

@Test
public void testBoundedCostBackupRequestsWithDefaultsDeser() throws IOException {
    BackupRequestsConfiguration brc = new BackupRequestsConfiguration();
    BoundedCostBackupRequests bcbr = new BoundedCostBackupRequests();
    bcbr.setCost(3);
    brc.setOperation("BATCH_GET");
    brc.setStrategy(BackupRequestsConfiguration.Strategy.create(bcbr));
    String json = new JacksonDataCodec().mapToString(brc.data());
    @SuppressWarnings("unchecked") Map<String, Object> map = JacksonUtil.getObjectMapper().readValue(json, Map.class);
    BackupRequestsStrategy strategy = BackupRequestsStrategyFactory.tryCreate(map);
    assertNotNull(strategy);
    assertTrue(strategy instanceof BoundedCostBackupRequestsStrategy);
    BoundedCostBackupRequestsStrategy boundedCostStrategy = (BoundedCostBackupRequestsStrategy) strategy;
    assertEquals(boundedCostStrategy.getHistoryLength(), (int) bcbr.getHistoryLength());
    assertEquals(boundedCostStrategy.getMinBackupDelayNano(), (long) bcbr.getMinBackupDelayMs() * 1000L * 1000L);
    assertEquals(boundedCostStrategy.getRequiredHistory(), (int) bcbr.getRequiredHistoryLength());
    assertEquals(boundedCostStrategy.getPercent(), (double) bcbr.getCost());
}
Also used : JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) BackupRequestsConfiguration(com.linkedin.d2.BackupRequestsConfiguration) BoundedCostBackupRequests(com.linkedin.d2.BoundedCostBackupRequests) Test(org.testng.annotations.Test)

Example 2 with BackupRequestsStrategy

use of com.linkedin.d2.backuprequests.BackupRequestsStrategy in project rest.li by linkedin.

the class TestBackupRequestsStrategyFactory method testBoundedCostBackupRequestsDeser.

@Test
public void testBoundedCostBackupRequestsDeser() throws IOException {
    BackupRequestsConfiguration brc = new BackupRequestsConfiguration();
    BoundedCostBackupRequests bcbr = new BoundedCostBackupRequests();
    bcbr.setCost(3);
    bcbr.setHistoryLength(4096);
    bcbr.setMaxBurst(16);
    bcbr.setMinBackupDelayMs(5);
    bcbr.setRequiredHistoryLength(65536);
    brc.setOperation("BATCH_GET");
    brc.setStrategy(BackupRequestsConfiguration.Strategy.create(bcbr));
    String json = new JacksonDataCodec().mapToString(brc.data());
    @SuppressWarnings("unchecked") Map<String, Object> map = JacksonUtil.getObjectMapper().readValue(json, Map.class);
    BackupRequestsStrategy strategy = BackupRequestsStrategyFactory.tryCreate(map);
    assertNotNull(strategy);
    assertTrue(strategy instanceof BoundedCostBackupRequestsStrategy);
    BoundedCostBackupRequestsStrategy boundedCostStrategy = (BoundedCostBackupRequestsStrategy) strategy;
    assertEquals(boundedCostStrategy.getHistoryLength(), (int) bcbr.getHistoryLength());
    assertEquals(boundedCostStrategy.getMinBackupDelayNano(), (long) bcbr.getMinBackupDelayMs() * 1000L * 1000L);
    assertEquals(boundedCostStrategy.getRequiredHistory(), (int) bcbr.getRequiredHistoryLength());
    assertEquals(boundedCostStrategy.getPercent(), (double) bcbr.getCost());
}
Also used : JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) BackupRequestsConfiguration(com.linkedin.d2.BackupRequestsConfiguration) BoundedCostBackupRequests(com.linkedin.d2.BoundedCostBackupRequests) Test(org.testng.annotations.Test)

Example 3 with BackupRequestsStrategy

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

BackupRequestsConfiguration (com.linkedin.d2.BackupRequestsConfiguration)2 BoundedCostBackupRequests (com.linkedin.d2.BoundedCostBackupRequests)2 JacksonDataCodec (com.linkedin.data.codec.JacksonDataCodec)2 Test (org.testng.annotations.Test)2 BackupRequestsStrategy (com.linkedin.d2.backuprequests.BackupRequestsStrategy)1 TestTrackingBackupRequestsStrategy (com.linkedin.d2.backuprequests.TestTrackingBackupRequestsStrategy)1 TrackingBackupRequestsStrategy (com.linkedin.d2.backuprequests.TrackingBackupRequestsStrategy)1 LoadBalancer (com.linkedin.d2.balancer.LoadBalancer)1 StaticLoadBalancerState (com.linkedin.d2.balancer.StaticLoadBalancerState)1 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)1 SimpleLoadBalancer (com.linkedin.d2.balancer.simple.SimpleLoadBalancer)1 ByteString (com.linkedin.data.ByteString)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 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)1 URI (java.net.URI)1