dbconfig.cc 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <fcntl.h>
  21. #include <sys/mman.h>
  22. #include <dlfcn.h>
  23. #include <ctype.h>
  24. #include "value.h"
  25. #include "protocol.h"
  26. #include "../log/log.h"
  27. #include "config/dbconfig.h"
  28. #include "../table/table_def.h"
  29. #include "config.h"
  30. #include <libgen.h>
  31. #include "../../../core/global.h"
  32. #define HELPERPATHFORMAT "@dtcd[%d]helper%d%c"
  33. #define DTC_EXPIRE_TIME_FIELD "_dtc_sys_expiretime"
  34. static int ParseDbLine(const char *buf, uint16_t *dbIdx)
  35. {
  36. char *p = (char *)buf; // discard const
  37. if(*p++ != '[')
  38. return -1;
  39. int n = 0;
  40. while(*p)
  41. {
  42. if(!isdigit(p[0]))
  43. break;
  44. int begin, end;
  45. begin = strtol(p, &p, 0);
  46. if(*p!='-' )
  47. end = begin;
  48. else {
  49. p++;
  50. if(!isdigit(p[0]))
  51. break;
  52. end = strtol(p, &p, 0);
  53. }
  54. while(begin <= end)
  55. dbIdx[n++] = begin++;
  56. if(p[0]!=',')
  57. break;
  58. else
  59. p++;
  60. }
  61. if(p[0] != ']')
  62. return -1;
  63. return n;
  64. }
  65. /* 前置空格已经过滤了 */
  66. static char *skip_blank(char *p)
  67. {
  68. char *iter = p;
  69. while (!isspace(*iter) && *iter != '\0')
  70. ++iter;
  71. *iter = '\0';
  72. return p;
  73. }
  74. bool MachineConfig::is_same(MachineConfig *mach)
  75. {
  76. // no need check NULL pointer as NULL is '\0'
  77. if (strcmp(this->role[0].addr, mach->role[0].addr))
  78. return false;
  79. if (strcmp(this->role[0].user, mach->role[0].user))
  80. return false;
  81. if (strcmp(this->role[0].pass, mach->role[0].pass))
  82. return false;
  83. if (mode != mach->mode)
  84. return false;
  85. if (mode > 0) {
  86. if (strcmp(this->role[1].addr, mach->role[1].addr))
  87. return false;
  88. if (strcmp(this->role[1].user, mach->role[1].user))
  89. return false;
  90. if (strcmp(this->role[1].pass, mach->role[1].pass))
  91. return false;
  92. }
  93. return true;
  94. }
  95. bool FieldConfig::is_same(FieldConfig *field)
  96. {
  97. if (type != field->type && size != field->size &&
  98. flags != field->flags && strcmp(name, field->name))
  99. return false;
  100. switch (type) {
  101. case DField::Signed:
  102. case DField::Unsigned:
  103. case DField::Float:
  104. if (dval.u64 != field->dval.u64)
  105. return false;
  106. break;
  107. case DField::String:
  108. case DField::Binary:
  109. if (dval.bin.len != field->dval.bin.len)
  110. return false;
  111. if (dval.bin.len == 0)
  112. return true;
  113. if (memcmp(dval.bin.ptr, field->dval.bin.ptr, dval.bin.len) !=
  114. 0)
  115. return false;
  116. break;
  117. default:
  118. return false;
  119. }
  120. return true;
  121. }
  122. int DbConfig::load_key_hash(DTCConfig *raw)
  123. {
  124. log4cplus_debug("try to load key-hash plugin");
  125. bool hashopen = false;
  126. /* init. */
  127. keyHashConfig.keyHashEnable = 0;
  128. keyHashConfig.keyHashFunction = 0;
  129. keyHashConfig.keyHashLeftBegin = 0;
  130. keyHashConfig.keyHashRightBegin = 0;
  131. /* not enable key-hash */
  132. if(raw->get_config_node()["primary"]["hot"])
  133. {
  134. if(raw->get_config_node()["primary"]["hot"]["sharding"] &&
  135. (raw->get_config_node()["primary"]["cache"]["field"][0]["type"].as<std::string>() == "string" ||
  136. raw->get_config_node()["primary"]["cache"]["field"][0]["type"].as<std::string>() == "binary"))
  137. {
  138. hashopen = true;
  139. }
  140. }
  141. if (!hashopen) {
  142. log4cplus_debug("key-hash plugin disable");
  143. return 0;
  144. }
  145. /* read key_hash_module */
  146. std::string so = DEFAULT_KEY_HASH_SO_NAME;
  147. /* read key_hash_function */
  148. std::string var = DEFAULT_KEY_HASH_FUNCTION;
  149. char *fun = 0;
  150. int isfunalloc = 0;
  151. const char *iter = strchr(var.c_str(), '(');
  152. if (NULL == iter) {
  153. /*
  154. * 按照整个buffer来处理
  155. */
  156. keyHashConfig.keyHashLeftBegin = 1;
  157. keyHashConfig.keyHashRightBegin = -1;
  158. fun = (char *)var.c_str();
  159. } else {
  160. fun = strndup(var.c_str(), (size_t)(iter - var.c_str()));
  161. isfunalloc = 1;
  162. if (sscanf(iter, "(%d, %d)", &keyHashConfig.keyHashLeftBegin,
  163. &keyHashConfig.keyHashRightBegin) != 2) {
  164. free(fun);
  165. log4cplus_error(
  166. "key-hash plugin function format error, %s:%s",
  167. so.c_str(), var.c_str());
  168. return -1;
  169. }
  170. if (keyHashConfig.keyHashLeftBegin == 0)
  171. keyHashConfig.keyHashLeftBegin = 1;
  172. if (keyHashConfig.keyHashRightBegin == 0)
  173. keyHashConfig.keyHashRightBegin = -1;
  174. }
  175. /* 过滤fun中的空格*/
  176. fun = skip_blank(fun);
  177. void *dll = dlopen(so.c_str(), RTLD_NOW | RTLD_GLOBAL);
  178. if (dll == (void *)NULL) {
  179. if (isfunalloc)
  180. free(fun);
  181. log4cplus_error("dlopen(%s) error: %s", so.c_str(), dlerror());
  182. return -1;
  183. }
  184. /* find key-hash function in key-hash.so */
  185. keyHashConfig.keyHashFunction = (key_hash_interface)dlsym(dll, fun);
  186. if (keyHashConfig.keyHashFunction == NULL) {
  187. log4cplus_error(
  188. "key-hash plugin function[%s] not found in [%s]", fun,
  189. so.c_str());
  190. if (isfunalloc)
  191. free(fun);
  192. return -1;
  193. }
  194. /* check passed, enable key-hash */
  195. keyHashConfig.keyHashEnable = 1;
  196. log4cplus_info("key-hash plugin %s->%s(%d, %d) %s",
  197. basename((char *)so.c_str()), fun,
  198. keyHashConfig.keyHashLeftBegin,
  199. keyHashConfig.keyHashRightBegin,
  200. keyHashConfig.keyHashEnable ? "enable" : "disable");
  201. if (isfunalloc)
  202. free(fun);
  203. return 0;
  204. }
  205. bool DbConfig::compare_mach(DbConfig *config)
  206. {
  207. bool found = true;
  208. if (this->machineCnt != config->machineCnt)
  209. return false;
  210. for (int i = 0; found && i < this->machineCnt; ++i) {
  211. found = false;
  212. for (int j = 0; j < config->machineCnt; ++j) {
  213. if (this->mach[i].is_same(config->mach + j)) {
  214. found = true;
  215. break;
  216. }
  217. }
  218. if (!found)
  219. return false;
  220. }
  221. return true;
  222. }
  223. bool DbConfig::Compare(DbConfig *config, bool compareMach)
  224. {
  225. if (compareMach && !compare_mach(config))
  226. return false;
  227. if (depoly != config->depoly || keyFieldCnt != config->keyFieldCnt ||
  228. idxFieldCnt != config->idxFieldCnt ||
  229. fieldCnt != config->fieldCnt ||
  230. database_max_count != config->database_max_count ||
  231. dbDiv != config->dbDiv || dbMod != config->dbMod ||
  232. tblMod != config->tblMod || tblDiv != config->tblDiv) {
  233. log4cplus_error(
  234. "before origin after param, depoly:[%d,%d],keyFieldCnt:[%d,%d],idxFieldCnt:[%d,%d],fieldCnt:[%d,%d],database_max_count:[%d,%d],dbDiv:[%d,%d],dbMod:[%d,%d],tblMod:[%d,%d],tblDiv:[%d,%d]",
  235. depoly, config->depoly, keyFieldCnt,
  236. config->keyFieldCnt, idxFieldCnt, config->idxFieldCnt,
  237. fieldCnt, config->fieldCnt, database_max_count,
  238. config->database_max_count, dbDiv, config->dbDiv, dbMod,
  239. config->dbMod, tblMod, config->tblMod, tblDiv,
  240. config->tblDiv);
  241. return false;
  242. }
  243. for (int i = 0; i < fieldCnt; ++i) {
  244. if (!(field + i)->is_same(config->field + i)) {
  245. log4cplus_error("field%d does not match", i + 1);
  246. return false;
  247. }
  248. }
  249. return true;
  250. }
  251. int DbConfig::find_new_mach(DbConfig *config, std::vector<int> &newMach,
  252. std::map<int, int> &machMap)
  253. {
  254. // be careful, same machine numbers, but change db distribution with new machine cause bug
  255. if (this->machineCnt == config->machineCnt)
  256. return 0;
  257. if (newMach.size() > 0)
  258. newMach.clear();
  259. if (machMap.size() > 0)
  260. machMap.clear();
  261. std::map<int, int> old2New;
  262. for (int i = 0; i < this->machineCnt; ++i) {
  263. bool found = false;
  264. for (int j = 0; j < config->machineCnt; ++j) {
  265. if (config->mach[j].is_same(this->mach + i)) {
  266. if (old2New.find(j) != old2New.end()) {
  267. log4cplus_error(
  268. "multi new machines to one old machine");
  269. return 0;
  270. }
  271. found = true;
  272. machMap[i] = j;
  273. old2New[j] = i;
  274. break;
  275. }
  276. }
  277. if (!found) {
  278. newMach.push_back(i);
  279. log4cplus_debug("found new db machine No. %d", i + 1);
  280. }
  281. }
  282. return newMach.size();
  283. }
  284. int DbConfig::convert_case_sensitivity(
  285. std::string& s_val)
  286. {
  287. int i = 0;
  288. while (s_val[i] != '\0' && i < s_val.length())
  289. {
  290. if (isupper(s_val[i])) {
  291. s_val[i] = tolower(s_val[i]);
  292. } else if (islower(s_val[i])) {
  293. s_val[i] = toupper(s_val[i]);
  294. }
  295. ++i;
  296. }
  297. return i;
  298. }
  299. std::string get_merge_string(YAML::Node node)
  300. {
  301. std::string str = "";
  302. if(!node)
  303. return str;
  304. for(int i = 0; i < node.size(); i++)
  305. {
  306. str += node[i].as<string>();
  307. }
  308. return str;
  309. }
  310. int DbConfig::get_dtc_config(YAML::Node dtc_config, DTCConfig* raw, int i_server_type)
  311. {
  312. std::string layer("");
  313. if(!dtc_config)
  314. return -1;
  315. log4cplus_info("primary table: %s", dtc_config["primary"]["table"].as<string>().c_str());
  316. switch (i_server_type)
  317. {
  318. case 0:
  319. layer = "hot";
  320. break;
  321. case 1:
  322. layer = "full";
  323. break;
  324. default:
  325. log4cplus_error("%s: %d.", "invalid layer", i_server_type);
  326. return -1;
  327. }
  328. log4cplus_info("layered storage: %s.", layer.c_str());
  329. const char *cp = NULL;
  330. char *p;
  331. //DB section
  332. if(dtc_config["primary"][layer]) //cache.datasource mode
  333. {
  334. machineCnt = dtc_config["primary"][layer]["real"].size();
  335. if (machineCnt <= 0) {
  336. log4cplus_error("%s", "invalid server_count");
  337. return -1;
  338. }
  339. }
  340. else{
  341. machineCnt = 0;
  342. }
  343. //Depoly
  344. if(dtc_config["primary"][layer]) //cache.datasource mode
  345. {
  346. YAML::Node node = dtc_config["primary"][layer]["real"];
  347. if(node.size() == 1 && dtc_config["primary"][layer]["real"][0]["db"].IsScalar()) //single db
  348. {
  349. if(dtc_config["primary"][layer]["sharding"] &&
  350. dtc_config["primary"][layer]["sharding"]["table"]["last"].as<int>() - dtc_config["primary"][layer]["sharding"]["table"]["start"].as<int>() + 1 > 1)
  351. depoly = SINGLE_DB_SHARDING_TAB;
  352. else
  353. depoly = SINGLE;
  354. }
  355. else if(node.size() >= 1) //multi db
  356. {
  357. if(dtc_config["primary"][layer]["sharding"] &&
  358. dtc_config["primary"][layer]["sharding"]["table"]["last"].as<int>() - dtc_config["primary"][layer]["sharding"]["table"]["start"].as<int>() + 1 > 1)
  359. depoly = SHARDING_DB_SHARDING_TAB;
  360. else
  361. depoly = SHARDING_DB_ONE_TAB;
  362. }
  363. else
  364. {
  365. log4cplus_error("%s", "invalid server_count");
  366. return -1;
  367. }
  368. }
  369. else
  370. {
  371. depoly = SINGLE;
  372. }
  373. //DB Name
  374. if(dtc_config["primary"][layer]) //cache.datasource mode
  375. {
  376. YAML::Node node = dtc_config["primary"][layer]["real"][0]["db"];
  377. if(!node)
  378. {
  379. log4cplus_error("primary.layer.real db not defined");
  380. return -1;
  381. }
  382. else
  383. {
  384. if(node.IsScalar())
  385. dbName = STRDUP(node.as<string>().c_str());
  386. else
  387. dbName = STRDUP(get_merge_string(node["prefix"]).c_str());
  388. }
  389. }
  390. dstype = 0;
  391. checkTable = 0;
  392. // key-hash dll
  393. if (load_key_hash(raw) != 0)
  394. return -1;
  395. if(dtc_config["primary"][layer])
  396. {
  397. if ((depoly & 1) == 0) { //single db
  398. if (strchr(dbName, '%') != NULL) {
  399. log4cplus_error(
  400. "Invalid [DATABASE_CONF].database_name, cannot contain symbol '%%'");
  401. return -1;
  402. }
  403. dbDiv = 1;
  404. dbMod = 1;
  405. dbFormat = dbName;
  406. } else {
  407. dbDiv = 1;
  408. if(dtc_config["primary"][layer]["real"][0]["db"].IsScalar())
  409. {
  410. dbMod = 1;
  411. }
  412. else
  413. {
  414. dbMod = dtc_config["primary"][layer]["real"][dtc_config["primary"][layer]["real"].size()-1]["db"]["last"].as<int>() -
  415. dtc_config["primary"][layer]["real"][0]["db"]["start"].as<int>() + 1;
  416. }
  417. if (dbMod > 100) {
  418. log4cplus_error(
  419. "invalid [DATABASE_CONF].DbMod = %d, mod value too large",
  420. dbMod);
  421. return -1;
  422. }
  423. p = strchr(dbName, '%');
  424. if (p) {
  425. dbFormat = STRDUP(dbName);
  426. *p = '\0';
  427. } else {
  428. dbFormat = (char *)MALLOC(strlen(dbName) + 3);
  429. snprintf(dbFormat, strlen(dbName) + 3, "%s%%d", dbName);
  430. }
  431. }
  432. if(dtc_config["primary"][layer]["real"][0]["db"].IsScalar())
  433. {
  434. database_max_count = 1;
  435. }
  436. else
  437. {
  438. database_max_count = dtc_config["primary"][layer]["real"][0]["db"]["last"].as<int>() - dtc_config["primary"][layer]["real"][0]["db"]["start"].as<int>() + 1;
  439. }
  440. if (database_max_count < 0 || database_max_count > 10000) {
  441. log4cplus_error("%s", "invalid [DATABASE_CONF].DbMax");
  442. return -1;
  443. }
  444. if (database_max_count < (int)dbMod) {
  445. log4cplus_warning(
  446. "invalid [TABLE_CONF].DbMax too small, increase to %d",
  447. dbMod);
  448. database_max_count = dbMod;
  449. }
  450. //Table section with DATABASE_IN_ADDITION.
  451. YAML::Node node = dtc_config["primary"][layer]["sharding"]["table"]["prefix"];
  452. if(node)
  453. {
  454. tblName = STRDUP(get_merge_string(node).c_str());
  455. }
  456. else if(dtc_config["primary"]["table"])
  457. {
  458. tblName = STRDUP(dtc_config["primary"]["table"].as<string>().c_str());
  459. }
  460. else
  461. {
  462. log4cplus_error("table name not defined");
  463. return -1;
  464. }
  465. if ((depoly & 2) == 0) {
  466. if (strchr(tblName, '%') != NULL) {
  467. log4cplus_error(
  468. "Invalid table_name, cannot contain symbol '%%'");
  469. return -1;
  470. }
  471. tblDiv = 1;
  472. tblMod = 1;
  473. tblFormat = tblName;
  474. } else {
  475. if(dtc_config["primary"][layer]["real"][0]["db"].IsScalar())
  476. {
  477. tblDiv = 1;
  478. }
  479. else
  480. {
  481. tblDiv = dtc_config["primary"][layer]["real"][dtc_config["primary"][layer]["real"].size()-1]["db"]["last"].as<int>() -
  482. dtc_config["primary"][layer]["real"][0]["db"]["start"].as<int>() + 1;
  483. }
  484. tblMod = dtc_config["primary"][layer]["sharding"]["table"]["last"].as<int>() - dtc_config["primary"][layer]["sharding"]["table"]["start"].as<int>() + 1;
  485. if(tblDiv == 0 || tblMod == 0) {
  486. log4cplus_error("invalid [TABLE_CONF].tblDiv = %d, tblMod = %d",
  487. tblDiv, tblMod);
  488. return -1;
  489. }
  490. p = strchr(tblName, '%');
  491. if (p) {
  492. tblFormat = STRDUP(tblName);
  493. *p = '\0';
  494. } else {
  495. tblFormat = (char *)MALLOC(strlen(tblName) + 3);
  496. snprintf(tblFormat, strlen(tblName) + 3, "%s%%d",
  497. tblName);
  498. }
  499. }
  500. }
  501. else
  502. {
  503. //Table section with CACHE_ONLY
  504. YAML::Node node = dtc_config["primary"]["table"];
  505. if(!node)
  506. {
  507. log4cplus_error("table name not defined");
  508. return -1;
  509. }
  510. tblName = STRDUP(node.as<string>().c_str());
  511. if ((depoly & 2) == 0)
  512. {
  513. if (strchr(tblName, '%') != NULL) {
  514. log4cplus_error(
  515. "Invalid table_name, cannot contain symbol '%%'");
  516. return -1;
  517. }
  518. tblDiv = 1;
  519. tblMod = 1;
  520. tblFormat = tblName;
  521. }
  522. }
  523. fieldCnt = dtc_config["primary"]["cache"]["field"].size();
  524. if (fieldCnt <= 0 || fieldCnt > 240) {
  525. log4cplus_error("invalid [TABLE_CONF].field_count:%d", fieldCnt);
  526. return -1;
  527. }
  528. keyFieldCnt = 1;
  529. if (keyFieldCnt <= 0 || keyFieldCnt > 32 || keyFieldCnt > fieldCnt) {
  530. log4cplus_error("invalid [TABLE_CONF].key_count");
  531. return -1;
  532. }
  533. log4cplus_info("keyFieldCnt:%d" , keyFieldCnt);
  534. idxFieldCnt = 0;
  535. if (keyFieldCnt < 0 || keyFieldCnt + idxFieldCnt > fieldCnt) {
  536. log4cplus_error("invalid [TABLE_CONF].IndexFieldCount");
  537. return -1;
  538. }
  539. ordIns = 0;
  540. if (ordIns < 0) {
  541. log4cplus_error("bad [TABLE_CONF].ServerOrderInsert");
  542. return -1;
  543. }
  544. //Machine setction
  545. mach = (struct MachineConfig *)calloc(machineCnt,
  546. sizeof(struct MachineConfig));
  547. if (!mach) {
  548. log4cplus_error("malloc failed, %m");
  549. return -1;
  550. }
  551. for (int i = 0; i < machineCnt; i++) {
  552. struct MachineConfig *m = &mach[i];
  553. m->helperType = MYSQL_HELPER;
  554. //master
  555. m->role[0].addr = STRDUP(dtc_config["primary"][layer]["real"][i]["addr"].as<string>().c_str());
  556. m->role[0].user = STRDUP(dtc_config["primary"][layer]["real"][i]["user"].as<string>().c_str());
  557. m->role[0].pass = STRDUP(dtc_config["primary"][layer]["real"][i]["pwd"].as<string>().c_str());
  558. m->role[0].optfile = STRDUP("../conf/my.conf");
  559. log4cplus_info("addr:%s,user:%s" , m->role[0].addr , m->role[0].user);
  560. /* master DB settings */
  561. if (m->role[0].addr == NULL) {
  562. log4cplus_error("missing database_address");
  563. return -1;
  564. }
  565. if (m->role[0].user == NULL)
  566. m->role[0].user = "";
  567. if (m->role[0].pass == NULL)
  568. m->role[0].pass = "";
  569. if (m->role[0].optfile == NULL)
  570. m->role[0].optfile = "";
  571. m->mode = 0;
  572. if(dtc_config["primary"][layer]["real"][i]["db"].IsScalar())
  573. m->dbCnt = 1;
  574. else
  575. {
  576. char szIdx[64] = {0};
  577. sprintf(szIdx, "[%d-%d]", dtc_config["primary"][layer]["real"][i]["db"]["start"].as<int>(),
  578. dtc_config["primary"][layer]["real"][i]["db"]["last"].as<int>());
  579. m->dbCnt = ParseDbLine (szIdx, m->dbIdx);
  580. }
  581. for (int j = 0; j < m->dbCnt; j++) {
  582. if (m->dbIdx[j] >= database_max_count) {
  583. log4cplus_error(
  584. "dbConfig error, database_max_count=%d, machine[%d].dbIdx=%d",
  585. database_max_count, j + 1, m->dbIdx[j]);
  586. return -1;
  587. }
  588. }
  589. /* Helper number alter */
  590. m->gprocs[0] = dtc_config["props"]["connector.procs"].as<int>();
  591. if (m->gprocs[0] < 1)
  592. m->gprocs[0] = 0;
  593. m->gprocs[1] = dtc_config["props"]["connector.procs"].as<int>();
  594. if (m->gprocs[1] < 1)
  595. m->gprocs[1] = 0;
  596. m->gprocs[2] = dtc_config["props"]["connector.procs"].as<int>();
  597. if (m->gprocs[2] < 1)
  598. m->gprocs[2] = 0;
  599. /* Helper Queue Size */
  600. m->gqueues[0] = raw->get_int_val(NULL, "QueueSize", 0);
  601. m->gqueues[1] =
  602. raw->get_int_val(NULL, "WriteQueueSize", 0);
  603. m->gqueues[2] =
  604. raw->get_int_val(NULL, "CommitQueueSize", 0);
  605. if (m->gqueues[0] <= 0)
  606. m->gqueues[0] = 1000;
  607. if (m->gqueues[1] <= 0)
  608. m->gqueues[1] = 1000;
  609. if (m->gqueues[2] <= 0)
  610. m->gqueues[2] = 10000;
  611. if (m->gqueues[0] <= 2)
  612. m->gqueues[0] = 2;
  613. if (m->gqueues[1] <= 2)
  614. m->gqueues[1] = 2;
  615. if (m->gqueues[2] <= 1000)
  616. m->gqueues[2] = 1000;
  617. if (m->dbCnt == 0) // database_index is NULL, no helper needed
  618. {
  619. m->gprocs[0] = 0;
  620. m->gprocs[1] = 0;
  621. m->gprocs[2] = 0;
  622. m->mode = 0;
  623. }
  624. switch (m->mode) {
  625. case BY_SLAVE:
  626. case BY_DB:
  627. case BY_TABLE:
  628. case BY_KEY:
  629. m->gprocs[3] = m->gprocs[0];
  630. m->gqueues[3] = m->gqueues[0];
  631. if (slaveGuard == 0) {
  632. slaveGuard = raw->get_int_val(
  633. "DATABASE_CONF", "SlaveGuardTime", 15);
  634. if (slaveGuard < 5)
  635. slaveGuard = 5;
  636. }
  637. break;
  638. default:
  639. m->gprocs[3] = 0;
  640. m->gqueues[3] = 0;
  641. }
  642. m->procs = m->gprocs[0] + m->gprocs[1] + m->gprocs[2];
  643. procs += m->procs;
  644. }
  645. //Field section
  646. field = (struct FieldConfig *)calloc(fieldCnt,
  647. sizeof(struct FieldConfig));
  648. if (!field) {
  649. log4cplus_error("malloc failed, %m");
  650. return -1;
  651. }
  652. autoinc = -1;
  653. lastmod = -1;
  654. lastcmod = -1;
  655. lastacc = -1;
  656. compressflag = -1;
  657. expireTime = -1;
  658. for (int i = 0; i < fieldCnt; i++) {
  659. struct FieldConfig *f = &field[i];
  660. f->name = STRDUP(dtc_config["primary"]["cache"]["field"][i]["name"].as<string>().c_str());
  661. if (f->name == NULL) {
  662. log4cplus_error("field name missing for %d",
  663. i);
  664. return -1;
  665. }
  666. f->type = -1;
  667. string type = dtc_config["primary"]["cache"]["field"][i]["type"].as<string>();
  668. if(type == "signed")
  669. f->type = 1;
  670. else if(type == "unsigned")
  671. f->type = 2;
  672. else if(type == "float")
  673. f->type = 3;
  674. else if(type == "string")
  675. f->type = 4;
  676. else if(type == "binary")
  677. f->type = 5;
  678. if (f->type <= 0 || f->type > 5) {
  679. log4cplus_error("Invalid value [%d].field_type:%s",
  680. i, type.c_str());
  681. return -1;
  682. }
  683. f->size = dtc_config["primary"]["cache"]["field"][i]["size"].as<int>();
  684. if (f->size == -1) {
  685. log4cplus_error("field size missing for %d",
  686. i);
  687. return -1;
  688. }
  689. if (i >= keyFieldCnt &&
  690. i < keyFieldCnt + idxFieldCnt) { // index field
  691. if (field[i].size > 255) {
  692. log4cplus_error(
  693. "index field[%s] size must less than 256",
  694. f->name);
  695. return -1;
  696. }
  697. std::string idx_order = dtc_config["primary"]["cache"]["order"].as<std::string>();
  698. if (idx_order == "desc")
  699. f->flags |= DB_FIELD_FLAGS_DESC_ORDER;
  700. else
  701. log4cplus_debug("index field[%s] order: ASC",
  702. f->name);
  703. }
  704. if (field[i].size >= (64 << 20)) {
  705. log4cplus_error("field[%s] size must less than 64M",
  706. f->name);
  707. return -1;
  708. }
  709. if (i >= keyFieldCnt &&
  710. raw->get_int_val(NULL, "ReadOnly", 0) > 0)
  711. f->flags |= DB_FIELD_FLAGS_READONLY;
  712. if(dtc_config["primary"]["cache"]["field"][i]["unique"])
  713. {
  714. if(dtc_config["primary"]["cache"]["field"][i]["unique"].as<int>() > 0)
  715. {
  716. log4cplus_debug("set index: %d unique", i);
  717. f->flags |= DB_FIELD_FLAGS_UNIQ;
  718. }
  719. }
  720. if (raw->get_int_val(NULL, "Volatile", 0) > 0) {
  721. if (i < keyFieldCnt) {
  722. log4cplus_error(
  723. "field%d: key field can't be volatile",
  724. i + 1);
  725. return -1;
  726. }
  727. if ((f->flags & DB_FIELD_FLAGS_UNIQ)) {
  728. log4cplus_error(
  729. "field%d: uniq field can't be volatile",
  730. i + 1);
  731. return -1;
  732. }
  733. f->flags |= DB_FIELD_FLAGS_VOLATILE;
  734. }
  735. if (raw->get_int_val(NULL, "Discard", 0) > 0) {
  736. if (i < keyFieldCnt) {
  737. log4cplus_error(
  738. "field%d: key field can't be discard",
  739. i + 1);
  740. return -1;
  741. }
  742. if ((f->flags & DB_FIELD_FLAGS_UNIQ)) {
  743. log4cplus_error(
  744. "field%d: uniq field can't be discard",
  745. i + 1);
  746. return -1;
  747. }
  748. f->flags |= DB_FIELD_FLAGS_DISCARD |
  749. DB_FIELD_FLAGS_VOLATILE;
  750. }
  751. if (!strcmp(f->name, DTC_EXPIRE_TIME_FIELD)) {
  752. if (f->type != DField::Unsigned &&
  753. f->type != DField::Signed) {
  754. log4cplus_error(
  755. "field%d: expire time field byte must be unsigned",
  756. i + 1);
  757. return -1;
  758. }
  759. if (expireTime >= 0) {
  760. log4cplus_error(
  761. "field%d already defined as expire time",
  762. expireTime + 1);
  763. return -1;
  764. }
  765. expireTime = i;
  766. }
  767. /* ATTN: must be last one */
  768. if(dtc_config["primary"]["cache"]["field"][i]["default"])
  769. {
  770. YAML::Node default_node = dtc_config["primary"]["cache"]["field"][i]["default"];
  771. std::string default_val = default_node.as<std::string>();
  772. if (default_val == "auto_increment") {
  773. if (f->type != DField::Unsigned &&
  774. f->type != DField::Signed) {
  775. log4cplus_error(
  776. "field%d: auto_increment field byte must be unsigned",
  777. i + 1);
  778. return -1;
  779. }
  780. if (autoinc >= 0) {
  781. log4cplus_error(
  782. "field%d already defined as auto_increment",
  783. autoinc + 1);
  784. return -1;
  785. }
  786. if ((f->flags & DB_FIELD_FLAGS_DISCARD)) {
  787. log4cplus_error(
  788. "field%d: auto_increment can't be Discard",
  789. autoinc + 1);
  790. return -1;
  791. }
  792. autoinc = i;
  793. f->flags |= DB_FIELD_FLAGS_READONLY;
  794. } else if (default_val == "lastmod") {
  795. if (i < keyFieldCnt) {
  796. log4cplus_error("key field%d can't be lastmod",
  797. i + 1);
  798. return -1;
  799. }
  800. if ((f->type != DField::Unsigned &&
  801. f->type != DField::Signed) ||
  802. f->size < 4) {
  803. log4cplus_error(
  804. "field%d: lastmod field byte must be unsigned & size>=4",
  805. i + 1);
  806. return -1;
  807. }
  808. if ((f->flags & DB_FIELD_FLAGS_UNIQ)) {
  809. log4cplus_error(
  810. "field%d: lastmod field byte can't be field_unique",
  811. i + 1);
  812. return -1;
  813. }
  814. if ((f->flags & DB_FIELD_FLAGS_DISCARD)) {
  815. log4cplus_error(
  816. "field%d: lastmod can't be Discard",
  817. autoinc + 1);
  818. return -1;
  819. }
  820. if (lastmod >= 0) {
  821. log4cplus_error(
  822. "field%d already defined as lastmod",
  823. lastmod + 1);
  824. return -1;
  825. }
  826. lastmod = i;
  827. } else if (default_val == "lastcmod") {
  828. if (i < keyFieldCnt) {
  829. log4cplus_error("key field%d can't be lastcmod",
  830. i + 1);
  831. return -1;
  832. }
  833. if ((f->type != DField::Unsigned &&
  834. f->type != DField::Signed) ||
  835. f->size < 4) {
  836. log4cplus_error(
  837. "field%d: lastcmod field byte must be unsigned & size>=4",
  838. i + 1);
  839. return -1;
  840. }
  841. if ((f->flags & DB_FIELD_FLAGS_UNIQ)) {
  842. log4cplus_error(
  843. "field%d: lastcmod field byte can't be field_unique",
  844. i + 1);
  845. return -1;
  846. }
  847. if ((f->flags & DB_FIELD_FLAGS_DISCARD)) {
  848. log4cplus_error(
  849. "field%d: lastcmod can't be Discard",
  850. autoinc + 1);
  851. return -1;
  852. }
  853. if (lastcmod >= 0) {
  854. log4cplus_error(
  855. "field%d already defined as lastcmod",
  856. lastcmod + 1);
  857. return -1;
  858. }
  859. lastcmod = i;
  860. } else if (default_val == "lastacc") {
  861. if (i < keyFieldCnt) {
  862. log4cplus_error("key field%d can't be lastacc",
  863. i + 1);
  864. return -1;
  865. }
  866. if ((f->type != DField::Unsigned &&
  867. f->type != DField::Signed) ||
  868. f->size != 4) {
  869. log4cplus_error(
  870. "field%d: lastacc field byte must be unsigned & size==4",
  871. i + 1);
  872. return -1;
  873. }
  874. if ((f->flags & DB_FIELD_FLAGS_UNIQ)) {
  875. log4cplus_error(
  876. "field%d: lastacc field byte can't be field_unique",
  877. i + 1);
  878. return -1;
  879. }
  880. if ((f->flags & DB_FIELD_FLAGS_DISCARD)) {
  881. log4cplus_error(
  882. "field%d: lastacc can't be Discard",
  883. autoinc + 1);
  884. return -1;
  885. }
  886. if (lastacc >= 0) {
  887. log4cplus_error(
  888. "field%d already defined as lastacc",
  889. lastacc + 1);
  890. return -1;
  891. }
  892. lastacc = i;
  893. } else if (default_val == "compressflag") {
  894. if (i < keyFieldCnt) {
  895. log4cplus_error(
  896. "key field%d can't be compressflag",
  897. i + 1);
  898. return -1;
  899. }
  900. if ((f->type != DField::Unsigned &&
  901. f->type != DField::Signed) ||
  902. f->size != 8) {
  903. log4cplus_error(
  904. "field%d: compressflag field byte must be unsigned & size==8",
  905. i + 1);
  906. return -1;
  907. }
  908. if ((f->flags & DB_FIELD_FLAGS_UNIQ)) {
  909. log4cplus_error(
  910. "field%d: compressflag field byte can't be field_unique",
  911. i + 1);
  912. return -1;
  913. }
  914. if ((f->flags & DB_FIELD_FLAGS_DISCARD)) {
  915. log4cplus_error(
  916. "field%d: compressflag can't be Discard",
  917. i + 1);
  918. return -1;
  919. }
  920. if (compressflag >= 0) {
  921. log4cplus_error(
  922. "field%d already defined as compressflag",
  923. i + 1);
  924. return -1;
  925. }
  926. compressflag = i;
  927. } else {
  928. if (i == 0 && default_val.length() > 0) {
  929. log4cplus_error(
  930. "specify DefaultValue for Field1 is invalid");
  931. return -1;
  932. }
  933. switch (f->type) {
  934. case DField::Unsigned:
  935. f->dval.Set(default_node.as<int>());
  936. break;
  937. case DField::Signed:
  938. f->dval.Set(default_node.as<int>());
  939. break;
  940. case DField::Float:
  941. f->dval.Set(default_node.as<float>());
  942. break;
  943. case DField::String:
  944. case DField::Binary:
  945. int len;
  946. cp = default_val.c_str();
  947. if (!cp || !cp[0]) {
  948. f->dval.Set(NULL, 0);
  949. break;
  950. } else if (cp[0] == '"') {
  951. /* Decode quoted string */
  952. cp++;
  953. len = strlen(cp);
  954. if (cp[len - 1] != '"') {
  955. log4cplus_error(
  956. "field%d: unmatched quoted default value",
  957. i + 1);
  958. return -1;
  959. }
  960. len--;
  961. } else if (cp[0] == '0' &&
  962. (cp[1] | 0x20) == 'x') {
  963. /* Decode hex value */
  964. int j = 2;
  965. len = 0;
  966. while (cp[j]) {
  967. char v;
  968. if (cp[j] == '-')
  969. continue;
  970. if (cp[j] >= '0' &&
  971. cp[j] <= '9')
  972. v = cp[j] - '0';
  973. else if (cp[j] >= 'a' &&
  974. cp[j] <= 'f')
  975. v = cp[j] - 'a' + 10;
  976. else if (cp[j] >= 'A' &&
  977. cp[j] <= 'F')
  978. v = cp[j] - 'A' + 10;
  979. else {
  980. log4cplus_error(
  981. "field%d: invalid hex default value",
  982. i + 1);
  983. return -1;
  984. }
  985. j++;
  986. if (cp[j] >= '0' &&
  987. cp[j] <= '9')
  988. v = (v << 4) + cp[j] -
  989. '0';
  990. else if (cp[j] >= 'a' &&
  991. cp[j] <= 'f')
  992. v = (v << 4) + cp[j] -
  993. 'a' + 10;
  994. else if (cp[j] >= 'A' &&
  995. cp[j] <= 'F')
  996. v = (v << 4) + cp[j] -
  997. 'A' + 10;
  998. else {
  999. log4cplus_error(
  1000. "field%d: invalid hex default value",
  1001. i + 1);
  1002. return -1;
  1003. }
  1004. j++;
  1005. //buf[len++] = v;
  1006. len++;
  1007. }
  1008. } else {
  1009. log4cplus_error(
  1010. "field%d: string default value must quoted or hex value",
  1011. i + 1);
  1012. return -1;
  1013. }
  1014. if (len > f->size) {
  1015. log4cplus_error(
  1016. "field%d: default value size %d truncated to %d",
  1017. i + 1, len, f->size);
  1018. return -1;
  1019. }
  1020. if (len == 0)
  1021. f->dval.Set(NULL, 0);
  1022. else {
  1023. char *p = (char *)MALLOC(len + 1);
  1024. memcpy(p, cp, len);
  1025. p[len] = '\0';
  1026. f->dval.Set(p, len);
  1027. }
  1028. break;
  1029. }
  1030. f->flags |= DB_FIELD_FLAGS_HAS_DEFAULT;
  1031. }
  1032. }
  1033. if (dtc_config["primary"]["cache"]["field"][i]["nullable"]) {
  1034. YAML::Node nullable_node = dtc_config["primary"]["cache"]["field"][i]["nullable"];
  1035. int null_val = nullable_node.as<int>();
  1036. if (null_val) {
  1037. if (f->flags & DB_FIELD_FLAGS_UNIQ) {
  1038. log4cplus_error(
  1039. "field%d: can't be null",
  1040. i + 1);
  1041. return -1;
  1042. }
  1043. f->flags |= DB_FIELD_FLAGS_NULLABLE;
  1044. }
  1045. }
  1046. }
  1047. if (field[0].type == DField::Float) {
  1048. log4cplus_error("%s", "FloatPoint key not supported");
  1049. return -1;
  1050. }
  1051. if (((field[0].type == DField::String ||
  1052. field[0].type == DField::Binary) &&
  1053. keyHashConfig.keyHashEnable == 0) ||
  1054. autoinc == 0) {
  1055. if (machineCnt != 1) {
  1056. log4cplus_error(
  1057. "%s",
  1058. "String/Binary/AutoInc key require server_count==1");
  1059. return -1;
  1060. }
  1061. if (database_max_count != 1) {
  1062. log4cplus_error(
  1063. "%s",
  1064. "String/Binary/AutoInc key require database_max_count==1");
  1065. return -1;
  1066. }
  1067. if (depoly != 0) {
  1068. log4cplus_error(
  1069. "%s",
  1070. "String/Binary/AutoInc key require Depoly==0");
  1071. return -1;
  1072. }
  1073. }
  1074. // expire time only support uniq key
  1075. if (expireTime != -1 && !(field[0].flags & DB_FIELD_FLAGS_UNIQ)) {
  1076. log4cplus_error("%s", "expire time only support uniq key");
  1077. return -1;
  1078. }
  1079. if (keyFieldCnt > 1) {
  1080. for (int j = 0; j < keyFieldCnt; j++) {
  1081. struct FieldConfig *f1 = &field[j];
  1082. if (f1->type != DField::Signed &&
  1083. f1->type != DField::Unsigned) {
  1084. log4cplus_error(
  1085. "%s",
  1086. "Only Multi-Integer-Key supported");
  1087. return -1;
  1088. }
  1089. }
  1090. }
  1091. return 0;
  1092. }
  1093. struct DbConfig *DbConfig::load_buffered(char *buf)
  1094. {
  1095. DTCConfig *raw = new DTCConfig();
  1096. if (raw->load_yaml_buffer(buf) < 0) {
  1097. delete raw;
  1098. return NULL;
  1099. }
  1100. DbConfig *dbConfig =
  1101. (struct DbConfig *)calloc(1, sizeof(struct DbConfig));
  1102. dbConfig->cfgObj = raw;
  1103. if (dbConfig->get_dtc_config(raw->get_config_node(), raw, HOT) == -1) {
  1104. log4cplus_error("get config error, destory now.");
  1105. dbConfig->destory();
  1106. dbConfig = NULL;
  1107. }
  1108. return dbConfig;
  1109. }
  1110. struct DbConfig *DbConfig::Load(const char *file)
  1111. {
  1112. DTCConfig *raw = new DTCConfig();
  1113. if (raw->load_yaml_file(file) < 0) {
  1114. delete raw;
  1115. return NULL;
  1116. }
  1117. DbConfig *dbConfig =
  1118. (struct DbConfig *)calloc(1, sizeof(struct DbConfig));
  1119. dbConfig->cfgObj = raw;
  1120. if (dbConfig->get_dtc_config(raw->get_config_node(), raw, HOT) == -1) {
  1121. dbConfig->destory();
  1122. dbConfig = NULL;
  1123. }
  1124. return dbConfig;
  1125. }
  1126. struct DbConfig *DbConfig::Load(DTCConfig *raw , int i_server_type)
  1127. {
  1128. DbConfig *dbConfig =
  1129. (struct DbConfig *)calloc(1, sizeof(struct DbConfig));
  1130. dbConfig->cfgObj = raw;
  1131. if (dbConfig->get_dtc_config(raw->get_config_node(), raw, i_server_type) == -1) {
  1132. dbConfig->destory();
  1133. dbConfig = NULL;
  1134. }
  1135. return dbConfig;
  1136. }
  1137. bool DbConfig::build_path(char *path, int n, int pid, int group, int role,
  1138. int type)
  1139. {
  1140. if (type == DUMMY_HELPER || type == DTC_HELPER)
  1141. return false;
  1142. memset(path, 0, n);
  1143. snprintf(path, n - 1, HELPERPATHFORMAT, pid, group,
  1144. MACHINEROLESTRING[role]);
  1145. return true;
  1146. }
  1147. int DbConfig::get_dtc_mode(YAML::Node dtc_config)
  1148. {
  1149. if(dtc_config["primary"]["hot"])
  1150. return DTC_MODE_DATABASE_ADDITION;
  1151. else
  1152. return DTC_MODE_CACHE_ONLY;
  1153. }
  1154. std::string DbConfig::get_shm_size(YAML::Node dtc_config)
  1155. {
  1156. if(dtc_config["props"]["shm.mem.size"])
  1157. return dtc_config["props"]["shm.mem.size"].as<string>();
  1158. else
  1159. return "0";
  1160. }
  1161. int DbConfig::get_shm_id(YAML::Node dtc_config)
  1162. {
  1163. if(dtc_config["props"]["listener.port.dtc"])
  1164. return dtc_config["props"]["listener.port.dtc"].as<int>();
  1165. else
  1166. return 0;
  1167. }
  1168. std::string DbConfig::get_bind_addr(YAML::Node dtc_config)
  1169. {
  1170. if(dtc_config["props"]["listener.port.dtc"])
  1171. {
  1172. int port = dtc_config["props"]["listener.port.dtc"].as<int>();
  1173. char sz[200] = {0};
  1174. sprintf(sz, "*:%d/tcp", port);
  1175. return std::string(sz, strlen(sz));
  1176. }
  1177. else
  1178. return "";
  1179. }
  1180. void DbConfig::set_helper_path(int serverId)
  1181. {
  1182. //multi DTC datasource
  1183. for (int i = 0; i < machineCnt; ++i) {
  1184. MachineConfig *machine_config = &mach[i];
  1185. for (int j = 0; j < ROLES_PER_MACHINE;
  1186. ++j) //#define ROLES_PER_MACHINE 2
  1187. {
  1188. //check dtc datasource
  1189. switch (machine_config->helperType) {
  1190. case DUMMY_HELPER:
  1191. break;
  1192. case DTC_HELPER:
  1193. snprintf(machine_config->role[j].path,
  1194. sizeof(machine_config->role[j].path) -
  1195. 1,
  1196. "%s", machine_config->role[j].addr);
  1197. log4cplus_info(
  1198. "build helper path, serverId:%d, addr:%s, path:%s, dstype:%d",
  1199. serverId, machine_config->role[j].addr,
  1200. machine_config->role[j].path, dstype);
  1201. break;
  1202. default:
  1203. snprintf(machine_config->role[j].path,
  1204. sizeof(machine_config->role[j].path) -
  1205. 1,
  1206. HELPERPATHFORMAT, serverId, i,
  1207. MACHINEROLESTRING[j]);
  1208. log4cplus_info(
  1209. "build helper path, serverId:%d, addr:%s, path:%s, dstype:%d",
  1210. serverId, machine_config->role[j].addr,
  1211. machine_config->role[j].path, dstype);
  1212. }
  1213. // rocksdb only start one process
  1214. if (dstype == 2)
  1215. break;
  1216. }
  1217. }
  1218. }
  1219. void DbConfig::destory(void)
  1220. {
  1221. if (this == NULL)
  1222. return;
  1223. // for (int i = 0; i < GROUPS_PER_MACHINE; i++) {
  1224. // FREE_IF(mach->role[i].addr);
  1225. // FREE_IF(mach->role[i].user);
  1226. // FREE_IF(mach->role[i].pass);
  1227. // FREE_IF(mach->role[i].optfile);
  1228. // FREE_IF(mach->role[i].dm);
  1229. // }
  1230. // machine hasn't dynamic objects
  1231. FREE_IF(mach);
  1232. if (field) {
  1233. for (int i = 0; i < fieldCnt; i++) {
  1234. switch (field[i].type) {
  1235. case DField::String:
  1236. case DField::Binary:
  1237. if (field[i].dval.str.ptr)
  1238. FREE(field[i].dval.str.ptr);
  1239. }
  1240. }
  1241. FREE(field);
  1242. }
  1243. if (dbFormat != dbName)
  1244. FREE_IF(dbFormat);
  1245. FREE_IF(dbName);
  1246. if (tblFormat != tblName)
  1247. FREE_IF(tblFormat);
  1248. FREE_IF(tblName);
  1249. FREE_IF(ordSql);
  1250. DELETE(cfgObj);
  1251. FREE((void *)this);
  1252. }
  1253. void DbConfig::dump_db_config(const struct DbConfig *cf)
  1254. {
  1255. int i, j;
  1256. printf("database_name: %s\n", cf->dbName);
  1257. printf("database_number: (%d,%d)\n", cf->dbDiv, cf->dbMod);
  1258. printf("server_count: %d\n", cf->machineCnt);
  1259. for (i = 0; i < cf->machineCnt; i++) {
  1260. struct MachineConfig *mach = &cf->mach[i];
  1261. printf("\n");
  1262. printf("Machine[%d].addr: %s\n", i, mach->role[0].addr);
  1263. printf("Machine[%d].user: %s\n", i, mach->role[0].user);
  1264. printf("Machine[%d].pass: %s\n", i, mach->role[0].pass);
  1265. printf("Machine[%d].procs: %d\n", i, mach->procs);
  1266. printf("Machine[%d].dbCnt: %d\n", i, mach->dbCnt);
  1267. printf("Machine[%d].dbIdx: ", i);
  1268. for (j = 0; j < mach->dbCnt; j++)
  1269. printf("%d ", mach->dbIdx[j]);
  1270. printf("\n");
  1271. }
  1272. printf("\nTableName: %s\n", cf->tblName);
  1273. printf("TableNum: (%d,%d)\n", cf->tblDiv, cf->tblMod);
  1274. printf("field_count: %d key %d\n", cf->fieldCnt, cf->keyFieldCnt);
  1275. for (i = 0; i < cf->fieldCnt; i++)
  1276. printf("Field[%d].name: %s, size: %d, type: %d\n", i,
  1277. cf->field[i].name, cf->field[i].size, cf->field[i].type);
  1278. }