Skip to content

Commit

Permalink
Simmy API review Part 2 - Prefer Chaos over Monkey (#1913)
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-csala authored Jan 22, 2024
1 parent 24a86c8 commit 54becab
Show file tree
Hide file tree
Showing 64 changed files with 540 additions and 538 deletions.
12 changes: 6 additions & 6 deletions docs/chaos/behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## About

- **Options**: [`BehaviorStrategyOptions`](xref:Polly.Simmy.Behavior.BehaviorStrategyOptions)
- **Options**: [`ChaosBehaviorStrategyOptions`](xref:Polly.Simmy.Behavior.ChaosBehaviorStrategyOptions)
- **Extensions**: `AddChaosBehavior`
- **Strategy Type**: Proactive

Expand All @@ -18,15 +18,15 @@ The behavior chaos strategy is designed to inject custom behaviors into system o
<!-- snippet: chaos-behavior-usage -->
```cs
// To use a custom delegated for injected behavior
var optionsWithBehaviorGenerator = new BehaviorStrategyOptions
var optionsWithBehaviorGenerator = new ChaosBehaviorStrategyOptions
{
BehaviorAction = static args => RestartRedisVM(),
Enabled = true,
InjectionRate = 0.05
};

// To get notifications when a behavior is injected
var optionsOnBehaviorInjected = new BehaviorStrategyOptions
var optionsOnBehaviorInjected = new ChaosBehaviorStrategyOptions
{
BehaviorAction = static args => RestartRedisVM(),
Enabled = true,
Expand All @@ -38,7 +38,7 @@ var optionsOnBehaviorInjected = new BehaviorStrategyOptions
}
};

// Add a behavior strategy with a BehaviorStrategyOptions instance to the pipeline
// Add a behavior strategy with a ChaosBehaviorStrategyOptions instance to the pipeline
new ResiliencePipelineBuilder().AddChaosBehavior(optionsWithBehaviorGenerator);
new ResiliencePipelineBuilder<HttpResponseMessage>().AddChaosBehavior(optionsOnBehaviorInjected);

Expand All @@ -60,7 +60,7 @@ var pipeline = new ResiliencePipelineBuilder()
MaxRetryAttempts = 4,
Delay = TimeSpan.FromSeconds(3),
})
.AddChaosBehavior(new BehaviorStrategyOptions // Chaos strategies are usually placed as the last ones in the pipeline
.AddChaosBehavior(new ChaosBehaviorStrategyOptions // Chaos strategies are usually placed as the last ones in the pipeline
{
BehaviorAction = static args => RestartRedisVM(),
Enabled = true,
Expand Down Expand Up @@ -130,4 +130,4 @@ Use behavior strategies to inject delays.

✅ DO

Use the latency chaos instead as the [`LatencyChaosStrategy`](latency.md) already correctly handles synchronous/asynchronous delay executions, cancellations, etc.
Use the latency chaos instead as the [`ChaosLatencyStrategy`](latency.md) already correctly handles synchronous/asynchronous delay executions, cancellations, etc.
16 changes: 8 additions & 8 deletions docs/chaos/fault.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## About

- **Options**: [`FaultStrategyOptions`](xref:Polly.Simmy.Fault.FaultStrategyOptions)
- **Options**: [`ChaosFaultStrategyOptions`](xref:Polly.Simmy.Fault.ChaosFaultStrategyOptions)
- **Extensions**: `AddChaosFault`
- **Strategy Type**: Proactive

Expand All @@ -18,7 +18,7 @@ The fault chaos strategy is designed to introduce faults (exceptions) into the s
<!-- snippet: chaos-fault-usage -->
```cs
// 10% of invocations will be randomly affected and one of the exceptions will be thrown (equal probability).
var optionsBasic = new FaultStrategyOptions
var optionsBasic = new ChaosFaultStrategyOptions
{
FaultGenerator = new FaultGenerator()
.AddException<InvalidOperationException>() // Uses default constructor
Expand All @@ -28,7 +28,7 @@ var optionsBasic = new FaultStrategyOptions
};

// To use a custom delegate to generate the fault to be injected
var optionsWithFaultGenerator = new FaultStrategyOptions
var optionsWithFaultGenerator = new ChaosFaultStrategyOptions
{
FaultGenerator = static args =>
{
Expand All @@ -48,7 +48,7 @@ var optionsWithFaultGenerator = new FaultStrategyOptions
};

// To get notifications when a fault is injected
var optionsOnFaultInjected = new FaultStrategyOptions
var optionsOnFaultInjected = new ChaosFaultStrategyOptions
{
FaultGenerator = new FaultGenerator().AddException<InvalidOperationException>(),
Enabled = true,
Expand All @@ -60,7 +60,7 @@ var optionsOnFaultInjected = new FaultStrategyOptions
}
};

// Add a fault strategy with a FaultStrategyOptions instance to the pipeline
// Add a fault strategy with a ChaosFaultStrategyOptions instance to the pipeline
new ResiliencePipelineBuilder().AddChaosFault(optionsBasic);
new ResiliencePipelineBuilder<HttpResponseMessage>().AddChaosFault(optionsWithFaultGenerator);

Expand All @@ -82,7 +82,7 @@ var pipeline = new ResiliencePipelineBuilder()
MaxRetryAttempts = 4,
Delay = TimeSpan.FromSeconds(3),
})
.AddChaosFault(new FaultStrategyOptions // Chaos strategies are usually placed as the last ones in the pipeline
.AddChaosFault(new ChaosFaultStrategyOptions // Chaos strategies are usually placed as the last ones in the pipeline
{
FaultGenerator = static args => new ValueTask<Exception?>(new InvalidOperationException("Dummy exception")),
Enabled = true,
Expand Down Expand Up @@ -152,7 +152,7 @@ The `FaultGenerator` is convenience API that allows you to specify what faults (
<!-- snippet: chaos-fault-generator-class -->
```cs
new ResiliencePipelineBuilder()
.AddChaosFault(new FaultStrategyOptions
.AddChaosFault(new ChaosFaultStrategyOptions
{
// Use FaultGenerator to register exceptions to be injected
FaultGenerator = new FaultGenerator()
Expand All @@ -171,7 +171,7 @@ Delegates give you the most flexibility at the expense of slightly more complica
<!-- snippet: chaos-fault-generator-delegate -->
```cs
new ResiliencePipelineBuilder()
.AddChaosFault(new FaultStrategyOptions
.AddChaosFault(new ChaosFaultStrategyOptions
{
// The same behavior can be achieved with delegates
FaultGenerator = args =>
Expand Down
6 changes: 3 additions & 3 deletions docs/chaos/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ Using Polly helps introduce resilience to a project, but we don't want to have t
* A way to revert easily, to control the blast radius.
* To be production grade, to run this in a production or near-production system with automation.

## Chaos strategies (a.k.a Monkey strategies)
## Chaos strategies

Chaos strategies (or Monkey strategies as we call them) are in essence a [Resilience strategy](../strategies/index.md#built-in-strategies), which means, as a *Resilience Strategy* is the minimum unit of resilience for Polly, a *Chaos Strategy* is the minimum unit of chaos for Simmy.
Chaos strategies (formerly known as Monkey strategies) are in essence a [Resilience strategy](../strategies/index.md#built-in-strategies), which means, as a *Resilience Strategy* is the minimum unit of resilience for Polly, a *Chaos Strategy* is the minimum unit of chaos for Simmy.

### Built-in strategies

Expand All @@ -44,7 +44,7 @@ It is usual to place the chaos strategy as the last strategy in the resilience p

## Common options across strategies

All the strategies' options implement the [`MonkeyStrategyOptions`](xref:Polly.Simmy.MonkeyStrategyOptions) class as it contains the basic configuration for every chaos strategy.
All the strategies' options implement the [`ChaosStrategyOptions`](xref:Polly.Simmy.ChaosStrategyOptions) class as it contains the basic configuration for every chaos strategy.

| Property | Default Value | Description |
|--------------------------|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
Expand Down
14 changes: 7 additions & 7 deletions docs/chaos/latency.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## About

- **Options**: [`LatencyStrategyOptions`](xref:Polly.Simmy.Latency.LatencyStrategyOptions)
- **Options**: [`ChaosLatencyStrategyOptions`](xref:Polly.Simmy.Latency.ChaosLatencyStrategyOptions)
- **Extensions**: `AddChaosLatency`
- **Strategy Type**: Proactive

Expand All @@ -19,18 +19,18 @@ The latency chaos strategy is designed to introduce controlled delays into syste
```cs
// Latency using the default options.
// See https://www.pollydocs.org/chaos/latency#defaults for defaults.
var optionsDefault = new LatencyStrategyOptions();
var optionsDefault = new ChaosLatencyStrategyOptions();

// 10% of invocations will be randomly affected
var basicOptions = new LatencyStrategyOptions
var basicOptions = new ChaosLatencyStrategyOptions
{
Latency = TimeSpan.FromSeconds(30),
Enabled = true,
InjectionRate = 0.1
};

// To use a custom function to generate the latency to inject
var optionsWithLatencyGenerator = new LatencyStrategyOptions
var optionsWithLatencyGenerator = new ChaosLatencyStrategyOptions
{
LatencyGenerator = static args =>
{
Expand All @@ -50,7 +50,7 @@ var optionsWithLatencyGenerator = new LatencyStrategyOptions
};

// To get notifications when a delay is injected
var optionsOnLatencyInjected = new LatencyStrategyOptions
var optionsOnLatencyInjected = new ChaosLatencyStrategyOptions
{
Latency = TimeSpan.FromSeconds(30),
Enabled = true,
Expand All @@ -62,7 +62,7 @@ var optionsOnLatencyInjected = new LatencyStrategyOptions
}
};

// Add a latency strategy with a LatencyStrategyOptions instance to the pipeline
// Add a latency strategy with a ChaosLatencyStrategyOptions instance to the pipeline
new ResiliencePipelineBuilder().AddChaosLatency(optionsDefault);
new ResiliencePipelineBuilder<HttpStatusCode>().AddChaosLatency(optionsWithLatencyGenerator);

Expand All @@ -85,7 +85,7 @@ var pipeline = new ResiliencePipelineBuilder()
Delay = TimeSpan.FromSeconds(3),
})
.AddTimeout(TimeSpan.FromSeconds(5))
.AddChaosLatency(new LatencyStrategyOptions // Chaos strategies are usually placed as the last ones in the pipeline
.AddChaosLatency(new ChaosLatencyStrategyOptions // Chaos strategies are usually placed as the last ones in the pipeline
{
Latency = TimeSpan.FromSeconds(10),
Enabled = true,
Expand Down
28 changes: 14 additions & 14 deletions docs/chaos/result.md → docs/chaos/outcome.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
## About

- **Options**:
- [`OutcomeStrategyOptions`](xref:Polly.Simmy.Outcomes.OutcomeStrategyOptions)
- [`OutcomeStrategyOptions<T>`](xref:Polly.Simmy.Outcomes.OutcomeStrategyOptions`1)
- **Extensions**: `AddChaosResult`
- [`ChaosOutcomeStrategyOptions`](xref:Polly.Simmy.Outcomes.ChaosOutcomeStrategyOptions)
- [`ChaosOutcomeStrategyOptions<T>`](xref:Polly.Simmy.Outcomes.ChaosOutcomeStrategyOptions`1)
- **Extensions**: `AddChaosOutcome`
- **Strategy Type**: Reactive

---
Expand All @@ -17,10 +17,10 @@ The outcome chaos strategy is designed to inject or substitute fake results into

## Usage

<!-- snippet: chaos-result-usage -->
<!-- snippet: chaos-outcome-usage -->
```cs
// To use OutcomeGenerator<T> to register the results and exceptions to be injected (equal probability)
var optionsWithResultGenerator = new OutcomeStrategyOptions<HttpResponseMessage>
var optionsWithResultGenerator = new ChaosOutcomeStrategyOptions<HttpResponseMessage>
{
OutcomeGenerator = new OutcomeGenerator<HttpResponseMessage>()
.AddResult(() => new HttpResponseMessage(HttpStatusCode.TooManyRequests))
Expand All @@ -31,7 +31,7 @@ var optionsWithResultGenerator = new OutcomeStrategyOptions<HttpResponseMessage>
};

// To get notifications when a result is injected
var optionsOnBehaviorInjected = new OutcomeStrategyOptions<HttpResponseMessage>
var optionsOnBehaviorInjected = new ChaosOutcomeStrategyOptions<HttpResponseMessage>
{
OutcomeGenerator = new OutcomeGenerator<HttpResponseMessage>()
.AddResult(() => new HttpResponseMessage(HttpStatusCode.InternalServerError)),
Expand All @@ -44,18 +44,18 @@ var optionsOnBehaviorInjected = new OutcomeStrategyOptions<HttpResponseMessage>
}
};

// Add a result strategy with a OutcomeStrategyOptions{<TResult>} instance to the pipeline
new ResiliencePipelineBuilder<HttpResponseMessage>().AddChaosResult(optionsWithResultGenerator);
new ResiliencePipelineBuilder<HttpResponseMessage>().AddChaosResult(optionsOnBehaviorInjected);
// Add a result strategy with a ChaosOutcomeStrategyOptions{<TResult>} instance to the pipeline
new ResiliencePipelineBuilder<HttpResponseMessage>().AddChaosOutcome(optionsWithResultGenerator);
new ResiliencePipelineBuilder<HttpResponseMessage>().AddChaosOutcome(optionsOnBehaviorInjected);

// There are also a couple of handy overloads to inject the chaos easily
new ResiliencePipelineBuilder<HttpResponseMessage>().AddChaosResult(0.1, () => new HttpResponseMessage(HttpStatusCode.TooManyRequests));
new ResiliencePipelineBuilder<HttpResponseMessage>().AddChaosOutcome(0.1, () => new HttpResponseMessage(HttpStatusCode.TooManyRequests));
```
<!-- endSnippet -->

Example execution:

<!-- snippet: chaos-result-execution -->
<!-- snippet: chaos-outcome-execution -->
```cs
var pipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
.AddRetry(new RetryStrategyOptions<HttpResponseMessage>
Expand All @@ -70,7 +70,7 @@ var pipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
MaxRetryAttempts = 4,
Delay = TimeSpan.FromSeconds(3),
})
.AddChaosResult(new OutcomeStrategyOptions<HttpResponseMessage> // Chaos strategies are usually placed as the last ones in the pipeline
.AddChaosOutcome(new ChaosOutcomeStrategyOptions<HttpResponseMessage> // Chaos strategies are usually placed as the last ones in the pipeline
{
OutcomeGenerator = static args =>
{
Expand Down Expand Up @@ -144,7 +144,7 @@ The `OutcomeGenerator<T>` is a convenience API that allows you to specify what o
<!-- snippet: chaos-outcome-generator-class -->
```cs
new ResiliencePipelineBuilder<HttpResponseMessage>()
.AddChaosResult(new OutcomeStrategyOptions<HttpResponseMessage>
.AddChaosOutcome(new ChaosOutcomeStrategyOptions<HttpResponseMessage>
{
// Use OutcomeGenerator<T> to register the results and exceptions to be injected
OutcomeGenerator = new OutcomeGenerator<HttpResponseMessage>()
Expand All @@ -163,7 +163,7 @@ Delegates give you the most flexibility at the expense of slightly more complica
<!-- snippet: chaos-outcome-generator-delegate -->
```cs
new ResiliencePipelineBuilder<HttpResponseMessage>()
.AddChaosResult(new OutcomeStrategyOptions<HttpResponseMessage>
.AddChaosOutcome(new ChaosOutcomeStrategyOptions<HttpResponseMessage>
{
// The same behavior can be achieved with delegates
OutcomeGenerator = args =>
Expand Down
Loading

0 comments on commit 54becab

Please sign in to comment.