forked from yandex/odyssey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.c
2317 lines (2025 loc) · 60.8 KB
/
frontend.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Odyssey.
*
* Scalable PostgreSQL connection pooler.
*/
#include <kiwi.h>
#include <machinarium.h>
#include <odyssey.h>
static inline void od_frontend_close(od_client_t *client)
{
assert(client->route == NULL);
assert(client->server == NULL);
od_router_t *router = client->global->router;
od_atomic_u32_dec(&router->clients);
od_io_close(&client->io);
od_client_free(client);
}
int od_frontend_info(od_client_t *client, char *fmt, ...)
{
va_list args;
va_start(args, fmt);
machine_msg_t *msg;
msg = od_frontend_info_msg(client, NULL, fmt, args);
va_end(args);
if (msg == NULL) {
return -1;
}
return od_write(&client->io, msg);
}
int od_frontend_error(od_client_t *client, char *code, char *fmt, ...)
{
va_list args;
va_start(args, fmt);
machine_msg_t *msg;
msg = od_frontend_error_msg(client, NULL, code, fmt, args);
va_end(args);
if (msg == NULL) {
return -1;
}
return od_write(&client->io, msg);
}
int od_frontend_fatal(od_client_t *client, char *code, char *fmt, ...)
{
va_list args;
va_start(args, fmt);
machine_msg_t *msg;
msg = od_frontend_fatal_msg(client, NULL, code, fmt, args);
va_end(args);
if (msg == NULL)
return -1;
return od_write(&client->io, msg);
}
static inline int od_frontend_error_fwd(od_client_t *client)
{
od_server_t *server = client->server;
assert(server != NULL);
assert(server->error_connect != NULL);
kiwi_fe_error_t error;
int rc;
rc = kiwi_fe_read_error(machine_msg_data(server->error_connect),
machine_msg_size(server->error_connect),
&error);
if (rc == -1)
return -1;
char text[512];
int text_len;
text_len =
od_snprintf(text, sizeof(text), "odyssey: %s%.*s: %s",
client->id.id_prefix, (signed)sizeof(client->id.id),
client->id.id, error.message);
int detail_len = error.detail ? strlen(error.detail) : 0;
int hint_len = error.hint ? strlen(error.hint) : 0;
machine_msg_t *msg;
msg = kiwi_be_write_error_as(NULL, error.severity, error.code,
error.detail, detail_len, error.hint,
hint_len, text, text_len);
if (msg == NULL)
return -1;
return od_write(&client->io, msg);
}
static inline bool
od_frontend_error_is_too_many_connections(od_client_t *client)
{
od_server_t *server = client->server;
assert(server != NULL);
if (server->error_connect == NULL)
return false;
kiwi_fe_error_t error;
int rc;
rc = kiwi_fe_read_error(machine_msg_data(server->error_connect),
machine_msg_size(server->error_connect),
&error);
if (rc == -1)
return false;
return strcmp(error.code, KIWI_TOO_MANY_CONNECTIONS) == 0;
}
static int od_frontend_startup(od_client_t *client)
{
od_instance_t *instance = client->global->instance;
machine_msg_t *msg;
for (uint32_t startup_attempt = 0;
startup_attempt < MAX_STARTUP_ATTEMPTS; startup_attempt++) {
msg = od_read_startup(
&client->io,
client->config_listen->client_login_timeout);
if (msg == NULL)
goto error;
int rc = kiwi_be_read_startup(machine_msg_data(msg),
machine_msg_size(msg),
&client->startup, &client->vars);
machine_msg_free(msg);
if (rc == -1)
goto error;
if (!client->startup.unsupported_request)
break;
/* not supported 'N' */
msg = machine_msg_create(sizeof(uint8_t));
if (msg == NULL)
return -1;
uint8_t *type = machine_msg_data(msg);
*type = 'N';
rc = od_write(&client->io, msg);
if (rc == -1) {
od_error(&instance->logger,
"unsupported protocol (gssapi)", client, NULL,
"write error: %s", od_io_error(&client->io));
return -1;
}
od_debug(&instance->logger, "unsupported protocol (gssapi)",
client, NULL, "ignoring");
}
/* client ssl request */
int rc = od_tls_frontend_accept(client, &instance->logger,
client->config_listen, client->tls);
if (rc == -1)
goto error;
if (!client->startup.is_ssl_request) {
rc = od_compression_frontend_setup(
client, client->config_listen, &instance->logger);
if (rc == -1)
return -1;
return 0;
}
/* read startup-cancel message followed after ssl
* negotiation */
assert(client->startup.is_ssl_request);
msg = od_read_startup(&client->io,
client->config_listen->client_login_timeout);
if (msg == NULL)
return -1;
rc = kiwi_be_read_startup(machine_msg_data(msg), machine_msg_size(msg),
&client->startup, &client->vars);
machine_msg_free(msg);
if (rc == -1)
goto error;
rc = od_compression_frontend_setup(client, client->config_listen,
&instance->logger);
if (rc == -1) {
return -1;
}
return 0;
error:
od_debug(&instance->logger, "startup", client, NULL,
"startup packet read error");
od_cron_t *cron = client->global->cron;
od_atomic_u64_inc(&cron->startup_errors);
return -1;
}
static inline od_frontend_status_t
od_frontend_attach(od_client_t *client, char *context,
kiwi_params_t *route_params)
{
od_instance_t *instance = client->global->instance;
od_router_t *router = client->global->router;
od_route_t *route = client->route;
if (route->rule->pool->reserve_prepared_statement) {
client->relay.require_full_prep_stmt = 1;
}
bool wait_for_idle = false;
for (;;) {
od_router_status_t status;
status = od_router_attach(router, client, wait_for_idle);
if (status != OD_ROUTER_OK) {
if (status == OD_ROUTER_ERROR_TIMEDOUT) {
od_error(&instance->logger, "router", client,
NULL,
"server pool wait timed out, closing");
return OD_EATTACH_TOO_MANY_CONNECTIONS;
}
return OD_EATTACH;
}
od_server_t *server = client->server;
if (server->io.io && !machine_connected(server->io.io)) {
od_log(&instance->logger, context, client, server,
"server disconnected, close connection and retry attach");
od_router_close(router, client);
continue;
}
od_debug(&instance->logger, context, client, server,
"client %s%.*s attached to %s%.*s",
client->id.id_prefix,
(int)sizeof(client->id.id_prefix), client->id.id,
server->id.id_prefix,
(int)sizeof(server->id.id_prefix), server->id.id);
assert(od_server_synchronized(server));
assert(server->relay.iov == 0 ||
!machine_iov_pending(server->relay.iov));
/* connect to server, if necessary */
if (server->io.io) {
return OD_OK;
}
int rc;
od_atomic_u32_inc(&router->servers_routing);
rc = od_backend_connect(server, context, route_params, client);
od_atomic_u32_dec(&router->servers_routing);
if (rc == -1) {
/* In case of 'too many connections' error, retry attach attempt by
* waiting for a idle server connection for pool_timeout ms
*/
wait_for_idle =
route->rule->pool->timeout > 0 &&
od_frontend_error_is_too_many_connections(
client);
if (wait_for_idle) {
od_router_close(router, client);
if (instance->config.server_login_retry) {
machine_sleep(
instance->config
.server_login_retry);
}
continue;
}
return OD_ESERVER_CONNECT;
}
return OD_OK;
}
}
static inline od_frontend_status_t
od_frontend_attach_and_deploy(od_client_t *client, char *context)
{
/* attach and maybe connect server */
od_frontend_status_t status;
status = od_frontend_attach(client, context, NULL);
if (status != OD_OK)
return status;
od_server_t *server = client->server;
/* configure server using client parameters */
int rc;
rc = od_deploy(client, context);
if (rc == -1)
return OD_ESERVER_WRITE;
/* set number of replies to discard */
server->deploy_sync = rc;
od_server_sync_request(server, server->deploy_sync);
return OD_OK;
}
static inline od_frontend_status_t od_frontend_setup_params(od_client_t *client)
{
od_instance_t *instance = client->global->instance;
od_router_t *router = client->global->router;
od_route_t *route = client->route;
/* ensure route has cached server parameters */
int rc;
rc = kiwi_params_lock_count(&route->params);
if (rc == 0) {
kiwi_params_t route_params;
kiwi_params_init(&route_params);
od_frontend_status_t status;
status = od_frontend_attach(client, "setup", &route_params);
if (status != OD_OK) {
kiwi_params_free(&route_params);
return status;
}
// close backend connection
od_router_close(router, client);
/* There is possible race here, so we will discard our
* attempt if params are already set */
rc = kiwi_params_lock_set_once(&route->params, &route_params);
if (!rc)
kiwi_params_free(&route_params);
}
od_debug(&instance->logger, "setup", client, NULL, "sending params:");
/* send parameters set by client or cached by the route */
kiwi_param_t *param = route->params.params.list;
machine_msg_t *stream = machine_msg_create(0);
if (stream == NULL)
return OD_EOOM;
while (param) {
kiwi_var_type_t type;
type = kiwi_vars_find(&client->vars, kiwi_param_name(param),
param->name_len);
kiwi_var_t *var;
var = kiwi_vars_get(&client->vars, type);
machine_msg_t *msg;
if (var) {
msg = kiwi_be_write_parameter_status(stream, var->name,
var->name_len,
var->value,
var->value_len);
od_debug(&instance->logger, "setup", client, NULL,
" %.*s = %.*s", var->name_len, var->name,
var->value_len, var->value);
} else {
msg = kiwi_be_write_parameter_status(
stream, kiwi_param_name(param), param->name_len,
kiwi_param_value(param), param->value_len);
od_debug(&instance->logger, "setup", client, NULL,
" %.*s = %.*s", param->name_len,
kiwi_param_name(param), param->value_len,
kiwi_param_value(param));
}
if (msg == NULL) {
machine_msg_free(stream);
return OD_EOOM;
}
param = param->next;
}
rc = od_write(&client->io, stream);
if (rc == -1)
return OD_ECLIENT_WRITE;
return OD_OK;
}
static inline od_frontend_status_t od_frontend_setup(od_client_t *client)
{
od_instance_t *instance = client->global->instance;
od_route_t *route = client->route;
/* set paremeters */
od_frontend_status_t status;
status = od_frontend_setup_params(client);
if (status != OD_OK)
return status;
if (route->rule->pool->reserve_prepared_statement) {
if (od_client_init_hm(client) != OK_RESPONSE) {
od_log(&instance->logger, "setup", client, NULL,
"failed to initialize hash map for prepared statements");
return OD_EOOM;
}
}
/* write key data message */
machine_msg_t *stream;
machine_msg_t *msg;
msg = kiwi_be_write_backend_key_data(NULL, client->key.key_pid,
client->key.key);
if (msg == NULL)
return OD_EOOM;
stream = msg;
/* write ready message */
msg = kiwi_be_write_ready(stream, 'I');
if (msg == NULL) {
machine_msg_free(stream);
return OD_EOOM;
}
int rc;
rc = od_write(&client->io, stream);
if (rc == -1)
return OD_ECLIENT_WRITE;
if (instance->config.log_session) {
client->time_setup = machine_time_us();
od_log(&instance->logger, "setup", client, NULL,
"login time: %d microseconds",
(client->time_setup - client->time_accept));
od_log(&instance->logger, "setup", client, NULL,
"client connection from %s to route %s.%s accepted",
client->peer, route->rule->db_name,
route->rule->user_name);
}
return OD_OK;
}
static inline od_frontend_status_t od_frontend_local_setup(od_client_t *client)
{
machine_msg_t *stream;
stream = machine_msg_create(0);
if (stream == NULL)
goto error;
/* client parameters */
machine_msg_t *msg;
char data[128];
int data_len;
/* current version and build */
data_len =
od_snprintf(data, sizeof(data), "%s-%s-%s", OD_VERSION_NUMBER,
OD_VERSION_GIT, OD_VERSION_BUILD);
msg = kiwi_be_write_parameter_status(stream, "server_version", 15, data,
data_len + 1);
if (msg == NULL)
goto error;
msg = kiwi_be_write_parameter_status(stream, "server_encoding", 16,
"UTF-8", 6);
if (msg == NULL)
goto error;
msg = kiwi_be_write_parameter_status(stream, "client_encoding", 16,
"UTF-8", 6);
if (msg == NULL)
goto error;
msg = kiwi_be_write_parameter_status(stream, "DateStyle", 10, "ISO", 4);
if (msg == NULL)
goto error;
msg = kiwi_be_write_parameter_status(stream, "TimeZone", 9, "GMT", 4);
if (msg == NULL)
goto error;
/* ready message */
msg = kiwi_be_write_ready(stream, 'I');
if (msg == NULL)
goto error;
int rc;
rc = od_write(&client->io, stream);
if (rc == -1)
return OD_ECLIENT_WRITE;
return OD_OK;
error:
if (stream)
machine_msg_free(stream);
return OD_EOOM;
}
static inline bool od_eject_conn_with_rate(od_client_t *client,
od_server_t *server,
od_instance_t *instance)
{
if (server == NULL) {
/* server is null - client was never attached to any server so its ok to eject this conn */
return true;
}
od_thread_global **gl = od_thread_global_get();
if (gl == NULL) {
od_log(&instance->logger, "shutdown", client, server,
"drop client connection on restart, unable to throttle (wid %d)",
(*gl)->wid);
/* this is clearly something bad, TODO: handle properly */
return true;
}
od_conn_eject_info *info = (*gl)->info;
struct timeval tv;
gettimeofday(&tv, NULL);
bool res = false;
pthread_mutex_lock(&info->mu);
{
if (info->last_conn_drop_ts + /* 1 sec */ 1 > tv.tv_sec) {
od_log(&instance->logger, "shutdown", client, server,
"delay drop client connection on restart, last drop was too recent (wid %d, last drop %d, curr time %d)",
(*gl)->wid, info->last_conn_drop_ts, tv.tv_sec);
} else {
info->last_conn_drop_ts = tv.tv_sec;
res = true;
od_log(&instance->logger, "shutdown", client, server,
"drop client connection on restart (wid %d, last eject %d, curr time %d)",
(*gl)->wid, info->last_conn_drop_ts, tv.tv_sec);
}
}
pthread_mutex_unlock(&info->mu);
return res;
}
static inline bool od_eject_conn_with_timeout(od_client_t *client,
__attribute__((unused))
od_server_t *server,
uint64_t timeout)
{
assert(server != NULL);
od_dbg_printf_on_dvl_lvl(1, "current time %lld, drop horizon %lld\n",
machine_time_us(),
client->time_last_active + timeout);
if (client->time_last_active + timeout < machine_time_us()) {
return true;
}
return false;
}
static inline bool od_should_drop_connection(od_client_t *client,
od_server_t *server)
{
od_instance_t *instance = client->global->instance;
switch (client->rule->pool->pool) {
case OD_RULE_POOL_SESSION: {
if (od_unlikely(client->rule->pool->client_idle_timeout)) {
// as we do not unroute client in session pooling after transaction block etc
// we should consider this case separately
// general logic is: if client do nothing long enough we can assume this is just a stale connection
// but we need to ensure this connection was initialized etc
if (od_unlikely(
server != NULL && !server->is_transaction &&
/* case when we are out of any transactional block ut perform some stmt */
od_server_synchronized(server))) {
if (od_eject_conn_with_timeout(
client, server,
client->rule->pool
->client_idle_timeout)) {
od_log(&instance->logger, "shutdown",
client, server,
"drop idle client connection on due timeout %d sec",
client->rule->pool
->client_idle_timeout);
return true;
}
}
}
if (od_unlikely(
client->rule->pool->idle_in_transaction_timeout)) {
// the same as above but we are going to drop client inside transaction block
if (server != NULL && server->is_transaction &&
/*server is sync - that means client executed some stmts and got get result, and now just... do nothing */
od_server_synchronized(server)) {
if (od_eject_conn_with_timeout(
client, server,
client->rule->pool
->idle_in_transaction_timeout)) {
od_log(&instance->logger, "shutdown",
client, server,
"drop idle in transaction connection on due timeout %d sec",
client->rule->pool
->idle_in_transaction_timeout);
return true;
}
}
}
}
/* fall through */
case OD_RULE_POOL_STATEMENT:
case OD_RULE_POOL_TRANSACTION: {
//TODO:: drop no more than X connection per sec/min/whatever
if (od_likely(instance->shutdown_worker_id ==
INVALID_COROUTINE_ID)) {
// try to optimize likely path
return false;
}
if (od_unlikely(client->rule->storage->storage_type ==
OD_RULE_STORAGE_LOCAL)) {
/* local server is not very important (db like console, pgbouncer used for stats)*/
return true;
}
if (od_unlikely(server == NULL)) {
return od_eject_conn_with_rate(client, server,
instance);
}
if (server->state ==
OD_SERVER_ACTIVE /* we can drop client that are just connected and do not perform any queries */
&& !od_server_synchronized(server)) {
/* most probably we are not in transcation, but still executing some stmt */
return false;
}
if (od_unlikely(!server->is_transaction)) {
return od_eject_conn_with_rate(client, server,
instance);
}
return false;
} break;
default:
return false;
}
}
static od_frontend_status_t od_frontend_ctl(od_client_t *client)
{
if (od_atomic_u64_of(&client->killed) == 1) {
return OD_STOP;
}
return OD_OK;
}
static od_frontend_status_t od_frontend_local(od_client_t *client)
{
od_instance_t *instance = client->global->instance;
for (;;) {
machine_msg_t *msg = NULL;
for (;;) {
/* local server is alwys null */
if (od_should_drop_connection(client, NULL)) {
/* Odyssey is in a state of completion, we done
* the last client's request and now we can drop the connection */
/* a sort of EAGAIN */
return OD_ECLIENT_READ;
}
/* one minute */
msg = od_read(&client->io, 60000);
if (machine_timedout()) {
/* retry wait to recheck exit condition */
assert(msg == NULL);
continue;
}
if (msg == NULL) {
return OD_ECLIENT_READ;
} else {
break;
}
}
/* client operations */
od_frontend_status_t status;
status = od_frontend_ctl(client);
if (status != OD_OK)
break;
kiwi_fe_type_t type;
type = *(char *)machine_msg_data(msg);
od_debug(&instance->logger, "local", client, NULL, "%s",
kiwi_fe_type_to_string(type));
if (type == KIWI_FE_TERMINATE) {
machine_msg_free(msg);
break;
}
machine_msg_t *stream = machine_msg_create(0);
if (stream == NULL) {
machine_msg_free(msg);
return OD_EOOM;
}
int rc;
if (type == KIWI_FE_QUERY) {
rc = od_console_query(client, stream,
machine_msg_data(msg),
machine_msg_size(msg));
machine_msg_free(msg);
if (rc == -1) {
machine_msg_free(stream);
return OD_EOOM;
}
} else {
/* unsupported */
machine_msg_free(msg);
od_error(&instance->logger, "local", client, NULL,
"unsupported request '%s'",
kiwi_fe_type_to_string(type));
msg = od_frontend_errorf(client, stream,
KIWI_FEATURE_NOT_SUPPORTED,
"unsupported request '%s'",
kiwi_fe_type_to_string(type));
if (msg == NULL) {
machine_msg_free(stream);
return OD_EOOM;
}
}
/* ready */
msg = kiwi_be_write_ready(stream, 'I');
if (msg == NULL) {
machine_msg_free(stream);
return OD_EOOM;
}
rc = od_write(&client->io, stream);
if (rc == -1) {
return OD_ECLIENT_WRITE;
}
}
return OD_OK;
}
static od_frontend_status_t od_frontend_remote_server(od_relay_t *relay,
char *data, int size)
{
od_client_t *client = relay->on_packet_arg;
od_server_t *server = client->server;
od_route_t *route = client->route;
od_instance_t *instance = client->global->instance;
od_frontend_status_t retstatus;
retstatus = OD_OK;
kiwi_be_type_t type = *data;
if (instance->config.log_debug)
od_debug(&instance->logger, "main", client, server, "%s",
kiwi_be_type_to_string(type));
int is_deploy = od_server_in_deploy(server);
int is_ready_for_query = 0;
int rc;
switch (type) {
case KIWI_BE_ERROR_RESPONSE:
od_backend_error(server, "main", data, size);
break;
case KIWI_BE_PARAMETER_STATUS:
rc = od_backend_update_parameter(server, "main", data, size, 0);
if (rc == -1)
return relay->error_read;
break;
case KIWI_BE_COPY_IN_RESPONSE:
case KIWI_BE_COPY_OUT_RESPONSE:
server->in_out_response_received++;
break;
case KIWI_BE_COPY_DONE:
/* should go after copy out
* states that backend copy ended
*/
server->done_fail_response_received++;
break;
case KIWI_BE_COPY_FAIL:
/*
* states that backend copy failed
*/
return relay->error_write;
case KIWI_BE_READY_FOR_QUERY: {
is_ready_for_query = 1;
od_backend_ready(server, data, size);
/* exactly one RFQ! */
if (od_server_in_sync_point(server)) {
retstatus = OD_SKIP;
}
if (is_deploy)
server->deploy_sync--;
if (!server->synced_settings) {
server->synced_settings = true;
break;
}
/* update server stats */
int64_t query_time = 0;
od_stat_query_end(&route->stats, &server->stats_state,
server->is_transaction, &query_time);
if (instance->config.log_debug && query_time > 0) {
od_debug(&instance->logger, "main", server->client,
server, "query time: %" PRIi64 " microseconds",
query_time);
}
break;
}
case KIWI_BE_PARSE_COMPLETE:
if (route->rule->pool->reserve_prepared_statement) {
// skip msg
retstatus = OD_SKIP;
}
default:
break;
}
/* discard replies during configuration deploy */
if (is_deploy || retstatus == OD_SKIP)
return OD_SKIP;
if (route->id.physical_rep || route->id.logical_rep) {
// do not detach server connection on replication
// the exceptional case in offine: we are going to shut down here
if (server->offline) {
return OD_DETACH;
}
} else {
if (is_ready_for_query && od_server_synchronized(server) &&
server->parse_msg == NULL) {
switch (route->rule->pool->pool) {
case OD_RULE_POOL_STATEMENT:
return OD_DETACH;
case OD_RULE_POOL_TRANSACTION:
if (!server->is_transaction) {
return OD_DETACH;
}
break;
case OD_RULE_POOL_SESSION:
if (server->offline &&
!server->is_transaction) {
return OD_DETACH;
}
break;
}
}
}
return retstatus;
}
static inline od_retcode_t od_frontend_log_query(od_instance_t *instance,
od_client_t *client,
char *data, int size)
{
uint32_t query_len;
char *query;
int rc;
rc = kiwi_be_read_query(data, size, &query, &query_len);
if (rc == -1)
return NOT_OK_RESPONSE;
od_log(&instance->logger, "query", client, NULL, "%.*s", query_len,
query);
return OK_RESPONSE;
}
static inline od_retcode_t od_frontend_log_describe(od_instance_t *instance,
od_client_t *client,
char *data, int size)
{
uint32_t name_len;
char *name;
int rc;
kiwi_fe_describe_type_t t;
rc = kiwi_be_read_describe(data, size, &name, &name_len, &t);
if (rc == -1)
return NOT_OK_RESPONSE;
od_log(&instance->logger, "describe", client, client->server,
"(%s) name: %.*s",
t == KIWI_FE_DESCRIBE_PORTAL ? "portal" : "statement", name_len,
name);
return OK_RESPONSE;
}
static inline od_retcode_t od_frontend_log_execute(od_instance_t *instance,
od_client_t *client,
char *data, int size)
{
uint32_t name_len;
char *name;
int rc;
rc = kiwi_be_read_execute(data, size, &name, &name_len);
if (rc == -1)
return NOT_OK_RESPONSE;
od_log(&instance->logger, "execute", client, client->server,
"name: %.*s", name_len, name);
return OK_RESPONSE;
}
static inline od_retcode_t od_frontend_parse_close(char *data, int size,
char **name,
uint32_t *name_len,
kiwi_fe_close_type_t *type)
{
int rc;
rc = kiwi_be_read_close(data, size, name, name_len, type);
if (rc == -1)
return NOT_OK_RESPONSE;
return OK_RESPONSE;
}
static inline od_retcode_t od_frontend_log_close(od_instance_t *instance,
od_client_t *client,
char *name, uint32_t name_len,
kiwi_fe_close_type_t type)
{
switch (type) {
case KIWI_FE_CLOSE_PORTAL:
od_log(&instance->logger, "close", client, client->server,
"portal, name: %.*s", name_len, name);
return OK_RESPONSE;
case KIWI_FE_CLOSE_PREPARED_STATEMENT:
od_log(&instance->logger, "close", client, client->server,
"prepared statement, name: %.*s", name_len, name);
return OK_RESPONSE;
default:
od_log(&instance->logger, "close", client, client->server,
"unknown close type, name: %.*s", name_len, name);
return NOT_OK_RESPONSE;
}
}
static inline od_retcode_t od_frontend_log_parse(od_instance_t *instance,
od_client_t *client,
char *context, char *data,
int size)
{
uint32_t query_len;
char *query;
uint32_t name_len;
char *name;
int rc;
rc = kiwi_be_read_parse(data, size, &name, &name_len, &query,
&query_len);
if (rc == -1)
return NOT_OK_RESPONSE;
od_log(&instance->logger, context, client, client->server, "%.*s %.*s",
name_len, name, query_len, query);
return OK_RESPONSE;
}
static inline od_retcode_t od_frontend_log_bind(od_instance_t *instance,
od_client_t *client, char *ctx,
char *data, int size)
{
uint32_t name_len;
char *name;
int rc;
rc = kiwi_be_read_bind_stmt_name(data, size, &name, &name_len);
if (rc == -1)
return NOT_OK_RESPONSE;
od_log(&instance->logger, ctx, client, client->server, "bind %.*s",
name_len, name);
return OK_RESPONSE;
}
// 8 hex
#define OD_HASH_LEN 9
static inline machine_msg_t *
od_frontend_rewrite_msg(char *data, int size, int opname_start_offset,
int operator_name_len, char *opname, int opnamelen)
{
machine_msg_t *msg =
machine_msg_create(size - operator_name_len + opnamelen);
char *rewrite_data = machine_msg_data(msg);
// packet header
memcpy(rewrite_data, data, opname_start_offset);
// prefix for opname
od_snprintf(rewrite_data + opname_start_offset, opnamelen, opname);
// rest of msg
memcpy(rewrite_data + opname_start_offset + opnamelen,
data + opname_start_offset + operator_name_len,
size - opname_start_offset - operator_name_len);
// set proper size to package
kiwi_header_set_size((kiwi_header_t *)rewrite_data,
size - operator_name_len + opnamelen);
return msg;
}
static od_frontend_status_t od_frontend_deploy_prepared_stmt(
od_server_t *server, od_relay_t *relay, char *ctx, char *data,
int size /* to adcance or to write? */, od_hash_t body_hash,
char *opname, int opnamelen)
{
od_route_t *route = server->route;
od_instance_t *instance = server->global->instance;
od_client_t *client = server->client;
od_hashmap_elt_t desc;
desc.data = data;