Search in sources :

Example 1 with SystemTopicBaseTxnBufferSnapshotService

use of org.apache.pulsar.broker.service.SystemTopicBaseTxnBufferSnapshotService in project pulsar by yahoo.

the class PulsarService method start.

/**
 * Start the pulsar service instance.
 */
public void start() throws PulsarServerException {
    LOG.info("Starting Pulsar Broker service; version: '{}'", (brokerVersion != null ? brokerVersion : "unknown"));
    LOG.info("Git Revision {}", PulsarVersion.getGitSha());
    LOG.info("Git Branch {}", PulsarVersion.getGitBranch());
    LOG.info("Built by {} on {} at {}", PulsarVersion.getBuildUser(), PulsarVersion.getBuildHost(), PulsarVersion.getBuildTime());
    // start time mills
    long startTimestamp = System.currentTimeMillis();
    mutex.lock();
    try {
        if (state != State.Init) {
            throw new PulsarServerException("Cannot start the service once it was stopped");
        }
        if (!config.getWebServicePort().isPresent() && !config.getWebServicePortTls().isPresent()) {
            throw new IllegalArgumentException("webServicePort/webServicePortTls must be present");
        }
        if (config.isAuthorizationEnabled() && !config.isAuthenticationEnabled()) {
            throw new IllegalStateException("Invalid broker configuration. Authentication must be enabled with " + "authenticationEnabled=true when authorization is enabled with authorizationEnabled=true.");
        }
        if (config.getDefaultRetentionSizeInMB() > 0 && config.getBacklogQuotaDefaultLimitBytes() > 0 && config.getBacklogQuotaDefaultLimitBytes() >= (config.getDefaultRetentionSizeInMB() * 1024L * 1024L)) {
            throw new IllegalArgumentException(String.format("The retention size must > the backlog quota limit " + "size, but the configured backlog quota limit bytes is %d, the retention size is %d", config.getBacklogQuotaDefaultLimitBytes(), config.getDefaultRetentionSizeInMB() * 1024L * 1024L));
        }
        if (config.getDefaultRetentionTimeInMinutes() > 0 && config.getBacklogQuotaDefaultLimitSecond() > 0 && config.getBacklogQuotaDefaultLimitSecond() >= config.getDefaultRetentionTimeInMinutes() * 60) {
            throw new IllegalArgumentException(String.format("The retention time must > the backlog quota limit " + "time, but the configured backlog quota limit time duration is %d, " + "the retention time duration is %d", config.getBacklogQuotaDefaultLimitSecond(), config.getDefaultRetentionTimeInMinutes() * 60));
        }
        if (!config.getLoadBalancerOverrideBrokerNicSpeedGbps().isPresent() && config.isLoadBalancerEnabled() && LinuxInfoUtils.isLinux() && !LinuxInfoUtils.checkHasNicSpeeds()) {
            throw new IllegalStateException("Unable to read VM NIC speed. You must set " + "[loadBalancerOverrideBrokerNicSpeedGbps] to override it when load balancer is enabled.");
        }
        localMetadataStore = createLocalMetadataStore();
        localMetadataStore.registerSessionListener(this::handleMetadataSessionEvent);
        coordinationService = new CoordinationServiceImpl(localMetadataStore);
        if (config.isConfigurationStoreSeparated()) {
            configurationMetadataStore = createConfigurationMetadataStore();
            shouldShutdownConfigurationMetadataStore = true;
        } else {
            configurationMetadataStore = localMetadataStore;
            shouldShutdownConfigurationMetadataStore = false;
        }
        pulsarResources = new PulsarResources(localMetadataStore, configurationMetadataStore, config.getMetadataStoreOperationTimeoutSeconds());
        pulsarResources.getClusterResources().getStore().registerListener(this::handleDeleteCluster);
        orderedExecutor = OrderedExecutor.newBuilder().numThreads(config.getNumOrderedExecutorThreads()).name("pulsar-ordered").build();
        // Initialize the message protocol handlers
        protocolHandlers = ProtocolHandlers.load(config);
        protocolHandlers.initialize(config);
        // Now we are ready to start services
        this.bkClientFactory = newBookKeeperClientFactory();
        managedLedgerClientFactory = ManagedLedgerStorage.create(config, localMetadataStore, bkClientFactory, ioEventLoopGroup);
        this.brokerService = newBrokerService(this);
        // Start load management service (even if load balancing is disabled)
        this.loadManager.set(LoadManager.create(this));
        // needs load management service and before start broker service,
        this.startNamespaceService();
        schemaStorage = createAndStartSchemaStorage();
        schemaRegistryService = SchemaRegistryService.create(schemaStorage, config.getSchemaRegistryCompatibilityCheckers());
        this.defaultOffloader = createManagedLedgerOffloader(OffloadPoliciesImpl.create(this.getConfiguration().getProperties()));
        this.brokerInterceptor = BrokerInterceptors.load(config);
        brokerService.setInterceptor(getBrokerInterceptor());
        this.brokerInterceptor.initialize(this);
        brokerService.start();
        // Load additional servlets
        this.brokerAdditionalServlets = AdditionalServlets.load(config);
        this.webService = new WebService(this);
        createMetricsServlet();
        this.addWebServerHandlers(webService, metricsServlet, this.config);
        this.webService.start();
        // Refresh addresses and update configuration, since the port might have been dynamically assigned
        if (config.getBrokerServicePort().equals(Optional.of(0))) {
            config.setBrokerServicePort(brokerService.getListenPort());
        }
        if (config.getBrokerServicePortTls().equals(Optional.of(0))) {
            config.setBrokerServicePortTls(brokerService.getListenPortTls());
        }
        this.webServiceAddress = webAddress(config);
        this.webServiceAddressTls = webAddressTls(config);
        this.brokerServiceUrl = brokerUrl(config);
        this.brokerServiceUrlTls = brokerUrlTls(config);
        if (null != this.webSocketService) {
            ClusterDataImpl clusterData = ClusterDataImpl.builder().serviceUrl(webServiceAddress).serviceUrlTls(webServiceAddressTls).brokerServiceUrl(brokerServiceUrl).brokerServiceUrlTls(brokerServiceUrlTls).build();
            this.webSocketService.setLocalCluster(clusterData);
        }
        // Initialize namespace service, after service url assigned. Should init zk and refresh self owner info.
        this.nsService.initialize();
        // Start topic level policies service
        if (config.isTopicLevelPoliciesEnabled() && config.isSystemTopicEnabled()) {
            this.topicPoliciesService = new SystemTopicBasedTopicPoliciesService(this);
        }
        this.topicPoliciesService.start();
        // Start the leader election service
        startLeaderElectionService();
        // Register heartbeat and bootstrap namespaces.
        this.nsService.registerBootstrapNamespaces();
        // Register pulsar system namespaces and start transaction meta store service
        if (config.isTransactionCoordinatorEnabled()) {
            this.transactionBufferSnapshotService = new SystemTopicBaseTxnBufferSnapshotService(getClient());
            this.transactionTimer = new HashedWheelTimer(new DefaultThreadFactory("pulsar-transaction-timer"));
            transactionBufferClient = TransactionBufferClientImpl.create(this, transactionTimer, config.getTransactionBufferClientMaxConcurrentRequests(), config.getTransactionBufferClientOperationTimeoutInMills());
            transactionMetadataStoreService = new TransactionMetadataStoreService(TransactionMetadataStoreProvider.newProvider(config.getTransactionMetadataStoreProviderClassName()), this, transactionBufferClient, transactionTimer);
            transactionBufferProvider = TransactionBufferProvider.newProvider(config.getTransactionBufferProviderClassName());
            transactionPendingAckStoreProvider = TransactionPendingAckStoreProvider.newProvider(config.getTransactionPendingAckStoreProviderClassName());
        }
        this.metricsGenerator = new MetricsGenerator(this);
        // By starting the Load manager service, the broker will also become visible
        // to the rest of the broker by creating the registration z-node. This needs
        // to be done only when the broker is fully operative.
        this.startLoadManagementService();
        // Initialize the message protocol handlers.
        // start the protocol handlers only after the broker is ready,
        // so that the protocol handlers can access broker service properly.
        this.protocolHandlers.start(brokerService);
        Map<String, Map<InetSocketAddress, ChannelInitializer<SocketChannel>>> protocolHandlerChannelInitializers = this.protocolHandlers.newChannelInitializers();
        this.brokerService.startProtocolHandlers(protocolHandlerChannelInitializers);
        acquireSLANamespace();
        // start function worker service if necessary
        this.startWorkerService(brokerService.getAuthenticationService(), brokerService.getAuthorizationService());
        // start packages management service if necessary
        if (config.isEnablePackagesManagement()) {
            this.startPackagesManagementService();
        }
        // Start the task to publish resource usage, if necessary
        this.resourceUsageTransportManager = DISABLE_RESOURCE_USAGE_TRANSPORT_MANAGER;
        if (isNotBlank(config.getResourceUsageTransportClassName())) {
            Class<?> clazz = Class.forName(config.getResourceUsageTransportClassName());
            Constructor<?> ctor = clazz.getConstructor(PulsarService.class);
            Object object = ctor.newInstance(new Object[] { this });
            this.resourceUsageTransportManager = (ResourceUsageTopicTransportManager) object;
        }
        this.resourceGroupServiceManager = new ResourceGroupService(this);
        long currentTimestamp = System.currentTimeMillis();
        final long bootstrapTimeSeconds = TimeUnit.MILLISECONDS.toSeconds(currentTimestamp - startTimestamp);
        final String bootstrapMessage = "bootstrap service " + (config.getWebServicePort().isPresent() ? "port = " + config.getWebServicePort().get() : "") + (config.getWebServicePortTls().isPresent() ? ", tls-port = " + config.getWebServicePortTls() : "") + (StringUtils.isNotEmpty(brokerServiceUrl) ? ", broker url= " + brokerServiceUrl : "") + (StringUtils.isNotEmpty(brokerServiceUrlTls) ? ", broker tls url= " + brokerServiceUrlTls : "");
        LOG.info("messaging service is ready, bootstrap_seconds={}", bootstrapTimeSeconds);
        LOG.info("messaging service is ready, {}, cluster={}, configs={}", bootstrapMessage, config.getClusterName(), config);
        state = State.Started;
    } catch (Exception e) {
        LOG.error("Failed to start Pulsar service: {}", e.getMessage(), e);
        throw new PulsarServerException(e);
    } finally {
        mutex.unlock();
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) WebService(org.apache.pulsar.broker.web.WebService) CoordinationServiceImpl(org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl) HashedWheelTimer(io.netty.util.HashedWheelTimer) MetadataStoreException(org.apache.pulsar.metadata.api.MetadataStoreException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) CompletionException(java.util.concurrent.CompletionException) DeploymentException(javax.websocket.DeploymentException) MalformedURLException(java.net.MalformedURLException) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) MetricsGenerator(org.apache.pulsar.broker.stats.MetricsGenerator) SystemTopicBaseTxnBufferSnapshotService(org.apache.pulsar.broker.service.SystemTopicBaseTxnBufferSnapshotService) ResourceGroupService(org.apache.pulsar.broker.resourcegroup.ResourceGroupService) PulsarResources(org.apache.pulsar.broker.resources.PulsarResources) ClusterDataImpl(org.apache.pulsar.common.policies.data.ClusterDataImpl) SystemTopicBasedTopicPoliciesService(org.apache.pulsar.broker.service.SystemTopicBasedTopicPoliciesService) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with SystemTopicBaseTxnBufferSnapshotService

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

the class PulsarService method start.

/**
 * Start the pulsar service instance.
 */
public void start() throws PulsarServerException {
    LOG.info("Starting Pulsar Broker service; version: '{}'", (brokerVersion != null ? brokerVersion : "unknown"));
    LOG.info("Git Revision {}", PulsarVersion.getGitSha());
    LOG.info("Git Branch {}", PulsarVersion.getGitBranch());
    LOG.info("Built by {} on {} at {}", PulsarVersion.getBuildUser(), PulsarVersion.getBuildHost(), PulsarVersion.getBuildTime());
    // start time mills
    long startTimestamp = System.currentTimeMillis();
    mutex.lock();
    try {
        if (state != State.Init) {
            throw new PulsarServerException("Cannot start the service once it was stopped");
        }
        if (!config.getWebServicePort().isPresent() && !config.getWebServicePortTls().isPresent()) {
            throw new IllegalArgumentException("webServicePort/webServicePortTls must be present");
        }
        if (config.isAuthorizationEnabled() && !config.isAuthenticationEnabled()) {
            throw new IllegalStateException("Invalid broker configuration. Authentication must be enabled with " + "authenticationEnabled=true when authorization is enabled with authorizationEnabled=true.");
        }
        if (config.getDefaultRetentionSizeInMB() > 0 && config.getBacklogQuotaDefaultLimitBytes() > 0 && config.getBacklogQuotaDefaultLimitBytes() >= (config.getDefaultRetentionSizeInMB() * 1024L * 1024L)) {
            throw new IllegalArgumentException(String.format("The retention size must > the backlog quota limit " + "size, but the configured backlog quota limit bytes is %d, the retention size is %d", config.getBacklogQuotaDefaultLimitBytes(), config.getDefaultRetentionSizeInMB() * 1024L * 1024L));
        }
        if (config.getDefaultRetentionTimeInMinutes() > 0 && config.getBacklogQuotaDefaultLimitSecond() > 0 && config.getBacklogQuotaDefaultLimitSecond() >= config.getDefaultRetentionTimeInMinutes() * 60) {
            throw new IllegalArgumentException(String.format("The retention time must > the backlog quota limit " + "time, but the configured backlog quota limit time duration is %d, " + "the retention time duration is %d", config.getBacklogQuotaDefaultLimitSecond(), config.getDefaultRetentionTimeInMinutes() * 60));
        }
        if (!config.getLoadBalancerOverrideBrokerNicSpeedGbps().isPresent() && config.isLoadBalancerEnabled() && LinuxInfoUtils.isLinux() && !LinuxInfoUtils.checkHasNicSpeeds()) {
            throw new IllegalStateException("Unable to read VM NIC speed. You must set " + "[loadBalancerOverrideBrokerNicSpeedGbps] to override it when load balancer is enabled.");
        }
        localMetadataStore = createLocalMetadataStore();
        localMetadataStore.registerSessionListener(this::handleMetadataSessionEvent);
        coordinationService = new CoordinationServiceImpl(localMetadataStore);
        if (config.isConfigurationStoreSeparated()) {
            configurationMetadataStore = createConfigurationMetadataStore();
            shouldShutdownConfigurationMetadataStore = true;
        } else {
            configurationMetadataStore = localMetadataStore;
            shouldShutdownConfigurationMetadataStore = false;
        }
        pulsarResources = new PulsarResources(localMetadataStore, configurationMetadataStore, config.getMetadataStoreOperationTimeoutSeconds());
        pulsarResources.getClusterResources().getStore().registerListener(this::handleDeleteCluster);
        orderedExecutor = OrderedExecutor.newBuilder().numThreads(config.getNumOrderedExecutorThreads()).name("pulsar-ordered").build();
        // Initialize the message protocol handlers
        protocolHandlers = ProtocolHandlers.load(config);
        protocolHandlers.initialize(config);
        // Now we are ready to start services
        this.bkClientFactory = newBookKeeperClientFactory();
        managedLedgerClientFactory = ManagedLedgerStorage.create(config, localMetadataStore, bkClientFactory, ioEventLoopGroup);
        this.brokerService = newBrokerService(this);
        // Start load management service (even if load balancing is disabled)
        this.loadManager.set(LoadManager.create(this));
        // needs load management service and before start broker service,
        this.startNamespaceService();
        schemaStorage = createAndStartSchemaStorage();
        schemaRegistryService = SchemaRegistryService.create(schemaStorage, config.getSchemaRegistryCompatibilityCheckers());
        this.defaultOffloader = createManagedLedgerOffloader(OffloadPoliciesImpl.create(this.getConfiguration().getProperties()));
        this.brokerInterceptor = BrokerInterceptors.load(config);
        brokerService.setInterceptor(getBrokerInterceptor());
        this.brokerInterceptor.initialize(this);
        brokerService.start();
        // Load additional servlets
        this.brokerAdditionalServlets = AdditionalServlets.load(config);
        this.webService = new WebService(this);
        createMetricsServlet();
        this.addWebServerHandlers(webService, metricsServlet, this.config);
        this.webService.start();
        // Refresh addresses and update configuration, since the port might have been dynamically assigned
        if (config.getBrokerServicePort().equals(Optional.of(0))) {
            config.setBrokerServicePort(brokerService.getListenPort());
        }
        if (config.getBrokerServicePortTls().equals(Optional.of(0))) {
            config.setBrokerServicePortTls(brokerService.getListenPortTls());
        }
        this.webServiceAddress = webAddress(config);
        this.webServiceAddressTls = webAddressTls(config);
        this.brokerServiceUrl = brokerUrl(config);
        this.brokerServiceUrlTls = brokerUrlTls(config);
        if (null != this.webSocketService) {
            ClusterDataImpl clusterData = ClusterDataImpl.builder().serviceUrl(webServiceAddress).serviceUrlTls(webServiceAddressTls).brokerServiceUrl(brokerServiceUrl).brokerServiceUrlTls(brokerServiceUrlTls).build();
            this.webSocketService.setLocalCluster(clusterData);
        }
        // Initialize namespace service, after service url assigned. Should init zk and refresh self owner info.
        this.nsService.initialize();
        // Start topic level policies service
        if (config.isTopicLevelPoliciesEnabled() && config.isSystemTopicEnabled()) {
            this.topicPoliciesService = new SystemTopicBasedTopicPoliciesService(this);
        }
        this.topicPoliciesService.start();
        // Start the leader election service
        startLeaderElectionService();
        // Register heartbeat and bootstrap namespaces.
        this.nsService.registerBootstrapNamespaces();
        // Register pulsar system namespaces and start transaction meta store service
        if (config.isTransactionCoordinatorEnabled()) {
            this.transactionBufferSnapshotService = new SystemTopicBaseTxnBufferSnapshotService(getClient());
            this.transactionTimer = new HashedWheelTimer(new DefaultThreadFactory("pulsar-transaction-timer"));
            transactionBufferClient = TransactionBufferClientImpl.create(this, transactionTimer, config.getTransactionBufferClientMaxConcurrentRequests(), config.getTransactionBufferClientOperationTimeoutInMills());
            transactionMetadataStoreService = new TransactionMetadataStoreService(TransactionMetadataStoreProvider.newProvider(config.getTransactionMetadataStoreProviderClassName()), this, transactionBufferClient, transactionTimer);
            transactionBufferProvider = TransactionBufferProvider.newProvider(config.getTransactionBufferProviderClassName());
            transactionPendingAckStoreProvider = TransactionPendingAckStoreProvider.newProvider(config.getTransactionPendingAckStoreProviderClassName());
        }
        this.metricsGenerator = new MetricsGenerator(this);
        // By starting the Load manager service, the broker will also become visible
        // to the rest of the broker by creating the registration z-node. This needs
        // to be done only when the broker is fully operative.
        this.startLoadManagementService();
        // Initialize the message protocol handlers.
        // start the protocol handlers only after the broker is ready,
        // so that the protocol handlers can access broker service properly.
        this.protocolHandlers.start(brokerService);
        Map<String, Map<InetSocketAddress, ChannelInitializer<SocketChannel>>> protocolHandlerChannelInitializers = this.protocolHandlers.newChannelInitializers();
        this.brokerService.startProtocolHandlers(protocolHandlerChannelInitializers);
        acquireSLANamespace();
        // start function worker service if necessary
        this.startWorkerService(brokerService.getAuthenticationService(), brokerService.getAuthorizationService());
        // start packages management service if necessary
        if (config.isEnablePackagesManagement()) {
            this.startPackagesManagementService();
        }
        // Start the task to publish resource usage, if necessary
        this.resourceUsageTransportManager = DISABLE_RESOURCE_USAGE_TRANSPORT_MANAGER;
        if (isNotBlank(config.getResourceUsageTransportClassName())) {
            Class<?> clazz = Class.forName(config.getResourceUsageTransportClassName());
            Constructor<?> ctor = clazz.getConstructor(PulsarService.class);
            Object object = ctor.newInstance(new Object[] { this });
            this.resourceUsageTransportManager = (ResourceUsageTopicTransportManager) object;
        }
        this.resourceGroupServiceManager = new ResourceGroupService(this);
        long currentTimestamp = System.currentTimeMillis();
        final long bootstrapTimeSeconds = TimeUnit.MILLISECONDS.toSeconds(currentTimestamp - startTimestamp);
        final String bootstrapMessage = "bootstrap service " + (config.getWebServicePort().isPresent() ? "port = " + config.getWebServicePort().get() : "") + (config.getWebServicePortTls().isPresent() ? ", tls-port = " + config.getWebServicePortTls() : "") + (StringUtils.isNotEmpty(brokerServiceUrl) ? ", broker url= " + brokerServiceUrl : "") + (StringUtils.isNotEmpty(brokerServiceUrlTls) ? ", broker tls url= " + brokerServiceUrlTls : "");
        LOG.info("messaging service is ready, bootstrap_seconds={}", bootstrapTimeSeconds);
        LOG.info("messaging service is ready, {}, cluster={}, configs={}", bootstrapMessage, config.getClusterName(), config);
        state = State.Started;
    } catch (Exception e) {
        LOG.error("Failed to start Pulsar service: {}", e.getMessage(), e);
        throw new PulsarServerException(e);
    } finally {
        mutex.unlock();
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) WebService(org.apache.pulsar.broker.web.WebService) CoordinationServiceImpl(org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl) HashedWheelTimer(io.netty.util.HashedWheelTimer) MetadataStoreException(org.apache.pulsar.metadata.api.MetadataStoreException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) CompletionException(java.util.concurrent.CompletionException) DeploymentException(javax.websocket.DeploymentException) MalformedURLException(java.net.MalformedURLException) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) MetricsGenerator(org.apache.pulsar.broker.stats.MetricsGenerator) SystemTopicBaseTxnBufferSnapshotService(org.apache.pulsar.broker.service.SystemTopicBaseTxnBufferSnapshotService) ResourceGroupService(org.apache.pulsar.broker.resourcegroup.ResourceGroupService) PulsarResources(org.apache.pulsar.broker.resources.PulsarResources) ClusterDataImpl(org.apache.pulsar.common.policies.data.ClusterDataImpl) SystemTopicBasedTopicPoliciesService(org.apache.pulsar.broker.service.SystemTopicBasedTopicPoliciesService) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with SystemTopicBaseTxnBufferSnapshotService

use of org.apache.pulsar.broker.service.SystemTopicBaseTxnBufferSnapshotService in project pulsar by apache.

the class PulsarService method start.

/**
 * Start the pulsar service instance.
 */
public void start() throws PulsarServerException {
    LOG.info("Starting Pulsar Broker service; version: '{}'", (brokerVersion != null ? brokerVersion : "unknown"));
    LOG.info("Git Revision {}", PulsarVersion.getGitSha());
    LOG.info("Git Branch {}", PulsarVersion.getGitBranch());
    LOG.info("Built by {} on {} at {}", PulsarVersion.getBuildUser(), PulsarVersion.getBuildHost(), PulsarVersion.getBuildTime());
    // start time mills
    long startTimestamp = System.currentTimeMillis();
    mutex.lock();
    try {
        if (state != State.Init) {
            throw new PulsarServerException("Cannot start the service once it was stopped");
        }
        if (!config.getWebServicePort().isPresent() && !config.getWebServicePortTls().isPresent()) {
            throw new IllegalArgumentException("webServicePort/webServicePortTls must be present");
        }
        if (config.isAuthorizationEnabled() && !config.isAuthenticationEnabled()) {
            throw new IllegalStateException("Invalid broker configuration. Authentication must be enabled with " + "authenticationEnabled=true when authorization is enabled with authorizationEnabled=true.");
        }
        localMetadataStore = createLocalMetadataStore();
        localMetadataStore.registerSessionListener(this::handleMetadataSessionEvent);
        coordinationService = new CoordinationServiceImpl(localMetadataStore);
        if (config.isConfigurationStoreSeparated()) {
            configurationMetadataStore = createConfigurationMetadataStore();
            shouldShutdownConfigurationMetadataStore = true;
        } else {
            configurationMetadataStore = localMetadataStore;
            shouldShutdownConfigurationMetadataStore = false;
        }
        pulsarResources = new PulsarResources(localMetadataStore, configurationMetadataStore, config.getMetadataStoreOperationTimeoutSeconds());
        pulsarResources.getClusterResources().getStore().registerListener(this::handleDeleteCluster);
        orderedExecutor = OrderedExecutor.newBuilder().numThreads(config.getNumOrderedExecutorThreads()).name("pulsar-ordered").build();
        // Initialize the message protocol handlers
        protocolHandlers = ProtocolHandlers.load(config);
        protocolHandlers.initialize(config);
        // Now we are ready to start services
        this.bkClientFactory = newBookKeeperClientFactory();
        managedLedgerClientFactory = ManagedLedgerStorage.create(config, localMetadataStore, bkClientFactory, ioEventLoopGroup);
        this.brokerService = newBrokerService(this);
        // Start load management service (even if load balancing is disabled)
        this.loadManager.set(LoadManager.create(this));
        // needs load management service and before start broker service,
        this.startNamespaceService();
        schemaStorage = createAndStartSchemaStorage();
        schemaRegistryService = SchemaRegistryService.create(schemaStorage, config.getSchemaRegistryCompatibilityCheckers());
        this.defaultOffloader = createManagedLedgerOffloader(OffloadPoliciesImpl.create(this.getConfiguration().getProperties()));
        this.brokerInterceptor = BrokerInterceptors.load(config);
        brokerService.setInterceptor(getBrokerInterceptor());
        this.brokerInterceptor.initialize(this);
        brokerService.start();
        // Load additional servlets
        this.brokerAdditionalServlets = AdditionalServlets.load(config);
        this.webService = new WebService(this);
        this.metricsServlet = new PrometheusMetricsServlet(this, config.isExposeTopicLevelMetricsInPrometheus(), config.isExposeConsumerLevelMetricsInPrometheus(), config.isExposeProducerLevelMetricsInPrometheus(), config.isSplitTopicAndPartitionLabelInPrometheus());
        if (pendingMetricsProviders != null) {
            pendingMetricsProviders.forEach(provider -> metricsServlet.addRawMetricsProvider(provider));
            this.pendingMetricsProviders = null;
        }
        this.addWebServerHandlers(webService, metricsServlet, this.config);
        this.webService.start();
        heartbeatNamespaceV2 = NamespaceService.getHeartbeatNamespaceV2(this.advertisedAddress, this.config);
        // Refresh addresses and update configuration, since the port might have been dynamically assigned
        if (config.getBrokerServicePort().equals(Optional.of(0))) {
            config.setBrokerServicePort(brokerService.getListenPort());
        }
        if (config.getBrokerServicePortTls().equals(Optional.of(0))) {
            config.setBrokerServicePortTls(brokerService.getListenPortTls());
        }
        this.webServiceAddress = webAddress(config);
        this.webServiceAddressTls = webAddressTls(config);
        this.brokerServiceUrl = brokerUrl(config);
        this.brokerServiceUrlTls = brokerUrlTls(config);
        if (null != this.webSocketService) {
            ClusterDataImpl clusterData = ClusterDataImpl.builder().serviceUrl(webServiceAddress).serviceUrlTls(webServiceAddressTls).brokerServiceUrl(brokerServiceUrl).brokerServiceUrlTls(brokerServiceUrlTls).build();
            this.webSocketService.setLocalCluster(clusterData);
        }
        // Initialize namespace service, after service url assigned. Should init zk and refresh self owner info.
        this.nsService.initialize();
        // Start topic level policies service
        if (config.isTopicLevelPoliciesEnabled() && config.isSystemTopicEnabled()) {
            this.topicPoliciesService = new SystemTopicBasedTopicPoliciesService(this);
        }
        this.topicPoliciesService.start();
        // Start the leader election service
        startLeaderElectionService();
        // Register heartbeat and bootstrap namespaces.
        this.nsService.registerBootstrapNamespaces();
        // Register pulsar system namespaces and start transaction meta store service
        if (config.isTransactionCoordinatorEnabled()) {
            this.transactionBufferSnapshotService = new SystemTopicBaseTxnBufferSnapshotService(getClient());
            this.transactionTimer = new HashedWheelTimer(new DefaultThreadFactory("pulsar-transaction-timer"));
            transactionBufferClient = TransactionBufferClientImpl.create(getClient(), transactionTimer);
            transactionMetadataStoreService = new TransactionMetadataStoreService(TransactionMetadataStoreProvider.newProvider(config.getTransactionMetadataStoreProviderClassName()), this, transactionBufferClient, transactionTimer);
            transactionBufferProvider = TransactionBufferProvider.newProvider(config.getTransactionBufferProviderClassName());
            transactionPendingAckStoreProvider = TransactionPendingAckStoreProvider.newProvider(config.getTransactionPendingAckStoreProviderClassName());
        }
        this.metricsGenerator = new MetricsGenerator(this);
        // By starting the Load manager service, the broker will also become visible
        // to the rest of the broker by creating the registration z-node. This needs
        // to be done only when the broker is fully operative.
        this.startLoadManagementService();
        // Initialize the message protocol handlers.
        // start the protocol handlers only after the broker is ready,
        // so that the protocol handlers can access broker service properly.
        this.protocolHandlers.start(brokerService);
        Map<String, Map<InetSocketAddress, ChannelInitializer<SocketChannel>>> protocolHandlerChannelInitializers = this.protocolHandlers.newChannelInitializers();
        this.brokerService.startProtocolHandlers(protocolHandlerChannelInitializers);
        acquireSLANamespace();
        // start function worker service if necessary
        this.startWorkerService(brokerService.getAuthenticationService(), brokerService.getAuthorizationService());
        // start packages management service if necessary
        if (config.isEnablePackagesManagement()) {
            this.startPackagesManagementService();
        }
        // Start the task to publish resource usage, if necessary
        this.resourceUsageTransportManager = DISABLE_RESOURCE_USAGE_TRANSPORT_MANAGER;
        if (isNotBlank(config.getResourceUsageTransportClassName())) {
            Class<?> clazz = Class.forName(config.getResourceUsageTransportClassName());
            Constructor<?> ctor = clazz.getConstructor(PulsarService.class);
            Object object = ctor.newInstance(new Object[] { this });
            this.resourceUsageTransportManager = (ResourceUsageTopicTransportManager) object;
        }
        this.resourceGroupServiceManager = new ResourceGroupService(this);
        long currentTimestamp = System.currentTimeMillis();
        final long bootstrapTimeSeconds = TimeUnit.MILLISECONDS.toSeconds(currentTimestamp - startTimestamp);
        final String bootstrapMessage = "bootstrap service " + (config.getWebServicePort().isPresent() ? "port = " + config.getWebServicePort().get() : "") + (config.getWebServicePortTls().isPresent() ? ", tls-port = " + config.getWebServicePortTls() : "") + (StringUtils.isNotEmpty(brokerServiceUrl) ? ", broker url= " + brokerServiceUrl : "") + (StringUtils.isNotEmpty(brokerServiceUrlTls) ? ", broker tls url= " + brokerServiceUrlTls : "");
        LOG.info("messaging service is ready, bootstrap_seconds={}", bootstrapTimeSeconds);
        LOG.info("messaging service is ready, {}, cluster={}, configs={}", bootstrapMessage, config.getClusterName(), config);
        state = State.Started;
    } catch (Exception e) {
        LOG.error("Failed to start Pulsar service: {}", e.getMessage(), e);
        throw new PulsarServerException(e);
    } finally {
        mutex.unlock();
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) WebService(org.apache.pulsar.broker.web.WebService) CoordinationServiceImpl(org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl) HashedWheelTimer(io.netty.util.HashedWheelTimer) MetadataStoreException(org.apache.pulsar.metadata.api.MetadataStoreException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) CompletionException(java.util.concurrent.CompletionException) DeploymentException(javax.websocket.DeploymentException) MalformedURLException(java.net.MalformedURLException) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) MetricsGenerator(org.apache.pulsar.broker.stats.MetricsGenerator) SystemTopicBaseTxnBufferSnapshotService(org.apache.pulsar.broker.service.SystemTopicBaseTxnBufferSnapshotService) PrometheusMetricsServlet(org.apache.pulsar.broker.stats.prometheus.PrometheusMetricsServlet) ResourceGroupService(org.apache.pulsar.broker.resourcegroup.ResourceGroupService) PulsarResources(org.apache.pulsar.broker.resources.PulsarResources) ClusterDataImpl(org.apache.pulsar.common.policies.data.ClusterDataImpl) SystemTopicBasedTopicPoliciesService(org.apache.pulsar.broker.service.SystemTopicBasedTopicPoliciesService) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

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 CompletionException (java.util.concurrent.CompletionException)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 ServletException (javax.servlet.ServletException)3 DeploymentException (javax.websocket.DeploymentException)3 ResourceGroupService (org.apache.pulsar.broker.resourcegroup.ResourceGroupService)3 PulsarResources (org.apache.pulsar.broker.resources.PulsarResources)3 SystemTopicBaseTxnBufferSnapshotService (org.apache.pulsar.broker.service.SystemTopicBaseTxnBufferSnapshotService)3 SystemTopicBasedTopicPoliciesService (org.apache.pulsar.broker.service.SystemTopicBasedTopicPoliciesService)3 MetricsGenerator (org.apache.pulsar.broker.stats.MetricsGenerator)3 WebService (org.apache.pulsar.broker.web.WebService)3