Enable/disable failure events publishing during leader election

This commit is contained in:
Gytis Trikleris
2018-06-19 11:15:02 +02:00
committed by Ioannis Canellos
parent 1cb39c98ab
commit 6b129cea9e
3 changed files with 30 additions and 2 deletions

View File

@@ -91,6 +91,12 @@ public class LeaderProperties {
*/
private double jitterFactor = DEFAULT_JITTER_FACTOR;
/**
* Enable/disable publishing events in case leadership acquisition fails.
* Default: false
*/
private boolean publishFailedEvents = false;
public boolean isAutoStartup() {
return autoStartup;
}
@@ -170,4 +176,12 @@ public class LeaderProperties {
public void setJitterFactor(double jitterFactor) {
this.jitterFactor = jitterFactor;
}
public boolean isPublishFailedEvents() {
return publishFailedEvents;
}
public void setPublishFailedEvents(boolean publishFailedEvents) {
this.publishFailedEvents = publishFailedEvents;
}
}

View File

@@ -192,8 +192,10 @@ public class LeadershipController {
}
private void handleOnFailed(Candidate candidate) {
Context context = new LeaderContext(candidate, this);
leaderEventPublisher.publishOnFailedToAcquire(this, context, candidate.getRole());
if (leaderProperties.isPublishFailedEvents()) {
Context context = new LeaderContext(candidate, this);
leaderEventPublisher.publishOnFailedToAcquire(this, context, candidate.getRole());
}
}
private Map<String, String> getLeaderData(Candidate candidate) {

View File

@@ -116,6 +116,7 @@ public class LeadershipControllerTest {
@Test
public void shouldFailToAcquireIfThereIsAnotherLeader() {
given(mockLeaderProperties.isPublishFailedEvents()).willReturn(true);
given(mockKubernetesHelper.getConfigMap()).willReturn(mockConfigMap);
given(mockKubernetesHelper.podExists(ID)).willReturn(true);
given(mockConfigMap.getData()).willReturn(leaderData);
@@ -131,6 +132,7 @@ public class LeadershipControllerTest {
@Test
public void shouldFailToAcquireBecauseOfException() {
given(mockLeaderProperties.isPublishFailedEvents()).willReturn(true);
doThrow(new KubernetesClientException("Test exception")).when(mockKubernetesHelper).createConfigMap(any());
boolean result = leadershipController.acquire(mockCandidate);
@@ -139,6 +141,16 @@ public class LeadershipControllerTest {
verifyPublishOnFailedToAcquire();
}
@Test
public void shouldFailToAcquireAndSuspendEvents() {
doThrow(new KubernetesClientException("Test exception")).when(mockKubernetesHelper).createConfigMap(any());
boolean result = leadershipController.acquire(mockCandidate);
assertThat(result).isFalse();
verify(mockLeaderEventPublisher, times(0)).publishOnFailedToAcquire(any(), any(), any());
}
@Test
public void shouldRevokeLeadership() {
given(mockKubernetesHelper.getConfigMap()).willReturn(mockConfigMap);