Search in sources :

Example 1 with LocalZooKeeperConnectionService

use of org.apache.pulsar.zookeeper.LocalZooKeeperConnectionService in project incubator-pulsar by apache.

the class ProxyService method start.

public void start() throws Exception {
    ServiceConfiguration serviceConfiguration = PulsarConfigurationLoader.convertFrom(proxyConfig);
    authenticationService = new AuthenticationService(serviceConfiguration);
    if (!isBlank(proxyConfig.getZookeeperServers()) && !isBlank(proxyConfig.getGlobalZookeeperServers())) {
        localZooKeeperConnectionService = new LocalZooKeeperConnectionService(getZooKeeperClientFactory(), proxyConfig.getZookeeperServers(), proxyConfig.getZookeeperSessionTimeoutMs());
        localZooKeeperConnectionService.start(new ShutdownService() {

            @Override
            public void shutdown(int exitCode) {
                LOG.error("Lost local ZK session. Shutting down the proxy");
                Runtime.getRuntime().halt(-1);
            }
        });
        discoveryProvider = new BrokerDiscoveryProvider(this.proxyConfig, getZooKeeperClientFactory());
        this.configurationCacheService = new ConfigurationCacheService(discoveryProvider.globalZkCache);
        authorizationService = new AuthorizationService(serviceConfiguration, configurationCacheService);
    }
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    bootstrap.group(acceptorGroup, workerGroup);
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
    bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(1024, 16 * 1024, 1 * 1024 * 1024));
    bootstrap.channel(EventLoopUtil.getServerSocketChannelClass(workerGroup));
    EventLoopUtil.enableTriggeredMode(bootstrap);
    bootstrap.childHandler(new ServiceChannelInitializer(this, proxyConfig, false));
    // Bind and start to accept incoming connections.
    bootstrap.bind(proxyConfig.getServicePort()).sync();
    LOG.info("Started Pulsar Proxy at {}", serviceUrl);
    if (proxyConfig.isTlsEnabledInProxy()) {
        ServerBootstrap tlsBootstrap = bootstrap.clone();
        tlsBootstrap.childHandler(new ServiceChannelInitializer(this, proxyConfig, true));
        tlsBootstrap.bind(proxyConfig.getServicePortTls()).sync();
        LOG.info("Started Pulsar TLS Proxy on port {}", proxyConfig.getServicePortTls());
    }
}
Also used : ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) AdaptiveRecvByteBufAllocator(io.netty.channel.AdaptiveRecvByteBufAllocator) LocalZooKeeperConnectionService(org.apache.pulsar.zookeeper.LocalZooKeeperConnectionService) AuthorizationService(org.apache.pulsar.broker.authorization.AuthorizationService) ShutdownService(org.apache.pulsar.zookeeper.ZooKeeperSessionWatcher.ShutdownService) ConfigurationCacheService(org.apache.pulsar.broker.cache.ConfigurationCacheService) AuthenticationService(org.apache.pulsar.broker.authentication.AuthenticationService) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 2 with LocalZooKeeperConnectionService

use of org.apache.pulsar.zookeeper.LocalZooKeeperConnectionService in project incubator-pulsar by apache.

the class LocalZooKeeperConnectionServiceTest method testSimpleZooKeeperConnection.

@Test
void testSimpleZooKeeperConnection() throws Exception {
    LocalZooKeeperConnectionService localZkConnectionService = new LocalZooKeeperConnectionService(mockZkClientFactory, "dummy", 1000);
    localZkConnectionService.start(null);
    // Get ZooKeeper client
    MockZooKeeper zk = (MockZooKeeper) localZkConnectionService.getLocalZooKeeper();
    // Check status
    assertTrue(zk.getState().isConnected());
    // Create persistent node
    LocalZooKeeperConnectionService.checkAndCreatePersistNode(zk, "/path1");
    assertTrue(zk.exists("/path1", false) != null);
    // Delete and re-create existing node
    // The sessionId must be set to except 0L in order to re-create.
    zk.setSessionId(-1L);
    LocalZooKeeperConnectionService.createIfAbsent(zk, "/path1", "data1", CreateMode.EPHEMERAL, true);
    assertEquals(zk.getData("/path1", null, null), "data1".getBytes());
    // Try to create existing node (nothing should happen)
    LocalZooKeeperConnectionService.checkAndCreatePersistNode(zk, "/path1");
    assertTrue(zk.exists("/path1", false) != null);
    // Create new node (data is given as String)
    LocalZooKeeperConnectionService.createIfAbsent(zk, "/path2", "data2", CreateMode.EPHEMERAL);
    assertTrue(zk.exists("/path2", false) != null);
    assertEquals(zk.getData("/path2", null, null), "data2".getBytes());
    // Create new node (data is given as bytes)
    LocalZooKeeperConnectionService.createIfAbsent(zk, "/path3", "data3".getBytes(), CreateMode.EPHEMERAL);
    assertTrue(zk.exists("/path3", false) != null);
    assertEquals(zk.getData("/path3", null, null), "data3".getBytes());
    // delete nodes
    LocalZooKeeperConnectionService.deleteIfExists(zk, "/path1", -1);
    assertTrue(zk.exists("/path1", false) == null);
    LocalZooKeeperConnectionService.deleteIfExists(zk, "/path2", -1);
    assertTrue(zk.exists("/path2", false) == null);
    LocalZooKeeperConnectionService.deleteIfExists(zk, "/path3", -1);
    assertTrue(zk.exists("/path3", false) == null);
    // delete not existing node
    LocalZooKeeperConnectionService.deleteIfExists(zk, "/not_exist", -1);
    // Try to create invalid node (nothing should happen)
    LocalZooKeeperConnectionService.checkAndCreatePersistNode(zk, "/////");
    assertTrue(zk.exists("//////", false) == null);
    localZkConnectionService.close();
}
Also used : MockZooKeeper(org.apache.zookeeper.MockZooKeeper) LocalZooKeeperConnectionService(org.apache.pulsar.zookeeper.LocalZooKeeperConnectionService) Test(org.testng.annotations.Test)

Example 3 with LocalZooKeeperConnectionService

use of org.apache.pulsar.zookeeper.LocalZooKeeperConnectionService in project incubator-pulsar by apache.

the class PulsarService method start.

/**
 * Start the pulsar service instance.
 */
public void start() throws PulsarServerException {
    mutex.lock();
    try {
        if (state != State.Init) {
            throw new PulsarServerException("Cannot start the service once it was stopped");
        }
        // Now we are ready to start services
        localZooKeeperConnectionProvider = new LocalZooKeeperConnectionService(getZooKeeperClientFactory(), config.getZookeeperServers(), config.getZooKeeperSessionTimeoutMillis());
        localZooKeeperConnectionProvider.start(shutdownService);
        // Initialize and start service to access configuration repository.
        this.startZkCacheService();
        this.bkClientFactory = getBookKeeperClientFactory();
        managedLedgerClientFactory = new ManagedLedgerClientFactory(config, getZkClient(), bkClientFactory);
        this.brokerService = new BrokerService(this);
        // Start load management service (even if load balancing is disabled)
        this.loadManager.set(LoadManager.create(this));
        this.startLoadManagementService();
        // needs load management service
        this.startNamespaceService();
        LOG.info("Starting Pulsar Broker service; version: '{}'", (brokerVersion != null ? brokerVersion : "unknown"));
        brokerService.start();
        this.webService = new WebService(this);
        Map<String, Object> attributeMap = Maps.newHashMap();
        attributeMap.put(WebService.ATTRIBUTE_PULSAR_NAME, this);
        Map<String, Object> vipAttributeMap = Maps.newHashMap();
        vipAttributeMap.put(VipStatus.ATTRIBUTE_STATUS_FILE_PATH, this.config.getStatusFilePath());
        this.webService.addRestResources("/", VipStatus.class.getPackage().getName(), false, vipAttributeMap);
        this.webService.addRestResources("/", "org.apache.pulsar.broker.web", false, attributeMap);
        this.webService.addRestResources("/admin", "org.apache.pulsar.broker.admin.v1", true, attributeMap);
        this.webService.addRestResources("/admin/v2", "org.apache.pulsar.broker.admin.v2", true, attributeMap);
        this.webService.addRestResources("/lookup", "org.apache.pulsar.broker.lookup", true, attributeMap);
        this.webService.addServlet("/metrics", new ServletHolder(new PrometheusMetricsServlet(this, config.exposeTopicLevelMetricsInPrometheus())), false, attributeMap);
        if (config.isWebSocketServiceEnabled()) {
            // Use local broker address to avoid different IP address when using a VIP for service discovery
            this.webSocketService = new WebSocketService(new ClusterData(webServiceAddress, webServiceAddressTls, brokerServiceUrl, brokerServiceUrlTls), config);
            this.webSocketService.start();
            final WebSocketServlet producerWebSocketServlet = new WebSocketProducerServlet(webSocketService);
            this.webService.addServlet(WebSocketProducerServlet.SERVLET_PATH, new ServletHolder(producerWebSocketServlet), true, attributeMap);
            this.webService.addServlet(WebSocketProducerServlet.SERVLET_PATH_V2, new ServletHolder(producerWebSocketServlet), true, attributeMap);
            final WebSocketServlet consumerWebSocketServlet = new WebSocketConsumerServlet(webSocketService);
            this.webService.addServlet(WebSocketConsumerServlet.SERVLET_PATH, new ServletHolder(consumerWebSocketServlet), true, attributeMap);
            this.webService.addServlet(WebSocketConsumerServlet.SERVLET_PATH_V2, new ServletHolder(consumerWebSocketServlet), true, attributeMap);
            final WebSocketServlet readerWebSocketServlet = new WebSocketReaderServlet(webSocketService);
            this.webService.addServlet(WebSocketReaderServlet.SERVLET_PATH, new ServletHolder(readerWebSocketServlet), true, attributeMap);
            this.webService.addServlet(WebSocketReaderServlet.SERVLET_PATH_V2, new ServletHolder(readerWebSocketServlet), true, attributeMap);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Attempting to add static directory");
        }
        this.webService.addStaticResources("/static", "/static");
        // Register heartbeat and bootstrap namespaces.
        this.nsservice.registerBootstrapNamespaces();
        // Start the leader election service
        this.leaderElectionService = new LeaderElectionService(this, new LeaderListener() {

            @Override
            public synchronized void brokerIsTheLeaderNow() {
                if (getConfiguration().isLoadBalancerEnabled()) {
                    long loadSheddingInterval = TimeUnit.MINUTES.toMillis(getConfiguration().getLoadBalancerSheddingIntervalMinutes());
                    long resourceQuotaUpdateInterval = TimeUnit.MINUTES.toMillis(getConfiguration().getLoadBalancerResourceQuotaUpdateIntervalMinutes());
                    loadSheddingTask = loadManagerExecutor.scheduleAtFixedRate(new LoadSheddingTask(loadManager), loadSheddingInterval, loadSheddingInterval, TimeUnit.MILLISECONDS);
                    loadResourceQuotaTask = loadManagerExecutor.scheduleAtFixedRate(new LoadResourceQuotaUpdaterTask(loadManager), resourceQuotaUpdateInterval, resourceQuotaUpdateInterval, TimeUnit.MILLISECONDS);
                }
            }

            @Override
            public synchronized void brokerIsAFollowerNow() {
                if (loadSheddingTask != null) {
                    loadSheddingTask.cancel(false);
                    loadSheddingTask = null;
                }
                if (loadResourceQuotaTask != null) {
                    loadResourceQuotaTask.cancel(false);
                    loadResourceQuotaTask = null;
                }
            }
        });
        leaderElectionService.start();
        webService.start();
        this.metricsGenerator = new MetricsGenerator(this);
        schemaRegistryService = SchemaRegistryService.create(this);
        state = State.Started;
        acquireSLANamespace();
        LOG.info("messaging service is ready, bootstrap service on port={}, broker url={}, cluster={}, configs={}", config.getWebServicePort(), brokerServiceUrl, config.getClusterName(), ReflectionToStringBuilder.toString(config));
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw new PulsarServerException(e);
    } finally {
        mutex.unlock();
    }
}
Also used : WebSocketReaderServlet(org.apache.pulsar.websocket.WebSocketReaderServlet) LeaderListener(org.apache.pulsar.broker.loadbalance.LeaderElectionService.LeaderListener) WebService(org.apache.pulsar.broker.web.WebService) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) WebSocketServlet(org.eclipse.jetty.websocket.servlet.WebSocketServlet) WebSocketProducerServlet(org.apache.pulsar.websocket.WebSocketProducerServlet) LoadResourceQuotaUpdaterTask(org.apache.pulsar.broker.loadbalance.LoadResourceQuotaUpdaterTask) WebSocketConsumerServlet(org.apache.pulsar.websocket.WebSocketConsumerServlet) IOException(java.io.IOException) MetricsGenerator(org.apache.pulsar.broker.stats.MetricsGenerator) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PrometheusMetricsServlet(org.apache.pulsar.broker.stats.prometheus.PrometheusMetricsServlet) LocalZooKeeperConnectionService(org.apache.pulsar.zookeeper.LocalZooKeeperConnectionService) LoadSheddingTask(org.apache.pulsar.broker.loadbalance.LoadSheddingTask) LeaderElectionService(org.apache.pulsar.broker.loadbalance.LeaderElectionService) WebSocketService(org.apache.pulsar.websocket.WebSocketService) BrokerService(org.apache.pulsar.broker.service.BrokerService)

Example 4 with LocalZooKeeperConnectionService

use of org.apache.pulsar.zookeeper.LocalZooKeeperConnectionService in project incubator-pulsar by apache.

the class LocalZooKeeperConnectionServiceTest method testSimpleZooKeeperConnectionFail.

@Test
void testSimpleZooKeeperConnectionFail() throws Exception {
    LocalZooKeeperConnectionService localZkConnectionService = new LocalZooKeeperConnectionService(new ZookeeperClientFactoryImpl(), "dummy", 1000);
    try {
        localZkConnectionService.start(null);
        fail("should fail");
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("Failed to establish session with local ZK"));
    }
    localZkConnectionService.close();
}
Also used : LocalZooKeeperConnectionService(org.apache.pulsar.zookeeper.LocalZooKeeperConnectionService) ZookeeperClientFactoryImpl(org.apache.pulsar.zookeeper.ZookeeperClientFactoryImpl) Test(org.testng.annotations.Test)

Aggregations

LocalZooKeeperConnectionService (org.apache.pulsar.zookeeper.LocalZooKeeperConnectionService)4 Test (org.testng.annotations.Test)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 AdaptiveRecvByteBufAllocator (io.netty.channel.AdaptiveRecvByteBufAllocator)1 IOException (java.io.IOException)1 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)1 AuthenticationService (org.apache.pulsar.broker.authentication.AuthenticationService)1 AuthorizationService (org.apache.pulsar.broker.authorization.AuthorizationService)1 ConfigurationCacheService (org.apache.pulsar.broker.cache.ConfigurationCacheService)1 LeaderElectionService (org.apache.pulsar.broker.loadbalance.LeaderElectionService)1 LeaderListener (org.apache.pulsar.broker.loadbalance.LeaderElectionService.LeaderListener)1 LoadResourceQuotaUpdaterTask (org.apache.pulsar.broker.loadbalance.LoadResourceQuotaUpdaterTask)1 LoadSheddingTask (org.apache.pulsar.broker.loadbalance.LoadSheddingTask)1 BrokerService (org.apache.pulsar.broker.service.BrokerService)1 MetricsGenerator (org.apache.pulsar.broker.stats.MetricsGenerator)1 PrometheusMetricsServlet (org.apache.pulsar.broker.stats.prometheus.PrometheusMetricsServlet)1 WebService (org.apache.pulsar.broker.web.WebService)1 ClusterData (org.apache.pulsar.common.policies.data.ClusterData)1 WebSocketConsumerServlet (org.apache.pulsar.websocket.WebSocketConsumerServlet)1 WebSocketProducerServlet (org.apache.pulsar.websocket.WebSocketProducerServlet)1