Search in sources :

Example 1 with CoordinationServiceImpl

use of org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl in project pulsar by apache.

the class DistributedIdGeneratorTest method setup.

@BeforeMethod(alwaysRun = true)
public void setup() throws Exception {
    store = MetadataStoreExtended.create("memory:local", MetadataStoreConfig.builder().build());
    coordinationService = new CoordinationServiceImpl(store);
}
Also used : CoordinationServiceImpl(org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with CoordinationServiceImpl

use of org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl in project pulsar by apache.

the class OwnershipCacheTest method setup.

@BeforeMethod
public void setup() throws Exception {
    final int port = 8080;
    selfBrokerUrl = "tcp://localhost:" + port;
    pulsar = mock(PulsarService.class);
    config = mock(ServiceConfiguration.class);
    executor = OrderedScheduler.newSchedulerBuilder().numThreads(1).name("test").build();
    zookeeperServer = new ZookeeperServerTest(0);
    zookeeperServer.start();
    store = MetadataStoreExtended.create(zookeeperServer.getHostPort(), MetadataStoreConfig.builder().sessionTimeoutMillis(5000).build());
    coordinationService = new CoordinationServiceImpl(store);
    otherStore = MetadataStoreExtended.create(zookeeperServer.getHostPort(), MetadataStoreConfig.builder().sessionTimeoutMillis(5000).build());
    when(pulsar.getConfigurationMetadataStore()).thenReturn(store);
    when(pulsar.getLocalMetadataStore()).thenReturn(store);
    when(pulsar.getConfigurationMetadataStore()).thenReturn(store);
    when(pulsar.getCoordinationService()).thenReturn(coordinationService);
    bundleFactory = new NamespaceBundleFactory(pulsar, Hashing.crc32());
    nsService = mock(NamespaceService.class);
    brokerService = mock(BrokerService.class);
    doReturn(CompletableFuture.completedFuture(1)).when(brokerService).unloadServiceUnit(any(), anyBoolean(), anyLong(), any());
    doReturn(config).when(pulsar).getConfiguration();
    doReturn(nsService).when(pulsar).getNamespaceService();
    doReturn(Optional.of(port)).when(config).getBrokerServicePort();
    doReturn(Optional.empty()).when(config).getWebServicePort();
    doReturn(brokerService).when(pulsar).getBrokerService();
    doReturn(selfBrokerUrl).when(pulsar).getBrokerServiceUrl();
}
Also used : PulsarService(org.apache.pulsar.broker.PulsarService) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) ZookeeperServerTest(org.apache.pulsar.zookeeper.ZookeeperServerTest) CoordinationServiceImpl(org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl) NamespaceBundleFactory(org.apache.pulsar.common.naming.NamespaceBundleFactory) BrokerService(org.apache.pulsar.broker.service.BrokerService) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with CoordinationServiceImpl

use of org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl 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 4 with CoordinationServiceImpl

use of org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl 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 5 with CoordinationServiceImpl

use of org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl 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)

Aggregations

CoordinationServiceImpl (org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl)63 Cleanup (lombok.Cleanup)54 CoordinationService (org.apache.pulsar.metadata.api.coordination.CoordinationService)54 MetadataStoreExtended (org.apache.pulsar.metadata.api.extended.MetadataStoreExtended)54 Test (org.testng.annotations.Test)54 LeaderElectionState (org.apache.pulsar.metadata.api.coordination.LeaderElectionState)24 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)9 CompletionException (java.util.concurrent.CompletionException)6 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)6 SessionEvent (org.apache.pulsar.metadata.api.extended.SessionEvent)6 BeforeMethod (org.testng.annotations.BeforeMethod)6 ImmutableMap (com.google.common.collect.ImmutableMap)3 SocketChannel (io.netty.channel.socket.SocketChannel)3 HashedWheelTimer (io.netty.util.HashedWheelTimer)3 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)3 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 Map (java.util.Map)3 CancellationException (java.util.concurrent.CancellationException)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3