forked from agroal/pgagroal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline_perf.c
296 lines (257 loc) · 8.31 KB
/
pipeline_perf.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
/*
* Copyright (C) 2022 Red Hat
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may
* be used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* pgagroal */
#include <pgagroal.h>
#include <logging.h>
#include <management.h>
#include <message.h>
#include <network.h>
#include <pipeline.h>
#include <worker.h>
/* system */
#include <errno.h>
#include <ev.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
static int performance_initialize(void*, void**, size_t*);
static void performance_start(struct ev_loop* loop, struct worker_io*);
static void performance_client(struct ev_loop* loop, struct ev_io* watcher, int revents);
static void performance_server(struct ev_loop* loop, struct ev_io* watcher, int revents);
static void performance_stop(struct ev_loop* loop, struct worker_io*);
static void performance_destroy(void*, size_t);
static void performance_periodic(void);
static bool saw_x = false;
struct pipeline
performance_pipeline(void)
{
struct pipeline pipeline;
pipeline.initialize = &performance_initialize;
pipeline.start = &performance_start;
pipeline.client = &performance_client;
pipeline.server = &performance_server;
pipeline.stop = &performance_stop;
pipeline.destroy = &performance_destroy;
pipeline.periodic = &performance_periodic;
return pipeline;
}
static int
performance_initialize(void* shmem, void** pipeline_shmem, size_t* pipeline_shmem_size)
{
return 0;
}
static void
performance_start(struct ev_loop* loop, struct worker_io* w)
{
struct configuration* config;
config = (struct configuration*)shmem;
for (int i = 0; i < config->max_connections; i++)
{
if (i != w->slot && !config->connections[i].new && config->connections[i].fd > 0)
{
pgagroal_disconnect(config->connections[i].fd);
}
}
return;
}
static void
performance_stop(struct ev_loop* loop, struct worker_io* w)
{
}
static void
performance_destroy(void* pipeline_shmem, size_t pipeline_shmem_size)
{
}
static void
performance_periodic(void)
{
}
static void
performance_client(struct ev_loop* loop, struct ev_io* watcher, int revents)
{
int status = MESSAGE_STATUS_ERROR;
struct worker_io* wi = NULL;
struct message* msg = NULL;
struct configuration* config = NULL;
wi = (struct worker_io*)watcher;
status = pgagroal_read_socket_message(wi->client_fd, &msg);
if (likely(status == MESSAGE_STATUS_OK))
{
if (likely(msg->kind != 'X'))
{
if (wi->server_ssl == NULL)
{
status = pgagroal_write_socket_message(wi->server_fd, msg);
}
else
{
status = pgagroal_write_ssl_message(wi->server_ssl, msg);
}
if (unlikely(status != MESSAGE_STATUS_OK))
{
goto server_error;
}
}
else if (msg->kind == 'X')
{
saw_x = true;
running = 0;
}
}
else if (status == MESSAGE_STATUS_ZERO)
{
goto client_done;
}
else
{
goto client_error;
}
ev_break (loop, EVBREAK_ONE);
return;
client_done:
config = (struct configuration*)shmem;
pgagroal_log_debug("[C] Client done (slot %d database %s user %s): %s (socket %d status %d)",
wi->slot, config->connections[wi->slot].database, config->connections[wi->slot].username,
strerror(errno), wi->client_fd, status);
errno = 0;
if (saw_x)
{
exit_code = WORKER_SUCCESS;
}
else
{
exit_code = WORKER_SERVER_FAILURE;
}
running = 0;
ev_break(loop, EVBREAK_ALL);
return;
client_error:
config = (struct configuration*)shmem;
pgagroal_log_warn("[C] Client error (slot %d database %s user %s): %s (socket %d status %d)",
wi->slot, config->connections[wi->slot].database, config->connections[wi->slot].username,
strerror(errno), wi->client_fd, status);
pgagroal_log_message(msg);
errno = 0;
exit_code = WORKER_CLIENT_FAILURE;
running = 0;
ev_break(loop, EVBREAK_ALL);
return;
server_error:
config = (struct configuration*)shmem;
pgagroal_log_warn("[C] Server error (slot %d database %s user %s): %s (socket %d status %d)",
wi->slot, config->connections[wi->slot].database, config->connections[wi->slot].username,
strerror(errno), wi->server_fd, status);
pgagroal_log_message(msg);
errno = 0;
exit_code = WORKER_SERVER_FAILURE;
running = 0;
ev_break(loop, EVBREAK_ALL);
return;
}
static void
performance_server(struct ev_loop* loop, struct ev_io* watcher, int revents)
{
int status = MESSAGE_STATUS_ERROR;
bool fatal = false;
struct worker_io* wi = NULL;
struct message* msg = NULL;
struct configuration* config = NULL;
wi = (struct worker_io*)watcher;
if (wi->server_ssl == NULL)
{
status = pgagroal_read_socket_message(wi->server_fd, &msg);
}
else
{
status = pgagroal_read_ssl_message(wi->server_ssl, &msg);
}
if (likely(status == MESSAGE_STATUS_OK))
{
status = pgagroal_write_socket_message(wi->client_fd, msg);
if (unlikely(status != MESSAGE_STATUS_OK))
{
goto client_error;
}
if (unlikely(msg->kind == 'E'))
{
fatal = false;
if (!strncmp(msg->data + 6, "FATAL", 5) || !strncmp(msg->data + 6, "PANIC", 5))
{
fatal = true;
}
if (fatal)
{
exit_code = WORKER_SERVER_FATAL;
running = 0;
}
}
}
else if (status == MESSAGE_STATUS_ZERO)
{
goto server_done;
}
else
{
goto server_error;
}
ev_break(loop, EVBREAK_ONE);
return;
client_error:
config = (struct configuration*)shmem;
pgagroal_log_warn("[S] Client error (slot %d database %s user %s): %s (socket %d status %d)",
wi->slot, config->connections[wi->slot].database, config->connections[wi->slot].username,
strerror(errno), wi->client_fd, status);
pgagroal_log_message(msg);
errno = 0;
exit_code = WORKER_CLIENT_FAILURE;
running = 0;
ev_break(loop, EVBREAK_ALL);
return;
server_done:
config = (struct configuration*)shmem;
pgagroal_log_debug("[S] Server done (slot %d database %s user %s): %s (socket %d status %d)",
wi->slot, config->connections[wi->slot].database, config->connections[wi->slot].username,
strerror(errno), wi->server_fd, status);
errno = 0;
running = 0;
ev_break(loop, EVBREAK_ALL);
return;
server_error:
config = (struct configuration*)shmem;
pgagroal_log_warn("[S] Server error (slot %d database %s user %s): %s (socket %d status %d)",
wi->slot, config->connections[wi->slot].database, config->connections[wi->slot].username,
strerror(errno), wi->server_fd, status);
pgagroal_log_message(msg);
errno = 0;
exit_code = WORKER_SERVER_FAILURE;
running = 0;
ev_break(loop, EVBREAK_ALL);
return;
}