New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: return results from getPartitions() in order #653
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
51d8129
fix: return results from getPartitions() in order
schmidt-sebastian 471add5
Add unit tests
schmidt-sebastian f230d86
Add unit tests
schmidt-sebastian f1ac25d
Merge
schmidt-sebastian 5b66e51
Update google-cloud-firestore/src/main/java/com/google/cloud/firestor…
schmidt-sebastian 23077ad
Update CollectionGroup.java
schmidt-sebastian File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
google-cloud-firestore/src/test/java/com/google/cloud/firestore/PartitionQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.firestore; | ||
|
||
import static com.google.cloud.firestore.LocalFirestoreHelper.queryResponse; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.fail; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.api.core.ApiFutures; | ||
import com.google.api.gax.rpc.ApiStreamObserver; | ||
import com.google.api.gax.rpc.ServerStreamingCallable; | ||
import com.google.api.gax.rpc.UnaryCallable; | ||
import com.google.cloud.firestore.spi.v1.FirestoreRpc; | ||
import com.google.cloud.firestore.v1.FirestoreClient.PartitionQueryPage; | ||
import com.google.cloud.firestore.v1.FirestoreClient.PartitionQueryPagedResponse; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.firestore.v1.Cursor; | ||
import com.google.firestore.v1.PartitionQueryRequest; | ||
import com.google.firestore.v1.PartitionQueryResponse; | ||
import com.google.firestore.v1.RunQueryRequest; | ||
import com.google.firestore.v1.StructuredQuery; | ||
import com.google.firestore.v1.Value; | ||
import java.util.List; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.mockito.Matchers; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.Spy; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class PartitionQuery { | ||
public static final String DATABASE_NAME = "projects/test-project/databases/(default)/documents"; | ||
public static final Cursor CURSOR1 = | ||
Cursor.newBuilder() | ||
.addValues(Value.newBuilder().setReferenceValue(DATABASE_NAME + "/collection/doc1")) | ||
.build(); | ||
public static final Cursor PARTITION1 = CURSOR1.toBuilder().setBefore(true).build(); | ||
public static final Cursor CURSOR2 = | ||
Cursor.newBuilder() | ||
.addValues(Value.newBuilder().setReferenceValue(DATABASE_NAME + "/collection/doc2")) | ||
.build(); | ||
public static final Cursor PARTITION2 = CURSOR2.toBuilder().setBefore(true).build(); | ||
|
||
@Spy | ||
private final FirestoreImpl firestoreMock = | ||
new FirestoreImpl( | ||
FirestoreOptions.newBuilder().setProjectId("test-project").build(), | ||
Mockito.mock(FirestoreRpc.class)); | ||
|
||
@Mock private UnaryCallable<PartitionQueryRequest, PartitionQueryPagedResponse> callable; | ||
@Mock private PartitionQueryPagedResponse pagedResponse; | ||
@Mock private PartitionQueryPage queryPage; | ||
|
||
@Captor private ArgumentCaptor<RunQueryRequest> runQuery; | ||
@Captor private ArgumentCaptor<ApiStreamObserver> streamObserverCapture; | ||
@Captor private ArgumentCaptor<PartitionQueryRequest> requestCaptor; | ||
|
||
@Test | ||
public void requestsOneLessThanDesired() throws Exception { | ||
int desiredPartitionsCount = 2; | ||
|
||
PartitionQueryRequest expectedRequest = | ||
PartitionQueryRequest.newBuilder() | ||
.setParent(DATABASE_NAME) | ||
.setStructuredQuery( | ||
StructuredQuery.newBuilder() | ||
.addFrom( | ||
StructuredQuery.CollectionSelector.newBuilder() | ||
.setAllDescendants(true) | ||
.setCollectionId("collectionId")) | ||
.addOrderBy( | ||
StructuredQuery.Order.newBuilder() | ||
.setField( | ||
StructuredQuery.FieldReference.newBuilder() | ||
.setFieldPath("__name__")) | ||
.setDirection(StructuredQuery.Direction.ASCENDING))) | ||
.setPartitionCount(desiredPartitionsCount - 1) | ||
.build(); | ||
|
||
PartitionQueryResponse response = | ||
PartitionQueryResponse.newBuilder().addPartitions(CURSOR1).build(); | ||
|
||
when(pagedResponse.iterateAll()).thenReturn(ImmutableList.of(CURSOR1)); | ||
when(queryPage.getResponse()).thenReturn(response); | ||
doReturn(ApiFutures.immediateFuture(pagedResponse)) | ||
.when(firestoreMock) | ||
.sendRequest( | ||
requestCaptor.capture(), | ||
Matchers.<UnaryCallable<PartitionQueryRequest, PartitionQueryPagedResponse>>any()); | ||
|
||
firestoreMock.collectionGroup("collectionId").getPartitions(desiredPartitionsCount).get(); | ||
|
||
PartitionQueryRequest actualRequest = requestCaptor.getValue(); | ||
assertEquals(actualRequest, expectedRequest); | ||
} | ||
|
||
@Test | ||
public void doesNotIssueRpcIfOnlyASinglePartitionIsRequested() throws Exception { | ||
int desiredPartitionsCount = 1; | ||
|
||
List<QueryPartition> partitions = | ||
firestoreMock.collectionGroup("collectionId").getPartitions(desiredPartitionsCount).get(); | ||
|
||
assertEquals(partitions.size(), 1); | ||
assertNull(partitions.get(0).getStartAt()); | ||
assertNull(partitions.get(0).getEndBefore()); | ||
} | ||
|
||
@Test | ||
public void validatesPartitionCount() { | ||
int desiredPartitionsCount = 0; | ||
try { | ||
firestoreMock.collectionGroup("collectionId").getPartitions(desiredPartitionsCount); | ||
fail(); | ||
} catch (IllegalArgumentException e) { | ||
assertEquals(e.getMessage(), "Desired partition count must be one or greater"); | ||
} | ||
} | ||
|
||
@Test | ||
public void convertsPartitionsToQueries() throws Exception { | ||
int desiredPartitionsCount = 3; | ||
|
||
PartitionQueryResponse response = | ||
PartitionQueryResponse.newBuilder().addPartitions(CURSOR1).build(); | ||
|
||
when(pagedResponse.iterateAll()).thenReturn(ImmutableList.of(CURSOR1, CURSOR2)); | ||
when(queryPage.getResponse()).thenReturn(response); | ||
doReturn(ApiFutures.immediateFuture(pagedResponse)) | ||
.when(firestoreMock) | ||
.sendRequest( | ||
requestCaptor.capture(), | ||
Matchers.<UnaryCallable<PartitionQueryRequest, PartitionQueryPagedResponse>>any()); | ||
|
||
doAnswer(queryResponse()) | ||
.when(firestoreMock) | ||
.streamRequest( | ||
runQuery.capture(), | ||
streamObserverCapture.capture(), | ||
Matchers.<ServerStreamingCallable>any()); | ||
|
||
List<QueryPartition> partitions = | ||
firestoreMock.collectionGroup("collectionId").getPartitions(desiredPartitionsCount).get(); | ||
|
||
assertEquals(partitions.size(), 3); | ||
for (QueryPartition partition : partitions) { | ||
partition.createQuery().get(); | ||
} | ||
|
||
assertEquals(runQuery.getAllValues().size(), 3); | ||
|
||
assertFalse(runQuery.getAllValues().get(0).getStructuredQuery().hasStartAt()); | ||
assertEquals(runQuery.getAllValues().get(0).getStructuredQuery().getEndAt(), PARTITION1); | ||
assertEquals(runQuery.getAllValues().get(1).getStructuredQuery().getStartAt(), PARTITION1); | ||
assertEquals(runQuery.getAllValues().get(1).getStructuredQuery().getEndAt(), PARTITION2); | ||
assertEquals(runQuery.getAllValues().get(2).getStructuredQuery().getStartAt(), PARTITION2); | ||
assertFalse(runQuery.getAllValues().get(2).getStructuredQuery().hasEndAt()); | ||
} | ||
|
||
@Test | ||
public void sortsPartitions() throws Exception { | ||
int desiredPartitionsCount = 3; | ||
|
||
PartitionQueryResponse response = | ||
PartitionQueryResponse.newBuilder().addPartitions(CURSOR1).build(); | ||
|
||
when(pagedResponse.iterateAll()).thenReturn(ImmutableList.of(CURSOR2, CURSOR1)); | ||
when(queryPage.getResponse()).thenReturn(response); | ||
doReturn(ApiFutures.immediateFuture(pagedResponse)) | ||
.when(firestoreMock) | ||
.sendRequest( | ||
requestCaptor.capture(), | ||
Matchers.<UnaryCallable<PartitionQueryRequest, PartitionQueryPagedResponse>>any()); | ||
|
||
List<QueryPartition> partitions = | ||
firestoreMock.collectionGroup("collectionId").getPartitions(desiredPartitionsCount).get(); | ||
|
||
assertEquals(((DocumentReference) partitions.get(0).getEndBefore()[0]).getId(), "doc1"); | ||
assertEquals(((DocumentReference) partitions.get(1).getEndBefore()[0]).getId(), "doc2"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like an interesting optimization. And also an unlikely case? Is it worth special handling in libs or should we just let the backend respond with no partitions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The backend returns an error when 0 partitions are requested. We should catch this ourselves since we request one less than what the user requested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I already ensured we always returned a minimum of 1 QueryPartition. I just added the short circuit in golang though, since it is the case we don't really need to request anything.