本文整理汇总了Java中org.apache.curator.framework.recipes.leader.LeaderLatch类的典型用法代码示例。如果您正苦于以下问题:Java LeaderLatch类的具体用法?Java LeaderLatch怎么用?Java LeaderLatch使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LeaderLatch类属于org.apache.curator.framework.recipes.leader包,在下文中一共展示了LeaderLatch类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
@Override
public void run(String... strings) throws Exception {
client = CuratorFrameworkFactory.newClient(zookeeperConnString,
new ExponentialBackoffRetry(1000, Integer.MAX_VALUE));
client.start();
client.getZookeeperClient().blockUntilConnectedOrTimedOut();
leaderLatch = new LeaderLatch(client, "/http-job-scheduler/leader",
ManagementFactory.getRuntimeMXBean().getName());
leaderLatch.addListener(new LeaderLatchListener() {
@Override
public void isLeader() {
setMaster(true);
masterJobScheduler.resume();
}
@Override
public void notLeader() {
setMaster(false);
masterJobScheduler.pause();
}
});
leaderLatch.start();
}
示例2: participateForLeadership
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
/**
* 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());
}
}
示例3: startLeaderElect
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
/**
* 异步选主,只能启动一次
*
* @param listener 选主监听器
*/
public void startLeaderElect(final LeaderLatchListener listener) {
executor.submit(new Runnable() {
@Override
public void run() {
boolean errFlag = true;
leaderLatch = new LeaderLatch((CuratorFramework) registryCenter.getRawClient(), ConsoleNode.LATCH);
leaderLatch.addListener(listener);
do {
try {
leaderLatch.start();
leaderLatch.await();
} catch (Exception e) {
log.error("Failed to elect a Leader! will retry", e);
errFlag = false;
}
} while (!errFlag);
}
});
}
示例4: elect
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
@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);
}
示例5: testLeaderElectionIsJoinedOnStart
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
@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);
activeInstanceElectorService.start();
verify(leaderLatch).start();
}
示例6: testListenerIsAddedForActiveInstanceCallbacks
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
@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);
}
示例7: testLeaderElectionIsLeftOnStop
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
@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();
}
示例8: testCuratorFactoryIsClosedOnStop
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
@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();
}
示例9: testSharedStateIsUpdatedWhenInstanceIsActive
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
@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");
}
示例10: ChildReaper
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
/**
* @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);
}
示例11: addListenerToLeaderLatch
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
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());
}
示例12: releaseControl
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
@Override
public void releaseControl(long dpid) {
log.info("Releasing control for {}", HexString.toHexString(dpid));
String dpidStr = HexString.toHexString(dpid);
SwitchLeadershipData swData = switches.remove(dpidStr);
if (swData == null) {
log.debug("Trying to release control of a switch we are not contesting");
return;
}
LeaderLatch latch = swData.getLatch();
latch.removeListener(swData.getListener());
try {
latch.close();
} catch (IOException e) {
// I think it's OK not to do anything here. Either the node got
// deleted correctly, or the connection went down and the node got deleted.
log.debug("releaseControl: caught IOException {}", dpidStr);
}
}
示例13: TaskResource
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
@Inject
public TaskResource(TaskRequestManager taskRequestManager, TaskManager taskManager, SlaveManager slaveManager, MesosClient mesosClient, SingularityTaskMetadataConfiguration taskMetadataConfiguration,
SingularityAuthorizationHelper authorizationHelper, RequestManager requestManager, SingularityValidator validator, DisasterManager disasterManager,
AsyncHttpClient httpClient, LeaderLatch leaderLatch, ObjectMapper objectMapper, RequestHelper requestHelper) {
super(httpClient, leaderLatch, objectMapper);
this.taskManager = taskManager;
this.taskRequestManager = taskRequestManager;
this.taskMetadataConfiguration = taskMetadataConfiguration;
this.slaveManager = slaveManager;
this.mesosClient = mesosClient;
this.requestManager = requestManager;
this.authorizationHelper = authorizationHelper;
this.validator = validator;
this.disasterManager = disasterManager;
this.requestHelper = requestHelper;
}
示例14: injectPollerDependencies
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
@Inject
void injectPollerDependencies(SingularityManagedScheduledExecutorServiceFactory executorServiceFactory,
LeaderLatch leaderLatch,
SingularityExceptionNotifier exceptionNotifier,
SingularityAbort abort,
SingularityMesosScheduler mesosScheduler,
SingularityConfiguration configuration,
@Named(SingularityMainModule.STATUS_UPDATE_DELTA_30S_AVERAGE) AtomicLong statusUpdateDelta30sAverage) {
this.executorService = executorServiceFactory.get(getClass().getSimpleName());
this.leaderLatch = checkNotNull(leaderLatch, "leaderLatch is null");
this.exceptionNotifier = checkNotNull(exceptionNotifier, "exceptionNotifier is null");
this.abort = checkNotNull(abort, "abort is null");
this.mesosScheduler = checkNotNull(mesosScheduler, "mesosScheduler is null");
this.delayPollersWhenDeltaOverMs = configuration.getDelayPollersWhenDeltaOverMs();
this.statusUpdateDelta30sAverage = checkNotNull(statusUpdateDelta30sAverage, "statusUpdateDeltaAverage is null");
}
示例15: StatusResource
import org.apache.curator.framework.recipes.leader.LeaderLatch; //导入依赖的package包/类
@Inject
public StatusResource(LocalLbAdapter adapter,
LoadBalancerConfiguration loadBalancerConfiguration,
BaragonAgentMetadata agentMetadata,
AtomicReference<BaragonAgentState> agentState,
@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) {
this.adapter = adapter;
this.loadBalancerConfiguration = loadBalancerConfiguration;
this.leaderLatch = leaderLatch;
this.mostRecentRequestId = mostRecentRequestId;
this.connectionState = connectionState;
this.agentMetadata = agentMetadata;
this.errorMessage = errorMessage;
this.agentState = agentState;
}