Skip to content
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

Refactor/Clean leader election related classes #5937

Merged
merged 5 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -20,9 +20,9 @@

public class LeaderCallbacks {

private Runnable onStartLeading;
private Runnable onStopLeading;
private Consumer<String> onNewLeader;
private final Runnable onStartLeading;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just add final, nothing more changes in this class

private final Runnable onStopLeading;
private final Consumer<String> onNewLeader;

public LeaderCallbacks(Runnable onStartLeading, Runnable onStopLeading, Consumer<String> onNewLeader) {
this.onStartLeading = Objects.requireNonNull(onStartLeading, "onStartLeading callback is required");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import java.net.HttpURLConnection;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
Expand All @@ -46,10 +45,9 @@ public class LeaderElector {

protected static final Double JITTER_FACTOR = 1.2;

private KubernetesClient kubernetesClient;
private LeaderElectionConfig leaderElectionConfig;
private final KubernetesClient kubernetesClient;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add final

private final LeaderElectionConfig leaderElectionConfig;
private final AtomicReference<LeaderElectionRecord> observedRecord = new AtomicReference<>();
private final AtomicReference<LocalDateTime> observedTime = new AtomicReference<>();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dropped the observedTime variable, it was updated, but never used

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why this was added on the first place. I need to do some digging before we can merge this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The go client switch how time was tracked so that it was local to each elector, but we never fully implemented that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I traced it back to the initial implementation but it was never used. I guess I left that there for a follow up.

private final Executor executor;
private boolean started;
private boolean stopped;
Expand Down Expand Up @@ -254,7 +252,6 @@ synchronized boolean tryAcquireOrRenew() {
private void updateObserved(LeaderElectionRecord leaderElectionRecord) {
final LeaderElectionRecord current = observedRecord.getAndSet(leaderElectionRecord);
if (!Objects.equals(leaderElectionRecord, current)) {
observedTime.set(LocalDateTime.now());
final String currentLeader = current == null ? null : current.getHolderIdentity();
final String newLeader = leaderElectionRecord.getHolderIdentity();
if (!Objects.equals(newLeader, currentLeader)) {
Expand Down
Loading