Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
separate registering control commands for load balancer from construc…
Browse files Browse the repository at this point in the history
…tor of simple_load_balancer and greedy_load_balancer

Summary: Ref T10174

Test Plan: N/A

Reviewers: qinzuoyan, sunweijie, heyuchen, laiyingchun, wutao1

Reviewed By: qinzuoyan, sunweijie

Subscribers: #pegasus

Maniphest Tasks: T10174

Differential Revision: https://phabricator.d.xiaomi.net/D81593
  • Loading branch information
LoveHeat authored and shengofsun committed Feb 2, 2018
1 parent c5878a8 commit aa21707
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 16 deletions.
26 changes: 20 additions & 6 deletions src/dist/replication/meta_server/greedy_load_balancer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
namespace dsn {
namespace replication {

greedy_load_balancer::greedy_load_balancer(meta_service *_svc) : simple_load_balancer(_svc)
greedy_load_balancer::greedy_load_balancer(meta_service *_svc)
: simple_load_balancer(_svc),
_ctrl_balancer_in_turn(nullptr),
_ctrl_only_primary_balancer(nullptr),
_ctrl_only_move_primary(nullptr)
{
if (_svc != nullptr) {
_balancer_in_turn = _svc->get_meta_options()._lb_opts.balancer_in_turn;
Expand All @@ -59,15 +63,13 @@ greedy_load_balancer::greedy_load_balancer(meta_service *_svc) : simple_load_bal
_only_primary_balancer = false;
_only_move_primary = false;
}

greedy_load_balancer::register_ctrl_commands();
}

greedy_load_balancer::~greedy_load_balancer()
{
dsn_cli_deregister(_ctrl_balancer_in_turn);
dsn_cli_deregister(_ctrl_only_primary_balancer);
dsn_cli_deregister(_ctrl_only_move_primary);
unregister_helper(_ctrl_balancer_in_turn);
unregister_helper(_ctrl_only_move_primary);
unregister_helper(_ctrl_only_move_primary);
}

static void free_string_in_cli_reply(dsn_cli_reply reply)
Expand Down Expand Up @@ -116,6 +118,9 @@ void greedy_load_balancer::ctrl_only_primary_balancer(int argc,

void greedy_load_balancer::register_ctrl_commands()
{
// register command that belong to simple_load_balancer
simple_load_balancer::register_ctrl_commands();

_ctrl_balancer_in_turn =
dsn_cli_app_register("lb.balancer_in_turn",
"control whether do app balancer in turn",
Expand Down Expand Up @@ -153,6 +158,15 @@ void greedy_load_balancer::register_ctrl_commands()
free_string_in_cli_reply);
}

void greedy_load_balancer::unregister_ctrl_commands()
{
unregister_helper(_ctrl_balancer_in_turn);
unregister_helper(_ctrl_only_move_primary);
unregister_helper(_ctrl_only_move_primary);

simple_load_balancer::unregister_ctrl_commands();
}

std::shared_ptr<configuration_balancer_request>
greedy_load_balancer::generate_balancer_request(const partition_configuration &pc,
const balance_type &type,
Expand Down
5 changes: 3 additions & 2 deletions src/dist/replication/meta_server/greedy_load_balancer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class greedy_load_balancer : public simple_load_balancer
virtual ~greedy_load_balancer();
bool balance(meta_view view, migration_list &list) override;

void register_ctrl_commands() override;
void unregister_ctrl_commands() override;

private:
enum class balance_type
{
Expand Down Expand Up @@ -79,8 +82,6 @@ class greedy_load_balancer : public simple_load_balancer
dsn_handle_t _ctrl_only_move_primary;

private:
void register_ctrl_commands();

void ctrl_balancer_in_turn(int argc, const char **argv, dsn_cli_reply *reply);
void ctrl_only_primary_balancer(int argc, const char **argv, dsn_cli_reply *reply);
void ctrl_only_move_primary(int argc, const char **argv, dsn_cli_reply *reply);
Expand Down
2 changes: 2 additions & 0 deletions src/dist/replication/meta_server/meta_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ error_code meta_service::start()
server_load_balancer *balancer = utils::factory_store<server_load_balancer>::create(
_meta_opts._lb_opts.server_load_balancer_type.c_str(), PROVIDER_TYPE_MAIN, this);
_balancer.reset(balancer);
// register control command to singleton-container for load balancer
_balancer->register_ctrl_commands();

// initializing the backup_handler should after remote_storage be initialized,
// because we should use _cluster_root
Expand Down
8 changes: 8 additions & 0 deletions src/dist/replication/meta_server/server_load_balancer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,8 @@ static void free_string_in_cli_reply(dsn_cli_reply reply)

void simple_load_balancer::register_ctrl_commands()
{
server_load_balancer::register_ctrl_commands();

_ctrl_assign_delay_ms =
dsn_cli_app_register("lb.assign_delay_ms",
"control the replica_assign_delay_ms_for_dropouts config",
Expand All @@ -770,6 +772,12 @@ void simple_load_balancer::register_ctrl_commands()
free_string_in_cli_reply);
}

void simple_load_balancer::unregister_ctrl_commands()
{
unregister_helper(_ctrl_assign_delay_ms);
server_load_balancer::unregister_ctrl_commands();
}

void simple_load_balancer::ctrl_assign_delay_ms(int argc, const char **argv, dsn_cli_reply *reply)
{
std::string *ret_msg = new std::string("OK");
Expand Down
37 changes: 29 additions & 8 deletions src/dist/replication/meta_server/server_load_balancer.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
namespace dsn {
namespace replication {

#define unregister_helper(ptr) \
do { \
if (ptr != nullptr) { \
dsn_cli_deregister(ptr); \
ptr = nullptr; \
} \
} while (0)

class server_load_balancer
{
public:
Expand Down Expand Up @@ -101,6 +109,20 @@ class server_load_balancer
configuration_balancer_response &resp);
void apply_balancer(meta_view view, const migration_list &ml);

//
// Try to register some cli-commands
//
// ATTENTION: because this function will register the cli-commands to singleton-container, so
// you must unregister the commands that you have already registered or release the instance of
// server_load_balancer before you call this function again
//
virtual void register_ctrl_commands() {}

//
// Try to unregister cli-commands
//
virtual void unregister_ctrl_commands() {}

public:
typedef std::function<bool(const rpc_address &addr1, const rpc_address &addr2)> node_comparator;
typedef std::function<bool(const node_state &ns)> node_filter;
Expand Down Expand Up @@ -163,7 +185,8 @@ class server_load_balancer
class simple_load_balancer : public server_load_balancer
{
public:
simple_load_balancer(meta_service *svc) : server_load_balancer(svc)
simple_load_balancer(meta_service *svc)
: server_load_balancer(svc), _ctrl_assign_delay_ms(nullptr)
{
if (svc != nullptr) {
mutation_2pc_min_replica_count = svc->get_options().mutation_2pc_min_replica_count;
Expand All @@ -175,13 +198,8 @@ class simple_load_balancer : public server_load_balancer
mutation_2pc_min_replica_count = 0;
replica_assign_delay_ms_for_dropouts = 0;
}

register_ctrl_commands();
}
virtual ~simple_load_balancer()
{
dsn_cli_deregister(_ctrl_assign_delay_ms);
}
virtual ~simple_load_balancer() { unregister_helper(_ctrl_assign_delay_ms); }

bool balance(meta_view, migration_list &list) override
{
Expand All @@ -199,6 +217,10 @@ class simple_load_balancer : public server_load_balancer

bool construct_replica(meta_view view, const gpid &pid, int max_replica_count) override;

void register_ctrl_commands() override;

void unregister_ctrl_commands() override;

protected:
// if a proposal is generated by cure, meta will record the POSSIBLE PARTITION COUNT
// IN FUTURE of a node with module "newly_partitions".
Expand All @@ -215,7 +237,6 @@ class simple_load_balancer : public server_load_balancer
pc_status on_missing_secondary(meta_view &view, const dsn::gpid &gpid);
pc_status on_redundant_secondary(meta_view &view, const dsn::gpid &gpid);

void register_ctrl_commands();
void ctrl_assign_delay_ms(int argc, const char **argv, dsn_cli_reply *reply);

int32_t mutation_2pc_min_replica_count;
Expand Down

0 comments on commit aa21707

Please sign in to comment.