Skip to content
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: set timeouts for BatchGetDocuments/RunQuery #799

Merged
merged 4 commits into from Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -132,6 +132,9 @@ public GrpcFirestoreRpc(final FirestoreOptions options) throws IOException {
FirestoreStubSettings.Builder firestoreBuilder =
FirestoreStubSettings.newBuilder(clientContext)
.applyToAllUnaryMethods(retrySettingsSetter);
// Manually apply the retry settings to streaming methods
firestoreBuilder.runQuerySettings().setRetrySettings(options.getRetrySettings());
firestoreBuilder.batchGetDocumentsSettings().setRetrySettings(options.getRetrySettings());
firestoreStub = GrpcFirestoreStub.create(firestoreBuilder.build());
} catch (Exception e) {
throw new IOException(e);
Expand Down
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.google.cloud.firestore;
package com.google.cloud.firestore.it;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand All @@ -24,6 +24,14 @@

import com.google.api.core.ApiFuture;
import com.google.cloud.Timestamp;
import com.google.cloud.firestore.BulkWriter;
import com.google.cloud.firestore.CollectionReference;
import com.google.cloud.firestore.DocumentReference;
import com.google.cloud.firestore.DocumentSnapshot;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
import com.google.cloud.firestore.LocalFirestoreHelper;
import com.google.cloud.firestore.WriteResult;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Collections;
Expand Down
Expand Up @@ -58,7 +58,10 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class ITQueryWatchTest {

private static Firestore firestore;
Expand Down
Expand Up @@ -32,7 +32,10 @@
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ITShutdownTest {
@Rule public final Timeout timeout = new Timeout(5, TimeUnit.SECONDS);
@Rule public TestName testName = new TestName();
Expand Down
Expand Up @@ -33,12 +33,14 @@
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.core.SettableApiFuture;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.ApiStreamObserver;
import com.google.cloud.Timestamp;
import com.google.cloud.firestore.BulkWriter;
Expand Down Expand Up @@ -100,7 +102,11 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.threeten.bp.Duration;

@RunWith(JUnit4.class)
public class ITSystemTest {

private static final double DOUBLE_EPSILON = 0.000001;
Expand Down Expand Up @@ -1761,4 +1767,41 @@ public void testRecursiveDeleteWithCustomBulkWriterInstance() throws Exception {
assertEquals(0, countCollectionChildren(randomColl));
assertEquals(6, callbackCount[0]);
}

@Test
public void testEnforcesTimeouts() throws Exception {
FirestoreOptions firestoreOptions =
FirestoreOptions.newBuilder()
.setRetrySettings(
RetrySettings.newBuilder()
.setMaxRpcTimeout(Duration.ofMillis(1))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be nice to add some asserts to the retry settings once built in a unit test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, the code in the stub handler is not exercised by unit tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, can we add one? Something along the lines of:

 FirestoreOptions firestoreOptions =
                FirestoreOptions.newBuilder()
                        .setRetrySettings(
                                RetrySettings.newBuilder()
                                        .setMaxRpcTimeout(Duration.ofMillis(1))
                                        .setTotalTimeout(Duration.ofMillis(1))
                                        .setInitialRpcTimeout(Duration.ofMillis(1))
                                        .build())
                        .build();

Duration maxRpcTimeout = firestoreOptions.getRetrySettings().getMaxRpcTimeout();
Truth.assertThat(maxRpcTimeout).isEqualTo(Duration.ofMillis(1));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and could add additional cases for the unary timeouts that were already getting set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, this would not catch the issue here. The issue is that the retry settings do not get applied correctly to the underlying RCPs, not that the settings itself are off. The code snippet you provided is just auto-generated code (unless I misunderstood the intent)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it - I thought FirestoreOptions was handwritten. SGTM

.setTotalTimeout(Duration.ofMillis(1))
.setInitialRpcTimeout(Duration.ofMillis(1))
.build())
.build();
firestore = firestoreOptions.getService();
CollectionReference collection = firestore.collection("timeout");

// RunQuery
assertThrows(ExecutionException.class, () -> collection.get().get());
// CommitRequest
assertThrows(ExecutionException.class, () -> collection.add(map()).get());
// BulkCommit
assertThrows(
ExecutionException.class,
() -> {
BulkWriter bulkWriter = firestore.bulkWriter();
ApiFuture<WriteResult> op = bulkWriter.set(collection.document(), map());
bulkWriter.close();
op.get();
});
// BatchGetDocuments
assertThrows(ExecutionException.class, () -> collection.document().get().get());
// ListDocuments
assertThrows(FirestoreException.class, () -> collection.listDocuments().iterator().hasNext());
// ListCollections
assertThrows(
FirestoreException.class,
() -> collection.document().listCollections().iterator().hasNext());
}
}