da_conf.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #ifndef DA_CONF_H_
  17. #define DA_CONF_H_
  18. #include "da_hashkit.h"
  19. #include "da_string.h"
  20. #include "mxml.h"
  21. #define CONF_ERROR (void *)"has an invalid value"
  22. #define CONF_OK (void *)NULL
  23. #define CONF_UNSET_HASH (hash_type_t) - 1
  24. #define CONF_UNSET_PTR NULL
  25. #define CONF_UNSET_NUM -1
  26. #define CONF_DEFAULT_SERVERS 8
  27. #define CONF_DEFAULT_POOL 8
  28. #define CONF_DEFAULT_INSTANCE 8
  29. #define CONF_DEFAULT_HASH HASH_CHASH
  30. #define CONF_DEFAULT_TIMEOUT -1
  31. #define CONF_DEFAULT_LISTEN_BACKLOG 512
  32. #define CONF_DEFAULT_CLIENT_CONNECTIONS 0
  33. #define CONF_DEFAULT_REDIS false
  34. #define CONF_DEFAULT_PRECONNECT false
  35. #define CONF_DEFAULT_AUTO_EJECT_HOSTS false
  36. #define CONF_DEFAULT_SERVER_RETRY_TIMEOUT 30 * 1000 /* in msec */
  37. #define CONF_DEFAULT_SERVER_FAILURE_LIMIT 2
  38. #define CONF_DEFAULT_SERVER_CONNECTIONS 1
  39. #define CONF_DEFAULT_TOP_PERCENTILE_ENABLE 1
  40. #define CONF_DEFAULT_TOP_PERCENTILE_DOMAIN "127.0.0.1"
  41. #define CONF_DEFAULT_TOP_PERCENTILE_PORT 20020
  42. #define CONF_DEFAULT_LOG_SWITCH 0 /*1:on, 0:off*/
  43. #define CONF_DEFAULT_REMOTE_LOG_SWITCH 1 /*1:on, 0:off*/
  44. #define CONF_DEFAULT_REMOTE_LOG_IP "127.0.0.1"
  45. #define CONF_DEFAULT_REMOTE_LOG_PORT 9997
  46. struct conf_listen {
  47. struct string pname; /* listen: as "name:port" */
  48. struct string name; /* name */
  49. int port; /* port */
  50. struct sockinfo info; /* listen socket info */
  51. unsigned valid : 1; /* valid? */
  52. };
  53. struct conf_server {
  54. struct string name; /* name */
  55. struct array instance; /* instance */
  56. int replica_enable;
  57. struct string idc;
  58. unsigned valid : 1; /* valid? */
  59. };
  60. struct conf_instance {
  61. struct string addr; /* server: as "name:port:weight" */
  62. int port; /* port */
  63. int weight; /* weight */
  64. struct sockinfo info; /* connect socket info */
  65. struct string idc; /* idc */
  66. struct string role; /* role */
  67. unsigned valid : 1; /* valid? */
  68. int enable; /* Enable */
  69. };
  70. struct conf_pool {
  71. int mid;
  72. struct string name; /* pool name (root node) */
  73. struct string accesskey;
  74. struct conf_listen listen; /* listen: */
  75. hash_type_t hash; /* hash: */
  76. int timeout; /* timeout: */
  77. int backlog; /* backlog: */
  78. int client_connections; /* client_connections: */
  79. int preconnect; /* preconnect: */
  80. int server_connections; /* server_connections: */
  81. struct array server; /* servers: conf_server[] */
  82. struct string idc; /* idc*/
  83. int replica_enable; /* ReplicaEnable*/
  84. int main_report;
  85. int instance_report;
  86. int auto_remove_replica;
  87. int top_percentile_enable; /*tp99 性能指标开启状态*/
  88. struct string top_percentile_domain; /*tp99 性能指标上报服务器地址*/
  89. int top_percentile_port; /*tp99 性能指标上报服务器端口*/
  90. char localip[16]; /*本地IP,放在此位置*/
  91. unsigned valid : 1; /* valid? */
  92. };
  93. struct conf_log {
  94. int log_switch;
  95. int remote_log_switch;
  96. struct string remote_log_ip;
  97. int remote_log_port;
  98. };
  99. struct conf {
  100. char *fname; /* file name (ref in argv[]) */
  101. FILE *fh; /* file handle */
  102. struct string arg; /* string[] (parsed {key, value} pairs) */
  103. struct array pool; /* conf_pool[] (parsed pools) */
  104. mxml_node_t *tree;
  105. char localip[16];
  106. struct conf_log stCL;
  107. unsigned parsed : 1; /* parsed? */
  108. unsigned valid : 1; /* valid? */
  109. };
  110. struct command {
  111. struct string name;
  112. char *(*set)(struct conf *cf, struct command *cmd, void *data);
  113. int offset;
  114. };
  115. #define null_command \
  116. { \
  117. null_string, NULL, 0 \
  118. }
  119. int conf_server_each_transform(void *elem, void *data);
  120. int conf_pool_each_transform(void *elem, void *data);
  121. struct conf *conf_create(char *filename);
  122. char *conf_set_string(struct conf *cf, struct command *cmd, void *conf);
  123. char *conf_add_server(struct conf *cf, struct command *cmd, void *conf);
  124. char *conf_set_bool(struct conf *cf, struct command *cmd, void *conf);
  125. char *conf_set_num(struct conf *cf, struct command *cmd, void *conf);
  126. char *conf_set_hash(struct conf *cf, struct command *cmd, void *conf);
  127. char *conf_set_listen(struct conf *cf, struct command *cmd, void *conf);
  128. char *conf_set_addr(struct conf *cf, struct command *cmd, void *conf);
  129. void conf_destroy(struct conf *cf);
  130. #endif /* DA_CONF_H_ */