da_client.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <inttypes.h>
  17. #include "da_client.h"
  18. #include "da_core.h"
  19. #include "da_event.h"
  20. #include "da_queue.h"
  21. #include "da_util.h"
  22. #include "da_server.h"
  23. #include "da_msg.h"
  24. #include "da_request.h"
  25. #include "da_conn.h"
  26. #include "da_stats.h"
  27. #include "da_time.h"
  28. #include "da_top_percentile.h"
  29. void client_ref(struct conn *conn, void *owner) {
  30. ASSERT((conn->type & FRONTWORK) && conn->owner==NULL);
  31. struct server_pool *pool = owner;
  32. conn->family = 0;
  33. conn->addrlen = 0;
  34. conn->addr = NULL;
  35. pool->c_conn_count++;
  36. conn->owner = owner;
  37. TAILQ_INSERT_TAIL(&pool->c_conn_q, conn, conn_tqe);
  38. log_debug("ref conn %p owner %p into pool '%.*s'", conn, pool,
  39. pool->name.len, pool->name.data);
  40. }
  41. void client_unref(struct conn *conn) {
  42. ASSERT((conn->type & FRONTWORK) && conn->owner!=NULL);
  43. struct server_pool *pool;
  44. pool = conn->owner;
  45. conn->owner = NULL;
  46. ASSERT(pool->c_conn_count != 0);
  47. pool->c_conn_count--;
  48. TAILQ_REMOVE(&pool->c_conn_q, conn, conn_tqe);
  49. log_debug("unref conn %p owner %p from pool '%.*s'", conn, pool,
  50. pool->name.len, pool->name.data);
  51. }
  52. int client_active(struct conn *conn) {
  53. ASSERT(conn->type & FRONTWORK);
  54. if (!TAILQ_EMPTY(&conn->imsg_q)) {
  55. return true;
  56. }
  57. if (!TAILQ_EMPTY(&conn->omsg_q)) {
  58. return true;
  59. }
  60. if (conn->rmsg != NULL) {
  61. return true;
  62. }
  63. if (conn->smsg != NULL) {
  64. return true;
  65. }
  66. return false;
  67. }
  68. static void client_close_stats(struct context *ctx, struct server_pool *pool, int err,
  69. unsigned eof)
  70. {
  71. stats_pool_decr(ctx, pool, client_connections);
  72. if(eof)
  73. {
  74. stats_pool_incr(ctx, pool, client_eof);
  75. return;
  76. }
  77. if(err)
  78. {
  79. stats_pool_incr(ctx, pool, client_err);
  80. }
  81. return;
  82. }
  83. void client_close(struct context *ctx, struct conn *conn) {
  84. ASSERT(ctx!=NULL);
  85. ASSERT(conn->type & FRONTWORK);
  86. int status;
  87. struct msg *msg, *nmsg;
  88. client_close_stats(ctx,conn->owner, conn->err,conn->eof);
  89. if (conn->fd < 0) {
  90. conn->unref(conn);
  91. conn_put(conn);
  92. return;
  93. }
  94. for (msg = TAILQ_FIRST(&conn->imsg_q); msg != NULL; msg = nmsg) {
  95. nmsg = TAILQ_NEXT(msg, c_i_tqe);
  96. conn->dequeue_inq(ctx, conn, msg);
  97. /* count the elapse time */
  98. if(msg->frag_id == 0)
  99. {
  100. int64_t elaspe_time = now_us - msg->start_ts;
  101. stats_pool_incr_by(ctx, conn->owner, pool_elaspe_time, elaspe_time);
  102. top_percentile_report(ctx, conn->owner, elaspe_time, 1, RT_SHARDING);
  103. top_percentile_report(ctx, conn->owner, elaspe_time, 1, RT_ALL);
  104. }
  105. if (msg->done) {
  106. req_put(msg);
  107. } else {
  108. //set swallow flag
  109. msg->swallow = 1;
  110. }
  111. }
  112. for (msg = TAILQ_FIRST(&conn->omsg_q); msg != NULL; msg = nmsg) {
  113. nmsg = TAILQ_NEXT(msg, c_o_tqe);
  114. int64_t elaspe_time = now_us - msg->start_ts;
  115. /* count the elapse time */
  116. stats_pool_incr_by(ctx, conn->owner, pool_elaspe_time, elaspe_time);
  117. top_percentile_report(ctx, conn->owner, elaspe_time, 1, RT_SHARDING);
  118. top_percentile_report(ctx, conn->owner, elaspe_time, 1, RT_ALL);
  119. conn->dequeue_outq(ctx, conn, msg);
  120. req_put(msg);
  121. }
  122. msg = conn->rmsg;
  123. if (msg != NULL) {
  124. conn->rmsg = NULL;
  125. ASSERT(msg->request);
  126. req_put(msg);
  127. /* count the elapse time */
  128. int64_t elaspe_time = now_us - msg->start_ts;
  129. stats_pool_incr_by(ctx, conn->owner, pool_elaspe_time, elaspe_time);
  130. top_percentile_report(ctx, conn->owner, elaspe_time, 1, RT_SHARDING);
  131. top_percentile_report(ctx, conn->owner, elaspe_time, 1, RT_ALL);
  132. log_debug("close s %d discarding req %"PRIu64" len %"PRIu32" "
  133. "in error", conn->fd, msg->id, msg->mlen);
  134. }
  135. conn->unref(conn);
  136. status = close(conn->fd);
  137. if (status < 0) {
  138. log_error("close c %d failed, ignored: %s", conn->fd, strerror(errno));
  139. }
  140. conn->fd = -1;
  141. conn_put(conn);
  142. }