2018-09-18 11:14:11 +08:00
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
#include <sys/poll.h>
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
#include <MESA/MESA_handle_logger.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "cache_evbase_client.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct cache_evbase_instance *instance_asyn;
|
2018-09-27 15:04:56 +08:00
|
|
|
|
int runing_over=0;
|
2018-09-18 11:14:11 +08:00
|
|
|
|
|
|
|
|
|
|
struct future_pdata
|
|
|
|
|
|
{
|
|
|
|
|
|
struct future * future;
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
char filename[256];
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void get_future_success(future_result_t* result, void * user)
|
|
|
|
|
|
{
|
2018-09-23 15:35:13 +08:00
|
|
|
|
struct tango_cache_result *res = cache_evbase_read_result(result);
|
2018-09-18 11:14:11 +08:00
|
|
|
|
struct future_pdata *pdata = (struct future_pdata *)user;
|
|
|
|
|
|
char buffer[1024];
|
|
|
|
|
|
|
2018-10-15 16:38:22 +08:00
|
|
|
|
switch(res->type)
|
2018-09-18 11:14:11 +08:00
|
|
|
|
{
|
2018-10-15 16:38:22 +08:00
|
|
|
|
case RESULT_TYPE_USERTAG:
|
|
|
|
|
|
case RESULT_TYPE_HEADER:
|
|
|
|
|
|
memcpy(buffer, res->data_frag, res->size>=1024?1023:res->size);
|
|
|
|
|
|
buffer[res->size] = '\0';
|
|
|
|
|
|
printf("%s", buffer);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case RESULT_TYPE_BODY:
|
|
|
|
|
|
fwrite(res->data_frag, res->size, 1, pdata->fp);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case RESULT_TYPE_MISS:
|
|
|
|
|
|
printf("cache not hit/fresh\n");
|
|
|
|
|
|
case RESULT_TYPE_END:
|
|
|
|
|
|
if(res->type != RESULT_TYPE_MISS)
|
|
|
|
|
|
printf("get cache over, total length: %ld\n", res->tlength);
|
|
|
|
|
|
future_destroy(pdata->future);
|
|
|
|
|
|
fclose(pdata->fp);
|
|
|
|
|
|
free(pdata);
|
|
|
|
|
|
runing_over = 1;
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:break;
|
2018-09-18 11:14:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void get_future_failed(enum e_future_error err, const char * what, void * user)
|
|
|
|
|
|
{
|
|
|
|
|
|
printf("GET fail: %s\n", what);
|
2018-09-27 15:04:56 +08:00
|
|
|
|
runing_over = 2;
|
2018-09-18 11:14:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-23 20:26:06 +08:00
|
|
|
|
void head_future_success(future_result_t* result, void * user)
|
|
|
|
|
|
{
|
|
|
|
|
|
struct tango_cache_result *res = cache_evbase_read_result(result);
|
|
|
|
|
|
struct future_pdata *pdata = (struct future_pdata *)user;
|
|
|
|
|
|
char buffer[1024];
|
|
|
|
|
|
|
|
|
|
|
|
switch(res->type)
|
|
|
|
|
|
{
|
|
|
|
|
|
case RESULT_TYPE_USERTAG:
|
|
|
|
|
|
case RESULT_TYPE_HEADER:
|
|
|
|
|
|
memcpy(buffer, res->data_frag, res->size>=1024?1023:res->size);
|
|
|
|
|
|
buffer[res->size] = '\0';
|
|
|
|
|
|
printf("%s", buffer);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case RESULT_TYPE_BODY:
|
|
|
|
|
|
assert(0);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case RESULT_TYPE_MISS:
|
|
|
|
|
|
printf("cache not hit/fresh\n");
|
|
|
|
|
|
case RESULT_TYPE_END:
|
|
|
|
|
|
if(res->type != RESULT_TYPE_MISS)
|
|
|
|
|
|
printf("HEAD cache over, total length: %ld\n", res->tlength);
|
|
|
|
|
|
future_destroy(pdata->future);
|
|
|
|
|
|
free(pdata);
|
|
|
|
|
|
runing_over = 1;
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void head_future_failed(enum e_future_error err, const char * what, void * user)
|
|
|
|
|
|
{
|
|
|
|
|
|
printf("HEAD fail: %s\n", what);
|
|
|
|
|
|
runing_over = 2;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-18 11:14:11 +08:00
|
|
|
|
void put_future_success(future_result_t* result, void * user)
|
|
|
|
|
|
{
|
|
|
|
|
|
struct future_pdata *pdata = (struct future_pdata *)user;
|
|
|
|
|
|
|
|
|
|
|
|
printf("PUT %s succ\n", pdata->filename);
|
|
|
|
|
|
future_destroy(pdata->future);
|
|
|
|
|
|
free(pdata);
|
2018-09-27 15:04:56 +08:00
|
|
|
|
runing_over = 1;
|
2018-09-18 11:14:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
void put_future_failed(enum e_future_error err, const char * what, void * user)
|
|
|
|
|
|
{
|
|
|
|
|
|
struct future_pdata *pdata = (struct future_pdata *)user;
|
|
|
|
|
|
|
|
|
|
|
|
printf("PUT %s fail: %s\n", what, pdata->filename);
|
|
|
|
|
|
future_destroy(pdata->future);
|
|
|
|
|
|
free(pdata);
|
2018-09-27 15:04:56 +08:00
|
|
|
|
runing_over = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void del_future_success(future_result_t* result, void * user)
|
|
|
|
|
|
{
|
|
|
|
|
|
struct future_pdata *pdata = (struct future_pdata *)user;
|
|
|
|
|
|
|
|
|
|
|
|
printf("DEL %s succ\n", pdata->filename);
|
|
|
|
|
|
future_destroy(pdata->future);
|
|
|
|
|
|
free(pdata);
|
|
|
|
|
|
runing_over = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
void del_future_failed(enum e_future_error err, const char * what, void * user)
|
|
|
|
|
|
{
|
|
|
|
|
|
struct future_pdata *pdata = (struct future_pdata *)user;
|
|
|
|
|
|
|
|
|
|
|
|
printf("DEL %s fail: %s\n", pdata->filename, what);
|
|
|
|
|
|
future_destroy(pdata->future);
|
|
|
|
|
|
free(pdata);
|
|
|
|
|
|
runing_over = 1;
|
2018-09-18 11:14:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char * get_file_content(const char *filename, size_t *filelen_out)
|
|
|
|
|
|
{
|
|
|
|
|
|
char *buffer;
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
size_t filelen = 0;
|
|
|
|
|
|
struct stat filestat;
|
|
|
|
|
|
int readlen;
|
|
|
|
|
|
|
|
|
|
|
|
fp = fopen(filename, "rb");
|
|
|
|
|
|
if(fp == NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
printf("fopen file %s failed.\n", filename);
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
if(fstat(fileno(fp), &filestat))
|
|
|
|
|
|
{
|
|
|
|
|
|
printf("fstat %s failed.\n", filename);
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
buffer = (char *)malloc(filestat.st_size);
|
|
|
|
|
|
|
|
|
|
|
|
while(filelen < (size_t)filestat.st_size)
|
|
|
|
|
|
{
|
|
|
|
|
|
readlen = fread(buffer + filelen, 1, filestat.st_size - filelen, fp);
|
|
|
|
|
|
if(readlen < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
printf("read error: %s\n", strerror(errno));
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
filelen += readlen;
|
|
|
|
|
|
}
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
*filelen_out = filestat.st_size;
|
|
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
|
{
|
|
|
|
|
|
int n, index=0;
|
|
|
|
|
|
char method[16], filename_in[256], filename_out[256], *p;
|
2018-10-15 16:38:22 +08:00
|
|
|
|
struct tango_cache_meta_put putmeta;
|
|
|
|
|
|
struct tango_cache_meta_get getmeta;
|
2018-09-18 11:14:11 +08:00
|
|
|
|
struct future_pdata *pdata;
|
|
|
|
|
|
struct cache_evbase_ctx *ctx;
|
|
|
|
|
|
void *runtime_log;
|
2018-11-07 15:05:55 +08:00
|
|
|
|
struct tango_cache_parameter *parameter;
|
2018-09-18 11:14:11 +08:00
|
|
|
|
|
|
|
|
|
|
if(argc != 2 && argc!=3)
|
|
|
|
|
|
{
|
2018-09-27 15:04:56 +08:00
|
|
|
|
printf("USGAE: %s <PUT/PUTONCE/PUTONCEEV/GET/DEL:filename> [get_out_file_index]\n", argv[0]);
|
2018-09-18 11:14:11 +08:00
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
if(argc==3)
|
|
|
|
|
|
{
|
|
|
|
|
|
index = atoi(argv[2]);
|
|
|
|
|
|
}
|
2021-03-22 14:10:43 +08:00
|
|
|
|
|
|
|
|
|
|
future_promise_library_init(NULL);
|
|
|
|
|
|
|
2018-09-18 11:14:11 +08:00
|
|
|
|
runtime_log = MESA_create_runtime_log_handle("./runtime.log", 10);
|
|
|
|
|
|
if(NULL==runtime_log)
|
|
|
|
|
|
{
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
2018-09-30 19:20:06 +08:00
|
|
|
|
|
|
|
|
|
|
cache_evbase_global_init();
|
2018-11-07 15:05:55 +08:00
|
|
|
|
parameter = cache_evbase_parameter_new("./pangu_tg_cahce.conf", "TANGO_CACHE", runtime_log);
|
|
|
|
|
|
assert(parameter != NULL);
|
|
|
|
|
|
instance_asyn = cache_evbase_instance_new(parameter, runtime_log);
|
2018-09-18 11:14:11 +08:00
|
|
|
|
assert(instance_asyn!=NULL);
|
|
|
|
|
|
|
|
|
|
|
|
pdata = (struct future_pdata *)malloc(sizeof(struct future_pdata));
|
|
|
|
|
|
|
|
|
|
|
|
if(sscanf(argv[1], "%[^:]:%1023s%n", method, filename_in, &n) != 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
assert(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
if(strlen(filename_in) > 0)
|
|
|
|
|
|
{
|
2018-10-15 16:38:22 +08:00
|
|
|
|
memset(&putmeta, 0, sizeof(struct tango_cache_meta_put));
|
|
|
|
|
|
memset(&getmeta, 0, sizeof(struct tango_cache_meta_get));
|
|
|
|
|
|
putmeta.url = filename_in;
|
|
|
|
|
|
putmeta.std_hdr[HDR_CONTENT_TYPE] = "Content-Type: maintype/subtype";
|
|
|
|
|
|
putmeta.std_hdr[HDR_CONTENT_ENCODING] = "Content-Encoding: gzip";
|
|
|
|
|
|
putmeta.usertag = "Etag: hgdkqkwdwqekdfjwjfjwelkjfkwfejwhf\r\n";
|
|
|
|
|
|
putmeta.usertag_len = strlen(putmeta.usertag);
|
|
|
|
|
|
|
|
|
|
|
|
getmeta.url = filename_in;
|
2018-09-18 11:14:11 +08:00
|
|
|
|
|
|
|
|
|
|
p = method;
|
|
|
|
|
|
while(*p=='\r'||*p=='\n') p++;
|
|
|
|
|
|
assert(*p!='\0');
|
|
|
|
|
|
|
|
|
|
|
|
if(!strcasecmp(p, "GET"))
|
|
|
|
|
|
{
|
|
|
|
|
|
sprintf(filename_out, "file_index_%u.bin", index);
|
2021-03-22 14:10:43 +08:00
|
|
|
|
pdata->future = future_create("_get", get_future_success, get_future_failed, (void *)pdata);
|
2018-09-18 11:14:11 +08:00
|
|
|
|
pdata->fp = fopen(filename_out, "w");
|
|
|
|
|
|
|
2018-12-14 15:07:09 +08:00
|
|
|
|
cache_evbase_fetch_object(instance_asyn, pdata->future, &getmeta, OBJECT_IN_UNKNOWN);
|
2018-09-27 15:04:56 +08:00
|
|
|
|
}
|
2018-10-23 20:26:06 +08:00
|
|
|
|
else if(!strcasecmp(p, "HEAD"))
|
|
|
|
|
|
{
|
2021-03-22 14:10:43 +08:00
|
|
|
|
pdata->future = future_create("_head", head_future_success, head_future_failed, (void *)pdata);
|
2018-10-23 20:26:06 +08:00
|
|
|
|
cache_evbase_head_object(instance_asyn, pdata->future, &getmeta);
|
|
|
|
|
|
}
|
2018-09-27 15:04:56 +08:00
|
|
|
|
else if(!strcasecmp(p, "DEL"))
|
|
|
|
|
|
{
|
2021-03-22 14:10:43 +08:00
|
|
|
|
pdata->future = future_create("_del", del_future_success, del_future_failed, (void *)pdata);
|
2018-09-27 15:04:56 +08:00
|
|
|
|
sprintf(pdata->filename, "%s", filename_in);
|
|
|
|
|
|
cache_evbase_delete_object(instance_asyn, pdata->future, filename_in);
|
2018-09-18 11:14:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if(!strcasecmp(p, "PUTONCE"))
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t filelen;
|
|
|
|
|
|
p = get_file_content(filename_in, &filelen);
|
2021-03-22 14:10:43 +08:00
|
|
|
|
pdata->future = future_create("_putonce", put_future_success, put_future_failed, pdata);
|
|
|
|
|
|
|
2018-10-15 16:38:22 +08:00
|
|
|
|
cache_evbase_upload_once_data(instance_asyn, pdata->future, PUT_MEM_FREE, p, filelen, &putmeta, pdata->filename, 256);
|
2018-09-21 14:50:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if(!strcasecmp(p, "PUTONCEEV"))
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t readlen;
|
2021-03-22 14:10:43 +08:00
|
|
|
|
pdata->future = future_create("_putonceev", put_future_success, put_future_failed, (void *)pdata);
|
2018-09-21 14:50:41 +08:00
|
|
|
|
struct evbuffer *evbuf = evbuffer_new();
|
|
|
|
|
|
char buffer[1024];
|
|
|
|
|
|
|
|
|
|
|
|
FILE *fp = fopen(filename_in, "rb");
|
|
|
|
|
|
while(!feof(fp))
|
|
|
|
|
|
{
|
|
|
|
|
|
readlen = fread(buffer, 1, 1024, fp);
|
|
|
|
|
|
if(readlen < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
assert(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
evbuffer_add(evbuf, buffer, readlen);
|
|
|
|
|
|
}
|
|
|
|
|
|
fclose(fp);
|
2018-10-15 16:38:22 +08:00
|
|
|
|
cache_evbase_upload_once_evbuf(instance_asyn, pdata->future, evbuf, &putmeta, pdata->filename, 256);
|
2018-09-18 11:14:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-03-22 14:10:43 +08:00
|
|
|
|
pdata->future = future_create("_default", put_future_success, put_future_failed, (void *)pdata);
|
2018-09-18 11:14:11 +08:00
|
|
|
|
|
2018-10-15 16:38:22 +08:00
|
|
|
|
ctx = cache_evbase_update_start(instance_asyn, pdata->future, &putmeta);
|
2018-09-18 11:14:11 +08:00
|
|
|
|
char buffer[1024];
|
|
|
|
|
|
FILE *fp = fopen(filename_in, "r");
|
|
|
|
|
|
while(!feof(fp))
|
|
|
|
|
|
{
|
|
|
|
|
|
n = fread(buffer, 1, 1024, fp);
|
|
|
|
|
|
assert(n>=0);
|
2018-09-21 14:50:41 +08:00
|
|
|
|
cache_evbase_update_frag_data(ctx, PUT_MEM_COPY, buffer, n);
|
2018-09-18 11:14:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-14 15:07:09 +08:00
|
|
|
|
cache_evbase_update_end(ctx, pdata->filename, 256);
|
2018-09-18 11:14:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
printf("Waiting to finish.......\n");
|
2018-10-23 20:26:06 +08:00
|
|
|
|
//static int num=0;
|
2018-09-27 15:04:56 +08:00
|
|
|
|
while(!runing_over)
|
2018-09-18 11:14:11 +08:00
|
|
|
|
{
|
2018-09-21 14:50:41 +08:00
|
|
|
|
/*if(++num==10)
|
|
|
|
|
|
{
|
|
|
|
|
|
cache_evbase_update_end(ctx);
|
|
|
|
|
|
}*/
|
2018-09-18 11:14:11 +08:00
|
|
|
|
sleep(1);
|
|
|
|
|
|
}
|
2018-09-27 15:04:56 +08:00
|
|
|
|
if(runing_over==2) //GETʱ<54><CAB1>ʱɾ<CAB1><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
{
|
|
|
|
|
|
sleep(5);
|
|
|
|
|
|
}
|
2018-09-18 11:14:11 +08:00
|
|
|
|
|
|
|
|
|
|
struct cache_statistics out;
|
|
|
|
|
|
cache_evbase_get_statistics(instance_asyn, &out);
|
2018-12-14 15:07:09 +08:00
|
|
|
|
printf("-------------------------------------------------------------------------------------------\n"
|
|
|
|
|
|
"get_recv: %llu, get_http: %llu, get_redis: %llu, get_fail_http: %llu, get_fail_redis: %llu, get_miss: %llu\n"
|
|
|
|
|
|
"put_recv: %llu, put_http: %llu, put_redis: %llu, put_fail_http: %llu, put_fail_redis: %llu\n"
|
|
|
|
|
|
"del_recv: %llu, del_succ: %llu, del_fail: %llu, drop_num: %llu, session_redis: %llu, session_http: %llu, memory: %llu\n",
|
|
|
|
|
|
out.get_recv_num, out.get_succ_http, out.get_succ_redis, out.get_err_http, out.get_err_redis, out.get_miss_num,
|
|
|
|
|
|
out.put_recv_num, out.put_succ_http, out.put_succ_redis, out.put_err_http, out.put_err_redis,
|
|
|
|
|
|
out.del_recv_num, out.del_succ_num, out.del_error_num, out.totaldrop_num, out.session_redis, out.session_http, out.memory_used);
|
2018-09-18 11:14:11 +08:00
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|