use of org.apache.pulsar.metadata.api.extended.SessionEvent in project pulsar by apache.
the class ZKSessionTest method testDisconnection.
@Test
public void testDisconnection() throws Exception {
@Cleanup MetadataStoreExtended store = MetadataStoreExtended.create(zks.getConnectionString(), MetadataStoreConfig.builder().sessionTimeoutMillis(300_000).build());
BlockingQueue<SessionEvent> sessionEvents = new LinkedBlockingQueue<>();
store.registerSessionListener(sessionEvents::add);
zks.stop();
SessionEvent e = sessionEvents.poll(5, TimeUnit.SECONDS);
assertEquals(e, SessionEvent.ConnectionLost);
zks.start();
e = sessionEvents.poll(20, TimeUnit.SECONDS);
assertEquals(e, SessionEvent.Reconnected);
e = sessionEvents.poll(5, TimeUnit.SECONDS);
assertNull(e);
}
use of org.apache.pulsar.metadata.api.extended.SessionEvent in project pulsar by apache.
the class ZKSessionTest method testReacquireLocksAfterSessionLost.
@Test
public void testReacquireLocksAfterSessionLost() throws Exception {
@Cleanup MetadataStoreExtended store = MetadataStoreExtended.create(zks.getConnectionString(), MetadataStoreConfig.builder().sessionTimeoutMillis(2_000).build());
BlockingQueue<SessionEvent> sessionEvents = new LinkedBlockingQueue<>();
store.registerSessionListener(sessionEvents::add);
@Cleanup CoordinationService coordinationService = new CoordinationServiceImpl(store);
@Cleanup LockManager<String> lm1 = coordinationService.getLockManager(String.class);
String path = newKey();
ResourceLock<String> lock = lm1.acquireLock(path, "value-1").join();
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);
e = sessionEvents.poll(10, TimeUnit.SECONDS);
assertEquals(e, SessionEvent.Reconnected);
e = sessionEvents.poll(10, TimeUnit.SECONDS);
assertEquals(e, SessionEvent.SessionReestablished);
Awaitility.await().untilAsserted(() -> assertTrue(store.get(path).join().isPresent()));
assertFalse(lock.getLockExpiredFuture().isDone());
}
use of org.apache.pulsar.metadata.api.extended.SessionEvent 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());
}
use of org.apache.pulsar.metadata.api.extended.SessionEvent in project pulsar by yahoo.
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());
}
use of org.apache.pulsar.metadata.api.extended.SessionEvent in project pulsar by yahoo.
the class ZKSessionTest method testDisconnection.
@Test
public void testDisconnection() throws Exception {
@Cleanup MetadataStoreExtended store = MetadataStoreExtended.create(zks.getConnectionString(), MetadataStoreConfig.builder().sessionTimeoutMillis(300_000).build());
BlockingQueue<SessionEvent> sessionEvents = new LinkedBlockingQueue<>();
store.registerSessionListener(sessionEvents::add);
zks.stop();
SessionEvent e = sessionEvents.poll(5, TimeUnit.SECONDS);
assertEquals(e, SessionEvent.ConnectionLost);
zks.start();
e = sessionEvents.poll(20, TimeUnit.SECONDS);
assertEquals(e, SessionEvent.Reconnected);
e = sessionEvents.poll(5, TimeUnit.SECONDS);
assertNull(e);
}
Aggregations