-
Notifications
You must be signed in to change notification settings - Fork 2
Add DB chaos by locking DB file #1578
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
Draft
neekolas
wants to merge
1
commit into
11-13-add_streams_to_fork_test
Choose a base branch
from
11-13-add_db_chaos_by_locking_db_file
base: 11-13-add_streams_to_fork_test
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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,91 @@ | ||
| import type { Worker } from "@workers/manager"; | ||
|
|
||
| // Store active lock promises for cleanup | ||
| const activeLocks = new Map<string, Promise<void>>(); | ||
|
|
||
| /** | ||
| * Applies database lock chaos to a random selection of workers | ||
| * @param allWorkers - Array of workers to potentially lock | ||
| * @param lockDurationMin - Minimum lock duration in ms | ||
| * @param lockDurationMax - Maximum lock duration in ms | ||
| */ | ||
| const applyDbChaos = ( | ||
| allWorkers: Worker[], | ||
| lockDurationMin: number, | ||
| lockDurationMax: number, | ||
| ) => { | ||
| console.log("[db-chaos] Applying database locks..."); | ||
|
|
||
| for (const worker of allWorkers) { | ||
| // Randomly decide whether to lock this worker's DB (50% chance) | ||
| if (Math.random() < 0.5) { | ||
| const duration = Math.floor( | ||
| lockDurationMin + Math.random() * (lockDurationMax - lockDurationMin), | ||
| ); | ||
|
|
||
| const lockKey = `${worker.name}-${worker.installationId}`; | ||
|
|
||
| // Only lock if not already locked | ||
| if (!activeLocks.has(lockKey)) { | ||
| console.log( | ||
| `[db-chaos] Locking ${worker.name} database for ${duration}ms`, | ||
| ); | ||
|
|
||
| // Call the lockDB method on the worker and track it | ||
| const lockPromise = worker.worker | ||
| .lockDB(duration) | ||
| .catch((err: unknown) => { | ||
| console.warn(err); | ||
| }) | ||
| .finally(() => { | ||
| activeLocks.delete(lockKey); | ||
| }); | ||
|
|
||
| activeLocks.set(lockKey, lockPromise); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Starts the database chaos loop | ||
| * @param allWorkers - Array of workers to apply chaos to | ||
| * @param lockDurationMin - Minimum lock duration in ms | ||
| * @param lockDurationMax - Maximum lock duration in ms | ||
| * @param interval - How often to apply chaos in ms | ||
| * @returns Interval ID for cleanup | ||
| */ | ||
| export const startDbChaos = ( | ||
| allWorkers: Worker[], | ||
| lockDurationMin: number, | ||
| lockDurationMax: number, | ||
| interval: number, | ||
| ): NodeJS.Timeout => { | ||
| console.log(`[db-chaos] Initialized for ${allWorkers.length} workers`); | ||
| console.log( | ||
| `[db-chaos] Lock duration: ${lockDurationMin}-${lockDurationMax}ms, interval: ${interval}ms`, | ||
| ); | ||
|
|
||
| // Function to apply chaos to workers | ||
| const applyChaos = () => { | ||
| applyDbChaos(allWorkers, lockDurationMin, lockDurationMax); | ||
| }; | ||
|
|
||
| return setInterval(applyChaos, interval); | ||
| }; | ||
|
|
||
| /** | ||
| * Clears all active database locks | ||
| */ | ||
| export const clearDbChaos = async () => { | ||
| console.log("[db-chaos] Clearing all active database locks..."); | ||
|
|
||
| // Wait for all active locks to complete | ||
| if (activeLocks.size > 0) { | ||
| console.log(`[db-chaos] Waiting for ${activeLocks.size} locks to clear...`); | ||
| await Promise.allSettled(Array.from(activeLocks.values())); | ||
| } | ||
|
|
||
| activeLocks.clear(); | ||
| console.log("[db-chaos] Cleanup complete"); | ||
| }; |
This file contains hidden or 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
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.
showHelp()lists defaults that donβt matchmain()(db-lock-time-max2000 vs 6000,db-lock-interval15000 vs 5000). Consider updating the help text to reflect the actual runtime defaults.