mantis-control-plane/mantis-control-plane-server/src/main/java/io/mantisrx/master/zk/LeaderElector.java [101:136]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    private LeaderLatch createNewLeaderLatch(String leaderPath) {
        final LeaderLatch newLeaderLatch = new LeaderLatch(curator, leaderPath, "127.0.0.1");

        newLeaderLatch.addListener(
            new LeaderLatchListener() {
                @Override
                public void isLeader() {
                    announceLeader();
                }

                @Override
                public void notLeader() {
                    leadershipManager.stopBeingLeader();
                }
            }, Executors.newSingleThreadExecutor(new DefaultThreadFactory("MasterLeader-%s")));

        return newLeaderLatch;
    }

    private void announceLeader() {
        try {
            logger.info("Announcing leader");
            byte[] masterDescription = jsonMapper.writeValueAsBytes(leadershipManager.getDescription());

            // There is no need to lock anything because we ensure only leader will write to the leader path
            curator
                .setData()
                .inBackground((client, event) -> {
                    if (event.getResultCode() == OK.intValue()) {
                        leadershipManager.becomeLeader();
                    } else {
                        logger.warn("Failed to elect leader from path {} with event {}", leaderPath, event);
                    }
                }).forPath(leaderPath, masterDescription);
        } catch (Exception e) {
            throw new RuntimeException("Failed to announce leader: "+e.getMessage(), e);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



mantis-control-plane/mantis-control-plane-server/src/main/java/io/mantisrx/master/zk/ZookeeperLeaderElector.java [103:138]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    private LeaderLatch createNewLeaderLatch(String leaderPath) {
        final LeaderLatch newLeaderLatch = new LeaderLatch(curator, leaderPath, "127.0.0.1");

        newLeaderLatch.addListener(
            new LeaderLatchListener() {
                @Override
                public void isLeader() {
                    announceLeader();
                }

                @Override
                public void notLeader() {
                    leadershipManager.stopBeingLeader();
                }
            }, Executors.newSingleThreadExecutor(new DefaultThreadFactory("MasterLeader-%s")));

        return newLeaderLatch;
    }

    private void announceLeader() {
        try {
            logger.info("Announcing leader");
            byte[] masterDescription = jsonMapper.writeValueAsBytes(leadershipManager.getDescription());

            // There is no need to lock anything because we ensure only leader will write to the leader path
            curator
                .setData()
                .inBackground((client, event) -> {
                    if (event.getResultCode() == OK.intValue()) {
                        leadershipManager.becomeLeader();
                    } else {
                        logger.warn("Failed to elect leader from path {} with event {}", leaderPath, event);
                    }
                }).forPath(leaderPath, masterDescription);
        } catch (Exception e) {
            throw new RuntimeException("Failed to announce leader: "+e.getMessage(), e);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



