da_server.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_SERVER_H_
  17. #define DA_SERVER_H_
  18. #include "da_array.h"
  19. #include "da_conn.h"
  20. #include "da_string.h"
  21. #include <stddef.h>
  22. #include <stdint.h>
  23. typedef uint32_t (*hash_t)(const char *, size_t);
  24. #define ERROR_UPPER_LIMIT 1
  25. #define FAIL_TIME_LIMIT 6
  26. struct continuum {
  27. uint32_t index; /* server index */
  28. uint32_t value; /* hash value */
  29. };
  30. struct cache_instance {
  31. struct string pname;
  32. void *owner;
  33. int nerr;
  34. int idx;
  35. uint16_t port; /* port */
  36. uint32_t weight; /* weight */
  37. int family; /* socket family */
  38. socklen_t addrlen; /* socket length */
  39. struct sockaddr *addr; /* socket address (ref in conf_server) */
  40. uint32_t ns_conn_q; /* # server connection */
  41. struct conn_tqh s_conn_q; /*server connection*/
  42. uint64_t last_failure_ms; /*cahche the failure time*/
  43. uint16_t failure_num; /*cache failure time*/
  44. int num;
  45. };
  46. struct server {
  47. uint32_t idx; /* server index */
  48. struct server_pool *owner; /* owner pool */
  49. uint16_t high_prty_idx;
  50. uint16_t high_prty_cnt;
  51. uint16_t low_prty_idx;
  52. uint16_t low_prty_cnt;
  53. struct string name; /* name (ref in conf_server) */
  54. int weight;
  55. int replica_enable;
  56. struct cache_instance *master;
  57. struct array high_ptry_ins;
  58. struct array low_prty_ins;
  59. };
  60. struct server_pool {
  61. uint32_t idx; /* pool index */
  62. uint32_t mid; /* pool manager id*/
  63. struct context *ctx; /* owner context */
  64. struct conn *listener;
  65. struct conn_tqh c_conn_q; /*client connection*/
  66. uint32_t c_conn_count; /* number of client connection */
  67. struct array server; /* server[] */
  68. uint32_t ncontinuum; /* # continuum points */
  69. uint32_t
  70. nserver_continuum; /* # servers - live and dead on continuum (const) */
  71. struct continuum *continuum; /* continuum */
  72. struct string name; /* pool name (ref in conf_pool) */
  73. struct string addrstr; /* pool address (ref in conf_pool) */
  74. struct string accesskey; /* access token for this pool */
  75. uint16_t port; /* port */
  76. int family; /* socket family */
  77. socklen_t addrlen; /* socket length */
  78. struct sockaddr *addr; /* socket address (ref in conf_pool) */
  79. int key_hash_type; /* key hash type (hash_type_t) */
  80. hash_t key_hash; /* key hasher */
  81. int backlog; /* listen backlog */
  82. int timeout; /* timeout in msec */
  83. uint32_t client_connections; /* maximum # client connection */
  84. uint32_t server_connections; /* maximum # server connection */
  85. int replica_enable; /*Replica enable for whole system*/
  86. struct string module_idc; /*sp IDC*/
  87. unsigned preconnect : 1; /* preconnect? */
  88. int main_report;
  89. int instance_report;
  90. int auto_remove_replica;
  91. int top_percentile_enable;
  92. int top_percentile_fd;
  93. struct sockaddr_in top_percentile_addr;
  94. int top_percentile_addr_len;
  95. struct remote_param *top_percentile_param;
  96. };
  97. uint32_t server_pool_idx(struct server_pool *pool, uint8_t *key,
  98. uint32_t keylen);
  99. void server_close(struct context *ctx, struct conn *conn);
  100. void instance_ref(struct conn *conn, void *owner);
  101. void instance_unref(struct conn *conn);
  102. int server_active(struct conn *conn);
  103. int server_init(struct array *server, struct array *conf_server,
  104. struct server_pool *sp);
  105. void server_connected(struct context *ctx, struct conn *conn);
  106. int server_init(struct array *server, struct array *conf_server,
  107. struct server_pool *sp);
  108. void instance_deinit(struct array *server);
  109. void server_deinit(struct array *server);
  110. int server_pool_preconnect(struct context *ctx);
  111. void server_pool_disconnect(struct context *ctx);
  112. int server_pool_run(struct server_pool *pool);
  113. void server_pool_deinit(struct array *server_pool);
  114. int server_pool_init(struct array *server_pool, struct array *conf_pool,
  115. struct context *ctx);
  116. int server_timeout(struct conn *conn);
  117. struct conn *server_pool_conn(struct context *ctx, struct server_pool *pool,
  118. struct msg *msg);
  119. int incr_instance_failure_time(struct cache_instance *ci);
  120. int decr_instance_failure_time(struct msg *msg);
  121. #endif /* DA_SERVER_H_ */