- int reload_prdt_config()
- {
- int ret = 0;
- int total_num = 0;
- int old_num = 0;
- int added_num = 0;
- zcache_product_param_t param;
- char fname[ZCACHE_MAX_FILE_LEN] = {0};
- char const * err_msg = NULL;
- static product_dyconf_t old_prdts[ZCACHE_MAX_PRODUCT_NUM];
- static zcache_node_t nodes[ZCACHE_MAX_NODE_NUM];
- static string line(2048,'\0');
- //product=IMAGE,token=image_token,block_num=1000,max_query_persec=10000,min_cache_item=10000,del_strategy=1
- //8
- static const char * prdt_conf_fmt= "product=%s token=%s block_num=%u max_query_persec=%u "
- "min_cache_item=%u "
- "random_seed=%u is_live_time=%u "
- "del_strategy=%u";
- //7
- static const char * prdt_conf_fmt_without_token = "product=%s block_num=%u max_query_persec=%u "
- "min_cache_item=%u "
- "random_seed=%u is_live_time=%u "
- "del_strategy=%u";
- //6
- static const char * prdt_conf_fmt_without_token_random = "product=%s block_num=%u max_query_persec=%u "
- "min_cache_item=%u "
- "is_live_time=%u "
- "del_strategy=%u";
- //6
- static const char * prdt_conf_fmt_without_token_live = "product=%s block_num=%u max_query_persec=%u "
- "min_cache_item=%u del_strategy=%u "
- "random_seed=%u "
- "del_strategy=%u";
- //7
- static const char * prdt_conf_fmt_without_random = "product=%s token=%s block_num=%u max_query_persec=%u "
- "min_cache_item=%u "
- "is_live_time=%u "
- "del_strategy=%u";
- //6
- static const char * prdt_conf_fmt_without_random_live = "product=%s token=%s block_num=%u max_query_persec=%u "
- "min_cache_item=%u del_strategy=%u";
- //7
- static const char * prdt_conf_fmt_without_live = "product=%s token=%s block_num=%u max_query_persec=%u "
- "min_cache_item=%u "
- "random_seed=%u "
- "del_strategy=%u";
- //5
- static const char * prdt_conf_fmt_without_token_random_live = "product=%s block_num=%u max_query_persec=%u "
- "min_cache_item=%u del_strategy=%u";
- static char prdt_conf_fmt_reg_str[1024]={0};
- bool rept_req_buf_changed = false;
- snprintf(prdt_conf_fmt_reg_str, 1024, "product=[A-Za-z0-9_]{1,%d} "
- "(token=[A-Za-z0-9_]{1,%d} ){0,1}"
- "block_num=[1-9][0-9]{0,5} "
- "max_query_persec=[1-9][0-9]{0,9} "
- "min_cache_item=[0-9]{1,9} "
- "(random_seed=[0-9]{1,9} ){0,1}"
- "(is_live_time=[0-9]{1,9} ){0,1}"
- "del_strategy=[01]",
- ZCACHE_MAX_PRODUCT_NAME_LEN - 1, ZCACHE_MAX_TOKEN_LEN - 1);
- static spreg_t *prdt_re = spreg_init(prdt_conf_fmt_reg_str, &err_msg);
- if (prdt_re == NULL)
- {
- URGENT_LOG("spret_init error [msg:%s]", err_msg);
- return -1;
- }
- snprintf(fname, ZCACHE_MAX_FILE_LEN, "%s/%s", gConf.conf_path, gConf.prdt_conf_file);
- ifstream fs(fname);
- if (!fs)
- {
- URGENT_LOG("fail to open prdt config file [fname:%s]", fname);
- ret = -1;
- goto label_exit;
- }
- //读取配置
- gDyconf.prdt_last_load_time = time(NULL);
- gDyconf.prdt_need_to_reload = false;
- while (getline(fs, line))
- {
- char name[ZCACHE_MAX_PRODUCT_NAME_LEN]={0};
- char token[ZCACHE_MAX_TOKEN_LEN] ={0} ;
- unsigned int block_num = 0;
- unsigned int max_query_persec = 0;
- unsigned int min_cache_item = 0;
- unsigned int del_strategy = 0;
- unsigned int is_live_time = 0;
- unsigned int random_seed = 0;
- char *find_ret = NULL;
- long ccount = 0;
- line = trim(line);
- if (line.size() <= 1 || line[0] == '#')
- continue;
- replace(line.begin(), line.end(), ',', ' ');
- line = trim(line);
- //replace(line.begin(), line.end(), ' ', ',');
- ccount = count(line.begin(), line.end(), ' ');
- if (spreg_match(prdt_re, line.c_str(), line.size()) <= 0)
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- if (ccount == 7)
- {
- if (sscanf(line.c_str(), prdt_conf_fmt, name, token, &block_num,
- &max_query_persec, &min_cache_item, &random_seed, &is_live_time, &del_strategy) != 8)
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- }
- else if (ccount == 6)
- {
- find_ret = find_prdt_conf_spec(const_cast<char*>(line.data()));
- if (strncmp(find_ret, "101", 3) == 0)
- {
- if (sscanf(line.c_str(), prdt_conf_fmt_without_random, name, token, &block_num,
- &max_query_persec, &min_cache_item, &is_live_time, &del_strategy) != 7)
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- }
- else if (strncmp(find_ret, "011", 3) == 0)
- {
- if (sscanf(line.c_str(), prdt_conf_fmt_without_token, name, &block_num,
- &max_query_persec, &min_cache_item, &random_seed, &is_live_time, &del_strategy) != 7)
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- }
- else if (strncmp(find_ret, "110", 3) == 0)
- {
- if (sscanf(line.c_str(), prdt_conf_fmt_without_live, name, token, &block_num,
- &max_query_persec, &min_cache_item, &random_seed, &del_strategy) != 7)
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- }
- else
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- }
- else if (ccount == 5)
- {
- find_ret = find_prdt_conf_spec(const_cast<char*>(line.data()));
- if (strncmp(find_ret, "001", 3) == 0)
- {
- if (sscanf(line.c_str(), prdt_conf_fmt_without_token_random, name, &block_num,
- &max_query_persec, &min_cache_item, &is_live_time, &del_strategy) != 6)
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- }
- else if (strncmp(find_ret, "010", 3) == 0)
- {
- if (sscanf(line.c_str(), prdt_conf_fmt_without_token_live, name, &block_num,
- &max_query_persec, &min_cache_item, &random_seed, &del_strategy) != 6)
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- }
- else if (strncmp(find_ret, "100", 3) == 0)
- {
- if (sscanf(line.c_str(), prdt_conf_fmt_without_random_live, name, token, &block_num,
- &max_query_persec, &min_cache_item, &del_strategy) != 6)
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- }
- else
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- }
- else if (ccount == 4)
- {
- if (sscanf(line.c_str(), prdt_conf_fmt_without_token_random_live, name, &block_num,
- &max_query_persec, &min_cache_item, &del_strategy) != 5)
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- }
- else
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("product configure line error [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- if (block_num == 0)
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("block num should bigger than 0 [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- if (max_query_persec == 0)
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("product's max_query_persec must be bigger than 0 [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- if (del_strategy != 0 && del_strategy != 1)
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("product's del_strategy range[0 1] [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- if (is_live_time != 0 && is_live_time > 99999999)
- {
- replace(line.begin(), line.end(), ' ', ',');
- URGENT_LOG("product's is_live_time range[0 99999999] [line:%s]", line.c_str());
- ret = -1;
- goto label_exit;
- }
- NOTICE_LOG("read prdt config [name:%s, token:%s, block_num:%u, min_cache_item:%u, "
- "max_req_persec:%u, del_strategy:%d]", name, token,
- block_num, min_cache_item, max_query_persec, del_strategy);
- param.del_strategy = del_strategy;
- param.maximal_request_num_persec = max_query_persec;
- param.minimal_cache_item_num = min_cache_item;
- param.block_num = block_num;
- //param.random_seed = random_seed;
- //param.is_live_time = is_live_time;
- gDyconf.new_prdts[total_num].del_strategy = param.del_strategy;
- gDyconf.new_prdts[total_num].max_query_per_sec = param.maximal_request_num_persec;
- gDyconf.new_prdts[total_num].min_cache_item = param.minimal_cache_item_num;
- gDyconf.new_prdts[total_num].block_num = block_num;
- gDyconf.new_prdts[total_num].random_seed = random_seed;
- gDyconf.new_prdts[total_num].is_live_time = is_live_time;
- snprintf(gDyconf.new_prdts[total_num].token, ZCACHE_MAX_TOKEN_LEN ,"%s", token);
- snprintf(gDyconf.new_prdts[total_num].name, ZCACHE_MAX_PRODUCT_NAME_LEN, "%s", name);
- ProductPuppet prdt(name);
- if (!prdt)
- {
- added_num ++;
- gDyconf.new_prdts[total_num].is_new = 1;
- }
- else
- {
- gDyconf.new_prdts[total_num].is_new = 0;
- /** 判断是否参数发生变化, 如果有通知所有cacheserver并写di */
- zcache_product_param_t old_param;
- prdt.GetParam(old_param);
- if (old_param.block_num != param.block_num)
- {
- URGENT_LOG("prdt block num can not be changed [prdt:%s, old:%u, new:%u]",
- name, old_param.block_num, param.block_num);
- ret = -1;
- goto label_exit;
- }
- if (old_param.block_num != param.block_num)
- {
- URGENT_LOG("You Cannot change block num of product [prdt:%s, old_blk_num:%u, new_blk_num:%u]",
- name, old_param.block_num, param.block_num);
- ret = -1;
- goto label_exit;
- }
- /**
- if (prdt.Auth(token) == false)
- {
- FATAL_LOG("prdt token can not be changed [prdt:%s, old_token:%s, new_token:%s]",
- name, prdt.GetToken(), token);
- ret = -1;
- goto label_exit;
- }
- */
- prdt.SetToken(token);
- if (old_param.maximal_request_num_persec != param.maximal_request_num_persec
- || old_param.minimal_cache_item_num != param.minimal_cache_item_num
- || ((bool)(old_param.del_strategy)) != ((bool)(param.del_strategy)))
- {
- unsigned int csvr_num = 0;
- prdt.SetParam(param);
- /** 需要向所有cacheserver发送参数调整信息 */
- csvr_num = CacheserverMgr::Instance()->GetAllCsvrloads(false, ZCACHE_MAX_NODE_NUM, nodes);
- param.maximal_request_num_persec = (unsigned int)1.0*param.maximal_request_num_persec/csvr_num;
- param.minimal_cache_item_num = (unsigned int)1.0*param.minimal_cache_item_num/csvr_num;
- for (unsigned int i = 0; i < csvr_num; i ++)
- {
- send_prdt_param_adj_req(nodes[i].ip, nodes[i].port, name, param);
- }
- rept_req_buf_changed = true;
- }
- }
- total_num ++;
- }
- //说明有减少的, 需要先处理减少的, 然后处理增加的
- old_num = ProductMgr::Instance()->PrdtNum();
- if (total_num - added_num != old_num)
- {
- old_num = ProductMgr::Instance()->GetAllPrdts(old_prdts, ZCACHE_MAX_PRODUCT_NUM);
- for (int i = 0; i < old_num; i ++)
- {
- bool exist = false;
- for (int j = 0; j < total_num; j ++)
- {
- if (gDyconf.new_prdts[j].is_new)
- {
- continue;
- }
- if (strcmp(old_prdts[i].name, gDyconf.new_prdts[j].name) == 0)
- {
- exist = true;
- break;
- }
- }
- if (!exist)
- {
- unsigned int csvr_num = 0;
- /** 需要通知所有cacheserver删除产品线, 并写di*/
- ProductMgr::Instance()->DelProduct(old_prdts[i].name);
- transdi_del_prdt(old_prdts[i].name);
- csvr_num = CacheserverMgr::Instance()->GetAllCsvrloads(false, ZCACHE_MAX_NODE_NUM, nodes);
- for (unsigned int m = 0; m < csvr_num; m ++)
- {
- send_del_prdt_req(nodes[m].ip, nodes[m].port, old_prdts[i].name);
- }
- rept_req_buf_changed = true;
- }
- }
- }
- //有增加
- if (added_num > 0)
- {
- for (int i = 0; i < total_num; i ++)
- {
- if (gDyconf.new_prdts[i].is_new)
- {
- unsigned int csvr_num = 0;
- param.block_num = gDyconf.new_prdts[i].block_num;
- param.del_strategy = gDyconf.new_prdts[i].del_strategy;
- param.maximal_request_num_persec = gDyconf.new_prdts[i].max_query_per_sec;
- param.minimal_cache_item_num = gDyconf.new_prdts[i].min_cache_item;
- //param.random_seed = gDyconf.new_prdts[i].random_seed;
- //param.is_live_time = gDyconf.new_prdts[i].is_live_time;
- if (ProductMgr::Instance()->AddProduct(gDyconf.new_prdts[i].name, param,
- gDyconf.new_prdts[i].token,
- &gDyconf.new_prdts[i].random_seed) < 0)
- {
- URGENT_LOG("fail to add product [prdt:%s]", gDyconf.new_prdts[i].name);
- ret = -1;
- goto label_exit;
- }
- rept_req_buf_changed = true;
- transdi_add_prdt(gDyconf.new_prdts[i].name, param, gDyconf.new_prdts[i].token);
- /** 需要向所有cacheserver发送参数调整信息 */
- csvr_num = CacheserverMgr::Instance()->GetAllCsvrloads(false, ZCACHE_MAX_NODE_NUM, nodes);
- param.maximal_request_num_persec =(unsigned int)(param.maximal_request_num_persec*1.0/csvr_num);
- param.minimal_cache_item_num = (unsigned int)(param.minimal_cache_item_num*1.0/csvr_num);
- for (unsigned int f = 0; f < csvr_num; f ++)
- {
- send_prdt_param_adj_req(nodes[f].ip, nodes[f].port,gDyconf.new_prdts[i].name, param);
- }
- }
- }
- }
- if (rept_req_buf_changed)
- {
- unsigned int csvr_num_now = CacheserverMgr::Instance()->CsvrNum();
- ProductMgr::Instance()->RebuildReptReqBuf(csvr_num_now);
- }
- label_exit:
- return ret;
- }
- void dynamic_load_prdt_config()
- {
- struct stat prdt_stat;
- char fname[ZCACHE_MAX_FILE_LEN] = {0};
- static product_dyconf_t old_prdts[ZCACHE_MAX_PRODUCT_NUM];
- snprintf(fname, ZCACHE_MAX_FILE_LEN, "%s/%s", gConf.conf_path, gConf.prdt_conf_file);
- if (!gDyconf.prdt_need_to_reload)
- {
- if (stat(fname, &prdt_stat) != 0)
- URGENT_LOG("fail to stat [filename:%s]", fname);
- else
- {
- if (prdt_stat.st_mtim.tv_sec > gDyconf.prdt_last_load_time)
- {
- gDyconf.prdt_change_detect_time = prdt_stat.st_mtim.tv_sec;
- gDyconf.prdt_need_to_reload = true;
- }
- }
- goto label_exit;
- }
- else
- {
- if (time(NULL) < (time_t)(gConf.config_change_confirm_msec/1000 + gDyconf.prdt_change_detect_time))
- goto label_exit;
- }
- //double check
- if (stat(fname, &prdt_stat) != 0)
- URGENT_LOG("fail to stat [filename:%s]", fname);
- else
- { //又改了
- if (prdt_stat.st_mtim.tv_sec != gDyconf.prdt_change_detect_time)
- {
- gDyconf.prdt_change_detect_time = prdt_stat.st_mtime;
- goto label_exit;
- }
- }
- NOTICE_LOG("dynamic reload prdt configure [filename:%s, last_load_time:%lu, "
- "change_detect_time:%lu, now:%lu]", fname, gDyconf.prdt_last_load_time,
- gDyconf.prdt_change_detect_time, time(NULL));
- if (reload_prdt_config() < 0)
- {
- URGENT_LOG("reload_prdt_config error");
- }
- label_exit:
- return;
- }