示例#1
0
  public void start() throws Exception {
    m_leaderLatch =
        new LeaderLatch(
            m_client.get(),
            m_config.getMetaServerLeaderElectionZkPath(),
            m_config.getMetaServerName());

    m_leaderLatch.addListener(
        new LeaderLatchListener() {

          @Override
          public void notLeader() {
            log.info("Become follower");
            m_hasLeadership.set(false);
            m_leader.set(fetcheLeaderInfoFromZk());
            m_listenerContainer.notLeader(ClusterStateHolder.this);
          }

          @Override
          public void isLeader() {
            log.info("Become leader");
            m_hasLeadership.set(true);
            m_leader.set(fetcheLeaderInfoFromZk());
            m_listenerContainer.isLeader(ClusterStateHolder.this);
          }
        },
        Executors.newSingleThreadExecutor(
            HermesThreadFactory.create("LeaderLatchListenerPool", true)));

    // call notLeader before start, since if this is not leader, it won't trigger notLeader on start
    m_listenerContainer.notLeader(this);

    m_leaderLatch.start();
  }
示例#2
0
  public static void main(String[] args) throws Exception {
    List<LeaderLatch> leaders = new ArrayList<LeaderLatch>();
    List<CuratorFramework> clients = new ArrayList<CuratorFramework>();

    TestingServer server = new TestingServer();

    try {
      for (int i = 0; i < 10; i++) {
        CuratorFramework client =
            CuratorFrameworkFactory.newClient(
                "192.168.50.202:2181,192.168.50.203:2181,192.168.50.204:2181",
                new ExponentialBackoffRetry(20000, 3));
        clients.add(client);

        LeaderLatch leader = new LeaderLatch(client, "/francis/leader");
        leader.addListener(
            new LeaderLatchListener() {

              @Override
              public void isLeader() {
                // TODO Auto-generated method stub
                System.out.println("I am Leader");
              }

              @Override
              public void notLeader() {
                // TODO Auto-generated method stub
                System.out.println("I am not Leader");
              }
            });

        leaders.add(leader);

        client.start();
        leader.start();
      }

      Thread.sleep(Integer.MAX_VALUE);
    } finally {

      for (CuratorFramework client : clients) {
        CloseableUtils.closeQuietly(client);
      }

      for (LeaderLatch leader : leaders) {
        CloseableUtils.closeQuietly(leader);
      }

      //            CloseableUtils.closeQuietly(server);
    }

    Thread.sleep(Integer.MAX_VALUE);
  }
 public void registerLeaderLatchListener(LeaderLatchListener... leaderListener) {
   try {
     leaderLatch.start();
     Arrays.stream(leaderListener).forEach(leaderLatch::addListener);
   } catch (Exception e) {
     throw new InternalProcessingException(e);
   }
 }
示例#4
0
  public String getCurrentLeader() {
    try {
      final LeaderLatch latch = leaderLatch.get();

      if (latch == null) {
        return null;
      }

      Participant participant = latch.getLeader();
      if (participant.isLeader()) {
        return participant.getId();
      }

      return null;
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }
示例#5
0
  private HostPort fetcheLeaderInfoFromZk() {
    try {
      Participant leader = m_leaderLatch.getLeader();
      return JSON.parseObject(leader.getId(), HostPort.class);
    } catch (Exception e) {
      log.error("Failed to fetch leader info from zk.", e);
    }

    return null;
  }
示例#6
0
    @Override
    public void run() {
      try {
        synchronized (lock) {
          final LeaderLatch latch = leaderLatch.get();
          if (latch == null || !latch.hasLeadership()) {
            log.info(
                "LEGGO MY EGGO. [%s] is leader.", latch == null ? null : latch.getLeader().getId());
            stopBeingLeader();
            return;
          }
        }

        List<Boolean> allStarted =
            Arrays.asList(databaseSegmentManager.isStarted(), serverInventoryView.isStarted());
        for (Boolean aBoolean : allStarted) {
          if (!aBoolean) {
            log.error("InventoryManagers not started[%s]", allStarted);
            stopBeingLeader();
            return;
          }
        }

        // Do coordinator stuff.
        DruidCoordinatorRuntimeParams params =
            DruidCoordinatorRuntimeParams.newBuilder()
                .withStartTime(startTime)
                .withDatasources(databaseSegmentManager.getInventory())
                .withDynamicConfigs(dynamicConfigs.get())
                .withEmitter(emitter)
                .build();

        for (DruidCoordinatorHelper helper : helpers) {
          params = helper.run(params);
        }
      } catch (Exception e) {
        log.makeAlert(e, "Caught exception, ignoring so that schedule keeps going.").emit();
      }
    }
示例#7
0
  private LeaderLatch createNewLeaderLatch() {
    final LeaderLatch newLeaderLatch =
        new LeaderLatch(
            curator,
            ZKPaths.makePath(zkPaths.getCoordinatorPath(), COORDINATOR_OWNER_NODE),
            config.getHost());

    newLeaderLatch.addListener(
        new LeaderLatchListener() {
          @Override
          public void isLeader() {
            DruidCoordinator.this.becomeLeader();
          }

          @Override
          public void notLeader() {
            DruidCoordinator.this.stopBeingLeader();
          }
        },
        Execs.singleThreaded("CoordinatorLeader-%s"));

    return leaderLatch.getAndSet(newLeaderLatch);
  }
 public boolean isLeader() {
   return curatorClient.getZookeeperClient().isConnected() && leaderLatch.hasLeadership();
 }
示例#9
0
 public void close() throws Exception {
   m_leaderLatch.close();
 }