yifan-c commented on code in PR #575:
URL:
https://github.com/apache/cassandra-cpp-driver/pull/575#discussion_r2944627185
##########
src/cluster_config.cpp:
##########
@@ -295,11 +296,13 @@ CassError
cass_cluster_set_load_balance_dc_aware_n(CassCluster* cluster, const c
size_t local_dc_length,
unsigned
used_hosts_per_remote_dc,
cass_bool_t
allow_remote_dcs_for_local_cl) {
- if (local_dc == NULL || local_dc_length == 0) {
- return CASS_ERROR_LIB_BAD_PARAMS;
- }
+ // If local_dc is NULL or length is 0, we use an empty string which causes
the driver
+ // to use the DC of the first connected node
+ String dc_name = (local_dc != NULL && local_dc_length > 0) ?
+ String(local_dc, local_dc_length) : String();
+
Review Comment:
Since now `cass_cluster_set_load_balance_dc_aware_n` accepts NULL
`local_dc`, can we remove the duplicated NULL check?
For example,
```c++
CassError cass_cluster_set_load_balance_dc_aware(CassCluster* cluster, const
char* local_dc,
unsigned
used_hosts_per_remote_dc,
cass_bool_t
allow_remote_dcs_for_local_cl) {
return cass_cluster_set_load_balance_dc_aware_n(cluster, local_dc,
SAFE_STRLEN(local_dc),
used_hosts_per_remote_dc,
allow_remote_dcs_for_local_cl);
}
CassError cass_cluster_set_load_balance_dc_aware_n(CassCluster* cluster,
const char* local_dc,
size_t local_dc_length,
unsigned
used_hosts_per_remote_dc,
cass_bool_t
allow_remote_dcs_for_local_cl) {
// NULL or zero-length local_dc means auto-detect from the first connected
node
cluster->config().set_load_balancing_policy(new DCAwarePolicy(
String(local_dc != NULL && local_dc_length > 0 ? local_dc : "",
local_dc_length),
used_hosts_per_remote_dc, !allow_remote_dcs_for_local_cl));
return CASS_OK;
}
```
##########
tests/src/unit/tests/test_cluster_config.cpp:
##########
@@ -0,0 +1,163 @@
+/*
+ Copyright (c) DataStax, Inc.
+
+ Licensed 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 "cluster_config.hpp"
+#include "cassandra.h"
+#include "dc_aware_policy.hpp"
+#include "string.hpp"
+
+using namespace datastax;
+using namespace datastax::internal;
+using namespace datastax::internal::core;
+
+class ClusterConfigUnitTest : public testing::Test {
+public:
+ ClusterConfigUnitTest() {}
+
+ virtual void SetUp() {
+ cluster_ = cass_cluster_new();
+ }
+
+ virtual void TearDown() {
+ cass_cluster_free(cluster_);
+ }
+
+private:
+
+protected:
+ CassCluster* cluster_;
+
+ const DCAwarePolicy* build_dc_policy() {
+
+ // Disable token-aware routing in this case in order to avoid illegal
instructions
+ // when creating a TokenAwarePolicy in build_load_balancing_policy()
+ cluster_->config().set_token_aware_routing(false);
+ cluster_->config().default_profile().build_load_balancing_policy();
+
+ // Verify the policy was set correctly with the right datacenter name
+ const LoadBalancingPolicy* policy =
+ cluster_->config().load_balancing_policy().get();
+ return static_cast<const DCAwarePolicy*>(policy);
+ }
+};
+
+// ==================== cass_cluster_set_load_balance_dc_aware_n
====================
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNHappyPath) {
+
+ // Test valid parameters
+ const char* valid_dc = "my_datacenter";
+ EXPECT_EQ(CASS_OK, cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, valid_dc, strlen(valid_dc),
+ 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ // Should be using the partial string as local DC
+ EXPECT_EQ(dc_policy->local_dc(), valid_dc);
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNWithNullLocalDc) {
+
+ // Test with NULL pointer (should now succeed and use empty string for DC)
+ EXPECT_EQ(CASS_OK,
+ cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, NULL, 10, 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ EXPECT_EQ(dc_policy->local_dc(), String());
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNWithZeroLengthLocalDc) {
+
+ // Test with zero length (should now succeed and use empty string for DC)
+ const char* valid_dc = "my_datacenter";
+ EXPECT_EQ(CASS_OK,
+ cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, valid_dc, 0, 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ EXPECT_EQ(dc_policy->local_dc(), String());
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNWithEmptyLocalDc) {
+
+ // Test with empty string (should now succeed)
+ const char* empty_string = "";
+ EXPECT_EQ(CASS_OK,
+ cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, empty_string, strlen(empty_string), 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ EXPECT_EQ(dc_policy->local_dc(), String());
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNWithPartialStringLocalDc) {
+
+ // Test with partial string length
+ const char* long_dc_name = "my_datacenter_with_a_long_name";
+ size_t partial_length = 5; // Should just use "my_da" as the datacenter name
+ EXPECT_EQ(CASS_OK, cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, long_dc_name, partial_length,
+ 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ // Should be using the partial string as local DC
+ EXPECT_EQ(dc_policy->local_dc(), String(long_dc_name, partial_length));
+ EXPECT_EQ(dc_policy->local_dc(), String("my_da"));
Review Comment:
Are they identical?
##########
tests/src/unit/tests/test_cluster_config.cpp:
##########
@@ -0,0 +1,163 @@
+/*
+ Copyright (c) DataStax, Inc.
+
+ Licensed 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 "cluster_config.hpp"
+#include "cassandra.h"
+#include "dc_aware_policy.hpp"
+#include "string.hpp"
+
+using namespace datastax;
+using namespace datastax::internal;
+using namespace datastax::internal::core;
+
+class ClusterConfigUnitTest : public testing::Test {
+public:
+ ClusterConfigUnitTest() {}
+
+ virtual void SetUp() {
+ cluster_ = cass_cluster_new();
+ }
+
+ virtual void TearDown() {
+ cass_cluster_free(cluster_);
+ }
+
+private:
+
+protected:
+ CassCluster* cluster_;
+
+ const DCAwarePolicy* build_dc_policy() {
+
+ // Disable token-aware routing in this case in order to avoid illegal
instructions
+ // when creating a TokenAwarePolicy in build_load_balancing_policy()
+ cluster_->config().set_token_aware_routing(false);
+ cluster_->config().default_profile().build_load_balancing_policy();
+
+ // Verify the policy was set correctly with the right datacenter name
+ const LoadBalancingPolicy* policy =
+ cluster_->config().load_balancing_policy().get();
+ return static_cast<const DCAwarePolicy*>(policy);
+ }
+};
+
+// ==================== cass_cluster_set_load_balance_dc_aware_n
====================
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNHappyPath) {
+
+ // Test valid parameters
+ const char* valid_dc = "my_datacenter";
+ EXPECT_EQ(CASS_OK, cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, valid_dc, strlen(valid_dc),
+ 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ // Should be using the partial string as local DC
+ EXPECT_EQ(dc_policy->local_dc(), valid_dc);
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNWithNullLocalDc) {
+
+ // Test with NULL pointer (should now succeed and use empty string for DC)
+ EXPECT_EQ(CASS_OK,
+ cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, NULL, 10, 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ EXPECT_EQ(dc_policy->local_dc(), String());
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNWithZeroLengthLocalDc) {
+
+ // Test with zero length (should now succeed and use empty string for DC)
+ const char* valid_dc = "my_datacenter";
+ EXPECT_EQ(CASS_OK,
+ cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, valid_dc, 0, 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ EXPECT_EQ(dc_policy->local_dc(), String());
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNWithEmptyLocalDc) {
+
+ // Test with empty string (should now succeed)
+ const char* empty_string = "";
+ EXPECT_EQ(CASS_OK,
+ cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, empty_string, strlen(empty_string), 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ EXPECT_EQ(dc_policy->local_dc(), String());
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNWithPartialStringLocalDc) {
+
+ // Test with partial string length
+ const char* long_dc_name = "my_datacenter_with_a_long_name";
+ size_t partial_length = 5; // Should just use "my_da" as the datacenter name
+ EXPECT_EQ(CASS_OK, cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, long_dc_name, partial_length,
+ 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ // Should be using the partial string as local DC
+ EXPECT_EQ(dc_policy->local_dc(), String(long_dc_name, partial_length));
+ EXPECT_EQ(dc_policy->local_dc(), String("my_da"));
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+// ==================== cass_cluster_set_load_balance_dc_aware
====================
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareWithNullLocalDc) {
+ // Test with NULL to use local DC from connected node
+ EXPECT_EQ(CASS_OK, cass_cluster_set_load_balance_dc_aware(
+ cluster_, NULL, 3, cass_false));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ // Should be using empty string as local DC (will be determined at runtime)
+ EXPECT_EQ(dc_policy->local_dc(), String());
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 3u);
+ EXPECT_TRUE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareWithEmptyLocalDc) {
+ // Test with empty string to use local DC from connected node
Review Comment:
nit: 4 space indentation, should probably be 2 to be consistent with other
comments.
##########
tests/src/unit/tests/test_cluster_config.cpp:
##########
@@ -0,0 +1,163 @@
+/*
+ Copyright (c) DataStax, Inc.
+
+ Licensed 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 "cluster_config.hpp"
+#include "cassandra.h"
+#include "dc_aware_policy.hpp"
+#include "string.hpp"
+
+using namespace datastax;
+using namespace datastax::internal;
+using namespace datastax::internal::core;
+
+class ClusterConfigUnitTest : public testing::Test {
+public:
+ ClusterConfigUnitTest() {}
+
+ virtual void SetUp() {
+ cluster_ = cass_cluster_new();
+ }
+
+ virtual void TearDown() {
+ cass_cluster_free(cluster_);
+ }
+
+private:
Review Comment:
Is it redundant?
##########
tests/src/unit/tests/test_cluster_config.cpp:
##########
@@ -0,0 +1,163 @@
+/*
+ Copyright (c) DataStax, Inc.
+
+ Licensed 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 "cluster_config.hpp"
+#include "cassandra.h"
+#include "dc_aware_policy.hpp"
+#include "string.hpp"
+
+using namespace datastax;
+using namespace datastax::internal;
+using namespace datastax::internal::core;
+
+class ClusterConfigUnitTest : public testing::Test {
+public:
+ ClusterConfigUnitTest() {}
+
+ virtual void SetUp() {
+ cluster_ = cass_cluster_new();
+ }
+
+ virtual void TearDown() {
+ cass_cluster_free(cluster_);
+ }
+
+private:
+
+protected:
+ CassCluster* cluster_;
+
+ const DCAwarePolicy* build_dc_policy() {
+
+ // Disable token-aware routing in this case in order to avoid illegal
instructions
+ // when creating a TokenAwarePolicy in build_load_balancing_policy()
+ cluster_->config().set_token_aware_routing(false);
+ cluster_->config().default_profile().build_load_balancing_policy();
+
+ // Verify the policy was set correctly with the right datacenter name
+ const LoadBalancingPolicy* policy =
+ cluster_->config().load_balancing_policy().get();
+ return static_cast<const DCAwarePolicy*>(policy);
+ }
+};
+
+// ==================== cass_cluster_set_load_balance_dc_aware_n
====================
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNHappyPath) {
+
+ // Test valid parameters
+ const char* valid_dc = "my_datacenter";
+ EXPECT_EQ(CASS_OK, cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, valid_dc, strlen(valid_dc),
+ 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ // Should be using the partial string as local DC
+ EXPECT_EQ(dc_policy->local_dc(), valid_dc);
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNWithNullLocalDc) {
+
+ // Test with NULL pointer (should now succeed and use empty string for DC)
+ EXPECT_EQ(CASS_OK,
+ cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, NULL, 10, 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ EXPECT_EQ(dc_policy->local_dc(), String());
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNWithZeroLengthLocalDc) {
+
+ // Test with zero length (should now succeed and use empty string for DC)
+ const char* valid_dc = "my_datacenter";
+ EXPECT_EQ(CASS_OK,
+ cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, valid_dc, 0, 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ EXPECT_EQ(dc_policy->local_dc(), String());
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNWithEmptyLocalDc) {
+
+ // Test with empty string (should now succeed)
+ const char* empty_string = "";
+ EXPECT_EQ(CASS_OK,
+ cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, empty_string, strlen(empty_string), 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ EXPECT_EQ(dc_policy->local_dc(), String());
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareNWithPartialStringLocalDc) {
+
+ // Test with partial string length
+ const char* long_dc_name = "my_datacenter_with_a_long_name";
+ size_t partial_length = 5; // Should just use "my_da" as the datacenter name
+ EXPECT_EQ(CASS_OK, cass_cluster_set_load_balance_dc_aware_n(
+ cluster_, long_dc_name, partial_length,
+ 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ // Should be using the partial string as local DC
+ EXPECT_EQ(dc_policy->local_dc(), String(long_dc_name, partial_length));
+ EXPECT_EQ(dc_policy->local_dc(), String("my_da"));
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+// ==================== cass_cluster_set_load_balance_dc_aware
====================
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareWithNullLocalDc) {
+ // Test with NULL to use local DC from connected node
+ EXPECT_EQ(CASS_OK, cass_cluster_set_load_balance_dc_aware(
+ cluster_, NULL, 3, cass_false));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ // Should be using empty string as local DC (will be determined at runtime)
+ EXPECT_EQ(dc_policy->local_dc(), String());
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 3u);
+ EXPECT_TRUE(dc_policy->skip_remote_dcs_for_local_cl());
+}
+
+TEST_F(ClusterConfigUnitTest, SetLoadBalanceDcAwareWithEmptyLocalDc) {
+ // Test with empty string to use local DC from connected node
+ EXPECT_EQ(CASS_OK, cass_cluster_set_load_balance_dc_aware(
+ cluster_, "", 2, cass_true));
+
+ const DCAwarePolicy* dc_policy = build_dc_policy();
+
+ // Should be using empty string as local DC (will be determined at runtime)
+ EXPECT_EQ(dc_policy->local_dc(), String());
+ EXPECT_EQ(dc_policy->used_hosts_per_remote_dc(), 2u);
+ EXPECT_FALSE(dc_policy->skip_remote_dcs_for_local_cl());
+}
Review Comment:
nit: new line at the end
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]