关键词搜索

源码搜索 ×
×

漫话Redis源码之二十二

发布2021-12-05浏览561次

详情内容

这里主要是校验,确定配置文件是否被设置:

  1. /* This function is for checking whether sentinel config file has been set,
  2. * also checking whether we have write permissions. */
  3. void sentinelCheckConfigFile(void) {
  4. if (server.configfile == NULL) {
  5. serverLog(LL_WARNING,
  6. "Sentinel needs config file on disk to save state. Exiting...");
  7. exit(1);
  8. } else if (access(server.configfile,W_OK) == -1) {
  9. serverLog(LL_WARNING,
  10. "Sentinel config file %s is not writable: %s. Exiting...",
  11. server.configfile,strerror(errno));
  12. exit(1);
  13. }
  14. }
  15. /* This function gets called when the server is in Sentinel mode, started,
  16. * loaded the configuration, and is ready for normal operations. */
  17. void sentinelIsRunning(void) {
  18. int j;
  19. /* If this Sentinel has yet no ID set in the configuration file, we
  20. * pick a random one and persist the config on disk. From now on this
  21. * will be this Sentinel ID across restarts. */
  22. for (j = 0; j < CONFIG_RUN_ID_SIZE; j++)
  23. if (sentinel.myid[j] != 0) break;
  24. if (j == CONFIG_RUN_ID_SIZE) {
  25. /* Pick ID and persist the config. */
  26. getRandomHexChars(sentinel.myid,CONFIG_RUN_ID_SIZE);
  27. sentinelFlushConfig();
  28. }
  29. /* Log its ID to make debugging of issues simpler. */
  30. serverLog(LL_WARNING,"Sentinel ID is %s", sentinel.myid);
  31. /* We want to generate a +monitor event for every configured master
  32. * at startup. */
  33. sentinelGenerateInitialMonitorEvents();
  34. }
  35. /* ============================== sentinelAddr ============================== */
  36. /* Create a sentinelAddr object and return it on success.
  37. * On error NULL is returned and errno is set to:
  38. * ENOENT: Can't resolve the hostname.
  39. * EINVAL: Invalid port number.
  40. */
  41. sentinelAddr *createSentinelAddr(char *hostname, int port) {
  42. char ip[NET_IP_STR_LEN];
  43. sentinelAddr *sa;
  44. if (port < 0 || port > 65535) {
  45. errno = EINVAL;
  46. return NULL;
  47. }
  48. if (anetResolve(NULL,hostname,ip,sizeof(ip),
  49. sentinel.resolve_hostnames ? ANET_NONE : ANET_IP_ONLY) == ANET_ERR) {
  50. errno = ENOENT;
  51. return NULL;
  52. }
  53. sa = zmalloc(sizeof(*sa));
  54. sa->hostname = sdsnew(hostname);
  55. sa->ip = sdsnew(ip);
  56. sa->port = port;
  57. return sa;
  58. }
  59. /* Return a duplicate of the source address. */
  60. sentinelAddr *dupSentinelAddr(sentinelAddr *src) {
  61. sentinelAddr *sa;
  62. sa = zmalloc(sizeof(*sa));
  63. sa->hostname = sdsnew(src->hostname);
  64. sa->ip = sdsnew(src->ip);
  65. sa->port = src->port;
  66. return sa;
  67. }
  68. /* Free a Sentinel address. Can't fail. */
  69. void releaseSentinelAddr(sentinelAddr *sa) {
  70. sdsfree(sa->hostname);
  71. sdsfree(sa->ip);
  72. zfree(sa);
  73. }
  74. /* Return non-zero if two addresses are equal. */
  75. int sentinelAddrIsEqual(sentinelAddr *a, sentinelAddr *b) {
  76. return a->port == b->port && !strcasecmp(a->ip,b->ip);
  77. }
  78. /* Return non-zero if a hostname matches an address. */
  79. int sentinelAddrEqualsHostname(sentinelAddr *a, char *hostname) {
  80. char ip[NET_IP_STR_LEN];
  81. /* We always resolve the hostname and compare it to the address */
  82. if (anetResolve(NULL, hostname, ip, sizeof(ip),
  83. sentinel.resolve_hostnames ? ANET_NONE : ANET_IP_ONLY) == ANET_ERR)
  84. return 0;
  85. return !strcasecmp(a->ip, ip);
  86. }
  87. const char *announceSentinelAddr(const sentinelAddr *a) {
  88. return sentinel.announce_hostnames ? a->hostname : a->ip;
  89. }
  90. /* Return an allocated sds with hostname/address:port. IPv6
  91. * addresses are bracketed the same way anetFormatAddr() does.
  92. */
  93. sds announceSentinelAddrAndPort(const sentinelAddr *a) {
  94. const char *addr = announceSentinelAddr(a);
  95. if (strchr(addr, ':') != NULL)
  96. return sdscatprintf(sdsempty(), "[%s]:%d", addr, a->port);
  97. else
  98. return sdscatprintf(sdsempty(), "%s:%d", addr, a->port);
  99. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载