You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pegasus.apache.org by "empiredan (via GitHub)" <gi...@apache.org> on 2023/04/10 10:14:58 UTC

[GitHub] [incubator-pegasus] empiredan commented on a diff in pull request #1430: feat(FQDN): Implement of struct rpc_host_port

empiredan commented on code in PR #1430:
URL: https://github.com/apache/incubator-pegasus/pull/1430#discussion_r1161598953


##########
src/runtime/rpc/rpc_host_port.h:
##########
@@ -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.
+ */
+
+#pragma once
+
+// IWYU pragma: no_include <experimental/string_view>
+#include <stdint.h>
+#include <cstddef>
+#include <iosfwd>
+#include <string>
+
+#include "runtime/rpc/rpc_address.h"
+#include "utils/fmt_logging.h"
+
+namespace dsn {
+
+class host_port
+{
+public:
+    static const host_port s_invalid_host_port;
+    explicit host_port() = default;
+    explicit host_port(std::string host, uint16_t port);
+    explicit host_port(rpc_address addr);
+
+    host_port(const host_port &other) { *this = other; }
+    host_port &operator=(const host_port &other);
+
+    void reset();
+    ~host_port() { reset(); }
+
+    dsn_host_type_t type() const { return _type; }
+    const std::string &host() const { return _host; }
+    uint16_t port() const { return _port; }
+
+    bool is_invalid() const { return _type == HOST_TYPE_INVALID; }
+
+    std::string to_string() const;
+
+    friend std::ostream &operator<<(std::ostream &os, const host_port &hp)
+    {
+        return os << hp.to_string();
+    }
+
+private:
+    std::string _host;
+    uint16_t _port = 0;
+    dsn_host_type_t _type = HOST_TYPE_INVALID;
+};
+
+inline bool operator<(const host_port &hp1, const host_port &hp2)

Review Comment:
   When we would use `operator<` ?



##########
src/runtime/rpc/rpc_host_port.cpp:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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 "fmt/core.h"
+#include "runtime/rpc/rpc_host_port.h"
+#include "utils/utils.h"
+
+#include <utility>
+
+namespace dsn {
+
+const host_port host_port::s_invalid_host_port;
+
+host_port::host_port(std::string host, uint16_t port)
+    : _host(std::move(host)), _port(port), _type(HOST_TYPE_IPV4)
+{
+    uint32_t ip = rpc_address::ipv4_from_host(_host.c_str());
+    CHECK_NE_MSG(ip, 0, "invalid hostname: {}", _host);

Review Comment:
   ```suggestion
       CHECK_NE_MSG(rpc_address::ipv4_from_host(_host.c_str()), 0, "invalid hostname: {}", _host);
   ```



##########
src/runtime/test/host_port_test.cpp:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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 <gtest/gtest.h>
+#include <gtest/gtest-message.h>
+#include <gtest/gtest-test-part.h>
+#include <string>
+
+#include "runtime/rpc/rpc_address.h"
+#include "runtime/rpc/rpc_host_port.h"
+
+namespace dsn {
+
+TEST(host_port_test, host_port_to_string)
+{
+    {
+        host_port hp = host_port("localhost", 8080);
+        ASSERT_EQ(std::string("localhost:8080"), hp.to_string());
+    }
+
+    {
+        host_port hp;
+        ASSERT_EQ(std::string("invalid address"), hp.to_string());
+    }
+}
+
+TEST(host_port_test, host_port_build)
+{
+    host_port hp = host_port("localhost", 8080);
+    ASSERT_EQ(HOST_TYPE_IPV4, hp.type());
+    ASSERT_EQ(8080, hp.port());
+    ASSERT_EQ("localhost", hp.host());
+}
+
+TEST(host_port_test, operators)
+{
+    host_port hp("localhost", 8080);
+    ASSERT_EQ(hp, hp);
+
+    {
+        host_port new_hp(hp);
+        ASSERT_EQ(hp, new_hp);
+    }
+
+    {
+        host_port new_hp("localhost", 8081);
+        ASSERT_NE(hp, new_hp);
+    }
+
+    host_port hp2;
+    ASSERT_NE(hp, hp2);
+    ASSERT_FALSE(hp.is_invalid());
+    ASSERT_TRUE(hp2.is_invalid());

Review Comment:
   Is it necessary to test `operator<` ?



##########
src/runtime/test/host_port_test.cpp:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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 <gtest/gtest.h>
+#include <gtest/gtest-message.h>
+#include <gtest/gtest-test-part.h>
+#include <string>
+
+#include "runtime/rpc/rpc_address.h"
+#include "runtime/rpc/rpc_host_port.h"
+
+namespace dsn {
+
+TEST(host_port_test, host_port_to_string)
+{
+    {
+        host_port hp = host_port("localhost", 8080);
+        ASSERT_EQ(std::string("localhost:8080"), hp.to_string());
+    }
+
+    {
+        host_port hp;
+        ASSERT_EQ(std::string("invalid address"), hp.to_string());
+    }
+}
+
+TEST(host_port_test, host_port_build)
+{
+    host_port hp = host_port("localhost", 8080);
+    ASSERT_EQ(HOST_TYPE_IPV4, hp.type());
+    ASSERT_EQ(8080, hp.port());
+    ASSERT_EQ("localhost", hp.host());
+}
+
+TEST(host_port_test, operators)
+{
+    host_port hp("localhost", 8080);
+    ASSERT_EQ(hp, hp);

Review Comment:
   Could add a case that compare with another object which is equal.



##########
src/runtime/test/host_port_test.cpp:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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 <gtest/gtest.h>
+#include <gtest/gtest-message.h>
+#include <gtest/gtest-test-part.h>
+#include <string>
+
+#include "runtime/rpc/rpc_address.h"
+#include "runtime/rpc/rpc_host_port.h"
+
+namespace dsn {
+
+TEST(host_port_test, host_port_to_string)
+{
+    {
+        host_port hp = host_port("localhost", 8080);
+        ASSERT_EQ(std::string("localhost:8080"), hp.to_string());
+    }
+
+    {
+        host_port hp;
+        ASSERT_EQ(std::string("invalid address"), hp.to_string());
+    }
+}
+
+TEST(host_port_test, host_port_build)
+{
+    host_port hp = host_port("localhost", 8080);
+    ASSERT_EQ(HOST_TYPE_IPV4, hp.type());
+    ASSERT_EQ(8080, hp.port());
+    ASSERT_EQ("localhost", hp.host());

Review Comment:
   Could add a case to test ``host_port(rpc_address addr)`, and another case to test default object whose type is invalid.



##########
src/runtime/rpc/rpc_host_port.h:
##########
@@ -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.
+ */
+
+#pragma once
+
+// IWYU pragma: no_include <experimental/string_view>
+#include <stdint.h>
+#include <cstddef>
+#include <iosfwd>
+#include <string>
+
+#include "runtime/rpc/rpc_address.h"
+#include "utils/fmt_logging.h"
+
+namespace dsn {
+
+class host_port
+{
+public:
+    static const host_port s_invalid_host_port;
+    explicit host_port() = default;
+    explicit host_port(std::string host, uint16_t port);
+    explicit host_port(rpc_address addr);
+
+    host_port(const host_port &other) { *this = other; }
+    host_port &operator=(const host_port &other);
+
+    void reset();
+    ~host_port() { reset(); }
+
+    dsn_host_type_t type() const { return _type; }
+    const std::string &host() const { return _host; }
+    uint16_t port() const { return _port; }
+
+    bool is_invalid() const { return _type == HOST_TYPE_INVALID; }
+
+    std::string to_string() const;
+
+    friend std::ostream &operator<<(std::ostream &os, const host_port &hp)
+    {
+        return os << hp.to_string();
+    }
+
+private:
+    std::string _host;
+    uint16_t _port = 0;
+    dsn_host_type_t _type = HOST_TYPE_INVALID;
+};
+
+inline bool operator<(const host_port &hp1, const host_port &hp2)
+{
+    if (hp1.type() != hp2.type()) {
+        return hp1.type() < hp2.type();
+    }
+
+    switch (hp1.type()) {
+    case HOST_TYPE_IPV4: {
+        int ret = hp1.host().compare(hp2.host());
+        return ret < 0 || (ret == 0 && hp1.port() < hp2.port());
+    }
+    case HOST_TYPE_GROUP:
+        CHECK(false, "type HOST_TYPE_GROUP not support!");
+    default:
+        return true;
+    }
+}
+
+inline bool operator==(const host_port &hp1, const host_port &hp2)
+{
+    if (&hp1 == &hp2) {
+        return true;
+    }
+
+    if (hp1.type() != hp2.type()) {
+        return false;
+    }
+
+    switch (hp1.type()) {
+    case HOST_TYPE_IPV4:
+        return hp1.host() == hp2.host() && hp1.port() == hp2.port();
+    case HOST_TYPE_GROUP:
+        CHECK(false, "type HOST_TYPE_GROUP not support!");
+    default:
+        return true;
+    }
+}
+
+inline bool operator!=(const host_port &hp1, const host_port &hp2) { return !(hp1 == hp2); }
+
+} // namespace dsn
+
+namespace std {
+template <>
+struct hash<::dsn::host_port>
+{
+    size_t operator()(const ::dsn::host_port &hp) const
+    {
+        switch (hp.type()) {
+        case HOST_TYPE_IPV4:
+            return std::hash<std::string>()(hp.host()) ^ hp.port();

Review Comment:
   ```suggestion
               return std::hash<std::string>()(hp.host()) ^ std::hash<uint16_t>()(hp.port());
   ```



##########
src/runtime/rpc/rpc_host_port.cpp:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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 "fmt/core.h"
+#include "runtime/rpc/rpc_host_port.h"
+#include "utils/utils.h"
+
+#include <utility>
+
+namespace dsn {
+
+const host_port host_port::s_invalid_host_port;
+
+host_port::host_port(std::string host, uint16_t port)
+    : _host(std::move(host)), _port(port), _type(HOST_TYPE_IPV4)
+{
+    uint32_t ip = rpc_address::ipv4_from_host(_host.c_str());
+    CHECK_NE_MSG(ip, 0, "invalid hostname: {}", _host);
+}
+
+host_port::host_port(rpc_address addr)
+{
+    switch (addr.type()) {
+    case HOST_TYPE_IPV4: {
+        std::string hostname;
+        CHECK(utils::hostname_from_ip(addr.ipv4_str(), &hostname),

Review Comment:
   Could we use `hostname_from_ip(htonl(addr.ip()), std::string *)` instead ?  Since `hostname_from_ip(addr.ipv4_str(), std::string *)` will do `uint32_t -> const char * -> uint32_t` conversion, while another just do `uint32_t -> uint32_t` conversion.
   ```suggestion
           CHECK(utils::hostname_from_ip(htonl(addr.ip()), &_host),
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org