ceph rgw:rgw的I/O路徑 后篇

在上一篇文章中,我分析了rgw main函數(shù)的流程,其中fe->run()開始了frontend的運(yùn)行,這篇文章就以run()函數(shù)開始。

rgw 支持很多frontend,以默認(rèn)的frontend civietweb來分析。

RGWCivetWebFrontend::run

run函數(shù)很長,但其中大部分都是在處理配置,其功能代碼只有下面幾行:

  struct mg_callbacks cb;
  memset((void *)&cb, 0, sizeof(cb));
  cb.begin_request = civetweb_callback;
  cb.log_message = rgw_civetweb_log_callback;
  cb.log_access = rgw_civetweb_log_access_callback;
  ctx = mg_start(&cb, this, options.data());

代碼很簡(jiǎn)單,就是對(duì)civetweb的使用,首先注冊(cè)了我們自己的各種事件處理函數(shù),然后使用mg_start開啟了服務(wù)器(在新的線程執(zhí)行)。
有關(guān)mg_callbacksmg_start參考:
https://github.com/civetweb/civetweb/blob/master/docs/api/mg_callbacks.md
https://github.com/civetweb/civetweb/blob/master/docs/api/mg_start.md

civetweb_callback

其中,請(qǐng)求的處理函數(shù)civetweb_callback實(shí)現(xiàn)如下:

static int civetweb_callback(struct mg_connection* conn)
{
  const struct mg_request_info* const req_info = mg_get_request_info(conn);
  return static_cast<RGWCivetWebFrontend *>(req_info->user_data)->process(conn);
}

可以看到,這里只是做了用于參數(shù)的獲取和轉(zhuǎn)發(fā),其真正的處理函數(shù)是RGWCivetWebFrontend::process

RGWCivetWebFrontend::process

int RGWCivetWebFrontend::process(struct mg_connection*  const conn)
{
  /* Hold a read lock over access to env.store for reconfiguration. */
  RWLock::RLocker lock(env.mutex);

  RGWCivetWeb cw_client(conn);
  auto real_client_io = rgw::io::add_reordering(
                          rgw::io::add_buffering(dout_context,
                            rgw::io::add_chunking(
                              rgw::io::add_conlen_controlling(
                                &cw_client))));
  RGWRestfulIO client_io(dout_context, &real_client_io);

  RGWRequest req(env.store->get_new_req_id());
  //處理函數(shù)
  int ret = process_request(env.store, env.rest, &req, env.uri_prefix,
                            *env.auth_registry, &client_io, env.olog);
  if (ret < 0) {
    /* We don't really care about return code. */
    dout(20) << "process_request() returned " << ret << dendl;
  }

  /* Mark as processed. */
  return 1;
}

rgw_process.cc/process_request

process函數(shù)將請(qǐng)求以及處理請(qǐng)求所需要的環(huán)境信息都準(zhǔn)備好,調(diào)用process_request函數(shù)進(jìn)行處理。這個(gè)函數(shù)比較長,只貼出關(guān)鍵的代碼片段:

  struct req_state rstate(g_ceph_context, &rgw_env, &userinfo);
  struct req_state *s = &rstate;
  
  ......
  
  RGWRESTMgr *mgr;
  RGWHandler_REST *handler = rest->get_handler(store, s,
    auth_registry,
    frontend_prefix,
    client_io, &mgr, &init_error);
  
  ......

  ret = rgw_process_authenticated(handler, op, req, s);
  
  ......
  
  client_io->complete_request();
  ......

RGWREST::get_handler

process_request 將req的狀態(tài)和一些必要的env存入rstate對(duì)象,然后調(diào)用rest->get_handler獲得對(duì)應(yīng)api的處理函數(shù),要注意的是,這里的rest就是之前傳入process的env.rest,我們追蹤下這個(gè)env.rest究竟是什么。

讓我們回到rgw_main.cc/main函數(shù):

RGWREST rest;
......
if (apis_map.count("s3") > 0 || s3website_enabled) {
    if (! swift_at_root) {
        rest.register_default_mgr(set_logging(rest_filter(store, RGW_REST_S3,new RGWRESTMgr_S3(s3website_enabled))));
    } else {
        derr << "Cannot have the S3 or S3 Website enabled together with "
            << "Swift API placed in the root of hierarchy" << dendl;
        return EINVAL;
    }
}
......
RGWProcessEnv env = { store, &rest, olog, 0, uri_prefix, auth_registry };
fe = new RGWCivetWebFrontend(env, config);

上面的代碼很清楚了,env.rest會(huì)隨著api配置的不同而不同,下面代碼繼續(xù)對(duì)get_handler進(jìn)行fen分析,以S3的api為例。

rest->get_handler(RGWHandler_REST* RGWREST::get_handler)函數(shù)比較復(fù)雜,只列出關(guān)鍵代碼片段:

RGWRESTMgr *m = mgr.get_manager(s, frontend_prefix, s->decoded_uri,&s->relative_uri);
RGWHandler_REST* handler = m->get_handler(s, auth_registry, frontend_prefix);
return handler;

RGWRESTMgr_S3::get_handler

可以看到它轉(zhuǎn)而去調(diào)用了具體的api所對(duì)應(yīng)的get_handler函數(shù),具體到S3,會(huì)調(diào)用RGWHandler_REST* RGWRESTMgr_S3::get_handler(..)函數(shù):

RGWHandler_REST* RGWRESTMgr_S3::get_handler(struct req_state* const s,
                                            const rgw::auth::StrategyRegistry& auth_registry,
                                            const std::string& frontend_prefix)
{
  // 根據(jù)配置判斷使用html還是xml控制
  bool is_s3website = enable_s3website && (s->prot_flags & RGW_REST_WEBSITE);
  int ret =
    RGWHandler_REST_S3::init_from_header(s,
                    is_s3website ? RGW_FORMAT_HTML :
                    RGW_FORMAT_XML, true);
  if (ret < 0)
    return NULL;

  RGWHandler_REST* handler;
  // 基于html的handler
  if (is_s3website) {
    // 根據(jù)請(qǐng)求中操作對(duì)象的不同返回不同的handler
    if (s->init_state.url_bucket.empty()) {
      handler = new RGWHandler_REST_Service_S3Website(auth_registry);
    } else if (s->object.empty()) {
      handler = new RGWHandler_REST_Bucket_S3Website(auth_registry);
    } else {
      handler = new RGWHandler_REST_Obj_S3Website(auth_registry);
    }
    //基于xml的handler
  } else {
    // 根據(jù)請(qǐng)求中操作對(duì)象的不同返回不同的handler      
    if (s->init_state.url_bucket.empty()) {
      handler = new RGWHandler_REST_Service_S3(auth_registry);
    } else if (s->object.empty()) {
      handler = new RGWHandler_REST_Bucket_S3(auth_registry);
    } else {
      handler = new RGWHandler_REST_Obj_S3(auth_registry);
    }
  }

  ldout(s->cct, 20) << __func__ << " handler=" << typeid(*handler).name()
            << dendl;
  return handler;
}

回到 rgw_process.cc/process_request

  struct req_state rstate(g_ceph_context, &rgw_env, &userinfo);
  struct req_state *s = &rstate;
  
  ......
  
  RGWRESTMgr *mgr;
  RGWHandler_REST *handler = rest->get_handler(store, s,
    auth_registry,
    frontend_prefix,
    client_io, &mgr, &init_error);
  
  ......
  // 開始分析以下部分代碼
  ret = rgw_process_authenticated(handler, op, req, s);
  
  ......
  
  client_io->complete_request();
  ......

我們?cè)谥耙呀?jīng)分析了process_request的前部分代碼,分析了handler是如何獲得的。

在獲得handler之后,經(jīng)過各種參數(shù)檢查,權(quán)限認(rèn)證之后,其真正執(zhí)行請(qǐng)求是在rgw_process_authenticated函數(shù)中,執(zhí)行完之后,調(diào)用complete_request完成請(qǐng)求。

rgw_process.cc/rgw_process_authenticated

這是rgw_process_authenticated有關(guān)執(zhí)行邏輯的代碼:

  req->log(s, "pre-executing");
  op->pre_exec(); //拼接reponse的header,并返回給client

  req->log(s, "executing");
  op->execute(); //執(zhí)行

  req->log(s, "completing");
  op->complete(); //調(diào)用send_response,返回執(zhí)行結(jié)果給client

至于op的獲得,稍微補(bǔ)充下

op = handler->get_op(store);

get_op函數(shù)會(huì)根據(jù)req的信息,去調(diào)用對(duì)應(yīng)的handler的op_xxx函數(shù),比如RGWHandler_REST_Obj_S3首先了下面一系列操作。

  RGWOp *op_get() override;
  RGWOp *op_head() override;
  RGWOp *op_put() override;
  RGWOp *op_delete() override;
  RGWOp *op_post() override;
  RGWOp *op_options() override;

每一個(gè)操作對(duì)對(duì)應(yīng)一個(gè)RGWOp的子類,比如RGWGetObj_ObjStore_S3、RGWGetObjTags_ObjStore_S3、RGWListBucket_ObjStore_S3等一系列類對(duì)象。

到這,從frontend到操作的執(zhí)行就走通了,接下來就可以對(duì)自己想要詳細(xì)學(xué)習(xí)的operation進(jìn)行閱讀了。只需要看對(duì)應(yīng)op對(duì)象的execute函數(shù),pre_exec和complete函數(shù)基本一致,具體見代碼注釋。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容