Skip to content

Commit

Permalink
Log event when we find an operation to suspend on
Browse files Browse the repository at this point in the history
Summary:
As part of our discussion around exploring a Reactive Key Value store architecture, we identified that OperationTracker would be incompatible in its current form.

As a baseline we would like to be able to collect data showing how effective OperationTacker is today. One way to measure that would be: How often does it identify an operation on which we could suspend that is _not_ the fragment's parent operation. This log event will allow us to track that statistic in prod.

Reviewed By: rbalicki2

Differential Revision: D43294511

fbshipit-source-id: 6e6f648234dc95b2b71ecd01b918a16df47a9c87
  • Loading branch information
captbaritone authored and facebook-github-bot committed Feb 17, 2023
1 parent ee9391e commit bd0c22a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

'use strict';

import type {LogEvent} from 'relay-runtime/store/RelayStoreTypes';

const {createFragmentResource} = require('../FragmentResource');
const invariant = require('invariant');
const {
createOperationDescriptor,
createReaderSelector,
Expand All @@ -37,16 +40,19 @@ describe('FragmentResource with Operation Tracker and Missing Data', () => {
let operationTracker;
let viewerOperation;
let nodeOperation;
let logger;

beforeEach(() => {
operationLoader = {
load: jest.fn(),
get: jest.fn(),
};
operationTracker = new RelayOperationTracker();
logger = jest.fn<[LogEvent], void>();
environment = createMockEnvironment({
operationTracker,
operationLoader,
log: logger,
});
NodeQuery = graphql`
query FragmentResourceWithOperationTrackerTestNodeQuery($id: ID!) {
Expand Down Expand Up @@ -262,6 +268,24 @@ describe('FragmentResource with Operation Tracker and Missing Data', () => {
}
// Assert that promise from first read was cached
expect(cached).toBe(thrown);

// Assert that we logged a 'pendingoperation.found' event.
const pendingOperationFoundEvents = logger.mock.calls
.map(([event]) => event)
.filter(event => event.name === 'pendingoperation.found');

expect(pendingOperationFoundEvents.length).toBe(1);
const event = pendingOperationFoundEvents[0];
invariant(event.name === 'pendingoperation.found');
expect(event.fragment.name).toBe(
'FragmentResourceWithOperationTrackerTestPlainUserNameRenderer_name',
);
expect(event.fragmentOwner.node.operation.name).toBe(
viewerOperation.request.node.operation.name,
);
expect(
event.pendingOperations.map(owner => owner.node.operation.name),
).toEqual(['FragmentResourceWithOperationTrackerTestNodeQuery']);
});

it('should read the data from the store once operation fully completed', () => {
Expand Down
9 changes: 9 additions & 0 deletions packages/relay-runtime/store/RelayStoreTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,15 @@ export type LogEvent =
// Are we reading this result from the fragment resource cache?
+cached: boolean,
}
| {
// Indicates getPendingOperationForFragment identified a pending operation.
// Useful for measuring how frequently RelayOperationTracker identifies a
// related operation on which to suspend.
+name: 'pendingoperation.found',
+fragment: ReaderFragment,
+fragmentOwner: RequestDescriptor,
+pendingOperations: $ReadOnlyArray<RequestDescriptor>,
}
| {
+name: 'network.info',
+networkRequestId: number,
Expand Down
10 changes: 10 additions & 0 deletions packages/relay-runtime/util/getPendingOperationsForFragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ function getPendingOperationsForFragment(
: `Relay(${pendingOperationName}:${fragmentName})`;
// $FlowExpectedError[prop-missing] Expando to annotate Promises.
promise.displayName = promiseDisplayName;

// In order to monitor the efficacy of RelayOperationTracker, we log
// enough information to track whether we are suspending on the fragment
// owner's operation, or some other operation.
environment.__log({
name: 'pendingoperation.found',
fragment: fragmentNode,
fragmentOwner,
pendingOperations,
});
return {promise, pendingOperations};
}

Expand Down

0 comments on commit bd0c22a

Please sign in to comment.