Search in sources :

Example 1 with PulsarService

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

the class PulsarStandaloneStarter method start.

void start() throws Exception {
    if (config == null) {
        System.exit(1);
    }
    log.debug("--- setup PulsarStandaloneStarter ---");
    if (!onlyBroker) {
        // Start LocalBookKeeper
        bkEnsemble = new LocalBookkeeperEnsemble(numOfBk, zkPort, bkPort, zkDir, bkDir, wipeData, config.getAdvertisedAddress());
        bkEnsemble.startStandalone();
    }
    if (noBroker) {
        return;
    }
    // load aspectj-weaver agent for instrumentation
    AgentLoader.loadAgentClass(Agent.class.getName(), null);
    // initialize the functions worker
    if (!noFunctionsWorker) {
        WorkerConfig workerConfig;
        if (isBlank(fnWorkerConfigFile)) {
            workerConfig = new WorkerConfig();
        } else {
            workerConfig = WorkerConfig.load(fnWorkerConfigFile);
        }
        // worker talks to local broker
        workerConfig.setPulsarServiceUrl("pulsar://127.0.0.1:" + config.getBrokerServicePort());
        workerConfig.setPulsarWebServiceUrl("http://127.0.0.1:" + config.getWebServicePort());
        String hostname = ServiceConfigurationUtils.getDefaultOrConfiguredAddress(config.getAdvertisedAddress());
        workerConfig.setWorkerHostname(hostname);
        workerConfig.setWorkerId("c-" + config.getClusterName() + "-fw-" + hostname + "-" + workerConfig.getWorkerPort());
        fnWorkerService = new WorkerService(workerConfig);
    }
    // Start Broker
    broker = new PulsarService(config, Optional.ofNullable(fnWorkerService));
    broker.start();
    // Create a sample namespace
    URL webServiceUrl = new URL(String.format("http://%s:%d", config.getAdvertisedAddress(), config.getWebServicePort()));
    final String brokerServiceUrl = String.format("pulsar://%s:%d", config.getAdvertisedAddress(), config.getBrokerServicePort());
    admin = new PulsarAdmin(webServiceUrl, config.getBrokerClientAuthenticationPlugin(), config.getBrokerClientAuthenticationParameters());
    final String property = "sample";
    final String cluster = config.getClusterName();
    final String globalCluster = "global";
    final String namespace = property + "/" + cluster + "/ns1";
    try {
        ClusterData clusterData = new ClusterData(webServiceUrl.toString(), null, /* serviceUrlTls */
        brokerServiceUrl, null);
        if (!admin.clusters().getClusters().contains(cluster)) {
            admin.clusters().createCluster(cluster, clusterData);
        } else {
            admin.clusters().updateCluster(cluster, clusterData);
        }
        // Create marker for "global" cluster
        if (!admin.clusters().getClusters().contains(globalCluster)) {
            admin.clusters().createCluster(globalCluster, new ClusterData(null, null));
        }
        if (!admin.properties().getProperties().contains(property)) {
            admin.properties().createProperty(property, new PropertyAdmin(Lists.newArrayList(config.getSuperUserRoles()), Sets.newHashSet(cluster)));
        }
        if (!admin.namespaces().getNamespaces(property).contains(namespace)) {
            admin.namespaces().createNamespace(namespace);
        }
    } catch (PulsarAdminException e) {
        log.info(e.getMessage());
    }
    if (null != fnWorkerService) {
        fnWorkerService.start();
    }
    log.debug("--- setup completed ---");
}
Also used : Agent(org.aspectj.weaver.loadtime.Agent) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PulsarService(org.apache.pulsar.broker.PulsarService) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) WorkerConfig(org.apache.pulsar.functions.worker.WorkerConfig) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) LocalBookkeeperEnsemble(org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble) WorkerService(org.apache.pulsar.functions.worker.WorkerService) URL(java.net.URL)

Example 2 with PulsarService

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

the class NamespaceBundlesTest method getNamespaceBundleFactory.

@SuppressWarnings("unchecked")
private NamespaceBundleFactory getNamespaceBundleFactory() {
    PulsarService pulsar = mock(PulsarService.class);
    LocalZooKeeperCacheService localZkCache = mock(LocalZooKeeperCacheService.class);
    ZooKeeperDataCache<LocalPolicies> poilciesCache = mock(ZooKeeperDataCache.class);
    when(pulsar.getLocalZkCacheService()).thenReturn(localZkCache);
    when(localZkCache.policiesCache()).thenReturn(poilciesCache);
    doNothing().when(poilciesCache).registerListener(any());
    return NamespaceBundleFactory.createFactory(pulsar, Hashing.crc32());
}
Also used : PulsarService(org.apache.pulsar.broker.PulsarService) LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies) LocalZooKeeperCacheService(org.apache.pulsar.broker.cache.LocalZooKeeperCacheService)

Example 3 with PulsarService

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

the class ReplicatorTestBase method setup.

void setup() throws Exception {
    log.info("--- Starting ReplicatorTestBase::setup ---");
    int globalZKPort = PortManager.nextFreePort();
    globalZkS = new ZookeeperServerTest(globalZKPort);
    globalZkS.start();
    // Start region 1
    int zkPort1 = PortManager.nextFreePort();
    bkEnsemble1 = new LocalBookkeeperEnsemble(3, zkPort1, PortManager.nextFreePort());
    bkEnsemble1.start();
    int webServicePort1 = PortManager.nextFreePort();
    int webServicePortTls1 = PortManager.nextFreePort();
    // NOTE: we have to instantiate a new copy of System.getProperties() to make sure pulsar1 and pulsar2 have
    // completely
    // independent config objects instead of referring to the same properties object
    config1.setClusterName("r1");
    config1.setAdvertisedAddress("localhost");
    config1.setWebServicePort(webServicePort1);
    config1.setWebServicePortTls(webServicePortTls1);
    config1.setZookeeperServers("127.0.0.1:" + zkPort1);
    config1.setGlobalZookeeperServers("127.0.0.1:" + globalZKPort + "/foo");
    config1.setBrokerDeleteInactiveTopicsEnabled(isBrokerServicePurgeInactiveTopic());
    config1.setBrokerServicePurgeInactiveFrequencyInSeconds(inSec(getBrokerServicePurgeInactiveFrequency(), TimeUnit.SECONDS));
    config1.setBrokerServicePort(PortManager.nextFreePort());
    config1.setBrokerServicePortTls(PortManager.nextFreePort());
    config1.setTlsEnabled(true);
    config1.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    config1.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    config1.setTlsTrustCertsFilePath(TLS_SERVER_CERT_FILE_PATH);
    config1.setBacklogQuotaCheckIntervalInSeconds(TIME_TO_CHECK_BACKLOG_QUOTA);
    config1.setDefaultNumberOfNamespaceBundles(1);
    pulsar1 = new PulsarService(config1);
    pulsar1.start();
    ns1 = pulsar1.getBrokerService();
    url1 = new URL("http://localhost:" + webServicePort1);
    urlTls1 = new URL("https://localhost:" + webServicePortTls1);
    admin1 = new PulsarAdmin(url1, (Authentication) null);
    // Start region 2
    // Start zk & bks
    int zkPort2 = PortManager.nextFreePort();
    bkEnsemble2 = new LocalBookkeeperEnsemble(3, zkPort2, PortManager.nextFreePort());
    bkEnsemble2.start();
    int webServicePort2 = PortManager.nextFreePort();
    int webServicePortTls2 = PortManager.nextFreePort();
    config2.setClusterName("r2");
    config2.setAdvertisedAddress("localhost");
    config2.setWebServicePort(webServicePort2);
    config2.setWebServicePortTls(webServicePortTls2);
    config2.setZookeeperServers("127.0.0.1:" + zkPort2);
    config2.setGlobalZookeeperServers("127.0.0.1:" + globalZKPort + "/foo");
    config2.setBrokerDeleteInactiveTopicsEnabled(isBrokerServicePurgeInactiveTopic());
    config2.setBrokerServicePurgeInactiveFrequencyInSeconds(inSec(getBrokerServicePurgeInactiveFrequency(), TimeUnit.SECONDS));
    config2.setBrokerServicePort(PortManager.nextFreePort());
    config2.setBrokerServicePortTls(PortManager.nextFreePort());
    config2.setTlsEnabled(true);
    config2.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    config2.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    config2.setTlsTrustCertsFilePath(TLS_SERVER_CERT_FILE_PATH);
    config2.setBacklogQuotaCheckIntervalInSeconds(TIME_TO_CHECK_BACKLOG_QUOTA);
    config2.setDefaultNumberOfNamespaceBundles(1);
    pulsar2 = new PulsarService(config2);
    pulsar2.start();
    ns2 = pulsar2.getBrokerService();
    url2 = new URL("http://localhost:" + webServicePort2);
    urlTls2 = new URL("https://localhost:" + webServicePortTls2);
    admin2 = new PulsarAdmin(url2, (Authentication) null);
    // Start region 3
    // Start zk & bks
    int zkPort3 = PortManager.nextFreePort();
    bkEnsemble3 = new LocalBookkeeperEnsemble(3, zkPort3, PortManager.nextFreePort());
    bkEnsemble3.start();
    int webServicePort3 = PortManager.nextFreePort();
    int webServicePortTls3 = PortManager.nextFreePort();
    config3.setClusterName("r3");
    config3.setAdvertisedAddress("localhost");
    config3.setWebServicePort(webServicePort3);
    config3.setWebServicePortTls(webServicePortTls3);
    config3.setZookeeperServers("127.0.0.1:" + zkPort3);
    config3.setGlobalZookeeperServers("127.0.0.1:" + globalZKPort + "/foo");
    config3.setBrokerDeleteInactiveTopicsEnabled(isBrokerServicePurgeInactiveTopic());
    config3.setBrokerServicePurgeInactiveFrequencyInSeconds(inSec(getBrokerServicePurgeInactiveFrequency(), TimeUnit.SECONDS));
    config3.setBrokerServicePort(PortManager.nextFreePort());
    config3.setBrokerServicePortTls(PortManager.nextFreePort());
    config3.setTlsEnabled(true);
    config3.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    config3.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    config3.setTlsTrustCertsFilePath(TLS_SERVER_CERT_FILE_PATH);
    config3.setDefaultNumberOfNamespaceBundles(1);
    pulsar3 = new PulsarService(config3);
    pulsar3.start();
    ns3 = pulsar3.getBrokerService();
    url3 = new URL("http://localhost:" + webServicePort3);
    urlTls3 = new URL("https://localhost:" + webServicePortTls3);
    admin3 = new PulsarAdmin(url3, (Authentication) null);
    // Provision the global namespace
    admin1.clusters().createCluster("r1", new ClusterData(url1.toString(), urlTls1.toString(), pulsar1.getBrokerServiceUrl(), pulsar1.getBrokerServiceUrlTls()));
    admin1.clusters().createCluster("r2", new ClusterData(url2.toString(), urlTls2.toString(), pulsar2.getBrokerServiceUrl(), pulsar2.getBrokerServiceUrlTls()));
    admin1.clusters().createCluster("r3", new ClusterData(url3.toString(), urlTls3.toString(), pulsar3.getBrokerServiceUrl(), pulsar3.getBrokerServiceUrlTls()));
    admin1.clusters().createCluster("global", new ClusterData("http://global:8080", "https://global:8443"));
    admin1.properties().createProperty("pulsar", new PropertyAdmin(Lists.newArrayList("appid1", "appid2", "appid3"), Sets.newHashSet("r1", "r2", "r3")));
    admin1.namespaces().createNamespace("pulsar/global/ns");
    admin1.namespaces().setNamespaceReplicationClusters("pulsar/global/ns", Lists.newArrayList("r1", "r2", "r3"));
    admin1.namespaces().createNamespace("pulsar/global/ns1");
    admin1.namespaces().setNamespaceReplicationClusters("pulsar/global/ns1", Lists.newArrayList("r1", "r2"));
    assertEquals(admin2.clusters().getCluster("r1").getServiceUrl(), url1.toString());
    assertEquals(admin2.clusters().getCluster("r2").getServiceUrl(), url2.toString());
    assertEquals(admin2.clusters().getCluster("r3").getServiceUrl(), url3.toString());
    assertEquals(admin2.clusters().getCluster("r1").getBrokerServiceUrl(), pulsar1.getBrokerServiceUrl());
    assertEquals(admin2.clusters().getCluster("r2").getBrokerServiceUrl(), pulsar2.getBrokerServiceUrl());
    assertEquals(admin2.clusters().getCluster("r3").getBrokerServiceUrl(), pulsar3.getBrokerServiceUrl());
    Thread.sleep(100);
    log.info("--- ReplicatorTestBase::setup completed ---");
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PulsarService(org.apache.pulsar.broker.PulsarService) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) ZookeeperServerTest(org.apache.pulsar.zookeeper.ZookeeperServerTest) Authentication(org.apache.pulsar.client.api.Authentication) LocalBookkeeperEnsemble(org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble) URL(java.net.URL)

Example 4 with PulsarService

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

the class PersistentDispatcherFailoverConsumerTest method setup.

@BeforeMethod
public void setup() throws Exception {
    ServiceConfiguration svcConfig = spy(new ServiceConfiguration());
    PulsarService pulsar = spy(new PulsarService(svcConfig));
    doReturn(svcConfig).when(pulsar).getConfiguration();
    mlFactoryMock = mock(ManagedLedgerFactory.class);
    doReturn(mlFactoryMock).when(pulsar).getManagedLedgerFactory();
    ZooKeeper mockZk = createMockZooKeeper();
    doReturn(mockZk).when(pulsar).getZkClient();
    doReturn(createMockBookKeeper(mockZk)).when(pulsar).getBookKeeperClient();
    configCacheService = mock(ConfigurationCacheService.class);
    @SuppressWarnings("unchecked") ZooKeeperDataCache<Policies> zkDataCache = mock(ZooKeeperDataCache.class);
    LocalZooKeeperCacheService zkCache = mock(LocalZooKeeperCacheService.class);
    doReturn(CompletableFuture.completedFuture(Optional.empty())).when(zkDataCache).getAsync(any());
    doReturn(zkDataCache).when(zkCache).policiesCache();
    doReturn(zkDataCache).when(configCacheService).policiesCache();
    doReturn(configCacheService).when(pulsar).getConfigurationCache();
    doReturn(zkCache).when(pulsar).getLocalZkCacheService();
    brokerService = spy(new BrokerService(pulsar));
    doReturn(brokerService).when(pulsar).getBrokerService();
    consumerChanges = new LinkedBlockingQueue<>();
    this.channelCtx = mock(ChannelHandlerContext.class);
    doAnswer(invocationOnMock -> {
        ByteBuf buf = invocationOnMock.getArgumentAt(0, ByteBuf.class);
        ByteBuf cmdBuf = buf.retainedSlice(4, buf.writerIndex() - 4);
        try {
            int cmdSize = (int) cmdBuf.readUnsignedInt();
            int writerIndex = cmdBuf.writerIndex();
            cmdBuf.writerIndex(cmdBuf.readerIndex() + cmdSize);
            ByteBufCodedInputStream cmdInputStream = ByteBufCodedInputStream.get(cmdBuf);
            BaseCommand.Builder cmdBuilder = BaseCommand.newBuilder();
            BaseCommand cmd = cmdBuilder.mergeFrom(cmdInputStream, null).build();
            cmdBuilder.recycle();
            cmdBuf.writerIndex(writerIndex);
            cmdInputStream.recycle();
            if (cmd.hasActiveConsumerChange()) {
                consumerChanges.put(cmd.getActiveConsumerChange());
            }
            cmd.recycle();
        } finally {
            cmdBuf.release();
        }
        return null;
    }).when(channelCtx).writeAndFlush(any(), any());
    serverCnx = spy(new ServerCnx(brokerService));
    doReturn(true).when(serverCnx).isActive();
    doReturn(true).when(serverCnx).isWritable();
    doReturn(new InetSocketAddress("localhost", 1234)).when(serverCnx).clientAddress();
    when(serverCnx.getRemoteEndpointProtocolVersion()).thenReturn(ProtocolVersion.v12.getNumber());
    when(serverCnx.ctx()).thenReturn(channelCtx);
    serverCnxWithOldVersion = spy(new ServerCnx(brokerService));
    doReturn(true).when(serverCnxWithOldVersion).isActive();
    doReturn(true).when(serverCnxWithOldVersion).isWritable();
    doReturn(new InetSocketAddress("localhost", 1234)).when(serverCnxWithOldVersion).clientAddress();
    when(serverCnxWithOldVersion.getRemoteEndpointProtocolVersion()).thenReturn(ProtocolVersion.v11.getNumber());
    when(serverCnxWithOldVersion.ctx()).thenReturn(channelCtx);
    NamespaceService nsSvc = mock(NamespaceService.class);
    doReturn(nsSvc).when(pulsar).getNamespaceService();
    doReturn(true).when(nsSvc).isServiceUnitOwned(any(NamespaceBundle.class));
    doReturn(true).when(nsSvc).isServiceUnitActive(any(TopicName.class));
    setupMLAsyncCallbackMocks();
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) Policies(org.apache.pulsar.common.policies.data.Policies) BaseCommand(org.apache.pulsar.common.api.proto.PulsarApi.BaseCommand) InetSocketAddress(java.net.InetSocketAddress) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) TopicName(org.apache.pulsar.common.naming.TopicName) ZooKeeper(org.apache.zookeeper.ZooKeeper) MockedPulsarServiceBaseTest.createMockZooKeeper(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest.createMockZooKeeper) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) PulsarService(org.apache.pulsar.broker.PulsarService) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ConfigurationCacheService(org.apache.pulsar.broker.cache.ConfigurationCacheService) ByteBufCodedInputStream(org.apache.pulsar.common.util.protobuf.ByteBufCodedInputStream) LocalZooKeeperCacheService(org.apache.pulsar.broker.cache.LocalZooKeeperCacheService) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 5 with PulsarService

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

the class PersistentTopicTest method setup.

@BeforeMethod
public void setup() throws Exception {
    ServiceConfiguration svcConfig = spy(new ServiceConfiguration());
    pulsar = spy(new PulsarService(svcConfig));
    doReturn(svcConfig).when(pulsar).getConfiguration();
    mlFactoryMock = mock(ManagedLedgerFactory.class);
    doReturn(mlFactoryMock).when(pulsar).getManagedLedgerFactory();
    ZooKeeper mockZk = createMockZooKeeper();
    doReturn(mockZk).when(pulsar).getZkClient();
    doReturn(createMockBookKeeper(mockZk)).when(pulsar).getBookKeeperClient();
    configCacheService = mock(ConfigurationCacheService.class);
    @SuppressWarnings("unchecked") ZooKeeperDataCache<Policies> zkDataCache = mock(ZooKeeperDataCache.class);
    doReturn(zkDataCache).when(configCacheService).policiesCache();
    doReturn(configCacheService).when(pulsar).getConfigurationCache();
    doReturn(Optional.empty()).when(zkDataCache).get(anyString());
    LocalZooKeeperCacheService zkCache = mock(LocalZooKeeperCacheService.class);
    doReturn(CompletableFuture.completedFuture(Optional.empty())).when(zkDataCache).getAsync(any());
    doReturn(zkDataCache).when(zkCache).policiesCache();
    doReturn(configCacheService).when(pulsar).getConfigurationCache();
    doReturn(zkCache).when(pulsar).getLocalZkCacheService();
    brokerService = spy(new BrokerService(pulsar));
    doReturn(brokerService).when(pulsar).getBrokerService();
    serverCnx = spy(new ServerCnx(brokerService));
    doReturn(true).when(serverCnx).isActive();
    doReturn(true).when(serverCnx).isWritable();
    doReturn(new InetSocketAddress("localhost", 1234)).when(serverCnx).clientAddress();
    NamespaceService nsSvc = mock(NamespaceService.class);
    doReturn(nsSvc).when(pulsar).getNamespaceService();
    doReturn(true).when(nsSvc).isServiceUnitOwned(any(NamespaceBundle.class));
    doReturn(true).when(nsSvc).isServiceUnitActive(any(TopicName.class));
    setupMLAsyncCallbackMocks();
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) Policies(org.apache.pulsar.common.policies.data.Policies) InetSocketAddress(java.net.InetSocketAddress) TopicName(org.apache.pulsar.common.naming.TopicName) ZooKeeper(org.apache.zookeeper.ZooKeeper) MockedPulsarServiceBaseTest.createMockZooKeeper(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest.createMockZooKeeper) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) PulsarService(org.apache.pulsar.broker.PulsarService) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ConfigurationCacheService(org.apache.pulsar.broker.cache.ConfigurationCacheService) LocalZooKeeperCacheService(org.apache.pulsar.broker.cache.LocalZooKeeperCacheService) BeforeMethod(org.testng.annotations.BeforeMethod)

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