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

The following examples show how to use org.apache.curator.framework.recipes.leader.LeaderLatch. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SingularityLifecycleManagedTest.java    From Singularity with Apache License 2.0 6 votes vote down vote up
@Inject
public SingularityLifecycleManagedTest(
  SingularityManagedThreadPoolFactory cachedThreadPoolFactory,
  SingularityManagedScheduledExecutorServiceFactory scheduledExecutorServiceFactory,
  AsyncHttpClient asyncHttpClient,
  CuratorFramework curatorFramework,
  SingularityLeaderController leaderController,
  LeaderLatch leaderLatch,
  SingularityMesosExecutorInfoSupport executorInfoSupport,
  SingularityGraphiteReporter graphiteReporter,
  ExecutorIdGenerator executorIdGenerator,
  Set<SingularityLeaderOnlyPoller> leaderOnlyPollers
) {
  super(
    cachedThreadPoolFactory,
    scheduledExecutorServiceFactory,
    asyncHttpClient,
    curatorFramework,
    leaderController,
    leaderLatch,
    executorInfoSupport,
    graphiteReporter,
    executorIdGenerator,
    leaderOnlyPollers
  );
}
 
Example #2
Source File: ActiveInstanceElectorServiceTest.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testLeaderElectionIsLeftOnStop() throws IOException, AtlasException {
    when(configuration.containsKey(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getBoolean(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getStringArray(HAConfiguration.ATLAS_SERVER_IDS)).thenReturn(new String[] {"id1"});
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn("127.0.0.1:21000");
    when(configuration.getString(
            HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
            thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);

    LeaderLatch leaderLatch = mock(LeaderLatch.class);
    when(curatorFactory.leaderLatchInstance("id1", HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(leaderLatch);

    ActiveInstanceElectorService activeInstanceElectorService =
            new ActiveInstanceElectorService(configuration, new HashSet<ActiveStateChangeHandler>(), curatorFactory,
                    activeInstanceState, serviceState, metricsUtil);
    activeInstanceElectorService.start();
    activeInstanceElectorService.stop();

    verify(leaderLatch).close();
}
 
Example #3
Source File: StartupRunner.java    From liteflow with Apache License 2.0 6 votes vote down vote up
@Override
public void run(String... strings) throws Exception {
    LeaderLatch leaderLatch = new LeaderLatch(client, path);
    leaderLatch.addListener(new LeaderLatchListener() {
        @Override
        public void isLeader() {
            MasterInfo.setIsMaster(true);
            refreshLeaderIp();
            scheduler.start();
        }

        @Override
        public void notLeader() {
            MasterInfo.setIsMaster(false);
            scheduler.stop();
            EventQueue.clear();
        }
    });
    leaderLatch.start();
}
 
Example #4
Source File: ActiveInstanceElectorServiceTest.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testListenerIsAddedForActiveInstanceCallbacks() throws Exception {
    when(configuration.containsKey(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getBoolean(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getStringArray(HAConfiguration.ATLAS_SERVER_IDS)).thenReturn(new String[] {"id1"});
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn("127.0.0.1:21000");
    when(configuration.getString(
            HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
            thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);

    LeaderLatch leaderLatch = mock(LeaderLatch.class);
    when(curatorFactory.leaderLatchInstance("id1", HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(leaderLatch);

    ActiveInstanceElectorService activeInstanceElectorService =
            new ActiveInstanceElectorService(configuration, new HashSet<ActiveStateChangeHandler>(), curatorFactory,
                    activeInstanceState, serviceState, metricsUtil);
    activeInstanceElectorService.start();

    verify(leaderLatch).addListener(activeInstanceElectorService);
}
 
Example #5
Source File: SimpleLeaderManager.java    From sdmq with Apache License 2.0 6 votes vote down vote up
public void init() {
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
            .connectString(properties.getServerList())
            .retryPolicy(new ExponentialBackoffRetry(properties.getBaseSleepTimeMilliseconds(),
                    properties.getMaxRetries(),
                    properties.getMaxSleepTimeMilliseconds()))
            .namespace(ServerNode.NAMESPACE);
    framework = builder.build();
    framework.start();
    leaderLatch = new LeaderLatch(framework, ServerNode.LEADERLATCH, serverName, LeaderLatch.CloseMode.NOTIFY_LEADER);
    for (LeaderLatchListener listener : listeners) {
        leaderLatch.addListener(listener);
    }
    LOGGER.info("starting Queue Master Slave Model ...");
    start();
}
 
Example #6
Source File: ActiveInstanceElectorServiceTest.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testLeaderElectionIsJoinedOnStart() throws Exception {
    when(configuration.containsKey(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getBoolean(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getStringArray(HAConfiguration.ATLAS_SERVER_IDS)).thenReturn(new String[] {"id1"});
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn("127.0.0.1:21000");
    when(configuration.getString(
            HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
            thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);
    LeaderLatch leaderLatch = mock(LeaderLatch.class);
    when(curatorFactory.leaderLatchInstance("id1", HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(leaderLatch);

    ActiveInstanceElectorService activeInstanceElectorService =
            new ActiveInstanceElectorService(configuration, new HashSet<ActiveStateChangeHandler>(), curatorFactory,
                    activeInstanceState, serviceState, metricsUtil);
    activeInstanceElectorService.start();

    verify(leaderLatch).start();
}
 
Example #7
Source File: BaragonServiceTestBase.java    From Baragon with Apache License 2.0 6 votes vote down vote up
protected void startAgent(String baseUrl, String group) {
  try {
    BaragonAgentMetadata agentMetadata = new BaragonAgentMetadata(
        baseUrl,
        UUID.randomUUID().toString(),
        Optional.absent(),
        null,
        Optional.absent(),
        null,
        true);
    LeaderLatch leaderLatch = loadBalancerDatastore.createLeaderLatch(group, agentMetadata);
    String id = leaderLatch.getId();
    leaderLatch.start();
    activeLeaderLatch.add(leaderLatch);
    while (leaderLatch.getParticipants().stream().map(Participant::getId).noneMatch(id::equals)) {
      Thread.sleep(5);
    }
  } catch (Throwable t) {
    throw new RuntimeException(t);
  }
}
 
Example #8
Source File: ChildReaper.java    From xian with Apache License 2.0 6 votes vote down vote up
/**
 * @param client the client
 * @param path path to reap children from
 * @param executor executor to use for background tasks
 * @param reapingThresholdMs threshold in milliseconds that determines that a path can be deleted
 * @param mode reaping mode
 * @param leaderPath if not null, uses a leader selection so that only 1 reaper is active in the cluster
 * @param lockSchema a set of the possible subnodes of the children of path that must be reaped in addition to the child nodes
 */
public ChildReaper(CuratorFramework client, String path, Reaper.Mode mode, ScheduledExecutorService executor, int reapingThresholdMs, String leaderPath, Set<String> lockSchema)
{
    this.client = client;
    this.mode = mode;
    this.executor = new CloseableScheduledExecutorService(executor);
    this.reapingThresholdMs = reapingThresholdMs;
    if (leaderPath != null)
    {
        leaderLatch = new LeaderLatch(client, leaderPath);
    }
    else
    {
        leaderLatch = null;
    }
    this.reaper = new Reaper(client, executor, reapingThresholdMs, leaderLatch);
    this.lockSchema = lockSchema;
    addPath(path);
}
 
Example #9
Source File: Reaper.java    From xian with Apache License 2.0 6 votes vote down vote up
private void addListenerToLeaderLatch(LeaderLatch leaderLatch)
{

    LeaderLatchListener listener = new LeaderLatchListener()
    {
        @Override
        public void isLeader()
        {
            reapingIsActive.set(true);
            for ( PathHolder holder : activePaths.values() )
            {
                schedule(holder, reapingThresholdMs);
            }
        }

        @Override
        public void notLeader()
        {
            reapingIsActive.set(false);
        }
    };
    leaderLatch.addListener(listener);

    reapingIsActive.set(leaderLatch.hasLeadership());
}
 
Example #10
Source File: ActiveInstanceElectorServiceTest.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testCuratorFactoryIsClosedOnStop() throws AtlasException {
    when(configuration.containsKey(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getBoolean(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getStringArray(HAConfiguration.ATLAS_SERVER_IDS)).thenReturn(new String[] {"id1"});
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn("127.0.0.1:21000");
    when(configuration.getString(
            HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
            thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);

    LeaderLatch leaderLatch = mock(LeaderLatch.class);
    when(curatorFactory.leaderLatchInstance("id1", HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(leaderLatch);

    ActiveInstanceElectorService activeInstanceElectorService =
            new ActiveInstanceElectorService(configuration, new HashSet<ActiveStateChangeHandler>(), curatorFactory,
                    activeInstanceState, serviceState, metricsUtil);
    activeInstanceElectorService.start();
    activeInstanceElectorService.stop();

    verify(curatorFactory).close();
}
 
Example #11
Source File: ActiveInstanceElectorServiceTest.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testSharedStateIsUpdatedWhenInstanceIsActive() throws Exception {
    when(configuration.containsKey(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getBoolean(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getStringArray(HAConfiguration.ATLAS_SERVER_IDS)).thenReturn(new String[] {"id1"});
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn("127.0.0.1:21000");
    when(configuration.getString(
            HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
            thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);

    LeaderLatch leaderLatch = mock(LeaderLatch.class);
    when(curatorFactory.leaderLatchInstance("id1", HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(leaderLatch);

    ActiveInstanceElectorService activeInstanceElectorService =
            new ActiveInstanceElectorService(configuration, new HashSet<ActiveStateChangeHandler>(), curatorFactory,
                    activeInstanceState, serviceState, metricsUtil);

    activeInstanceElectorService.start();
    activeInstanceElectorService.isLeader();

    verify(activeInstanceState).update("id1");
}
 
Example #12
Source File: MycatLeaderLatch.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
public MycatLeaderLatch( String latchPath )  {		
	this.myId = ZkConfig.getInstance().getValue(ZkParamCfg.ZK_CFG_MYID);
	this.latchPath = ZKUtils.getZKBasePath() + latchPath;
	this.client  = ZKUtils.getConnection();
	isLeader = false;
	//ZKUtils.createPath(this.latchPath, "");
	latch = new LeaderLatch(client, this.latchPath ,this.myId);	
	
	Map<String, PhysicalDBPool> dataSourceHosts = MycatServer.getInstance().getConfig().getDataHosts();
	try {
		for(String dataSource : dataSourceHosts.keySet()) {
			manageHeartBeatChangeSet.add(new ManageHeartBeatChange(this, dataSource));
		}
	} catch (Exception e) {
		LOGGER.warn("init ManageHeartBeatChange err:", e);
	}
	
}
 
Example #13
Source File: ZKLeadershipParticipant.java    From registry with Apache License 2.0 6 votes vote down vote up
/**
 * Participates for leader lock with the given configuration.
 *
 * @throws Exception if any errors encountered.
 */
@Override
public void participateForLeadership() throws Exception {
    // if the existing leader latch is closed, recreate and connect again
    if (LeaderLatch.State.CLOSED.equals(leaderLatchRef.get().getState())) {
        // remove listener from earlier closed leader latch
        leaderLatchRef.get().removeListener(leaderLatchListener);

        leaderLatchRef.set(createLeaderLatch());
        leaderLatchRef.get().addListener(leaderLatchListener);
        LOG.info("Existing leader latch is in CLOSED state, it is recreated.");
    }

    // if the existing leader latch is not yet started, start now!!
    if (LeaderLatch.State.LATENT.equals(leaderLatchRef.get().getState())) {
        leaderLatchRef.get().start();
        LOG.info("Existing leader latch is in LATENT state, it is started. leader latch: [{}]", leaderLatchRef.get());
    }
}
 
Example #14
Source File: DefaultLeaderElector.java    From x-pipe with Apache License 2.0 6 votes vote down vote up
@Override
public void elect() throws Exception {
	
	zkClient.createContainers(ctx.getLeaderElectionZKPath());
	
	latch = new LeaderLatch(zkClient, ctx.getLeaderElectionZKPath(), ctx.getLeaderElectionID());
	latch.addListener(new LeaderLatchListener() {

		@Override
		public void notLeader() {
		}

		@Override
		public void isLeader() {
		}
	});

	latch.start();
	logger.info("[elect]{}", ctx);
}
 
Example #15
Source File: StatusResource.java    From Baragon with Apache License 2.0 6 votes vote down vote up
@Inject
public StatusResource(LocalLbAdapter adapter,
                      LoadBalancerConfiguration loadBalancerConfiguration,
                      BaragonAgentMetadata agentMetadata,
                      AtomicReference<BaragonAgentState> agentState,
                      DirectoryChangesListener directoryChangesListener,
                      @Named(BaragonAgentServiceModule.AGENT_LEADER_LATCH) LeaderLatch leaderLatch,
                      @Named(BaragonAgentServiceModule.AGENT_MOST_RECENT_REQUEST_ID) AtomicReference<String> mostRecentRequestId,
                      @Named(BaragonDataModule.BARAGON_ZK_CONNECTION_STATE) AtomicReference<ConnectionState> connectionState,
                      @Named(BaragonAgentServiceModule.CONFIG_ERROR_MESSAGE) AtomicReference<Optional<String>> errorMessage,
                      @Named(BaragonAgentServiceModule.LOCAL_STATE_ERROR_MESSAGE) Set<String> stateErrors,
                      @Named(BaragonAgentServiceModule.INTERNAL_STATE_CACHE) Map<String, BasicServiceContext> internalStateCache) {
  this.adapter = adapter;
  this.loadBalancerConfiguration = loadBalancerConfiguration;
  this.leaderLatch = leaderLatch;
  this.mostRecentRequestId = mostRecentRequestId;
  this.connectionState = connectionState;
  this.agentMetadata = agentMetadata;
  this.errorMessage = errorMessage;
  this.stateErrors = stateErrors;
  this.agentState = agentState;
  this.internalStateCache = internalStateCache;
  this.directoryChangesListener = directoryChangesListener;
}
 
Example #16
Source File: ActiveInstanceElectorServiceTest.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testListenerIsAddedForActiveInstanceCallbacks() throws Exception {
    when(configuration.containsKey(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getBoolean(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getStringArray(HAConfiguration.ATLAS_SERVER_IDS)).thenReturn(new String[] {"id1"});
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn("127.0.0.1:21000");
    when(configuration.getString(
            HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
            thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);

    LeaderLatch leaderLatch = mock(LeaderLatch.class);
    when(curatorFactory.leaderLatchInstance("id1", HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(leaderLatch);

    ActiveInstanceElectorService activeInstanceElectorService =
            new ActiveInstanceElectorService(configuration, new HashSet<ActiveStateChangeHandler>(), curatorFactory,
                    activeInstanceState, serviceState);
    activeInstanceElectorService.start();

    verify(leaderLatch).addListener(activeInstanceElectorService);
}
 
Example #17
Source File: ActiveInstanceElectorServiceTest.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testLeaderElectionIsLeftOnStop() throws IOException, AtlasException {
    when(configuration.containsKey(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getBoolean(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getStringArray(HAConfiguration.ATLAS_SERVER_IDS)).thenReturn(new String[] {"id1"});
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn("127.0.0.1:21000");
    when(configuration.getString(
            HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
            thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);

    LeaderLatch leaderLatch = mock(LeaderLatch.class);
    when(curatorFactory.leaderLatchInstance("id1", HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(leaderLatch);

    ActiveInstanceElectorService activeInstanceElectorService =
            new ActiveInstanceElectorService(configuration, new HashSet<ActiveStateChangeHandler>(), curatorFactory,
                    activeInstanceState, serviceState);
    activeInstanceElectorService.start();
    activeInstanceElectorService.stop();

    verify(leaderLatch).close();
}
 
Example #18
Source File: ActiveInstanceElectorServiceTest.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testCuratorFactoryIsClosedOnStop() throws AtlasException {
    when(configuration.containsKey(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getBoolean(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getStringArray(HAConfiguration.ATLAS_SERVER_IDS)).thenReturn(new String[] {"id1"});
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn("127.0.0.1:21000");
    when(configuration.getString(
            HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
            thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);

    LeaderLatch leaderLatch = mock(LeaderLatch.class);
    when(curatorFactory.leaderLatchInstance("id1", HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(leaderLatch);

    ActiveInstanceElectorService activeInstanceElectorService =
            new ActiveInstanceElectorService(configuration, new HashSet<ActiveStateChangeHandler>(), curatorFactory,
                    activeInstanceState, serviceState);
    activeInstanceElectorService.start();
    activeInstanceElectorService.stop();

    verify(curatorFactory).close();
}
 
Example #19
Source File: ActiveInstanceElectorServiceTest.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testSharedStateIsUpdatedWhenInstanceIsActive() throws Exception {
    when(configuration.containsKey(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getBoolean(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getStringArray(HAConfiguration.ATLAS_SERVER_IDS)).thenReturn(new String[] {"id1"});
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn("127.0.0.1:21000");
    when(configuration.getString(
            HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
            thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);

    LeaderLatch leaderLatch = mock(LeaderLatch.class);
    when(curatorFactory.leaderLatchInstance("id1", HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(leaderLatch);

    ActiveInstanceElectorService activeInstanceElectorService =
            new ActiveInstanceElectorService(configuration, new HashSet<ActiveStateChangeHandler>(), curatorFactory,
                    activeInstanceState, serviceState);

    activeInstanceElectorService.start();
    activeInstanceElectorService.isLeader();

    verify(activeInstanceState).update("id1");
}
 
Example #20
Source File: AgentLeaderElector.java    From jumbune with GNU Lesser General Public License v3.0 6 votes vote down vote up
public AgentLeaderElector(AgentNode agentNode, String zkHost) {
	this.zkHost = zkHost;
	this.agentNode = agentNode;
	curator = CuratorFrameworkFactory.newClient(this.zkHost, new ExponentialBackoffRetry(1000, 3));
	curator.start();
	
	createAgentZnodes();
	
	leaderLatch = new LeaderLatch(this.curator, ZKConstants.AGENT_FOLLOWER_PATH);
	
	HashSet<AgentNode> followers = getFollowers();
	if (followers == null) {
		followers = new HashSet<>();
	}
	followers.add(agentNode);
	CONSOLE_LOGGER.info("setting followers data - " + followers);
	try {
		curator.setData().forPath(ZKConstants.AGENT_FOLLOWER_PATH, GSON.toJson(followers).getBytes());
	} catch (Exception e) {
		e.printStackTrace();
	}
	
	CONSOLE_LOGGER.info("new participant[agent] for election - " + agentNode);
	LOGGER.debug("new participant[agent] for election - " + agentNode);
}
 
Example #21
Source File: StatusManager.java    From Baragon with Apache License 2.0 6 votes vote down vote up
@Inject
public StatusManager(BaragonRequestDatastore requestDatastore,
                     ObjectMapper objectMapper,
                     @Named(BaragonDataModule.BARAGON_SERVICE_LEADER_LATCH) LeaderLatch leaderLatch,
                     @Named(BaragonDataModule.BARAGON_SERVICE_WORKER_LAST_START) AtomicLong workerLastStart,
                     @Named(BaragonDataModule.BARAGON_ELB_WORKER_LAST_START) AtomicLong elbWorkerLastStart,
                     @Named(BaragonDataModule.BARAGON_ZK_CONNECTION_STATE) AtomicReference<ConnectionState> connectionState,
                     @Named(BaragonServiceModule.BARAGON_SERVICE_HTTP_CLIENT)AsyncHttpClient httpClient) {
  this.requestDatastore = requestDatastore;
  this.leaderLatch = leaderLatch;
  this.workerLastStart = workerLastStart;
  this.elbWorkerLastStart = elbWorkerLastStart;
  this.connectionState = connectionState;
  this.httpClient = httpClient;
  this.objectMapper = objectMapper;
}
 
Example #22
Source File: DeployResource.java    From Singularity with Apache License 2.0 6 votes vote down vote up
@Inject
public DeployResource(
  RequestManager requestManager,
  DeployManager deployManager,
  SingularityValidator validator,
  SingularityAuthorizer authorizationHelper,
  SingularityConfiguration configuration,
  TaskManager taskManager,
  LeaderLatch leaderLatch,
  AsyncHttpClient httpClient,
  @Singularity ObjectMapper objectMapper,
  RequestHelper requestHelper
) {
  super(
    requestManager,
    deployManager,
    validator,
    authorizationHelper,
    httpClient,
    leaderLatch,
    objectMapper,
    requestHelper
  );
  this.configuration = configuration;
  this.taskManager = taskManager;
}
 
Example #23
Source File: SandboxResource.java    From Singularity with Apache License 2.0 6 votes vote down vote up
@Inject
public SandboxResource(
  AsyncHttpClient httpClient,
  LeaderLatch leaderLatch,
  @Singularity ObjectMapper objectMapper,
  HistoryManager historyManager,
  TaskManager taskManager,
  SandboxManager sandboxManager,
  DeployManager deployManager,
  SingularityMesosExecutorInfoSupport logSupport,
  SingularityConfiguration configuration,
  SingularityAuthorizer authorizationHelper
) {
  super(
    httpClient,
    leaderLatch,
    objectMapper,
    historyManager,
    taskManager,
    deployManager,
    authorizationHelper
  );
  this.configuration = configuration;
  this.sandboxManager = sandboxManager;
  this.logSupport = logSupport;
}
 
Example #24
Source File: SlaveResource.java    From Singularity with Apache License 2.0 6 votes vote down vote up
@Inject
public SlaveResource(
  AsyncHttpClient httpClient,
  LeaderLatch leaderLatch,
  @Singularity ObjectMapper objectMapper,
  SlaveManager slaveManager,
  SingularityAuthorizer authorizationHelper,
  SingularityValidator validator
) {
  super(
    httpClient,
    leaderLatch,
    objectMapper,
    slaveManager,
    authorizationHelper,
    validator
  );
}
 
Example #25
Source File: RackResource.java    From Singularity with Apache License 2.0 6 votes vote down vote up
@Inject
public RackResource(
  AsyncHttpClient httpClient,
  LeaderLatch leaderLatch,
  @Singularity ObjectMapper objectMapper,
  RackManager rackManager,
  SingularityAuthorizer authorizationHelper,
  SingularityValidator validator
) {
  super(
    httpClient,
    leaderLatch,
    objectMapper,
    rackManager,
    authorizationHelper,
    validator
  );
}
 
Example #26
Source File: AbstractRequestResource.java    From Singularity with Apache License 2.0 6 votes vote down vote up
public AbstractRequestResource(
  RequestManager requestManager,
  DeployManager deployManager,
  SingularityValidator validator,
  SingularityAuthorizer authorizationHelper,
  AsyncHttpClient httpClient,
  LeaderLatch leaderLatch,
  ObjectMapper objectMapper,
  RequestHelper requestHelper
) {
  super(httpClient, leaderLatch, objectMapper);
  this.requestManager = requestManager;
  this.deployManager = deployManager;
  this.requestHelper = requestHelper;
  this.validator = validator;
  this.authorizationHelper = authorizationHelper;
}
 
Example #27
Source File: SingularityLifecycleManaged.java    From Singularity with Apache License 2.0 6 votes vote down vote up
@Inject
public SingularityLifecycleManaged(
  SingularityManagedThreadPoolFactory cachedThreadPoolFactory,
  SingularityManagedScheduledExecutorServiceFactory scheduledExecutorServiceFactory,
  AsyncHttpClient asyncHttpClient,
  CuratorFramework curatorFramework,
  SingularityLeaderController leaderController,
  LeaderLatch leaderLatch,
  SingularityMesosExecutorInfoSupport executorInfoSupport,
  SingularityGraphiteReporter graphiteReporter,
  ExecutorIdGenerator executorIdGenerator,
  Set<SingularityLeaderOnlyPoller> leaderOnlyPollers
) {
  this.cachedThreadPoolFactory = cachedThreadPoolFactory;
  this.scheduledExecutorServiceFactory = scheduledExecutorServiceFactory;
  this.asyncHttpClient = asyncHttpClient;
  this.curatorFramework = curatorFramework;
  this.leaderController = leaderController;
  this.leaderLatch = leaderLatch;
  this.executorInfoSupport = executorInfoSupport;
  this.graphiteReporter = graphiteReporter;
  this.executorIdGenerator = executorIdGenerator;
  this.leaderOnlyPollers = leaderOnlyPollers;
}
 
Example #28
Source File: SimpleLeaderManager.java    From mykit-delay with Apache License 2.0 6 votes vote down vote up
public void init() {
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
            .connectString(properties.getServerList())
            .retryPolicy(new ExponentialBackoffRetry(properties.getBaseSleepTimeMilliseconds(),
                    properties.getMaxRetries(),
                    properties.getMaxSleepTimeMilliseconds()))
            .namespace(ServerNode.NAMESPACE);
    framework = builder.build();
    framework.start();
    leaderLatch = new LeaderLatch(framework, ServerNode.LEADERLATCH, serverName, LeaderLatch.CloseMode.NOTIFY_LEADER);
    for (LeaderLatchListener listener : listeners) {
        leaderLatch.addListener(listener);
    }
    LOGGER.info("starting Queue Master Slave Model ...");
    start();
}
 
Example #29
Source File: ZooKeeperLeaderElectionService.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a ZooKeeperLeaderElectionService object.
 *
 * @param client Client which is connected to the ZooKeeper quorum
 * @param latchPath ZooKeeper node path for the leader election latch
 * @param leaderPath ZooKeeper node path for the node which stores the current leader information
 */
public ZooKeeperLeaderElectionService(CuratorFramework client, String latchPath, String leaderPath) {
	this.client = Preconditions.checkNotNull(client, "CuratorFramework client");
	this.leaderPath = Preconditions.checkNotNull(leaderPath, "leaderPath");

	leaderLatch = new LeaderLatch(client, latchPath);
	cache = new NodeCache(client, leaderPath);

	issuedLeaderSessionID = null;
	confirmedLeaderSessionID = null;
	leaderContender = null;

	running = false;
}
 
Example #30
Source File: S3LogResource.java    From Singularity with Apache License 2.0 5 votes vote down vote up
@Inject
public S3LogResource(
  AsyncHttpClient httpClient,
  LeaderLatch leaderLatch,
  @Singularity ObjectMapper objectMapper,
  RequestManager requestManager,
  HistoryManager historyManager,
  RequestHistoryHelper requestHistoryHelper,
  TaskManager taskManager,
  DeployManager deployManager,
  Optional<S3Configuration> configuration,
  SingularityAuthorizer authorizationHelper,
  SingularityS3Services s3Services
) {
  super(
    httpClient,
    leaderLatch,
    objectMapper,
    historyManager,
    taskManager,
    deployManager,
    authorizationHelper
  );
  this.requestManager = requestManager;
  this.configuration = configuration;
  this.requestHistoryHelper = requestHistoryHelper;
  this.s3Services = s3Services;
}