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: Lazy-started transactions #2017

Merged
merged 22 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 47 additions & 12 deletions dev/src/document-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ import {Timestamp} from './timestamp';
import {DocumentData} from '@google-cloud/firestore';
import api = google.firestore.v1;

interface BatchGetResponse<AppModelType, DbModelType extends DocumentData> {
result: Array<DocumentSnapshot<AppModelType, DbModelType>>;
/**
* The transaction that was started as part of this request. Will only be if
* `DocumentReader.transactionIdOrNewTransaction` was `api.ITransactionOptions`.
*/
transaction?: Uint8Array;
}

/**
* A wrapper around BatchGetDocumentsRequest that retries request upon stream
* failure and returns ordered results.
Expand All @@ -35,13 +44,14 @@ import api = google.firestore.v1;
export class DocumentReader<AppModelType, DbModelType extends DocumentData> {
/** An optional field mask to apply to this read. */
fieldMask?: FieldPath[];
/** An optional transaction ID to use for this read. */
transactionId?: Uint8Array;
/** An optional transaction ID or options for beginning a new transaction to use for this read. */
transactionIdOrNewTransaction?: Uint8Array | api.ITransactionOptions;
/** An optional readTime to use for this read. */
readTime?: Timestamp;

private outstandingDocuments = new Set<string>();
private retrievedDocuments = new Map<string, DocumentSnapshot>();
private readonly outstandingDocuments = new Set<string>();
private readonly retrievedDocuments = new Map<string, DocumentSnapshot>();
private retrievedTransactionId?: Uint8Array;

/**
* Creates a new DocumentReader that fetches the provided documents (via
Expand All @@ -51,22 +61,38 @@ export class DocumentReader<AppModelType, DbModelType extends DocumentData> {
* @param allDocuments The documents to get.
*/
constructor(
private firestore: Firestore,
private allDocuments: Array<DocumentReference<AppModelType, DbModelType>>
private readonly firestore: Firestore,
private readonly allDocuments: ReadonlyArray<
DocumentReference<AppModelType, DbModelType>
>
) {
for (const docRef of this.allDocuments) {
this.outstandingDocuments.add(docRef.formattedName);
}
}

/**
* Invokes the BatchGetDocuments RPC and returns the results.
* Invokes the BatchGetDocuments RPC and returns the results as an array of
* documents.
*
* @param requestTag A unique client-assigned identifier for this request.
*/
async get(
requestTag: string
): Promise<Array<DocumentSnapshot<AppModelType, DbModelType>>> {
const {result} = await this.getResponse(requestTag);
return result;
}

/**
* Invokes the BatchGetDocuments RPC and returns the results with transaction
* metadata.
*
* @param requestTag A unique client-assigned identifier for this request.
*/
async getResponse(
requestTag: string
): Promise<BatchGetResponse<AppModelType, DbModelType>> {
await this.fetchDocuments(requestTag);

// BatchGetDocuments doesn't preserve document order. We use the request
Expand All @@ -92,7 +118,10 @@ export class DocumentReader<AppModelType, DbModelType extends DocumentData> {
}
}

return orderedDocuments;
return {
result: orderedDocuments,
transaction: this.retrievedTransactionId,
};
}

private async fetchDocuments(requestTag: string): Promise<void> {
Expand All @@ -104,8 +133,10 @@ export class DocumentReader<AppModelType, DbModelType extends DocumentData> {
database: this.firestore.formattedName,
documents: Array.from(this.outstandingDocuments),
};
if (this.transactionId) {
request.transaction = this.transactionId;
if (this.transactionIdOrNewTransaction instanceof Uint8Array) {
request.transaction = this.transactionIdOrNewTransaction;
} else if (this.transactionIdOrNewTransaction) {
request.newTransaction = this.transactionIdOrNewTransaction;
} else if (this.readTime) {
request.readTime = this.readTime.toProto().timestampValue;
}
Expand All @@ -131,7 +162,10 @@ export class DocumentReader<AppModelType, DbModelType extends DocumentData> {
for await (const response of stream) {
let snapshot: DocumentSnapshot<DocumentData>;

if (response.found) {
if (response.transaction) {
this.retrievedTransactionId = response.transaction;
continue;
} else if (response.found) {
logger(
'DocumentReader.fetchDocuments',
requestTag,
Expand Down Expand Up @@ -163,7 +197,8 @@ export class DocumentReader<AppModelType, DbModelType extends DocumentData> {
} catch (error) {
const shouldRetry =
// Transactional reads are retried via the transaction runner.
!this.transactionId &&
!this.transactionIdOrNewTransaction &&
!this.retrievedTransactionId &&
tom-andersen marked this conversation as resolved.
Show resolved Hide resolved
// Only retry if we made progress.
resultCount > 0 &&
// Don't retry permanent errors.
Expand Down
Loading