Search in sources :

Example 1 with LeaderElectionState

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

Example 2 with LeaderElectionState

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

Example 3 with LeaderElectionState

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

Example 4 with LeaderElectionState

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

Example 5 with LeaderElectionState

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());
}
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)

Aggregations

LeaderElectionState (org.apache.pulsar.metadata.api.coordination.LeaderElectionState)27 Cleanup (lombok.Cleanup)24 CoordinationService (org.apache.pulsar.metadata.api.coordination.CoordinationService)24 MetadataStoreExtended (org.apache.pulsar.metadata.api.extended.MetadataStoreExtended)24 CoordinationServiceImpl (org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl)24 Test (org.testng.annotations.Test)24 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)9 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 SessionEvent (org.apache.pulsar.metadata.api.extended.SessionEvent)3