在上一篇文章中瞻鹏,我分析了rgw main函數(shù)的流程朦佩,其中fe->run()
開(kāi)始了frontend的運(yùn)行,這篇文章就以run()函數(shù)開(kāi)始喉刘。
rgw 支持很多frontend瞧柔,以默認(rèn)的frontend civietweb來(lái)分析。
RGWCivetWebFrontend::run
run函數(shù)很長(zhǎng)睦裳,但其中大部分都是在處理配置造锅,其功能代碼只有下面幾行:
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開(kāi)啟了服務(wù)器(在新的線程執(zhí)行)倒谷。
有關(guān)mg_callbacks
和mg_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ù)比較長(zhǎng),只貼出關(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);
......
// 開(kāi)始分析以下部分代碼
ret = rgw_process_authenticated(handler, op, req, s);
......
client_io->complete_request();
......
我們?cè)谥耙呀?jīng)分析了process_request的前部分代碼汰蓉,分析了handler是如何獲得的绷蹲。
在獲得handler之后,經(jīng)過(guò)各種參數(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í)行就走通了拧廊,接下來(lái)就可以對(duì)自己想要詳細(xì)學(xué)習(xí)的operation進(jìn)行閱讀了。只需要看對(duì)應(yīng)op對(duì)象的execute函數(shù)晋修,pre_exec和complete函數(shù)基本一致吧碾,具體見(jiàn)代碼注釋。