-
Notifications
You must be signed in to change notification settings - Fork 2
Add network chaos to fork tests #1570
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e7dadb3
Add network chaos to fork tests
neekolas e3207b5
Set back to 20 epochs
neekolas ac7a406
Add streams to fork test
neekolas b193caa
Add DB chaos by locking DB file
neekolas c2c0187
Refactor chaos
neekolas 822709e
Clone workers in streams
neekolas d9e7df2
Temporarily turn on device sync and multiple client instances
neekolas 8f23c7b
Properly catch missing groups
neekolas 20e7ef4
Revert "Temporarily turn on device sync and multiple client instances"
neekolas 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import type { ChaosProvider } from "@chaos/provider"; | ||
| import type { WorkerManager } from "@workers/manager"; | ||
|
|
||
| export type DbChaosConfig = { | ||
| minLockTime: number; // Minimum duration in milliseconds to lock the database | ||
| maxLockTime: number; // Maximum duration in milliseconds to lock the database | ||
| lockInterval: number; // Interval in milliseconds between lock attempts | ||
| impactedWorkerPercentage: number; // number between 0 and 100 for what % of workers to lock on each run | ||
| }; | ||
|
|
||
| export class DbChaos implements ChaosProvider { | ||
| config: DbChaosConfig; | ||
| activeLocks = new Map<string, Promise<void>>(); | ||
| interval?: NodeJS.Timeout; | ||
|
|
||
| constructor(config: DbChaosConfig) { | ||
| validateConfig(config); | ||
| this.config = config; | ||
| } | ||
|
|
||
| start(workers: WorkerManager): Promise<void> { | ||
| const { minLockTime, maxLockTime, lockInterval, impactedWorkerPercentage } = | ||
| this.config; | ||
| console.log( | ||
| `Starting DB Chaos: | ||
| Locking for ${minLockTime}ms - ${maxLockTime}ms | ||
| Interval: ${lockInterval}ms`, | ||
| ); | ||
| this.interval = setInterval(() => { | ||
| for (const worker of workers.getAll()) { | ||
| if (Math.random() * 100 > impactedWorkerPercentage) { | ||
| continue; | ||
| } | ||
| const duration = Math.floor( | ||
| minLockTime + Math.random() * (maxLockTime - minLockTime), | ||
| ); | ||
|
|
||
| const lockKey = `${worker.name}-${worker.installationId}`; | ||
|
|
||
| // Only lock if not already locked | ||
| if (!this.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(() => { | ||
| this.activeLocks.delete(lockKey); | ||
| }); | ||
|
|
||
| this.activeLocks.set(lockKey, lockPromise); | ||
| } | ||
| } | ||
| }, lockInterval); | ||
|
|
||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| async stop() { | ||
| console.log("Stopping DB Chaos"); | ||
| if (this.interval) { | ||
| clearInterval(this.interval); | ||
| } | ||
|
|
||
| // Wait for all the existing locks to complete | ||
| await Promise.allSettled(Array.from(this.activeLocks.values())); | ||
| } | ||
| } | ||
|
|
||
| function validateConfig(config: DbChaosConfig): void { | ||
| if (config.minLockTime > config.maxLockTime) { | ||
| throw new Error( | ||
| "Minimum lock time cannot be greater than maximum lock time", | ||
| ); | ||
| } | ||
|
|
||
| if ( | ||
| config.impactedWorkerPercentage < 0 || | ||
| config.impactedWorkerPercentage > 100 | ||
| ) { | ||
| throw new Error("Impacted worker percentage must be between 0 and 100"); | ||
| } | ||
|
|
||
| if (!config.lockInterval) { | ||
| throw new Error("Lock interval must be defined"); | ||
| } | ||
|
|
||
| if (config.impactedWorkerPercentage === undefined) { | ||
| throw new Error("Impacted worker percentage must be defined"); | ||
| } | ||
| } | ||
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,95 @@ | ||
| import type { ChaosProvider } from "@chaos/provider"; | ||
| import type { DockerContainer } from "network-stability/container"; | ||
|
|
||
| export type NetworkChaosConfig = { | ||
| delayMin: number; // Minimum delay in ms | ||
| delayMax: number; // Maximum delay in ms | ||
| jitterMin: number; // Minimum jitter in ms | ||
| jitterMax: number; // Maximum jitter in ms | ||
| lossMin: number; // Minimum packet loss percentage (0-100) | ||
| lossMax: number; // Maximum packet loss percentage (0-100) | ||
| interval: number; // How often to apply chaos in ms | ||
| }; | ||
|
|
||
| export class NetworkChaos implements ChaosProvider { | ||
| config: NetworkChaosConfig; | ||
| interval?: NodeJS.Timeout; | ||
| nodes: DockerContainer[]; | ||
|
|
||
| constructor(config: NetworkChaosConfig, nodes: DockerContainer[]) { | ||
| this.config = config; | ||
| this.nodes = nodes; | ||
| } | ||
|
|
||
| start(): Promise<void> { | ||
| console.log(`Starting network chaos: | ||
| Nodes: ${this.nodes.map((node) => node.name).join(", ")} | ||
| Delay: ${this.config.delayMin}ms - ${this.config.delayMax}ms | ||
| Jitter: ${this.config.jitterMin}ms - ${this.config.jitterMax}ms | ||
| Loss: ${this.config.lossMin}% - ${this.config.lossMax}% | ||
| Interval: ${this.config.interval}ms`); | ||
|
|
||
| validateContainers(this.nodes); | ||
| this.clearAll(); | ||
|
|
||
| this.interval = setInterval(() => { | ||
neekolas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (const node of this.nodes) { | ||
| this.applyToNode(node); | ||
| } | ||
| }, this.config.interval); | ||
|
|
||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| private applyToNode(node: DockerContainer) { | ||
| const { delayMin, delayMax, jitterMin, jitterMax, lossMin, lossMax } = | ||
| this.config; | ||
| const delay = Math.floor(delayMin + Math.random() * (delayMax - delayMin)); | ||
| const jitter = Math.floor( | ||
| jitterMin + Math.random() * (jitterMax - jitterMin), | ||
| ); | ||
| const loss = lossMin + Math.random() * (lossMax - lossMin); | ||
|
|
||
| try { | ||
| node.addJitter(delay, jitter); | ||
| node.addLoss(loss); | ||
| } catch (err) { | ||
| console.warn(`[chaos] Error applying netem on ${node.name}:`, err); | ||
| } | ||
| } | ||
|
|
||
| clearAll() { | ||
| for (const node of this.nodes) { | ||
| try { | ||
| node.clearLatency(); | ||
| } catch (err) { | ||
| console.warn(`[chaos] Error clearing latency on ${node.name}:`, err); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| stop(): Promise<void> { | ||
| if (this.interval) { | ||
| clearInterval(this.interval); | ||
| } | ||
|
|
||
| this.clearAll(); | ||
|
|
||
| return Promise.resolve(); | ||
| } | ||
| } | ||
|
|
||
| const validateContainers = (allNodes: DockerContainer[]) => { | ||
humanagent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (const node of allNodes) { | ||
| try { | ||
| // Test if container exists by trying to get its IP | ||
| if (!node.ip || !node.veth) { | ||
| throw new Error(`Container ${node.name} has no IP address`); | ||
| } | ||
| } catch { | ||
| throw new Error( | ||
| `Docker container ${node.name} is not running. Network chaos requires local multinode setup (./dev/up).`, | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
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,10 @@ | ||
| import { type WorkerManager } from "@workers/manager"; | ||
|
|
||
| // Generic interface for the Chaos Provider. | ||
| // A Chaos Provider is started after the test has been setup, but before we start performing actions | ||
| // It is stopped after the core of the test has been completed, and should remove all chaos so that final | ||
| // validations can be performed cleanly. | ||
| export interface ChaosProvider { | ||
| start(workers: WorkerManager): Promise<void>; | ||
| stop(): Promise<void>; | ||
| } |
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,41 @@ | ||
| import type { ChaosProvider } from "@chaos/provider"; | ||
| import { typeofStream, type WorkerClient } from "@workers/main"; | ||
| import type { WorkerManager } from "@workers/manager"; | ||
|
|
||
| export type StreamsConfig = { | ||
| cloned: boolean; // Should the stream be run against the workers used in the tests, or a cloned client instance? | ||
| }; | ||
|
|
||
| export class StreamsChaos implements ChaosProvider { | ||
| workers?: WorkerClient[]; | ||
| config: StreamsConfig; | ||
|
|
||
| constructor(config: StreamsConfig) { | ||
| this.config = config; | ||
| } | ||
|
|
||
| async start(workers: WorkerManager) { | ||
| console.log("Starting StreamsChaos"); | ||
| let allWorkers = workers.getAll().map((w) => w.worker); | ||
| if (this.config.cloned) { | ||
| allWorkers = await Promise.all(allWorkers.map((w) => w.clone())); | ||
| } | ||
|
|
||
| this.workers = allWorkers; | ||
| for (const worker of allWorkers) { | ||
| worker.startStream(typeofStream.Message); | ||
| } | ||
|
|
||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| stop() { | ||
humanagent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (this.workers) { | ||
| for (const worker of this.workers) { | ||
| worker.stopStreams(); | ||
| } | ||
| } | ||
|
|
||
| return Promise.resolve(); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.