Skip to content

Commit

Permalink
fix: verify partition count before invoking GetPartition RPC (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Oct 22, 2020
1 parent e49b49d commit 2054ae9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
Expand Up @@ -20,6 +20,7 @@
import com.google.api.gax.rpc.ApiExceptions;
import com.google.api.gax.rpc.ApiStreamObserver;
import com.google.cloud.firestore.v1.FirestoreClient;
import com.google.common.base.Preconditions;
import com.google.firestore.v1.Cursor;
import com.google.firestore.v1.PartitionQueryRequest;
import io.opencensus.common.Scope;
Expand Down Expand Up @@ -53,42 +54,51 @@ public class CollectionGroup extends Query {
*/
public void getPartitions(
long desiredPartitionCount, ApiStreamObserver<QueryPartition> observer) {
Preconditions.checkArgument(
desiredPartitionCount > 0, "Desired partition count must be one or greater");

// Partition queries require explicit ordering by __name__.
Query queryWithDefaultOrder = orderBy(FieldPath.DOCUMENT_ID);

PartitionQueryRequest.Builder request = PartitionQueryRequest.newBuilder();
request.setStructuredQuery(queryWithDefaultOrder.buildQuery());
request.setParent(options.getParentPath().toString());
if (desiredPartitionCount == 1) {
// Short circuit if the user only requested a single partition.
observer.onNext(new QueryPartition(queryWithDefaultOrder, null, null));
} else {
PartitionQueryRequest.Builder request = PartitionQueryRequest.newBuilder();
request.setStructuredQuery(queryWithDefaultOrder.buildQuery());
request.setParent(options.getParentPath().toString());

// Since we are always returning an extra partition (with en empty endBefore cursor), we
// reduce the desired partition count by one.
request.setPartitionCount(desiredPartitionCount - 1);
// Since we are always returning an extra partition (with en empty endBefore cursor), we
// reduce the desired partition count by one.
request.setPartitionCount(desiredPartitionCount - 1);

final FirestoreClient.PartitionQueryPagedResponse response;
final TraceUtil traceUtil = TraceUtil.getInstance();
Span span = traceUtil.startSpan(TraceUtil.SPAN_NAME_PARTITIONQUERY);
try (Scope scope = traceUtil.getTracer().withSpan(span)) {
response =
ApiExceptions.callAndTranslateApiException(
rpcContext.sendRequest(
request.build(), rpcContext.getClient().partitionQueryPagedCallable()));
} catch (ApiException exception) {
span.setStatus(Status.UNKNOWN.withDescription(exception.getMessage()));
throw FirestoreException.apiException(exception);
} finally {
span.end(TraceUtil.END_SPAN_OPTIONS);
}
final FirestoreClient.PartitionQueryPagedResponse response;
final TraceUtil traceUtil = TraceUtil.getInstance();
Span span = traceUtil.startSpan(TraceUtil.SPAN_NAME_PARTITIONQUERY);
try (Scope scope = traceUtil.getTracer().withSpan(span)) {
response =
ApiExceptions.callAndTranslateApiException(
rpcContext.sendRequest(
request.build(), rpcContext.getClient().partitionQueryPagedCallable()));
} catch (ApiException exception) {
span.setStatus(Status.UNKNOWN.withDescription(exception.getMessage()));
throw FirestoreException.apiException(exception);
} finally {
span.end(TraceUtil.END_SPAN_OPTIONS);
}

@Nullable Object[] lastCursor = null;
for (Cursor cursor : response.iterateAll()) {
Object[] decodedCursorValue = new Object[cursor.getValuesCount()];
for (int i = 0; i < cursor.getValuesCount(); ++i) {
decodedCursorValue[i] = UserDataConverter.decodeValue(rpcContext, cursor.getValues(i));
@Nullable Object[] lastCursor = null;
for (Cursor cursor : response.iterateAll()) {
Object[] decodedCursorValue = new Object[cursor.getValuesCount()];
for (int i = 0; i < cursor.getValuesCount(); ++i) {
decodedCursorValue[i] = UserDataConverter.decodeValue(rpcContext, cursor.getValues(i));
}
observer.onNext(new QueryPartition(queryWithDefaultOrder, lastCursor, decodedCursorValue));
lastCursor = decodedCursorValue;
}
observer.onNext(new QueryPartition(queryWithDefaultOrder, lastCursor, decodedCursorValue));
lastCursor = decodedCursorValue;
observer.onNext(new QueryPartition(queryWithDefaultOrder, lastCursor, null));
}
observer.onNext(new QueryPartition(queryWithDefaultOrder, lastCursor, null));

observer.onCompleted();
}
}
Expand Up @@ -615,6 +615,17 @@ public void emptyPartitionedQuery() throws Exception {
assertNull(partitions.get(0).getEndBefore());
}

@Test
public void validatesPartitionCount() {
StreamConsumer<QueryPartition> consumer = new StreamConsumer<>();
try {
firestore.collectionGroup(randomColl.getId()).getPartitions(0, consumer);
fail();
} catch (IllegalArgumentException e) {
assertEquals("Desired partition count must be one or greater", e.getMessage());
}
}

@Test
public void failedTransaction() {
try {
Expand Down

0 comments on commit 2054ae9

Please sign in to comment.