app_client_set.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "app_client_set.h"
  18. #include "config_center_client.h"
  19. #include "ca_quick_find.h"
  20. #include "app_shm_manager.h"
  21. typedef struct handle_ptr {
  22. uint64_t version;
  23. IP_NODE *master_app_set;
  24. IP_NODE *slave_app_set;
  25. FORWARD_ITEM *forward_ptr;
  26. } HANDLE_PTR;
  27. int print_forward(FORWARD_ITEM *header_ptr)
  28. {
  29. if (!header_ptr)
  30. return -NULL_PTR;
  31. printf("lock=[%d]\n", header_ptr->lock);
  32. printf("shm_key=[%d]\n", header_ptr->shm_key);
  33. printf("shm_size=[%d]\n", header_ptr->shm_size);
  34. printf("time_stamp=[%lu]\n", header_ptr->time_stamp);
  35. printf("bid_size=[%d]\n", header_ptr->bid_size);
  36. printf("node_size=[%d]\n", header_ptr->node_size);
  37. printf("forward_shm_id=[%d]\n", header_ptr->forward_shm_id);
  38. printf("master_shm_id=[%d]\n", header_ptr->master_shm_id);
  39. printf("slave_shm_id=[%d]\n", header_ptr->slave_shm_id);
  40. int i = 0;
  41. for (; i < header_ptr->bid_size; ++i) {
  42. printf("count=[%d] bid=[%d] size=[%d] offset=[%d]\n", i,
  43. header_ptr->headers[i].bid, header_ptr->headers[i].size,
  44. header_ptr->headers[i].offset);
  45. }
  46. return 0;
  47. }
  48. int print_app_set(IP_NODE *app_set_ptr, int size)
  49. {
  50. if (!app_set_ptr)
  51. return -NULL_PTR;
  52. int i = 0;
  53. for (; i < size; ++i) {
  54. printf("bid=%dip=%sport=%dstatus=%dweight=%d\n",
  55. app_set_ptr->bid, app_set_ptr->ip, app_set_ptr->port,
  56. app_set_ptr->status, app_set_ptr->weight);
  57. app_set_ptr += 1;
  58. }
  59. return 0;
  60. }
  61. int load_header(HANDLE_PTR *ca_handler)
  62. {
  63. if (!ca_handler)
  64. return -NULL_PTR;
  65. int exist = 1;
  66. int shm_id = 0;
  67. ca_handler->forward_ptr =
  68. (FORWARD_ITEM *)get_shm(FORWARD_SHM_KEY,
  69. sizeof(FORWARD_ITEM) * 2, 0644, &shm_id,
  70. false, &exist);
  71. if (!ca_handler->forward_ptr)
  72. return -GET_SHM_ERR;
  73. ca_handler->version = ca_handler->forward_ptr->time_stamp;
  74. return 0;
  75. }
  76. int load_app_set(HANDLE_PTR *ca_handler)
  77. {
  78. if (!ca_handler)
  79. return -NULL_PTR;
  80. int exist = 1;
  81. int shm_id = 0;
  82. FORWARD_ITEM *master_forward = ca_handler->forward_ptr;
  83. FORWARD_ITEM *slave_forward = ca_handler->forward_ptr + 1;
  84. ca_handler->master_app_set =
  85. (IP_NODE *)get_shm(MASTER_SHM_KEY, master_forward->shm_size,
  86. 0644, &shm_id, false, &exist);
  87. ca_handler->slave_app_set =
  88. (IP_NODE *)get_shm(SLAVE_SHM_KEY, slave_forward->shm_size, 0644,
  89. &shm_id, false, &exist);
  90. if (!ca_handler->master_app_set || !ca_handler->slave_app_set)
  91. return -GET_SHM_ERR;
  92. return 0;
  93. }
  94. int init_shm(HANDLE_PTR *ca_handler)
  95. {
  96. if (!ca_handler)
  97. return -NULL_PTR;
  98. memset(ca_handler, 0, sizeof(HANDLE_PTR));
  99. int ret = load_header(ca_handler);
  100. if (ret < 0)
  101. return ret;
  102. ret = load_app_set(ca_handler);
  103. return ret;
  104. }
  105. int get_version(uint64_t *version)
  106. {
  107. HANDLE_PTR ca_handler;
  108. int ret = load_header(&ca_handler);
  109. if (ret < 0)
  110. return ret;
  111. *version = ca_handler.version;
  112. return detach_shm(ca_handler.forward_ptr);
  113. }
  114. int dump_ca_shm()
  115. {
  116. HANDLE_PTR client;
  117. int ret = init_shm(&client);
  118. if (ret < 0)
  119. return ret;
  120. {
  121. if (!client.forward_ptr->lock) {
  122. printf("\n======================= master ====================\n");
  123. print_forward(client.forward_ptr);
  124. printf("\n======================= master app set ====================\n");
  125. int size = client.forward_ptr->node_size;
  126. print_app_set(client.master_app_set, size);
  127. } else {
  128. printf("\n======================= slave ====================\n");
  129. print_forward(client.forward_ptr + 1);
  130. printf("\n======================= slave app set ====================\n");
  131. int size = client.forward_ptr->node_size;
  132. print_app_set(client.slave_app_set, size);
  133. }
  134. }
  135. return 0;
  136. }
  137. int detach_handler(HANDLE_PTR *ca_handler)
  138. {
  139. int ret1 = detach_shm(ca_handler->forward_ptr);
  140. int ret2 = detach_shm(ca_handler->master_app_set);
  141. int ret3 = detach_shm(ca_handler->slave_app_set);
  142. if (ret1 < 0 || ret2 < 0 || ret3 < 0)
  143. return -DETACH_SHM_ERR;
  144. return 0;
  145. }
  146. int get_ip_route(int bid, IP_ROUTE *ip_route)
  147. {
  148. if (bid < 0 || ip_route == NULL)
  149. return PARAM_ERR;
  150. HANDLE_PTR ca_handler;
  151. int ret = init_shm(&ca_handler);
  152. if (ret < 0) {
  153. detach_handler(&ca_handler);
  154. return ret;
  155. }
  156. FORWARD_ITEM *master_forward = ca_handler.forward_ptr;
  157. FORWARD_ITEM *slave_forward = ca_handler.forward_ptr + 1;
  158. IP_NODE *master_app_set = ca_handler.master_app_set;
  159. IP_NODE *slave_app_set = ca_handler.slave_app_set;
  160. if (!master_forward->lock) {
  161. // printf("master!\n");
  162. int pos = binary_search_header(master_forward->headers, 0,
  163. master_forward->bid_size, bid);
  164. if (pos < 0) {
  165. detach_handler(&ca_handler);
  166. return -NOT_FIND_BID;
  167. }
  168. if (master_forward->headers[pos].offset < 0) {
  169. detach_handler(&ca_handler);
  170. return -OFFSET_ERR;
  171. }
  172. ip_route->ip_num = master_forward->headers[pos].size;
  173. if (ip_route->ip_num <= 0)
  174. return -IP_NUM_ERR;
  175. ip_route->ip_list =
  176. (IP_NODE *)malloc(sizeof(IP_NODE) * (ip_route->ip_num));
  177. memcpy(ip_route->ip_list,
  178. master_app_set + master_forward->headers[pos].offset,
  179. sizeof(IP_NODE) * (ip_route->ip_num));
  180. } else {
  181. // printf("slave!\n");
  182. int pos = binary_search_header(slave_forward->headers, 0,
  183. slave_forward->bid_size, bid);
  184. if (pos < 0) {
  185. detach_handler(&ca_handler);
  186. return -NOT_FIND_BID;
  187. }
  188. if (slave_forward->headers[pos].offset < 0) {
  189. detach_handler(&ca_handler);
  190. return -OFFSET_ERR;
  191. }
  192. ip_route->ip_num = slave_forward->headers[pos].size;
  193. if (ip_route->ip_num <= 0)
  194. return -IP_NUM_ERR;
  195. ip_route->ip_list =
  196. (IP_NODE *)malloc(sizeof(IP_NODE) * (ip_route->ip_num));
  197. memcpy(ip_route->ip_list,
  198. slave_app_set + slave_forward->headers[pos].offset,
  199. sizeof(IP_NODE) * (ip_route->ip_num));
  200. }
  201. return detach_handler(&ca_handler);
  202. }
  203. int free_ip_route(IP_ROUTE *ip_route)
  204. {
  205. if (ip_route) {
  206. ip_route->ip_num = 0;
  207. if (ip_route->ip_list) {
  208. free(ip_route->ip_list);
  209. ip_route->ip_list = NULL;
  210. }
  211. }
  212. return 0;
  213. }