Skip to content

Commit

Permalink
Fix deterministic trouble for custom watched services
Browse files Browse the repository at this point in the history
We now only counts attacks on custom watched services instead of
counting all attacks.

Closes #451
  • Loading branch information
denniseffing committed Jan 17, 2025
1 parent 78b8c8e commit 9061073
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,11 @@ public ChaosMonkeyRequestScope(ChaosMonkeySettings chaosMonkeySettings, List<Cha
}

public void callChaosMonkey(ChaosTarget type, String simpleName) {
if (isEnabled(type, simpleName) && isTrouble()) {

if (isEnabled(type, simpleName) && isTrouble(simpleName)) {
if (metricEventPublisher != null) {
metricEventPublisher.publishMetricEvent(MetricType.APPLICATION_REQ_COUNT, "type", "total");
}

// Custom watched services can be defined at runtime, if there are any, only
// these will be attacked!
AssaultProperties assaultProps = chaosMonkeySettings.getAssaultProperties();
if (assaultProps.isWatchedCustomServicesActive()) {
if (assaultProps.getWatchedCustomServices().stream().anyMatch(simpleName::startsWith)) {
// only all listed custom methods will be attacked
chooseAndRunAttack();
}
} else {
// default attack if no custom watched service is defined
chooseAndRunAttack();
}
chooseAndRunAttack();
}
}

Expand All @@ -105,7 +92,14 @@ private ChaosMonkeyAssault getRandomFrom(List<ChaosMonkeyAssault> activeAssaults
return activeAssaults.get(exceptionRand);
}

private boolean isTrouble() {
private boolean isTrouble(String simpleName) {
// Custom watched services can be defined at runtime, if there are any, only
// these will be attacked!
AssaultProperties assaultProperties = chaosMonkeySettings.getAssaultProperties();
if (assaultProperties.isWatchedCustomServicesActive() && assaultProperties.getWatchedCustomServices().stream().noneMatch(simpleName::startsWith)) {
return false;
}

if (chaosMonkeySettings.getAssaultProperties().isDeterministic()) {
return assaultCounter.incrementAndGet() % chaosMonkeySettings.getAssaultProperties().getLevel() == 0;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,23 @@ void callChaosMonkey_givenDeterministicTrouble_shouldRunAttackAndPublishRequestC
verify(metricEventPublisherMock).publishMetricEvent(MetricType.APPLICATION_REQ_COUNT, "type", "assaulted");
}

@Test
void callChaosMonkey_givenDeterministicTroubleAndConfiguredWatchedCustomServices_shouldNotRunAttackIfAttackCountIsLowerThanLevel() {
givenChaosMonkeyIsEnabled();
givenOneActiveAttack();
given(assaultProperties.isDeterministic()).willReturn(true);
given(assaultProperties.getLevel()).willReturn(3);
given(assaultProperties.isWatchedCustomServicesActive()).willReturn(true);
given(assaultProperties.getWatchedCustomServices()).willReturn(List.of("de.test.CustomService.someMethod"));

// Important: We call chaos monkey three times, but the second attack is not on the watched custom service!
chaosMonkeyRequestScope.callChaosMonkey(null, "de.test.CustomService.someMethod");
chaosMonkeyRequestScope.callChaosMonkey(null, "de.test.CustomService.someOtherMethod");
chaosMonkeyRequestScope.callChaosMonkey(null, "de.test.CustomService.someMethod");

verify(assaults.get(0), never()).attack();
}

@Test
void callChaosMonkey_givenConfiguredWatchedCustomServices_shouldRunAttackIfTargetPackageNameMatches() {
givenChaosMonkeyIsEnabled();
Expand Down

0 comments on commit 9061073

Please sign in to comment.