use of org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService in project flink by apache.
the class KubernetesMultipleComponentLeaderElectionDriverTest method testPublishLeaderInformation.
@Test
public void testPublishLeaderInformation() throws Exception {
new TestFixture() {
{
runTest(() -> {
leaderCallbackGrantLeadership();
leaderElectionListener.await(LeaderElectionEvent.IsLeaderEvent.class);
final LeaderInformation leaderInformation = LeaderInformation.known(UUID.randomUUID(), "localhost");
final String componentId = "componentId";
final DefaultLeaderRetrievalService leaderRetrievalService = new DefaultLeaderRetrievalService(new KubernetesMultipleComponentLeaderRetrievalDriverFactory(getFlinkKubeClient(), getConfigMapSharedWatcher(), testExecutorExtension.getExecutor(), LEADER_CONFIGMAP_NAME, componentId));
final TestingListener leaderRetrievalListener = new TestingListener();
leaderRetrievalService.start(leaderRetrievalListener);
leaderElectionDriver.publishLeaderInformation(componentId, leaderInformation);
notifyLeaderRetrievalWatchOnModifiedConfigMap();
leaderRetrievalListener.waitForNewLeader(10_000L);
assertThat(leaderRetrievalListener.getLeader()).isEqualTo(leaderInformation);
});
}
};
}
use of org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService in project flink by apache.
the class ZooKeeperLeaderElectionTest method testZooKeeperReelectionWithReplacement.
/**
* Tests the repeated reelection of {@link LeaderContender} once the current leader dies.
* Furthermore, it tests that new LeaderElectionServices can be started later on and that they
* successfully register at ZooKeeper and take part in the leader election.
*/
@Test
public void testZooKeeperReelectionWithReplacement() throws Exception {
int num = 3;
int numTries = 30;
DefaultLeaderElectionService[] leaderElectionService = new DefaultLeaderElectionService[num];
TestingContender[] contenders = new TestingContender[num];
DefaultLeaderRetrievalService leaderRetrievalService = null;
TestingListener listener = new TestingListener();
try {
leaderRetrievalService = ZooKeeperUtils.createLeaderRetrievalService(curatorFrameworkWrapper.asCuratorFramework());
leaderRetrievalService.start(listener);
for (int i = 0; i < num; i++) {
leaderElectionService[i] = ZooKeeperUtils.createLeaderElectionService(curatorFrameworkWrapper.asCuratorFramework());
contenders[i] = new TestingContender(LEADER_ADDRESS + "_" + i + "_0", leaderElectionService[i]);
leaderElectionService[i].start(contenders[i]);
}
String pattern = LEADER_ADDRESS + "_" + "(\\d+)" + "_" + "(\\d+)";
Pattern regex = Pattern.compile(pattern);
for (int i = 0; i < numTries; i++) {
listener.waitForNewLeader(timeout);
String address = listener.getAddress();
Matcher m = regex.matcher(address);
if (m.find()) {
int index = Integer.parseInt(m.group(1));
int lastTry = Integer.parseInt(m.group(2));
assertEquals(listener.getLeaderSessionID(), contenders[index].getLeaderSessionID());
// stop leader election service = revoke leadership
leaderElectionService[index].stop();
// create new leader election service which takes part in the leader election
leaderElectionService[index] = ZooKeeperUtils.createLeaderElectionService(curatorFrameworkWrapper.asCuratorFramework());
contenders[index] = new TestingContender(LEADER_ADDRESS + "_" + index + "_" + (lastTry + 1), leaderElectionService[index]);
leaderElectionService[index].start(contenders[index]);
} else {
throw new Exception("Did not find the leader's index.");
}
}
} finally {
if (leaderRetrievalService != null) {
leaderRetrievalService.stop();
}
for (DefaultLeaderElectionService electionService : leaderElectionService) {
if (electionService != null) {
electionService.stop();
}
}
}
}
use of org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService in project flink by apache.
the class ZooKeeperLeaderElectionTest method testZooKeeperReelection.
/**
* Tests repeatedly the reelection of still available LeaderContender. After a contender has
* been elected as the leader, it is removed. This forces the DefaultLeaderElectionService to
* elect a new leader.
*/
@Test
public void testZooKeeperReelection() throws Exception {
Deadline deadline = Deadline.fromNow(Duration.ofMinutes(5L));
int num = 10;
DefaultLeaderElectionService[] leaderElectionService = new DefaultLeaderElectionService[num];
TestingContender[] contenders = new TestingContender[num];
DefaultLeaderRetrievalService leaderRetrievalService = null;
TestingListener listener = new TestingListener();
try {
leaderRetrievalService = ZooKeeperUtils.createLeaderRetrievalService(curatorFrameworkWrapper.asCuratorFramework());
LOG.debug("Start leader retrieval service for the TestingListener.");
leaderRetrievalService.start(listener);
for (int i = 0; i < num; i++) {
leaderElectionService[i] = ZooKeeperUtils.createLeaderElectionService(curatorFrameworkWrapper.asCuratorFramework());
contenders[i] = new TestingContender(createAddress(i), leaderElectionService[i]);
LOG.debug("Start leader election service for contender #{}.", i);
leaderElectionService[i].start(contenders[i]);
}
String pattern = LEADER_ADDRESS + "_" + "(\\d+)";
Pattern regex = Pattern.compile(pattern);
int numberSeenLeaders = 0;
while (deadline.hasTimeLeft() && numberSeenLeaders < num) {
LOG.debug("Wait for new leader #{}.", numberSeenLeaders);
String address = listener.waitForNewLeader(deadline.timeLeft().toMillis());
Matcher m = regex.matcher(address);
if (m.find()) {
int index = Integer.parseInt(m.group(1));
TestingContender contender = contenders[index];
// check that the retrieval service has retrieved the correct leader
if (address.equals(createAddress(index)) && listener.getLeaderSessionID().equals(contender.getLeaderSessionID())) {
// kill the election service of the leader
LOG.debug("Stop leader election service of contender #{}.", numberSeenLeaders);
leaderElectionService[index].stop();
leaderElectionService[index] = null;
numberSeenLeaders++;
}
} else {
fail("Did not find the leader's index.");
}
}
assertFalse("Did not complete the leader reelection in time.", deadline.isOverdue());
assertEquals(num, numberSeenLeaders);
} finally {
if (leaderRetrievalService != null) {
leaderRetrievalService.stop();
}
for (DefaultLeaderElectionService electionService : leaderElectionService) {
if (electionService != null) {
electionService.stop();
}
}
}
}
use of org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService in project flink by apache.
the class ZooKeeperMultipleComponentLeaderElectionDriverTest method testPublishEmptyLeaderInformation.
@Test
public void testPublishEmptyLeaderInformation() throws Exception {
new Context() {
{
runTest(() -> {
leaderElectionListener.await(LeaderElectionEvent.IsLeaderEvent.class);
final String componentId = "retrieved-component";
final DefaultLeaderRetrievalService defaultLeaderRetrievalService = new DefaultLeaderRetrievalService(new ZooKeeperLeaderRetrievalDriverFactory(curatorFramework.asCuratorFramework(), componentId, ZooKeeperLeaderRetrievalDriver.LeaderInformationClearancePolicy.ON_LOST_CONNECTION));
final TestingListener leaderRetrievalListener = new TestingListener();
defaultLeaderRetrievalService.start(leaderRetrievalListener);
leaderElectionDriver.publishLeaderInformation(componentId, LeaderInformation.known(UUID.randomUUID(), "foobar"));
leaderRetrievalListener.waitForNewLeader(10_000L);
leaderElectionDriver.publishLeaderInformation(componentId, LeaderInformation.empty());
leaderRetrievalListener.waitForEmptyLeaderInformation(10_000L);
assertThat(leaderRetrievalListener.getLeader()).isEqualTo(LeaderInformation.empty());
});
}
};
}
use of org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService in project flink by apache.
the class ZooKeeperMultipleComponentLeaderElectionDriverTest method testPublishLeaderInformation.
@Test
public void testPublishLeaderInformation() throws Exception {
new Context() {
{
runTest(() -> {
leaderElectionListener.await(LeaderElectionEvent.IsLeaderEvent.class);
final String componentId = "retrieved-component";
final DefaultLeaderRetrievalService defaultLeaderRetrievalService = new DefaultLeaderRetrievalService(new ZooKeeperLeaderRetrievalDriverFactory(curatorFramework.asCuratorFramework(), componentId, ZooKeeperLeaderRetrievalDriver.LeaderInformationClearancePolicy.ON_LOST_CONNECTION));
final TestingListener leaderRetrievalListener = new TestingListener();
defaultLeaderRetrievalService.start(leaderRetrievalListener);
final LeaderInformation leaderInformation = LeaderInformation.known(UUID.randomUUID(), "foobar");
leaderElectionDriver.publishLeaderInformation(componentId, leaderInformation);
leaderRetrievalListener.waitForNewLeader(10_000L);
assertThat(leaderRetrievalListener.getLeader()).isEqualTo(leaderInformation);
});
}
};
}
Aggregations