Java 类org.apache.curator.framework.recipes.leader.LeaderSelector 实例源码

项目:centraldogma    文件:ZooKeeperCommandExecutor.java   
private ZooKeeperCommandExecutor(String replicaId, CommandExecutor delegate, CuratorFramework curator,
                                 String zkPath, boolean createPathIfNotExist, File revisionFile,
                                 int numWorkers, int maxLogCount, long minLogAgeMillis) {
    super(replicaId);

    this.delegate = delegate;
    this.revisionFile = revisionFile;
    this.curator = curator;
    this.zkPath = zkPath;
    this.createPathIfNotExist = createPathIfNotExist;
    this.maxLogCount = maxLogCount;
    this.minLogAgeMillis = minLogAgeMillis;

    final ThreadPoolExecutor executor = new ThreadPoolExecutor(
            numWorkers, numWorkers,
            60, TimeUnit.SECONDS, new LinkedTransferQueue<>(),
            new DefaultThreadFactory("zookeeper-command-executor", true));
    executor.allowCoreThreadTimeOut(true);
    this.executor = executor;

    logWatcher = new PathChildrenCache(curator, absolutePath(LOG_PATH), true);
    logWatcher.getListenable().addListener(this, MoreExecutors.directExecutor());
    oldLogRemover = new OldLogRemover();
    leaderSelector = new LeaderSelector(curator, absolutePath(LEADER_PATH), oldLogRemover);
    leaderSelector.autoRequeue();
}
项目:snowflake    文件:SnowflakeServer.java   
public void start() throws Exception {
    client.start();

    leaderSelector = new LeaderSelector(client, SnowflakeConfig.getLeaderPath(), this);
    leaderSelector.autoRequeue();
    LOGGER.info("start SnowflakeServer... ip: {}", getHostname());

    leaderSelector.start();
    while (!hasLeader()) {
        Thread.sleep(1000);
    }

    initWorkerId();

    ReporterHolder.metrics.register(MetricRegistry.name("SnowflakeServer", "workerId"), new Gauge<Integer>() {
        @Override
        public Integer getValue() {
            return workerId;
        }
    });
}
项目:weixin-utils    文件:TokenCache.java   
private void initialize() throws Exception {
    if (!initialized) {
        logger.info("lock");
        synchronized (this) {
            logger.info("lock acquired");
            if (!initialized) {
                String zkServers = WeixinConfig.getZKServerList();
                logger.info("ZK servers: {}", zkServers);
                client = CuratorFrameworkFactory.newClient(zkServers, new ExponentialBackoffRetry(1000, 10));
                client.start();
                client.blockUntilConnected();
                logger.info("ZK connected");
                if (WeixinConfig.isLeaderMode()) {
                    leaderSelector = new LeaderSelector(client, getZkLeaderPath(), this);
                    leaderSelector.autoRequeue();
                    leaderSelector.start();
                }
                value = new DistributedAtomicValue(client, getZkValuePath(), new ExponentialBackoffRetry(1000, 10));
                initialized = true;
            }
        }
    }
}
项目:workflow    文件:SchedulerSelector.java   
public SchedulerSelector(WorkflowManagerImpl workflowManager, QueueFactory queueFactory, AutoCleanerHolder autoCleanerHolder)
{
    this.workflowManager = workflowManager;
    this.queueFactory = queueFactory;
    this.autoCleanerHolder = autoCleanerHolder;

    LeaderSelectorListener listener = new LeaderSelectorListenerAdapter()
    {
        @Override
        public void takeLeadership(CuratorFramework client) throws Exception
        {
            SchedulerSelector.this.takeLeadership();
        }
    };
    leaderSelector = new LeaderSelector(workflowManager.getCurator(), ZooKeeperConstants.getSchedulerLeaderPath(), listener);
    leaderSelector.autoRequeue();
}
项目:tachyon-rdma    文件:LeaderSelectorClient.java   
public LeaderSelectorClient(String zookeeperAddress, String electionPath, String leaderPath,
    String name) {
  ZOOKEEPER_ADDRESS = zookeeperAddress;
  ELECTION_PATH = electionPath;
  if (leaderPath.endsWith(Constants.PATH_SEPARATOR)) {
    LEADER_FOLDER = leaderPath;
  } else {
    LEADER_FOLDER = leaderPath + Constants.PATH_SEPARATOR;
  }
  NAME = name;

  // create a leader selector using the given path for management
  // all participants in a given leader selection must use the same path
  // ExampleClient here is also a LeaderSelectorListener but this isn't required
  CuratorFramework client =
      CuratorFrameworkFactory.newClient(ZOOKEEPER_ADDRESS, new ExponentialBackoffRetry(
          Constants.SECOND_MS, 3));
  client.start();
  LEADER_SELECTOR = new LeaderSelector(client, ELECTION_PATH, this);
  LEADER_SELECTOR.setId(name);

  // for most cases you will want your instance to requeue when it relinquishes leadership
  LEADER_SELECTOR.autoRequeue();
}
项目:spring-cloud-cluster    文件:LeaderInitiator.java   
/**
 * Start the registration of the {@link #candidate} for leader election.
 */
@Override
public synchronized void start() {
    if (!this.running) {
        if (this.client.getState() != CuratorFrameworkState.STARTED) {
            // we want to do curator start here because it needs to
            // be started before leader selector and it gets a little
            // complicated to control ordering via beans so that
            // curator is fully started.
            this.client.start();
        }
        this.leaderSelector = new LeaderSelector(this.client, buildLeaderPath(), new LeaderListener());
        this.leaderSelector.setId(this.candidate.getId());
        this.leaderSelector.autoRequeue();
        this.leaderSelector.start();

        this.running = true;
    }
}
项目:javabase    文件:MasterSelector.java   
public MasterSelector(String name, CuratorFramework client, String path) {
    this.name=name;
    //leaderSelector 多个客户端监听同一个节点
    this.leaderSelector=new LeaderSelector(client, path, this);
    //保证在此实例释放领导权之后还可能获得领导权
    leaderSelector.autoRequeue();
    //启动
    leaderSelector.start();
}
项目:javabase    文件:ClientSelector.java   
public ClientSelector(String name,CuratorFramework client, String path) {
    this.name=name;
    //leaderSelector 多个客户端监听同一个节点
    this.leaderSelector=new LeaderSelector(client, path, this);
    //保证在此实例释放领导权之后还可能获得领导权
    leaderSelector.autoRequeue();
    //启动
    leaderSelector.start();
}
项目:pravega    文件:SegmentContainerMonitor.java   
/**
 * Monitor to manage pravega host addition and removal in the cluster.
 *
 * @param hostStore             The store to read and write the host container mapping data.
 * @param client                The curator client for coordination.
 * @param balancer              The host to segment container balancer implementation.
 * @param minRebalanceInterval  The minimum interval between any two rebalance operations in seconds.
 *                              0 indicates there can be no waits between retries.
 */
public SegmentContainerMonitor(HostControllerStore hostStore, CuratorFramework client, ContainerBalancer balancer,
        int minRebalanceInterval) {
    Preconditions.checkNotNull(hostStore, "hostStore");
    Preconditions.checkNotNull(client, "client");
    Preconditions.checkNotNull(balancer, "balancer");

    leaderZKPath = ZKPaths.makePath("cluster", "faulthandlerleader");

    segmentMonitorLeader = new SegmentMonitorLeader(hostStore, balancer, minRebalanceInterval);
    leaderSelector = new LeaderSelector(client, leaderZKPath, segmentMonitorLeader);

    //Listen for any zookeeper connectivity error and relinquish leadership.
    client.getConnectionStateListenable().addListener(
            (curatorClient, newState) -> {
                switch (newState) {
                    case LOST:
                        log.warn("Connection to zookeeper lost, attempting to interrrupt the leader thread");
                        leaderSelector.interruptLeadership();
                        break;
                    case SUSPENDED:
                        if (leaderSelector.hasLeadership()) {
                            log.info("Zookeeper session suspended, pausing the segment monitor");
                            segmentMonitorLeader.suspend();
                        }
                        break;
                    case RECONNECTED:
                        if (leaderSelector.hasLeadership()) {
                            log.info("Zookeeper session reconnected, resume the segment monitor");
                            segmentMonitorLeader.resume();
                        }
                        break;
                    //$CASES-OMITTED$
                    default:
                        log.debug("Connection state to zookeeper updated: " + newState.toString());
                }
            }
    );
}
项目:OracleToElasticSearch    文件:SimpleClient.java   
public SimpleClient(String clientName, CuratorFramework client, String path) {
    this.clientName = clientName;
    this.client = client;
    this.path = path;
    leaderSelector = new LeaderSelector(this.client,this.path, this);
    leaderSelector.autoRequeue();
    // initialize oracle change notification
    ctx = new ClassPathXmlApplicationContext("spring-context.xml");
    consumer = (DBNotifactionConsumer)ctx.getBean("consumer");
}
项目:oracledbnotification    文件:SimpleClient.java   
public SimpleClient(String clientName, CuratorFramework client, String path) {
    this.clientName = clientName;
    this.client = client;
    this.path = path;
    leaderSelector = new LeaderSelector(this.client,this.path, this);
    leaderSelector.autoRequeue();
    // initialize oracle change notification
    ctx = new ClassPathXmlApplicationContext("spring-context.xml");
    consumer = (DBNotifactionConsumer)ctx.getBean("consumer");
}
项目:curator    文件:ExampleClient.java   
public ExampleClient(CuratorFramework client, String path, String name)
{
    this.name = name;

    // create a leader selector using the given path for management
    // all participants in a given leader selection must use the same path
    // ExampleClient here is also a LeaderSelectorListener but this isn't required
    leaderSelector = new LeaderSelector(client, path, this);

    // for most cases you will want your instance to requeue when it relinquishes leadership
    leaderSelector.autoRequeue();
}
项目:rakam    文件:KafkaEventStore.java   
@Inject
public KafkaEventStore(@Named("event.store.kafka") KafkaConfig config, FieldDependencyBuilder.FieldDependency fieldDependency) {
    config = checkNotNull(config, "config is null");
    this.sourceFields = Sets.union(fieldDependency.dependentFields.keySet(),
            fieldDependency.constantFields.stream().map(SchemaField::getName)
                    .collect(Collectors.toSet()));

    Properties props = new Properties();
    props.put("metadata.broker.list", config.getNodes().stream().map(HostAndPort::toString).collect(Collectors.joining(",")));
    props.put("serializer.class", config.SERIALIZER);

    ProducerConfig producerConfig = new ProducerConfig(props);
    this.producer = new Producer(producerConfig);

    CuratorFramework client = CuratorFrameworkFactory.newClient(config.getZookeeperNode().toString(),
            new ExponentialBackoffRetry(1000, 3));
    client.start();

    try {
        if (client.checkExists().forPath(ZK_OFFSET_PATH) == null)
            client.create().forPath(ZK_OFFSET_PATH);
    } catch (Exception e) {
        LOGGER.error(e, format("Couldn't create event offset path %s", ZK_OFFSET_PATH));
    }

    new LeaderSelector(client, ZK_OFFSET_PATH, this).start();
}
项目:fluo    文件:OracleClient.java   
@Override
public void run() {

  try {
    synchronized (this) {
      // want this code to be mutually exclusive with close() .. so if in middle of setup, close
      // method will wait till finished
      if (closed.get()) {
        return;
      }

      curatorFramework = CuratorUtil.newAppCurator(env.getConfiguration());
      CuratorCnxnListener cnxnListener = new CuratorCnxnListener();
      curatorFramework.getConnectionStateListenable().addListener(cnxnListener);
      curatorFramework.start();

      while (!cnxnListener.isConnected()) {
        Thread.sleep(200);
      }

      leaderSelector = new LeaderSelector(curatorFramework, ZookeeperPath.ORACLE_SERVER, this);

      pathChildrenCache =
          new PathChildrenCache(curatorFramework, ZookeeperPath.ORACLE_SERVER, true);
      pathChildrenCache.getListenable().addListener(this);
      pathChildrenCache.start();

      connect();
    }
    doWork();
  } catch (Exception e) {
    if (!closed.get()) {
      log.error("Exception occurred in run() method", e);
    } else {
      log.debug("Exception occurred in run() method", e);
    }
  }
}
项目:fluo    文件:OracleServer.java   
public synchronized void start() throws Exception {
  if (started) {
    throw new IllegalStateException();
  }

  final InetSocketAddress addr = startServer();

  curatorFramework = CuratorUtil.newAppCurator(env.getConfiguration());
  curatorFramework.getConnectionStateListenable().addListener(cnxnListener);
  curatorFramework.start();

  while (!cnxnListener.isConnected()) {
    Thread.sleep(200);
  }

  leaderSelector = new LeaderSelector(curatorFramework, ZookeeperPath.ORACLE_SERVER, this);
  String leaderId = HostUtil.getHostName() + ":" + addr.getPort();
  leaderSelector.setId(leaderId);
  log.info("Leader ID = " + leaderId);
  leaderSelector.start();

  pathChildrenCache = new PathChildrenCache(curatorFramework, oraclePath, true);
  pathChildrenCache.getListenable().addListener(this);
  pathChildrenCache.start();

  while (!cnxnListener.isConnected()) {
    Thread.sleep(200);
  }

  log.info("Listening " + addr);

  started = true;
}
项目:Juice    文件:LeaderSelectorClient.java   
public LeaderSelectorClient(SchedulerDriver schedulerDriver) {
    leaderSelector = new LeaderSelector(schedulerDriver.getCuratorUtils().getClient(), ZKLOCKS + HTTP_SEPERATOR + MESOS_FRAMEWORK_TAG, this);
    leaderSelector.autoRequeue();
    this.schedulerDriver = schedulerDriver;

}
项目:star-map    文件:SelectorClient.java   
@Autowired
public SelectorClient(CuratorFramework curatorFramework) {
    this.leaderSelector = new LeaderSelector(curatorFramework, selectorPath, this);
    this.leaderSelector.autoRequeue();
}
项目:mycat-src-1.6.1-RELEASE    文件:DistributedSequenceHandler.java   
public LeaderSelector getLeaderSelector() {
    return leaderSelector;
}
项目:dble    文件:DistributedSequenceHandler.java   
public LeaderSelector getLeaderSelector() {
    return leaderSelector;
}
项目:replicaza    文件:ShutdownHook.java   
public ShutdownHook(LeaderSelector leaderSelector, CuratorFramework curatorFramework) {
    this.leaderSelector = leaderSelector;
    this.curatorFramework = curatorFramework;
}
项目:replicator    文件:ZookeeperCoordinator.java   
public CoordinatorLeaderElectionListener(CuratorFramework client, String path, Runnable onLeadership) {
    super();
    leaderSelector = new LeaderSelector(client, path, this);

    callback = onLeadership;
}
项目:light-eventuate-4j    文件:EventTableChangesToAggregateTopicTranslator.java   
public EventTableChangesToAggregateTopicTranslator(CdcKafkaPublisher<EVENT> cdcKafkaPublisher, CdcProcessor<EVENT> cdcProcessor, CuratorFramework client, CdcConfig cdcConfig) {
  this.cdcKafkaPublisher = cdcKafkaPublisher;
  this.cdcProcessor = cdcProcessor;
  this.cdcConfig = cdcConfig;
  this.leaderSelector = new LeaderSelector(client, cdcConfig.getLeadershipLockPath(), new EventuateLeaderSelectorListener(this));
}
项目:cicada    文件:ClusterLeaderManager.java   
@PostConstruct
public void init() {
  leaderSelector = new LeaderSelector(zkClient, props.getMasterNodePath(), this);
  leaderSelector.autoRequeue();
}
项目:paradox-nakadi-consumer    文件:ZKConsumerLeader.java   
private LeaderControl(final LeaderSelector selector) {
    this.selector = selector;
}
项目:paradox-nakadi-consumer    文件:ZKConsumerLeader.java   
static LeaderControl of(final LeaderSelector selector) {
    return new LeaderControl(selector);
}
项目:xio    文件:ZkClient.java   
public void electLeader(String ELECTION_PATH, LeaderSelectorListener listener) {
  leaderSelector = new LeaderSelector(client, ELECTION_PATH, listener);

  leaderSelector.autoRequeue();
  leaderSelector.start();
}
项目:ZKRecipesByExample    文件:ExampleClient.java   
public ExampleClient(CuratorFramework client, String path, String name) {
    this.name = name;
    leaderSelector = new LeaderSelector(client, path, this);
    leaderSelector.autoRequeue();
}
项目:workflow    文件:SchedulerSelector.java   
@VisibleForTesting
LeaderSelector getLeaderSelector()
{
    return leaderSelector;
}
项目:replicaza    文件:App.java   
public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {
    config = new Config(args[0]);

CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(config.getProperty("zookeeper.hosts"), new ExponentialBackoffRetry(1000, 3));
    curatorFramework.start();

    LeaderSelector leaderSelector = new LeaderSelector(curatorFramework, "/replicaza_leader", new LeaderListener());
    leaderSelector.start();

      Runtime.getRuntime().addShutdownHook(new ShutdownHook(leaderSelector, curatorFramework));

      Thread.currentThread().join();
  }
项目:zookeeper-book-example    文件:CuratorMaster.java   
/**
 * Creates a new Curator client, setting the the retry policy
 * to ExponentialBackoffRetry.
 * 
 * @param myId
 *          master identifier
 * @param hostPort
 *          list of zookeeper servers comma-separated
 * @param retryPolicy
 *          Curator retry policy
 */
public CuratorMaster(String myId, String hostPort, RetryPolicy retryPolicy){
    LOG.info( myId + ": " + hostPort );

    this.myId = myId;
    this.client = CuratorFrameworkFactory.newClient(hostPort, 
            retryPolicy);
    this.leaderSelector = new LeaderSelector(this.client, "/master", this);
    this.workersCache = new PathChildrenCache(this.client, "/workers", true);
    this.tasksCache = new PathChildrenCache(this.client, "/tasks", true);
}
项目:zookeeper-book-example    文件:CuratorMasterSelector.java   
/**
 * Creates a new Curator client, setting the the retry policy
 * to ExponentialBackoffRetry.
 * 
 * @param myId
 *          master identifier
 * @param hostPort
 *          list of zookeeper servers comma-separated
 * @param retryPolicy
 *          Curator retry policy
 */
public CuratorMasterSelector(String myId, String hostPort, RetryPolicy retryPolicy){
    LOG.info( myId + ": " + hostPort );

    this.myId = myId;
    this.client = CuratorFrameworkFactory.newClient(hostPort, 
            retryPolicy);
    this.leaderSelector = new LeaderSelector(this.client, "/master", this);
    this.workersCache = new PathChildrenCache(this.client, "/workers", true);
    this.tasksCache = new PathChildrenCache(this.client, "/tasks", true);
}