1、提供Maat_cmd_set_lines函数,支持批量写入line_rule;2、修复原有Maat_cmd_set_line函数内存泄漏的bug;

This commit is contained in:
zhengchao
2017-08-31 18:21:56 +08:00
parent 0a399b0555
commit 9396bb9e63
4 changed files with 71 additions and 41 deletions

View File

@@ -145,7 +145,7 @@ int Maat_cmd_commit(Maat_feather_t feather);
int Maat_cmd_set_group(Maat_feather_t feather, int group_id, const struct Maat_region_t* region, enum MAAT_OPERATION op); int Maat_cmd_set_group(Maat_feather_t feather, int group_id, const struct Maat_region_t* region, enum MAAT_OPERATION op);
int Maat_cmd_set_line(Maat_feather_t feather,const struct Maat_line_t* line_rule, enum MAAT_OPERATION op); int Maat_cmd_set_line(Maat_feather_t feather,const struct Maat_line_t* line_rule, enum MAAT_OPERATION op);
int Maat_cmd_set_lines(Maat_feather_t feather,const struct Maat_line_t** line_rule, int line_num ,enum MAAT_OPERATION op);
//Return the value of key after the increment. //Return the value of key after the increment.
//If the key does not exist, it is set to 0 before performing the operation. //If the key does not exist, it is set to 0 before performing the operation.
long long Maat_cmd_incrby(Maat_feather_t feather,const char* key, int increment); long long Maat_cmd_incrby(Maat_feather_t feather,const char* key, int increment);

View File

@@ -388,6 +388,10 @@ int get_rm_key_list(unsigned int version,redisContext *c,struct serial_rule_t**
} }
*new_version=version_in_redis; *new_version=version_in_redis;
if(version==0)
{
goto FULL_UPDATE;
}
if(version_in_redis<version) if(version_in_redis<version)
{ {
MESA_handle_runtime_log(logger, RLOG_LV_INFO, maat_redis_monitor, MESA_handle_runtime_log(logger, RLOG_LV_INFO, maat_redis_monitor,
@@ -1123,51 +1127,70 @@ int Maat_cmd_set_group(Maat_feather_t feather,int group_id, const struct Maat_re
assert(0); assert(0);
return 0; return 0;
} }
int Maat_cmd_set_line(Maat_feather_t feather,const struct Maat_line_t* line_rule, enum MAAT_OPERATION op) int Maat_cmd_set_lines(Maat_feather_t feather,const struct Maat_line_t** line_rule, int line_num ,enum MAAT_OPERATION op)
{ {
int i=0;
_Maat_feather_t* _feather=(_Maat_feather_t*)feather; _Maat_feather_t* _feather=(_Maat_feather_t*)feather;
int ret=0, table_id=0,retry=0; int ret=0, table_id=0,retry=0,success_cnt=0;
struct serial_rule_t s_rule; struct serial_rule_t *s_rule=NULL;
long long server_time=0,absolute_expire_time=0; long long server_time=0,absolute_expire_time=0;
ret=map_str2int(_feather->map_tablename2id, line_rule->table_name, &table_id); server_time=redis_server_time(_feather->redis_write_ctx);
s_rule=(struct serial_rule_t *)calloc(sizeof(struct serial_rule_t),line_num);
for(i=0;i<line_num;i++)
{
ret=map_str2int(_feather->map_tablename2id, line_rule[i]->table_name, &table_id);
if(ret<0) if(ret<0)
{ {
MESA_handle_runtime_log(_feather->logger,RLOG_LV_FATAL,maat_module MESA_handle_runtime_log(_feather->logger,RLOG_LV_FATAL,maat_module
,"Command set line id %d failed: unknown table %s." ,"Command set line id %d failed: unknown table %s."
, line_rule->rule_id , line_rule[i]->rule_id
, line_rule->table_name); , line_rule[i]->table_name);
ret=-1;
return -1; goto error_out;
} }
if(TABLE_TYPE_PLUGIN!=_feather->p_table_info[table_id]->table_type) if(TABLE_TYPE_PLUGIN!=_feather->p_table_info[table_id]->table_type)
{ {
MESA_handle_runtime_log(_feather->logger,RLOG_LV_FATAL,maat_module MESA_handle_runtime_log(_feather->logger,RLOG_LV_FATAL,maat_module
,"Command set line id %d failed: table %s is not a plugin table." ,"Command set line id %d failed: table %s is not a plugin table."
, line_rule->rule_id , line_rule[i]->rule_id
, line_rule->table_name); , line_rule[i]->table_name);
return -1; ret=-1;
goto error_out;
} }
server_time=redis_server_time(_feather->redis_write_ctx); if(line_rule[i]->expire_after>0)
if( line_rule->expire_after>0)
{ {
absolute_expire_time=server_time+line_rule->expire_after; absolute_expire_time=server_time+line_rule[i]->expire_after;
}
set_serial_rule(s_rule+i, op,line_rule[i]->rule_id,line_rule[i]->label_id,line_rule[i]->table_name,line_rule[i]->table_line, absolute_expire_time);
} }
set_serial_rule(&s_rule, op,line_rule->rule_id,line_rule->label_id,line_rule->table_name,line_rule->table_line, absolute_expire_time);
ret=0; ret=0;
while(ret==0) while(success_cnt<line_num)
{ {
ret=exec_serial_rule(_feather->redis_write_ctx,&s_rule, 1,server_time); success_cnt+=exec_serial_rule(_feather->redis_write_ctx,s_rule+success_cnt, line_num-success_cnt,server_time);
retry++; retry++;
} }
if(retry>10) if(retry>10)
{ {
MESA_handle_runtime_log(_feather->logger,RLOG_LV_INFO,maat_module MESA_handle_runtime_log(_feather->logger,RLOG_LV_INFO,maat_module
,"Command set line id %d success after retry %d times." ,"Command set line id %d success after retry %d times."
, line_rule->rule_id , line_rule[0]->rule_id
); );
} }
return 0; error_out:
for(i=0;i<line_num;i++)
{
empty_serial_rules(s_rule+i);
}
free(s_rule);
return ret;
}
int Maat_cmd_set_line(Maat_feather_t feather,const struct Maat_line_t* line_rule, enum MAAT_OPERATION op)
{
int ret=0;
ret=Maat_cmd_set_lines(feather,&line_rule, 1, op);
return ret;
} }
void Maat_add_region2cmd(struct Maat_cmd_t* cmd,int which_group,const struct Maat_region_t* region) void Maat_add_region2cmd(struct Maat_cmd_t* cmd,int which_group,const struct Maat_region_t* region)
{ {

View File

@@ -491,20 +491,28 @@ int test_table_conjunction(Maat_feather_t feather,const char* table_name,const c
} }
return 0; return 0;
} }
#define TEST_CMD_LINE_NUM 4
void test_set_cmd_line(Maat_feather_t feather) void test_set_cmd_line(Maat_feather_t feather)
{ {
struct Maat_line_t line_rule; const struct Maat_line_t *p_line[TEST_CMD_LINE_NUM];
int ret=0; struct Maat_line_t line_rule[TEST_CMD_LINE_NUM];
char table_line[TEST_CMD_LINE_NUM][128];
int ret=0,i=0;
memset(&line_rule,0,sizeof(line_rule)); memset(&line_rule,0,sizeof(line_rule));
line_rule.label_id=0; for(i=0;i<4;i++)
line_rule.rule_id=(int)Maat_cmd_incrby(feather,"TEST_PLUG_SEQ", 1); {
line_rule.table_name="QD_ENTRY_INFO"; line_rule[i].label_id=0;
line_rule.table_line="1\t192.168.0.1\t101\t1"; line_rule[i].rule_id=(int)Maat_cmd_incrby(feather,"TEST_PLUG_SEQ", 1);
line_rule.expire_after=0; line_rule[i].table_name="QD_ENTRY_INFO";
ret=Maat_cmd_set_line(feather, &line_rule, MAAT_OP_ADD); snprintf(table_line[i],sizeof(table_line[i]),"1\t192.168.0.1\t%d\t1",100+i);
line_rule[i].table_line=table_line[i];
line_rule[i].expire_after=0;
p_line[i]=line_rule+i;
}
ret=Maat_cmd_set_lines(feather, p_line,TEST_CMD_LINE_NUM, MAAT_OP_ADD);
assert(ret==0); assert(ret==0);
sleep(1); sleep(1);
ret=Maat_cmd_set_line(feather, &line_rule, MAAT_OP_DEL); ret=Maat_cmd_set_lines(feather, p_line,TEST_CMD_LINE_NUM, MAAT_OP_DEL);
assert(ret==0); assert(ret==0);
return; return;
} }
@@ -671,7 +679,7 @@ int main(int argc,char* argv[])
scan_status_t mid=NULL; scan_status_t mid=NULL;
int wait_second=400; int wait_second=400;
if(argc<2) if(argc<2||argv[1][0]!='-')
{ {
maat_test_print_usage(); maat_test_print_usage();
return 0; return 0;

View File

@@ -7,4 +7,3 @@ redis-cli -h $host -p $port FLUSHALL
redis-cli -h $host -p $port SET MAAT_VERSION "0" redis-cli -h $host -p $port SET MAAT_VERSION "0"
redis-cli -h $host -p $port SET SEQUENCE_REGION "1" redis-cli -h $host -p $port SET SEQUENCE_REGION "1"
redis-cli -h $host -p $port SET SEQUENCE_GROUP "1" redis-cli -h $host -p $port SET SEQUENCE_GROUP "1"