This repository was archived by the owner on Mar 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathnc_response.c
378 lines (303 loc) · 10.1 KB
/
nc_response.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* twemproxy - A fast and lightweight proxy for memcached protocol.
* Copyright (C) 2011 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <nc_core.h>
#include <nc_server.h>
struct msg *
rsp_get(struct conn *conn, struct msg_base *msgb)
{
struct msg *msg;
ASSERT(!conn->client && !conn->proxy);
msg = msg_get(conn, false, conn->source_type, msgb);
if (msg == NULL) {
conn->err = errno;
}
return msg;
}
void
rsp_put(struct msg *msg)
{
ASSERT(!msg->request);
ASSERT(msg->peer == NULL);
msg_put(msg);
}
static struct msg *
rsp_make_error(struct context *ctx, struct conn *conn, struct msg *msg)
{
struct msg *pmsg; /* peer message (response) */
struct msg *cmsg, *nmsg; /* current and next message (request) */
uint64_t id;
err_t err;
ASSERT(conn->client && !conn->proxy);
ASSERT(msg->request && req_error(conn, msg));
ASSERT(msg->owner == conn);
id = msg->frag_id;
if (id != 0) {
for (err = 0, cmsg = TAILQ_NEXT(msg, c_tqe);
cmsg != NULL && cmsg->frag_id == id;
cmsg = nmsg) {
nmsg = TAILQ_NEXT(cmsg, c_tqe);
/* dequeue request (error fragment) from client outq */
conn->dequeue_outq(ctx, conn, cmsg);
if (err == 0 && cmsg->err != 0) {
err = cmsg->err;
}
req_put(cmsg);
}
} else {
err = msg->err;
}
pmsg = msg->peer;
if (pmsg != NULL) {
ASSERT(!pmsg->request && pmsg->peer == msg);
msg->peer = NULL;
pmsg->peer = NULL;
rsp_put(pmsg);
}
return msg_get_error(conn->source_type, err, ctx->msgb);
}
struct msg *
rsp_recv_next(struct context *ctx, struct conn *conn, bool alloc)
{
struct msg *msg;
ASSERT(!conn->client && !conn->proxy);
if (conn->eof) {
msg = conn->rmsg;
/* server sent eof before sending the entire request */
if (msg != NULL) {
conn->rmsg = NULL;
ASSERT(msg->peer == NULL);
ASSERT(!msg->request);
log_error("eof s %d discarding incomplete rsp %"PRIu64" len "
"%"PRIu32"", conn->sd, msg->id, msg->mlen);
rsp_put(msg);
}
/*
* We treat TCP half-close from a server different from how we treat
* those from a client. On a FIN from a server, we close the connection
* immediately by sending the second FIN even if there were outstanding
* or pending requests. This is actually a tricky part in the FA, as
* we don't expect this to happen unless the server is misbehaving or
* it crashes
*/
conn->done = 1;
log_error("s %d active %d is done", conn->sd, conn->active(conn));
return NULL;
}
msg = conn->rmsg;
if (msg != NULL) {
ASSERT(!msg->request);
return msg;
}
if (!alloc) {
return NULL;
}
msg = rsp_get(conn, ctx->msgb);
if (msg != NULL) {
conn->rmsg = msg;
}
return msg;
}
static bool
rsp_filter(struct context *ctx, struct conn *conn, struct msg *msg)
{
struct msg *pmsg;
ASSERT(!conn->client && !conn->proxy);
if (msg_empty(msg)) {
ASSERT(conn->rmsg == NULL);
log_debug(LOG_VERB, "filter empty rsp %"PRIu64" on s %d", msg->id,
conn->sd);
rsp_put(msg);
return true;
}
pmsg = TAILQ_FIRST(&conn->omsg_q);
if (pmsg == NULL) {
log_debug(LOG_ERR, "filter stray rsp %"PRIu64" len %"PRIu32" on s %d",
msg->id, msg->mlen, conn->sd);
rsp_put(msg);
/*
* Memcached server can respond with an error response before it has
* received the entire request. This is most commonly seen for set
* requests that exceed item_size_max. IMO, this behavior of memcached
* is incorrect. The right behavior for update requests that are over
* item_size_max would be to either:
* - close the connection Or,
* - read the entire item_size_max data and then send CLIENT_ERROR
*
* We handle this stray packet scenario in nutcracker by closing the
* server connection which would end up sending SERVER_ERROR to all
* clients that have requests pending on this server connection. The
* fix is aggressive, but not doing so would lead to clients getting
* out of sync with the server and as a result clients end up getting
* responses that don't correspond to the right request.
*
* See: https://github.com/twitter/twemproxy/issues/149
*/
conn->err = EINVAL;
conn->done = 1;
return true;
}
ASSERT(pmsg->peer == NULL);
ASSERT(pmsg->request && !pmsg->done);
/*
* If the response from a server suggests a protocol level transient
* failure, close the server connection and send back a generic error
* response to the client.
*
* If auto_eject_host is enabled, this will also update the failure_count
* and eject the server if it exceeds the failure_limit
*/
if (msg->failure(msg)) {
log_debug(LOG_INFO, "server failure rsp %"PRIu64" len %"PRIu32" "
"type %d on s %d", msg->id, msg->mlen, msg->type, conn->sd);
rsp_put(msg);
conn->err = EINVAL;
conn->done = 1;
return true;
}
if (pmsg->swallow) {
conn->swallow_msg(conn, pmsg, msg);
conn->dequeue_outq(ctx, conn, pmsg);
pmsg->done = 1;
log_debug(LOG_INFO, "swallow rsp %"PRIu64" len %"PRIu32" of req "
"%"PRIu64" on s %d", msg->id, msg->mlen, pmsg->id,
conn->sd);
rsp_put(msg);
req_put(pmsg);
return true;
}
return false;
}
static void
rsp_forward_stats(struct context *ctx, struct server *server, struct msg *msg, uint32_t msgsize)
{
ASSERT(!msg->request);
stats_server_incr(ctx, server, responses);
stats_server_incr_by(ctx, server, response_bytes, msgsize);
}
static void
rsp_forward(struct context *ctx, struct conn *s_conn, struct msg *msg)
{
rstatus_t status;
struct msg *pmsg;
struct conn *c_conn;
uint32_t msgsize;
ASSERT(!s_conn->client && !s_conn->proxy);
msgsize = msg->mlen;
/* response from server implies that server is ok and heartbeating */
server_ok(ctx, s_conn);
/* dequeue peer message (request) from server */
pmsg = TAILQ_FIRST(&s_conn->omsg_q);
ASSERT(pmsg != NULL && pmsg->peer == NULL);
ASSERT(pmsg->request && !pmsg->done);
s_conn->dequeue_outq(ctx, s_conn, pmsg);
pmsg->done = 1;
/* establish msg <-> pmsg (response <-> request) link */
pmsg->peer = msg;
msg->peer = pmsg;
msg->pre_coalesce(msg);
c_conn = pmsg->owner;
ASSERT(c_conn->client && !c_conn->proxy);
if (req_done(c_conn, TAILQ_FIRST(&c_conn->omsg_q))) {
status = event_add_out(ctx->evb, c_conn);
if (status != NC_OK) {
c_conn->err = errno;
}
}
rsp_forward_stats(ctx, s_conn->owner, msg, msgsize);
}
void
rsp_recv_done(struct context *ctx, struct conn *conn, struct msg *msg,
struct msg *nmsg)
{
ASSERT(!conn->client && !conn->proxy);
ASSERT(msg != NULL && conn->rmsg == msg);
ASSERT(!msg->request);
ASSERT(msg->owner == conn);
ASSERT(nmsg == NULL || !nmsg->request);
/* enqueue next message (response), if any */
conn->rmsg = nmsg;
if (rsp_filter(ctx, conn, msg)) {
return;
}
rsp_forward(ctx, conn, msg);
}
struct msg *
rsp_send_next(struct context *ctx, struct conn *conn)
{
rstatus_t status;
struct msg *msg, *pmsg; /* response and it's peer request */
ASSERT(conn->client && !conn->proxy);
pmsg = TAILQ_FIRST(&conn->omsg_q);
if (pmsg == NULL || !req_done(conn, pmsg)) {
/* nothing is outstanding, initiate close? */
if (pmsg == NULL && conn->eof) {
conn->done = 1;
log_debug(LOG_INFO, "c %d is done", conn->sd);
}
status = event_del_out(ctx->evb, conn);
if (status != NC_OK) {
conn->err = errno;
}
return NULL;
}
msg = conn->smsg;
if (msg != NULL) {
ASSERT(!msg->request && msg->peer != NULL);
ASSERT(req_done(conn, msg->peer));
pmsg = TAILQ_NEXT(msg->peer, c_tqe);
}
if (pmsg == NULL || !req_done(conn, pmsg)) {
conn->smsg = NULL;
return NULL;
}
ASSERT(pmsg->request && !pmsg->swallow);
if (req_error(conn, pmsg)) {
msg = rsp_make_error(ctx, conn, pmsg);
if (msg == NULL) {
conn->err = errno;
return NULL;
}
msg->peer = pmsg;
pmsg->peer = msg;
if (conn->source_type != NC_SOURCE_TYPE_PROXY)
stats_pool_incr(ctx, conn->owner, forward_error);
} else {
msg = pmsg->peer;
}
ASSERT(!msg->request);
conn->smsg = msg;
log_debug(LOG_VVERB, "send next rsp %"PRIu64" on c %d", msg->id, conn->sd);
return msg;
}
void
rsp_send_done(struct context *ctx, struct conn *conn, struct msg *msg)
{
struct msg *pmsg; /* peer message (request) */
ASSERT(conn->client && !conn->proxy);
ASSERT(conn->smsg == NULL);
log_debug(LOG_VVERB, "send done rsp %"PRIu64" on c %d", msg->id, conn->sd);
pmsg = msg->peer;
if (pmsg->start_ts > 0) {
slowlog_push_entry_if_needed(pmsg,(long long)(nc_usec_now()-pmsg->start_ts));
}
ASSERT(!msg->request && pmsg->request);
ASSERT(pmsg->peer == msg);
ASSERT(pmsg->done && !pmsg->swallow);
/* dequeue request from client outq */
conn->dequeue_outq(ctx, conn, pmsg);
req_put(pmsg);
}