Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 31st, 2012  |  syntax: None  |  size: 18.22 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. int reload_prdt_config()
  2. {
  3.     int ret = 0;
  4.     int total_num = 0;
  5.     int old_num = 0;
  6.     int added_num = 0;
  7.     zcache_product_param_t param;
  8.     char fname[ZCACHE_MAX_FILE_LEN] = {0};
  9.     char const * err_msg = NULL;
  10.     static product_dyconf_t  old_prdts[ZCACHE_MAX_PRODUCT_NUM];
  11.     static zcache_node_t     nodes[ZCACHE_MAX_NODE_NUM];
  12.     static string line(2048,'\0');
  13.     //product=IMAGE,token=image_token,block_num=1000,max_query_persec=10000,min_cache_item=10000,del_strategy=1
  14.        
  15.         //8
  16.     static const char * prdt_conf_fmt= "product=%s token=%s block_num=%u max_query_persec=%u "
  17.                                        "min_cache_item=%u "
  18.                                                                            "random_seed=%u is_live_time=%u "
  19.                                                                            "del_strategy=%u";
  20.                                                                        
  21.  
  22.         //7
  23.     static const char * prdt_conf_fmt_without_token = "product=%s block_num=%u max_query_persec=%u "
  24.                                                       "min_cache_item=%u "
  25.                                                                                                           "random_seed=%u is_live_time=%u "
  26.                                                                                                           "del_strategy=%u";
  27.  
  28.         //6
  29.     static const char * prdt_conf_fmt_without_token_random = "product=%s block_num=%u max_query_persec=%u "
  30.                                        "min_cache_item=%u "
  31.                                                                            "is_live_time=%u "
  32.                                                                            "del_strategy=%u";
  33.  
  34.         //6
  35.     static const char * prdt_conf_fmt_without_token_live = "product=%s block_num=%u max_query_persec=%u "
  36.                                        "min_cache_item=%u del_strategy=%u "
  37.                                                                            "random_seed=%u "
  38.                                                                            "del_strategy=%u";
  39.  
  40.         //7
  41.     static const char * prdt_conf_fmt_without_random = "product=%s token=%s block_num=%u max_query_persec=%u "
  42.                                        "min_cache_item=%u "
  43.                                                                            "is_live_time=%u "
  44.                                                                            "del_strategy=%u";
  45.  
  46.         //6
  47.     static const char * prdt_conf_fmt_without_random_live = "product=%s token=%s block_num=%u max_query_persec=%u "
  48.                                        "min_cache_item=%u del_strategy=%u";
  49.  
  50.         //7
  51.     static const char * prdt_conf_fmt_without_live = "product=%s token=%s block_num=%u max_query_persec=%u "
  52.                                        "min_cache_item=%u "
  53.                                                                            "random_seed=%u "
  54.                                                                            "del_strategy=%u";
  55.  
  56.         //5
  57.     static const char * prdt_conf_fmt_without_token_random_live = "product=%s block_num=%u max_query_persec=%u "
  58.                                        "min_cache_item=%u del_strategy=%u";
  59.  
  60.     static char prdt_conf_fmt_reg_str[1024]={0};
  61.     bool rept_req_buf_changed = false;
  62.     snprintf(prdt_conf_fmt_reg_str, 1024, "product=[A-Za-z0-9_]{1,%d} "
  63.              "(token=[A-Za-z0-9_]{1,%d} ){0,1}"
  64.              "block_num=[1-9][0-9]{0,5} "
  65.              "max_query_persec=[1-9][0-9]{0,9} "
  66.              "min_cache_item=[0-9]{1,9} "
  67.                          "(random_seed=[0-9]{1,9} ){0,1}"
  68.                          "(is_live_time=[0-9]{1,9} ){0,1}"
  69.              "del_strategy=[01]",
  70.              ZCACHE_MAX_PRODUCT_NAME_LEN - 1, ZCACHE_MAX_TOKEN_LEN - 1);
  71.     static spreg_t *prdt_re = spreg_init(prdt_conf_fmt_reg_str, &err_msg);
  72.     if (prdt_re == NULL)
  73.     {
  74.         URGENT_LOG("spret_init error [msg:%s]", err_msg);
  75.         return -1;
  76.     }
  77.  
  78.     snprintf(fname, ZCACHE_MAX_FILE_LEN, "%s/%s", gConf.conf_path, gConf.prdt_conf_file);
  79.     ifstream fs(fname);
  80.     if (!fs)
  81.     {
  82.         URGENT_LOG("fail to open prdt config  file [fname:%s]",  fname);
  83.         ret = -1;
  84.         goto label_exit;
  85.     }
  86.     //读取配置
  87.     gDyconf.prdt_last_load_time = time(NULL);
  88.     gDyconf.prdt_need_to_reload = false;
  89.  
  90.     while (getline(fs, line))
  91.     {
  92.         char name[ZCACHE_MAX_PRODUCT_NAME_LEN]={0};
  93.         char token[ZCACHE_MAX_TOKEN_LEN] ={0} ;
  94.         unsigned int block_num = 0;
  95.         unsigned int max_query_persec = 0;
  96.         unsigned int min_cache_item = 0;
  97.         unsigned int del_strategy = 0;
  98.                 unsigned int is_live_time = 0;
  99.                 unsigned int random_seed = 0;
  100.                 char *find_ret = NULL;
  101.         long ccount = 0;
  102.         line = trim(line);
  103.         if (line.size() <= 1 || line[0] == '#')
  104.             continue;
  105.         replace(line.begin(), line.end(), ',', ' ');
  106.         line = trim(line);
  107.         //replace(line.begin(), line.end(), ' ', ',');
  108.         ccount = count(line.begin(), line.end(), ' ');
  109.         if (spreg_match(prdt_re, line.c_str(), line.size()) <= 0)
  110.         {
  111.             replace(line.begin(), line.end(), ' ', ',');
  112.             URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
  113.             ret = -1;
  114.             goto label_exit;
  115.         }
  116.  
  117.         if (ccount == 7)
  118.                 {
  119.             if (sscanf(line.c_str(), prdt_conf_fmt, name, token, &block_num,
  120.                        &max_query_persec, &min_cache_item, &random_seed, &is_live_time, &del_strategy) != 8)
  121.                         {
  122.                 replace(line.begin(), line.end(), ' ', ',');
  123.                 URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
  124.                 ret = -1;
  125.                 goto label_exit;
  126.                         }
  127.                
  128.                 }
  129.                 else if (ccount == 6)
  130.                 {
  131.                         find_ret = find_prdt_conf_spec(const_cast<char*>(line.data()));
  132.  
  133.                         if (strncmp(find_ret, "101", 3) == 0)
  134.                         {
  135.                                 if (sscanf(line.c_str(), prdt_conf_fmt_without_random, name, token, &block_num,
  136.                        &max_query_persec, &min_cache_item, &is_live_time, &del_strategy) != 7)
  137.                                 {
  138.                                         replace(line.begin(), line.end(), ' ', ',');
  139.                                         URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
  140.                                         ret = -1;
  141.                                         goto label_exit;
  142.                                 }
  143.                        
  144.                         }
  145.                         else if (strncmp(find_ret, "011", 3) == 0)
  146.                         {
  147.                        
  148.                                 if (sscanf(line.c_str(), prdt_conf_fmt_without_token, name, &block_num,
  149.                        &max_query_persec, &min_cache_item, &random_seed, &is_live_time, &del_strategy) != 7)
  150.                                 {
  151.                                         replace(line.begin(), line.end(), ' ', ',');
  152.                                         URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
  153.                                         ret = -1;
  154.                                         goto label_exit;
  155.                                 }
  156.  
  157.                         }
  158.                         else if (strncmp(find_ret, "110", 3) == 0)
  159.                         {
  160.                                 if (sscanf(line.c_str(), prdt_conf_fmt_without_live, name, token, &block_num,
  161.                        &max_query_persec, &min_cache_item, &random_seed, &del_strategy) != 7)
  162.                                 {
  163.                                         replace(line.begin(), line.end(), ' ', ',');
  164.                                         URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
  165.                                         ret = -1;
  166.                                         goto label_exit;
  167.                                 }
  168.  
  169.                         }
  170.                         else
  171.                         {
  172.                                 replace(line.begin(), line.end(), ' ', ',');
  173.                                 URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
  174.                                 ret = -1;
  175.                                 goto label_exit;
  176.                         }
  177.                 }
  178.                 else if (ccount == 5)
  179.         {
  180.                         find_ret = find_prdt_conf_spec(const_cast<char*>(line.data()));
  181.  
  182.                         if (strncmp(find_ret, "001", 3) == 0)
  183.                         {
  184.                                 if (sscanf(line.c_str(), prdt_conf_fmt_without_token_random, name, &block_num,
  185.                        &max_query_persec, &min_cache_item, &is_live_time, &del_strategy) != 6)
  186.                                 {
  187.                                         replace(line.begin(), line.end(), ' ', ',');
  188.                                         URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
  189.                                         ret = -1;
  190.                                         goto label_exit;
  191.                                 }
  192.  
  193.  
  194.                         }
  195.                         else if (strncmp(find_ret, "010", 3) == 0)
  196.                         {
  197.                                 if (sscanf(line.c_str(), prdt_conf_fmt_without_token_live,  name, &block_num,
  198.                        &max_query_persec, &min_cache_item, &random_seed, &del_strategy) != 6)
  199.                                 {
  200.                                         replace(line.begin(), line.end(), ' ', ',');
  201.                                         URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
  202.                                         ret = -1;
  203.                                         goto label_exit;
  204.                                 }
  205.  
  206.                        
  207.                         }
  208.                         else if (strncmp(find_ret, "100", 3) == 0)
  209.                         {
  210.                                 if (sscanf(line.c_str(), prdt_conf_fmt_without_random_live,  name, token, &block_num,
  211.                        &max_query_persec, &min_cache_item, &del_strategy) != 6)
  212.                                 {
  213.                                         replace(line.begin(), line.end(), ' ', ',');
  214.                                         URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
  215.                                         ret = -1;
  216.                                         goto label_exit;
  217.                                 }
  218.  
  219.                         }
  220.                         else
  221.                         {
  222.                                 replace(line.begin(), line.end(), ' ', ',');
  223.                                 URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
  224.                                 ret = -1;
  225.                                 goto label_exit;
  226.                         }
  227.         }
  228.         else if (ccount == 4)
  229.         {
  230.             if (sscanf(line.c_str(), prdt_conf_fmt_without_token_random_live, name, &block_num,
  231.                        &max_query_persec, &min_cache_item, &del_strategy) != 5)
  232.             {
  233.                 replace(line.begin(), line.end(), ' ', ',');
  234.                 URGENT_LOG("prdt configure line error [line:%s]", line.c_str());
  235.                 ret = -1;
  236.                 goto label_exit;
  237.             }
  238.         }
  239.         else
  240.         {
  241.             replace(line.begin(), line.end(), ' ', ',');
  242.             URGENT_LOG("product configure line error [line:%s]", line.c_str());
  243.             ret = -1;
  244.             goto label_exit;
  245.         }
  246.  
  247.         if (block_num == 0)
  248.         {
  249.             replace(line.begin(), line.end(), ' ', ',');
  250.             URGENT_LOG("block num should bigger than 0 [line:%s]", line.c_str());
  251.             ret = -1;
  252.             goto label_exit;
  253.         }
  254.  
  255.         if (max_query_persec == 0)
  256.         {
  257.             replace(line.begin(), line.end(), ' ', ',');
  258.             URGENT_LOG("product's max_query_persec must be bigger than 0 [line:%s]",  line.c_str());
  259.             ret = -1;
  260.             goto label_exit;
  261.         }
  262.         if (del_strategy != 0 && del_strategy != 1)
  263.         {
  264.             replace(line.begin(), line.end(), ' ', ',');
  265.             URGENT_LOG("product's del_strategy range[0 1] [line:%s]",  line.c_str());
  266.             ret = -1;
  267.             goto label_exit;
  268.         }
  269.  
  270.                 if (is_live_time != 0 && is_live_time > 99999999)
  271.                 {
  272.             replace(line.begin(), line.end(), ' ', ',');
  273.             URGENT_LOG("product's is_live_time range[0 99999999] [line:%s]",  line.c_str());
  274.             ret = -1;
  275.             goto label_exit;
  276.                 }
  277.  
  278.         NOTICE_LOG("read prdt config [name:%s, token:%s, block_num:%u, min_cache_item:%u, "
  279.                    "max_req_persec:%u, del_strategy:%d]",  name, token,
  280.                    block_num, min_cache_item, max_query_persec, del_strategy);
  281.         param.del_strategy = del_strategy;
  282.         param.maximal_request_num_persec = max_query_persec;
  283.         param.minimal_cache_item_num = min_cache_item;
  284.         param.block_num = block_num;
  285.         //param.random_seed = random_seed;
  286.         //param.is_live_time = is_live_time;
  287.         gDyconf.new_prdts[total_num].del_strategy = param.del_strategy;
  288.         gDyconf.new_prdts[total_num].max_query_per_sec = param.maximal_request_num_persec;
  289.         gDyconf.new_prdts[total_num].min_cache_item = param.minimal_cache_item_num;
  290.         gDyconf.new_prdts[total_num].block_num = block_num;
  291.         gDyconf.new_prdts[total_num].random_seed = random_seed;
  292.         gDyconf.new_prdts[total_num].is_live_time = is_live_time;
  293.         snprintf(gDyconf.new_prdts[total_num].token, ZCACHE_MAX_TOKEN_LEN ,"%s", token);
  294.         snprintf(gDyconf.new_prdts[total_num].name, ZCACHE_MAX_PRODUCT_NAME_LEN, "%s", name);
  295.  
  296.         ProductPuppet prdt(name);
  297.         if (!prdt)
  298.         {
  299.             added_num ++;
  300.             gDyconf.new_prdts[total_num].is_new = 1;
  301.         }
  302.         else
  303.         {
  304.             gDyconf.new_prdts[total_num].is_new = 0;
  305.             /** 判断是否参数发生变化, 如果有通知所有cacheserver并写di */
  306.             zcache_product_param_t old_param;
  307.             prdt.GetParam(old_param);
  308.             if (old_param.block_num != param.block_num)
  309.             {
  310.                 URGENT_LOG("prdt block num can not be changed [prdt:%s, old:%u, new:%u]",
  311.                            name, old_param.block_num, param.block_num);
  312.                 ret = -1;
  313.                 goto label_exit;
  314.             }
  315.  
  316.             if (old_param.block_num != param.block_num)
  317.             {
  318.                 URGENT_LOG("You Cannot change block num of product [prdt:%s, old_blk_num:%u, new_blk_num:%u]",
  319.                            name, old_param.block_num, param.block_num);
  320.                 ret = -1;
  321.                 goto label_exit;
  322.             }
  323.             /**
  324.             if (prdt.Auth(token) == false)
  325.             {
  326.                 FATAL_LOG("prdt token can not be changed [prdt:%s, old_token:%s, new_token:%s]",
  327.                           name, prdt.GetToken(), token);
  328.                 ret = -1;
  329.                 goto label_exit;
  330.             }
  331.             */
  332.             prdt.SetToken(token);
  333.             if (old_param.maximal_request_num_persec != param.maximal_request_num_persec
  334.                 || old_param.minimal_cache_item_num != param.minimal_cache_item_num
  335.                 || ((bool)(old_param.del_strategy)) != ((bool)(param.del_strategy)))
  336.             {
  337.                 unsigned int csvr_num = 0;
  338.                 prdt.SetParam(param);
  339.                 /** 需要向所有cacheserver发送参数调整信息 */
  340.                 csvr_num = CacheserverMgr::Instance()->GetAllCsvrloads(false, ZCACHE_MAX_NODE_NUM, nodes);
  341.                 param.maximal_request_num_persec = (unsigned int)1.0*param.maximal_request_num_persec/csvr_num;
  342.                 param.minimal_cache_item_num    = (unsigned int)1.0*param.minimal_cache_item_num/csvr_num;
  343.                 for (unsigned int i = 0; i < csvr_num; i ++)
  344.                 {
  345.                     send_prdt_param_adj_req(nodes[i].ip, nodes[i].port, name, param);
  346.                 }
  347.                 rept_req_buf_changed = true;
  348.             }
  349.         }
  350.         total_num ++;
  351.     }
  352.  
  353.     //说明有减少的, 需要先处理减少的, 然后处理增加的
  354.     old_num = ProductMgr::Instance()->PrdtNum();
  355.     if (total_num - added_num != old_num)
  356.     {
  357.         old_num =  ProductMgr::Instance()->GetAllPrdts(old_prdts, ZCACHE_MAX_PRODUCT_NUM);
  358.         for (int i = 0; i < old_num; i ++)
  359.         {
  360.             bool exist = false;
  361.             for (int j = 0; j < total_num; j ++)
  362.             {
  363.                 if (gDyconf.new_prdts[j].is_new)
  364.                 {
  365.                     continue;
  366.                 }
  367.                 if (strcmp(old_prdts[i].name, gDyconf.new_prdts[j].name) == 0)
  368.                 {
  369.                     exist = true;
  370.                     break;
  371.                 }
  372.             }
  373.             if (!exist)
  374.             {
  375.                 unsigned int csvr_num = 0;
  376.                 /** 需要通知所有cacheserver删除产品线, 并写di*/
  377.                 ProductMgr::Instance()->DelProduct(old_prdts[i].name);
  378.                 transdi_del_prdt(old_prdts[i].name);
  379.                 csvr_num = CacheserverMgr::Instance()->GetAllCsvrloads(false, ZCACHE_MAX_NODE_NUM, nodes);
  380.                 for (unsigned int m = 0; m < csvr_num; m ++)
  381.                 {
  382.                     send_del_prdt_req(nodes[m].ip, nodes[m].port, old_prdts[i].name);
  383.                 }
  384.                 rept_req_buf_changed  = true;
  385.             }
  386.         }
  387.     }
  388.  
  389.     //有增加
  390.     if (added_num > 0)
  391.     {
  392.         for (int i = 0; i < total_num; i ++)
  393.         {
  394.             if (gDyconf.new_prdts[i].is_new)
  395.             {
  396.                 unsigned int csvr_num = 0;
  397.                 param.block_num = gDyconf.new_prdts[i].block_num;
  398.                 param.del_strategy  = gDyconf.new_prdts[i].del_strategy;
  399.                 param.maximal_request_num_persec = gDyconf.new_prdts[i].max_query_per_sec;
  400.                 param.minimal_cache_item_num = gDyconf.new_prdts[i].min_cache_item;
  401.                 //param.random_seed = gDyconf.new_prdts[i].random_seed;
  402.                 //param.is_live_time = gDyconf.new_prdts[i].is_live_time;
  403.                 if (ProductMgr::Instance()->AddProduct(gDyconf.new_prdts[i].name, param,
  404.                                                        gDyconf.new_prdts[i].token,
  405.                                                        &gDyconf.new_prdts[i].random_seed) < 0)
  406.                 {
  407.                     URGENT_LOG("fail to add product [prdt:%s]",  gDyconf.new_prdts[i].name);
  408.                     ret = -1;
  409.                     goto label_exit;
  410.                 }
  411.                 rept_req_buf_changed  = true;
  412.                 transdi_add_prdt(gDyconf.new_prdts[i].name, param, gDyconf.new_prdts[i].token);
  413.                 /** 需要向所有cacheserver发送参数调整信息 */
  414.                 csvr_num = CacheserverMgr::Instance()->GetAllCsvrloads(false, ZCACHE_MAX_NODE_NUM, nodes);
  415.                 param.maximal_request_num_persec =(unsigned int)(param.maximal_request_num_persec*1.0/csvr_num);
  416.                 param.minimal_cache_item_num = (unsigned int)(param.minimal_cache_item_num*1.0/csvr_num);
  417.                 for (unsigned int f = 0; f < csvr_num; f ++)
  418.                 {
  419.                     send_prdt_param_adj_req(nodes[f].ip, nodes[f].port,gDyconf.new_prdts[i].name, param);
  420.                 }
  421.             }
  422.         }
  423.     }
  424.  
  425.     if (rept_req_buf_changed)
  426.     {
  427.         unsigned int csvr_num_now = CacheserverMgr::Instance()->CsvrNum();
  428.         ProductMgr::Instance()->RebuildReptReqBuf(csvr_num_now);
  429.     }
  430. label_exit:
  431.     return ret;
  432. }
  433.  
  434. void dynamic_load_prdt_config()
  435. {
  436.     struct stat prdt_stat;
  437.     char fname[ZCACHE_MAX_FILE_LEN] = {0};
  438.     static product_dyconf_t  old_prdts[ZCACHE_MAX_PRODUCT_NUM];
  439.     snprintf(fname, ZCACHE_MAX_FILE_LEN, "%s/%s", gConf.conf_path, gConf.prdt_conf_file);
  440.  
  441.     if (!gDyconf.prdt_need_to_reload)
  442.     {
  443.         if (stat(fname, &prdt_stat) != 0)
  444.             URGENT_LOG("fail to stat [filename:%s]", fname);
  445.         else
  446.         {
  447.             if (prdt_stat.st_mtim.tv_sec > gDyconf.prdt_last_load_time)
  448.             {
  449.                 gDyconf.prdt_change_detect_time = prdt_stat.st_mtim.tv_sec;
  450.                 gDyconf.prdt_need_to_reload = true;
  451.             }
  452.         }
  453.         goto label_exit;
  454.     }
  455.     else
  456.     {
  457.         if (time(NULL)  < (time_t)(gConf.config_change_confirm_msec/1000 + gDyconf.prdt_change_detect_time))
  458.             goto label_exit;
  459.     }
  460.  
  461.     //double check
  462.     if (stat(fname, &prdt_stat) != 0)
  463.         URGENT_LOG("fail to stat [filename:%s]", fname);
  464.     else
  465.     {                                                                //又改了
  466.         if (prdt_stat.st_mtim.tv_sec != gDyconf.prdt_change_detect_time)
  467.         {
  468.             gDyconf.prdt_change_detect_time = prdt_stat.st_mtime;
  469.             goto label_exit;
  470.         }
  471.     }
  472.  
  473.     NOTICE_LOG("dynamic reload prdt configure [filename:%s, last_load_time:%lu, "
  474.                "change_detect_time:%lu, now:%lu]", fname, gDyconf.prdt_last_load_time,
  475.                gDyconf.prdt_change_detect_time, time(NULL));
  476.  
  477.     if (reload_prdt_config() < 0)
  478.     {
  479.         URGENT_LOG("reload_prdt_config error");
  480.     }
  481. label_exit:
  482.     return;
  483. }