Skip to content

Commit 2b4acc9

Browse files
committed
refactor(FQDN): feather refactor on client and host_port.h
1 parent 812edcd commit 2b4acc9

File tree

6 files changed

+53
-19
lines changed

6 files changed

+53
-19
lines changed

src/common/replication_common.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,16 @@ int32_t replication_options::app_mutation_2pc_min_replica_count(int32_t app_max_
180180
rc.learner_signature = invalid_signature;
181181
SET_OBJ_IP_AND_HOST_PORT(rc, primary, pc, primary);
182182

183-
if (node == pc.hp_primary) {
183+
host_port primary;
184+
GET_HOST_PORT(pc, primary, primary);
185+
if (node == primary) {
184186
rc.status = partition_status::PS_PRIMARY;
185187
return true;
186188
}
187189

188-
if (utils::contains(pc.hp_secondaries, node)) {
190+
std::vector<host_port> secondaries;
191+
GET_HOST_PORTS(pc, secondaries, secondaries);
192+
if (utils::contains(secondaries, node)) {
189193
rc.status = partition_status::PS_SECONDARY;
190194
return true;
191195
}

src/common/replication_other_types.h

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "consensus_types.h"
3737
#include "replica_admin_types.h"
3838
#include "common/replication_enums.h"
39+
#include "rpc/dns_resolver.h"
3940
#include "rpc/rpc_address.h"
4041
#include "rpc/rpc_host_port.h"
4142

@@ -78,18 +79,33 @@ inline bool is_member(const partition_configuration &pc, const rpc_address &node
7879
inline bool is_partition_config_equal(const partition_configuration &pc1,
7980
const partition_configuration &pc2)
8081
{
81-
// secondaries no need to be same order
82-
for (const auto &pc1_secondary : pc1.hp_secondaries) {
82+
if (pc1.ballot != pc2.ballot || pc1.pid != pc2.pid ||
83+
pc1.max_replica_count != pc2.max_replica_count ||
84+
pc1.last_committed_decree != pc2.last_committed_decree) {
85+
return false;
86+
}
87+
88+
host_port pc1_primary;
89+
GET_HOST_PORT(pc1, primary, pc1_primary);
90+
host_port pc2_primary;
91+
GET_HOST_PORT(pc2, primary, pc2_primary);
92+
if (pc1_primary != pc2_primary) {
93+
return false;
94+
}
95+
96+
// secondaries no need to be in the same order.
97+
std::vector<host_port> pc1_secondaries;
98+
GET_HOST_PORTS(pc1, secondaries, pc1_secondaries);
99+
for (const auto &pc1_secondary : pc1_secondaries) {
83100
if (!is_secondary(pc2, pc1_secondary)) {
84101
return false;
85102
}
86103
}
104+
105+
std::vector<host_port> pc2_secondaries;
106+
GET_HOST_PORTS(pc2, secondaries, pc2_secondaries);
87107
// last_drops is not considered into equality check
88-
return pc1.ballot == pc2.ballot && pc1.pid == pc2.pid &&
89-
pc1.max_replica_count == pc2.max_replica_count && pc1.primary == pc2.primary &&
90-
pc1.hp_primary == pc2.hp_primary && pc1.secondaries.size() == pc2.secondaries.size() &&
91-
pc1.hp_secondaries.size() == pc2.hp_secondaries.size() &&
92-
pc1.last_committed_decree == pc2.last_committed_decree;
108+
return pc1_secondaries.size() == pc2_secondaries.size();
93109
}
94110

95111
class replica_helper

src/meta/cluster_balance_policy.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,17 @@ bool cluster_balance_policy::get_app_migration_info(std::shared_ptr<app_state> a
229229
info.partitions.reserve(app->pcs.size());
230230
for (const auto &pc : app->pcs) {
231231
std::map<host_port, partition_status::type> pstatus_map;
232-
pstatus_map[pc.hp_primary] = partition_status::PS_PRIMARY;
233-
if (pc.hp_secondaries.size() != pc.max_replica_count - 1) {
232+
host_port primary;
233+
GET_HOST_PORT(pc, primary, primary);
234+
pstatus_map[primary] = partition_status::PS_PRIMARY;
235+
236+
std::vector<host_port> secondaries;
237+
GET_HOST_PORTS(pc, secondaries, secondaries);
238+
if (secondaries.size() != pc.max_replica_count - 1) {
234239
// partition is unhealthy
235240
return false;
236241
}
237-
for (const auto &secondary : pc.hp_secondaries) {
242+
for (const auto &secondary : secondaries) {
238243
pstatus_map[secondary] = partition_status::PS_SECONDARY;
239244
}
240245
info.partitions.push_back(std::move(pstatus_map));

src/meta/load_balance_policy.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "meta_admin_types.h"
3636
#include "rpc/dns_resolver.h" // IWYU pragma: keep
3737
#include "rpc/rpc_address.h"
38+
#include "rpc/rpc_host_port.h"
3839
#include "utils/command_manager.h"
3940
#include "utils/fail_point.h"
4041
#include "utils/flags.h"
@@ -172,14 +173,17 @@ generate_balancer_request(const app_mapper &apps,
172173
new_proposal_action(to, to, config_type::CT_UPGRADE_TO_PRIMARY));
173174
result.action_list.emplace_back(new_proposal_action(to, from, config_type::CT_REMOVE));
174175
break;
175-
case balance_type::COPY_SECONDARY:
176+
case balance_type::COPY_SECONDARY: {
176177
ans = "copy_secondary";
177178
result.balance_type = balancer_request_type::copy_secondary;
179+
host_port primary;
180+
GET_HOST_PORT(pc, primary, primary);
178181
result.action_list.emplace_back(
179-
new_proposal_action(pc.hp_primary, to, config_type::CT_ADD_SECONDARY_FOR_LB));
182+
new_proposal_action(primary, to, config_type::CT_ADD_SECONDARY_FOR_LB));
180183
result.action_list.emplace_back(
181-
new_proposal_action(pc.hp_primary, from, config_type::CT_REMOVE));
184+
new_proposal_action(primary, from, config_type::CT_REMOVE));
182185
break;
186+
}
183187
default:
184188
CHECK(false, "");
185189
}
@@ -566,7 +570,9 @@ void ford_fulkerson::update_decree(int node_id, const node_state &ns)
566570
{
567571
ns.for_each_primary(_app->app_id, [&, this](const gpid &pid) {
568572
const auto &pc = _app->pcs[pid.get_partition_index()];
569-
for (const auto &secondary : pc.hp_secondaries) {
573+
std::vector<host_port> secondaries;
574+
GET_HOST_PORTS(pc, secondaries, secondaries);
575+
for (const auto &secondary : secondaries) {
570576
auto i = _host_port_id.find(secondary);
571577
CHECK(i != _host_port_id.end(), "invalid secondary: {}", secondary);
572578
_network[node_id][i->second]++;

src/meta/meta_data.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,10 @@ app_state::app_state(const app_info &info) : app_info(info), helpers(new app_sta
538538
CLEAR_IP_AND_HOST_PORT(pc, secondaries);
539539
CLEAR_IP_AND_HOST_PORT(pc, last_drops);
540540

541+
// TODO(yujingwei): use marco simplify the code, and the logical may should change
542+
pc.__set_hp_secondaries({});
543+
pc.__set_hp_last_drops({});
544+
541545
pcs.assign(app_info::partition_count, pc);
542546
for (int i = 0; i != app_info::partition_count; ++i) {
543547
pcs[i].pid.set_partition_index(i);

src/rpc/rpc_host_port.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ class TProtocol;
132132
auto &_obj = (obj); \
133133
_obj.field.set_invalid(); \
134134
_obj.hp_##field.reset(); \
135-
_obj.__isset.hp_##field = false; \
136135
} while (0)
137136

138137
// Clear the '<field>' and optional 'hp_<field>' of 'obj'. The types of the fields are std::vector
@@ -141,8 +140,8 @@ class TProtocol;
141140
do { \
142141
auto &_obj = (obj); \
143142
_obj.field.clear(); \
144-
_obj.hp_##field.clear(); \
145-
_obj.__isset.hp_##field = false; \
143+
_obj.hp_##field.clear(); \
144+
_obj.__isset.hp_##field = false; \
146145
} while (0)
147146

148147
// Add 'addr' and 'hp' to the vector '<field>' and optional vector 'hp_<field>' of 'obj'. The types

0 commit comments

Comments
 (0)