This is an automated email from the ASF dual-hosted git repository.

technoboy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new ad96be06796 [fix][broker] Create "standalone" cluster when starting 
standalone server (#15583)
ad96be06796 is described below

commit ad96be06796f42715770bc0248e3fd9154e1c284
Author: Masahiro Sakamoto <[email protected]>
AuthorDate: Sun May 15 10:09:23 2022 +0900

    [fix][broker] Create "standalone" cluster when starting standalone server 
(#15583)
---
 .../java/org/apache/pulsar/PulsarStandalone.java   | 17 ++++-
 .../org/apache/pulsar/PulsarStandaloneTest.java    | 85 ++++++++++++++++++++++
 pulsar-client-cpp/pulsar-test-service-start.sh     | 15 ++--
 3 files changed, 109 insertions(+), 8 deletions(-)

diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/PulsarStandalone.java 
b/pulsar-broker/src/main/java/org/apache/pulsar/PulsarStandalone.java
index 085ce94b9f3..8f12d806178 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/PulsarStandalone.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/PulsarStandalone.java
@@ -21,6 +21,7 @@ package org.apache.pulsar;
 import static org.apache.pulsar.common.naming.NamespaceName.SYSTEM_NAMESPACE;
 import static 
org.apache.pulsar.common.naming.SystemTopicNames.TRANSACTION_COORDINATOR_ASSIGN;
 import com.beust.jcommander.Parameter;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.Sets;
 import java.io.File;
 import java.nio.file.Paths;
@@ -30,12 +31,14 @@ import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.logging.log4j.LogManager;
 import org.apache.pulsar.broker.PulsarService;
 import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.resources.ClusterResources;
 import org.apache.pulsar.broker.resources.NamespaceResources;
 import org.apache.pulsar.broker.resources.TenantResources;
 import org.apache.pulsar.client.admin.PulsarAdmin;
 import org.apache.pulsar.common.naming.NamespaceName;
 import org.apache.pulsar.common.naming.TopicName;
 import org.apache.pulsar.common.partition.PartitionedTopicMetadata;
+import org.apache.pulsar.common.policies.data.ClusterData;
 import org.apache.pulsar.common.policies.data.Policies;
 import org.apache.pulsar.common.policies.data.TenantInfo;
 import org.apache.pulsar.functions.worker.WorkerConfig;
@@ -320,10 +323,22 @@ public class PulsarStandalone implements AutoCloseable {
         log.debug("--- setup completed ---");
     }
 
-    private void createNameSpace(String cluster, String publicTenant, 
NamespaceName ns) throws Exception {
+    @VisibleForTesting
+    void createNameSpace(String cluster, String publicTenant, NamespaceName 
ns) throws Exception {
+        ClusterResources cr = 
broker.getPulsarResources().getClusterResources();
         TenantResources tr = broker.getPulsarResources().getTenantResources();
         NamespaceResources nsr = 
broker.getPulsarResources().getNamespaceResources();
 
+        if (!cr.clusterExists(cluster)) {
+            cr.createCluster(cluster,
+                    ClusterData.builder()
+                            .serviceUrl(broker.getWebServiceAddress())
+                            .serviceUrlTls(broker.getWebServiceAddressTls())
+                            .brokerServiceUrl(broker.getBrokerServiceUrl())
+                            
.brokerServiceUrlTls(broker.getBrokerServiceUrlTls())
+                            .build());
+        }
+
         if (!tr.tenantExists(publicTenant)) {
             tr.createTenant(publicTenant,
                     TenantInfo.builder()
diff --git 
a/pulsar-broker/src/test/java/org/apache/pulsar/PulsarStandaloneTest.java 
b/pulsar-broker/src/test/java/org/apache/pulsar/PulsarStandaloneTest.java
new file mode 100644
index 00000000000..b7b62eccb51
--- /dev/null
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/PulsarStandaloneTest.java
@@ -0,0 +1,85 @@
+/**
+ * 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 static org.mockito.Mockito.any;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.apache.pulsar.common.naming.NamespaceName;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.resources.ClusterResources;
+import org.apache.pulsar.broker.resources.NamespaceResources;
+import org.apache.pulsar.broker.resources.PulsarResources;
+import org.apache.pulsar.broker.resources.TenantResources;
+import org.testng.annotations.Test;
+
+@Test(groups = "broker")
+public class PulsarStandaloneTest {
+
+    @Test
+    public void testCreateNameSpace() throws Exception {
+        final String cluster = "cluster1";
+        final String tenant = "tenant1";
+        final NamespaceName ns = NamespaceName.get(tenant, "ns1");
+
+        ClusterResources cr = mock(ClusterResources.class);
+        when(cr.clusterExists(cluster)).thenReturn(false).thenReturn(true);
+        doNothing().when(cr).createCluster(eq(cluster), any());
+
+        TenantResources tr = mock(TenantResources.class);
+        when(tr.tenantExists(tenant)).thenReturn(false).thenReturn(true);
+        doNothing().when(tr).createTenant(eq(tenant), any());
+
+        NamespaceResources nsr = mock(NamespaceResources.class);
+        when(nsr.namespaceExists(ns)).thenReturn(false).thenReturn(true);
+        doNothing().when(nsr).createPolicies(eq(ns), any());
+
+        PulsarResources resources = mock(PulsarResources.class);
+        when(resources.getClusterResources()).thenReturn(cr);
+        when(resources.getTenantResources()).thenReturn(tr);
+        when(resources.getNamespaceResources()).thenReturn(nsr);
+
+        PulsarService broker = mock(PulsarService.class);
+        when(broker.getPulsarResources()).thenReturn(resources);
+        
when(broker.getWebServiceAddress()).thenReturn("pulsar://localhost:8080");
+        when(broker.getWebServiceAddressTls()).thenReturn(null);
+        
when(broker.getBrokerServiceUrl()).thenReturn("pulsar://localhost:6650");
+        when(broker.getBrokerServiceUrlTls()).thenReturn(null);
+
+        ServiceConfiguration config = new ServiceConfiguration();
+        config.setClusterName(cluster);
+
+        PulsarStandalone standalone = new PulsarStandalone();
+        standalone.setBroker(broker);
+        standalone.setConfig(config);
+
+        standalone.createNameSpace(cluster, tenant, ns);
+        standalone.createNameSpace(cluster, tenant, ns);
+        verify(cr, times(1)).createCluster(eq(cluster), any());
+        verify(tr, times(1)).createTenant(eq(tenant), any());
+        verify(nsr, times(1)).createPolicies(eq(ns), any());
+    }
+
+}
diff --git a/pulsar-client-cpp/pulsar-test-service-start.sh 
b/pulsar-client-cpp/pulsar-test-service-start.sh
index 928f72b4a4b..75aa6ef43cc 100755
--- a/pulsar-client-cpp/pulsar-test-service-start.sh
+++ b/pulsar-client-cpp/pulsar-test-service-start.sh
@@ -66,13 +66,14 @@ echo "-- Pulsar service is ready -- Configure permissions"
 
 export PULSAR_CLIENT_CONF=$SRC_DIR/pulsar-client-cpp/test-conf/client-ssl.conf
 
-# Create "standalone" cluster
-$PULSAR_DIR/bin/pulsar-admin clusters create \
-        standalone \
-        --url http://localhost:8080/ \
-        --url-secure https://localhost:8443/ \
-        --broker-url pulsar://localhost:6650/ \
-        --broker-url-secure pulsar+ssl://localhost:6651/
+# Create "standalone" cluster if it does not exist
+$PULSAR_DIR/bin/pulsar-admin clusters list | grep -q '^standalone$' ||
+        $PULSAR_DIR/bin/pulsar-admin clusters create \
+                standalone \
+                --url http://localhost:8080/ \
+                --url-secure https://localhost:8443/ \
+                --broker-url pulsar://localhost:6650/ \
+                --broker-url-secure pulsar+ssl://localhost:6651/
 
 # Update "public" tenant
 $PULSAR_DIR/bin/pulsar-admin tenants update public -r "anonymous" -c 
"standalone"

Reply via email to