Skip to content

Commit fb5d631

Browse files
committed
refactor(FQDN): feather refactor on client and host_port.h
1 parent 043a150 commit fb5d631

File tree

3 files changed

+67
-58
lines changed

3 files changed

+67
-58
lines changed

src/meta/meta_data.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828

2929
#include <algorithm>
3030
#include <atomic>
31+
#include <cstdint>
3132
#include <functional>
3233
#include <map>
3334
#include <memory>
3435
#include <set>
35-
#include <stdint.h>
3636
#include <string>
3737
#include <unordered_map>
3838
#include <utility>
@@ -316,22 +316,21 @@ struct split_state
316316
int32_t splitting_count{0};
317317
// partition_index -> split_status
318318
std::map<int32_t, split_status::type> status;
319-
split_state() : splitting_count(0) {}
319+
split_state() = default;
320320
};
321321

322322
class app_state;
323323

324324
class app_state_helper
325325
{
326326
public:
327-
app_state *owner;
327+
app_state *owner{nullptr};
328328
std::atomic_int partitions_in_progress{0};
329329
std::vector<config_context> contexts;
330330
dsn::message_ex *pending_response;
331331
std::vector<restore_state> restore_states;
332332
split_state split_states;
333333

334-
public:
335334
app_state_helper() : owner(nullptr), partitions_in_progress(0)
336335
{
337336
contexts.clear();

src/meta/partition_guardian.cpp

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -159,38 +159,50 @@ bool partition_guardian::from_proposals(meta_view &view,
159159
config_context &cc = *get_config_context(*(view.apps), gpid);
160160
bool is_action_valid;
161161

162+
auto handle_invalid_action = [&](std::string_view reason) {
163+
LOG_INFO("proposal action({}) for gpid({}) is invalid, clear all proposal actions: {}",
164+
action, gpid, reason);
165+
action.type = config_type::CT_INVALID;
166+
167+
while (!cc.lb_actions.empty()) {
168+
configuration_proposal_action cpa = *cc.lb_actions.front();
169+
if (!cc.lb_actions.is_from_balancer()) {
170+
finish_cure_proposal(view, gpid, cpa);
171+
}
172+
cc.lb_actions.pop_front();
173+
}
174+
return false;
175+
};
176+
162177
if (cc.lb_actions.empty()) {
163178
action.type = config_type::CT_INVALID;
164179
return false;
165180
}
181+
166182
action = *(cc.lb_actions.front());
167183
host_port target;
168184
host_port node;
169185
GET_HOST_PORT(action, target, target);
170186
std::string reason;
171187
host_port primary;
172188
GET_HOST_PORT(pc, primary, primary);
189+
173190
if (!target) {
174-
reason = "action target is invalid";
175-
goto invalid_action;
191+
return handle_invalid_action("action target is invalid");
176192
}
177193
if (!is_node_alive(*(view.nodes), target)) {
178-
reason = fmt::format("action target({}) is not alive", target);
179-
goto invalid_action;
194+
return handle_invalid_action(fmt::format("action target({}) is not alive", target));
180195
}
181196
GET_HOST_PORT(action, node, node);
182197
if (!node) {
183-
reason = "action node is invalid";
184-
goto invalid_action;
198+
return handle_invalid_action("action node is invalid");
185199
}
186200
if (!is_node_alive(*(view.nodes), node)) {
187-
reason = fmt::format("action node({}) is not alive", node);
188-
goto invalid_action;
201+
return handle_invalid_action(fmt::format("action node({}) is not alive", node));
189202
}
190203

191204
if (cc.lb_actions.is_abnormal_learning_proposal()) {
192-
reason = "learning process abnormal";
193-
goto invalid_action;
205+
return handle_invalid_action("learning process abnormal");
194206
}
195207

196208
switch (action.type) {
@@ -219,25 +231,9 @@ bool partition_guardian::from_proposals(meta_view &view,
219231

220232
if (is_action_valid) {
221233
return true;
222-
} else {
223-
reason = "action is invalid";
224-
}
225-
226-
invalid_action:
227-
LOG_INFO("proposal action({}) for gpid({}) is invalid, clear all proposal actions: {}",
228-
action,
229-
gpid,
230-
reason);
231-
action.type = config_type::CT_INVALID;
232-
233-
while (!cc.lb_actions.empty()) {
234-
configuration_proposal_action cpa = *cc.lb_actions.front();
235-
if (!cc.lb_actions.is_from_balancer()) {
236-
finish_cure_proposal(view, gpid, cpa);
237-
}
238-
cc.lb_actions.pop_front();
239234
}
240-
return false;
235+
236+
return handle_invalid_action("action is invalid");
241237
}
242238

243239
pc_status partition_guardian::on_missing_primary(meta_view &view, const dsn::gpid &gpid)

src/meta/test/meta_data.cpp

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@
3939
#include "rpc/rpc_host_port.h"
4040
#include "gtest/gtest.h"
4141

42-
using namespace dsn::replication;
42+
namespace dsn {
43+
namespace replication {
4344

4445
TEST(meta_data, dropped_cmp)
4546
{
46-
dsn::host_port n;
47+
host_port n;
4748

4849
dropped_replica d1, d2;
4950
// time not equal
@@ -112,10 +113,10 @@ TEST(meta_data, collect_replica)
112113
app_mapper apps;
113114
node_mapper nodes;
114115

115-
dsn::app_info info;
116+
app_info info;
116117
info.app_id = 1;
117118
info.is_stateful = true;
118-
info.status = dsn::app_status::AS_AVAILABLE;
119+
info.status = app_status::AS_AVAILABLE;
119120
info.app_name = "test";
120121
info.app_type = "test";
121122
info.max_replica_count = 3;
@@ -126,12 +127,12 @@ TEST(meta_data, collect_replica)
126127

127128
replica_info rep;
128129
rep.app_type = "test";
129-
rep.pid = dsn::gpid(1, 0);
130+
rep.pid = gpid(1, 0);
130131

131132
auto &pc = *get_config(apps, rep.pid);
132133
auto &cc = *get_config_context(apps, rep.pid);
133134

134-
std::vector<dsn::host_port> node_list;
135+
std::vector<host_port> node_list;
135136
generate_node_list(node_list, 10, 10);
136137

137138
#define CLEAR_REPLICA \
@@ -355,10 +356,10 @@ TEST(meta_data, construct_replica)
355356
app_mapper apps;
356357
node_mapper nodes;
357358

358-
dsn::app_info info;
359+
app_info info;
359360
info.app_id = 1;
360361
info.is_stateful = true;
361-
info.status = dsn::app_status::AS_AVAILABLE;
362+
info.status = app_status::AS_AVAILABLE;
362363
info.app_name = "test";
363364
info.app_type = "test";
364365
info.max_replica_count = 3;
@@ -369,12 +370,12 @@ TEST(meta_data, construct_replica)
369370

370371
replica_info rep;
371372
rep.app_type = "test";
372-
rep.pid = dsn::gpid(1, 0);
373+
rep.pid = gpid(1, 0);
373374

374-
dsn::partition_configuration &pc = *get_config(apps, rep.pid);
375+
partition_configuration &pc = *get_config(apps, rep.pid);
375376
config_context &cc = *get_config_context(apps, rep.pid);
376377

377-
std::vector<dsn::host_port> node_list;
378+
std::vector<host_port> node_list;
378379
generate_node_list(node_list, 10, 10);
379380

380381
#define CLEAR_REPLICA \
@@ -405,12 +406,15 @@ TEST(meta_data, construct_replica)
405406
CLEAR_ALL;
406407
cc.dropped = {dropped_replica{node_list[0], dropped_replica::INVALID_TIMESTAMP, 5, 10, 12}};
407408
ASSERT_TRUE(construct_replica(view, rep.pid, 3));
408-
dsn::host_port primary;
409+
410+
host_port primary;
409411
GET_HOST_PORT(pc, primary, primary);
410412
ASSERT_EQ(node_list[0], primary);
411-
std::vector<dsn::host_port> secondaries;
413+
414+
std::vector<host_port> secondaries;
412415
GET_HOST_PORTS(pc, secondaries, secondaries);
413416
ASSERT_TRUE(secondaries.empty());
417+
414418
ASSERT_TRUE(cc.dropped.empty());
415419
ASSERT_EQ(-1, cc.prefered_dropped);
416420
}
@@ -423,17 +427,20 @@ TEST(meta_data, construct_replica)
423427
dropped_replica{node_list[3], dropped_replica::INVALID_TIMESTAMP, 8, 10, 12},
424428
dropped_replica{node_list[4], dropped_replica::INVALID_TIMESTAMP, 9, 11, 12}};
425429
ASSERT_TRUE(construct_replica(view, rep.pid, 3));
426-
dsn::host_port primary;
430+
431+
host_port primary;
427432
GET_HOST_PORT(pc, primary, primary);
428433
ASSERT_EQ(node_list[4], primary);
429-
std::vector<dsn::host_port> secondaries;
434+
435+
std::vector<host_port> secondaries;
430436
GET_HOST_PORTS(pc, secondaries, secondaries);
431437
ASSERT_TRUE(secondaries.empty());
432438

433-
std::vector<dsn::host_port> nodes = {node_list[2], node_list[3]};
434-
std::vector<dsn::host_port> last_drops;
439+
std::vector<host_port> nodes = {node_list[2], node_list[3]};
440+
std::vector<host_port> last_drops;
435441
GET_HOST_PORTS(pc, last_drops, last_drops);
436442
ASSERT_EQ(nodes, last_drops);
443+
437444
ASSERT_EQ(3, cc.dropped.size());
438445
ASSERT_EQ(2, cc.prefered_dropped);
439446
}
@@ -446,17 +453,20 @@ TEST(meta_data, construct_replica)
446453
dropped_replica{node_list[2], dropped_replica::INVALID_TIMESTAMP, 7, 12, 12}};
447454

448455
ASSERT_TRUE(construct_replica(view, rep.pid, 3));
449-
dsn::host_port primary;
456+
457+
host_port primary;
450458
GET_HOST_PORT(pc, primary, primary);
451-
ASSERT_EQ(node_list[4], primary);
452-
std::vector<dsn::host_port> secondaries;
459+
ASSERT_EQ(node_list[2], primary);
460+
461+
std::vector<host_port> secondaries;
453462
GET_HOST_PORTS(pc, secondaries, secondaries);
454463
ASSERT_TRUE(secondaries.empty());
455464

456-
std::vector<dsn::host_port> nodes = {node_list[0], node_list[1]};
457-
std::vector<dsn::host_port> last_drops;
465+
std::vector<host_port> nodes = {node_list[0], node_list[1]};
466+
std::vector<host_port> last_drops;
458467
GET_HOST_PORTS(pc, last_drops, last_drops);
459468
ASSERT_EQ(nodes, last_drops);
469+
460470
ASSERT_EQ(2, cc.dropped.size());
461471
ASSERT_EQ(1, cc.prefered_dropped);
462472
}
@@ -470,19 +480,23 @@ TEST(meta_data, construct_replica)
470480
dropped_replica{node_list[3], dropped_replica::INVALID_TIMESTAMP, 7, 14, 14}};
471481

472482
ASSERT_TRUE(construct_replica(view, rep.pid, 3));
473-
dsn::host_port primary;
483+
host_port primary;
474484
GET_HOST_PORT(pc, primary, primary);
475485
ASSERT_EQ(node_list[3], primary);
476-
std::vector<dsn::host_port> secondaries;
486+
487+
std::vector<host_port> secondaries;
477488
GET_HOST_PORTS(pc, secondaries, secondaries);
478489
ASSERT_TRUE(secondaries.empty());
479490

480-
std::vector<dsn::host_port> nodes = {node_list[1], node_list[2]};
481-
std::vector<dsn::host_port> last_drops;
491+
std::vector<host_port> nodes = {node_list[1], node_list[2]};
492+
std::vector<host_port> last_drops;
482493
GET_HOST_PORTS(pc, last_drops, last_drops);
483494
ASSERT_EQ(nodes, last_drops);
484495

485496
ASSERT_EQ(3, cc.dropped.size());
486497
ASSERT_EQ(2, cc.prefered_dropped);
487498
}
488499
}
500+
501+
} // namespace replication
502+
} // namespace dsn

0 commit comments

Comments
 (0)