Repository navigation
[NoQA] add detailed logs to PersistedRequests and SequentialQueue #80605
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
Changes from all commits
2d99e48
4e05738
5832871
07947cb
f58da2a
ea52137
d10527c
be3f366
2ae8562
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import { | |
| deleteRequestsByIndices as deletePersistedRequestsByIndices, | ||
| endRequestAndRemoveFromQueue as endPersistedRequestAndRemoveFromQueue, | ||
| getAll as getAllPersistedRequests, | ||
| getCommands, | ||
| onInitialization as onPersistedRequestsInitialization, | ||
| processNextRequest as processNextPersistedRequest, | ||
| rollbackOngoingRequest as rollbackOngoingPersistedRequest, | ||
|
|
@@ -133,6 +134,12 @@ function process(): Promise<void> { | |
| } | ||
|
|
||
| const persistedRequests = getAllPersistedRequests(); | ||
|
|
||
| Log.info('[SequentialQueue] process() called', false, { | ||
| persistedRequestsLength: persistedRequests.length, | ||
| isSequentialQueueRunning, | ||
| }); | ||
|
|
||
| if (persistedRequests.length === 0) { | ||
| Log.info('[SequentialQueue] Unable to process. No requests to process.'); | ||
| return Promise.resolve(); | ||
|
|
@@ -144,54 +151,106 @@ function process(): Promise<void> { | |
| return Promise.resolve(); | ||
| } | ||
|
|
||
| Log.info('[SequentialQueue] Starting to process request', false, { | ||
| command: requestToProcess.command, | ||
| isRollback: requestToProcess.isRollback ?? false, | ||
| persistWhenOngoing: requestToProcess.persistWhenOngoing ?? false, | ||
| }); | ||
|
|
||
| // Set the current request to a promise awaiting its processing so that getCurrentRequest can be used to take some action after the current request has processed. | ||
| currentRequestPromise = processWithMiddleware(requestToProcess, true) | ||
| .then((response) => { | ||
| Log.info('[SequentialQueue] Request processed successfully', false, { | ||
| command: requestToProcess.command, | ||
| shouldPauseQueue: response?.shouldPauseQueue ?? false, | ||
| hasQueueFlushedData: !!requestToProcess.queueFlushedData, | ||
| }); | ||
|
|
||
| // A response might indicate that the queue should be paused. This happens when a gap in onyx updates is detected between the client and the server and | ||
| // that gap needs resolved before the queue can continue. | ||
| if (response?.shouldPauseQueue) { | ||
| Log.info("[SequentialQueue] Handled 'shouldPauseQueue' in response. Pausing the queue."); | ||
| pause(); | ||
| } | ||
|
|
||
| Log.info('[SequentialQueue] Removing persisted request because it was processed successfully.', false, {request: requestToProcess}); | ||
| Log.info('[SequentialQueue] Removing persisted request because it was processed successfully.', false, { | ||
| command: requestToProcess.command, | ||
| remainingRequests: getAllPersistedRequests().length, | ||
| }); | ||
| endPersistedRequestAndRemoveFromQueue(requestToProcess); | ||
|
|
||
| if (requestToProcess.queueFlushedData) { | ||
| Log.info('[SequentialQueue] Will store queueFlushedData.', false, {queueFlushedData: requestToProcess.queueFlushedData}); | ||
| Log.info('[SequentialQueue] Will store queueFlushedData.', false, { | ||
| command: requestToProcess.command, | ||
| queueFlushedDataLength: requestToProcess.queueFlushedData.length, | ||
| }); | ||
| saveQueueFlushedData(...requestToProcess.queueFlushedData); | ||
| } | ||
|
|
||
| sequentialQueueRequestThrottle.clear(); | ||
| Log.info('[SequentialQueue] Continuing to process next request'); | ||
| return process(); | ||
| }) | ||
| .catch((error: RequestError) => { | ||
| Log.info('[SequentialQueue] Request failed with error', false, { | ||
| command: requestToProcess.command, | ||
| errorName: error.name ?? 'unknown', | ||
| errorMessage: error.message ?? 'unknown', | ||
| errorStatus: error.status ?? 'unknown', | ||
| shouldFailAllRequests, | ||
| }); | ||
|
|
||
| // On sign out we cancel any in flight requests from the user. Since that user is no longer signed in their requests should not be retried. | ||
| // Duplicate records don't need to be retried as they just mean the record already exists on the server | ||
| if (error.name === CONST.ERROR.REQUEST_CANCELLED || error.message === CONST.ERROR.DUPLICATE_RECORD || shouldFailAllRequests) { | ||
| if (shouldFailAllRequests) { | ||
| const onyxUpdates = [...((requestToProcess.failureData ?? []) as never), ...((requestToProcess.finallyData ?? []) as never)] as Array<OnyxUpdate<OnyxKey>>; | ||
| Log.info('[SequentialQueue] Applying failure and finally data because shouldFailAllRequests', false, { | ||
| command: requestToProcess.command, | ||
| updatesCount: onyxUpdates.length, | ||
| }); | ||
| Onyx.update(onyxUpdates); | ||
| } | ||
| Log.info("[SequentialQueue] Removing persisted request because it failed and doesn't need to be retried.", false, {error, request: requestToProcess}); | ||
| Log.info("[SequentialQueue] Removing persisted request because it failed and doesn't need to be retried.", false, { | ||
| command: requestToProcess.command, | ||
| errorName: error.name, | ||
| errorMessage: error.message, | ||
| }); | ||
| endPersistedRequestAndRemoveFromQueue(requestToProcess); | ||
| sequentialQueueRequestThrottle.clear(); | ||
| return process(); | ||
| } | ||
| // For rate limiting errors (429) on ResendValidateCode, don't retry to prevent spam | ||
| if (error.message === CONST.ERROR.THROTTLED && requestToProcess.command === WRITE_COMMANDS.RESEND_VALIDATE_CODE) { | ||
| Log.info('[SequentialQueue] RESEND_VALIDATE_CODE throttled, not retrying', false, { | ||
| command: requestToProcess.command, | ||
| }); | ||
| Onyx.update(requestToProcess.failureData ?? []); | ||
| endPersistedRequestAndRemoveFromQueue(requestToProcess); | ||
| sequentialQueueRequestThrottle.clear(); | ||
| return process(); | ||
| } | ||
|
|
||
| Log.info('[SequentialQueue] Will retry request after rollback and throttle delay', false, { | ||
| command: requestToProcess.command, | ||
| errorMessage: error.message, | ||
| }); | ||
|
|
||
| rollbackOngoingPersistedRequest(); | ||
| return sequentialQueueRequestThrottle | ||
| .sleep(error, requestToProcess.command) | ||
| .then(process) | ||
| .then(() => { | ||
| Log.info('[SequentialQueue] Throttle delay completed, retrying request', false, { | ||
| command: requestToProcess.command, | ||
| }); | ||
| return process(); | ||
| }) | ||
| .catch(() => { | ||
| Log.info('[SequentialQueue] Request failed too many times, giving up', false, { | ||
| command: requestToProcess.command, | ||
| errorMessage: error.message, | ||
| }); | ||
| Onyx.update(requestToProcess.failureData ?? []); | ||
| Log.info('[SequentialQueue] Removing persisted request because it failed too many times.', false, {error, request: requestToProcess}); | ||
| endPersistedRequestAndRemoveFromQueue(requestToProcess); | ||
| sequentialQueueRequestThrottle.clear(); | ||
| if (requestToProcess.command === WRITE_COMMANDS.OPEN_APP) { | ||
|
|
@@ -222,18 +281,41 @@ function flush(shouldResetPromise = true) { | |
| return; | ||
| } | ||
|
|
||
| if (getAllPersistedRequests().length === 0 && isEmpty()) { | ||
| const currentPersistedRequests = getAllPersistedRequests(); | ||
| const persistedRequestsLength = currentPersistedRequests.length; | ||
| const hasOnyxUpdates = !isEmpty(); | ||
|
|
||
| Log.info('[SequentialQueue] flush() called', false, { | ||
| shouldResetPromise, | ||
| persistedRequestsLength, | ||
| hasQueuedOnyxUpdates: hasOnyxUpdates, | ||
| isClientTheLeader: isClientTheLeader(), | ||
| }); | ||
|
|
||
| if (persistedRequestsLength === 0 && !hasOnyxUpdates) { | ||
| Log.info('[SequentialQueue] Unable to flush. No requests or queued Onyx updates to process.'); | ||
| return; | ||
| } | ||
|
|
||
| Log.info('[SequentialQueue] Checking if client is leader', false, { | ||
| persistedRequestsLength, | ||
| hasOnyxUpdates, | ||
| }); | ||
|
|
||
| // ONYXKEYS.PERSISTED_REQUESTS is shared across clients, thus every client/tab will have a copy | ||
| // It is very important to only process the queue from leader client otherwise requests will be duplicated. | ||
| if (!isClientTheLeader()) { | ||
| Log.info('[SequentialQueue] Unable to flush. Client is not the leader.'); | ||
| Log.info('[SequentialQueue] Unable to flush. Client is not the leader.', false, { | ||
| persistedRequestsLength, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| Log.info('[SequentialQueue] Starting queue processing', false, { | ||
| persistedRequestsLength, | ||
| persistedCommands: getCommands(currentPersistedRequests), | ||
| }); | ||
|
|
||
| isSequentialQueueRunning = true; | ||
|
|
||
| if (shouldResetPromise) { | ||
|
|
@@ -251,28 +333,50 @@ function flush(shouldResetPromise = true) { | |
| // an existing connection already made in PersistedRequests.ts. | ||
| reuseConnection: false, | ||
| callback: () => { | ||
| Log.info('[SequentialQueue] PERSISTED_REQUESTS loaded, starting process()', false, { | ||
| requestsLength: getAllPersistedRequests().length, | ||
| }); | ||
| Onyx.disconnect(connection); | ||
| process().finally(() => { | ||
| Log.info('[SequentialQueue] Finished processing queue.'); | ||
| const remainingRequests = getAllPersistedRequests().length; | ||
| Log.info('[SequentialQueue] Finished processing queue.', false, { | ||
| remainingRequests, | ||
| isOffline: isOffline(), | ||
| willResolvePromise: isOffline() || remainingRequests === 0, | ||
| }); | ||
|
|
||
| isSequentialQueueRunning = false; | ||
| if (isOffline() || getAllPersistedRequests().length === 0) { | ||
| if (isOffline() || remainingRequests === 0) { | ||
| Log.info('[SequentialQueue] Resolving isReadyPromise', false, { | ||
| reason: isOffline() ? 'offline' : 'queue empty', | ||
| }); | ||
| resolveIsReadyPromise?.(); | ||
| } | ||
| currentRequestPromise = null; | ||
|
|
||
| // The queue can be paused when we sync the data with backend so we should only update the Onyx data when the queue is empty | ||
| if (getAllPersistedRequests().length === 0) { | ||
| if (remainingRequests === 0) { | ||
| Log.info('[SequentialQueue] Queue is empty, flushing Onyx updates'); | ||
| flushOnyxUpdatesQueue()?.then(() => { | ||
| const queueFlushedData = getQueueFlushedData(); | ||
| if (queueFlushedData.length === 0) { | ||
| Log.info('[SequentialQueue] No queueFlushedData to apply'); | ||
| return; | ||
| } | ||
| Log.info('[SequentialQueue] Will store queueFlushedData.', false, {queueFlushedData}); | ||
| Log.info('[SequentialQueue] Applying queueFlushedData', false, { | ||
| queueFlushedDataLength: queueFlushedData.length, | ||
| }); | ||
| Onyx.update(queueFlushedData).then(() => { | ||
| Log.info('[SequentialQueue] QueueFlushedData has been stored.', false, {queueFlushedData}); | ||
| Log.info('[SequentialQueue] QueueFlushedData has been applied and stored', false, { | ||
| queueFlushedDataLength: queueFlushedData.length, | ||
| }); | ||
| clearQueueFlushedData(); | ||
| }); | ||
| }); | ||
| } else { | ||
| Log.info('[SequentialQueue] Queue still has requests, NOT flushing Onyx updates', false, { | ||
| remainingRequests, | ||
| }); | ||
| } | ||
| }); | ||
| }, | ||
|
|
@@ -288,19 +392,30 @@ function unpause() { | |
| return; | ||
| } | ||
|
|
||
| const numberOfPersistedRequests = getAllPersistedRequests().length || 0; | ||
| Log.info(`[SequentialQueue] Unpausing the queue and flushing ${numberOfPersistedRequests} requests`); | ||
| const currentPersistedRequests = getAllPersistedRequests(); | ||
| const numberOfPersistedRequests = currentPersistedRequests.length; | ||
| const persistedCommands = getCommands(currentPersistedRequests); | ||
|
|
||
| Log.info('[SequentialQueue] Unpausing the queue', false, { | ||
| numberOfPersistedRequests, | ||
| persistedCommands, | ||
| }); | ||
|
|
||
| isQueuePaused = false; | ||
|
|
||
| // If there are no persisted requests, we need to flush the Onyx updates queue | ||
| if (numberOfPersistedRequests === 0) { | ||
| Log.info('[SequentialQueue] No persisted requests, flushing Onyx updates queue'); | ||
| flushOnyxUpdatesQueue(); | ||
| } | ||
|
|
||
| // When the queue is paused and then unpaused, we call flush which by defaults recreates the isReadyPromise. | ||
| // After all the WRITE requests are done, the isReadyPromise is resolved, but since it's a new instance of promise, | ||
| // the pending READ request never received the resolved callback. That's why we don't want to recreate | ||
| // the promise when unpausing the queue. | ||
| Log.info('[SequentialQueue] Calling flush(false) to start processing', false, { | ||
| numberOfPersistedRequests, | ||
| }); | ||
| flush(false); | ||
| } | ||
|
|
||
|
|
@@ -323,53 +438,114 @@ onReconnection(flush); | |
| onPersistedRequestsInitialization(flush); | ||
|
TMisiukiewicz marked this conversation as resolved.
Resolved
|
||
|
|
||
| function handleConflictActions<TKey extends OnyxKey>(conflictAction: ConflictData, newRequest: OnyxRequest<TKey>) { | ||
| Log.info('[SequentialQueue] handleConflictActions', false, { | ||
| conflictType: conflictAction.type, | ||
| newCommand: newRequest.command, | ||
| currentQueueLength: getAllPersistedRequests().length, | ||
| }); | ||
|
|
||
| if (conflictAction.type === 'push') { | ||
| Log.info('[SequentialQueue] Conflict resolution: PUSH', false, { | ||
| command: newRequest.command, | ||
| }); | ||
| savePersistedRequest(newRequest); | ||
| } else if (conflictAction.type === 'replace') { | ||
| Log.info('[SequentialQueue] Conflict resolution: REPLACE', false, { | ||
| command: newRequest.command, | ||
| replaceIndex: conflictAction.index, | ||
| replacementRequest: conflictAction.request?.command ?? newRequest.command, | ||
| }); | ||
| updatePersistedRequest(conflictAction.index, conflictAction.request ?? (newRequest as AnyRequest)); | ||
| } else if (conflictAction.type === 'delete') { | ||
| Log.info('[SequentialQueue] Conflict resolution: DELETE', false, { | ||
| command: newRequest.command, | ||
| deleteIndices: conflictAction.indices, | ||
| willPushNewRequest: conflictAction.pushNewRequest ?? false, | ||
| hasNextAction: !!conflictAction.nextAction, | ||
| }); | ||
| deletePersistedRequestsByIndices(conflictAction.indices); | ||
| if (conflictAction.pushNewRequest) { | ||
| Log.info('[SequentialQueue] Pushing new request after delete', false, { | ||
| command: newRequest.command, | ||
| }); | ||
| savePersistedRequest(newRequest); | ||
| } | ||
| if (conflictAction.nextAction) { | ||
| Log.info('[SequentialQueue] Processing next conflict action', false, { | ||
| command: newRequest.command, | ||
| nextActionType: conflictAction.nextAction.type, | ||
| }); | ||
| handleConflictActions(conflictAction.nextAction, newRequest); | ||
| } | ||
| } else { | ||
| Log.info(`[SequentialQueue] No action performed to command ${newRequest.command} and it will be ignored.`); | ||
| Log.info('[SequentialQueue] No action performed, request ignored', false, { | ||
| command: newRequest.command, | ||
| conflictType: conflictAction.type, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| function push<TKey extends OnyxKey>(newRequest: OnyxRequest<TKey>) { | ||
| const currentRequests = getAllPersistedRequests(); | ||
| Log.info('[SequentialQueue] push() called', false, { | ||
| command: newRequest.command, | ||
| hasConflictChecker: !!newRequest.checkAndFixConflictingRequest, | ||
| currentQueueLength: currentRequests.length, | ||
| isOffline: isOffline(), | ||
| isSequentialQueueRunning, | ||
| }); | ||
|
|
||
| if (newRequest.checkAndFixConflictingRequest) { | ||
| const requests = getAllPersistedRequests(); | ||
| const requests = currentRequests; | ||
| Log.info('[SequentialQueue] Checking for conflicts', false, { | ||
| command: newRequest.command, | ||
| existingRequestsCount: requests.length, | ||
| }); | ||
|
|
||
| const {conflictAction} = newRequest.checkAndFixConflictingRequest(requests as Array<OnyxRequest<TKey>>); | ||
| Log.info(`[SequentialQueue] Conflict action for command ${newRequest.command} - ${conflictAction.type}:`); | ||
| Log.info('[SequentialQueue] Conflict action determined', false, { | ||
| command: newRequest.command, | ||
| conflictType: conflictAction.type, | ||
| }); | ||
|
|
||
| // don't try to serialize a function. | ||
| // eslint-disable-next-line no-param-reassign | ||
| delete newRequest.checkAndFixConflictingRequest; | ||
| handleConflictActions(conflictAction, newRequest); | ||
| } else { | ||
| Log.info('[SequentialQueue] No conflict action. Adding request to Persisted Requests', false, {command: newRequest.command}); | ||
| Log.info('[SequentialQueue] No conflict action. Adding request to Persisted Requests', false, { | ||
| command: newRequest.command, | ||
| }); | ||
| // Add request to Persisted Requests so that it can be retried if it fails | ||
| savePersistedRequest(newRequest); | ||
| } | ||
|
|
||
| // If we are offline we don't need to trigger the queue to empty as it will happen when we come back online | ||
| if (isOffline()) { | ||
| Log.info('[SequentialQueue] Unable to push request due to offline status'); | ||
| Log.info('[SequentialQueue] Unable to push request due to offline status', false, { | ||
| command: newRequest.command, | ||
| queueLength: getAllPersistedRequests().length, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here — |
||
| }); | ||
| return; | ||
| } | ||
|
|
||
| // If the queue is running this request will run once it has finished processing the current batch | ||
| if (isSequentialQueueRunning) { | ||
| Log.info('[SequentialQueue] Queue is running. Will flush when the current request is finished.'); | ||
| isReadyPromise.then(() => flush(true)); | ||
| Log.info('[SequentialQueue] Queue is running. Will flush when the current request is finished.', false, { | ||
| command: newRequest.command, | ||
| }); | ||
| isReadyPromise.then(() => { | ||
| Log.info('[SequentialQueue] isReadyPromise resolved, flushing queue', false, { | ||
| command: newRequest.command, | ||
| }); | ||
| flush(true); | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| Log.info('[SequentialQueue] Queue is not running. Flushing the queue.'); | ||
| Log.info('[SequentialQueue] Queue is not running. Flushing the queue.', false, { | ||
| command: newRequest.command, | ||
| }); | ||
| flush(true); | ||
| } | ||
|
|
||
|
|
||
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.
Is it intentional to use
getAllPersistedRequests()instead ofpersistedRequests?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.
Yep, intentional — the local
persistedRequestsis captured before the async callback so it can be stale by the time we get here. Grabbing fresh data to get the actual state.