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

lizhanhui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git


The following commit(s) were added to refs/heads/master by this push:
     new 920c9ae9 feat: enhance StaticNameServerResolver to allow use of gRPC 
naming sc… (#617)
920c9ae9 is described below

commit 920c9ae91f267fd2e859f041f214045fbd6d84bf
Author: Zhanhui Li <[email protected]>
AuthorDate: Sun Oct 8 19:11:40 2023 +0800

    feat: enhance StaticNameServerResolver to allow use of gRPC naming sc… 
(#617)
    
    * feat: enhance StaticNameServerResolver to allow use of gRPC naming scheme 
compliant endpoints
    
    Signed-off-by: Li Zhanhui <[email protected]>
    
    * fix: fix separator issue
    
    Signed-off-by: Li Zhanhui <[email protected]>
    
    ---------
    
    Signed-off-by: Li Zhanhui <[email protected]>
---
 cpp/source/rocketmq/ClientImpl.cpp                 |  2 +-
 cpp/source/rocketmq/NamingScheme.cpp               | 18 ++++++-
 cpp/source/rocketmq/StaticNameServerResolver.cpp   |  9 +++-
 cpp/source/rocketmq/include/NamingScheme.h         |  2 +
 cpp/source/rocketmq/tests/BUILD.bazel              |  8 +++
 .../tests/StaticNameServerResolverTest.cpp         | 58 ++++++++++++++++++++++
 6 files changed, 94 insertions(+), 3 deletions(-)

diff --git a/cpp/source/rocketmq/ClientImpl.cpp 
b/cpp/source/rocketmq/ClientImpl.cpp
index e2f401e3..5be559b6 100644
--- a/cpp/source/rocketmq/ClientImpl.cpp
+++ b/cpp/source/rocketmq/ClientImpl.cpp
@@ -70,7 +70,7 @@ rmq::Endpoints ClientImpl::accessPoint() {
     host_port = absl::StripPrefix(endpoints, NamingScheme::DnsPrefix);
   }
 
-  std::vector<std::string> pairs = absl::StrSplit(host_port, ';', 
absl::SkipWhitespace());
+  std::vector<std::string> pairs = absl::StrSplit(host_port, 
absl::ByAnyChar(";,"), absl::SkipWhitespace());
   // Now endpoint is in form of host:port
   for (auto& endpoint : pairs) {
     std::reverse(endpoint.begin(), endpoint.end());
diff --git a/cpp/source/rocketmq/NamingScheme.cpp 
b/cpp/source/rocketmq/NamingScheme.cpp
index 467e295b..10757bf4 100644
--- a/cpp/source/rocketmq/NamingScheme.cpp
+++ b/cpp/source/rocketmq/NamingScheme.cpp
@@ -83,7 +83,23 @@ std::string NamingScheme::buildAddress(const 
std::vector<std::string>& list) {
   if (!ipv6.empty()) {
     return "ipv6:" + absl::StrJoin(ipv4, ",", absl::PairFormatter(":"));
   }
-  return std::string();
+  return {};
+}
+
+bool NamingScheme::accept(absl::string_view endpoint) {
+  if (absl::StartsWith(endpoint, DnsPrefix)) {
+    return true;
+  }
+
+  if (absl::StartsWith(endpoint, IPv4Prefix)) {
+    return true;
+  }
+  
+  if (absl::StartsWith(endpoint, IPv6Prefix)) {
+    return true;
+  }
+
+  return false;
 }
 
 ROCKETMQ_NAMESPACE_END
\ No newline at end of file
diff --git a/cpp/source/rocketmq/StaticNameServerResolver.cpp 
b/cpp/source/rocketmq/StaticNameServerResolver.cpp
index 8f2d2556..eaa20cbe 100644
--- a/cpp/source/rocketmq/StaticNameServerResolver.cpp
+++ b/cpp/source/rocketmq/StaticNameServerResolver.cpp
@@ -24,7 +24,14 @@
 ROCKETMQ_NAMESPACE_BEGIN
 
 StaticNameServerResolver::StaticNameServerResolver(absl::string_view 
name_server_list) {
-  std::vector<std::string> segments = absl::StrSplit(name_server_list, ';');
+  // If the given endpoint already follows gRPC naming scheme, 
https://github.com/grpc/grpc/blob/master/doc/naming.md,
+  // We should use it directly.
+  if (naming_scheme_.accept(name_server_list)) {
+    name_server_address_ = std::string(name_server_list.data(), 
name_server_list.size());
+    return;
+  }
+
+  std::vector<std::string> segments = absl::StrSplit(name_server_list, 
absl::ByAnyChar(",;"));
   name_server_address_ = naming_scheme_.buildAddress(segments);
   if (name_server_address_.empty()) {
     SPDLOG_WARN("Failed to create gRPC naming scheme compliant address from 
{}",
diff --git a/cpp/source/rocketmq/include/NamingScheme.h 
b/cpp/source/rocketmq/include/NamingScheme.h
index 433aef45..4db3b3cd 100644
--- a/cpp/source/rocketmq/include/NamingScheme.h
+++ b/cpp/source/rocketmq/include/NamingScheme.h
@@ -31,6 +31,8 @@ public:
 
   std::string buildAddress(const std::vector<std::string>& list);
 
+  bool accept(absl::string_view endpoint);
+
   static const char* DnsPrefix;
   static const char* IPv4Prefix;
   static const char* IPv6Prefix;
diff --git a/cpp/source/rocketmq/tests/BUILD.bazel 
b/cpp/source/rocketmq/tests/BUILD.bazel
index 25303d7c..99f2fb2a 100644
--- a/cpp/source/rocketmq/tests/BUILD.bazel
+++ b/cpp/source/rocketmq/tests/BUILD.bazel
@@ -43,4 +43,12 @@ cc_test(
         "ClientImplTest.cpp",
     ],
     deps = base_deps,
+)
+
+cc_test(
+    name = "static_name_server_resolver_test",
+    srcs = [
+        "StaticNameServerResolverTest.cpp",
+    ],
+    deps = base_deps,
 )
\ No newline at end of file
diff --git a/cpp/source/rocketmq/tests/StaticNameServerResolverTest.cpp 
b/cpp/source/rocketmq/tests/StaticNameServerResolverTest.cpp
new file mode 100644
index 00000000..11ddb14a
--- /dev/null
+++ b/cpp/source/rocketmq/tests/StaticNameServerResolverTest.cpp
@@ -0,0 +1,58 @@
+/*
+ * 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.
+ */
+
+#include <tuple>
+#include <vector>
+
+#include "NamingScheme.h"
+#include "StaticNameServerResolver.h"
+#include "absl/strings/str_join.h"
+#include "absl/strings/str_split.h"
+#include "gtest/gtest.h"
+#include "rocketmq/RocketMQ.h"
+
+ROCKETMQ_NAMESPACE_BEGIN
+
+TEST(StaticNameServerResolverTest, testCtor) {
+  std::string addr1 = "127.0.0.1:9876";
+  std::string addr2 = "10.0.0.1:9876";
+  std::string endpoint = absl::StrJoin(std::vector<std::string>{addr1, addr2}, 
";");
+
+  StaticNameServerResolver resolver(endpoint);
+  std::string result = resolver.resolve();
+
+  ASSERT_TRUE(absl::StartsWith(result, NamingScheme::IPv4Prefix));
+  ASSERT_TRUE(absl::StrContains(result, addr1));
+  ASSERT_TRUE(absl::StrContains(result, addr2));
+
+  std::string endpoint2 = absl::StrJoin(std::vector<std::string>{addr1, 
addr2}, ",");
+  StaticNameServerResolver resolver2(endpoint2);
+  std::string result2 = resolver2.resolve();
+  ASSERT_TRUE(absl::StartsWith(result2, NamingScheme::IPv4Prefix));
+  ASSERT_TRUE(absl::StrContains(result2, addr1));
+  ASSERT_TRUE(absl::StrContains(result2, addr2));
+
+  std::string grpc_naming_endpoint = "ipv4:127.0.0.1:9876,10.0.0.1:9876";
+  StaticNameServerResolver resolver3(grpc_naming_endpoint);
+  ASSERT_EQ(grpc_naming_endpoint, resolver3.resolve());
+
+  std::string grpc_naming_endpoint2 = "dns:localhost:9876";
+  StaticNameServerResolver resolver4(grpc_naming_endpoint2);
+  ASSERT_EQ(grpc_naming_endpoint2, resolver4.resolve());
+}
+
+ROCKETMQ_NAMESPACE_END
\ No newline at end of file

Reply via email to