[PATCH]delete useless code
This commit is contained in:
@@ -12,14 +12,19 @@
|
||||
#include "maat_config_monitor.h"
|
||||
#include "maat_redis_monitor.h"
|
||||
#include "json2iris.h"
|
||||
#include "../test/test_utils.h"
|
||||
#include "hiredis/hiredis.h"
|
||||
|
||||
#define MODULE_REDIS_TOOL module_name_str("maat.redis_tool")
|
||||
|
||||
#define WORK_MODE_DUMP 0
|
||||
#define WORK_MODE_UPLOAD 1
|
||||
#define WORK_MODE_TEST_TRANS 2
|
||||
|
||||
const char *redis_dump_dir = "./redis_dump";
|
||||
const char *default_table_info = "./table_info.conf";
|
||||
|
||||
void maat_tool_print_usage(void)
|
||||
static void maat_tool_print_usage(void)
|
||||
{
|
||||
printf("maat_redis_tool manipulate rules from redis.\n");
|
||||
printf("Usage:\n");
|
||||
@@ -30,7 +35,6 @@ void maat_tool_print_usage(void)
|
||||
printf("\t-k, try to execute the 'keys EFFECTIVE_RULE:*' transaction, and give a specific reason if an error occurs\n");
|
||||
printf("\t-u [json_file], flush redis and upload all rules to redis, confirm the risk before proceeding\n");
|
||||
printf("\t-v [version], dump specific [version] from redis, dump latest version as default.\n");
|
||||
printf("\t-j [payload.json], add or delete rules as maat json. Must have field compile_table field, and plugin table's valid flag must be in the last column.\n");
|
||||
printf("\t-t [timeout], timeout config after t seconds, default is 0 which means never timeout.\n");
|
||||
printf("example: ./maat_redis_tool -h 127.0.0.1 -p 6379 -d %s\n",redis_dump_dir);
|
||||
printf(" ./maat_redis_tool -h 127.0.0.1 -p 6379 -k\n");
|
||||
@@ -50,9 +54,10 @@ static int compare_serial_rule(const void *a, const void *b)
|
||||
return ret;
|
||||
}
|
||||
|
||||
int set_file_rulenum(const char *path, int rule_num)
|
||||
static int set_file_rulenum(const char *path, int rule_num)
|
||||
{
|
||||
FILE* fp=NULL;
|
||||
|
||||
if (0 == rule_num) {
|
||||
fp = fopen(path, "w");
|
||||
} else {
|
||||
@@ -60,7 +65,8 @@ int set_file_rulenum(const char *path, int rule_num)
|
||||
}
|
||||
|
||||
if (NULL == fp) {
|
||||
fprintf(stderr, "fopen %s failed %s at set rule num\n", path, strerror(errno));
|
||||
fprintf(stderr, "fopen %s failed %s at set rule num\n",
|
||||
path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -70,7 +76,8 @@ int set_file_rulenum(const char *path, int rule_num)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void read_rule_from_redis(redisContext *c, const char *output_path, struct log_handle *logger)
|
||||
static void read_rule_from_redis(redisContext *c, const char *output_path,
|
||||
struct log_handle *logger)
|
||||
{
|
||||
int i = 0;
|
||||
int ret = 0;
|
||||
@@ -85,7 +92,8 @@ void read_rule_from_redis(redisContext *c, const char *output_path, struct log_h
|
||||
FILE *index_fp = NULL;
|
||||
struct serial_rule *rule_list = NULL;
|
||||
|
||||
int rule_num = maat_get_rm_key_list(c, 0, &version, NULL, &rule_list, &update_type, logger);
|
||||
int rule_num = maat_get_rm_key_list(c, 0, &version, NULL, &rule_list,
|
||||
&update_type, logger);
|
||||
if (0 == rule_num) {
|
||||
printf("No Effective Rules.\n");
|
||||
return;
|
||||
@@ -110,13 +118,15 @@ void read_rule_from_redis(redisContext *c, const char *output_path, struct log_h
|
||||
|
||||
printf("Sorting.\n");
|
||||
qsort(rule_list, rule_num, sizeof(struct serial_rule), compare_serial_rule);
|
||||
|
||||
if ((access(output_path, F_OK)) <0) {
|
||||
if ((mkdir(output_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) < 0) {
|
||||
printf("mkdir %s error\n", output_path);
|
||||
}
|
||||
}
|
||||
|
||||
snprintf(foreign_files_dir, sizeof(foreign_files_dir), "%s/foreign_files/",output_path);
|
||||
snprintf(foreign_files_dir, sizeof(foreign_files_dir),
|
||||
"%s/foreign_files/", output_path);
|
||||
|
||||
if ((access(foreign_files_dir, F_OK)) <0) {
|
||||
if((mkdir(foreign_files_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) < 0) {
|
||||
@@ -124,13 +134,16 @@ void read_rule_from_redis(redisContext *c, const char *output_path, struct log_h
|
||||
}
|
||||
}
|
||||
|
||||
ret = maat_get_foreign_keys_by_prefix(c, rule_list, rule_num, foreign_files_dir, logger);
|
||||
ret = maat_get_foreign_keys_by_prefix(c, rule_list, rule_num,
|
||||
foreign_files_dir, logger);
|
||||
if (ret > 0) {
|
||||
printf("%d lines has foreign content.\n", ret);
|
||||
maat_get_foreign_conts(c, rule_list, rule_num, 1, NULL);
|
||||
}
|
||||
|
||||
snprintf(index_path,sizeof(index_path), "%s/full_config_index.%020lld", output_path, version);
|
||||
snprintf(index_path,sizeof(index_path), "%s/full_config_index.%020lld",
|
||||
output_path, version);
|
||||
|
||||
index_fp = fopen(index_path, "w");
|
||||
if (NULL == index_fp) {
|
||||
printf("Open %s failed.\n", index_path);
|
||||
@@ -142,7 +155,8 @@ void read_rule_from_redis(redisContext *c, const char *output_path, struct log_h
|
||||
maat_rewrite_table_line_with_foreign(rule_list+i);
|
||||
}
|
||||
|
||||
if (NULL == cur_table || 0 != strcmp(cur_table,rule_list[i].table_name)) {
|
||||
if (NULL == cur_table ||
|
||||
0 != strcmp(cur_table,rule_list[i].table_name)) {
|
||||
if (table_fp != NULL) {
|
||||
fprintf(index_fp, "%s\t%d\t%s\n", cur_table, line_count, table_path);
|
||||
fclose(table_fp);
|
||||
@@ -152,8 +166,8 @@ void read_rule_from_redis(redisContext *c, const char *output_path, struct log_h
|
||||
printf("Written table %s\n",table_path);
|
||||
}
|
||||
|
||||
snprintf(table_path, sizeof(table_path), "%s/%s.%020lld", output_path,
|
||||
rule_list[i].table_name, version);
|
||||
snprintf(table_path, sizeof(table_path), "%s/%s.%020lld",
|
||||
output_path, rule_list[i].table_name, version);
|
||||
set_file_rulenum(table_path, 0);
|
||||
|
||||
table_fp = fopen(table_path, "a");
|
||||
@@ -170,7 +184,8 @@ void read_rule_from_redis(redisContext *c, const char *output_path, struct log_h
|
||||
rule_list[i].table_line[line_len - 1] = '\0';
|
||||
}
|
||||
|
||||
fprintf(table_fp, "%s\tkey=%lld\n", rule_list[i].table_line, rule_list[i].rule_id);
|
||||
fprintf(table_fp, "%s\tkey=%lld\n", rule_list[i].table_line,
|
||||
rule_list[i].rule_id);
|
||||
line_count++;
|
||||
}
|
||||
|
||||
@@ -189,10 +204,6 @@ clean_up:
|
||||
|
||||
FREE(rule_list);
|
||||
|
||||
if (c != NULL) {
|
||||
redisFree(c);
|
||||
}
|
||||
|
||||
if (index_fp != NULL) {
|
||||
fclose(index_fp);
|
||||
}
|
||||
@@ -202,195 +213,7 @@ clean_up:
|
||||
}
|
||||
}
|
||||
|
||||
int count_line_num_cb(const char *table_name, const char *line, void *u_para)
|
||||
{
|
||||
(*((unsigned int *)u_para))++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int line_idx = 0;
|
||||
int make_serial_rule(const char *table_name, const char *line, void *u_para)
|
||||
{
|
||||
struct serial_rule *s_rule=(struct serial_rule *)u_para;
|
||||
redisContext *ctx = s_rule->ref_ctx;
|
||||
long long absolute_expire_time = s_rule->timeout;
|
||||
char *buff = ALLOC(char, strlen(line) + 1);
|
||||
|
||||
memcpy(buff, line, strlen(line) + 1);
|
||||
while(buff[strlen(line) - 1] == '\n' || buff[strlen(line) - 1] == '\t') {
|
||||
buff[strlen(line) - 1] = '\0';
|
||||
}
|
||||
|
||||
const char *redis_rule_key = "TEST_RULE_KEY";
|
||||
redisReply *reply =
|
||||
maat_wrap_redis_command(ctx, NULL, "INCRBY %s %d", redis_rule_key, 1);
|
||||
if (reply->type == REDIS_REPLY_NIL) {
|
||||
printf("incrby redis_rule_key:%s failed.", redis_rule_key);
|
||||
return -1;
|
||||
} else {
|
||||
s_rule->rule_id = maat_read_redis_integer(reply);
|
||||
freeReplyObject(reply);
|
||||
reply = NULL;
|
||||
}
|
||||
|
||||
maat_set_serial_rule(s_rule + line_idx, MAAT_OP_ADD, s_rule->rule_id,
|
||||
table_name, buff, absolute_expire_time);
|
||||
(s_rule + line_idx)->ref_ctx = ctx;
|
||||
line_idx++;
|
||||
FREE(buff);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_config_to_redis(redisContext *c, char *json_filename,
|
||||
struct log_handle *logger)
|
||||
{
|
||||
char tmp_iris_path[128] = {0};
|
||||
char *json_buff = NULL;
|
||||
size_t json_buff_sz = 0;
|
||||
|
||||
snprintf(tmp_iris_path, sizeof(tmp_iris_path),
|
||||
"%s_iris_tmp", json_filename);
|
||||
|
||||
int ret = load_file_to_memory(json_filename,
|
||||
(unsigned char **)&json_buff,
|
||||
&json_buff_sz);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = json2iris(json_buff, json_filename, c, tmp_iris_path,
|
||||
sizeof(tmp_iris_path), NULL, NULL, logger);
|
||||
FREE(json_buff);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t total_line_cnt = 0;
|
||||
config_monitor_traverse(0, tmp_iris_path, NULL, count_line_num_cb, NULL,
|
||||
&total_line_cnt, NULL, logger);
|
||||
|
||||
struct serial_rule *s_rule = ALLOC(struct serial_rule, total_line_cnt);
|
||||
s_rule->ref_ctx = c;
|
||||
long long server_time = maat_redis_server_time_s(c);
|
||||
if (server_time < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
s_rule->timeout = server_time + 300;
|
||||
config_monitor_traverse(0, tmp_iris_path, NULL, make_serial_rule, NULL,
|
||||
s_rule, NULL, logger);
|
||||
s_rule->ref_ctx = NULL;
|
||||
line_idx = 0;
|
||||
|
||||
int success_cnt = 0;
|
||||
do {
|
||||
success_cnt = maat_cmd_write_rule(c, s_rule, total_line_cnt,
|
||||
server_time, logger);
|
||||
} while (success_cnt < 0);
|
||||
|
||||
assert(success_cnt == (int)total_line_cnt);
|
||||
|
||||
for (size_t i = 0; i < total_line_cnt; i++) {
|
||||
maat_clear_rule_cache(s_rule + i);
|
||||
}
|
||||
|
||||
FREE(s_rule);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rollback_redis_version(redisContext *c, struct log_handle *logger)
|
||||
{
|
||||
redisReply *reply =
|
||||
maat_wrap_redis_command(c, logger, "SET MAAT_VERSION 0");
|
||||
if (NULL == reply) {
|
||||
log_fatal(logger, MODULE_REDIS_TOOL,
|
||||
"[%s:%d] set MAAT_VERSION failed, "
|
||||
"Redis Communication error: %s",
|
||||
__FUNCTION__, __LINE__, c->errstr);
|
||||
return -1;
|
||||
}
|
||||
freeReplyObject(reply);
|
||||
reply = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int clear_config_in_redis(redisContext *c, struct log_handle *logger)
|
||||
{
|
||||
long long redis_version = 0;
|
||||
redisReply *reply = maat_wrap_redis_command(c, logger, "GET MAAT_VERSION");
|
||||
if (reply != NULL) {
|
||||
if (reply->type == REDIS_REPLY_NIL ||
|
||||
reply->type == REDIS_REPLY_ERROR) {
|
||||
log_fatal(logger, MODULE_REDIS_TOOL,
|
||||
"[%s:%d] GET MAAT_VERSION failed, maybe Redis is busy",
|
||||
__FUNCTION__, __LINE__);
|
||||
freeReplyObject(reply);
|
||||
reply = NULL;
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
log_fatal(logger, MODULE_REDIS_TOOL,
|
||||
"[%s:%d] GET MAAT_VERSION failed with NULL reply, error: %s",
|
||||
__FUNCTION__, __LINE__, c->errstr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
redis_version = maat_read_redis_integer(reply);
|
||||
if (redis_version < 0) {
|
||||
if (reply->type == REDIS_REPLY_ERROR) {
|
||||
log_fatal(logger, MODULE_REDIS_TOOL,
|
||||
"[%s:%d] Redis Communication error: %s",
|
||||
__FUNCTION__, __LINE__, reply->str);
|
||||
}
|
||||
freeReplyObject(reply);
|
||||
reply = NULL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
freeReplyObject(reply);
|
||||
reply = NULL;
|
||||
|
||||
reply = maat_wrap_redis_command(c, logger, "MULTI");
|
||||
freeReplyObject(reply);
|
||||
reply = NULL;
|
||||
|
||||
int append_cmd_cnt = 0;
|
||||
redisAppendCommand(c, "FLUSHDB");
|
||||
append_cmd_cnt++;
|
||||
redisAppendCommand(c, "SET MAAT_VERSION %lld", redis_version);
|
||||
append_cmd_cnt++;
|
||||
redisAppendCommand(c, "SET MAAT_PRE_VER %lld", redis_version);
|
||||
append_cmd_cnt++;
|
||||
redisAppendCommand(c, "SET %s 1", mr_region_id_var);
|
||||
append_cmd_cnt++;
|
||||
redisAppendCommand(c, "SET %s 1", mr_group_id_var);
|
||||
append_cmd_cnt++;
|
||||
redisAppendCommand(c, "EXEC");
|
||||
append_cmd_cnt++;
|
||||
|
||||
int redis_transaction_success = 1;
|
||||
for (int i = 0; i < append_cmd_cnt; i++) {
|
||||
int ret = maat_wrap_redis_get_reply(c, &reply);
|
||||
if (ret == REDIS_OK) {
|
||||
if (reply->type == REDIS_REPLY_NIL) {
|
||||
redis_transaction_success = 0;
|
||||
}
|
||||
freeReplyObject(reply);
|
||||
reply = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (0 == redis_transaction_success) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int exec_keys_transaction(redisContext *c, struct log_handle *logger)
|
||||
static int exec_keys_transaction(redisContext *c, struct log_handle *logger)
|
||||
{
|
||||
printf("Ready to execute [keys EFFECTIVE_RULE:*] transaction...\n");
|
||||
struct timespec start, end;
|
||||
@@ -454,20 +277,14 @@ int exec_keys_transaction(redisContext *c, struct log_handle *logger)
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define WORK_MODE_DUMP 0
|
||||
#define WORK_MODE_JSON 1
|
||||
#define WORK_MODE_UPLOAD 2
|
||||
#define WORK_MODE_TEST_TRANS 3
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
int oc = 0;
|
||||
int opt = 0;
|
||||
int mode = 0;
|
||||
char redis_ip[64] = {0};
|
||||
int redis_port = 6379;
|
||||
int redis_db = 0;
|
||||
char dump_dir[128] = {0};
|
||||
char json_file[128] = {0};
|
||||
char upload_file[128] = {0};
|
||||
char log_path[128] = "./maat_redis_tool.log";
|
||||
int timeout = 0;
|
||||
@@ -476,8 +293,8 @@ int main(int argc, char * argv[])
|
||||
strncpy(dump_dir, redis_dump_dir, sizeof(dump_dir));
|
||||
struct log_handle *logger = log_handle_create(log_path, 0);
|
||||
|
||||
while ((oc = getopt(argc,argv,"h:p:n:d:f:j:t:u:k")) != -1) {
|
||||
switch (oc) {
|
||||
while ((opt = getopt(argc,argv,"h:p:n:d:f:t:u:k")) != -1) {
|
||||
switch (opt) {
|
||||
case 'h':
|
||||
strncpy(redis_ip, optarg, sizeof(redis_ip));
|
||||
break;
|
||||
@@ -494,10 +311,6 @@ int main(int argc, char * argv[])
|
||||
dump_dir[strlen(dump_dir)-1] = '\0';
|
||||
}
|
||||
break;
|
||||
case 'j':
|
||||
strncpy(json_file, optarg, sizeof(json_file));
|
||||
mode = WORK_MODE_JSON;
|
||||
break;
|
||||
case 'k':
|
||||
mode = WORK_MODE_TEST_TRANS;
|
||||
break;
|
||||
@@ -516,96 +329,28 @@ int main(int argc, char * argv[])
|
||||
}
|
||||
}
|
||||
|
||||
if (mode == WORK_MODE_UPLOAD) {
|
||||
return write_json_to_redis(upload_file, redis_ip, redis_port,
|
||||
redis_db, logger);
|
||||
}
|
||||
|
||||
redisContext *c = maat_connect_redis(redis_ip, redis_port, redis_db, logger);
|
||||
if (NULL == c) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
char tmp_iris_path[128] = {0};
|
||||
if (mode == WORK_MODE_DUMP) {
|
||||
log_info(logger, MODULE_REDIS_TOOL, "Reading key list from %s:%d db%d.",
|
||||
redis_ip, redis_port, redis_db);
|
||||
read_rule_from_redis(c, dump_dir, logger);
|
||||
} else if(mode == WORK_MODE_JSON) {
|
||||
char *json_buff = NULL;
|
||||
size_t json_buff_sz = 0;
|
||||
ret = load_file_to_memory(json_file, (unsigned char**)&json_buff, &json_buff_sz);
|
||||
if (ret < 0) {
|
||||
log_fatal(logger, MODULE_REDIS_TOOL, "open file:%s failed.", json_file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = json2iris(json_buff, json_file, c, tmp_iris_path, sizeof(tmp_iris_path), NULL, NULL, logger);
|
||||
FREE(json_buff);
|
||||
if (ret < 0) {
|
||||
log_fatal(logger, MODULE_REDIS_TOOL, "Invalid json format.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t total_line_cnt = 0;
|
||||
config_monitor_traverse(0, tmp_iris_path, NULL, count_line_num_cb, NULL, &total_line_cnt, NULL, logger);
|
||||
log_fatal(logger, MODULE_REDIS_TOOL, "Serialize %s to %zu lines, write temp file to %s .",
|
||||
json_file, total_line_cnt, tmp_iris_path);
|
||||
|
||||
struct serial_rule *s_rule = ALLOC(struct serial_rule, total_line_cnt);
|
||||
s_rule->ref_ctx = c;
|
||||
long long server_time = maat_redis_server_time_s(c);
|
||||
if (!server_time) {
|
||||
log_fatal(logger, MODULE_REDIS_TOOL, "Get Redis Time failed.");
|
||||
FREE(s_rule);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (timeout > 0) {
|
||||
s_rule->timeout = server_time + timeout;
|
||||
}
|
||||
log_info(logger, MODULE_REDIS_TOOL, "Timeout = %lld\n", s_rule->timeout);
|
||||
config_monitor_traverse(0, tmp_iris_path, NULL, make_serial_rule, NULL, s_rule, NULL, logger);
|
||||
s_rule->ref_ctx = NULL;
|
||||
|
||||
int success_cnt = 0;
|
||||
do {
|
||||
success_cnt = maat_cmd_write_rule(c, s_rule, total_line_cnt, server_time, logger);
|
||||
} while(success_cnt < 0);
|
||||
|
||||
if (success_cnt != (int)total_line_cnt) {
|
||||
log_fatal(logger, MODULE_REDIS_TOOL, "Only Add %d of %zu, rule id maybe conflicts.",
|
||||
success_cnt, total_line_cnt);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < total_line_cnt; i++) {
|
||||
maat_clear_rule_cache(s_rule+i);
|
||||
}
|
||||
FREE(s_rule);
|
||||
redisFree(c);
|
||||
} else if (mode == WORK_MODE_UPLOAD) {
|
||||
ret = clear_config_in_redis(c, logger);
|
||||
if (ret < 0) {
|
||||
log_fatal(logger, MODULE_REDIS_TOOL, "clear config in redis failed.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = write_config_to_redis(c, upload_file, logger);
|
||||
if (ret < 0) {
|
||||
log_fatal(logger, MODULE_REDIS_TOOL, "write config to redis failed.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = rollback_redis_version(c, logger);
|
||||
if (ret < 0) {
|
||||
log_fatal(logger, MODULE_REDIS_TOOL, "rollback redis version failed.");
|
||||
return -1;
|
||||
}
|
||||
redisFree(c);
|
||||
} else if (mode == WORK_MODE_TEST_TRANS) {
|
||||
ret = exec_keys_transaction(c, logger);
|
||||
if (ret < 0) {
|
||||
printf("[ERROR]: execute 'KEYS EFFECTIVE_RULE:*' failed.");
|
||||
return -1;
|
||||
}
|
||||
redisFree(c);
|
||||
}
|
||||
|
||||
return 0;
|
||||
redisFree(c);
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user