Search in sources :

Example 26 with PulsarService

use of org.apache.pulsar.broker.PulsarService in project incubator-pulsar by apache.

the class LoadBalancerTest method testLeaderElection.

/*
     * Test all brokers are consistent on current leader and close leader to trigger re-election.
     */
@Test
public void testLeaderElection() throws Exception {
    for (int i = 0; i < BROKER_COUNT - 1; i++) {
        Set<PulsarService> activePulsar = new HashSet<PulsarService>();
        LeaderBroker oldLeader = null;
        PulsarService leaderPulsar = null;
        PulsarService followerPulsar = null;
        for (int j = 0; j < BROKER_COUNT; j++) {
            if (pulsarServices[j].getState() != PulsarService.State.Closed) {
                activePulsar.add(pulsarServices[j]);
                LeaderElectionService les = pulsarServices[j].getLeaderElectionService();
                if (les.isLeader()) {
                    oldLeader = les.getCurrentLeader();
                    leaderPulsar = pulsarServices[j];
                } else {
                    followerPulsar = pulsarServices[j];
                }
            }
        }
        // Make sure both brokers see the same leader
        log.info("Old leader is : {}", oldLeader.getServiceUrl());
        for (PulsarService pulsar : activePulsar) {
            assertEquals(pulsar.getLeaderElectionService().getCurrentLeader(), oldLeader);
        }
        // Do leader election by killing the leader broker
        leaderPulsar.close();
        LeaderBroker newLeader = oldLeader;
        newLeader = loopUntilLeaderChanges(followerPulsar.getLeaderElectionService(), oldLeader, newLeader);
        log.info("New leader is : {}", newLeader.getServiceUrl());
        Assert.assertNotEquals(newLeader, oldLeader);
    }
}
Also used : PulsarService(org.apache.pulsar.broker.PulsarService) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 27 with PulsarService

use of org.apache.pulsar.broker.PulsarService in project incubator-pulsar by apache.

the class ModularLoadManagerImplTest method setup.

@BeforeMethod
void setup() throws Exception {
    // Start local bookkeeper ensemble
    bkEnsemble = new LocalBookkeeperEnsemble(3, ZOOKEEPER_PORT, PortManager.nextFreePort());
    bkEnsemble.start();
    // Start broker 1
    ServiceConfiguration config1 = new ServiceConfiguration();
    config1.setLoadManagerClassName(ModularLoadManagerImpl.class.getName());
    config1.setClusterName("use");
    config1.setWebServicePort(PRIMARY_BROKER_WEBSERVICE_PORT);
    config1.setZookeeperServers("127.0.0.1" + ":" + ZOOKEEPER_PORT);
    config1.setBrokerServicePort(PRIMARY_BROKER_PORT);
    pulsar1 = new PulsarService(config1);
    pulsar1.start();
    primaryHost = String.format("%s:%d", InetAddress.getLocalHost().getHostName(), PRIMARY_BROKER_WEBSERVICE_PORT);
    url1 = new URL("http://127.0.0.1" + ":" + PRIMARY_BROKER_WEBSERVICE_PORT);
    admin1 = new PulsarAdmin(url1, (Authentication) null);
    // Start broker 2
    ServiceConfiguration config2 = new ServiceConfiguration();
    config2.setLoadManagerClassName(ModularLoadManagerImpl.class.getName());
    config2.setClusterName("use");
    config2.setWebServicePort(SECONDARY_BROKER_WEBSERVICE_PORT);
    config2.setZookeeperServers("127.0.0.1" + ":" + ZOOKEEPER_PORT);
    config2.setBrokerServicePort(SECONDARY_BROKER_PORT);
    pulsar2 = new PulsarService(config2);
    secondaryHost = String.format("%s:%d", InetAddress.getLocalHost().getHostName(), SECONDARY_BROKER_WEBSERVICE_PORT);
    pulsar2.start();
    url2 = new URL("http://127.0.0.1" + ":" + SECONDARY_BROKER_WEBSERVICE_PORT);
    admin2 = new PulsarAdmin(url2, (Authentication) null);
    primaryLoadManager = (ModularLoadManagerImpl) getField(pulsar1.getLoadManager().get(), "loadManager");
    secondaryLoadManager = (ModularLoadManagerImpl) getField(pulsar2.getLoadManager().get(), "loadManager");
    nsFactory = new NamespaceBundleFactory(pulsar1, Hashing.crc32());
    Thread.sleep(100);
}
Also used : ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) PulsarService(org.apache.pulsar.broker.PulsarService) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) Authentication(org.apache.pulsar.client.api.Authentication) NamespaceBundleFactory(org.apache.pulsar.common.naming.NamespaceBundleFactory) LocalBookkeeperEnsemble(org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble) ModularLoadManagerImpl(org.apache.pulsar.broker.loadbalance.impl.ModularLoadManagerImpl) URL(java.net.URL) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 28 with PulsarService

use of org.apache.pulsar.broker.PulsarService in project incubator-pulsar by apache.

the class ModularLoadManagerImplTest method testOwnBrokerZnodeByMultipleBroker.

/**
 * It verifies that pulsar-service fails if load-manager tries to create ephemeral znode for broker which is already
 * created by other zk-session-id.
 *
 * @throws Exception
 */
@Test
public void testOwnBrokerZnodeByMultipleBroker() throws Exception {
    ServiceConfiguration config = new ServiceConfiguration();
    config.setLoadManagerClassName(ModularLoadManagerImpl.class.getName());
    config.setClusterName("use");
    int brokerWebServicePort = PortManager.nextFreePort();
    int brokerServicePort = PortManager.nextFreePort();
    config.setWebServicePort(brokerWebServicePort);
    config.setZookeeperServers("127.0.0.1" + ":" + ZOOKEEPER_PORT);
    config.setBrokerServicePort(brokerServicePort);
    PulsarService pulsar = new PulsarService(config);
    // create znode using different zk-session
    final String brokerZnode = LoadManager.LOADBALANCE_BROKERS_ROOT + "/" + pulsar.getAdvertisedAddress() + ":" + brokerWebServicePort;
    ZkUtils.createFullPathOptimistic(pulsar1.getZkClient(), brokerZnode, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    try {
        pulsar.start();
    } catch (PulsarServerException e) {
    // Ok.
    }
    pulsar.close();
}
Also used : PulsarServerException(org.apache.pulsar.broker.PulsarServerException) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) PulsarService(org.apache.pulsar.broker.PulsarService) ModularLoadManagerImpl(org.apache.pulsar.broker.loadbalance.impl.ModularLoadManagerImpl) Test(org.testng.annotations.Test)

Example 29 with PulsarService

use of org.apache.pulsar.broker.PulsarService in project incubator-pulsar by apache.

the class SimpleLoadManagerImplTest method setup.

@BeforeMethod
void setup() throws Exception {
    // Start local bookkeeper ensemble
    bkEnsemble = new LocalBookkeeperEnsemble(3, ZOOKEEPER_PORT, PortManager.nextFreePort());
    bkEnsemble.start();
    // Start broker 1
    ServiceConfiguration config1 = spy(new ServiceConfiguration());
    config1.setClusterName("use");
    config1.setWebServicePort(PRIMARY_BROKER_WEBSERVICE_PORT);
    config1.setZookeeperServers("127.0.0.1" + ":" + ZOOKEEPER_PORT);
    config1.setBrokerServicePort(PRIMARY_BROKER_PORT);
    config1.setLoadManagerClassName(SimpleLoadManagerImpl.class.getName());
    pulsar1 = new PulsarService(config1);
    pulsar1.start();
    url1 = new URL("http://127.0.0.1" + ":" + PRIMARY_BROKER_WEBSERVICE_PORT);
    admin1 = new PulsarAdmin(url1, (Authentication) null);
    brokerStatsClient1 = admin1.brokerStats();
    primaryHost = String.format("http://%s:%d", InetAddress.getLocalHost().getHostName(), PRIMARY_BROKER_WEBSERVICE_PORT);
    // Start broker 2
    ServiceConfiguration config2 = new ServiceConfiguration();
    config2.setClusterName("use");
    config2.setWebServicePort(SECONDARY_BROKER_WEBSERVICE_PORT);
    config2.setZookeeperServers("127.0.0.1" + ":" + ZOOKEEPER_PORT);
    config2.setBrokerServicePort(SECONDARY_BROKER_PORT);
    config2.setLoadManagerClassName(SimpleLoadManagerImpl.class.getName());
    pulsar2 = new PulsarService(config2);
    pulsar2.start();
    url2 = new URL("http://127.0.0.1" + ":" + SECONDARY_BROKER_WEBSERVICE_PORT);
    admin2 = new PulsarAdmin(url2, (Authentication) null);
    brokerStatsClient2 = admin2.brokerStats();
    secondaryHost = String.format("http://%s:%d", InetAddress.getLocalHost().getHostName(), SECONDARY_BROKER_WEBSERVICE_PORT);
    Thread.sleep(100);
}
Also used : ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) SimpleLoadManagerImpl(org.apache.pulsar.broker.loadbalance.impl.SimpleLoadManagerImpl) PulsarService(org.apache.pulsar.broker.PulsarService) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) Authentication(org.apache.pulsar.client.api.Authentication) LocalBookkeeperEnsemble(org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble) URL(java.net.URL) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 30 with PulsarService

use of org.apache.pulsar.broker.PulsarService in project incubator-pulsar by apache.

the class SLAMonitoringTest method testUnloadIfBrokerCrashes.

@Test
public void testUnloadIfBrokerCrashes() {
    int crashIndex = BROKER_COUNT / 2;
    log.info("Trying to close the broker at index = {}", crashIndex);
    try {
        pulsarServices[crashIndex].close();
    } catch (PulsarServerException e) {
        e.printStackTrace();
        fail("Should be a able to close the broker index " + crashIndex + " Exception: " + e);
    }
    String topic = String.format("persistent://%s/%s/%s:%s/%s", NamespaceService.SLA_NAMESPACE_PROPERTY, "my-cluster", pulsarServices[crashIndex].getAdvertisedAddress(), brokerWebServicePorts[crashIndex], "my-topic");
    log.info("Lookup for namespace {}", topic);
    String broker = null;
    try {
        broker = pulsarAdmins[BROKER_COUNT - 1].lookups().lookupTopic(topic);
        log.info("{} Namespace is owned by {}", topic, broker);
        assertNotEquals(broker, "pulsar://" + pulsarServices[crashIndex].getAdvertisedAddress() + ":" + brokerNativeBrokerPorts[crashIndex]);
    } catch (PulsarAdminException e) {
        e.printStackTrace();
        fail("The SLA Monitor namespace should be owned by some other broker");
    }
    // Check if the namespace is properly unloaded and reowned by the broker
    try {
        pulsarServices[crashIndex] = new PulsarService(configurations[crashIndex]);
        pulsarServices[crashIndex].start();
        assertEquals(pulsarServices[crashIndex].getConfiguration().getBrokerServicePort(), brokerNativeBrokerPorts[crashIndex]);
    } catch (PulsarServerException e) {
        e.printStackTrace();
        fail("The broker should be able to start without exception");
    }
    try {
        broker = pulsarAdmins[0].lookups().lookupTopic(topic);
        log.info("{} Namespace is re-owned by {}", topic, broker);
        assertEquals(broker, "pulsar://" + pulsarServices[crashIndex].getAdvertisedAddress() + ":" + brokerNativeBrokerPorts[crashIndex]);
    } catch (PulsarAdminException e) {
        e.printStackTrace();
        fail("The SLA Monitor namespace should be reowned by the broker" + broker);
    }
    try {
        pulsarServices[crashIndex].close();
    } catch (PulsarServerException e) {
        e.printStackTrace();
        fail("The broker should be able to stop without exception");
    }
}
Also used : PulsarServerException(org.apache.pulsar.broker.PulsarServerException) PulsarService(org.apache.pulsar.broker.PulsarService) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) LoadBalancerTest(org.apache.pulsar.broker.loadbalance.LoadBalancerTest) Test(org.testng.annotations.Test)

Aggregations

PulsarService (org.apache.pulsar.broker.PulsarService)33 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)22 URL (java.net.URL)14 BeforeMethod (org.testng.annotations.BeforeMethod)13 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)12 Test (org.testng.annotations.Test)12 ClusterData (org.apache.pulsar.common.policies.data.ClusterData)10 LocalBookkeeperEnsemble (org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble)10 Authentication (org.apache.pulsar.client.api.Authentication)9 TopicName (org.apache.pulsar.common.naming.TopicName)8 PropertyAdmin (org.apache.pulsar.common.policies.data.PropertyAdmin)8 URI (java.net.URI)7 NamespaceService (org.apache.pulsar.broker.namespace.NamespaceService)7 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)7 Field (java.lang.reflect.Field)6 LoadManager (org.apache.pulsar.broker.loadbalance.LoadManager)6 ModularLoadManagerImpl (org.apache.pulsar.broker.loadbalance.impl.ModularLoadManagerImpl)6 SimpleResourceUnit (org.apache.pulsar.broker.loadbalance.impl.SimpleResourceUnit)5 ServiceUnitId (org.apache.pulsar.common.naming.ServiceUnitId)5 Optional (java.util.Optional)4