-
Notifications
You must be signed in to change notification settings - Fork 115
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
[IND-402] Cache and send order updates for stateful orders. #683
Merged
vincentwschau
merged 7 commits into
main
from
vincentc/ind-402-send-cached-stateful-order-updates
Oct 23, 2023
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1a79d21
[IND-402] Add cache to store order updates for stateful orders.
vincentwschau ce25de4
Expose new cache.
vincentwschau b966802
Fix typo.
vincentwschau b6a2c7a
[IND-402] Send cached stateful order updates.
vincentwschau fbe8b0b
Remove uneeded file.
vincentwschau 2352bd2
Merge branch 'main' into vincentc/ind-402-send-cached-stateful-order-…
vincentwschau 51cea70
Add config / start loop in service.
vincentwschau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
indexer/services/roundtable/__tests__/tasks/remove-old-order-updates.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { stats } from '@dydxprotocol-indexer/base'; | ||
import { | ||
StatefulOrderUpdateInfo, | ||
StatefulOrderUpdatesCache, | ||
redis, | ||
redisTestConstants, | ||
} from '@dydxprotocol-indexer/redis'; | ||
import config from '../../src/config'; | ||
import removeOldOrderUpdatesTask from '../../src/tasks/remove-old-order-updates'; | ||
import { redisClient } from '../../src/helpers/redis'; | ||
|
||
describe('remove-old-order-updates', () => { | ||
const fakeTime: Date = new Date(2023, 9, 25, 0, 0, 0, 0); | ||
|
||
beforeAll(() => { | ||
jest.useFakeTimers().setSystemTime(fakeTime); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.resetAllMocks(); | ||
jest.useRealTimers(); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.spyOn(stats, 'gauge'); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await redis.deleteAllAsync(redisClient); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('succeeds with no cached order updates', async () => { | ||
await removeOldOrderUpdatesTask(); | ||
expect(stats.gauge).toHaveBeenCalledWith( | ||
`${config.SERVICE_NAME}.remove_old_order_updates.num_removed`, | ||
0, | ||
); | ||
}); | ||
|
||
it('succeeds with no old cached order updates', async () => { | ||
await StatefulOrderUpdatesCache.addStatefulOrderUpdate( | ||
redisTestConstants.defaultOrderUuidGoodTilBlockTime, | ||
redisTestConstants.orderUpdate.orderUpdate, | ||
fakeTime.getTime() - 1, | ||
redisClient, | ||
); | ||
const existingUpdates: StatefulOrderUpdateInfo[] = await StatefulOrderUpdatesCache | ||
.getOldOrderUpdates( | ||
fakeTime.getTime(), | ||
redisClient, | ||
); | ||
expect(existingUpdates).toHaveLength(1); | ||
|
||
await removeOldOrderUpdatesTask(); | ||
expect(stats.gauge).toHaveBeenCalledWith( | ||
`${config.SERVICE_NAME}.remove_old_order_updates.num_removed`, | ||
0, | ||
); | ||
|
||
const updatesAfterTask: StatefulOrderUpdateInfo[] = await StatefulOrderUpdatesCache | ||
.getOldOrderUpdates( | ||
fakeTime.getTime(), | ||
redisClient, | ||
); | ||
expect(updatesAfterTask).toEqual(existingUpdates); | ||
}); | ||
|
||
it('succeeds with no old cached order updates', async () => { | ||
await StatefulOrderUpdatesCache.addStatefulOrderUpdate( | ||
redisTestConstants.defaultOrderUuidGoodTilBlockTime, | ||
redisTestConstants.orderUpdate.orderUpdate, | ||
fakeTime.getTime() - 1, | ||
redisClient, | ||
); | ||
const existingUpdates: StatefulOrderUpdateInfo[] = await StatefulOrderUpdatesCache | ||
.getOldOrderUpdates( | ||
fakeTime.getTime(), | ||
redisClient, | ||
); | ||
expect(existingUpdates).toHaveLength(1); | ||
|
||
await removeOldOrderUpdatesTask(); | ||
expect(stats.gauge).toHaveBeenCalledWith( | ||
`${config.SERVICE_NAME}.remove_old_order_updates.num_removed`, | ||
0, | ||
); | ||
|
||
const updatesAfterTask: StatefulOrderUpdateInfo[] = await StatefulOrderUpdatesCache | ||
.getOldOrderUpdates( | ||
fakeTime.getTime(), | ||
redisClient, | ||
); | ||
expect(updatesAfterTask).toEqual(existingUpdates); | ||
}); | ||
|
||
it('succeeds removing old cached order update', async () => { | ||
await StatefulOrderUpdatesCache.addStatefulOrderUpdate( | ||
redisTestConstants.defaultOrderUuidGoodTilBlockTime, | ||
redisTestConstants.orderUpdate.orderUpdate, | ||
fakeTime.getTime() - config.OLD_CACHED_ORDER_UPDATES_WINDOW_MS, | ||
redisClient, | ||
); | ||
const existingUpdates: StatefulOrderUpdateInfo[] = await StatefulOrderUpdatesCache | ||
.getOldOrderUpdates( | ||
fakeTime.getTime(), | ||
redisClient, | ||
); | ||
expect(existingUpdates).toHaveLength(1); | ||
|
||
await removeOldOrderUpdatesTask(); | ||
expect(stats.gauge).toHaveBeenCalledWith( | ||
`${config.SERVICE_NAME}.remove_old_order_updates.num_removed`, | ||
1, | ||
); | ||
|
||
const updatesAfterTask: StatefulOrderUpdateInfo[] = await StatefulOrderUpdatesCache | ||
.getOldOrderUpdates( | ||
fakeTime.getTime(), | ||
redisClient, | ||
); | ||
expect(updatesAfterTask).toHaveLength(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
indexer/services/roundtable/src/tasks/remove-old-order-updates.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { | ||
logger, | ||
stats, | ||
} from '@dydxprotocol-indexer/base'; | ||
import { | ||
StatefulOrderUpdateInfo, | ||
StatefulOrderUpdatesCache, | ||
} from '@dydxprotocol-indexer/redis'; | ||
import { OrderUpdateV1 } from '@dydxprotocol-indexer/v4-protos'; | ||
|
||
import config from '../config'; | ||
import { redisClient } from '../helpers/redis'; | ||
|
||
/** | ||
* This task removes any old cached stateful orer updates from the StatefulOrderUpdates cache | ||
*/ | ||
export default async function runTask(): Promise<void> { | ||
const start: number = Date.now(); | ||
|
||
try { | ||
const oldUpdateCutoff: number = Date.now() - config.OLD_CACHED_ORDER_UPDATES_WINDOW_MS; | ||
|
||
const oldUpdateInfo: StatefulOrderUpdateInfo[] = await StatefulOrderUpdatesCache | ||
.getOldOrderUpdates( | ||
oldUpdateCutoff, | ||
redisClient, | ||
); | ||
const removedUpdates: OrderUpdateV1[] = (await Promise.all( | ||
oldUpdateInfo.map( | ||
(updateInfo: StatefulOrderUpdateInfo): Promise<OrderUpdateV1 | undefined> => { | ||
return StatefulOrderUpdatesCache.removeStatefulOrderUpdate( | ||
updateInfo.orderId, | ||
updateInfo.timestamp, | ||
redisClient, | ||
); | ||
}, | ||
), | ||
)).filter( | ||
(removedUpdate: OrderUpdateV1 | undefined): removedUpdate is OrderUpdateV1 => { | ||
if (removedUpdate !== undefined) { | ||
logger.info({ | ||
at: 'remove-old-order-updates#runTask', | ||
message: 'Removed old stateful order update', | ||
removedUpdate, | ||
}); | ||
return true; | ||
} | ||
return false; | ||
}, | ||
); | ||
|
||
stats.gauge( | ||
`${config.SERVICE_NAME}.remove_old_order_updates.num_removed`, | ||
removedUpdates.length, | ||
); | ||
} catch (error) { | ||
logger.error({ | ||
at: 'remove-old-order-updates#runTas', | ||
message: 'Error occurred in task to remove old stateful order updates', | ||
error, | ||
}); | ||
} finally { | ||
stats.timing( | ||
`${config.SERVICE_NAME}.remove_old_order_updates`, | ||
Date.now() - start, | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
are you going to create a followup PR for index.ts changes?
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.
Good point, I missed adding the code to run the task. Will add.