org.apache.curator.framework.recipes.leader.Participant Java Examples
The following examples show how to use
org.apache.curator.framework.recipes.leader.Participant.
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: BaragonServiceTestBase.java From Baragon with Apache License 2.0 | 6 votes |
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 #2
Source File: OracleServer.java From fluo with Apache License 2.0 | 6 votes |
@Override public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception { try { if (isConnected() && (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED) || event.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED) || event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED))) { synchronized (this) { Participant participant = leaderLatch.getLeader(); if (isLeader(participant) && !leaderLatch.hasLeadership()) { // in case current instance becomes leader, we want to know who came before it. currentLeader = participant; } } } } catch (InterruptedException e) { log.warn("Oracle leadership watcher has been interrupted unexpectedly"); } }
Example #3
Source File: OracleClient.java From fluo with Apache License 2.0 | 6 votes |
/** * It's possible an Oracle has gone into a bad state. Upon the leader being changed, we want to * update our state */ @Override public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception { if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED) || event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED) || event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)) { Participant participant = leaderLatch.getLeader(); synchronized (this) { if (isLeader(participant)) { currentLeader = leaderLatch.getLeader(); } else { currentLeader = null; } } } }
Example #4
Source File: ServiceDiscoveryStateService.java From kylin with Apache License 2.0 | 5 votes |
public Set<Participant> getAllParticipants() { CuratorLeaderSelector leaderSelector = CuratorScheduler.getLeaderSelector(); if (leaderSelector != null) { return leaderSelector.getParticipants(); } else { return Collections.emptySet(); } }
Example #5
Source File: LeaderServiceTest.java From curator-extensions with Apache License 2.0 | 5 votes |
/** Test starting and stopping a single instance of LeaderService. */ @Test public void testLifeCycle() throws Exception { ServiceTriggers triggers = new ServiceTriggers(); LeaderService leader = newLeaderService(1, TimeUnit.HOURS, supply(triggers.listenTo(new NopService()))); assertEquals("test-id", leader.getId()); assertFalse(leader.hasLeadership()); // Start trying to obtain leadership leader.startAsync(); assertTrue(triggers.getRunning().firedWithin(1, TimeUnit.MINUTES)); assertTrue(leader.isRunning()); assertTrue(leader.hasLeadership()); assertEquals(new Participant("test-id", true), leader.getLeader()); assertEquals(Collections.singletonList(new Participant("test-id", true)), leader.getParticipants()); assertFalse(triggers.getTerminated().hasFired()); assertTrue(leader.getCurrentDelegateService().get().isRunning()); // Start watching ZooKeeper directly for changes WatchTrigger childrenTrigger = WatchTrigger.childrenTrigger(); _curator.getChildren().usingWatcher(childrenTrigger).forPath(PATH); // Stop trying to obtain leadership leader.stopAsync(); assertTrue(triggers.getTerminated().firedWithin(1, TimeUnit.SECONDS)); assertFalse(leader.isRunning()); assertFalse(leader.getCurrentDelegateService().isPresent()); // Wait for stopped state to reflect in ZooKeeper then poll ZooKeeper for leadership participants state assertTrue(childrenTrigger.firedWithin(1, TimeUnit.SECONDS)); assertFalse(leader.hasLeadership()); assertTrue(_curator.getChildren().forPath(PATH).isEmpty()); assertEquals(new Participant("", false), leader.getLeader()); assertEquals(Collections.<Participant>emptyList(), leader.getParticipants()); }
Example #6
Source File: CuratorLeaderElectionManager.java From nifi with Apache License 2.0 | 5 votes |
@Override public String getLeader(final String roleName) { if (isStopped()) { return determineLeaderExternal(roleName); } final LeaderRole role = getLeaderRole(roleName); if (role == null) { return determineLeaderExternal(roleName); } final long startNanos = System.nanoTime(); Participant participant; try { participant = role.getLeaderSelector().getLeader(); } catch (Exception e) { logger.debug("Unable to determine leader for role '{}'; returning null", roleName); return null; } if (participant == null) { return null; } final String participantId = participant.getId(); if (StringUtils.isEmpty(participantId)) { return null; } registerPollTime(System.nanoTime() - startNanos); final String previousLeader = lastKnownLeader.put(roleName, participantId); if (previousLeader != null && !previousLeader.equals(participantId)) { onLeaderChanged(roleName); } return participantId; }
Example #7
Source File: CuratorLeaderSelector.java From kylin-on-parquet-v2 with Apache License 2.0 | 5 votes |
public Participant getLeader() { try { return leaderSelector.getLeader(); } catch (Exception e) { logger.error("Can not get leader.", e); } return new Participant("", false); }
Example #8
Source File: CuratorLeaderSelector.java From kylin with Apache License 2.0 | 5 votes |
public Set<Participant> getParticipants() { Set<Participant> r = new HashSet<>(); try { r.addAll(leaderSelector.getParticipants()); } catch (Exception e) { logger.error("Can not get participants.", e); } return r; }
Example #9
Source File: CuratorLeaderSelector.java From kylin with Apache License 2.0 | 5 votes |
public Participant getLeader() { try { return leaderSelector.getLeader(); } catch (Exception e) { logger.error("Can not get leader.", e); } return new Participant("", false); }
Example #10
Source File: CuratorLeaderElectionManager.java From localization_nifi with Apache License 2.0 | 5 votes |
@Override public String getLeader(final String roleName) { if (isStopped()) { return determineLeaderExternal(roleName); } final LeaderRole role = getLeaderRole(roleName); if (role == null) { return determineLeaderExternal(roleName); } Participant participant; try { participant = role.getLeaderSelector().getLeader(); } catch (Exception e) { logger.debug("Unable to determine leader for role '{}'; returning null", roleName); return null; } if (participant == null) { return null; } final String participantId = participant.getId(); if (StringUtils.isEmpty(participantId)) { return null; } return participantId; }
Example #11
Source File: ServiceDiscoveryStateService.java From kylin-on-parquet-v2 with Apache License 2.0 | 5 votes |
public Set<Participant> getAllParticipants() { CuratorLeaderSelector leaderSelector = CuratorScheduler.getLeaderSelector(); if (leaderSelector != null) { return leaderSelector.getParticipants(); } else { return Collections.emptySet(); } }
Example #12
Source File: CuratorLeaderSelector.java From kylin-on-parquet-v2 with Apache License 2.0 | 5 votes |
public Set<Participant> getParticipants() { Set<Participant> r = new HashSet<>(); try { r.addAll(leaderSelector.getParticipants()); } catch (Exception e) { logger.error("Can not get participants.", e); } return r; }
Example #13
Source File: CuratorLeaderSelectorTest.java From kylin with Apache License 2.0 | 4 votes |
@Test public void testGetBasic() throws SchedulerException, IOException, InterruptedException { KylinConfig kylinConfig = KylinConfig.getInstanceFromEnv(); final String zkString = zkTestServer.getConnectString(); final String server1 = "server1:1111"; final String server2 = "server2:2222"; String jobEnginePath = CuratorScheduler.JOB_ENGINE_LEADER_PATH; CuratorFramework client = CuratorFrameworkFactory.newClient(zkString, new ExponentialBackoffRetry(3000, 3)); client.start(); CuratorLeaderSelector s1 = new CuratorLeaderSelector(client // , jobEnginePath // , server1 // , new JobEngineConfig(kylinConfig)); // Assert.assertFalse(s1.hasDefaultSchedulerStarted()); CuratorLeaderSelector s2 = new CuratorLeaderSelector(client // , jobEnginePath // , server2 // , new JobEngineConfig(kylinConfig)); // s1.start(); //wait for Selector starting Thread.sleep(1000); Assert.assertEquals(1, s1.getParticipants().size()); Assert.assertEquals(s1.getParticipants(), s2.getParticipants()); s2.start(); Thread.sleep(1000); Assert.assertEquals(2, s1.getParticipants().size()); Assert.assertEquals(s1.getParticipants(), s2.getParticipants()); Assert.assertEquals(new Participant(server1, true), s1.getLeader()); Assert.assertEquals(s1.getLeader(), s2.getLeader()); assertSchedulerStart(s1); s1.close(); Thread.sleep(1000); Assert.assertEquals(1, s1.getParticipants().size()); Assert.assertEquals(s1.getParticipants(), s2.getParticipants()); Assert.assertEquals(new Participant(server2, true), s1.getLeader()); assertSchedulerStart(s2); s2.close(); Thread.sleep(1000); Assert.assertEquals(0, s1.getParticipants().size()); Assert.assertEquals(s1.getParticipants(), s2.getParticipants()); }
Example #14
Source File: ServiceDiscoveryStateController.java From kylin with Apache License 2.0 | 4 votes |
@RequestMapping(value = "/all", method = { RequestMethod.GET }, produces = { "application/json" }) @ResponseBody public EnvelopeResponse<ServiceDiscoveryState> getAllNodeStates() throws IOException { checkCuratorSchedulerEnabled(); Set<String> allNodes = new HashSet<>(); Set<String> queryNodes = new HashSet<>(); Set<String> jobNodes = new HashSet<>(); Set<String> leaders = new HashSet<>(); // get all nodes and query nodes for (String serverWithMode : KylinConfig.getInstanceFromEnv().getRestServersWithMode()) { String[] split = serverWithMode.split(":"); Preconditions.checkArgument(split.length == 3, "String should be \"host:server:mode\", actual:" + serverWithMode); String server = split[0] + ":" + split[1]; String mode = split[2]; allNodes.add(server); if (mode.equals("query") || mode.equals("all")) { queryNodes.add(server); } if (mode.equals("job") || mode.equals("all")) { jobNodes.add(server); } } // Get all selection participants(only job nodes will participate in the election) and selected leaders Set<Participant> allParticipants = serviceDiscoveryStateService.getAllParticipants(); if (!allParticipants.isEmpty()) { jobNodes = allParticipants.stream() // .map(Participant::getId) // .collect(Collectors.toSet()); // // There should only one leader, if there are more than one leader, means something wrong happened leaders = allParticipants.stream() // .filter(Participant::isLeader) // .map(Participant::getId) // .collect(Collectors.toSet()); // } // Ask for other nodes for its job server state // current Kylin only has one active job node // If there are more than one active job node, means something wrong happened Set<String> activeJobNodes = getActiveJobNodes(allNodes); return new EnvelopeResponse<>(ResponseCode.CODE_SUCCESS, new ServiceDiscoveryState(allNodes, jobNodes, queryNodes, leaders, activeJobNodes), "get service discovery's state"); }
Example #15
Source File: MycatLeaderLatch.java From Mycat2 with GNU General Public License v3.0 | 4 votes |
public Collection<Participant> getParticipants() throws Exception { return latch.getParticipants(); }
Example #16
Source File: LeadershipManager.java From Decision with Apache License 2.0 | 4 votes |
public Participant currentLeader() throws Exception { return leaderLatch.getLeader(); }
Example #17
Source File: ServiceDiscoveryStateController.java From kylin-on-parquet-v2 with Apache License 2.0 | 4 votes |
@RequestMapping(value = "/all", method = { RequestMethod.GET }, produces = { "application/json" }) @ResponseBody public EnvelopeResponse<ServiceDiscoveryState> getAllNodeStates() throws IOException { checkCuratorSchedulerEnabled(); Set<String> allNodes = new HashSet<>(); Set<String> queryNodes = new HashSet<>(); Set<String> jobNodes = new HashSet<>(); Set<String> leaders = new HashSet<>(); // get all nodes and query nodes for (String serverWithMode : KylinConfig.getInstanceFromEnv().getRestServersWithMode()) { String[] split = serverWithMode.split(":"); Preconditions.checkArgument(split.length == 3, "String should be \"host:server:mode\", actual:" + serverWithMode); String server = split[0] + ":" + split[1]; String mode = split[2]; allNodes.add(server); if (mode.equals("query") || mode.equals("all")) { queryNodes.add(server); } if (mode.equals("job") || mode.equals("all")) { jobNodes.add(server); } } // Get all selection participants(only job nodes will participate in the election) and selected leaders Set<Participant> allParticipants = serviceDiscoveryStateService.getAllParticipants(); if (!allParticipants.isEmpty()) { jobNodes = allParticipants.stream() // .map(Participant::getId) // .collect(Collectors.toSet()); // // There should only one leader, if there are more than one leader, means something wrong happened leaders = allParticipants.stream() // .filter(Participant::isLeader) // .map(Participant::getId) // .collect(Collectors.toSet()); // } // Ask for other nodes for its job server state // current Kylin only has one active job node // If there are more than one active job node, means something wrong happened Set<String> activeJobNodes = getActiveJobNodes(allNodes); return new EnvelopeResponse<>(ResponseCode.CODE_SUCCESS, new ServiceDiscoveryState(allNodes, jobNodes, queryNodes, leaders, activeJobNodes), "get service discovery's state"); }
Example #18
Source File: OracleClient.java From fluo with Apache License 2.0 | 4 votes |
private boolean isLeader(Participant participant) { return participant != null && participant.isLeader(); }
Example #19
Source File: OracleServer.java From fluo with Apache License 2.0 | 4 votes |
private boolean isLeader(Participant participant) { return participant != null && participant.isLeader(); }
Example #20
Source File: CuratorLeaderSelectorTest.java From kylin-on-parquet-v2 with Apache License 2.0 | 4 votes |
@Test public void testGetBasic() throws SchedulerException, IOException, InterruptedException { KylinConfig kylinConfig = KylinConfig.getInstanceFromEnv(); final String zkString = zkTestServer.getConnectString(); final String server1 = "server1:1111"; final String server2 = "server2:2222"; String jobEnginePath = CuratorScheduler.JOB_ENGINE_LEADER_PATH; CuratorFramework client = CuratorFrameworkFactory.newClient(zkString, new ExponentialBackoffRetry(3000, 3)); client.start(); CuratorLeaderSelector s1 = new CuratorLeaderSelector(client // , jobEnginePath // , server1 // , new JobEngineConfig(kylinConfig)); // Assert.assertFalse(s1.hasDefaultSchedulerStarted()); CuratorLeaderSelector s2 = new CuratorLeaderSelector(client // , jobEnginePath // , server2 // , new JobEngineConfig(kylinConfig)); // s1.start(); //wait for Selector starting Thread.sleep(1000); Assert.assertEquals(1, s1.getParticipants().size()); Assert.assertEquals(s1.getParticipants(), s2.getParticipants()); s2.start(); Thread.sleep(1000); Assert.assertEquals(2, s1.getParticipants().size()); Assert.assertEquals(s1.getParticipants(), s2.getParticipants()); Assert.assertEquals(new Participant(server1, true), s1.getLeader()); Assert.assertEquals(s1.getLeader(), s2.getLeader()); assertSchedulerStart(s1); s1.close(); Thread.sleep(1000); Assert.assertEquals(1, s1.getParticipants().size()); Assert.assertEquals(s1.getParticipants(), s2.getParticipants()); Assert.assertEquals(new Participant(server2, true), s1.getLeader()); assertSchedulerStart(s2); s2.close(); Thread.sleep(1000); Assert.assertEquals(0, s1.getParticipants().size()); Assert.assertEquals(s1.getParticipants(), s2.getParticipants()); }
Example #21
Source File: LeaderService.java From curator-extensions with Apache License 2.0 | 2 votes |
/** * <B>NOTE</B> - this method polls the ZooKeeper server. Therefore it may return a value that does not match * {@link #hasLeadership()} as hasLeadership returns a cached value. * * @return The set of current participants in the leader selection. * @throws Exception ZooKeeper or thread error. */ public Collection<Participant> getParticipants() throws Exception { return _latch.getParticipants(); }
Example #22
Source File: LeaderService.java From curator-extensions with Apache License 2.0 | 2 votes |
/** * <B>NOTE</B> - this method polls the ZooKeeper server. Therefore it may return a value that does not match * {@link #hasLeadership()} as hasLeadership returns a cached value. * * @return The id for the current leader. If for some reason there is no current leader, a dummy participant * is returned. * @throws Exception ZooKeeper or thread error. */ public Participant getLeader() throws Exception { return _latch.getLeader(); }