delete xx_plugin get ex_data log
This commit is contained in:
@@ -721,33 +721,21 @@ void *maat_plugin_table_get_ex_data(struct maat *maat_instance, int table_id, co
|
|||||||
|
|
||||||
if (NULL == maat_instance || table_id < 0 || table_id >= MAX_TABLE_NUM
|
if (NULL == maat_instance || table_id < 0 || table_id >= MAX_TABLE_NUM
|
||||||
|| NULL == key) {
|
|| NULL == key) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] table(table_id:%d) input parameter is invalid",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct maat_runtime *maat_rt = maat_instance->maat_rt;
|
struct maat_runtime *maat_rt = maat_instance->maat_rt;
|
||||||
if (NULL == maat_rt) {
|
if (NULL == maat_rt) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] table(table_id:%d) maat runtime is NULL",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *runtime = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id);
|
void *runtime = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id);
|
||||||
if (NULL == runtime) {
|
if (NULL == runtime) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] table(table_id:%d) runtime is NULL",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *schema = table_manager_get_schema(maat_rt->ref_tbl_mgr, table_id);
|
void *schema = table_manager_get_schema(maat_rt->ref_tbl_mgr, table_id);
|
||||||
if (NULL == schema) {
|
if (NULL == schema) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] table(table_id:%d) schema is NULL",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -758,9 +746,6 @@ void *maat_plugin_table_get_ex_data(struct maat *maat_instance, int table_id, co
|
|||||||
} else if (TABLE_TYPE_PLUGIN == table_type) {
|
} else if (TABLE_TYPE_PLUGIN == table_type) {
|
||||||
ret = plugin_runtime_get_ex_data(runtime, schema, key, strlen(key));
|
ret = plugin_runtime_get_ex_data(runtime, schema, key, strlen(key));
|
||||||
} else {
|
} else {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] table(table_id:%d) type is invalid, neither compile nor plugin",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -773,34 +758,22 @@ int maat_ip_plugin_table_get_ex_data(struct maat *maat_instance, int table_id,
|
|||||||
{
|
{
|
||||||
if (NULL == maat_instance || table_id < 0 || table_id >= MAX_TABLE_NUM
|
if (NULL == maat_instance || table_id < 0 || table_id >= MAX_TABLE_NUM
|
||||||
|| NULL == ip_addr || NULL == ex_data_array || 0 == n_ex_data) {
|
|| NULL == ip_addr || NULL == ex_data_array || 0 == n_ex_data) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] ip_plugin table(table_id:%d) input parameter is invalid.",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct maat_runtime *maat_rt = maat_instance->maat_rt;
|
struct maat_runtime *maat_rt = maat_instance->maat_rt;
|
||||||
if (NULL == maat_rt) {
|
if (NULL == maat_rt) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] ip_plugin table(table_id:%d) maat runtime is NULL",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *ip_plugin_rt = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id);
|
void *ip_plugin_rt = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id);
|
||||||
if (NULL == ip_plugin_rt) {
|
if (NULL == ip_plugin_rt) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] ip_plugin table(table_id:%d) runtime is NULL",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int n_hit_ex_data = ip_plugin_runtime_get_ex_data(ip_plugin_rt, ip_addr,
|
int n_hit_ex_data = ip_plugin_runtime_get_ex_data(ip_plugin_rt, ip_addr,
|
||||||
ex_data_array, n_ex_data);
|
ex_data_array, n_ex_data);
|
||||||
if (n_hit_ex_data < 0) {
|
if (n_hit_ex_data < 0) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] ip_plugin table(table_id:%d) get ex_data error.",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -813,34 +786,22 @@ int maat_fqdn_plugin_table_get_ex_data(struct maat *maat_instance, int table_id,
|
|||||||
{
|
{
|
||||||
if (NULL == maat_instance || table_id < 0 || table_id >= MAX_TABLE_NUM
|
if (NULL == maat_instance || table_id < 0 || table_id >= MAX_TABLE_NUM
|
||||||
|| NULL == fqdn || NULL == ex_data_array || 0 == n_ex_data) {
|
|| NULL == fqdn || NULL == ex_data_array || 0 == n_ex_data) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] fqdn_plugin table(table_id:%d) input parameter is invalid.",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct maat_runtime *maat_rt = maat_instance->maat_rt;
|
struct maat_runtime *maat_rt = maat_instance->maat_rt;
|
||||||
if (NULL == maat_rt) {
|
if (NULL == maat_rt) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] fqdn_plugin table(table_id:%d) maat runtime is NULL",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *fqdn_plugin_rt = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id);
|
void *fqdn_plugin_rt = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id);
|
||||||
if (NULL == fqdn_plugin_rt) {
|
if (NULL == fqdn_plugin_rt) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] fqdn_plugin table(table_id:%d) runtime is NULL",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int n_hit_ex_data = fqdn_plugin_runtime_get_ex_data(fqdn_plugin_rt, fqdn,
|
int n_hit_ex_data = fqdn_plugin_runtime_get_ex_data(fqdn_plugin_rt, fqdn,
|
||||||
ex_data_array, n_ex_data);
|
ex_data_array, n_ex_data);
|
||||||
if (n_hit_ex_data < 0) {
|
if (n_hit_ex_data < 0) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] fqdn_plugin table(table_id:%d) get ex_data error.",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -853,34 +814,22 @@ int maat_bool_plugin_table_get_ex_data(struct maat *maat_instance, int table_id,
|
|||||||
{
|
{
|
||||||
if (NULL == maat_instance || table_id < 0 || table_id >= MAX_TABLE_NUM
|
if (NULL == maat_instance || table_id < 0 || table_id >= MAX_TABLE_NUM
|
||||||
|| NULL == item_ids || NULL == ex_data_array || 0 == n_ex_data) {
|
|| NULL == item_ids || NULL == ex_data_array || 0 == n_ex_data) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] bool_plugin table(table_id:%d) input parameter is invalid.",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct maat_runtime *maat_rt = maat_instance->maat_rt;
|
struct maat_runtime *maat_rt = maat_instance->maat_rt;
|
||||||
if (NULL == maat_rt) {
|
if (NULL == maat_rt) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] bool_plugin table(table_id:%d) maat runtime is NULL",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *bool_plugin_rt = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id);
|
void *bool_plugin_rt = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id);
|
||||||
if (NULL == bool_plugin_rt) {
|
if (NULL == bool_plugin_rt) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] bool_plugin table(table_id:%d) runtime is NULL",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int n_hit_ex_data = bool_plugin_runtime_get_ex_data(bool_plugin_rt, item_ids, n_item,
|
int n_hit_ex_data = bool_plugin_runtime_get_ex_data(bool_plugin_rt, item_ids, n_item,
|
||||||
ex_data_array, n_ex_data);
|
ex_data_array, n_ex_data);
|
||||||
if (n_hit_ex_data < 0) {
|
if (n_hit_ex_data < 0) {
|
||||||
log_error(maat_instance->logger, MODULE_MAAT_API,
|
|
||||||
"[%s:%d] bool_plugin table(table_id:%d) get ex_data error.",
|
|
||||||
__FUNCTION__, __LINE__, table_id);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -304,7 +304,7 @@ static int register_tablename2id(cJSON *json, struct maat_kv_store *tablename2id
|
|||||||
}
|
}
|
||||||
|
|
||||||
maat_kv_register(tablename2id_map, tmp_item->valuestring, table_id);
|
maat_kv_register(tablename2id_map, tmp_item->valuestring, table_id);
|
||||||
log_info(logger, MODULE_TABLE, "tablename[%s] -> table_id:[%d]",
|
log_info(logger, MODULE_TABLE, "table_name[%s] -> table_id:[%d]",
|
||||||
tmp_item->valuestring, table_id);
|
tmp_item->valuestring, table_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ file(COPY file_test_tableinfo.conf DESTINATION ./)
|
|||||||
file(COPY literal_expr.conf DESTINATION ./)
|
file(COPY literal_expr.conf DESTINATION ./)
|
||||||
file(COPY regex_expr.conf DESTINATION ./)
|
file(COPY regex_expr.conf DESTINATION ./)
|
||||||
file(COPY maat_json.json DESTINATION ./)
|
file(COPY maat_json.json DESTINATION ./)
|
||||||
|
file(COPY maat_json.json DESTINATION ../tools/)
|
||||||
file(COPY ntcrule DESTINATION ./)
|
file(COPY ntcrule DESTINATION ./)
|
||||||
file(COPY testdata DESTINATION ./)
|
file(COPY testdata DESTINATION ./)
|
||||||
file(COPY test_streamfiles DESTINATION ./)
|
file(COPY test_streamfiles DESTINATION ./)
|
||||||
@@ -292,7 +292,7 @@ int ip_table_set_line(struct maat *maat_instance, const char *table_name, enum m
|
|||||||
|
|
||||||
return maat_cmd_set_line(maat_instance, &line_rule);
|
return maat_cmd_set_line(maat_instance, &line_rule);
|
||||||
}
|
}
|
||||||
|
#if 0
|
||||||
class MaatFlagScan : public testing::Test
|
class MaatFlagScan : public testing::Test
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
@@ -5269,6 +5269,182 @@ TEST_F(MaatCmdTest, StreamScanSegfaultWhenVersionRollBack_TSG6324) {
|
|||||||
maat_state_free(state);
|
maat_state_free(state);
|
||||||
state = NULL;
|
state = NULL;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
class MaatRollbackTest : public testing::Test
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
static void SetUpTestCase() {
|
||||||
|
const char *accept_tags = "{\"tags\":[{\"tag\":\"location\",\"value\":\"北京/朝阳/华严北里/甲22号\"},{\"tag\":\"isp\",\"value\":\"移动\"},{\"tag\":\"location\",\"value\":\"Astana\"}]}";
|
||||||
|
char redis_ip[64] = "127.0.0.1";
|
||||||
|
int redis_port = 6379;
|
||||||
|
int redis_db = 0;
|
||||||
|
|
||||||
|
logger = log_handle_create("./maat_framework_gtest.log", 0);
|
||||||
|
int ret = write_config_to_redis(redis_ip, redis_port, redis_db, logger);
|
||||||
|
if (ret < 0) {
|
||||||
|
log_error(logger, MODULE_FRAMEWORK_GTEST,
|
||||||
|
"[%s:%d] write config to redis failed.", __FUNCTION__, __LINE__);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct maat_options *opts = maat_options_new();
|
||||||
|
maat_options_set_redis(opts, redis_ip, redis_port, redis_db);
|
||||||
|
maat_options_set_logger(opts, "./maat_framework_gtest.log", LOG_LEVEL_INFO);
|
||||||
|
maat_options_set_accept_tags(opts, accept_tags);
|
||||||
|
|
||||||
|
_shared_maat_instance = maat_new(opts, table_info_path);
|
||||||
|
maat_options_free(opts);
|
||||||
|
if (NULL == _shared_maat_instance) {
|
||||||
|
log_error(logger, MODULE_FRAMEWORK_GTEST,
|
||||||
|
"[%s:%d] create maat instance in MaatFlagScan failed.",
|
||||||
|
__FUNCTION__, __LINE__);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void TearDownTestCase() {
|
||||||
|
maat_free(_shared_maat_instance);
|
||||||
|
log_handle_destroy(logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct log_handle *logger;
|
||||||
|
static struct maat *_shared_maat_instance;
|
||||||
|
};
|
||||||
|
struct maat *MaatRollbackTest::_shared_maat_instance;
|
||||||
|
struct log_handle *MaatRollbackTest::logger;
|
||||||
|
|
||||||
|
static int clear_config_in_redis(redisContext *c, struct log_handle *logger)
|
||||||
|
{
|
||||||
|
long long redis_version = 0;
|
||||||
|
redisReply *reply = maat_cmd_wrap_redis_command(c, "GET MAAT_VERSION");
|
||||||
|
if (reply != NULL) {
|
||||||
|
if (reply->type == REDIS_REPLY_NIL || reply->type == REDIS_REPLY_ERROR) {
|
||||||
|
log_error(logger, MODULE_FRAMEWORK_GTEST,
|
||||||
|
"[%s:%d] GET MAAT_VERSION failed, maybe Redis is busy",
|
||||||
|
__FUNCTION__, __LINE__);
|
||||||
|
freeReplyObject(reply);
|
||||||
|
reply = NULL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log_error(logger, MODULE_FRAMEWORK_GTEST,
|
||||||
|
"[%s:%d] GET MAAT_VERSION failed with NULL reply, error: %s",
|
||||||
|
__FUNCTION__, __LINE__, c->errstr);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
redis_version = maat_cmd_read_redis_integer(reply);
|
||||||
|
if (redis_version < 0) {
|
||||||
|
if (reply->type == REDIS_REPLY_ERROR) {
|
||||||
|
log_error(logger, MODULE_FRAMEWORK_GTEST,
|
||||||
|
"[%s:%d] Redis Communication error: %s",
|
||||||
|
__FUNCTION__, __LINE__, reply->str);
|
||||||
|
}
|
||||||
|
freeReplyObject(reply);
|
||||||
|
reply = NULL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
freeReplyObject(reply);
|
||||||
|
reply = NULL;
|
||||||
|
|
||||||
|
reply = maat_cmd_wrap_redis_command(c, "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_cmd_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;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int rollback_redis_version(redisContext *c, struct log_handle *logger)
|
||||||
|
{
|
||||||
|
redisReply *reply = maat_cmd_wrap_redis_command(c, "SET MAAT_VERSION 0");
|
||||||
|
if (NULL == reply) {
|
||||||
|
log_error(logger, MODULE_FRAMEWORK_GTEST,
|
||||||
|
"[%s:%d] set MAAT_VERSION failed, Redis Communication error: %s",
|
||||||
|
__FUNCTION__, __LINE__, c->errstr);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
freeReplyObject(reply);
|
||||||
|
reply = NULL;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(MaatRollbackTest, FullConfigRollback) {
|
||||||
|
const char *table_name = "HTTP_URL";
|
||||||
|
struct maat *maat_instance = MaatRollbackTest::_shared_maat_instance;
|
||||||
|
struct log_handle *logger = MaatRollbackTest::logger;
|
||||||
|
|
||||||
|
int table_id = maat_get_table_id(maat_instance, table_name);
|
||||||
|
ASSERT_GT(table_id, 0);
|
||||||
|
|
||||||
|
long long results[ARRAY_SIZE] = {0};
|
||||||
|
size_t n_hit_result = 0;
|
||||||
|
int thread_id = 0;
|
||||||
|
struct maat_state *state = maat_state_new(maat_instance, thread_id);
|
||||||
|
const char *scan_data = "http://www.cyberessays.com/search_results.php?action=search&query=username,abckkk,1234567";
|
||||||
|
|
||||||
|
int ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data),
|
||||||
|
results, ARRAY_SIZE, &n_hit_result, state);
|
||||||
|
EXPECT_EQ(ret, MAAT_SCAN_HIT);
|
||||||
|
EXPECT_EQ(n_hit_result, 1);
|
||||||
|
EXPECT_EQ(results[0], 125);
|
||||||
|
maat_state_reset(state);
|
||||||
|
|
||||||
|
sleep(5);
|
||||||
|
char redis_ip[64] = "127.0.0.1";
|
||||||
|
int redis_port = 6379;
|
||||||
|
int redis_db = 0;
|
||||||
|
redisContext *c = maat_cmd_connect_redis(redis_ip, redis_port, redis_db, logger);
|
||||||
|
EXPECT_TRUE(c != NULL);
|
||||||
|
|
||||||
|
ret = clear_config_in_redis(c, logger);
|
||||||
|
EXPECT_EQ(ret, 0);
|
||||||
|
|
||||||
|
ret = write_config_to_redis(redis_ip, redis_port, redis_db, logger);
|
||||||
|
EXPECT_EQ(ret, 0);
|
||||||
|
|
||||||
|
ret = rollback_redis_version(c, logger);
|
||||||
|
EXPECT_EQ(ret, 0);
|
||||||
|
|
||||||
|
sleep(WAIT_FOR_EFFECTIVE_S * 2);
|
||||||
|
|
||||||
|
ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data),
|
||||||
|
results, ARRAY_SIZE, &n_hit_result, state);
|
||||||
|
EXPECT_EQ(ret, MAAT_SCAN_HIT);
|
||||||
|
EXPECT_EQ(n_hit_result, 1);
|
||||||
|
EXPECT_EQ(results[0], 125);
|
||||||
|
maat_state_free(state);
|
||||||
|
state = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char ** argv)
|
int main(int argc, char ** argv)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ void maat_tool_print_usage(void)
|
|||||||
printf("\t-p [port], redis port, 6379 as default.\n");
|
printf("\t-p [port], redis port, 6379 as default.\n");
|
||||||
printf("\t-n [db], redis db, 0 as default.\n");
|
printf("\t-n [db], redis db, 0 as default.\n");
|
||||||
printf("\t-d [dir], dump rules from redis to [dir], %s as default.\n", redis_dump_dir);
|
printf("\t-d [dir], dump rules from redis to [dir], %s as default.\n", redis_dump_dir);
|
||||||
printf("\t-u [dir], flush redis and upload full rules to redis, confirm the risk before proceeding\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-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-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("\t-t [timeout], timeout config after t seconds, default is 0 which means never timeout.\n");
|
||||||
@@ -204,11 +204,11 @@ int count_line_num_cb(const char *table_name, const char *line, void *u_para)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int line_idx = 0;
|
int line_idx = 0;
|
||||||
long long absolute_expire_time=0;
|
|
||||||
int make_serial_rule(const char *table_name, const char *line, void *u_para)
|
int make_serial_rule(const char *table_name, const char *line, void *u_para)
|
||||||
{
|
{
|
||||||
struct serial_rule *s_rule=(struct serial_rule *)u_para;
|
struct serial_rule *s_rule=(struct serial_rule *)u_para;
|
||||||
redisContext *ctx = s_rule->ref_ctx;
|
redisContext *ctx = s_rule->ref_ctx;
|
||||||
|
long long absolute_expire_time = s_rule->timeout;
|
||||||
char *buff = ALLOC(char, strlen(line) + 1);
|
char *buff = ALLOC(char, strlen(line) + 1);
|
||||||
|
|
||||||
memcpy(buff, line, strlen(line) + 1);
|
memcpy(buff, line, strlen(line) + 1);
|
||||||
@@ -236,17 +236,157 @@ int make_serial_rule(const char *table_name, const char *line, void *u_para)
|
|||||||
return 0;
|
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, logger);
|
||||||
|
|
||||||
|
struct serial_rule *s_rule = ALLOC(struct serial_rule, total_line_cnt);
|
||||||
|
s_rule->ref_ctx = c;
|
||||||
|
long long server_time = maat_cmd_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, 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_cmd_clear_rule_cache(s_rule + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
FREE(s_rule);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int rollback_redis_version(redisContext *c, struct log_handle *logger)
|
||||||
|
{
|
||||||
|
redisReply *reply = maat_cmd_wrap_redis_command(c, "SET MAAT_VERSION 0");
|
||||||
|
if (NULL == reply) {
|
||||||
|
log_error(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_cmd_wrap_redis_command(c, "GET MAAT_VERSION");
|
||||||
|
if (reply != NULL) {
|
||||||
|
if (reply->type == REDIS_REPLY_NIL || reply->type == REDIS_REPLY_ERROR) {
|
||||||
|
log_error(logger, MODULE_REDIS_TOOL,
|
||||||
|
"[%s:%d] GET MAAT_VERSION failed, maybe Redis is busy",
|
||||||
|
__FUNCTION__, __LINE__);
|
||||||
|
freeReplyObject(reply);
|
||||||
|
reply = NULL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log_error(logger, MODULE_REDIS_TOOL,
|
||||||
|
"[%s:%d] GET MAAT_VERSION failed with NULL reply, error: %s",
|
||||||
|
__FUNCTION__, __LINE__, c->errstr);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
redis_version = maat_cmd_read_redis_integer(reply);
|
||||||
|
if (redis_version < 0) {
|
||||||
|
if (reply->type == REDIS_REPLY_ERROR) {
|
||||||
|
log_error(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_cmd_wrap_redis_command(c, "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_cmd_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;
|
||||||
|
}
|
||||||
|
|
||||||
#define WORK_MODE_DUMP 0
|
#define WORK_MODE_DUMP 0
|
||||||
#define WORK_MODE_JSON 1
|
#define WORK_MODE_JSON 1
|
||||||
|
#define WORK_MODE_UPLOAD 2
|
||||||
|
|
||||||
int main(int argc, char * argv[])
|
int main(int argc, char * argv[])
|
||||||
{
|
{
|
||||||
int oc = 0;
|
int oc = 0;
|
||||||
int model = 0;
|
int mode = 0;
|
||||||
char redis_ip[64] = {0};
|
char redis_ip[64] = {0};
|
||||||
int redis_port = 6379;
|
int redis_port = 6379;
|
||||||
int redis_db = 0;
|
int redis_db = 0;
|
||||||
char dump_dir[128] = {0};
|
char dump_dir[128] = {0};
|
||||||
char json_file[128] = {0};
|
char json_file[128] = {0};
|
||||||
|
char upload_file[128] = {0};
|
||||||
char log_path[128] = "./maat_redis_tool.log";
|
char log_path[128] = "./maat_redis_tool.log";
|
||||||
int timeout = 0;
|
int timeout = 0;
|
||||||
long long desired_version = 0;
|
long long desired_version = 0;
|
||||||
@@ -254,7 +394,7 @@ int main(int argc, char * argv[])
|
|||||||
strncpy(dump_dir, redis_dump_dir, sizeof(dump_dir));
|
strncpy(dump_dir, redis_dump_dir, sizeof(dump_dir));
|
||||||
struct log_handle *logger = log_handle_create(log_path, 0);
|
struct log_handle *logger = log_handle_create(log_path, 0);
|
||||||
|
|
||||||
while ((oc = getopt(argc,argv,"h:p:n:d:v:f:j:t:")) != -1) {
|
while ((oc = getopt(argc,argv,"h:p:n:d:v:f:j:t:u:")) != -1) {
|
||||||
switch (oc) {
|
switch (oc) {
|
||||||
case 'h':
|
case 'h':
|
||||||
strncpy(redis_ip, optarg, sizeof(redis_ip));
|
strncpy(redis_ip, optarg, sizeof(redis_ip));
|
||||||
@@ -266,7 +406,7 @@ int main(int argc, char * argv[])
|
|||||||
sscanf(optarg, "%d", &redis_db);
|
sscanf(optarg, "%d", &redis_db);
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
model = WORK_MODE_DUMP;
|
mode = WORK_MODE_DUMP;
|
||||||
strncpy(dump_dir, optarg, sizeof(dump_dir));
|
strncpy(dump_dir, optarg, sizeof(dump_dir));
|
||||||
if (dump_dir[strlen(dump_dir)-1] == '/') {
|
if (dump_dir[strlen(dump_dir)-1] == '/') {
|
||||||
dump_dir[strlen(dump_dir)-1] = '\0';
|
dump_dir[strlen(dump_dir)-1] = '\0';
|
||||||
@@ -277,11 +417,15 @@ int main(int argc, char * argv[])
|
|||||||
break;
|
break;
|
||||||
case 'j':
|
case 'j':
|
||||||
strncpy(json_file, optarg, sizeof(json_file));
|
strncpy(json_file, optarg, sizeof(json_file));
|
||||||
model = WORK_MODE_JSON;
|
mode = WORK_MODE_JSON;
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
sscanf(optarg,"%d", &timeout);
|
sscanf(optarg,"%d", &timeout);
|
||||||
break;
|
break;
|
||||||
|
case 'u':
|
||||||
|
strncpy(upload_file, optarg, sizeof(upload_file));
|
||||||
|
mode = WORK_MODE_UPLOAD;
|
||||||
|
break;
|
||||||
case '?':
|
case '?':
|
||||||
default:
|
default:
|
||||||
maat_tool_print_usage();
|
maat_tool_print_usage();
|
||||||
@@ -296,11 +440,11 @@ int main(int argc, char * argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
char tmp_iris_path[128] = {0};
|
char tmp_iris_path[128] = {0};
|
||||||
if (model == WORK_MODE_DUMP) {
|
if (mode == WORK_MODE_DUMP) {
|
||||||
log_info(logger, MODULE_REDIS_TOOL, "Reading key list from %s:%d db%d.",
|
log_info(logger, MODULE_REDIS_TOOL, "Reading key list from %s:%d db%d.",
|
||||||
redis_ip, redis_port, redis_db);
|
redis_ip, redis_port, redis_db);
|
||||||
read_rule_from_redis(c, desired_version, dump_dir, logger);
|
read_rule_from_redis(c, desired_version, dump_dir, logger);
|
||||||
} else if(model == WORK_MODE_JSON) {
|
} else if(mode == WORK_MODE_JSON) {
|
||||||
char *json_buff = NULL;
|
char *json_buff = NULL;
|
||||||
size_t json_buff_sz = 0;
|
size_t json_buff_sz = 0;
|
||||||
int ret = load_file_to_memory(json_file, (unsigned char**)&json_buff, &json_buff_sz);
|
int ret = load_file_to_memory(json_file, (unsigned char**)&json_buff, &json_buff_sz);
|
||||||
@@ -331,14 +475,12 @@ int main(int argc, char * argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (timeout > 0) {
|
if (timeout > 0) {
|
||||||
absolute_expire_time = server_time + timeout;
|
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, logger);
|
config_monitor_traverse(0, tmp_iris_path, NULL, make_serial_rule, NULL, s_rule, logger);
|
||||||
s_rule->ref_ctx = NULL;
|
s_rule->ref_ctx = NULL;
|
||||||
log_info(logger, MODULE_REDIS_TOOL, "Timeout = %lld\n", absolute_expire_time);
|
|
||||||
|
|
||||||
ret = 0;
|
|
||||||
int success_cnt = 0;
|
int success_cnt = 0;
|
||||||
do {
|
do {
|
||||||
success_cnt = maat_cmd_write_rule(c, s_rule, total_line_cnt, server_time, logger);
|
success_cnt = maat_cmd_write_rule(c, s_rule, total_line_cnt, server_time, logger);
|
||||||
@@ -354,6 +496,25 @@ int main(int argc, char * argv[])
|
|||||||
}
|
}
|
||||||
FREE(s_rule);
|
FREE(s_rule);
|
||||||
redisFree(c);
|
redisFree(c);
|
||||||
|
} else if (mode == WORK_MODE_UPLOAD) {
|
||||||
|
int ret = clear_config_in_redis(c, logger);
|
||||||
|
if (ret < 0) {
|
||||||
|
log_error(logger, MODULE_REDIS_TOOL, "clear config in redis failed.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = write_config_to_redis(c, upload_file, logger);
|
||||||
|
if (ret < 0) {
|
||||||
|
log_error(logger, MODULE_REDIS_TOOL, "write config to redis failed.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = rollback_redis_version(c, logger);
|
||||||
|
if (ret < 0) {
|
||||||
|
log_error(logger, MODULE_REDIS_TOOL, "rollback redis version failed.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
redisFree(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user