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: Use correct limit when retrying a limit query stream with a cursor #2203

Merged
merged 5 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 28 additions & 2 deletions dev/src/reference/query-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export class QueryUtil<
const startTime = Date.now();
const isExplain = explainOptions !== undefined;

let numDocumentsReceived = 0;
let lastReceivedDocument: QueryDocumentSnapshot<
AppModelType,
DbModelType
Expand Down Expand Up @@ -239,6 +240,7 @@ export class QueryUtil<
);
}

++numDocumentsReceived;
callback(undefined, output);

if (proto.done) {
Expand Down Expand Up @@ -317,6 +319,12 @@ export class QueryUtil<
stream.destroy(err);
streamActive.resolve(/* active= */ false);
} else if (lastReceivedDocument && retryWithCursor) {
if (query instanceof VectorQuery) {
throw new Error(
'Unimplemented: Vector query does not support cursors yet.'
);
}

logger(
'Query._stream',
tag,
Expand All @@ -330,12 +338,30 @@ export class QueryUtil<
// the query cursor. Note that we do not use backoff here. The
// call to `requestStream()` will backoff should the restart
// fail before delivering any results.
let newQuery: Query<AppModelType, DbModelType>;
if (!this._queryOptions.limit) {
newQuery = query;
} else {
const newLimit =
this._queryOptions.limit - numDocumentsReceived;
if (
this._queryOptions.limitType === undefined ||
this._queryOptions.limitType === LimitType.First
) {
newQuery = query.limit(newLimit);
} else {
newQuery = query.limitToLast(newLimit);
}
}

if (this._queryOptions.requireConsistency) {
request = query
request = newQuery
.startAfter(lastReceivedDocument)
.toProto(lastReceivedDocument.readTime);
} else {
request = query.startAfter(lastReceivedDocument).toProto();
request = newQuery
.startAfter(lastReceivedDocument)
.toProto();
}

// Set lastReceivedDocument to null before each retry attempt to ensure the retry makes progress
Expand Down
Loading
Loading