Search in sources :

Example 1 with PartitionKey

use of com.google.api.gax.batching.PartitionKey in project gax-java by googleapis.

the class BatcherFactoryTest method testGetPushingBatcher.

@Test
public void testGetPushingBatcher() {
    BatchingSettings batchingSettings = BatchingSettings.newBuilder().setDelayThreshold(Duration.ofSeconds(1)).setElementCountThreshold(2L).setRequestByteThreshold(1000L).build();
    FlowControlSettings flowControlSettings = FlowControlSettings.newBuilder().setLimitExceededBehavior(LimitExceededBehavior.Ignore).build();
    FlowController flowController = new FlowController(flowControlSettings);
    BatcherFactory<LabeledIntList, List<Integer>> batcherFactory = new BatcherFactory<>(new SquarerBatchingDescriptor(), batchingSettings, batchingExecutor, flowController);
    Truth.assertThat(batcherFactory.getBatchingSettings()).isSameInstanceAs(batchingSettings);
    ThresholdBatcher<Batch<LabeledIntList, List<Integer>>> batcherFoo = batcherFactory.getPushingBatcher(new PartitionKey("foo"));
    ThresholdBatcher<Batch<LabeledIntList, List<Integer>>> batcherFoo2 = batcherFactory.getPushingBatcher(new PartitionKey("foo"));
    ThresholdBatcher<Batch<LabeledIntList, List<Integer>>> batcherBar = batcherFactory.getPushingBatcher(new PartitionKey("bar"));
    Truth.assertThat(batcherFoo).isSameInstanceAs(batcherFoo2);
    Truth.assertThat(batcherFoo).isNotSameInstanceAs(batcherBar);
}
Also used : FlowControlSettings(com.google.api.gax.batching.FlowControlSettings) SquarerBatchingDescriptor(com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptor) LabeledIntList(com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList) PartitionKey(com.google.api.gax.batching.PartitionKey) List(java.util.List) LabeledIntList(com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList) FlowController(com.google.api.gax.batching.FlowController) BatchingSettings(com.google.api.gax.batching.BatchingSettings) Test(org.junit.Test)

Example 2 with PartitionKey

use of com.google.api.gax.batching.PartitionKey in project gax-java by googleapis.

the class BatchExecutorTest method testValidateFailure.

@Test(expected = IllegalArgumentException.class)
public void testValidateFailure() {
    BatchExecutor<List<Integer>, Integer> executor = new BatchExecutor<List<Integer>, Integer>(integerDescriptor, new PartitionKey(0));
    List<Integer> request = new ArrayList<>();
    request.add(3);
    Batch<List<Integer>, Integer> batchingContextOk = new Batch<>(integerDescriptor, request, null, null);
    executor.validateBatch(batchingContextOk);
}
Also used : ArrayList(java.util.ArrayList) PartitionKey(com.google.api.gax.batching.PartitionKey) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with PartitionKey

use of com.google.api.gax.batching.PartitionKey in project gax-java by googleapis.

the class BatchExecutorTest method testValidate.

@Test
public void testValidate() {
    BatchExecutor<List<Integer>, Integer> executor = new BatchExecutor<List<Integer>, Integer>(integerDescriptor, new PartitionKey(0));
    List<Integer> request = new ArrayList<Integer>();
    request.add(2);
    Batch<List<Integer>, Integer> batchingContextOk = new Batch<>(integerDescriptor, request, null, null);
    executor.validateBatch(batchingContextOk);
}
Also used : ArrayList(java.util.ArrayList) PartitionKey(com.google.api.gax.batching.PartitionKey) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with PartitionKey

use of com.google.api.gax.batching.PartitionKey in project gax-java by googleapis.

the class BatchingCallable method futureCall.

@Override
public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext context) {
    if (batcherFactory.getBatchingSettings().getIsEnabled()) {
        BatchedFuture<ResponseT> result = BatchedFuture.<ResponseT>create();
        UnaryCallable<RequestT, ResponseT> unaryCallable = callable.withDefaultCallContext(context);
        Batch<RequestT, ResponseT> batchableMessage = new Batch<RequestT, ResponseT>(batchingDescriptor, request, unaryCallable, result);
        PartitionKey partitionKey = batchingDescriptor.getBatchPartitionKey(request);
        ThresholdBatcher<Batch<RequestT, ResponseT>> batcher = batcherFactory.getPushingBatcher(partitionKey);
        try {
            batcher.add(batchableMessage);
            return result;
        } catch (FlowControlException e) {
            throw FlowControlRuntimeException.fromFlowControlException(e);
        }
    } else {
        return callable.futureCall(request, context);
    }
}
Also used : FlowControlException(com.google.api.gax.batching.FlowController.FlowControlException) PartitionKey(com.google.api.gax.batching.PartitionKey)

Aggregations

PartitionKey (com.google.api.gax.batching.PartitionKey)4 List (java.util.List)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 BatchingSettings (com.google.api.gax.batching.BatchingSettings)1 FlowControlSettings (com.google.api.gax.batching.FlowControlSettings)1 FlowController (com.google.api.gax.batching.FlowController)1 FlowControlException (com.google.api.gax.batching.FlowController.FlowControlException)1 LabeledIntList (com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList)1 SquarerBatchingDescriptor (com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptor)1