修正cache_cookied_content开关未生效的bug;在http结束时增加cache日志;

This commit is contained in:
zhengchao
2018-12-22 19:52:49 +06:00
parent a530a4f0ce
commit bba7a4d9b1
3 changed files with 68 additions and 23 deletions

View File

@@ -539,8 +539,8 @@ struct pangu_http_ctx
struct tfe_http_half* cache_revalidate_req; struct tfe_http_half* cache_revalidate_req;
struct tfe_http_half* cached_response; struct tfe_http_half* cached_response;
size_t cache_result_declared_sz, cache_result_actual_sz; size_t cache_result_declared_sz, cache_result_actual_sz;
struct cache_write_context* cache_update_ctx; struct cache_write_context* cache_write_ctx;
int cache_wirte_result;
int thread_id; int thread_id;
}; };
@@ -594,10 +594,10 @@ static void pangu_http_ctx_free(struct pangu_http_ctx * ctx)
{ {
Maat_stream_scan_string_end(&(ctx->sp)); Maat_stream_scan_string_end(&(ctx->sp));
} }
if(ctx->cache_update_ctx) if(ctx->cache_write_ctx)
{ {
web_cache_write_end(ctx->cache_update_ctx); web_cache_write_end(ctx->cache_write_ctx);
ctx->cache_update_ctx=NULL; ctx->cache_write_ctx=NULL;
} }
//On session recycle //On session recycle
@@ -1367,16 +1367,16 @@ void cache_write(const struct tfe_http_session * session, enum tfe_http_event ev
if(events & EV_HTTP_RESP_BODY_BEGIN) if(events & EV_HTTP_RESP_BODY_BEGIN)
{ {
ctx->cache_update_ctx=web_cache_write_start(g_pangu_rt->cache, thread_id, session, &(ctx->cmid)); ctx->cache_write_ctx=web_cache_write_start(g_pangu_rt->cache, thread_id, session, &(ctx->cmid));
} }
if(events & EV_HTTP_RESP_BODY_CONT && ctx->cache_update_ctx!=NULL) if(events & EV_HTTP_RESP_BODY_CONT && ctx->cache_write_ctx!=NULL)
{ {
web_cache_write(ctx->cache_update_ctx, body_frag, frag_size); web_cache_write(ctx->cache_write_ctx, body_frag, frag_size);
} }
if(events & EV_HTTP_RESP_BODY_END && ctx->cache_update_ctx!=NULL) if(events & EV_HTTP_RESP_BODY_END && ctx->cache_write_ctx!=NULL)
{ {
web_cache_write_end(ctx->cache_update_ctx); ctx->cache_wirte_result=web_cache_write_end(ctx->cache_write_ctx);
ctx->cache_update_ctx=NULL; ctx->cache_write_ctx=NULL;
//printf("cache update success: %s\n", ctx->ref_session->req->req_spec.url); //printf("cache update success: %s\n", ctx->ref_session->req->req_spec.url);
} }
@@ -1482,6 +1482,10 @@ void pangu_on_http_end(const struct tfe_stream * stream,
{ {
ATOMIC_INC(&(g_pangu_rt->stat_val[STAT_ACTION_REPLACE])); ATOMIC_INC(&(g_pangu_rt->stat_val[STAT_ACTION_REPLACE]));
} }
TFE_LOG_DEBUG(g_pangu_rt->local_logger, "cache %s %s upload=%d",
session->req->req_spec.url,
cache_pending_result_string(ctx->pending_result),
ctx->cache_wirte_result);
cache_mid_clear(&(ctx->cmid)); cache_mid_clear(&(ctx->cmid));
pangu_http_ctx_free(ctx); pangu_http_ctx_free(ctx);
*pme = NULL; *pme = NULL;

View File

@@ -132,6 +132,29 @@ struct cache_write_context
size_t content_len; size_t content_len;
size_t uploaded_len; size_t uploaded_len;
}; };
const char* cache_pending_result_string(enum cache_pending_result result)
{
switch (result)
{
case PENDING_RESULT_NONE:
return "none";
case PENDING_RESULT_FOBIDDEN:
return "forbidden";
case PENDING_RESULT_REVALIDATE:
return "revalidate";
case PENDING_RESULT_ALLOWED:
return "allowd";
case PENDING_RESULT_HIT:
return "hit";
case PENDING_RESULT_MISS:
return "miss";
default:
assert(0);
return "?";
}
}
static void web_cache_stat_cb(evutil_socket_t fd, short what, void * arg) static void web_cache_stat_cb(evutil_socket_t fd, short what, void * arg)
{ {
struct cache_handle* cache=(struct cache_handle *)arg; struct cache_handle* cache=(struct cache_handle *)arg;
@@ -743,7 +766,7 @@ struct cache_handle* create_web_cache_handle(const char* profile_path, const cha
MESA_load_profile_int_def(profile_path, section, "max_cache_obj_size", &(temp), 1024*1024*1024); MESA_load_profile_int_def(profile_path, section, "max_cache_obj_size", &(temp), 1024*1024*1024);
cache->default_cache_policy.max_cache_obj_size=temp; //<1GB by default cache->default_cache_policy.max_cache_obj_size=temp; //<1GB by default
MESA_load_profile_int_def(profile_path, section, "min_cache_obj_size", &(temp), 16*1024); MESA_load_profile_int_def(profile_path, section, "min_cache_obj_size", &(temp), 1*1024);
cache->default_cache_policy.min_cache_obj_size=temp;// > 16kb by default cache->default_cache_policy.min_cache_obj_size=temp;// > 16kb by default
if(cache->cache_policy_enabled) if(cache->cache_policy_enabled)
@@ -982,14 +1005,14 @@ static void cache_read_meta_on_succ(future_result_t * result, void * user)
break; break;
case RESULT_TYPE_USERTAG: case RESULT_TYPE_USERTAG:
cached_meta_set(&ctx->cached_obj_meta, RESULT_TYPE_USERTAG, _result->data_frag, _result->size); cached_meta_set(&ctx->cached_obj_meta, RESULT_TYPE_USERTAG, _result->data_frag, _result->size);
TFE_LOG_DEBUG(ctx->ref_handle->logger, "cache meta query hit: %s %s %s" TFE_LOG_DEBUG(ctx->ref_handle->logger, "cache meta read hit: %s %s %s"
, ctx->url , ctx->url
, ctx->cached_obj_meta.last_modified ? ctx->cached_obj_meta.last_modified:"no_last_modify" , ctx->cached_obj_meta.last_modified ? ctx->cached_obj_meta.last_modified:"no_last_modify"
, ctx->cached_obj_meta.etag ? ctx->cached_obj_meta.etag:"no_etag"); , ctx->cached_obj_meta.etag ? ctx->cached_obj_meta.etag:"no_etag");
break; break;
case RESULT_TYPE_MISS: case RESULT_TYPE_MISS:
ctx->status=PENDING_RESULT_MISS; ctx->status=PENDING_RESULT_MISS;
TFE_LOG_DEBUG(ctx->ref_handle->logger, "cache meta query miss: %s", ctx->url); TFE_LOG_DEBUG(ctx->ref_handle->logger, "cache meta read miss: %s", ctx->url);
//NOT break intentionally. //NOT break intentionally.
case RESULT_TYPE_END: case RESULT_TYPE_END:
//last call. //last call.
@@ -1077,7 +1100,7 @@ enum cache_pending_result web_cache_async_pending(struct cache_handle* handle, u
} }
if(_mid->shall_bypass || if(_mid->shall_bypass ||
(!param->force_caching && !param->cache_dyn_url && _mid->is_dyn_url && param->key_descr.qs_num==0) || (!param->force_caching && !param->cache_dyn_url && _mid->is_dyn_url && param->key_descr.qs_num==0) ||
(!param->force_caching && param->cache_cookied_cont && _mid->has_cookie)) (!param->force_caching && !param->cache_cookied_cont && _mid->has_cookie))
{ {
_mid->result=PENDING_RESULT_FOBIDDEN; _mid->result=PENDING_RESULT_FOBIDDEN;
return _mid->result; return _mid->result;
@@ -1262,7 +1285,7 @@ struct cache_write_context* web_cache_write_start(struct cache_handle* handle, u
if(!(param->ignore_res_nocache || param->force_caching)) if(!(param->ignore_res_nocache || param->force_caching))
{ {
ATOMIC_INC(&(handle->stat_val[STAT_CACHE_WRITE_FORBIDEN])); ATOMIC_INC(&(handle->stat_val[STAT_CACHE_WRITE_FORBIDEN]));
TFE_LOG_DEBUG(handle->logger, "cache update forbiden: %s", session->req->req_spec.url); TFE_LOG_DEBUG(handle->logger, "cache write forbiden: %s", session->req->req_spec.url);
return NULL; return NULL;
} }
break; break;
@@ -1280,6 +1303,13 @@ struct cache_write_context* web_cache_write_start(struct cache_handle* handle, u
|| (!param->cache_html && _mid->is_html) || (!param->cache_html && _mid->is_html)
) )
{ {
ATOMIC_INC(&(handle->stat_val[STAT_CACHE_WRITE_FORBIDEN]));
TFE_LOG_DEBUG(handle->logger, "cache write forbiden: %s, bypass:%d, cont_len:%lld, has_cookie:%d, is_html:%d",
session->req->req_spec.url,
_mid->shall_bypass,
content_len,
_mid->has_cookie,
_mid->is_html);
ATOMIC_INC(&(handle->stat_val[STAT_CACHE_WRITE_BYPASS])); ATOMIC_INC(&(handle->stat_val[STAT_CACHE_WRITE_BYPASS]));
return NULL; return NULL;
} }
@@ -1372,31 +1402,37 @@ void web_cache_write(struct cache_write_context* ctx, const unsigned char * body
ATOMIC_ADD(&(ctx->ref_cache_handle->stat_val[STAT_CACHE_WRITE_BYTES]), frag_size); ATOMIC_ADD(&(ctx->ref_cache_handle->stat_val[STAT_CACHE_WRITE_BYTES]), frag_size);
return; return;
} }
void web_cache_write_end(struct cache_write_context* ctx) int web_cache_write_end(struct cache_write_context* ctx)
{ {
int ret=0; int tmp_ret=0, ret=0;
struct cache_write_future_ctx* future_ctx=ctx->future_ctx; struct cache_write_future_ctx* future_ctx=ctx->future_ctx;
if(ctx->uploaded_len==ctx->content_len) if(ctx->uploaded_len==ctx->content_len)
{ {
ret=tango_cache_update_end(ctx->write_ctx, future_ctx->upload_path, sizeof(future_ctx->upload_path)); tmp_ret=tango_cache_update_end(ctx->write_ctx, future_ctx->upload_path, sizeof(future_ctx->upload_path));
if(ret<0) if(tmp_ret<0)
{ {
//upload too slow or storage server error; //upload too slow or storage server error;
TFE_LOG_DEBUG(ctx->ref_cache_handle->logger, "cache upload failed: %s",ctx->future_ctx->url); TFE_LOG_DEBUG(ctx->ref_cache_handle->logger, "cache upload failed: %s",ctx->future_ctx->url);
cache_write_future_ctx_free(ctx->future_ctx); cache_write_future_ctx_free(ctx->future_ctx);
ctx->future_ctx=NULL; ctx->future_ctx=NULL;
ATOMIC_INC(&(ctx->ref_cache_handle->stat_val[STAT_CACHE_WRITE_ERR])); ATOMIC_INC(&(ctx->ref_cache_handle->stat_val[STAT_CACHE_WRITE_ERR]));
ret=-1;
}
else
{
ret=1;
} }
} }
else else
{ {
tango_cache_update_cancel(ctx->write_ctx); tango_cache_update_cancel(ctx->write_ctx);
ATOMIC_INC(&(ctx->ref_cache_handle->stat_val[STAT_CACHE_WRITE_CANCEL])); ATOMIC_INC(&(ctx->ref_cache_handle->stat_val[STAT_CACHE_WRITE_CANCEL]));
ret=-2;
} }
ATOMIC_DEC(&(ctx->ref_cache_handle->stat_val[STAT_CACHE_WRITING])); ATOMIC_DEC(&(ctx->ref_cache_handle->stat_val[STAT_CACHE_WRITING]));
ctx->write_ctx = NULL; ctx->write_ctx = NULL;
ctx->ref_cache_handle = NULL; ctx->ref_cache_handle = NULL;
free(ctx); free(ctx);
return; return ret;
} }

View File

@@ -44,6 +44,7 @@ enum cache_pending_result
PENDING_RESULT_HIT, PENDING_RESULT_HIT,
PENDING_RESULT_MISS PENDING_RESULT_MISS
}; };
const char* cache_pending_result_string(enum cache_pending_result);
struct cache_mid; struct cache_mid;
void cache_mid_clear(struct cache_mid **mid); void cache_mid_clear(struct cache_mid **mid);
@@ -58,7 +59,11 @@ struct cache_write_context;
struct cache_write_context* web_cache_write_start(struct cache_handle* handle, unsigned int thread_id, struct cache_write_context* web_cache_write_start(struct cache_handle* handle, unsigned int thread_id,
const struct tfe_http_session * session, struct cache_mid **mid); const struct tfe_http_session * session, struct cache_mid **mid);
void web_cache_write(struct cache_write_context* ctx, const unsigned char * body_frag, size_t frag_size); void web_cache_write(struct cache_write_context* ctx, const unsigned char * body_frag, size_t frag_size);
void web_cache_write_end(struct cache_write_context* ctx); //return 1 on success
//return -1 on failed
//return -2 on cancel
int web_cache_write_end(struct cache_write_context* ctx);