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

feat: add support for read-only transactions in TransactionOptions #320

Merged
merged 15 commits into from Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from 14 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
6 changes: 6 additions & 0 deletions google-cloud-firestore/pom.xml
Expand Up @@ -152,6 +152,12 @@
<version>2.11.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<reporting>
Expand Down
Expand Up @@ -30,7 +30,6 @@
import com.google.firestore.v1.BatchGetDocumentsResponse;
import com.google.firestore.v1.DatabaseRootName;
import com.google.protobuf.ByteString;
import io.grpc.Context;
import io.opencensus.trace.AttributeValue;
import io.opencensus.trace.Tracer;
import io.opencensus.trace.Tracing;
Expand All @@ -40,7 +39,6 @@
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Executor;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -310,15 +308,9 @@ public <T> ApiFuture<T> runAsyncTransaction(
public <T> ApiFuture<T> runAsyncTransaction(
@Nonnull final Transaction.AsyncFunction<T> updateFunction,
@Nonnull TransactionOptions transactionOptions) {
final Executor userCallbackExecutor =
Context.currentContextExecutor(
transactionOptions.getExecutor() != null
? transactionOptions.getExecutor()
: firestoreClient.getExecutor());

TransactionRunner<T> transactionRunner =
new TransactionRunner<>(
this, updateFunction, userCallbackExecutor, transactionOptions.getNumberOfAttempts());
new TransactionRunner<>(this, updateFunction, transactionOptions);
return transactionRunner.run();
}

Expand Down
Expand Up @@ -19,6 +19,7 @@
import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.cloud.firestore.TransactionOptions.TransactionOptionsType;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.firestore.v1.BeginTransactionRequest;
Expand Down Expand Up @@ -61,12 +62,18 @@ public interface AsyncFunction<T> {
ApiFuture<T> updateCallback(Transaction transaction);
}

private final TransactionOptions transactionOptions;
@Nullable private final ByteString previousTransactionId;
private ByteString transactionId;
private @Nullable ByteString previousTransactionId;

Transaction(FirestoreImpl firestore, @Nullable Transaction previousTransaction) {
Transaction(
FirestoreImpl firestore,
TransactionOptions transactionOptions,
@Nullable Transaction previousTransaction) {
super(firestore);
previousTransactionId = previousTransaction != null ? previousTransaction.transactionId : null;
this.transactionOptions = transactionOptions;
this.previousTransactionId =
previousTransaction != null ? previousTransaction.transactionId : null;
}

Transaction wrapResult(ApiFuture<WriteResult> result) {
Expand All @@ -78,11 +85,18 @@ ApiFuture<Void> begin() {
BeginTransactionRequest.Builder beginTransaction = BeginTransactionRequest.newBuilder();
beginTransaction.setDatabase(firestore.getDatabaseName());

if (previousTransactionId != null) {
if (TransactionOptionsType.READ_WRITE.equals(transactionOptions.getType())
&& previousTransactionId != null) {
beginTransaction
.getOptionsBuilder()
.getReadWriteBuilder()
.setRetryTransaction(previousTransactionId);
} else if (TransactionOptionsType.READ_ONLY.equals(transactionOptions.getType())
&& transactionOptions.getReadTime() != null) {
beginTransaction
.getOptionsBuilder()
.getReadOnlyBuilder()
.setReadTime(transactionOptions.getReadTime());
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we must call beginTransaction.getOptionsBuilder().getReadOnlyBuilder() for read-only transaction even if we don't have a read time. You can probably just drop && transactionOptions.getReadTime() != null (and maybe also && previousTransactionId != null).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch. I did need to handle the else branch here that was previously taken care of by the options class. I've updated the code to handle both cases.

}

ApiFuture<BeginTransactionResponse> transactionBeginFuture =
Expand Down