
pl.allegro.tech.hermes.management.infrastructure.leader.ManagementLeadership Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hermes-management Show documentation
Show all versions of hermes-management Show documentation
Fast and reliable message broker built on top of Kafka.
package pl.allegro.tech.hermes.management.infrastructure.leader;
import jakarta.annotation.PostConstruct;
import java.util.Optional;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.LeaderLatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.allegro.tech.hermes.infrastructure.zookeeper.ZookeeperPaths;
import pl.allegro.tech.hermes.management.infrastructure.zookeeper.ZookeeperClient;
import pl.allegro.tech.hermes.management.infrastructure.zookeeper.ZookeeperClientManager;
public class ManagementLeadership {
private final String leaderElectionDc;
private final Optional leaderLatch;
private static final Logger logger = LoggerFactory.getLogger(ManagementLeadership.class);
public ManagementLeadership(
ZookeeperClientManager zookeeperClientManager,
String leaderElectionDc,
ZookeeperPaths zookeeperPaths) {
this.leaderElectionDc = leaderElectionDc;
Optional leaderCuratorFramework =
zookeeperClientManager.getClients().stream()
.filter(it -> it.getDatacenterName().equals(leaderElectionDc))
.findFirst()
.map(ZookeeperClient::getCuratorFramework);
String leaderPath = zookeeperPaths.managementLeaderPath();
this.leaderLatch = leaderCuratorFramework.map(it -> new LeaderLatch(it, leaderPath));
}
@PostConstruct
public void startListeningForLeadership() {
if (leaderLatch.isPresent()) {
logger.info("Starting listening for leadership");
try {
leaderLatch.get().start();
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
logLeaderZookeeperClientNotFound(leaderElectionDc);
}
}
public boolean isLeader() {
return leaderLatch.map(LeaderLatch::hasLeadership).orElse(false);
}
private void logLeaderZookeeperClientNotFound(String dc) {
logger.error(
"Cannot start listening for leadership - no zookeeper client for datacenter={}", dc);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy