Skip to content

feat: config option to not exit when stops leading #2500

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 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,15 @@ private void startLeading() {
}

private void stopLeading() {
log.info("Stopped leading for identity: {}. Exiting.", identity);
// When leader stops leading the process ends immediately to prevent multiple reconciliations
// running parallel.
// Note that some reconciliations might run for a very long time.
System.exit(1);
if (configurationService.getLeaderElectionConfiguration().orElseThrow().isExitOnStopLeading()) {
log.info("Stopped leading for identity: {}. Exiting.", identity);
// When leader stops leading the process ends immediately to prevent multiple reconciliations
// running parallel.
// Note that some reconciliations might run for a very long time.
System.exit(1);
} else {
log.info("Stopped leading, configured not to exit");
}
}

private String identity(LeaderElectionConfiguration config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ public class LeaderElectionConfiguration {
private final Duration retryPeriod;

private final LeaderCallbacks leaderCallbacks;
private final boolean exitOnStopLeading;

public LeaderElectionConfiguration(String leaseName, String leaseNamespace, String identity) {
this(
leaseName,
leaseNamespace,
LEASE_DURATION_DEFAULT_VALUE,
RENEW_DEADLINE_DEFAULT_VALUE,
RETRY_PERIOD_DEFAULT_VALUE, identity, null);
RETRY_PERIOD_DEFAULT_VALUE, identity, null, true);
}

public LeaderElectionConfiguration(String leaseName, String leaseNamespace) {
Expand All @@ -36,7 +37,7 @@ public LeaderElectionConfiguration(String leaseName, String leaseNamespace) {
leaseNamespace,
LEASE_DURATION_DEFAULT_VALUE,
RENEW_DEADLINE_DEFAULT_VALUE,
RETRY_PERIOD_DEFAULT_VALUE, null, null);
RETRY_PERIOD_DEFAULT_VALUE, null, null, true);
}

public LeaderElectionConfiguration(String leaseName) {
Expand All @@ -45,7 +46,7 @@ public LeaderElectionConfiguration(String leaseName) {
null,
LEASE_DURATION_DEFAULT_VALUE,
RENEW_DEADLINE_DEFAULT_VALUE,
RETRY_PERIOD_DEFAULT_VALUE, null, null);
RETRY_PERIOD_DEFAULT_VALUE, null, null, true);
}

public LeaderElectionConfiguration(
Expand All @@ -54,7 +55,7 @@ public LeaderElectionConfiguration(
Duration leaseDuration,
Duration renewDeadline,
Duration retryPeriod) {
this(leaseName, leaseNamespace, leaseDuration, renewDeadline, retryPeriod, null, null);
this(leaseName, leaseNamespace, leaseDuration, renewDeadline, retryPeriod, null, null, true);
}

public LeaderElectionConfiguration(
Expand All @@ -64,14 +65,16 @@ public LeaderElectionConfiguration(
Duration renewDeadline,
Duration retryPeriod,
String identity,
LeaderCallbacks leaderCallbacks) {
LeaderCallbacks leaderCallbacks,
boolean exitOnStopLeading) {
this.leaseName = leaseName;
this.leaseNamespace = leaseNamespace;
this.leaseDuration = leaseDuration;
this.renewDeadline = renewDeadline;
this.retryPeriod = retryPeriod;
this.identity = identity;
this.leaderCallbacks = leaderCallbacks;
this.exitOnStopLeading = exitOnStopLeading;
}

public Optional<String> getLeaseNamespace() {
Expand Down Expand Up @@ -101,4 +104,8 @@ public Optional<String> getIdentity() {
public Optional<LeaderCallbacks> getLeaderCallbacks() {
return Optional.ofNullable(leaderCallbacks);
}

public boolean isExitOnStopLeading() {
return exitOnStopLeading;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public final class LeaderElectionConfigurationBuilder {
private Duration renewDeadline = RENEW_DEADLINE_DEFAULT_VALUE;
private Duration retryPeriod = RETRY_PERIOD_DEFAULT_VALUE;
private LeaderCallbacks leaderCallbacks;
private boolean exitOnStopLeading = true;

private LeaderElectionConfigurationBuilder(String leaseName) {
this.leaseName = leaseName;
Expand Down Expand Up @@ -55,8 +56,13 @@ public LeaderElectionConfigurationBuilder withLeaderCallbacks(LeaderCallbacks le
return this;
}

public LeaderElectionConfigurationBuilder withExitOnStopLeading(boolean exitOnStopLeading) {
this.exitOnStopLeading = exitOnStopLeading;
return this;
}

public LeaderElectionConfiguration build() {
return new LeaderElectionConfiguration(leaseName, leaseNamespace, leaseDuration, renewDeadline,
retryPeriod, identity, leaderCallbacks);
retryPeriod, identity, leaderCallbacks, exitOnStopLeading);
}
}
Loading