comm.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/file.h>
  5. #include <fcntl.h>
  6. #include <getopt.h>
  7. #include <string>
  8. // local
  9. #include "comm.h"
  10. #include "hwc_global.h"
  11. #include "async_file.h"
  12. // common
  13. #include "log/log.h"
  14. #include "config/dbconfig.h"
  15. DTC::Server CComm::master;
  16. CRegistor CComm::registor;
  17. ConnectorProcess CComm::mysql_process_;
  18. const char *CComm::version = "hwc.0.1";
  19. char* CComm::dtc_conf = "/etc/dtc/dtc.yaml";
  20. char* CComm::table_conf = "/etc/dtc/dtc.yaml";
  21. int CComm::backend = 0;
  22. int CComm::normal = 1;
  23. void CComm::show_usage(int argc, char **argv)
  24. {
  25. fprintf(stderr, "Usage: %s [OPTION]...\n"
  26. "Sync DTC/Bitmap master data to DTC/bitmap slave.\n\n"
  27. "\t -n, --normal normal running mode.\n"
  28. "\t -b, --backend runing in background.\n"
  29. "\t -v, --version show version.\n"
  30. "\t -d, --dtc_conf dtc config file path.\n"
  31. "\t -t, --table_conf table config file path.\n"
  32. "\t -h, --help display this help and exit.\n\n", argv[0]);
  33. return;
  34. }
  35. void CComm::show_version(int argc, char **argv)
  36. {
  37. fprintf(stderr, "%s(%s)\n", argv[0], version);
  38. return;
  39. }
  40. void CComm::parse_argv(int argc, char **argv)
  41. {
  42. if (argc < 2) {
  43. show_usage(argc, argv);
  44. exit(-1);
  45. }
  46. int option_index = 0, c = 0;
  47. static struct option long_options[] = {
  48. {"normal", 0, 0, 'n'},
  49. {"backend", 0, 0, 'b'},
  50. {"version", 0, 0, 'v'},
  51. {"table_conf", 0, 0, 't'},
  52. {"help", 0, 0, 'h'},
  53. {"dtc_conf", 1, 0, 'd'},
  54. {0, 0, 0, 0},
  55. };
  56. while ((c =
  57. getopt_long(argc, argv, "nbvt:hd:", long_options,
  58. &option_index)) != -1) {
  59. switch (c) {
  60. case 'n':
  61. normal = 1;
  62. break;
  63. case 'b':
  64. backend = 1;
  65. break;;
  66. case 'v':
  67. show_version(argc, argv);
  68. exit(0);
  69. break;
  70. case 'h':
  71. show_usage(argc, argv);
  72. exit(0);
  73. break;
  74. case 't':
  75. if (optarg)
  76. {
  77. table_conf = optarg;
  78. }
  79. break;
  80. case 'd':
  81. if (optarg)
  82. {
  83. dtc_conf = optarg;
  84. }
  85. break;
  86. case '?':
  87. default:
  88. show_usage(argc, argv);
  89. exit(-1);
  90. }
  91. }
  92. if (optind < argc) {
  93. show_usage(argc, argv);
  94. exit(-1);
  95. }
  96. return;
  97. }
  98. int CComm::ReInitDtcAgency(DbConfig* pParser)
  99. {
  100. if (CComm::connect_ttc_server(1, pParser)) {
  101. return -1;
  102. }
  103. CComm::registor.SetMasterServer(&CComm::master);
  104. return 0;
  105. }
  106. int CComm::connect_ttc_server(
  107. int ping_master,
  108. DbConfig* pParser)
  109. {
  110. log4cplus_warning("try to ping master server");
  111. char* p_bind_addr = pParser->get_bind_addr(pParser->cfgObj->get_config_node()).c_str();
  112. if (strlen(p_bind_addr)) {
  113. return -1;
  114. }
  115. log4cplus_debug("master:%s.", p_bind_addr);
  116. if(ping_master) {
  117. int ret = master.SetAddress(p_bind_addr);
  118. master.SetTimeout(30);
  119. master.IntKey();
  120. master.SetTableName(pParser->tblFormat);
  121. master.SetAutoUpdateTab(false);
  122. if (-DTC::EC_BAD_HOST_STRING == ret
  123. || (ret = master.Ping()) != 0) {
  124. log4cplus_error("ping master[%s] failed, err:%d", p_bind_addr, ret);
  125. return -1;
  126. }
  127. log4cplus_warning("ping master[%s] success", p_bind_addr);
  128. }
  129. return 0;
  130. }
  131. int CComm::load_config(const char *p)
  132. {
  133. const char *f = strdup(p);
  134. // if (config.ParseConfig(f, "SYSTEM")) {
  135. // fprintf(stderr, "parse config %s failed\n", f);
  136. // return -1;
  137. // }
  138. return 0;
  139. }
  140. int CComm::check_hb_status()
  141. {
  142. CAsyncFileChecker checker;
  143. if (checker.Check()) {
  144. log4cplus_error("check hb status, __NOT__ pass! errmsg: %s, try use --fixed parament to start",
  145. checker.ErrorMessage());
  146. return -1;
  147. }
  148. log4cplus_warning("check hb status, passed");
  149. return 0;
  150. }
  151. int CComm::fixed_hb_env()
  152. {
  153. /* FIXME: 简单删除,后续再考虑如何恢复 */
  154. if (system("cd ../bin/ && ./hb_fixed_env.sh hbp")) {
  155. log4cplus_error("invoke hb_fixed_env.sh hbp failed, %m");
  156. return -1;
  157. }
  158. log4cplus_warning("fixed hb env, passed");
  159. return 0;
  160. }
  161. int CComm::fixed_slave_env()
  162. {
  163. if (system("cd ../bin/ && ./hb_fixed_env.sh slave")) {
  164. log4cplus_error("invoke hb_fixed_env.sh slave failed, %m");
  165. return -1;
  166. }
  167. log4cplus_warning("fixed slave env, passed");
  168. return 0;
  169. }
  170. /* 确保hbp唯一, 锁住hbp的控制文件目录 */
  171. int CComm::uniq_lock(const char *p)
  172. {
  173. if (access(p, F_OK | X_OK))
  174. mkdir(p, 0777);
  175. int fd = open(p, O_RDONLY);
  176. if (fd < 0)
  177. return -1;
  178. fcntl(fd, F_SETFD, FD_CLOEXEC);
  179. return flock(fd, LOCK_EX | LOCK_NB);
  180. }