Search in sources :

Example 1 with SessionEvent

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);
}
Also used : SessionEvent(org.apache.pulsar.metadata.api.extended.SessionEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Cleanup(lombok.Cleanup) MetadataStoreExtended(org.apache.pulsar.metadata.api.extended.MetadataStoreExtended) Test(org.testng.annotations.Test)

Example 2 with SessionEvent

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());
}
Also used : SessionEvent(org.apache.pulsar.metadata.api.extended.SessionEvent) CoordinationService(org.apache.pulsar.metadata.api.coordination.CoordinationService) CoordinationServiceImpl(org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Cleanup(lombok.Cleanup) MetadataStoreExtended(org.apache.pulsar.metadata.api.extended.MetadataStoreExtended) Test(org.testng.annotations.Test)

Example 3 with SessionEvent

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());
}
Also used : CoordinationServiceImpl(org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Cleanup(lombok.Cleanup) LeaderElectionState(org.apache.pulsar.metadata.api.coordination.LeaderElectionState) SessionEvent(org.apache.pulsar.metadata.api.extended.SessionEvent) CoordinationService(org.apache.pulsar.metadata.api.coordination.CoordinationService) MetadataStoreExtended(org.apache.pulsar.metadata.api.extended.MetadataStoreExtended) Test(org.testng.annotations.Test)

Example 4 with SessionEvent

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());
}
Also used : CoordinationServiceImpl(org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Cleanup(lombok.Cleanup) LeaderElectionState(org.apache.pulsar.metadata.api.coordination.LeaderElectionState) SessionEvent(org.apache.pulsar.metadata.api.extended.SessionEvent) CoordinationService(org.apache.pulsar.metadata.api.coordination.CoordinationService) MetadataStoreExtended(org.apache.pulsar.metadata.api.extended.MetadataStoreExtended) Test(org.testng.annotations.Test)

Example 5 with SessionEvent

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);
}
Also used : SessionEvent(org.apache.pulsar.metadata.api.extended.SessionEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Cleanup(lombok.Cleanup) MetadataStoreExtended(org.apache.pulsar.metadata.api.extended.MetadataStoreExtended) Test(org.testng.annotations.Test)

Aggregations

SessionEvent (org.apache.pulsar.metadata.api.extended.SessionEvent)15 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)12 Cleanup (lombok.Cleanup)12 MetadataStoreExtended (org.apache.pulsar.metadata.api.extended.MetadataStoreExtended)12 Test (org.testng.annotations.Test)12 CoordinationService (org.apache.pulsar.metadata.api.coordination.CoordinationService)6 CoordinationServiceImpl (org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl)6 CompletableFuture (java.util.concurrent.CompletableFuture)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 LeaderElectionState (org.apache.pulsar.metadata.api.coordination.LeaderElectionState)3