Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit 9d1f090

Browse files
committed
make PartitionKey "backward-compatible"
This change is still a breaking change, but it is just compatible enough that GAPIC-generated clients will compile and work properly. Toolkit will be updated to take advantage of the new PartitionKey endpoint. Once all clients are migrated, we can get rid of the old String endpoint.
1 parent 0c12089 commit 9d1f090

6 files changed

Lines changed: 29 additions & 15 deletions

File tree

src/main/java/com/google/api/gax/batching/PartitionKey.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@
3131
package com.google.api.gax.batching;
3232

3333
import com.google.common.collect.ImmutableList;
34+
import java.util.Map;
3435

36+
/**
37+
* Requests with the same {@code PartitionKey} can be batched together in one RPC call.
38+
*
39+
* Instances of this class may be used as keys in {@link Map}s. Consequently, arguments to the
40+
* constructor of this class should not be modified.
41+
*/
3542
public final class PartitionKey {
3643
private final ImmutableList<Object> keys;
3744
private final int hash;

src/main/java/com/google/api/gax/grpc/BatchExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public BatchExecutor(
6363

6464
@Override
6565
public void validateBatch(Batch<RequestT, ResponseT> item) {
66-
PartitionKey itemPartitionKey = batchingDescriptor.getBatchPartitionKey(item.getRequest());
66+
PartitionKey itemPartitionKey = batchingDescriptor.getPartitionKey(item.getRequest());
6767
if (!itemPartitionKey.equals(partitionKey)) {
6868
String requestClassName = item.getRequest().getClass().getSimpleName();
6969
throw new IllegalArgumentException(

src/main/java/com/google/api/gax/grpc/BatchingCallable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public ApiFuture<ResponseT> futureCall(RequestT request, CallContext context) {
6666
UnaryCallable.<RequestT, ResponseT>create(callable).bind(context.getChannel());
6767
Batch<RequestT, ResponseT> batchableMessage =
6868
new Batch<RequestT, ResponseT>(batchingDescriptor, request, unaryCallable, result);
69-
PartitionKey partitionKey = batchingDescriptor.getBatchPartitionKey(request);
69+
PartitionKey partitionKey = batchingDescriptor.getPartitionKey(request);
7070
ThresholdBatcher<Batch<RequestT, ResponseT>> batcher =
7171
batcherFactory.getPushingBatcher(partitionKey);
7272
try {

src/main/java/com/google/api/gax/grpc/BatchingDescriptor.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,44 @@
4343
* <p>
4444
* This is public only for technical reasons, for advanced usage.
4545
*/
46-
public interface BatchingDescriptor<RequestT, ResponseT> {
46+
public abstract class BatchingDescriptor<RequestT, ResponseT> {
4747

4848
/**
4949
* Returns the value of the partition key for the given request.
5050
*/
51-
PartitionKey getBatchPartitionKey(RequestT request);
51+
protected String getBatchPartitionKey(RequestT request) {
52+
throw new UnsupportedOperationException(
53+
"Implementors of BatchingDescriptor must override either getBatchPartitionKey or getPartitionKey");
54+
}
55+
56+
public PartitionKey getPartitionKey(RequestT request) {
57+
return new PartitionKey(getBatchPartitionKey(request));
58+
}
5259

5360
/** Get the Builder object for the request type RequestT. */
54-
RequestBuilder<RequestT> getRequestBuilder();
61+
public abstract RequestBuilder<RequestT> getRequestBuilder();
5562

5663
/**
5764
* Splits the result from a batched call into an individual setResponse call on each
5865
* RequestIssuer.
5966
*/
60-
void splitResponse(
67+
public abstract void splitResponse(
6168
ResponseT batchResponse, Collection<? extends BatchedRequestIssuer<ResponseT>> batch);
6269

6370
/**
6471
* Splits the exception that resulted from a batched call into an individual setException call on
6572
* each RequestIssuer.
6673
*/
67-
void splitException(
74+
public abstract void splitException(
6875
Throwable throwable, Collection<? extends BatchedRequestIssuer<ResponseT>> batch);
6976

7077
/**
7178
* Returns the number of elements contained in this request.
7279
*/
73-
long countElements(RequestT request);
80+
public abstract long countElements(RequestT request);
7481

7582
/**
7683
* Returns the size in bytes of this request.
7784
*/
78-
long countBytes(RequestT request);
85+
public abstract long countBytes(RequestT request);
7986
}

src/test/java/com/google/api/gax/grpc/BatchExecutorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class BatchExecutorTest {
4242
new BatchingDescriptor<List<Integer>, Integer>() {
4343

4444
@Override
45-
public PartitionKey getBatchPartitionKey(List<Integer> request) {
45+
public PartitionKey getPartitionKey(List<Integer> request) {
4646
return new PartitionKey(request.get(0) % 2);
4747
}
4848

src/test/java/com/google/api/gax/grpc/UnaryCallableTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void pagedBind() {
188188
new BatchingDescriptor<List<Integer>, List<Integer>>() {
189189

190190
@Override
191-
public PartitionKey getBatchPartitionKey(List<Integer> request) {
191+
public PartitionKey getPartitionKey(List<Integer> request) {
192192
return new PartitionKey();
193193
}
194194

@@ -586,7 +586,7 @@ public ApiFuture<List<Integer>> futureCall(LabeledIntList request, CallContext c
586586
new BatchingDescriptor<LabeledIntList, List<Integer>>() {
587587

588588
@Override
589-
public PartitionKey getBatchPartitionKey(LabeledIntList request) {
589+
public PartitionKey getPartitionKey(LabeledIntList request) {
590590
return new PartitionKey(request.label);
591591
}
592592

@@ -709,7 +709,7 @@ public void batchingWithFlowControl() throws Exception {
709709
Truth.assertThat(f2.get()).isEqualTo(Arrays.asList(9, 16));
710710

711711
batcherFactory
712-
.getPushingBatcher(SQUARER_BATCHING_DESC.getBatchPartitionKey(requestA))
712+
.getPushingBatcher(SQUARER_BATCHING_DESC.getPartitionKey(requestA))
713713
.pushCurrentBatch()
714714
.get();
715715

@@ -727,8 +727,8 @@ public void batchingWithFlowControl() throws Exception {
727727
new BatchingDescriptor<LabeledIntList, List<Integer>>() {
728728

729729
@Override
730-
public PartitionKey getBatchPartitionKey(LabeledIntList request) {
731-
Assert.fail("getBatchPartitionKey should not be invoked while batching is disabled.");
730+
public PartitionKey getPartitionKey(LabeledIntList request) {
731+
Assert.fail("getPartitionKey should not be invoked while batching is disabled.");
732732
return null;
733733
}
734734

0 commit comments

Comments
 (0)