run.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. */
  17. #include <signal.h>
  18. #include "daemons.h"
  19. #include "main_entry.h"
  20. #include "stattool.h"
  21. #include "hwc.h"
  22. #include "listener/listener.h"
  23. #include "helper.h"
  24. #include "logger.h"
  25. #include "config/dbconfig.h"
  26. #include "log/log.h"
  27. #include "daemon/daemon.h"
  28. #include "../core/global.h"
  29. /* 打开看门狗 */
  30. int start_dtc(int (*entry)(void *), void *args)
  31. {
  32. int delay = 5;
  33. dbConfig->set_helper_path(getpid());
  34. WatchDog *wdog = NULL;
  35. WatchDogListener *srv = NULL;
  36. if (g_dtc_config->get_int_val("cache", "DisableWatchDog", 1) == 0) {
  37. signal(SIGCHLD, SIG_DFL);
  38. delay = g_dtc_config->get_int_val("cache", "WatchDogTime", 30);
  39. if (delay < 5)
  40. delay = 5;
  41. wdog = new WatchDog;
  42. srv = new WatchDogListener(wdog, delay);
  43. srv->attach_watch_dog();
  44. start_fault_logger(wdog);
  45. if (g_dtc_config->get_int_val("cache", "StartStatReporter", 0) >
  46. 0) {
  47. WatchDogStatTool *stat_tool =
  48. new WatchDogStatTool(wdog, delay);
  49. if (stat_tool->new_proc_fork() < 0)
  50. /* cann't fork reporter */
  51. return -1;
  52. log4cplus_info("fork stat reporter");
  53. }
  54. }
  55. if (DbConfig::get_dtc_mode(g_dtc_config->get_config_node()) == DTC_MODE_DATABASE_ADDITION) {
  56. int nh = 0;
  57. /* starting master helper */
  58. for (int g = 0; g < dbConfig->machineCnt; ++g) {
  59. for (int r = 0; r < ROLES_PER_MACHINE; ++r) {
  60. HELPERTYPE t = dbConfig->mach[g].helperType;
  61. log4cplus_debug("helper type = %d", t);
  62. /* check helper type is dtc */
  63. if (DTC_HELPER >= t)
  64. break;
  65. int i, n = 0;
  66. for (i = 0; i < GROUPS_PER_ROLE &&
  67. (r * GROUPS_PER_ROLE + i) <
  68. GROUPS_PER_MACHINE;
  69. i++) {
  70. n += dbConfig->mach[g].gprocs
  71. [r * GROUPS_PER_ROLE + i];
  72. }
  73. if (n <= 0)
  74. continue;
  75. WatchDogHelper *h = NULL;
  76. NEW(WatchDogHelper(
  77. wdog, delay,
  78. dbConfig->mach[g].role[r].path, g,
  79. r, n + 1, t),
  80. h);
  81. if (NULL == h) {
  82. log4cplus_error(
  83. "create WatchDogHelper object failed, msg:%m");
  84. return -1;
  85. }
  86. if (h->new_proc_fork() < 0 || h->verify() < 0)
  87. return -1;
  88. nh++;
  89. }
  90. }
  91. log4cplus_info("fork %d helper groups", nh);
  92. }
  93. if (wdog) {
  94. const int recovery = g_dtc_config->get_idx_val(
  95. "cache", "ServerRecovery",
  96. ((const char *const[]){ "none", "crash", "crashdebug",
  97. "killed", "error", "always",
  98. NULL }),
  99. 2);
  100. MainEntry *dtc =
  101. new MainEntry(wdog, entry, args, recovery);
  102. if (dtc->fork_main() < 0)
  103. return -1;
  104. // wait for dtc entry step to agentlisten
  105. usleep(100 * 1000);
  106. if (g_dtc_config->get_config_node()["primary"]["hot"]) { // open hwcserver at DATABASE_IN_ADDITION mode.
  107. WatchDogHWC* p_hwc_wd = new WatchDogHWC(wdog, delay);
  108. if (p_hwc_wd->new_proc_fork() < 0) {
  109. log4cplus_error("fork hwc server fail");
  110. return -1;
  111. }
  112. log4cplus_info ("fork hwc server");
  113. }
  114. wdog->run_loop();
  115. exit(0);
  116. }
  117. return 0;
  118. }