forked from Netflix/dynomite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyn_proxy.c
233 lines (185 loc) · 5.8 KB
/
dyn_proxy.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
/*
* Dynomite - A thin, distributed replication layer for multi non-distributed
* storages. Copyright (C) 2014 Netflix, Inc.
*/
/*
* 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 <sys/un.h>
#include "dyn_client.h"
#include "dyn_core.h"
#include "dyn_proxy.h"
#include "dyn_server.h"
static void proxy_ref(struct conn *conn, void *owner) {
struct server_pool *pool = owner;
ASSERT(conn->type == CONN_PROXY);
ASSERT(conn->owner == NULL);
conn->family = pool->proxy_endpoint.family;
conn->addrlen = pool->proxy_endpoint.addrlen;
conn->addr = pool->proxy_endpoint.addr;
string_duplicate(&conn->pname, &pool->proxy_endpoint.pname);
pool->p_conn = conn;
/* owner of the proxy connection is the server pool */
conn->owner = owner;
log_debug(LOG_VVERB, "ref conn %p owner %p", conn, pool);
}
static void proxy_unref(struct conn *conn) {
struct server_pool *pool;
ASSERT(conn->type == CONN_PROXY);
ASSERT(conn->owner != NULL);
conn_event_del_conn(conn);
pool = conn->owner;
conn->owner = NULL;
pool->p_conn = NULL;
log_debug(LOG_VVERB, "unref conn %p owner %p", conn, pool);
}
static void proxy_close(struct context *ctx, struct conn *conn) {
rstatus_t status;
ASSERT(conn->type == CONN_PROXY);
if (conn->sd < 0) {
conn_unref(conn);
conn_put(conn);
return;
}
ASSERT(conn->rmsg == NULL);
ASSERT(conn->smsg == NULL);
ASSERT(TAILQ_EMPTY(&conn->imsg_q));
ASSERT(TAILQ_EMPTY(&conn->omsg_q));
conn_unref(conn);
status = close(conn->sd);
if (status < 0) {
log_error("close p %d failed, ignored: %s", conn->sd, strerror(errno));
}
conn->sd = -1;
conn_put(conn);
}
rstatus_t proxy_init(struct context *ctx) {
rstatus_t status;
struct server_pool *pool = &ctx->pool;
struct conn *p = conn_get(pool, init_proxy_conn);
if (!p) {
return DN_ENOMEM;
}
status = conn_listen(pool->ctx, p);
if (status != DN_OK) {
conn_close(pool->ctx, p);
return status;
}
char *log_datastore = "not selected data store";
if (g_data_store == DATA_REDIS) {
log_datastore = "redis";
} else if (g_data_store == DATA_MEMCACHE) {
log_datastore = "memcache";
}
log_debug(LOG_NOTICE, "%s inited in %s %s", print_obj(p), log_datastore,
print_obj(pool));
return DN_OK;
}
void proxy_deinit(struct context *ctx) {
struct server_pool *pool = &ctx->pool;
struct conn *p = pool->p_conn;
if (p != NULL) {
conn_close(pool->ctx, p);
pool->p_conn = NULL;
}
log_debug(LOG_VVERB, "deinit proxy");
}
static rstatus_t proxy_accept(struct context *ctx, struct conn *p) {
rstatus_t status;
struct conn *c;
int sd;
ASSERT(p->type == CONN_PROXY);
ASSERT(p->sd > 0);
ASSERT(p->recv_active && p->recv_ready);
for (;;) {
sd = accept(p->sd, NULL, NULL);
if (sd < 0) {
if (errno == EINTR) {
log_warn("accept on %s not ready - eintr", print_obj(p));
continue;
}
if (errno == EAGAIN || errno == EWOULDBLOCK) {
p->recv_ready = 0;
return DN_OK;
}
/*
* FIXME: On EMFILE or ENFILE mask out IN event on the proxy; mask
* it back in when some existing connection gets closed
*/
log_error("accept on %s failed: %s", print_obj(p), strerror(errno));
return DN_ERROR;
}
break;
}
c = conn_get(p->owner, init_client_conn);
if (c == NULL) {
log_error("get conn for CLIENT %d from %s failed: %s", sd, print_obj(p),
strerror(errno));
status = close(sd);
if (status < 0) {
log_error("close c %d failed, ignored: %s", sd, strerror(errno));
}
return DN_ENOMEM;
}
c->sd = sd;
string_copy_c(&c->pname, (unsigned char *)dn_unresolve_peer_desc(c->sd));
stats_pool_incr(ctx, client_connections);
status = dn_set_nonblocking(c->sd);
if (status < 0) {
log_error("%s Failed to set nonblock on %s: %s", print_obj(p), print_obj(c),
strerror(errno));
conn_close(ctx, c);
return status;
}
if (p->family == AF_INET || p->family == AF_INET6) {
status = dn_set_tcpnodelay(c->sd);
if (status < 0) {
log_warn("%s Failed to set tcpnodelay on %s: %s", print_obj(p),
print_obj(c), strerror(errno));
}
}
status = conn_event_add_conn(c);
if (status < 0) {
log_error("%s Failed to add %s to event loop: %s", print_obj(p),
print_obj(c), strerror(errno));
conn_close(ctx, c);
return status;
}
log_notice("%s accepted %s", print_obj(p), print_obj(c));
return DN_OK;
}
static rstatus_t proxy_recv(struct context *ctx, struct conn *conn) {
ASSERT(conn->type == CONN_PROXY);
ASSERT(conn->recv_active);
conn->recv_ready = 1;
do {
if (proxy_accept(ctx, conn) != DN_OK) {
log_error("%s Failed to accept a connection. Continuing",
print_obj(conn));
continue;
}
} while (conn->recv_ready);
return DN_OK;
}
struct conn_ops proxy_ops = {proxy_recv, NULL, NULL, NULL, NULL, NULL,
proxy_close, NULL, proxy_ref, proxy_unref,
// enqueue, dequeues
NULL, NULL, NULL, NULL, conn_cant_handle_response};
void init_proxy_conn(struct conn *conn) {
conn->dyn_mode = 0;
conn->type = CONN_PROXY;
conn->ops = &proxy_ops;
}