/Users/deen/code/yugabyte-db/src/yb/integration-tests/load_balancer_respect_affinity-test.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include <gtest/gtest.h> |
15 | | |
16 | | #include "yb/client/client.h" |
17 | | |
18 | | #include "yb/consensus/consensus.pb.h" |
19 | | #include "yb/consensus/consensus.proxy.h" |
20 | | |
21 | | #include "yb/gutil/casts.h" |
22 | | |
23 | | #include "yb/integration-tests/external_mini_cluster.h" |
24 | | #include "yb/integration-tests/mini_cluster.h" |
25 | | #include "yb/integration-tests/yb_table_test_base.h" |
26 | | |
27 | | #include "yb/master/master_cluster.proxy.h" |
28 | | |
29 | | #include "yb/tools/yb-admin_client.h" |
30 | | |
31 | | #include "yb/util/result.h" |
32 | | |
33 | | using namespace std::literals; |
34 | | |
35 | | namespace yb { |
36 | | namespace integration_tests { |
37 | | |
38 | | const MonoDelta kDefaultTimeout = 30000ms; |
39 | | |
40 | | class LoadBalancerRespectAffinityTest : public YBTableTestBase { |
41 | | protected: |
42 | 2 | bool use_yb_admin_client() override { return true; } |
43 | | |
44 | 5 | bool use_external_mini_cluster() override { return true; } |
45 | | |
46 | 5 | size_t num_masters() override { |
47 | 5 | return 3; |
48 | 5 | } |
49 | | |
50 | 3 | size_t num_tablet_servers() override { |
51 | 3 | return 3; |
52 | 3 | } |
53 | | |
54 | 0 | Result<bool> IsLoadBalanced() { |
55 | 0 | return client_->IsLoadBalanced(narrow_cast<uint32_t>(num_tablet_servers())); |
56 | 0 | } |
57 | | |
58 | 0 | CHECKED_STATUS WaitLoadBalanced(MonoDelta timeout) { |
59 | 0 | return WaitFor([&]() -> Result<bool> { |
60 | 0 | return IsLoadBalanced(); |
61 | 0 | }, timeout, "IsLoadBalanced"); |
62 | 0 | } |
63 | | |
64 | 0 | Result<bool> AreLeadersOnPreferredOnly() { |
65 | 0 | master::AreLeadersOnPreferredOnlyRequestPB req; |
66 | 0 | master::AreLeadersOnPreferredOnlyResponsePB resp; |
67 | 0 | rpc::RpcController rpc; |
68 | 0 | rpc.set_timeout(kDefaultTimeout); |
69 | 0 | auto proxy = GetMasterLeaderProxy<master::MasterClusterProxy>(); |
70 | 0 | RETURN_NOT_OK(proxy.AreLeadersOnPreferredOnly(req, &resp, &rpc)); |
71 | 0 | return !resp.has_error(); |
72 | 0 | } |
73 | | |
74 | 1 | void CustomizeExternalMiniCluster(ExternalMiniClusterOptions* opts) override { |
75 | 1 | opts->extra_tserver_flags.push_back("--placement_cloud=c"); |
76 | 1 | opts->extra_tserver_flags.push_back("--placement_region=r"); |
77 | 1 | opts->extra_tserver_flags.push_back("--placement_zone=z${index}"); |
78 | 1 | opts->extra_tserver_flags.push_back("--transaction_tables_use_preferred_zones=false"); |
79 | 1 | } |
80 | | }; |
81 | | |
82 | | TEST_F(LoadBalancerRespectAffinityTest, |
83 | 1 | YB_DISABLE_TEST_IN_TSAN(TransactionUsePreferredZones)) { |
84 | 1 | ASSERT_OK(yb_admin_client_->ModifyPlacementInfo("c.r.z0,c.r.z1,c.r.z2", 3, "")); |
85 | 0 | ASSERT_OK(yb_admin_client_->SetPreferredZones({"c.r.z1"})); |
86 | | |
87 | | // First test whether load is correctly balanced when transaction tablet leaders are not |
88 | | // using preferred zones. |
89 | 0 | ASSERT_OK(WaitLoadBalanced(kDefaultTimeout * 2)); |
90 | |
|
91 | 0 | ASSERT_OK(WaitFor([&]() { |
92 | 0 | return AreLeadersOnPreferredOnly(); |
93 | 0 | }, kDefaultTimeout, "AreLeadersOnPreferredOnly")); |
94 | | |
95 | | // Now test that once setting this gflag, after leader load re-balances all leaders are |
96 | | // in the preferred zone. |
97 | 0 | for (ExternalDaemon* daemon : external_mini_cluster()->master_daemons()) { |
98 | 0 | ASSERT_OK(external_mini_cluster()-> |
99 | 0 | SetFlag(daemon, "transaction_tables_use_preferred_zones", "1")); |
100 | 0 | } |
101 | |
|
102 | 0 | ASSERT_OK(WaitLoadBalanced(kDefaultTimeout * 2)); |
103 | |
|
104 | 0 | ASSERT_OK(WaitFor([&]() { |
105 | 0 | return AreLeadersOnPreferredOnly(); |
106 | 0 | }, kDefaultTimeout, "AreLeadersOnPreferredOnly")); |
107 | | |
108 | | // Now test that toggling the gflag back to false rebalances the transaction tablet leaders |
109 | | // to not just be on preferred zones. |
110 | 0 | for (ExternalDaemon* daemon : external_mini_cluster()->master_daemons()) { |
111 | 0 | ASSERT_OK(external_mini_cluster()-> |
112 | 0 | SetFlag(daemon, "transaction_tables_use_preferred_zones", "0")); |
113 | 0 | } |
114 | |
|
115 | 0 | ASSERT_OK(WaitLoadBalanced(kDefaultTimeout * 2)); |
116 | |
|
117 | 0 | ASSERT_OK(WaitFor([&]() { |
118 | 0 | return AreLeadersOnPreferredOnly(); |
119 | 0 | }, kDefaultTimeout, "AreLeadersOnPreferredOnly")); |
120 | 0 | } |
121 | | |
122 | | } // namespace integration_tests |
123 | | } // namespace yb |