forked from vk-com/kphp-kdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc-proxy.c
2729 lines (2413 loc) · 72.7 KB
/
rpc-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
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
/*
This file is part of VK/KittenPHP-DB-Engine.
VK/KittenPHP-DB-Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
VK/KittenPHP-DB-Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with VK/KittenPHP-DB-Engine. If not, see <http://www.gnu.org/licenses/>.
This program is released under the GPL with the additional exemption
that compiling, linking, and/or using OpenSSL is allowed.
You are free to remove this exemption from derived works.
Copyright 2012-2013 Vkontakte Ltd
2012-2013 Vitaliy Valtman
*/
#define _FILE_OFFSET_BITS 64
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <aio.h>
#include <netdb.h>
#include <stdarg.h>
#include <math.h>
#include <stddef.h>
#include <sys/time.h>
#include <kfs.h>
#include <signal.h>
#include "crc32.h"
#include "md5.h"
#include "kdb-data-common.h"
#include "resolver.h"
#include "net-events.h"
#include "net-buffers.h"
#include "net-connections.h"
#include "server-functions.h"
#include "net-memcache-server.h"
#include "net-memcache-client.h"
#include "net-crypto-aes.h"
#include "net-rpc-server.h"
#include "net-rpc-client.h"
#include "net-rpc-common.h"
#include "rpc-proxy.h"
#include "rpc-proxy-merge.h"
#include "rpc-proxy-merge-diagonal.h"
#include "rpc-proxy-binlog.h"
#include "vv-tree.h"
#include "vv-tl-parse.h"
#include "vv-io.h"
#include "kdb-rpc-proxy-binlog.h"
#include "TL/constants.h"
#include "net-tcp-connections.h"
#include "net-tcp-rpc-server.h"
#include "net-tcp-rpc-client.h"
#include "net-rpc-targets.h"
#define MAX_KEY_LEN 1000
#define MAX_KEY_RETRIES 3
#define MAX_VALUE_LEN (1 << 24)
#define VERSION_STR "rpc-proxy-0.22"
#define TCP_PORT 11213
#define MAX_NET_RES (1L << 16)
#define PING_INTERVAL 30.0
#define STOP_INTERVAL (2 * ping_interval)
#define FAIL_INTERVAL (20 * ping_interval)
#define RESPONSE_FAIL_TIMEOUT 5
#define CONNECT_TIMEOUT 3
#define MAX_USER_FRIENDS 65536
#define DEFAULT_MIN_CONNECTIONS 2
#define DEFAULT_MAX_CONNECTIONS 3
#define SMALL_RESPONSE_THRESHOLD 256
#define BIG_RESPONSE_THRESHOLD 14000
#ifndef COMMIT
#define COMMIT "unknown"
#endif
struct rpc_extension *extensions[MAX_EXTENSIONS];
struct rpc_schema *schemas[MAX_SCHEMAS];
int extensions_num;
int schemas_num;
extern int rpc_disable_crc32_check;
double ping_interval = PING_INTERVAL;
extern int binlog_mode_on;
int tcp_buffers = 0;
/*
*
* MEMCACHED PORT
*
*/
int rpc_proxy_stats (struct connection *c);
int rpc_check_ready (struct connection *c);
int rpc_proxy_client_ready (struct connection *c);
int tcp_rpc_proxy_client_ready (struct connection *c);
int rpc_proxy_close_conn (struct connection *c, int who);
int rpc_proxy_udp_execute (struct udp_msg *msg);
int rpc_proxy_tcp_execute (struct connection *c, int op, struct raw_message *msg);
struct memcache_server_functions mc_proxy_inbound = {
.mc_store = mcs_store,
.mc_get_start = mcs_get_start,
.mc_get = mcs_get,
.mc_get_end = mcs_get_end,
.mc_incr = mcs_incr,
.mc_delete = mcs_delete,
.mc_version = mcs_version,
.mc_stats = rpc_proxy_stats,
.mc_check_perm = mcs_default_check_perm,
.mc_init_crypto = mcs_init_crypto
};
struct rpc_server_functions rpc_proxy_inbound = {
.execute = rpc_proxy_server_execute,
.check_ready = rpc_check_ready,
.flush_packet = flush_later,
.rpc_check_perm = rpcs_default_check_perm,
.rpc_init_crypto = rpcs_init_crypto,
.rpc_close = rpc_proxy_close_conn,
.memcache_fallback_type = &ct_memcache_server,
.memcache_fallback_extra = &mc_proxy_inbound
};
struct rpc_client_functions rpc_proxy_outbound = {
.execute = rpc_proxy_client_execute,
.check_ready = rpc_check_ready,
.flush_packet = flush_later,
.rpc_check_perm = rpcc_default_check_perm,
//.rpc_check_perm = 0,
.rpc_init_crypto = rpcc_init_crypto,
.rpc_start_crypto = rpcc_start_crypto,
.rpc_ready = rpc_proxy_client_ready,
};
struct tcp_rpc_server_functions rpc_proxy_tcp = {
.execute = rpc_proxy_tcp_execute,
.check_ready = rpc_check_ready,
.flush_packet = flush_later,
.rpc_check_perm = tcp_rpcs_default_check_perm,
.rpc_init_crypto = tcp_rpcs_init_crypto,
.rpc_close = rpc_proxy_close_conn,
.memcache_fallback_type = &ct_memcache_server,
.memcache_fallback_extra = &mc_proxy_inbound
};
struct tcp_rpc_client_functions rpc_proxy_tcp_outbound = {
.execute = rpc_proxy_tcp_execute,
.check_ready = rpc_check_ready,
.flush_packet = flush_later,
.rpc_check_perm = tcp_rpcc_default_check_perm,
//.rpc_check_perm = 0,
.rpc_init_crypto = tcp_rpcc_init_crypto,
.rpc_start_crypto = tcp_rpcc_start_crypto,
.rpc_ready = tcp_rpc_proxy_client_ready,
};
struct udp_functions rpc_proxy_udp_server = {
.magic = UDP_FUNC_MAGIC,
.title = "rpc_proxy udp server",
.process_msg = udp_target_process_msg
};
struct udp_target_methods rpc_proxy_udp_server_methods = {
.on_receive = rpc_proxy_udp_execute
};
int backlog = BACKLOG, port = TCP_PORT, maxconn = MAX_CONNECTIONS, daemonize = 0;
struct in_addr settings_addr;
int verbosity = 0, test_mode = 0;
int enable_ipv6;
int quit_steps;
int sfd;
char *fnames[3];
int fd[3];
long long fsize[3];
// unsigned char is_letter[256];
char *progname = "rpc-proxy", *username, *logname;
char extension_name[16];
int start_time;
int only_first_cluster;
extern int double_receive_count;
char *aes_pwd_file;
long long forwarded_queries;
long long received_answers;
long long received_errors;
long long timeouted_queries;
long long immediate_errors;
long long active_queries;
long long received_expired_answers;
long long skipped_answers;
long long sent_answers;
long long received_bad_answers;
long long received_bad_queries;
long long diagonal_queries;
int binlog_mode_on;
static struct rpc_current_query _CQ;
struct rpc_current_query *CQ = &_CQ;
struct rpc_query *default_create_rpc_query (long long new_qid);
struct rpc_query *default_double_receive_create_rpc_query (long long new_qid);
#define STATS_BUFF_SIZE (1 << 20)
int stats_buff_len;
char stats_buff[STATS_BUFF_SIZE];
static double get_double_time_since_epoch(void) __attribute__ ((unused));
static double get_double_time_since_epoch(void) {
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec + 1e-6 * tv.tv_usec;
}
int rpc_check_ready (struct connection *c) {
double first_query_time = 0;
double adj_precise_now = precise_now;
if (c->status == conn_connecting || (c->last_response_time == 0 && (c->status == conn_wait_answer || c->status == conn_reading_answer)) || RPCS_DATA(c)->in_packet_num < 0) {
if (c->last_query_sent_time < adj_precise_now - CONNECT_TIMEOUT) {
fail_connection (c, -6);
return cr_failed;
}
return cr_notyet;
}
if (c->last_query_sent_time < adj_precise_now - PING_INTERVAL) {
net_rpc_send_ping (c, lrand48 ());
c->last_query_sent_time = precise_now;
}
if (c->last_response_time < adj_precise_now - FAIL_INTERVAL || c->unreliability > 5000) {
if (verbosity > 1 && c->ready != cr_failed) {
fprintf (stderr, "changing connection %d readiness from %d to %d [FAILED] fq=%.03f lq=%.03f lqt=%.03f lr=%.03f now=%.03f unr=%d\n", c->fd, c->ready, cr_failed, first_query_time, c->last_query_sent_time, c->last_query_timeout, c->last_response_time, precise_now, c->unreliability);
}
fail_connection (c, -5);
return c->ready = cr_failed;
}
if (c->last_response_time < adj_precise_now - STOP_INTERVAL || c->unreliability > 2000) {
if (verbosity > 1 && c->ready != cr_stopped) {
fprintf (stderr, "changing connection %d readiness from %d to %d [STOPPED] fq=%.03f lq=%.03f lqt=%.03f lr=%.03f now=%.03f unr=%d\n", c->fd, c->ready, cr_stopped, first_query_time, c->last_query_sent_time, c->last_query_timeout, c->last_response_time, precise_now, c->unreliability);
}
return c->ready = cr_stopped;
}
if (!(c->flags & C_FAILED) && c->last_response_time > 0 && (c->status == conn_wait_answer || c->status == conn_reading_answer)) {
if (verbosity > 1 && c->ready != cr_ok) {
fprintf (stderr, "changing connection %d readiness from %d to %d [OK] fq=%.03f lq=%.03f lqt=%.03f lr=%.03f now=%.03f unr=%d\n", c->fd, c->ready, cr_ok, first_query_time, c->last_query_sent_time, c->last_query_timeout, c->last_response_time, precise_now, c->unreliability);
}
return c->ready = cr_ok;
}
return c->ready = cr_failed;
}
int rpc_proxy_client_ready (struct connection *c) {
c->last_query_sent_time = precise_now;
c->last_response_time = precise_now;
c->unreliability = 0;
assert (c->target);
if (c->target->custom_field == -1) {
c->target->custom_field = RPCC_DATA(c)->remote_pid.ip;
rpc_target_insert_target_ext (c->target, c->target->custom_field);
} else {
unsigned ip = RPCC_DATA(c)->remote_pid.ip;
assert (c->target->custom_field == ip || (c->target->custom_field == 0 && ip == PID.ip) || (c->target->custom_field == PID.ip && ip == 0));
}
return 0;
}
int tcp_rpc_proxy_client_ready (struct connection *c) {
c->last_query_sent_time = precise_now;
c->last_response_time = precise_now;
c->unreliability = 0;
assert (c->target);
if (c->target->custom_field == -1) {
c->target->custom_field = TCP_RPC_DATA(c)->remote_pid.ip;
rpc_target_insert_target_ext (c->target, c->target->custom_field);
} else {
unsigned ip = TCP_RPC_DATA(c)->remote_pid.ip;
assert (c->target->custom_field == ip || (c->target->custom_field == 0 && ip == PID.ip) || (c->target->custom_field == PID.ip && ip == 0));
}
return 0;
}
int rpc_proxy_close_conn (struct connection *c, int who) {
rpc_target_delete_conn (c);
return 0;
}
void rpc_extension_add (struct rpc_extension *E) {
assert (extensions_num < MAX_EXTENSIONS);
E->num = extensions_num;
extensions[extensions_num ++] = E;
}
void rpc_schema_add (struct rpc_schema *E) {
assert (schemas_num < MAX_SCHEMAS);
E->num = schemas_num;
schemas[schemas_num ++] = E;
}
/* {{{ */
void __conn_init_store (struct rpc_cluster_bucket *B, void *c, long long qid) {
assert (c);
if (!tcp_buffers) {
tl_store_init (c, qid);
} else {
tl_store_init_tcp_raw_msg (c, qid);
}
}
unsigned __conn_get_host (struct rpc_cluster_bucket *B) {
return B->T->custom_field;
}
int __conn_get_port (struct rpc_cluster_bucket *B) {
return B->T->port;
}
long long __conn_get_actor (struct rpc_cluster_bucket *B) {
return B->A;
}
void *__conn_get_conn (struct rpc_cluster_bucket *B) {
if (!B->RT) {
if (B->T->custom_field != -1) {
rpc_target_insert_target_ext (B->T, B->T->custom_field);
B->RT = rpc_target_lookup_target (B->T);
} else {
return 0;
}
}
return rpc_target_choose_connection (B->RT, 0);
}
int __conn_get_multi_conn (struct rpc_cluster_bucket *B, void **buf, int n) {
if (!B->RT) {
if (B->T->custom_field != -1) {
rpc_target_insert_target_ext (B->T, B->T->custom_field);
B->RT = rpc_target_lookup_target (B->T);
} else {
return 0;
}
}
return rpc_target_choose_random_connections (B->RT, 0, n, (void *)buf);
}
int __conn_get_state (struct rpc_cluster_bucket *B) {
if (!B->RT) {
if (B->T->custom_field != -1) {
rpc_target_insert_target_ext (B->T, B->T->custom_field);
B->RT = rpc_target_lookup_target (B->T);
} else {
return -1;
}
}
return rpc_target_get_state (B->RT, 0);
}
enum tl_type __conn_get_type (struct rpc_cluster_bucket *B) {
return tl_type_conn;
}
struct rpc_cluster_bucket_methods __conn_methods = {
.init_store = __conn_init_store,
.get_host = __conn_get_host,
.get_port = __conn_get_port,
.get_actor = __conn_get_actor,
.get_conn = __conn_get_conn,
.get_multi_conn = __conn_get_multi_conn,
.get_state = __conn_get_state,
.get_type = __conn_get_type
};
void __udp_init_store (struct rpc_cluster_bucket *B, void *c, long long qid) {
struct udp_target *S = udp_target_set_choose_target (B->S);
tl_store_init_raw_msg (S, qid);
}
unsigned __udp_get_host (struct rpc_cluster_bucket *B) {
return B->S->ip;
}
int __udp_get_port (struct rpc_cluster_bucket *B) {
return B->S->port;
}
long long __udp_get_actor (struct rpc_cluster_bucket *B) {
return B->A;
}
void *__udp_get_conn (struct rpc_cluster_bucket *B) {
struct udp_target *S = udp_target_set_choose_target (B->S);
if (!S) { return 0; }
return (void *)1l;
}
int __udp_get_multi_conn (struct rpc_cluster_bucket *B, void **buf, int n) {
struct udp_target *S = udp_target_set_choose_target (B->S);
if (!S) { return 0; }
*buf = (void *)1l;
return 1;
}
int __udp_get_state (struct rpc_cluster_bucket *B) {
struct udp_target *S = udp_target_set_choose_target (B->S);
if (!S) { return -1; }
if (S->state == udp_ok) {
return 1;
} else if (S->state == udp_stopped || S->state == udp_unknown) {
return 0;
} else {
return -1;
}
}
enum tl_type __udp_get_type (struct rpc_cluster_bucket *B) {
return tl_type_raw_msg;
}
struct rpc_cluster_bucket_methods __udp_methods = {
.init_store = __udp_init_store,
.get_host = __udp_get_host,
.get_port = __udp_get_port,
.get_actor = __udp_get_actor,
.get_conn = __udp_get_conn,
.get_multi_conn = __udp_get_multi_conn,
.get_type = __udp_get_type
};
/* }}} */
/* parse config functions {{{ */
/* file utils */
int open_file (int x, char *fname, int creat) {
fnames[x] = fname;
fd[x] = open (fname, creat > 0 ? O_RDWR | O_CREAT : O_RDONLY, 0600);
if (creat < 0 && fd[x] < 0) {
if (fd[x] < 0) {
fprintf (stderr, "%s: cannot open %s: %m\n", progname, fname);
}
return -1;
}
if (fd[x] < 0) {
fprintf (stderr, "%s: cannot open %s: %m\n", progname, fname);
exit(1);
}
fsize[x] = lseek (fd[x], 0, SEEK_END);
if (fsize[x] < 0) {
fprintf (stderr, "%s: cannot seek %s: %m\n", progname, fname);
exit(2);
}
lseek (fd[x], 0, SEEK_SET);
if (verbosity) {
fprintf (stderr, "opened file %s, fd=%d, size=%lld\n", fname, fd[x], fsize[x]);
}
return fd[x];
}
int default_fun_on_answer (void **IP, void **Data);
int default_fun_on_alarm (void **IP, void **Data);
int default_fun_on_free (void **IP, void **Data);
int default_on_net_fail (void **IP, void **Data);
/*
*
* CONFIGURATION PARSER
*
*/
#define IN_CLUSTER_CONFIG 1
#define IN_CLUSTER_CONNECTED 2
/*struct cluster_server {
int id;
char *hostname;
int port;
struct in_addr addr;
struct connection *c;
int conn, rconn;
int in_cluster;
int conn_retries;
int reconnect_time;
int list_len;
int list_first;
};*/
struct conn_target default_rpc_ct = {
.min_connections = DEFAULT_MIN_CONNECTIONS,
.max_connections = DEFAULT_MAX_CONNECTIONS,
.type = &ct_rpc_client,
.extra = &rpc_proxy_outbound,
.reconnect_timeout = 1
};
struct conn_target default_tcp_rpc_ct = {
.min_connections = DEFAULT_MIN_CONNECTIONS,
.max_connections = DEFAULT_MAX_CONNECTIONS,
.type = &ct_tcp_rpc_client,
.extra = &rpc_proxy_tcp_outbound,
.reconnect_timeout = 1
};
struct rpc_config Config[2], *CurConf = Config, *NextConf = Config + 1;
DEFINE_TREE_STD_ALLOC (rpc_cluster,struct rpc_cluster *,std_ll_ptr_compare,int,std_int_compare)
tree_rpc_cluster_t *rpc_cluster_tree;
struct rpc_cluster *CC;
#define MAX_CONFIG_SIZE (1 << 20)
char config_buff[MAX_CONFIG_SIZE+4], *config_filename, *cfg_start, *cfg_end, *cfg_cur;
int config_bytes, cfg_lno, cfg_lex = -1;
static int cfg_skipspc (void) {
while (*cfg_cur == ' ' || *cfg_cur == 9 || *cfg_cur == 13 || *cfg_cur == 10 || *cfg_cur == '#') {
if (*cfg_cur == '#') {
do cfg_cur++; while (*cfg_cur && *cfg_cur != 10);
continue;
}
if (*cfg_cur == 10) {
cfg_lno++;
}
cfg_cur++;
}
return (unsigned char) *cfg_cur;
}
static int cfg_skspc (void) {
while (*cfg_cur == ' ' || *cfg_cur == 9) {
cfg_cur++;
}
return (unsigned char) *cfg_cur;
}
static int cfg_getlex (void) {
switch (cfg_skipspc()) {
case ';':
case ':':
case '{':
case '}':
return cfg_lex = *cfg_cur++;
case 'c':
if (!memcmp (cfg_cur, "cluster", 7)) {
cfg_cur += 7;
return cfg_lex = 'c';
}
break;
case 'e':
if (!memcmp (cfg_cur, "extension_params", 16)) {
cfg_cur += 16;
return cfg_lex = 'E';
}
if (!memcmp (cfg_cur, "extension", 9)) {
cfg_cur += 9;
return cfg_lex = 'e';
}
break;
case 'i':
if (!memcmp (cfg_cur, "id", 2)) {
cfg_cur += 2;
return cfg_lex = 'i';
}
break;
case 'm':
if (!memcmp (cfg_cur, "mode", 4)) {
cfg_cur += 4;
return cfg_lex = 'm';
}
if (!memcmp (cfg_cur, "min_connections", 15)) {
cfg_cur += 15;
return cfg_lex = 'x';
}
if (!memcmp (cfg_cur, "max_connections", 15)) {
cfg_cur += 15;
return cfg_lex = 'X';
}
break;
case 's':
if (!memcmp (cfg_cur, "server", 6)) {
cfg_cur += 6;
return cfg_lex = 's';
}
if (!memcmp (cfg_cur, "step", 4)) {
cfg_cur += 4;
return cfg_lex = 'T';
}
if (!memcmp (cfg_cur, "schema", 6)) {
cfg_cur += 6;
return cfg_lex = 'S';
}
break;
case 't':
if (!memcmp (cfg_cur, "timeout", 7)) {
cfg_cur += 7;
return cfg_lex = 't';
}
break;
case 'n':
if (!memcmp (cfg_cur, "new_id", 6)) {
cfg_cur += 6;
return cfg_lex = 'n';
}
break;
case 'p':
if (!memcmp (cfg_cur, "proto", 5)) {
cfg_cur += 5;
return cfg_lex = 'p';
}
break;
case 0:
return cfg_lex = 0;
}
return cfg_lex = -1;
}
static int cfg_getword (void) {
cfg_skspc();
char *s = cfg_cur;
if (*s != '[') {
while ((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || (*s >= '0' && *s <= '9') || *s == '.' || *s == '-' || *s == '_') {
s++;
}
} else {
s++;
while ((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || (*s >= '0' && *s <= '9') || *s == '.' || *s == '-' || *s == '_' || *s == ':') {
s++;
}
if (*s == ']') {
s++;
}
}
return s - cfg_cur;
}
static long long cfg_getint (void) {
cfg_skspc();
char *s = cfg_cur;
long long x = 0;
while (*s >= '0' && *s <= '9') {
x = x*10 + (*s++ - '0');
}
cfg_cur = s;
return x;
}
static int syntax (const char *msg) {
char *ptr = cfg_cur, *end = ptr + 20;
if (!msg) {
msg = "syntax error";
}
if (cfg_lno) {
fprintf (stderr, "%s:%d: ", config_filename, cfg_lno);
}
fprintf (stderr, "fatal: %s near ", msg);
while (*ptr && *ptr != 13 && *ptr != 10) {
putc (*ptr++, stderr);
if (ptr > end) {
fprintf (stderr, " ...");
break;
}
}
putc ('\n', stderr);
return -1;
}
static void syntax_warning (const char *msg, ...) __attribute__ ((unused));
static void syntax_warning (const char *msg, ...) {
char *ptr = cfg_cur, *end = ptr + 20;
va_list args;
if (cfg_lno) {
fprintf (stderr, "%s:%d: ", config_filename, cfg_lno);
}
fputs ("warning: ", stderr);
va_start (args, msg);
vfprintf (stderr, msg, args);
va_end (args);
fputs (" near ", stderr);
while (*ptr && *ptr != 13 && *ptr != 10) {
putc (*ptr++, stderr);
if (ptr > end) {
fputs (" ...", stderr);
break;
}
}
putc ('\n', stderr);
}
static int expect_lexem (int lexem) {
if (cfg_lex != lexem) {
static char buff[32];
sprintf (buff, "%c expected", lexem);
return syntax (buff);
} else {
return 0;
}
}
#define Expect(l) { int t = expect_lexem (l); if (t < 0) { return t; } }
void clear_config (struct rpc_config *RC, int do_destroy_targets) {
int i, j;
for (i = 0; i < RC->clusters_num; i++) {
struct rpc_cluster *C = &RC->Clusters[i];
if (do_destroy_targets && (C->proto != PROTO_UDP)) {
for (j = 0; j < C->tot_buckets; j++) {
vkprintf (1, "destroying target " IP_PRINT_STR ":%d\n", IP_TO_PRINT (C->buckets[j].methods->get_host (&C->buckets[j])), C->buckets[j].methods->get_port (&C->buckets[j]));
destroy_target (C->buckets[j].T);
}
memset (C->buckets, 0, C->tot_buckets * sizeof (*C->buckets));
}
for (j = 0; j < C->extensions_num; j++) {
if (extensions[C->extensions[j]]->del) {
extensions[C->extensions[j]]->del (RC, C);
}
}
if (schemas[C->schema_num]->del) {
schemas[C->schema_num]->del (RC, C);
}
if (C->cluster_name) {
zfree (C->cluster_name, strlen (C->cluster_name) + 1);
C->cluster_name = 0;
}
}
RC->ConfigServersCount = 0;
RC->clusters_num = 0;
}
int parse_server (struct rpc_config *RC, struct rpc_cluster *C, int flags, int cluster_disabled) {
unsigned char ipv6[16];
unsigned ipv4 = 0;
struct hostent *h;
int port;
assert (C->tot_buckets < MAX_CLUSTER_SERVERS);
int l = cfg_getword ();
if (!l || l > 63) {
return syntax ("hostname expected");
}
char c = cfg_cur[l];
//hostname = cfg_cur;
cfg_cur[l] = 0;
if (!(h = kdb_gethostbyname (cfg_cur)) || !h->h_addr_list || !h->h_addr) {
kprintf ("cannot resolve %s\n", cfg_cur);
return syntax ("cannot resolve hostname");
}
*(cfg_cur += l) = c;
switch (h->h_addrtype) {
case AF_INET:
assert (h->h_length == 4);
ipv4 = *((unsigned *) h->h_addr);
break;
case AF_INET6:
assert (h->h_length == 16);
memcpy (ipv6, h->h_addr, 16);
break;
default:
return syntax ("Can not resolve hostname: bad address type");
}
cfg_getlex ();
Expect (':');
port = cfg_getint();
if (!port) {
return syntax ("port number expected");
}
if (port <= 0 || port >= 0x10000) {
return syntax ("port number out of range");
}
unsigned pid_host = ipv4;
unsigned pid_port = port;
long long actor_id = ACTOR_ID_UNINIT;
cfg_skspc();
if (*cfg_cur == '!') {
cfg_cur ++;
actor_id = cfg_getint ();
cfg_skspc();
}
if (*cfg_cur == '@') {
cfg_cur ++;
l = cfg_getword ();
if (!l || l > 63) {
return syntax ("hostname expected");
}
c = cfg_cur[l];
//hostname = cfg_cur;
cfg_cur[l] = 0;
if (!(h = kdb_gethostbyname (cfg_cur)) || !h->h_addr_list || !h->h_addr) {
kprintf ("cannot resolve %s\n", cfg_cur);
return syntax ("cannot resolve hostname");
}
*(cfg_cur += l) = c;
if (h->h_addrtype == AF_INET) {
assert (h->h_length == 4);
pid_host = *((unsigned *) h->h_addr);
} else {
return syntax ("cannot resolve hostname in pid");
}
cfg_skspc ();
if (*cfg_cur == ':') {
cfg_cur ++;
pid_port = cfg_getint ();
}
}
if (C->proto == PROTO_TCP) {
if (pid_host == ipv4) {
pid_host = 0;
}
struct conn_target ct = tcp_buffers ? default_tcp_rpc_ct : default_rpc_ct;
if (ipv4) {
ct.target = *(struct in_addr *)&ipv4;
memset (ct.target_ipv6, 0, 16);
} else {
ct.target.s_addr = 0;
memcpy (ct.target_ipv6, h->h_addr, 16);
}
ct.port = port;
ct.min_connections = C->min_connections;
ct.max_connections = C->max_connections;
ct.reconnect_timeout = 1.0 + 0.1 * drand48 ();
if ((flags & 1) && !cluster_disabled) {
int was_created = -1;
struct conn_target *D = create_target (&ct, &was_created);
//rpc_target_insert_target (D);
if (was_created <= 0) {
if (D->custom_field != -1 && pid_host) {
if (D->custom_field != pid_host) {
return syntax ("Bad pid host");
}
} else if (pid_host) {
D->custom_field = pid_host;
rpc_target_insert_target_ext (D, D->custom_field);
}
// syntax_warning ("duplicate hostname:port %.*s:%d in cluster", l, hostname, default_ct.port);
} else {
D->custom_field = pid_host ? pid_host : -1;
if (pid_host) {
rpc_target_insert_target_ext (D, D->custom_field);
}
}
RC->ConfigServers[RC->ConfigServersCount] = D;
C->buckets[C->tot_buckets].T = D;
C->buckets[C->tot_buckets].A = actor_id;
C->buckets[C->tot_buckets].RT = D->custom_field != -1 ? rpc_target_lookup_target (D) : 0;
C->buckets[C->tot_buckets].methods = &__conn_methods;
}
if (!cluster_disabled) {
C->tot_buckets++;
assert (RC->ConfigServersCount++ < MAX_CLUSTER_SERVERS);
}
vkprintf (1, "server #%d: ip %s, id %d\n", RC->ConfigServersCount, inet_ntoa (ct.target), ct.port);
} else {
pid_host = ntohl (pid_host);
if (pid_host == 0x7f000001) {
pid_host = PID.ip;
}
struct process_id pid;
memset (&pid, 0, sizeof (pid));
pid.ip = pid_host;
pid.port = pid_port;
if (ipv4) {
set_4in6 (ipv6, ipv4);
}
if ((flags & 1) && !cluster_disabled) {
struct udp_target *S = udp_target_create (&pid, ipv6, port, 0);
C->buckets[C->tot_buckets].S = S->ST;
C->buckets[C->tot_buckets].A = actor_id;
C->buckets[C->tot_buckets].methods = &__udp_methods;
}
if (!cluster_disabled) {
C->tot_buckets ++;
}
vkprintf (1, "server #-1: ip " IP_PRINT_STR ", id %d\n", IP_TO_PRINT (ipv4), port);
}
return 0;
}
// flags = 0 -- syntax check only (first pass), flags = 1 -- create targets and points as well (second pass)
int parse_config (struct rpc_config *RC, struct rpc_config *RC_Old, int flags) {
int r, /*c,*/ l, i;
// struct hostent *h;
//char *hostname;
int dup_port_checked, cluster_disabled;
if (!(flags & 1)) {
config_bytes = r = read (fd[0], config_buff, MAX_CONFIG_SIZE+1);
if (r < 0) {
fprintf (stderr, "error reading configuration file %s: %m\n", config_filename);
return -2;
}
if (r > MAX_CONFIG_SIZE) {
fprintf (stderr, "configuration file %s too long (max %d bytes)\n", config_filename, MAX_CONFIG_SIZE);
return -2;
}
}
cfg_cur = cfg_start = config_buff;
cfg_end = cfg_start + config_bytes;
*cfg_end = 0;
cfg_lno = 0;
if (!(flags & 1) && RC_Old) {
for (i = 0; i < RC_Old->clusters_num; i++) {
RC_Old->Clusters[i].other_cluster_no = -1;
}
}
RC->ConfigServersCount = 0;
RC->clusters_num = 0;
// RC->kitten_php_cluster = 0;
static struct rpc_config_create _Zconf;
struct rpc_config_create *Zconf = &_Zconf;
memset (Zconf, 0, sizeof (*Zconf));
for (i = 0; i < RPC_FUN_NUM; i++) {
Zconf->funs_last[i] = MAX_CLUSTER_FUNS;
Zconf->funs[i][--Zconf->funs_last[i]] = rpc_fun_ok;
}
memset (RC->schema_extra, 0, sizeof (RC->schema_extra));
memset (RC->extensions_extra, 0, sizeof (RC->extensions_extra));
while (cfg_skipspc ()) {
struct rpc_cluster *C = &RC->Clusters[RC->clusters_num];
if (cfg_getlex () != 'c') {
return syntax ("'cluster' expected");
}
assert (RC->clusters_num < MAX_CLUSTERS);
l = cfg_getword ();
if (!l) {
return syntax ("cluster name expected");
}
C->cluster_no = RC->clusters_num++;
if (C->cluster_name) {