srkukarni closed pull request #2080: Extract out common code from standalone 
and build Embedded Pulsar
URL: https://github.com/apache/incubator-pulsar/pull/2080
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/PulsarStandalone.java 
b/pulsar-broker/src/main/java/org/apache/pulsar/PulsarStandalone.java
new file mode 100644
index 0000000000..28e839b872
--- /dev/null
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/PulsarStandalone.java
@@ -0,0 +1,388 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar;
+
+import com.beust.jcommander.Parameter;
+import com.ea.agentloader.AgentLoader;
+import com.google.common.collect.Sets;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.ServiceConfigurationUtils;
+import org.apache.pulsar.client.admin.PulsarAdmin;
+import org.apache.pulsar.client.admin.PulsarAdminException;
+import org.apache.pulsar.common.naming.TopicName;
+import org.apache.pulsar.common.policies.data.ClusterData;
+import org.apache.pulsar.common.policies.data.TenantInfo;
+import org.apache.pulsar.functions.worker.WorkerConfig;
+import org.apache.pulsar.functions.worker.WorkerService;
+import org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble;
+import org.aspectj.weaver.loadtime.Agent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.URL;
+import java.nio.file.Paths;
+import java.util.Optional;
+
+import static org.apache.commons.lang3.StringUtils.isBlank;
+
+public class PulsarStandalone implements AutoCloseable {
+
+    private static final Logger log = 
LoggerFactory.getLogger(PulsarStandalone.class);
+
+    PulsarService broker;
+    PulsarAdmin admin;
+    LocalBookkeeperEnsemble bkEnsemble;
+    ServiceConfiguration config;
+    WorkerService fnWorkerService;
+
+    public void setBroker(PulsarService broker) {
+        this.broker = broker;
+    }
+
+    public void setAdmin(PulsarAdmin admin) {
+        this.admin = admin;
+    }
+
+    public void setBkEnsemble(LocalBookkeeperEnsemble bkEnsemble) {
+        this.bkEnsemble = bkEnsemble;
+    }
+
+    public void setBkPort(int bkPort) {
+        this.bkPort = bkPort;
+    }
+
+    public void setBkDir(String bkDir) {
+        this.bkDir = bkDir;
+    }
+
+    public void setAdvertisedAddress(String advertisedAddress) {
+        this.advertisedAddress = advertisedAddress;
+    }
+
+    public void setConfig(ServiceConfiguration config) { this.config = config; 
}
+
+    public void setFnWorkerService(WorkerService fnWorkerService) {
+        this.fnWorkerService = fnWorkerService;
+    }
+
+    public void setConfigFile(String configFile) {
+        this.configFile = configFile;
+    }
+
+    public void setWipeData(boolean wipeData) {
+        this.wipeData = wipeData;
+    }
+
+    public void setNumOfBk(int numOfBk) {
+        this.numOfBk = numOfBk;
+    }
+
+    public void setZkPort(int zkPort) {
+        this.zkPort = zkPort;
+    }
+
+    public void setZkDir(String zkDir) {
+        this.zkDir = zkDir;
+    }
+
+    public void setNoBroker(boolean noBroker) {
+        this.noBroker = noBroker;
+    }
+
+    public void setOnlyBroker(boolean onlyBroker) {
+        this.onlyBroker = onlyBroker;
+    }
+
+    public void setNoFunctionsWorker(boolean noFunctionsWorker) {
+        this.noFunctionsWorker = noFunctionsWorker;
+    }
+
+    public void setFnWorkerConfigFile(String fnWorkerConfigFile) {
+        this.fnWorkerConfigFile = fnWorkerConfigFile;
+    }
+
+    public void setNoStreamStorage(boolean noStreamStorage) {
+        this.noStreamStorage = noStreamStorage;
+    }
+
+    public void setStreamStoragePort(int streamStoragePort) {
+        this.streamStoragePort = streamStoragePort;
+    }
+
+    public void setHelp(boolean help) {
+        this.help = help;
+    }
+
+    public ServiceConfiguration getConfig() {
+        return config;
+    }
+
+    public String getConfigFile() {
+        return configFile;
+    }
+
+    public boolean isWipeData() {
+        return wipeData;
+    }
+
+    public int getNumOfBk() {
+        return numOfBk;
+    }
+
+    public int getZkPort() {
+        return zkPort;
+    }
+
+    public int getBkPort() {
+        return bkPort;
+    }
+
+    public String getZkDir() {
+        return zkDir;
+    }
+
+    public String getBkDir() {
+        return bkDir;
+    }
+
+    public boolean isNoBroker() {
+        return noBroker;
+    }
+
+    public boolean isOnlyBroker() {
+        return onlyBroker;
+    }
+
+    public boolean isNoFunctionsWorker() {
+        return noFunctionsWorker;
+    }
+
+    public String getFnWorkerConfigFile() {
+        return fnWorkerConfigFile;
+    }
+
+    public boolean isNoStreamStorage() {
+        return noStreamStorage;
+    }
+
+    public int getStreamStoragePort() {
+        return streamStoragePort;
+    }
+
+    public String getAdvertisedAddress() {
+        return advertisedAddress;
+    }
+
+    public boolean isHelp() {
+        return help;
+    }
+
+    @Parameter(names = { "-c", "--config" }, description = "Configuration file 
path", required = true)
+    private String configFile;
+
+    @Parameter(names = { "--wipe-data" }, description = "Clean up previous 
ZK/BK data")
+    private boolean wipeData = false;
+
+    @Parameter(names = { "--num-bookies" }, description = "Number of local 
Bookies")
+    private int numOfBk = 1;
+
+    @Parameter(names = { "--zookeeper-port" }, description = "Local 
zookeeper's port")
+    private int zkPort = 2181;
+
+    @Parameter(names = { "--bookkeeper-port" }, description = "Local bookies 
base port")
+    private int bkPort = 3181;
+
+    @Parameter(names = { "--zookeeper-dir" }, description = "Local zooKeeper's 
data directory")
+    private String zkDir = "data/standalone/zookeeper";
+
+    @Parameter(names = { "--bookkeeper-dir" }, description = "Local bookies 
base data directory")
+    private String bkDir = "data/standalone/bookkeeper";
+
+    @Parameter(names = { "--no-broker" }, description = "Only start ZK and BK 
services, no broker")
+    private boolean noBroker = false;
+
+    @Parameter(names = { "--only-broker" }, description = "Only start Pulsar 
broker service (no ZK, BK)")
+    private boolean onlyBroker = false;
+
+    @Parameter(names = {"-nfw", "--no-functions-worker"}, description = "Run 
functions worker with Broker")
+    private boolean noFunctionsWorker = false;
+
+    @Parameter(names = {"-fwc", "--functions-worker-conf"}, description = 
"Configuration file for Functions Worker")
+    private String fnWorkerConfigFile = 
Paths.get("").toAbsolutePath().normalize().toString() + 
"/conf/functions_worker.yml";
+
+    @Parameter(names = {"-nss", "--no-stream-storage"}, description = "Disable 
stream storage")
+    private boolean noStreamStorage = false;
+
+    @Parameter(names = { "--stream-storage-port" }, description = "Local 
bookies stream storage port")
+    private int streamStoragePort = 4181;
+
+    @Parameter(names = { "-a", "--advertised-address" }, description = 
"Standalone broker advertised address")
+    private String advertisedAddress = null;
+
+    @Parameter(names = { "-h", "--help" }, description = "Show this help 
message")
+    private boolean help = false;
+
+    void start() throws Exception {
+
+        if (config == null) {
+            System.exit(1);
+        }
+
+        log.debug("--- setup PulsarStandaloneStarter ---");
+
+        // load aspectj-weaver agent for instrumentation
+        AgentLoader.loadAgentClass(Agent.class.getName(), null);
+
+        if (!this.isOnlyBroker()) {
+            // Start LocalBookKeeper
+            bkEnsemble = new LocalBookkeeperEnsemble(
+                this.getNumOfBk(), this.getZkPort(), this.getBkPort(), 
this.getStreamStoragePort(), this.getZkDir(), this.getBkDir(), 
this.isWipeData(), config.getAdvertisedAddress());
+            bkEnsemble.startStandalone(!this.isNoStreamStorage());
+        }
+
+        if (this.isNoBroker()) {
+            return;
+        }
+
+        // initialize the functions worker
+        if (!this.isNoFunctionsWorker()) {
+            WorkerConfig workerConfig;
+            if (isBlank(this.getFnWorkerConfigFile())) {
+                workerConfig = new WorkerConfig();
+            } else {
+                workerConfig = WorkerConfig.load(this.getFnWorkerConfigFile());
+            }
+            // worker talks to local broker
+            workerConfig.setPulsarServiceUrl("pulsar://127.0.0.1:" + 
config.getBrokerServicePort());
+            workerConfig.setPulsarWebServiceUrl("http://127.0.0.1:"; + 
config.getWebServicePort());
+            workerConfig.setStateStorageServiceUrl("bk://127.0.0.1:" + 
this.getStreamStoragePort());
+            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();
+
+        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 = 
PulsarAdmin.builder().serviceHttpUrl(webServiceUrl.toString()).authentication(
+                config.getBrokerClientAuthenticationPlugin(), 
config.getBrokerClientAuthenticationParameters()).build();
+
+        final String cluster = config.getClusterName();
+
+        createSampleNameSpace(webServiceUrl, brokerServiceUrl, cluster);
+        createDefaultNameSpace(cluster);
+
+        log.debug("--- setup completed ---");
+    }
+
+    private void createDefaultNameSpace(String cluster) {
+        // Create a public tenant and default namespace
+        final String publicTenant = TopicName.PUBLIC_TENANT;
+        final String defaultNamespace = TopicName.PUBLIC_TENANT + "/" + 
TopicName.DEFAULT_NAMESPACE;
+        try {
+            if (!admin.tenants().getTenants().contains(publicTenant)) {
+                admin.tenants().createTenant(publicTenant,
+                        new 
TenantInfo(Sets.newHashSet(config.getSuperUserRoles()), 
Sets.newHashSet(cluster)));
+            }
+            if 
(!admin.namespaces().getNamespaces(publicTenant).contains(defaultNamespace)) {
+                admin.namespaces().createNamespace(defaultNamespace);
+                
admin.namespaces().setNamespaceReplicationClusters(defaultNamespace, 
Sets.newHashSet(config.getClusterName()));
+            }
+        } catch (PulsarAdminException e) {
+            log.info(e.getMessage());
+        }
+    }
+
+    private void createSampleNameSpace(URL webServiceUrl, String 
brokerServiceUrl, String cluster) {
+        // Create a sample namespace
+        final String property = "sample";
+        final String globalCluster = "global";
+        final String namespace = property + "/" + cluster + "/ns1";
+        try {
+            ClusterData clusterData = new 
ClusterData(webServiceUrl.toString(), null /* serviceUrlTls */,
+                    brokerServiceUrl, null /* brokerServiceUrlTls */);
+            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.tenants().getTenants().contains(property)) {
+                admin.tenants().createTenant(property,
+                        new 
TenantInfo(Sets.newHashSet(config.getSuperUserRoles()), 
Sets.newHashSet(cluster)));
+            }
+
+            if 
(!admin.namespaces().getNamespaces(property).contains(namespace)) {
+                admin.namespaces().createNamespace(namespace);
+            }
+        } catch (PulsarAdminException e) {
+            log.info(e.getMessage());
+        }
+    }
+    
+    /** this methods gets a buidler to use to build and an embedded pulsar 
instance `
+     * i.e.
+     * <pre>
+     * <code>
+     * PulsarStandalone pulsarStandalone = PulsarStandalone.builder().build();
+     * pulsarStandalone.start();
+     * pulsarStandalone.stop();
+     * </code>
+     * </pre>
+     * @return PulsarStandaloneBuilder instance
+     */
+    public static PulsarStandaloneBuilder builder(){
+        return PulsarStandaloneBuilder.instance();
+    }
+
+    @Override
+    public void close() {
+        try {
+            if (fnWorkerService != null) {
+                fnWorkerService.stop();
+            }
+
+            if (broker != null) {
+                broker.close();
+            }
+
+            if (bkEnsemble != null) {
+                bkEnsemble.stop();
+            }
+        } catch (Exception e) {
+            log.error("Shutdown failed: {}", e.getMessage());
+        }
+    }
+}
diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/PulsarStandaloneBuilder.java 
b/pulsar-broker/src/main/java/org/apache/pulsar/PulsarStandaloneBuilder.java
new file mode 100644
index 0000000000..fb1de4133c
--- /dev/null
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/PulsarStandaloneBuilder.java
@@ -0,0 +1,124 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar;
+
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.ServiceConfigurationUtils;
+
+import static org.apache.commons.lang3.StringUtils.isBlank;
+
+public final class PulsarStandaloneBuilder {
+
+    private PulsarStandalone pulsarStandalone;
+
+    private PulsarStandaloneBuilder() {
+        pulsarStandalone = new PulsarStandalone();
+        pulsarStandalone.setWipeData(true);
+        pulsarStandalone.setNoFunctionsWorker(true);
+    }
+
+    public static PulsarStandaloneBuilder instance() {
+        return new PulsarStandaloneBuilder();
+    }
+
+    public PulsarStandaloneBuilder withConfig(ServiceConfiguration config) {
+        pulsarStandalone.setConfig(config);
+        return this;
+    }
+
+    public PulsarStandaloneBuilder withWipeData(boolean wipeData) {
+        pulsarStandalone.setWipeData(wipeData);
+        return this;
+    }
+
+    public PulsarStandaloneBuilder withNumOfBk(int numOfBk) {
+        pulsarStandalone.setNumOfBk(numOfBk);
+        return this;
+    }
+
+    public PulsarStandaloneBuilder withZkPort(int zkPort) {
+        pulsarStandalone.setZkPort(zkPort);
+        return this;
+    }
+
+    public PulsarStandaloneBuilder withBkPort(int bkPort) {
+        pulsarStandalone.setBkPort(bkPort);
+        return this;
+    }
+
+    public PulsarStandaloneBuilder withZkDir(String zkDir) {
+        pulsarStandalone.setZkDir(zkDir);
+        return this;
+    }
+
+    public PulsarStandaloneBuilder withBkDir(String bkDir) {
+        pulsarStandalone.setBkDir(bkDir);
+        return this;
+    }
+
+    public PulsarStandaloneBuilder withNoBroker(boolean noBroker) {
+        pulsarStandalone.setNoBroker(noBroker);
+        return this;
+    }
+
+    public PulsarStandaloneBuilder withOnlyBroker(boolean onlyBroker) {
+        pulsarStandalone.setOnlyBroker(onlyBroker);
+        return this;
+    }
+
+    public PulsarStandaloneBuilder withNoStreamStorage(boolean 
noStreamStorage) {
+        pulsarStandalone.setNoStreamStorage(noStreamStorage);
+        return this;
+    }
+
+    public PulsarStandaloneBuilder withStreamStoragePort(int 
streamStoragePort) {
+        pulsarStandalone.setStreamStoragePort(streamStoragePort);
+        return this;
+    }
+
+    public PulsarStandaloneBuilder withAdvertisedAddress(String 
advertisedAddress) {
+        pulsarStandalone.setAdvertisedAddress(advertisedAddress);
+        return this;
+    }
+
+    public PulsarStandalone build() {
+        ServiceConfiguration config = new ServiceConfiguration();
+        config.setClusterName("standalone");
+        pulsarStandalone.setConfig(config);
+        String zkServers = "127.0.0.1";
+
+        if (pulsarStandalone.getAdvertisedAddress() != null) {
+            // Use advertised address from command line
+            
pulsarStandalone.getConfig().setAdvertisedAddress(pulsarStandalone.getAdvertisedAddress());
+            zkServers = pulsarStandalone.getAdvertisedAddress();
+        } else if 
(isBlank(pulsarStandalone.getConfig().getAdvertisedAddress())) {
+            // Use advertised address as local hostname
+            
pulsarStandalone.getConfig().setAdvertisedAddress(ServiceConfigurationUtils.unsafeLocalhostResolve());
+        } else {
+            // Use advertised address from config file
+        }
+
+        // Set ZK server's host to localhost
+        pulsarStandalone.getConfig().setZookeeperServers(zkServers + ":" + 
pulsarStandalone.getZkPort());
+        pulsarStandalone.getConfig().setConfigurationStoreServers(zkServers + 
":" + pulsarStandalone.getZkPort());
+        pulsarStandalone.getConfig().setRunningStandalone(true);
+        return pulsarStandalone;
+    }
+
+}
diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/PulsarStandaloneStarter.java 
b/pulsar-broker/src/main/java/org/apache/pulsar/PulsarStandaloneStarter.java
index d75d1d9d70..be645a6107 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/PulsarStandaloneStarter.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/PulsarStandaloneStarter.java
@@ -21,83 +21,16 @@
 import static org.apache.commons.lang3.StringUtils.isBlank;
 
 import java.io.FileInputStream;
-import java.net.URL;
 
-import java.nio.file.Paths;
-import java.util.Optional;
-import org.apache.pulsar.broker.PulsarService;
 import org.apache.pulsar.broker.ServiceConfiguration;
 import org.apache.pulsar.broker.ServiceConfigurationUtils;
-import org.apache.pulsar.client.admin.PulsarAdmin;
-import org.apache.pulsar.client.admin.PulsarAdminException;
 import org.apache.pulsar.common.configuration.PulsarConfigurationLoader;
-import org.apache.pulsar.common.naming.TopicName;
-import org.apache.pulsar.common.policies.data.ClusterData;
-import org.apache.pulsar.common.policies.data.TenantInfo;
-import org.apache.pulsar.functions.worker.WorkerConfig;
-import org.apache.pulsar.functions.worker.WorkerService;
-import org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble;
-import org.aspectj.weaver.loadtime.Agent;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.beust.jcommander.JCommander;
-import com.beust.jcommander.Parameter;
-import com.ea.agentloader.AgentLoader;
-import com.google.common.collect.Sets;
 
-public class PulsarStandaloneStarter {
-
-    PulsarService broker;
-    PulsarAdmin admin;
-    LocalBookkeeperEnsemble bkEnsemble;
-    ServiceConfiguration config;
-    WorkerService fnWorkerService;
-
-    @Parameter(names = { "-c", "--config" }, description = "Configuration file 
path", required = true)
-    private String configFile;
-
-    @Parameter(names = { "--wipe-data" }, description = "Clean up previous 
ZK/BK data")
-    private boolean wipeData = false;
-
-    @Parameter(names = { "--num-bookies" }, description = "Number of local 
Bookies")
-    private int numOfBk = 1;
-
-    @Parameter(names = { "--zookeeper-port" }, description = "Local 
zookeeper's port")
-    private int zkPort = 2181;
-
-    @Parameter(names = { "--bookkeeper-port" }, description = "Local bookies 
base port")
-    private int bkPort = 3181;
-
-    @Parameter(names = { "--zookeeper-dir" }, description = "Local zooKeeper's 
data directory")
-    private String zkDir = "data/standalone/zookeeper";
-
-    @Parameter(names = { "--bookkeeper-dir" }, description = "Local bookies 
base data directory")
-    private String bkDir = "data/standalone/bookkeeper";
-
-    @Parameter(names = { "--no-broker" }, description = "Only start ZK and BK 
services, no broker")
-    private boolean noBroker = false;
-
-    @Parameter(names = { "--only-broker" }, description = "Only start Pulsar 
broker service (no ZK, BK)")
-    private boolean onlyBroker = false;
-
-    @Parameter(names = {"-nfw", "--no-functions-worker"}, description = "Run 
functions worker with Broker")
-    private boolean noFunctionsWorker = false;
-
-    @Parameter(names = {"-fwc", "--functions-worker-conf"}, description = 
"Configuration file for Functions Worker")
-    private String fnWorkerConfigFile = 
Paths.get("").toAbsolutePath().normalize().toString() + 
"/conf/functions_worker.yml";
-
-    @Parameter(names = {"-nss", "--no-stream-storage"}, description = "Disable 
stream storage")
-    private boolean noStreamStorage = false;
-
-    @Parameter(names = { "--stream-storage-port" }, description = "Local 
bookies stream storage port")
-    private int streamStoragePort = 4181;
-
-    @Parameter(names = { "-a", "--advertised-address" }, description = 
"Standalone broker advertised address")
-    private String advertisedAddress = null;
-
-    @Parameter(names = { "-h", "--help" }, description = "Show this help 
message")
-    private boolean help = false;
+public class PulsarStandaloneStarter extends PulsarStandalone {
 
     private static final Logger log = 
LoggerFactory.getLogger(PulsarStandaloneStarter.class);
 
@@ -107,12 +40,12 @@ public PulsarStandaloneStarter(String[] args) throws 
Exception {
         try {
             jcommander.addObject(this);
             jcommander.parse(args);
-            if (help || isBlank(configFile)) {
+            if (this.isHelp() || isBlank(this.getConfigFile())) {
                 jcommander.usage();
                 return;
             }
 
-            if (noBroker && onlyBroker) {
+            if (this.isNoBroker() && this.isOnlyBroker()) {
                 log.error("Only one option is allowed between '--no-broker' 
and '--only-broker'");
                 jcommander.usage();
                 return;
@@ -122,14 +55,14 @@ public PulsarStandaloneStarter(String[] args) throws 
Exception {
             return;
         }
 
-        this.config = PulsarConfigurationLoader.create((new 
FileInputStream(configFile)), ServiceConfiguration.class);
+        this.config = PulsarConfigurationLoader.create((new 
FileInputStream(this.getConfigFile())), ServiceConfiguration.class);
 
         String zkServers = "127.0.0.1";
 
-        if (advertisedAddress != null) {
+        if (this.getAdvertisedAddress() != null) {
             // Use advertised address from command line
-            config.setAdvertisedAddress(advertisedAddress);
-            zkServers = advertisedAddress;
+            config.setAdvertisedAddress(this.getAdvertisedAddress());
+            zkServers = this.getAdvertisedAddress();
         } else if (isBlank(config.getAdvertisedAddress())) {
             // Use advertised address as local hostname
             
config.setAdvertisedAddress(ServiceConfigurationUtils.unsafeLocalhostResolve());
@@ -138,8 +71,8 @@ public PulsarStandaloneStarter(String[] args) throws 
Exception {
         }
 
         // Set ZK server's host to localhost
-        config.setZookeeperServers(zkServers + ":" + zkPort);
-        config.setConfigurationStoreServers(zkServers + ":" + zkPort);
+        config.setZookeeperServers(zkServers + ":" + this.getZkPort());
+        config.setConfigurationStoreServers(zkServers + ":" + 
this.getZkPort());
         config.setRunningStandalone(true);
 
         Runtime.getRuntime().addShutdownHook(new Thread() {
@@ -163,111 +96,6 @@ public void run() {
         });
     }
 
-    void start() throws Exception {
-
-        if (config == null) {
-            System.exit(1);
-        }
-
-        log.debug("--- setup PulsarStandaloneStarter ---");
-
-        // load aspectj-weaver agent for instrumentation
-        AgentLoader.loadAgentClass(Agent.class.getName(), null);
-
-        if (!onlyBroker) {
-            // Start LocalBookKeeper
-            bkEnsemble = new LocalBookkeeperEnsemble(
-                numOfBk, zkPort, bkPort, streamStoragePort, zkDir, bkDir, 
wipeData, config.getAdvertisedAddress());
-            bkEnsemble.startStandalone(!noStreamStorage);
-        }
-
-        if (noBroker) {
-            return;
-        }
-
-        // 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());
-            workerConfig.setStateStorageServiceUrl("bk://127.0.0.1:" + 
streamStoragePort);
-            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();
-
-        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 = 
PulsarAdmin.builder().serviceHttpUrl(webServiceUrl.toString()).authentication(
-                config.getBrokerClientAuthenticationPlugin(), 
config.getBrokerClientAuthenticationParameters()).build();
-
-        // Create a sample namespace
-        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 /* brokerServiceUrlTls */);
-            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.tenants().getTenants().contains(property)) {
-                admin.tenants().createTenant(property,
-                        new 
TenantInfo(Sets.newHashSet(config.getSuperUserRoles()), 
Sets.newHashSet(cluster)));
-            }
-
-            if 
(!admin.namespaces().getNamespaces(property).contains(namespace)) {
-                admin.namespaces().createNamespace(namespace);
-            }
-        } catch (PulsarAdminException e) {
-            log.info(e.getMessage());
-        }
-
-        // Create a public tenant and default namespace
-        final String publicTenant = TopicName.PUBLIC_TENANT;
-        final String defaultNamespace = TopicName.PUBLIC_TENANT + "/" + 
TopicName.DEFAULT_NAMESPACE;
-        try {
-            if (!admin.tenants().getTenants().contains(publicTenant)) {
-                admin.tenants().createTenant(publicTenant,
-                        new 
TenantInfo(Sets.newHashSet(config.getSuperUserRoles()), 
Sets.newHashSet(cluster)));
-            }
-            if 
(!admin.namespaces().getNamespaces(publicTenant).contains(defaultNamespace)) {
-                admin.namespaces().createNamespace(defaultNamespace);
-                
admin.namespaces().setNamespaceReplicationClusters(defaultNamespace, 
Sets.newHashSet(config.getClusterName()));
-            }
-        } catch (PulsarAdminException e) {
-            log.info(e.getMessage());
-        }
-
-        log.debug("--- setup completed ---");
-    }
-
     public static void main(String args[]) throws Exception {
         // Start standalone
         PulsarStandaloneStarter standalone = new PulsarStandaloneStarter(args);


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to