da_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. #include <sys/resource.h>
  17. #include <inttypes.h>
  18. #include <fcntl.h>
  19. #include "da_core.h"
  20. #include "da_event.h"
  21. #include "da_conn.h"
  22. #include "da_conf.h"
  23. #include "da_server.h"
  24. #include "da_listener.h"
  25. #include "da_request.h"
  26. #include "da_time.h"
  27. #include "da_signal.h"
  28. #include "da_stats.h"
  29. static enum core_status inst_status = NORMAL;
  30. static uint32_t ctx_id; /* context generation */
  31. int write_send_queue_len = 0;
  32. struct conn **wait_send_queue; /*conn*/
  33. void cache_send_event(struct conn *conn) {
  34. struct context * ctx = conn_to_ctx(conn);
  35. if(write_send_queue_len < ctx->sum_nconn) {
  36. wait_send_queue[write_send_queue_len] = conn;
  37. conn->writecached = 1;
  38. ++write_send_queue_len;
  39. }
  40. }
  41. /*
  42. * 根据可用连接及后端连接数,计算前端连接可用的连接数
  43. */
  44. static int core_calc_connections(struct context *ctx) {
  45. int status;
  46. struct rlimit limit;
  47. status = getrlimit(RLIMIT_NOFILE, &limit);
  48. if (status < 0) {
  49. log_error("getrlimit failed: %s", strerror(errno));
  50. return -1;
  51. }
  52. ctx->max_nfd = (uint32_t) limit.rlim_cur;
  53. ctx->max_ncconn = ctx->max_nfd - ctx->max_nsconn - RESERVED_FDS;
  54. log_debug(
  55. "max fds %"PRIu32" max client conns %"PRIu32" " "max server conns %"PRIu32"",
  56. ctx->max_nfd, ctx->max_ncconn, ctx->max_nsconn);
  57. return 0;
  58. }
  59. static struct context *core_ctx_create(struct instance *dai) {
  60. int status;
  61. struct context *ctx;
  62. ctx = malloc(sizeof(*ctx));
  63. if (ctx == NULL) {
  64. return NULL;
  65. }
  66. ctx->id = ++ctx_id;
  67. ctx->cf = NULL;
  68. ctx->evb = NULL;
  69. array_null(&ctx->pool);
  70. ctx->max_timeout = dai->event_max_timeout;
  71. ctx->timeout = ctx->max_timeout;
  72. ctx->max_nfd = 0;
  73. ctx->max_ncconn = 0;
  74. ctx->max_nsconn = 0;
  75. ctx->sum_nconn = 0;
  76. /* parse and create configuration */
  77. ctx->cf = conf_create(dai->conf_filename);
  78. if (ctx->cf == NULL) {
  79. free(ctx);
  80. return NULL;
  81. }
  82. /* initialize server pool from configuration */
  83. status = server_pool_init(&ctx->pool, &ctx->cf->pool, ctx);
  84. if (status != 0) {
  85. conf_destroy(ctx->cf);
  86. free(ctx);
  87. return NULL;
  88. }
  89. /*
  90. * Get rlimit and calculate max client connections after we have
  91. * calculated max server connections
  92. */
  93. status = core_calc_connections(ctx);
  94. if (status != 0) {
  95. server_pool_deinit(&ctx->pool);
  96. conf_destroy(ctx->cf);
  97. free(ctx);
  98. return NULL;
  99. }
  100. /* create stats per server pool */
  101. ctx->stats = stats_create(dai->stats_interval, ctx->cf->localip, &ctx->pool);
  102. if (ctx->stats == NULL) {
  103. server_pool_deinit(&ctx->pool);
  104. conf_destroy(ctx->cf);
  105. free(ctx);
  106. return NULL;
  107. }
  108. /* initialize event handling for client, proxy and server */
  109. ctx->evb = event_base_create(EVENT_SIZE, &core_core);
  110. if (ctx->evb == NULL) {
  111. stats_destroy(ctx->stats);
  112. server_pool_deinit(&ctx->pool);
  113. conf_destroy(ctx->cf);
  114. free(ctx);
  115. return NULL;
  116. }
  117. /* preconnect? servers in server pool */
  118. status = server_pool_preconnect(ctx);
  119. if (status != 0) {
  120. server_pool_disconnect(ctx);
  121. event_base_destroy(ctx->evb);
  122. stats_destroy(ctx->stats);
  123. server_pool_deinit(&ctx->pool);
  124. conf_destroy(ctx->cf);
  125. free(ctx);
  126. return NULL;
  127. }
  128. /* initialize listener per server pool */
  129. status = listener_init(ctx);
  130. if (status != 0) {
  131. server_pool_disconnect(ctx);
  132. event_base_destroy(ctx->evb);
  133. stats_destroy(ctx->stats);
  134. server_pool_deinit(&ctx->pool);
  135. conf_destroy(ctx->cf);
  136. free(ctx);
  137. return NULL;
  138. }
  139. log_debug("context sum_nconn:%d", ctx->sum_nconn);
  140. wait_send_queue = malloc(ctx->sum_nconn*sizeof(struct conn *));
  141. if(NULL == wait_send_queue) {
  142. listener_deinit(ctx);
  143. server_pool_disconnect(ctx);
  144. event_base_destroy(ctx->evb);
  145. stats_destroy(ctx->stats);
  146. server_pool_deinit(&ctx->pool);
  147. conf_destroy(ctx->cf);
  148. free(ctx);
  149. return NULL;
  150. }
  151. log_debug("created ctx %p id %"PRIu32"", ctx, ctx->id);
  152. return ctx;
  153. }
  154. struct context *core_start(struct instance *dai) {
  155. struct context *ctx;
  156. mbuf_init(dai);
  157. msg_init();
  158. conn_init();
  159. ctx = core_ctx_create(dai);
  160. if (ctx != NULL) {
  161. dai->ctx = ctx;
  162. return ctx;
  163. }
  164. conn_deinit();
  165. msg_deinit();
  166. mbuf_deinit();
  167. return NULL;
  168. }
  169. static void core_ctx_destroy(struct context *ctx) {
  170. log_debug("destroy ctx %p id %"PRIu32"", ctx, ctx->id);
  171. listener_deinit(ctx);
  172. server_pool_disconnect(ctx);
  173. event_base_destroy(ctx->evb);
  174. stats_destroy(ctx->stats);
  175. server_pool_deinit(&ctx->pool);
  176. conf_destroy(ctx->cf);
  177. free(wait_send_queue);
  178. free(ctx);
  179. }
  180. void core_stop(struct context *dai) {
  181. core_ctx_destroy(dai);
  182. conn_deinit();
  183. msg_deinit();
  184. mbuf_deinit();
  185. }
  186. static int core_recv(struct context *ctx, struct conn *conn) {
  187. int status;
  188. status = conn->recv(ctx, conn);
  189. if (status != 0) {
  190. log_error("recv on %d failed: %s", conn->fd, strerror(errno));
  191. }
  192. return status;
  193. }
  194. static int core_send(struct context *ctx, struct conn *conn) {
  195. int status;
  196. status = conn->send(ctx, conn);
  197. if (status != 0) {
  198. log_error("send on %d failed: status: %d errno: %d %s", conn->fd,
  199. status, errno, strerror(errno));
  200. }
  201. return status;
  202. }
  203. static void core_close(struct context *ctx, struct conn *conn) {
  204. int status;
  205. char *type, *addrstr;
  206. ASSERT(conn->sd > 0);
  207. if (conn->type & FRONTWORK) {
  208. type = "frontwork";
  209. addrstr = da_unresolve_peer_desc(conn->fd);
  210. } else {
  211. type = conn->type & BACKWORK ? "backwork" : "listener";
  212. addrstr = da_unresolve_addr(conn->addr, conn->addrlen);
  213. }
  214. log_debug(
  215. "close %s %d '%s' on event %04"PRIX32" eof %d done " "%d rb %zu sb %zu%c %s",
  216. type, conn->fd, addrstr, conn->events, conn->eof, conn->done,
  217. conn->recv_bytes, conn->send_bytes, conn->err ? ':' : ' ',
  218. conn->err ? strerror(conn->err) : "");
  219. status = event_del_conn(ctx->evb, conn);
  220. if (status < 0) {
  221. log_warning("event del conn %s %d failed, ignored: %s", type, conn->fd,
  222. strerror(errno));
  223. }
  224. conn->close(ctx, conn);
  225. }
  226. static void core_error(struct context *ctx, struct conn *conn) {
  227. int status;
  228. char *type =
  229. conn->type & FRONTWORK ?
  230. "frontwork" :
  231. (conn->type & BACKWORK ? "backwork" : "listener");
  232. status = get_soerror(conn->fd);
  233. if (status < 0) {
  234. log_warning("get soerr on %s %d failed, ignored: %s", type, conn->fd,
  235. strerror(errno));
  236. }
  237. conn->err = errno;
  238. core_close(ctx, conn);
  239. }
  240. int core_core(void *arg, uint32_t events) {
  241. int status;
  242. struct conn *conn = arg;
  243. struct context *ctx = conn_to_ctx(conn);
  244. conn->events = events;
  245. log_debug("enter core_core,fd:%d,events :%d conn_type %d", conn->fd, events,conn->type);
  246. if (events & EVENT_ERR) {
  247. core_error(ctx, conn);
  248. return -1;
  249. }
  250. if (events & EVENT_READ) {
  251. status = core_recv(ctx, conn);
  252. if (status != 0 || conn->done || conn->err) {
  253. core_close(ctx, conn);
  254. return -1;
  255. }
  256. }
  257. if (events & EVENT_WRITE) {
  258. status = core_send(ctx, conn);
  259. if (status < 0 || conn->done || conn->err) {
  260. core_close(ctx, conn);
  261. return -1;
  262. }
  263. }
  264. return 0;
  265. }
  266. /*
  267. * reclaim msg from server and client q
  268. */
  269. static void reclaim_msg(struct context *ctx, struct msg *msg) {
  270. struct conn *c_conn, *s_conn;
  271. c_conn = msg->owner;
  272. s_conn = msg->peer_conn;
  273. if (msg->cli_inq) {
  274. c_conn->dequeue_inq(ctx, c_conn, msg);
  275. }
  276. if (msg->cli_outq) {
  277. c_conn->dequeue_outq(ctx, c_conn, msg);
  278. }
  279. if (msg->sev_inq) {
  280. s_conn->dequeue_inq(ctx, s_conn, msg);
  281. }
  282. if (msg->sev_msgtree) {
  283. s_conn->de_msgtree(ctx, s_conn, msg);
  284. }
  285. req_put(msg);
  286. return;
  287. }
  288. /*
  289. * reclaim timeout msg
  290. */
  291. static void reclaim_timeout_msg(struct context *ctx, struct msg *msg) {
  292. struct conn *c_conn;
  293. struct conn *s_conn;
  294. c_conn = (struct conn *) msg->owner;
  295. s_conn = (struct conn *) msg->peer_conn;
  296. reclaim_msg(ctx, msg);
  297. return;
  298. }
  299. static void reclaim_sending_msg(struct context *ctx, struct msg *msg) {
  300. struct conn *c_conn, *s_conn;
  301. c_conn = msg->owner;
  302. s_conn = msg->peer_conn;
  303. if (msg->cli_inq) {
  304. c_conn->dequeue_inq(ctx, c_conn, msg);
  305. }
  306. if (msg->cli_outq) {
  307. c_conn->dequeue_outq(ctx, c_conn, msg);
  308. }
  309. msg->swallow = 1;
  310. }
  311. /*
  312. * timeout process,not close the connection
  313. */
  314. static void core_timeout(struct context *ctx) {
  315. for (;;) {
  316. struct msg *msg;
  317. struct conn *conn;
  318. uint64_t then;
  319. msg = msg_tmo_min();
  320. if (msg == NULL) {
  321. //set epoll wait time
  322. ctx->timeout = ctx->max_timeout;
  323. return;
  324. }
  325. if (msg->error | msg->done) {
  326. msg_tmo_delete(msg);
  327. continue;
  328. }
  329. conn = msg->tmo_rbe.data;
  330. then = msg->tmo_rbe.key;
  331. if (now_ms < then) {
  332. int delta = (int) (then - now_ms);
  333. ctx->timeout = MIN(delta, ctx->max_timeout);
  334. return;
  335. }
  336. log_error("req %"PRIu64" on s %d timedout,fragment:%"PRIu32".", msg->id,
  337. conn->fd, msg->nfrag);
  338. //if(msg->swallow == 0)
  339. //{
  340. /* count the elapse time */
  341. // stats_pool_incr_by(ctx, conn->owner, pool_elaspe_time, now_us - msg->start_ts);
  342. //}
  343. //reclaim time out msg
  344. if(!msg->sending)
  345. reclaim_timeout_msg(ctx, msg);
  346. else{
  347. reclaim_sending_msg(ctx, msg);
  348. msg_tmo_delete(msg);
  349. msg_tmo_insert(msg, conn);
  350. }
  351. }
  352. return;
  353. }
  354. static void process_cached_write_event(struct context *ctx) {
  355. int i, status;
  356. struct conn *conn;
  357. struct cache_instance *ci;
  358. for (i = 0; i < write_send_queue_len; i++) {
  359. conn = wait_send_queue[i];
  360. ci = conn->owner;
  361. core_send(ctx, conn);
  362. conn->writecached = 0;
  363. if((((conn->type & FRONTWORK) && !TAILQ_EMPTY(&conn->omsg_q)) ||
  364. ((conn->type & BACKWORK) && !TAILQ_EMPTY(&conn->imsg_q)))
  365. && conn->connected && !(conn->done || conn -> error)){
  366. status = event_add_out(ctx->evb, conn);
  367. if (status < 0) {
  368. conn->error = 1;
  369. conn->err = CONN_EPOLLCTL_ERR;
  370. }
  371. }
  372. }
  373. write_send_queue_len = 0;
  374. return;
  375. }
  376. int core_loop(struct context *ctx) {
  377. int nsd;
  378. signal_process_queue();
  379. nsd = event_wait(ctx->evb, ctx->timeout);
  380. if (nsd < 0) {
  381. return nsd;
  382. }
  383. process_cached_write_event(ctx);
  384. core_timeout(ctx);
  385. stats_swap(ctx->stats);
  386. return 0;
  387. }
  388. int core_exec_new_binary(struct instance *dai) {
  389. int32_t size, len;
  390. uint32_t i;
  391. char *envp[] = { NULL, NULL };
  392. char *fds = NULL;
  393. struct context *ctx = dai->ctx;
  394. struct array *pool = &(ctx->pool);
  395. /*
  396. * 1. fork
  397. */
  398. int pid = fork();
  399. switch (pid) {
  400. case -1:
  401. log_error("fork in core_exec_new_binary got error");
  402. return -1;
  403. case 0: /* child */
  404. break;
  405. default: /* parent */
  406. return 0;
  407. }
  408. /* this is in child if we got here*/
  409. /*
  410. * 2. put all listen fds to NC_ENV_FDS:
  411. * NC_ENV_FDS=4;5;10;12;
  412. */
  413. size = (int32_t) (sizeof(NC_ENV_FDS)
  414. + (array_n(pool)) * (1 + DA_UINT32_MAXLEN));
  415. len = 0;
  416. fds = malloc(size);
  417. if (fds == NULL) {
  418. return -1;
  419. }
  420. len += da_scnprintf(fds + len, size - len, NC_ENV_FDS "=");
  421. //len += nc_scnprintf(fds + len, size - len, "%u;", ctx->stats->sd);
  422. for (i = 0; i < array_n(pool); i++) {
  423. struct server_pool *p = array_get(pool, i);
  424. int fd = p->listener->fd;
  425. if (fd <= 0) {
  426. continue;
  427. }
  428. len += da_scnprintf(fds + len, size - len, "%u;", fd);
  429. }
  430. fds[len] = '\0';
  431. log_debug("exec new binary with env: %s", fds);
  432. /*
  433. * 3. exec,set envp
  434. */
  435. envp[0] = fds;
  436. execve(dai->argv[0], dai->argv, envp);
  437. return 0;
  438. }
  439. int core_inherited_socket(char *listen_address) {
  440. int sock = 0;
  441. char *inherited;
  442. char *p, *q;
  443. /* we will use nc_unresolve_desc and overwrite input listen_address */
  444. char address[NI_MAXHOST + NI_MAXSERV];
  445. inherited = getenv(NC_ENV_FDS);
  446. if (inherited == NULL) {
  447. /* not found */
  448. return 0;
  449. }
  450. strncpy(address, listen_address, sizeof(address));
  451. log_debug("trying to get inherited socket '%s' from '%s'", address,
  452. inherited);
  453. for (p = inherited, q = inherited; *p; p++) {
  454. if (*p == ';') {
  455. sock = da_atoi(q, p - q);
  456. if (strcmp(address, da_unresolve_desc(sock)) == 0) {
  457. log_debug("get inherited socket %d for '%s' from '%s'", sock,
  458. address, inherited);
  459. sock = dup(sock);
  460. log_debug("dup inherited socket as %d", sock);
  461. return sock;
  462. }
  463. q = p + 1;
  464. }
  465. }
  466. log_debug("can not inherited socket '%s'", address);
  467. return 0;
  468. }
  469. void core_cleanup_inherited_socket(void) {
  470. int sock = 0;
  471. char *inherited;
  472. char *p, *q;
  473. inherited = getenv(NC_ENV_FDS);
  474. if (inherited == NULL) {
  475. return;
  476. }
  477. for (p = inherited, q = inherited; *p; p++) {
  478. if (*p == ';') {
  479. sock = da_atoi(q, p - q);
  480. close(sock);
  481. q = p + 1;
  482. }
  483. }
  484. }
  485. void core_setinst_status(enum core_status status)
  486. {
  487. inst_status = status;
  488. }
  489. enum core_status core_getinst_status()
  490. {
  491. return inst_status;
  492. }