增加HTTP业务层和缓存运行状态统计。业务层状态输出到tfe.fieldstat,缓存输出到cache.fieldstat。增加缓存开关。

This commit is contained in:
zhengchao
2018-10-17 20:21:21 +08:00
parent 00a0ec675a
commit 71109517b4
9 changed files with 482 additions and 125 deletions

View File

@@ -53,6 +53,17 @@ enum scan_table
__SCAN_TABLE_MAX
};
enum pangu_http_stat
{
STAT_SESSION,
STAT_LOG_NUM,
STAT_ACTION_MONIT,
STAT_ACTION_REJECT,
STAT_ACTION_REDIRECT,
STAT_ACTION_REPLACE,
STAT_ACTION_WHITELSIT,
__PG_STAT_MAX
};
struct pangu_rt
{
Maat_feather_t maat;
@@ -64,7 +75,15 @@ struct pangu_rt
ctemplate::Template * tpl_403, * tpl_404, * tpl_451;
char * reject_page;
int page_size;
int cache_enabled;
struct cache_handle* cache;
screen_stat_handle_t fs_handle;
long long stat_val[__PG_STAT_MAX];
int fs_id[__PG_STAT_MAX];
struct event_base* gc_evbase;
struct event* gcev;
};
struct pangu_rt * g_pangu_rt;
@@ -148,13 +167,51 @@ error_out:
Maat_burn_feather(target);
return NULL;
}
static void pangu_http_gc_cb(evutil_socket_t fd, short what, void * arg)
{
int i=0;
for(i=0;i<__PG_STAT_MAX;i++)
{
FS_operate(g_pangu_rt->fs_handle, g_pangu_rt->fs_id[i], 0, FS_OP_SET, ATOMIC_READ(&(g_pangu_rt->stat_val[i])));
}
return;
}
static void pangu_http_stat_init(struct pangu_rt * pangu_runtime)
{
int i=0;
struct timeval gc_delay = {0, 500*1000}; //Microseconds, we set 500 miliseconds here.
const char* spec[__PG_STAT_MAX]={0};
spec[STAT_SESSION]="http_sess";
spec[STAT_LOG_NUM]="log_num";
spec[STAT_ACTION_MONIT]="monit";
spec[STAT_ACTION_REJECT]="reject";
spec[STAT_ACTION_REDIRECT]="redirect";
spec[STAT_ACTION_REPLACE]="replace";
spec[STAT_ACTION_WHITELSIT]="whitelist";
for(i=0;i<__PG_STAT_MAX;i++)
{
if(spec[i]!=NULL)
{
pangu_runtime->fs_id[i]=FS_register(pangu_runtime->fs_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, spec[i]);
}
}
g_pangu_rt->gcev = event_new(pangu_runtime->gc_evbase, -1, EV_PERSIST, pangu_http_gc_cb, NULL);
evtimer_add(g_pangu_rt->gcev, &gc_delay);
return;
}
int pangu_http_init(struct tfe_proxy * proxy)
{
const char * profile = "./pangu_conf/pangu_pxy.conf";
const char * logfile = "./log/pangu_pxy.log";
g_pangu_rt = ALLOC(struct pangu_rt, 1);
g_pangu_rt->thread_num = tfe_proxy_get_thread_count();
g_pangu_rt->thread_num = tfe_proxy_get_work_thread_count();
g_pangu_rt->gc_evbase=tfe_proxy_get_gc_evbase();
MESA_load_profile_int_def(profile, "DEBUG", "LOG_LEVEL", &(g_pangu_rt->log_level), 0);
g_pangu_rt->local_logger = MESA_create_runtime_log_handle(logfile, g_pangu_rt->log_level);
g_pangu_rt->send_logger = pangu_log_handle_create(profile, "LOG", g_pangu_rt->local_logger);
@@ -162,6 +219,11 @@ int pangu_http_init(struct tfe_proxy * proxy)
{
goto error_out;
}
g_pangu_rt->fs_handle = tfe_proxy_get_fs_handle();
pangu_http_stat_init(g_pangu_rt);
g_pangu_rt->maat = create_maat_feather(profile, "MAAT", g_pangu_rt->thread_num, g_pangu_rt->local_logger);
if (!g_pangu_rt->maat)
{
@@ -199,7 +261,17 @@ int pangu_http_init(struct tfe_proxy * proxy)
"./pangu_conf/template/HTTP451.html");
g_pangu_rt->tpl_451 = ctemplate::Template::GetTemplate(page_path, ctemplate::DO_NOT_STRIP);
g_pangu_rt->cache = create_web_cache_handle(profile, "TANGO_CACHE", g_pangu_rt->local_logger);
MESA_load_profile_int_def(profile, "TANGO_CACHE", "enable_cache", &(g_pangu_rt->cache_enabled), 1);
if(g_pangu_rt->cache_enabled)
{
g_pangu_rt->cache = create_web_cache_handle(profile, "TANGO_CACHE", g_pangu_rt->gc_evbase, g_pangu_rt->local_logger);
if(!g_pangu_rt->cache)
{
TFE_LOG_INFO(NULL, "Tango Cache init failed.");
goto error_out;
}
TFE_LOG_INFO(NULL, "Tango Cache Enabled.");
}
TFE_LOG_INFO(NULL, "Pangu HTTP init success.");
return 0;
@@ -207,25 +279,6 @@ error_out:
TFE_LOG_ERROR(NULL, "Pangu HTTP init failed.");
return -1;
}
static void _wrap_std_field_write(struct tfe_http_half * half, enum tfe_http_std_field field_id, const char * value)
{
struct http_field_name tmp_name;
tmp_name.field_id = field_id;
tmp_name.field_name = NULL;
tfe_http_field_write(half, &tmp_name, value);
return;
}
#if 0
static void _wrap_non_std_field_write(struct tfe_http_half * half, const char* field_name, const char * value)
{
struct http_field_name tmp_name;
tmp_name.field_id=TFE_HTTP_UNKNOWN_FIELD;
//todo remove force convert after tfe_http.h improved.
tmp_name.field_name=(char*)field_name;
tfe_http_field_write(half, &tmp_name, value);
return;
}
#endif
struct replace_ctx
{
@@ -450,7 +503,7 @@ static void cache_query_on_succ(future_result_t * result, void * user)
tfe_http_session_resume(ctx->ref_session);
ctx->cached_response=tfe_http_session_response_create(ctx->ref_session, 200);
tfe_http_nonstd_field_write(ctx->cached_response, "X-Cache-Lookup", "Hit From MESA-TFE");
tfe_http_nonstd_field_write(ctx->cached_response, "X-Cache-Lookup", "Hit From TFE");
tfe_http_std_field_write(ctx->cached_response, TFE_HTTP_CONT_TYPE, meta->content_type);
snprintf(temp, sizeof(temp), "%lu", meta->content_length);
tfe_http_std_field_write(ctx->cached_response, TFE_HTTP_CONT_LENGTH, temp);
@@ -469,8 +522,8 @@ static void cache_query_on_succ(future_result_t * result, void * user)
case CACHE_QUERY_RESULT_END:
assert(ctx->cached_response!=NULL);
ctx->cache_query_status=WEB_CACHE_HIT;
tfe_http_half_write_body_end(ctx->cached_response);
printf("cache query hit: %s\n", ctx->ref_session->req->req_spec.url);
tfe_http_half_write_body_end(ctx->cached_response);
//ownership has been transferred to http session, set to NULL.
ctx->cached_response=NULL;
assert(ctx->cache_result_actual_sz==ctx->cache_result_declared_sz);
@@ -846,16 +899,26 @@ void enforce_control_policy(const struct tfe_stream * stream, const struct tfe_h
switch (ctx->action)
{
case PG_ACTION_NONE:
break;
case PG_ACTION_MONIT:
ATOMIC_INC(&(g_pangu_rt->stat_val[STAT_ACTION_MONIT]));
//send log on close.
break;
case PG_ACTION_REJECT: http_reject(session, events, ctx);
case PG_ACTION_REJECT:
http_reject(session, events, ctx);
ATOMIC_INC(&(g_pangu_rt->stat_val[STAT_ACTION_REJECT]));
break;
case PG_ACTION_REDIRECT: http_redirect(session, events, ctx);
case PG_ACTION_REDIRECT:
http_redirect(session, events, ctx);
ATOMIC_INC(&(g_pangu_rt->stat_val[STAT_ACTION_REDIRECT]));
break;
case PG_ACTION_REPLACE: http_replace(stream, session, events, body_frag, frag_size, ctx);
case PG_ACTION_REPLACE:
http_replace(stream, session, events, body_frag, frag_size, ctx);
ATOMIC_INC(&(g_pangu_rt->stat_val[STAT_ACTION_REPLACE]));
break;
case PG_ACTION_WHITELIST: tfe_http_session_detach(session);
case PG_ACTION_WHITELIST:
tfe_http_session_detach(session);
ATOMIC_INC(&(g_pangu_rt->stat_val[STAT_ACTION_WHITELSIT]));
break;
default: assert(0);
break;
@@ -911,6 +974,7 @@ void pangu_on_http_begin(const struct tfe_stream * stream,
struct ipaddr sapp_addr;
int hit_cnt = 0;
assert(ctx == NULL);
ATOMIC_INC(&(g_pangu_rt->stat_val[STAT_SESSION]));
ctx = pangu_http_ctx_new(thread_id);
addr_tfe2sapp(stream->addr, &sapp_addr);
hit_cnt = Maat_scan_proto_addr(g_pangu_rt->maat, g_pangu_rt->scan_table_id[PXY_CTRL_IP], &sapp_addr, 0,
@@ -933,7 +997,7 @@ void pangu_on_http_end(const struct tfe_stream * stream,
const struct tfe_http_session * session, unsigned int thread_id, void ** pme)
{
struct pangu_http_ctx * ctx = *(struct pangu_http_ctx **) pme;
int i=0, j=0;
int i=0, j=0,ret=0;
if(ctx->action == PG_ACTION_REPLACE && ctx->rep_ctx->actually_replaced==0)
{
for(i=0; i< ctx->n_enforce; i++)
@@ -957,7 +1021,8 @@ void pangu_on_http_end(const struct tfe_stream * stream,
struct pangu_log log_msg = {.stream=stream, .http=session, .result=ctx->enforce_rules, .result_num=ctx->n_enforce};
if (ctx->action != PG_ACTION_NONE&& !(ctx->action == PG_ACTION_REPLACE && ctx->n_enforce==1 && ctx->rep_ctx->actually_replaced==0))
{
pangu_send_log(g_pangu_rt->send_logger, &log_msg);
ret=pangu_send_log(g_pangu_rt->send_logger, &log_msg);
ATOMIC_ADD(&(g_pangu_rt->stat_val[STAT_LOG_NUM]), ret);
}
pangu_http_ctx_free(ctx);
*pme = NULL;
@@ -984,16 +1049,17 @@ void pangu_on_http_data(const struct tfe_stream * stream, const struct tfe_http_
{
return;
}
if(events & EV_HTTP_REQ_HDR && !ctx->resume_from_cache_query)
if(g_pangu_rt->cache_enabled)
{
cache_query(session, thread_id, ctx);
}
if(!tfe_http_in_request(events))
{
cache_update(session, events, body_frag, frag_size, thread_id, ctx);
}
if(events & EV_HTTP_REQ_HDR && !ctx->resume_from_cache_query)
{
cache_query(session, thread_id, ctx);
}
if(!tfe_http_in_request(events))
{
cache_update(session, events, body_frag, frag_size, thread_id, ctx);
}
}
return;
}