public void start() throws Exception {
    Map<TTYPE, List<LeadershipStateListener>> listenerMap = this.listeners;
    if (conf.getBoolean(LEADER_IN_TEST, false)) {
      Map<TTYPE, List<LeadershipStateListener>> newListeners = new HashMap<>();
      newListeners.put(TTYPE.HOUSEKEEPING, new ArrayList<>());
      listenerMap.forEach((k, v) -> newListeners.get(TTYPE.HOUSEKEEPING).addAll(v));
      listenerMap = newListeners;
    }
    for (Map.Entry<TTYPE, List<LeadershipStateListener>> entry :
        listenerMap.entrySet()) {
      List<LeadershipStateListener> listenerList = entry.getValue();
      if (listenerList.isEmpty()) {
        continue;
      }
      if (auditLeaderListener != null) {
        listenerList.add(0, auditLeaderListener);
      }
      TTYPE ttype = entry.getKey();
      LeaderElection leaderElection = LeaderElectionFactory.create(conf);
      leaderElection.setName(ttype.name);
      listenerList.forEach(listener -> leaderElection.addStateListener(listener));
      leaderElections.add(leaderElection);
      
      Thread daemon = new Thread(() -> {
        try {
          Object mutex = getLeaderMutex(conf, ttype, servHost);
          leaderElection.tryBeLeader(conf, mutex);
        } catch (LeaderException e) {
          throw new RuntimeException("Error claiming to be leader: " + leaderElection.getName(), e);
        }
      });
      daemon.setName("Metastore Election " + leaderElection.getName());
      daemon.setDaemon(true);

      if (startAsDaemon) {
        daemon.start();
      } else {
        daemon.run();
      }
    }
  }
Types of tasks that exactly have one instance in a given warehouse.
For those tasks which belong to the same type, they will be running in the same leader.
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/leader/LeaderElectionContext.java