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

The following examples show how to use org.apache.curator.framework.recipes.leader.LeaderLatch. These examples are extracted from open source projects. 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 Project: mykit-delay   Source File: SimpleLeaderManager.java    License: 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 2
Source Project: liteflow   Source File: StartupRunner.java    License: 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 3
Source Project: sdmq   Source File: SimpleLeaderManager.java    License: 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 4
Source Project: xian   Source File: ChildReaper.java    License: 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 5
Source Project: xian   Source File: Reaper.java    License: 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 6
Source Project: Baragon   Source File: BaragonServiceTestBase.java    License: 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 7
Source Project: atlas   Source File: ActiveInstanceElectorServiceTest.java    License: 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 8
Source Project: atlas   Source File: ActiveInstanceElectorServiceTest.java    License: 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 9
Source Project: atlas   Source File: ActiveInstanceElectorServiceTest.java    License: 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 10
Source Project: atlas   Source File: ActiveInstanceElectorServiceTest.java    License: 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 Project: atlas   Source File: ActiveInstanceElectorServiceTest.java    License: 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 Project: Mycat2   Source File: MycatLeaderLatch.java    License: 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 Project: registry   Source File: ZKLeadershipParticipant.java    License: 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 Project: x-pipe   Source File: DefaultLeaderElector.java    License: 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 Project: Baragon   Source File: StatusResource.java    License: 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
@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
@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
@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
@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
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 Project: Baragon   Source File: StatusManager.java    License: 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 Project: Singularity   Source File: DeployResource.java    License: 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 Project: Singularity   Source File: SandboxResource.java    License: 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 Project: Singularity   Source File: SlaveResource.java    License: 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 Project: Singularity   Source File: RackResource.java    License: 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 Project: Singularity   Source File: AbstractRequestResource.java    License: 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 Project: Singularity   Source File: SingularityLifecycleManaged.java    License: 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
@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 29
/**
 * 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 Project: Baragon   Source File: LifecycleHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Inject
public LifecycleHelper(BaragonWorkerDatastore workerDatastore,
                       BaragonAuthDatastore authDatastore,
                       BaragonAgentConfiguration configuration,
                       BaragonAgentMetadata baragonAgentMetadata,
                       FilesystemConfigHelper configHelper,
                       BaragonStateDatastore stateDatastore,
                       ServerProvider serverProvider,
                       AtomicReference<BaragonAgentState> agentState,
                       @Named(BaragonAgentServiceModule.BARAGON_AGENT_HTTP_CLIENT) NingHttpClient httpClient,
                       @Named(BaragonAgentServiceModule.AGENT_SCHEDULED_EXECUTOR) ScheduledExecutorService executorService,
                       @Named(BaragonAgentServiceModule.AGENT_LEADER_LATCH) LeaderLatch leaderLatch,
                       @Named(BaragonAgentServiceModule.AGENT_LOCK) ReentrantLock agentLock,
                       @Named(BaragonAgentServiceModule.AGENT_LOCK_TIMEOUT_MS) long agentLockTimeoutMs,
                       @Named(BaragonAgentServiceModule.INTERNAL_STATE_CACHE) Map<String, BasicServiceContext> internalStateCache) {
  this.workerDatastore = workerDatastore;
  this.authDatastore = authDatastore;
  this.configuration = configuration;
  this.baragonAgentMetadata = baragonAgentMetadata;
  this.configHelper = configHelper;
  this.stateDatastore = stateDatastore;
  this.serverProvider = serverProvider;
  this.agentState = agentState;
  this.httpClient = httpClient;
  this.executorService = executorService;
  this.leaderLatch = leaderLatch;
  this.agentLock = agentLock;
  this.agentLockTimeoutMs = agentLockTimeoutMs;
  this.internalStateCache = internalStateCache;
}