use of org.apache.pulsar.metadata.api.coordination.LeaderElectionState in project pulsar by apache.
the class LeaderElectionTest method leaderNodeIsDeletedExternally.
@Test(dataProvider = "impl")
public void leaderNodeIsDeletedExternally(String provider, Supplier<String> urlSupplier) throws Exception {
@Cleanup MetadataStoreExtended store = MetadataStoreExtended.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
@Cleanup CoordinationService coordinationService = new CoordinationServiceImpl(store);
BlockingQueue<LeaderElectionState> notifications = new LinkedBlockingDeque<>();
@Cleanup LeaderElection<String> leaderElection = coordinationService.getLeaderElection(String.class, "/my/leader-election", t -> {
notifications.add(t);
});
LeaderElectionState les = leaderElection.elect("test-1").join();
assertEquals(les, LeaderElectionState.Leading);
assertEquals(notifications.poll(3, TimeUnit.SECONDS), LeaderElectionState.Leading);
store.delete("/my/leader-election", Optional.empty()).join();
assertEquals(notifications.poll(3, TimeUnit.SECONDS), LeaderElectionState.Leading);
assertEquals(les, LeaderElectionState.Leading);
}
use of org.apache.pulsar.metadata.api.coordination.LeaderElectionState in project pulsar by apache.
the class LeaderElectionTest method revalidateLeaderWithDifferentSessionsSameValue.
@Test(dataProvider = "impl")
public void revalidateLeaderWithDifferentSessionsSameValue(String provider, Supplier<String> urlSupplier) throws Exception {
if (provider.equals("Memory") || provider.equals("RocksDB")) {
// There are no multiple sessions for the local memory provider
return;
}
@Cleanup MetadataStoreExtended store = MetadataStoreExtended.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
@Cleanup MetadataStoreExtended store2 = MetadataStoreExtended.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
String path = newKey();
@Cleanup CoordinationService cs = new CoordinationServiceImpl(store);
@Cleanup LeaderElection<String> le = cs.getLeaderElection(String.class, path, __ -> {
});
store2.put(path, ObjectMapperFactory.getThreadLocal().writeValueAsBytes("test-1"), Optional.of(-1L), EnumSet.of(CreateOption.Ephemeral)).join();
LeaderElectionState les = le.elect("test-1").join();
assertEquals(les, LeaderElectionState.Leading);
assertEquals(le.getLeaderValue().join(), Optional.of("test-1"));
assertEquals(le.getLeaderValueIfPresent(), Optional.of("test-1"));
}
use of org.apache.pulsar.metadata.api.coordination.LeaderElectionState in project pulsar by apache.
the class LeaderElectionTest method basicTest.
@Test(dataProvider = "impl")
public void basicTest(String provider, Supplier<String> urlSupplier) throws Exception {
@Cleanup MetadataStoreExtended store = MetadataStoreExtended.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
@Cleanup CoordinationService coordinationService = new CoordinationServiceImpl(store);
MetadataCache<String> cache = store.getMetadataCache(String.class);
BlockingQueue<LeaderElectionState> notifications = new LinkedBlockingDeque<>();
@Cleanup LeaderElection<String> leaderElection = coordinationService.getLeaderElection(String.class, "/my/leader-election", t -> {
notifications.add(t);
});
assertEquals(cache.get("/my/leader-election").join(), Optional.empty());
LeaderElectionState les = leaderElection.elect("test-1").join();
assertEquals(les, LeaderElectionState.Leading);
assertEquals(notifications.poll(3, TimeUnit.SECONDS), LeaderElectionState.Leading);
assertEquals(cache.get("/my/leader-election").join(), Optional.of("test-1"));
leaderElection.close();
assertEquals(cache.get("/my/leader-election").join(), Optional.empty());
}
use of org.apache.pulsar.metadata.api.coordination.LeaderElectionState in project pulsar by apache.
the class LeaderElectionTest method closeAll.
@Test(dataProvider = "impl")
public void closeAll(String provider, Supplier<String> urlSupplier) throws Exception {
@Cleanup MetadataStoreExtended store = MetadataStoreExtended.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
MetadataCache<String> cache = store.getMetadataCache(String.class);
CoordinationService cs = new CoordinationServiceImpl(store);
LeaderElection<String> le1 = cs.getLeaderElection(String.class, "/my/leader-election-1", t -> {
});
LeaderElection<String> le2 = cs.getLeaderElection(String.class, "/my/leader-election-2", t -> {
});
LeaderElectionState les1 = le1.elect("test-1").join();
assertEquals(les1, LeaderElectionState.Leading);
LeaderElectionState les2 = le2.elect("test-2").join();
assertEquals(les2, LeaderElectionState.Leading);
cs.close();
assertEquals(cache.get("/my/leader-election-1").join(), Optional.empty());
assertEquals(cache.get("/my/leader-election-2").join(), Optional.empty());
}
use of org.apache.pulsar.metadata.api.coordination.LeaderElectionState in project pulsar by apache.
the class ZKSessionTest method testReacquireLeadershipAfterSessionLost.
@Test
public void testReacquireLeadershipAfterSessionLost() throws Exception {
// --- init
@Cleanup MetadataStoreExtended store = MetadataStoreExtended.create(zks.getConnectionString(), MetadataStoreConfig.builder().sessionTimeoutMillis(2_000).build());
BlockingQueue<SessionEvent> sessionEvents = new LinkedBlockingQueue<>();
store.registerSessionListener(sessionEvents::add);
BlockingQueue<LeaderElectionState> leaderElectionEvents = new LinkedBlockingQueue<>();
String path = newKey();
@Cleanup CoordinationService coordinationService = new CoordinationServiceImpl(store);
@Cleanup LeaderElection<String> le1 = coordinationService.getLeaderElection(String.class, path, leaderElectionEvents::add);
// --- test manual elect
le1.elect("value-1").join();
assertEquals(le1.getState(), LeaderElectionState.Leading);
LeaderElectionState les = leaderElectionEvents.poll(5, TimeUnit.SECONDS);
assertEquals(les, LeaderElectionState.Leading);
// --- expire session
zks.expireSession(((ZKMetadataStore) store).getZkSessionId());
SessionEvent e = sessionEvents.poll(5, TimeUnit.SECONDS);
assertEquals(e, SessionEvent.ConnectionLost);
e = sessionEvents.poll(10, TimeUnit.SECONDS);
assertEquals(e, SessionEvent.SessionLost);
// --- test le1 can be leader
Awaitility.await().atMost(Duration.ofSeconds(15)).untilAsserted(// reacquire leadership
() -> assertEquals(le1.getState(), LeaderElectionState.Leading));
e = sessionEvents.poll(10, TimeUnit.SECONDS);
assertEquals(e, SessionEvent.Reconnected);
e = sessionEvents.poll(10, TimeUnit.SECONDS);
assertEquals(e, SessionEvent.SessionReestablished);
Awaitility.await().atMost(Duration.ofSeconds(15)).untilAsserted(() -> assertEquals(le1.getState(), LeaderElectionState.Leading));
assertTrue(store.get(path).join().isPresent());
}
Aggregations