auto cct = global_init(&defaults, args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_DAEMON, flags, "rgw_data");
global_pre_init(defaults, args, module_type, code_env, flags);
CephContext *cct = new CephContext(iparams.module_type, code_env, flags);
构造时会完成daemon命令的注册
_admin_socket = new AdminSocket(this);
_admin_hook = new CephContextHook(this);
// m_hooks 保存注册到 _admin_socket 的命令
_admin_socket->register_command("config show", _admin_hook, "dump current config settings");
当模块初始化完成时,会启动命令接收线程,作为daemon命令的服务端
common_init_finish(g_ceph_context); ---> cct->start_service_thread();
关键代码如下
void CephContext::start_service_thread()
{
_service_thread = new CephContextServiceThread(this);
_service_thread->create("service");
// "admin_socket": "/var/lib/ceph/var/run/ceph/cf0dcec5-2932-4924-a736-3731738b14c1-client.radosgw.rgw1.asok"
_admin_socket->init(_conf->admin_socket); // bool AdminSocket::init(const std::string& path)
}
// 通过path将client与server建立连接
bool AdminSocket::init(const std::string& path)
{
// 创建两个管道,分别用于读写
err = create_wakeup_pipe(&pipe_rd, &pipe_wr);
// 绑定与监听
err = bind_and_listen(path, &sock_fd);
m_sock_fd = sock_fd;
m_wakeup_rd_fd = pipe_rd;
m_wakeup_wr_fd = pipe_wr;
m_path = path;
// 启动一个线程,用于执行AdminSocket::entry接收和发送消息
th = make_named_thread("admin_socket", &AdminSocket::entry, this);
}
void AdminSocket::entry() noexcept
{
while (true) {
struct pollfd fds[2];
memset(fds, 0, sizeof(fds));
fds[0].fd = m_sock_fd;
fds[0].events = POLLIN | POLLRDBAND;
fds[1].fd = m_wakeup_rd_fd;
fds[1].events = POLLIN | POLLRDBAND;
int ret = poll(fds, 2, -1);
// 从管道中读出请求,并执行
if (fds[0].revents & POLLIN) {
do_accept(); // void AdminSocket::do_accept()
}
}
}
void AdminSocket::do_accept()
{
while (1) {
// 获取client发送的cmd
int ret = safe_read(connection_fd, &cmd[pos], 1);
// 执行cmd的入口
rval = execute_command(c, out); // AdminSocket::execute_command
// 命令执行后的信息写入管道,以便client获取
int ret = safe_write(connection_fd, &len, sizeof(len));
}
}
int CephContext::_do_command
{
while (match.size()) {
p = m_hooks.find(match); // 从注册命令中查询该命令
}
auto match_hook = p->second; // _admin_hook = new CephContextHook(this);
bool success = match_hook->call(match, cmdmap, format, out); // 命令执行,既调用 CephContextHook的call函数
}
void CephContext::do_command(std::string command, cmdmap_t& cmdmap, std::string format, bufferlist *out)
rgw daemon命令注册与处理代码流程简介
最新推荐文章于 2022-03-31 08:00:00 发布