Skip to content

Commit

Permalink
Fabric leader clean up 5 (#1646)
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 authored Apr 29, 2024
1 parent 1c819d0 commit 279ee4d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors.
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,11 +35,11 @@ public Leader(String role, String id) {
}

public String getRole() {
return this.role;
return role;
}

public String getId() {
return this.id;
return id;
}

public boolean isCandidate(Candidate candidate) {
Expand All @@ -62,17 +62,17 @@ public boolean equals(Object o) {

Leader leader = (Leader) o;

return Objects.equals(this.role, leader.role) && Objects.equals(this.id, leader.id);
return Objects.equals(role, leader.role) && Objects.equals(id, leader.id);
}

@Override
public int hashCode() {
return Objects.hash(this.role, this.id);
return Objects.hash(role, id);
}

@Override
public String toString() {
return String.format("Leader{role='%s', id='%s'}", this.role, this.id);
return String.format("Leader{role='%s', id='%s'}", role, id);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors.
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,12 +35,17 @@ public LeaderContext(Candidate candidate, LeadershipController leadershipControl

@Override
public boolean isLeader() {
return this.leadershipController.getLocalLeader().filter(l -> l.isCandidate(this.candidate)).isPresent();
return leadershipController.getLocalLeader().filter(l -> l.isCandidate(candidate)).isPresent();
}

@Override
public void yield() {
this.leadershipController.revoke();
leadershipController.revoke();
}

@Override
public String getRole() {
return candidate.getRole();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors.
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,17 +20,15 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.context.SmartLifecycle;
import org.springframework.core.log.LogAccessor;

/**
* @author Gytis Trikleris
*/
public class LeaderInitiator implements SmartLifecycle {

private static final Logger LOGGER = LoggerFactory.getLogger(LeaderInitiator.class);
private static final LogAccessor LOGGER = new LogAccessor(LeaderInitiator.class);

private final LeaderProperties leaderProperties;

Expand All @@ -54,33 +52,33 @@ public LeaderInitiator(LeaderProperties leaderProperties, LeadershipController l

@Override
public boolean isAutoStartup() {
return this.leaderProperties.isAutoStartup();
return leaderProperties.isAutoStartup();
}

@Override
public void start() {
if (!isRunning()) {
LOGGER.debug("Leader initiator starting");
this.leaderRecordWatcher.start();
this.hostPodWatcher.start();
this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
this.scheduledExecutorService.scheduleAtFixedRate(this.leadershipController::update,
this.leaderProperties.getUpdatePeriod().toMillis(),
this.leaderProperties.getUpdatePeriod().toMillis(), TimeUnit.MILLISECONDS);
this.isRunning = true;
LOGGER.debug(() -> "Leader initiator starting");
leaderRecordWatcher.start();
hostPodWatcher.start();
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleAtFixedRate(leadershipController::update,
leaderProperties.getUpdatePeriod().toMillis(),
leaderProperties.getUpdatePeriod().toMillis(), TimeUnit.MILLISECONDS);
isRunning = true;
}
}

@Override
public void stop() {
if (isRunning()) {
LOGGER.debug("Leader initiator stopping");
this.scheduledExecutorService.shutdown();
this.scheduledExecutorService = null;
this.hostPodWatcher.stop();
this.leaderRecordWatcher.stop();
this.leadershipController.revoke();
this.isRunning = false;
LOGGER.debug(() -> "Leader initiator stopping");
scheduledExecutorService.shutdown();
scheduledExecutorService = null;
hostPodWatcher.stop();
leaderRecordWatcher.stop();
leadershipController.revoke();
isRunning = false;
}
}

Expand All @@ -92,7 +90,7 @@ public void stop(Runnable callback) {

@Override
public boolean isRunning() {
return this.isRunning;
return isRunning;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public static void guarded(ReentrantLock lock, Runnable runnable) {
finally {
lock.unlock();
}

}

}

0 comments on commit 279ee4d

Please sign in to comment.