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

rpc: several refactors for rpc #27

Merged
merged 3 commits into from
Apr 25, 2018
Merged

Conversation

shengofsun
Copy link
Contributor

  1. remove c interface of register rpc handler
  2. remove rpc code of RPC_L2_CLIENT_READ/WRITE, replace with proper storage engine rpc code.
  3. move some storage engine rpc_code's properties from config file to source code: is_write_operation, allow_batch, thread_pool

void *context,
dsn::gpid gpid DEFAULT(dsn::gpid()));
const char *extra_name,
const dsn_rpc_request_handler_t &cb);

/*! unregister callback to handle RPC request, and returns void* context upon \ref
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个注释我需要改

@@ -44,6 +44,11 @@ DEFINE_THREAD_POOL_CODE(THREAD_POOL_LOCAL_APP)
DEFINE_THREAD_POOL_CODE(THREAD_POOL_REPLICATION_LONG)
DEFINE_THREAD_POOL_CODE(THREAD_POOL_COMPACT)

#define STORAGE_WRITE(x, allow_batch) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加注释

@@ -122,7 +129,7 @@ class task_code
int _internal_code{0};
};

// you can define task_cods by the following macros
// you can define task_codes by the following macros
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

task_codes -> task_code

#define DEFINE_STORAGE_RPC_CODE(x, pri, pool, is_write, allow_batch) \
__selectany const ::dsn::task_code x( \
#x, TASK_TYPE_RPC_REQUEST, pri, pool, is_write, allow_batch); \
__selectany const ::dsn::task_code x##_ACK( \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么Reponse用THREAD_POOL_DEFAULT?说明原因并加注释

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用THREAD_POOL_DEFAULT没问题,不过这里还是用注释解释清楚,免得以后再看又要想

dsn_rpc_request_handler_t h;
};

typedef std::unordered_map<std::string, handler_entry *> rpc_handlers;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是否用 std::unordered_map<std::string, std::unique_ptr<handler_entry>> 更好?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handler_entry是两个地方都共享,用uniq_ptr没有让代码更简单。要用就用shared_ptr, 或者用我找找有没有ref counted thread unsafe的公共类

rpc_handlers _handlers;
mutable utils::rw_lock_nr _handlers_lock;

std::vector<std::pair<rpc_handler_info *, utils::rw_lock_nr> *> _vhandlers;
std::vector<std::pair<handler_entry *, utils::rw_lock_nr> *> _vhandlers;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为何用pair*来着?可以直接用 std::vector<std::pair<handler_entry *, utils::rw_lock_nr>> 吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在vector里面初始化utils::rw_lock_nr比较麻烦,各种编译不过……

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那么pair里面的handler_entry *至少可以改为 std::unique<handler_entry>吧?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个改了也不好,因为hander_entry是被_handlers这个map指向了两次,然后又被_vhandler指向了一次。有三个reference

dsn_rpc_request_handler_t cb = [](dsn_message_t request, void *param) {
auto hc2 = (hc_type1 *)param;

dsn_rpc_request_handler_t cb = [this, handler](dsn_message_t request) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是可以不赋值,而是直接放到函数参数里面?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对下层来说太麻烦了,一下要支持好多种类型的handler, rpc_engine的register接口就是一个std::function<void (dsn_message_t)> 来说会清晰非常多。性能问题应该还好,this和handler都是个单纯的指针

@@ -140,8 +148,10 @@ class rpc_engine
//
// rpc registrations
//
bool register_rpc_handler(rpc_handler_info *handler);
rpc_handler_info *unregister_rpc_handler(dsn::task_code rpc_code);
bool register_rpc_handler(dsn::task_code code,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是可以增加参数为 dsn_rpc_request_handler_t &&h 的重载函数?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

反正注册就是个一锤子买卖,就算拷贝也就拷贝一次。

void *context,
dsn::gpid gpid DEFAULT(dsn::gpid()));
const char *extra_name,
const dsn_rpc_request_handler_t &cb);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是可以增加 dsn_rpc_request_handler_t &&h 的重载函数?

public:
// not configurable [
dsn::task_code code;
dsn_task_type_t type;
std::string name;
dsn::task_code rpc_paired_code;
shared_exp_delay rpc_request_delayer;

bool rpc_request_for_storage;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有初始化?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

待会加一下

{
std::string name(handler->code.to_string());
handler_entry *ctx = new handler_entry{code, extra_name, h};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handler_entry在哪里delete的?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在unregister_rpc_handler的时候delete的

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果没有调用 unregister_rpc_handler,析构函数也没有delete?

rpc_handlers _handlers;
mutable utils::rw_lock_nr _handlers_lock;

std::vector<std::pair<rpc_handler_info *, utils::rw_lock_nr> *> _vhandlers;
std::vector<std::pair<handler_entry *, utils::rw_lock_nr> *> _vhandlers;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那么pair里面的handler_entry *至少可以改为 std::unique<handler_entry>吧?

#define DEFINE_STORAGE_RPC_CODE(x, pri, pool, is_write, allow_batch) \
__selectany const ::dsn::task_code x( \
#x, TASK_TYPE_RPC_REQUEST, pri, pool, is_write, allow_batch); \
__selectany const ::dsn::task_code x##_ACK( \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用THREAD_POOL_DEFAULT没问题,不过这里还是用注释解释清楚,免得以后再看又要想

@shengofsun shengofsun merged commit 0d35598 into XiaoMi:master Apr 25, 2018
@qinzuoyan qinzuoyan mentioned this pull request Aug 20, 2018
28 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants