private void listenLeaderChangeEvent() {
        sharedConfigService.subscribe(Leader.class, FullDocument.UPDATE_LOOKUP)
                .doOnNext(event -> {
                    Leader changedLeader = event.getFullDocument();
                    String clusterId = changedLeader != null
                            ? changedLeader.getClusterId()
                            : ChangeStreamUtil.getIdAsString(event.getDocumentKey());
                    if (!clusterId.equals(localNodeStatusManager.getLocalMember()
                            .getClusterId())) {
                        return;
                    }
                    switch (event.getOperationType()) {
                        case INSERT, REPLACE, UPDATE -> {
                            if (null == leader) {
                                LOGGER.info("The leader has changed to: {}", changedLeader);
                            } else if (!Objects.equals(leader.getNodeId(),
                                    changedLeader.getNodeId())
                                    || !Objects.equals(leader.getGeneration(),
                                            changedLeader.getGeneration())) {
                                LOGGER.info("The leader has changed from ({}) to: {}",
                                        leader,
                                        changedLeader);
                            }
                            leader = changedLeader;
                        }
                        case DELETE -> {
                            leader = null;
                            int delay = (int) (5 * ThreadLocalRandom.current()
                                    .nextFloat());
                            LOGGER.info(
                                    "The leader has been deleted. Tyring to be the first leader after {} seconds",
                                    delay);
                            Mono.delay(Duration.ofSeconds(delay))
                                    .subscribe(null, null, () -> {
                                        if (leader == null) {
                                            localNodeStatusManager.tryBecomeFirstLeader()
                                                    .subscribe(isLeader -> {
                                                        if (isLeader) {
                                                            LOGGER.info(
                                                                    "The local node has become the first leader");
                                                        } else {
                                                            LOGGER.info(
                                                                    "Another node has become the first leader");
                                                        }
                                                    },
                                                            t -> LOGGER.error(
                                                                    "Caught an error while trying to become the first leader",
                                                                    t));
                                        } else {
                                            LOGGER.info("Another node has become the first leader");
                                        }
                                    });
                        }
                        case INVALIDATE -> leader = null;
                        default -> LOGGER.fatal("Detected an illegal operation on the collection \""
                                + StringUtil
                                        .upperCamelToLowerCamelLatin1(Leader.class.getSimpleName())
                                + "\" in the change stream event: {}", event);
                    }
                })
                .onErrorContinue((throwable, o) -> LOGGER.error(
                        "Caught an error while processing the change stream event of Leader: {}",
                        o,
                        throwable))
                .subscribe();
    }
@return a positive or negative number, and never 0 to ensure that the order is consistent in
        every node.
turms-server-common/src/main/java/im/turms/server/common/infra/cluster/service/discovery/DiscoveryService.java