rule.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. #include "rule.h"
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include "../libs/hsql/include/SQLParser.h"
  5. #include "../libs/hsql/include/util/sqlhelper.h"
  6. #include "re_comm.h"
  7. #include "re_load.h"
  8. #include "re_match.h"
  9. #include "re_cache.h"
  10. #include "log.h"
  11. #include "mxml.h"
  12. #include "yaml-cpp/yaml.h"
  13. #include "mxml.h"
  14. #define SPECIFIC_L1_SCHEMA "L1"
  15. #define SPECIFIC_L2_SCHEMA "L2"
  16. #define SPECIFIC_L3_SCHEMA "L3"
  17. #define AGENT_XML_FILE "../conf/agent.xml"
  18. using namespace std;
  19. using namespace hsql;
  20. extern std::map<std::string, std::map<std::string, std::string>> g_map_dtc_yaml;
  21. std::string get_key_info(std::map<std::string, std::string>* buf)
  22. {
  23. if(buf->find(YAML_DTC_KEY_STRING) != buf->end())
  24. return (*buf)[YAML_DTC_KEY_STRING];
  25. YAML::Node config;
  26. try {
  27. config = YAML::Load((*buf)[YAML_DTC_BUFFER]);
  28. } catch (const YAML::Exception &e) {
  29. log4cplus_error("config file error:%s\n", e.what());
  30. return "";
  31. }
  32. YAML::Node node = config["primary"]["cache"]["field"][0]["name"];
  33. if(node)
  34. {
  35. std::string keystr = node.as<string>();
  36. transform(keystr.begin(),keystr.end(),keystr.begin(),::toupper);
  37. (*buf)[YAML_DTC_KEY_STRING] = keystr;
  38. return keystr;
  39. }
  40. return "";
  41. }
  42. extern "C" int get_statement_value(char* str, int len, const char* strkey, int* start_offset, int* end_offset)
  43. {
  44. hsql::SQLParserResult sql_ast;
  45. if(re_parse_sql(str, &sql_ast) != 0)
  46. return -1;
  47. StatementType t = sql_ast.getStatement(0)->type();
  48. if(t == kStmtInsert)
  49. {
  50. const InsertStatement* stmt = (const InsertStatement*)(sql_ast.getStatement(0));
  51. if(stmt->columns == NULL) // for all
  52. {
  53. int i = 0;
  54. int pos = 0;
  55. char sztmp[100] = {0};
  56. std::string strsql;
  57. switch (stmt->values->at(i)->type)
  58. {
  59. case hsql::ExprType::kExprLiteralInt:
  60. sprintf(sztmp, "%d", stmt->values->at(i)->ival);
  61. strsql = str;
  62. pos = strsql.find(sztmp);
  63. if(pos != string::npos)
  64. {
  65. *start_offset = pos;
  66. *end_offset = pos + strlen(sztmp);
  67. return 0;
  68. }
  69. else
  70. return -1;
  71. case hsql::ExprType::kExprLiteralFloat:
  72. sprintf(sztmp, "%f", stmt->values->at(i)->fval);
  73. strsql = str;
  74. pos = strsql.find(sztmp);
  75. if(pos != string::npos)
  76. {
  77. *start_offset = pos;
  78. *end_offset = pos + strlen(sztmp);
  79. return 0;
  80. }
  81. else
  82. return -1;
  83. case hsql::ExprType::kExprLiteralString:
  84. strsql = str;
  85. pos = strsql.find(stmt->values->at(i)->name);
  86. if(pos != string::npos)
  87. {
  88. *start_offset = pos;
  89. *end_offset = pos + strlen(stmt->values->at(i)->name);
  90. return 0;
  91. }
  92. else
  93. return -1;
  94. default:
  95. return -1;
  96. }
  97. }
  98. else if(stmt->columns->size() >= 0) // specified
  99. {
  100. for(int i = 0; i < stmt->columns->size(); i++)
  101. {
  102. if(std::string(stmt->columns->at(i)) == std::string(strkey))
  103. {
  104. std::string strsql;
  105. int pos = 0;
  106. char sztmp[100] = {0};
  107. switch (stmt->values->at(i)->type) {
  108. case hsql::ExprType::kExprLiteralInt:
  109. sprintf(sztmp, "%d", stmt->values->at(i)->ival);
  110. strsql = str;
  111. pos = strsql.find(sztmp);
  112. if(pos != string::npos)
  113. {
  114. *start_offset = pos;
  115. *end_offset = pos + strlen(sztmp);
  116. return 0;
  117. }
  118. else
  119. return -1;
  120. case hsql::ExprType::kExprLiteralFloat:
  121. sprintf(sztmp, "%f", stmt->values->at(i)->fval);
  122. strsql = str;
  123. pos = strsql.find(sztmp);
  124. if(pos != string::npos)
  125. {
  126. *start_offset = pos;
  127. *end_offset = pos + strlen(sztmp);
  128. return 0;
  129. }
  130. else
  131. return -1;
  132. case hsql::ExprType::kExprLiteralString:
  133. strsql = str;
  134. pos = strsql.find(stmt->values->at(i)->name);
  135. if(pos != string::npos)
  136. {
  137. *start_offset = pos;
  138. *end_offset = pos + strlen(stmt->values->at(i)->name);
  139. return 0;
  140. }
  141. else
  142. return -1;
  143. default:
  144. return -1;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. else
  151. return -1;
  152. }
  153. std::string get_table_with_db(const char* sessiondb, const char* sql)
  154. {
  155. char result[300];
  156. hsql::SQLParserResult sql_ast;
  157. if(re_parse_sql(sql, &sql_ast) != 0)
  158. return "";
  159. memset(result, 0, 300);
  160. // Get db name
  161. std::string sqldb = get_schema(&sql_ast);
  162. if(sqldb.length() > 0)
  163. {
  164. strcat(result, sqldb.c_str());
  165. }
  166. else if(exist_session_db(sessiondb))
  167. {
  168. strcat(result, sessiondb);
  169. }
  170. else
  171. {
  172. log4cplus_error("no database selected.");
  173. return "";
  174. }
  175. //Append symbol.
  176. strcat(result, ".");
  177. // Get table name
  178. std::string sqltb = get_table_name(&sql_ast);
  179. if(sqltb.length() > 0)
  180. {
  181. strcat(result, sqltb.c_str());
  182. }
  183. else
  184. {
  185. return "";
  186. }
  187. std::string strres = result;
  188. transform(strres.begin(),strres.end(),strres.begin(),::toupper);
  189. return strres;
  190. }
  191. int rule_get_key_type(std::map<std::string, std::string>* buf)
  192. {
  193. YAML::Node config;
  194. std::string str = "";
  195. if(buf->find(YAML_DTC_KEY_TYPE) != buf->end())
  196. str = (*buf)[YAML_DTC_KEY_TYPE];
  197. if(str.length() == 0)
  198. {
  199. if((*buf)[YAML_DTC_BUFFER].length() == 0)
  200. return -1;
  201. try {
  202. config = YAML::Load((*buf)[YAML_DTC_BUFFER]);
  203. } catch (const YAML::Exception &e) {
  204. log4cplus_error("config buf load error:%s\n", e.what());
  205. return -1;
  206. }
  207. YAML::Node node = config["primary"]["cache"]["field"][0]["type"];
  208. if(node)
  209. {
  210. str = node.as<string>();
  211. (*buf)[YAML_DTC_KEY_TYPE] = str;
  212. }
  213. }
  214. if(str == "signed")
  215. return 1;
  216. else if(str == "unsigned")
  217. return 2;
  218. else if(str == "float")
  219. return 3;
  220. else if(str == "string")
  221. return 4;
  222. else if(str == "binary")
  223. return 5;
  224. else
  225. return -1;
  226. return -1;
  227. }
  228. bool ParseAgentConf(std::string path, std::vector<std::string>* vec){
  229. FILE *fp = fopen(path.c_str(), "r");
  230. if (fp == NULL) {
  231. log4cplus_error("conf: failed to open configuration '%s': %s", path.c_str(), strerror(errno));
  232. return false;
  233. }
  234. mxml_node_t* tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK);
  235. if (tree == NULL) {
  236. log4cplus_error("mxmlLoadFile error, file: %s", path.c_str());
  237. return false;
  238. }
  239. fclose(fp);
  240. mxml_node_t *poolnode, *servernode, *instancenode, *lognode;
  241. for (poolnode = mxmlFindElement(tree, tree, "MODULE",
  242. NULL, NULL, MXML_DESCEND); poolnode != NULL;
  243. poolnode = mxmlFindElement(poolnode, tree, "MODULE",
  244. NULL, NULL, MXML_DESCEND))
  245. {
  246. std::string get_str;
  247. char* name = (char *)mxmlElementGetAttr(poolnode, "Name");
  248. if(name != NULL)
  249. {
  250. get_str = name;
  251. std::transform(get_str.begin(), get_str.end(), get_str.begin(), ::toupper);
  252. vec->push_back(get_str);
  253. }
  254. }
  255. mxmlDelete(tree);
  256. return true;
  257. }
  258. int is_ext_table(hsql::SQLParserResult* ast,const char* dbname)
  259. {
  260. //get db.tb array
  261. std::vector<std::string> agent_info;
  262. //load agent.xml
  263. if(ParseAgentConf("../conf/agent.xml", &agent_info) == false)
  264. {
  265. log4cplus_error("ParseAgentConf");
  266. return -1;
  267. }
  268. //get table name
  269. std::string table_name = get_table_name(ast);
  270. if(table_name.length() == 0)
  271. {
  272. log4cplus_debug("table name can not be found");
  273. return -2;
  274. }
  275. std::string schema = get_schema(ast);
  276. //combine db.tb
  277. std::string cmp_str;
  278. if(schema.length() > 0)
  279. {
  280. cmp_str += schema;
  281. }
  282. else if(dbname != NULL && strlen(dbname) > 0)
  283. {
  284. cmp_str += dbname;
  285. }
  286. else
  287. {
  288. log4cplus_debug("db can not be found");
  289. return -2;
  290. }
  291. cmp_str += ".";
  292. cmp_str += table_name;
  293. std::transform(cmp_str.begin(), cmp_str.end(), cmp_str.begin(), ::toupper);
  294. //string compare between array and above string
  295. std::vector<std::string>::iterator iter;
  296. for(iter = agent_info.begin(); iter < agent_info.end(); iter++)
  297. {
  298. log4cplus_debug("cmp: %s, %s", iter->c_str(), cmp_str.c_str());
  299. if(*iter == cmp_str)
  300. return 0;
  301. }
  302. return -1;
  303. }
  304. bool is_set_with_ast(hsql::SQLParserResult* ast)
  305. {
  306. int t = ast->getStatement(0)->type();
  307. if(t == kStmtSet)
  308. {
  309. return true;
  310. }
  311. return false;
  312. }
  313. bool is_show_create_table_with_ast(hsql::SQLParserResult* ast)
  314. {
  315. int t = ast->getStatement(0)->type();
  316. if(t == kStmtShow)
  317. {
  318. ShowStatement* stmt = (ShowStatement*)ast->getStatement(0);
  319. if(stmt->type == ShowType::kShowCreateTables)
  320. return true;
  321. }
  322. return false;
  323. }
  324. bool is_show_table_with_ast(hsql::SQLParserResult* ast)
  325. {
  326. int t = ast->getStatement(0)->type();
  327. if(t == kStmtShow)
  328. {
  329. ShowStatement* stmt = (ShowStatement*)ast->getStatement(0);
  330. if(stmt->type == ShowType::kShowTables)
  331. return true;
  332. }
  333. return false;
  334. }
  335. bool is_show_db_with_ast(hsql::SQLParserResult* ast)
  336. {
  337. int t = ast->getStatement(0)->type();
  338. if(t == kStmtShow)
  339. {
  340. //show databases;
  341. ShowStatement* stmt = (ShowStatement*)ast->getStatement(0);
  342. if(stmt->type == ShowType::kShowDatabases)
  343. return true;
  344. }
  345. else if(t == kStmtSelect)
  346. {
  347. //select database();
  348. SelectStatement* stmt = (SelectStatement*)ast->getStatement(0);
  349. if(stmt->select_object_type == SelectObjectType::kDataBase)
  350. return true;
  351. }
  352. return false;
  353. }
  354. bool exist_session_db(const char* dbname)
  355. {
  356. if(dbname != NULL && strlen(dbname) > 0)
  357. return true;
  358. return false;
  359. }
  360. bool exist_sql_db(hsql::SQLParserResult* ast)
  361. {
  362. if(get_schema(ast).length() > 0)
  363. return true;
  364. return false;
  365. }
  366. bool is_dtc_instance(std::string key)
  367. {
  368. if(key.length() > 0)
  369. return true;
  370. else
  371. return false;
  372. }
  373. extern "C" int re_load_all_rules()
  374. {
  375. init_log4cplus();
  376. FILE *fp = fopen(AGENT_XML_FILE, "r");
  377. mxml_node_t *poolnode = NULL;
  378. if (fp == NULL) {
  379. log4cplus_error("conf: failed to open configuration '%s': %s", AGENT_XML_FILE, strerror(errno));
  380. return false;
  381. }
  382. mxml_node_t* tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK);
  383. if (tree == NULL) {
  384. log4cplus_error("mxmlLoadFile error, file: %s", AGENT_XML_FILE);
  385. return false;
  386. }
  387. fclose(fp);
  388. for (poolnode = mxmlFindElement(tree, tree, "MODULE", NULL, NULL, MXML_DESCEND);
  389. poolnode != NULL;
  390. poolnode = mxmlFindElement(poolnode, tree, "MODULE", NULL, NULL, MXML_DESCEND))
  391. {
  392. char* Mid = (char *) mxmlElementGetAttr(poolnode, "Mid");
  393. if (Mid == NULL) {
  394. log4cplus_error("get Mid from conf '%s' error", AGENT_XML_FILE);
  395. mxmlDelete(tree);
  396. return false;
  397. }
  398. int imid = atoi(Mid);
  399. char* Name = (char *) mxmlElementGetAttr(poolnode, "Name");
  400. if (Name == NULL) {
  401. log4cplus_error("get Name from conf '%s' error", AGENT_XML_FILE);
  402. mxmlDelete(tree);
  403. return false;
  404. }
  405. std::string buf = load_dtc_yaml_buffer(imid);
  406. if(buf.length() > 0)
  407. {
  408. log4cplus_debug("push %s into map.", Name);
  409. std::string strname = Name;
  410. transform(strname.begin(),strname.end(),strname.begin(),::toupper);
  411. (g_map_dtc_yaml[strname])[YAML_DTC_BUFFER] = buf;
  412. log4cplus_debug("name: %s, buf len: %d", strname.c_str(), (g_map_dtc_yaml[strname])[YAML_DTC_BUFFER].length());
  413. }
  414. else
  415. {
  416. log4cplus_error("get dtc: %d yaml buffer error.", imid);
  417. return -2;
  418. }
  419. }
  420. mxmlDelete(tree);
  421. return 0;
  422. }
  423. extern "C" int rule_sql_match(const char* szsql, const char* osql, const char* dbsession, char* out_dtckey, int* out_keytype)
  424. {
  425. if(!szsql)
  426. return -1;
  427. std::string dtc_key = "";
  428. std::string sql = szsql;
  429. //init_log4cplus();
  430. log4cplus_debug("input sql: %s", osql);
  431. std::string db_dot_name = get_table_with_db(dbsession, szsql);
  432. if(db_dot_name.length() > 0 && g_map_dtc_yaml.count(db_dot_name) > 0)
  433. {
  434. dtc_key = get_key_info(&(g_map_dtc_yaml[db_dot_name]));
  435. if(dtc_key.length() == 0)
  436. {
  437. log4cplus_error("get dtc_key from yaml:%s failed.", db_dot_name.c_str());
  438. return -1;
  439. }
  440. strcpy(out_dtckey, dtc_key.c_str());
  441. *out_keytype = rule_get_key_type(&(g_map_dtc_yaml[db_dot_name]));
  442. }
  443. log4cplus_debug("dtc key len: %d, key: %s, dbname len: %d, dbname: %s", dtc_key.length(), dtc_key.c_str(), strlen(dbsession), std::string(dbsession).c_str());
  444. if(sql.find("WITHOUT@@") != sql.npos)
  445. {
  446. //L1: DTC cache.
  447. log4cplus_debug("layered: L1, data-lifecycle request, force routed.");
  448. return 1;
  449. }
  450. hsql::SQLParserResult sql_ast;
  451. if(re_parse_sql(osql, &sql_ast) != 0)
  452. {
  453. log4cplus_debug("layered: error, parse sql failed.");
  454. return -1;
  455. }
  456. if(is_show_db_with_ast(&sql_ast))
  457. {
  458. log4cplus_debug("layered: L3, SHOW statment.");
  459. return 2;
  460. }
  461. if(is_set_with_ast(&sql_ast))
  462. {
  463. log4cplus_debug("layered: L2, SET statement.");
  464. return 2;
  465. }
  466. if(is_show_table_with_ast(&sql_ast))
  467. {
  468. if(exist_session_db(dbsession))
  469. {
  470. log4cplus_debug("layered: L2, session db.");
  471. return 2;
  472. }
  473. else
  474. {
  475. log4cplus_debug("layered: error, no session db.");
  476. return -6;
  477. }
  478. }
  479. if(is_show_create_table_with_ast(&sql_ast))
  480. {
  481. if(exist_session_db(dbsession))
  482. {
  483. log4cplus_debug("layered: L2, show create table.");
  484. return 2;
  485. }
  486. else
  487. {
  488. log4cplus_debug("layered: error, no session db.");
  489. return -6;
  490. }
  491. }
  492. log4cplus_debug("Is dtc instance: %d %d %d", is_dtc_instance(dtc_key), exist_session_db(dbsession), exist_sql_db(&sql_ast));
  493. if((exist_session_db(dbsession) || (exist_sql_db(&sql_ast))) && !is_dtc_instance(dtc_key))
  494. {
  495. log4cplus_debug("layered: L2, db session & single table");
  496. return 2;
  497. }
  498. vector<vector<hsql::Expr*> > expr_rules;
  499. expr_rules.clear();
  500. hsql::SQLParserResult rule_ast;
  501. int ret = re_load_rule(&(g_map_dtc_yaml[db_dot_name]), &rule_ast, &expr_rules);
  502. if(ret != 0)
  503. {
  504. log4cplus_error("load rule error:%d", ret);
  505. return -5;
  506. }
  507. //Building condition sql tree, in order to do layered rule matching.
  508. hsql::SQLParserResult* ast = NULL;
  509. hsql::SQLParserResult ast2;
  510. std::string tempsql = "SELECT * FROM TMPTB ";
  511. if(sql.find("WHERE") != -1)
  512. {
  513. tempsql += sql.substr(sql.find("WHERE"));
  514. if(re_parse_sql(tempsql, &ast2) != 0)
  515. return -1;
  516. log4cplus_debug("temsql: %s", tempsql.c_str());
  517. ast = &ast2;
  518. }
  519. else if(sql_ast.getStatement(0)->type() == StatementType::kStmtInsert)
  520. {
  521. tempsql += "WHERE ";
  522. const InsertStatement* stmt = (const InsertStatement*)(sql_ast.getStatement(0));
  523. if(stmt->columns == NULL) // for all, not supported right now.
  524. return -1;
  525. for(int i = 0; i < stmt->columns->size(); i ++)
  526. {
  527. char sztmp[100] = {0};
  528. tempsql += stmt->columns->at(i);
  529. tempsql += "=";
  530. log4cplus_debug("name: %s, type: %d", stmt->columns->at(i), stmt->values->at(i)->type);
  531. if(stmt->values->at(i)->type == hsql::ExprType::kExprLiteralInt)
  532. {
  533. sprintf(sztmp, "%lld", stmt->values->at(i)->ival);
  534. tempsql += sztmp;
  535. }
  536. else if(stmt->values->at(i)->type == hsql::ExprType::kExprLiteralFloat)
  537. {
  538. sprintf(sztmp, "%f", stmt->values->at(i)->fval);
  539. tempsql += sztmp;
  540. }
  541. else if(stmt->values->at(i)->type == hsql::ExprType::kExprLiteralString)
  542. {
  543. tempsql += "'";
  544. tempsql += stmt->values->at(i)->name;
  545. tempsql += "'";
  546. }
  547. tempsql += " ";
  548. if(i + 1 < stmt->columns->size())
  549. tempsql += "AND ";
  550. }
  551. if(re_parse_sql(tempsql, &ast2) != 0)
  552. return -1;
  553. log4cplus_debug("temsql: %s", tempsql.c_str());
  554. ast = &ast2;
  555. }
  556. ret = re_match_sql(&sql_ast, expr_rules, ast); //rule match
  557. if(ret == 0 || is_update_delete_type(&sql_ast))
  558. {
  559. log4cplus_debug("dtc key: %s", dtc_key.c_str());
  560. if(re_is_cache_sql(&sql_ast, dtc_key)) //if exist dtc key.
  561. {
  562. //L1: DTC cache.
  563. log4cplus_debug("layered: L1.");
  564. return 1;
  565. }
  566. else
  567. {
  568. if(is_write_type(&sql_ast))
  569. {
  570. log4cplus_debug("layered: ERROR, writing without key, Refuse.");
  571. return -6;
  572. }
  573. else
  574. {
  575. //L2: sharding hot database.
  576. log4cplus_debug("layered: L2.");
  577. return 2;
  578. }
  579. }
  580. }
  581. else if(ret == -100)
  582. {
  583. //writing without match rule, Refuse.
  584. log4cplus_debug("layered: ERROR, writing without match rule, Refuse.");
  585. return -6;
  586. }
  587. else {
  588. //L3: full database.
  589. log4cplus_debug("layered: L3.");
  590. return 3;
  591. }
  592. log4cplus_debug("layered: L3.");
  593. return 3;
  594. }
  595. extern "C" int sql_parse_table(const char* szsql, char* out)
  596. {
  597. hsql::SQLParserResult sql_ast;
  598. if(re_parse_sql(szsql, &sql_ast) != 0)
  599. return -1;
  600. std::string tablename = get_table_name(&sql_ast);
  601. if(tablename.length() > 0)
  602. {
  603. strcpy(out, tablename.c_str());
  604. }
  605. return tablename.length();
  606. }