Package org.apache.curator.framework.recipes.leader

Examples of org.apache.curator.framework.recipes.leader.LeaderLatch


        // Beware race conditions: closeLeaderLatch() may be called by another thread at any time.
        while (isRunning()) {
            try {
                // Start attempting to acquire leadership via the Curator leadership latch.
                LOG.debug("Attempting to acquire leadership: {}", getId());
                LeaderLatch latch = startLeaderLatch();

                // Wait until (a) leadership is acquired or (b) the latch is closed by service shutdown or ZK cxn loss.
                if (isRunning()) {
                    try {
                        latch.await();
                    } catch (EOFException e) {
                        // Latch was closed while we were waiting.
                        checkState(!latch.hasLeadership());
                    }
                }

                // If we succeeded in acquiring leadership, start/run the leadership-managed delegate service.
                if (isRunning() && latch.hasLeadership()) {
                    LOG.debug("Leadership acquired: {}", getId());
                    runAsLeader();
                    LOG.debug("Leadership released: {}", getId());
                }
            } finally {
View Full Code Here


            _delegate = null;
        }
    }

    private LeaderLatch newLeaderLatch() {
        return new LeaderLatch(_curator, _leaderPath, _instanceId);
    }
View Full Code Here

        // Create a non-started latch that we can use to implement getLeader(), getParticipants() & friends.
        _latch = newLeaderLatch();
    }

    private synchronized LeaderLatch startLeaderLatch() throws InterruptedException {
        LeaderLatch latch = _latch; // Read the volatile once
        // Assert not started already.  initLeaderLatch() and closeLeaderLatch() leave the latch in the latent state.
        checkState(latch.getState() == LeaderLatch.State.LATENT);
        try {
            latch.start();
        } catch (InterruptedException ie) {
            throw ie;
        } catch (Throwable t) {
            LOG.error("Exception attempting to acquire leadership: {}", getId(), t);
        }
View Full Code Here

        }
        return latch;
    }

    private synchronized void closeLeaderLatch() {
        LeaderLatch latch = _latch; // Read the volatile once
        if (latch.getState() == LeaderLatch.State.STARTED) {
            try {
                latch.close();
            } catch (IOException e) {
                LOG.debug("Unexpected exception closing LeaderLatch.", e);
            }
        }
        // Return the latch to a latent state (newly created) for use by getLeader(), getParticipants() & friends.
View Full Code Here

    {
        this.client = client;
        this.queueAllocator = queueAllocator;
        this.queuePath = queuePath;
        this.policies = policies;
        leaderLatch = new LeaderLatch(client, leaderPath);
        service = Executors.newSingleThreadExecutor(policies.getThreadFactory());
    }
View Full Code Here

  }

  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;
View Full Code Here

    }
  }

  private LeaderLatch createNewLeaderLatch()
  {
    final LeaderLatch newLeaderLatch = new LeaderLatch(
        curator, ZKPaths.makePath(zkPaths.getCoordinatorPath(), COORDINATOR_OWNER_NODE), self.getHost()
    );

    newLeaderLatch.addListener(
        new LeaderLatchListener()
        {
          @Override
          public void isLeader()
          {
View Full Code Here

        }
      }
      catch (Exception e) {
        log.makeAlert(e, "Unable to become leader")
           .emit();
        final LeaderLatch oldLatch = createNewLeaderLatch();
        CloseQuietly.close(oldLatch);
        try {
          leaderLatch.get().start();
        }
        catch (Exception e1) {
View Full Code Here

    @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;
          }
        }
View Full Code Here

        "me",
        "localhost",
        8080
    );

    AtomicReference<LeaderLatch> leaderLatch = new AtomicReference<>(new LeaderLatch(localCf, "test"));

    ZkPathsConfig zkPathsConfig = new ZkPathsConfig()
    {
      @Override
      public String getZkBasePath()
View Full Code Here

TOP

Related Classes of org.apache.curator.framework.recipes.leader.LeaderLatch

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.