-
Notifications
You must be signed in to change notification settings - Fork 107
/
router.c
3458 lines (3234 loc) · 88 KB
/
router.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
/*
* Copyright 2013-2024 Fabian Groffen
*
* 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 <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <glob.h>
#include <sys/stat.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <assert.h>
#include "fnv1a.h"
#include "consistent-hash.h"
#include "server.h"
#include "queue.h"
#include "aggregator.h"
#include "allocator.h"
#include "relay.h"
#include "router.h"
#include "posixregex.h"
#include "conffile.h"
#include "conffile.tab.h"
struct _router {
cluster *clusters;
route *routes;
aggregator *aggregators;
servers *srvrs;
struct _router_collector {
char *stub;
int interval;
char *prefix;
col_mode mode;
} collector;
listener *listeners;
struct _router_parser_err {
const char *msg;
size_t line;
size_t start;
size_t stop;
} parser_err;
struct _router_conf {
char workercnt;
size_t queuesize;
size_t batchsize;
int maxstalls;
unsigned short iotimeout;
unsigned int sockbufsize;
} conf;
allocator *a;
};
/* declarations to avoid symbol/include hell when doing config.yy.h */
int router_yylex_init(void **);
void router_yy_scan_string(char *, void *);
int router_yylex_destroy(void *);
/* catch string used for aggrgator destinations */
#define STUB_AGGR "_aggregator_stub_"
/* catch string used for collector output */
#define STUB_STATS "_collector_stub_"
/* string representations for symbols in the matching enums from
* router.h */
const char *con_proto_str[] = {
/* 0 */ "<unset>",
/* 1 */ "tcp",
/* 2 */ "udp",
/* 3 */ "pipe",
/* 4 */ "file",
/* 5 */ "unix"
};
const char *con_type_str[] = {
/* 0 */ "<unset>",
/* 1 */ "linemode",
/* 2 */ "syslog"
};
const char *con_trnsp_str[] = {
/* 0 */ "<unset>",
/* 1 */ "plain",
/* 2 */ "gzip",
/* 3 */ "lz4",
/* 4 */ "snappy"
};
/**
* Frees regexes from routes.
*/
static void
router_free_intern(route *routes, char workercnt)
{
/* the only thing that isn't allocated using the allocators are the
* regexes */
while (routes != NULL) {
if (routes->matchtype == REGEX) {
int i;
for(i = 0; i < workercnt; i++)
regfree(&routes->rule[i]);
}
if (routes->next == NULL || routes->next->dests != routes->dests) {
while (routes->dests != NULL) {
if (routes->dests->cl->type == GROUP ||
routes->dests->cl->type == AGGRSTUB ||
routes->dests->cl->type == STATSTUB)
router_free_intern(routes->dests->cl->members.routes,
workercnt);
if (routes->dests->cl->type == VALIDATION)
router_free_intern(
routes->dests->cl->members.validation->rule,
workercnt);
routes->dests = routes->dests->next;
}
}
routes = routes->next;
}
}
/**
* Examines pattern and sets matchtype and rule or strmatch in route.
*/
static inline int
determine_if_regex(allocator *a, char workercnt, route *r, char *pat)
{
/* try and see if we can avoid using a regex match, for
* it is simply very slow/expensive to do so: most of
* the time, people don't need fancy matching rules */
char patbuf[8192];
char *e = pat;
char *pb = patbuf;
char escape = 0;
r->matchtype = CONTAINS;
r->nmatch = 0;
if (*e == '^' && r->matchtype == CONTAINS) {
e++;
r->matchtype = STARTS_WITH;
}
for (; *e != '\0'; e++) {
switch (*e) {
case '\\':
if (escape)
*pb++ = *e;
escape = !escape;
break;
case '.':
case '^':
case '*':
case '+':
if (!escape)
r->matchtype = REGEX;
*pb++ = *e;
escape = 0;
break;
case '$':
if (!escape && e[1] == '\0') {
if (r->matchtype == STARTS_WITH) {
r->matchtype = MATCHES;
} else {
r->matchtype = ENDS_WITH;
}
} else {
r->matchtype = REGEX;
}
escape = 0;
break;
default:
if (
!escape && (
(*e == '_') || (*e == '-') ||
(*e >= '0' && *e <= '9') ||
(*e >= 'a' && *e <= 'z') ||
(*e >= 'A' && *e <= 'Z')
)
)
{
*pb++ = *e;
} else {
r->matchtype = REGEX;
}
escape = 0;
break;
}
if (pb - patbuf == sizeof(patbuf))
r->matchtype = REGEX;
if (r->matchtype == REGEX)
break;
}
if (r->matchtype != REGEX) {
*pb = '\0';
r->strmatch = ra_strdup(a, patbuf);
r->pattern = ra_strdup(a, pat);
} else {
int i;
int ret;
r->rule = ra_malloc(a, sizeof(*r->rule) * workercnt);
if (r->rule == NULL) {
logerr("determine_if_regex: malloc failed for "
"regular expressions\n");
return REG_ESPACE; /* lie closest to the truth */
}
ret = regcomp(&r->rule[0], pat, REG_EXTENDED);
if (ret != 0)
return ret; /* allow use of regerror */
if (r->rule[0].re_nsub > 0) {
/* we need +1 because position 0 contains the entire
* expression */
r->nmatch = r->rule[0].re_nsub + 1;
if (r->nmatch > RE_MAX_MATCHES) {
logerr("determine_if_regex: too many match groups, "
"please increase RE_MAX_MATCHES in router.h\n");
regfree(&r->rule[0]);
return REG_ESPACE; /* lie closest to the truth */
}
}
for (i = 1; i < workercnt; i++) {
if (regcomp(&r->rule[i], pat, REG_EXTENDED) != 0) {
while (--i >= 0)
regfree(&r->rule[i]);
logerr("determine_if_regex: out of memory allocating "
"regexes\n");
return REG_ESPACE; /* lie closest to the truth */
}
}
r->strmatch = NULL;
r->pattern = ra_strdup(a, pat);
}
return 0;
}
static char serverip_server[256]; /* RFC1035 = 250 */
static const char *
serverip(server *s)
{
const char *srvr = server_ip(s);
if (strchr(srvr, ':') != NULL) {
snprintf(serverip_server, sizeof(serverip_server), "[%s]", srvr);
return serverip_server;
}
return srvr;
}
/**
* Callback for the bison parser for yyerror
*/
void
router_yyerror(
void *locptr,
void *s,
router *r,
allocator *ra,
allocator *a,
const char *msg)
{
(void)s;
ROUTER_YYLTYPE *locp = (ROUTER_YYLTYPE *)locptr;
regex_t re;
size_t nmatch = 3;
regmatch_t pmatch[3];
char buf1[METRIC_BUFSIZ];
char buf2[METRIC_BUFSIZ];
char *dummy;
char (*sa)[METRIC_BUFSIZ];
char (*sb)[METRIC_BUFSIZ];
char (*st)[METRIC_BUFSIZ];
if (r->parser_err.msg != NULL)
return;
/* clean up the "value" types */
if (regcomp(&re, "cr(STRING|COMMENT|INTVAL)", REG_EXTENDED) != 0) {
r->parser_err.msg = ra_strdup(a, msg);
return;
}
snprintf(buf1, sizeof(buf1), "%s", msg);
sa = &buf1;
sb = &buf2;
while (regexec(&re, *sa, nmatch, pmatch, 0) == 0) {
dummy = *sa + strlen(*sa);
if (router_rewrite_metric(sb, &dummy,
*sa, dummy, "\\_1", nmatch, pmatch) == 0)
{
r->parser_err.msg = ra_strdup(a, msg);
return;
}
st = sa;
sa = sb;
sb = st;
}
regfree(&re);
/* clean up the keywords */
if (regcomp(&re, "cr([A-Z][A-Z0-9_]*)", REG_EXTENDED) != 0) {
r->parser_err.msg = ra_strdup(a, msg);
return;
}
while (regexec(&re, *sa, nmatch, pmatch, 0) == 0) {
dummy = *sa + strlen(*sa);
if (router_rewrite_metric(sb, &dummy,
*sa, dummy, "'\\_1'", nmatch, pmatch) == 0)
{
r->parser_err.msg = ra_strdup(a, msg);
return;
}
st = sa;
sa = sb;
sb = st;
}
regfree(&re);
/* remove an artificial phrase */
if (regcomp(&re, ", unexpected 'unexpected'", REG_EXTENDED) != 0) {
r->parser_err.msg = ra_strdup(a, msg);
return;
}
while (regexec(&re, *sa, nmatch, pmatch, 0) == 0) {
dummy = *sa + strlen(*sa);
if (router_rewrite_metric(sb, &dummy,
*sa, dummy, "", nmatch, pmatch) == 0)
{
r->parser_err.msg = ra_strdup(a, msg);
return;
}
st = sa;
sa = sb;
sb = st;
}
regfree(&re);
/* clean up bison speak */
if (regcomp(&re, "\\$end", REG_EXTENDED) != 0) {
r->parser_err.msg = ra_strdup(a, msg);
return;
}
while (regexec(&re, *sa, nmatch, pmatch, 0) == 0) {
dummy = *sa + strlen(*sa);
if (router_rewrite_metric(sb, &dummy,
*sa, dummy, "end of file", nmatch, pmatch) == 0)
{
r->parser_err.msg = ra_strdup(a, msg);
return;
}
st = sa;
sa = sb;
sb = st;
}
regfree(&re);
r->parser_err.msg = ra_strdup(a, *sa);
if (locp != NULL) {
r->parser_err.line = 1 + locp->first_line;
r->parser_err.start = locp->first_column;
if (locp->first_line == locp->last_line) {
r->parser_err.stop = locp->last_column;
} else {
r->parser_err.stop = locp->first_column;
}
} else {
r->parser_err.line = 0;
}
}
/**
* Parse ip string and validate it. When it is a numeric address, write
* the cannonical version in retip.
*/
char *
router_validate_address(
router *rtr,
char **retip, unsigned short *retport, void **retsaddr,
void **rethint, char *ip, con_proto proto, char allowanyaddr)
{
unsigned short port = GRAPHITE_PORT;
struct addrinfo *saddr = NULL;
struct addrinfo hint;
char sport[8];
char *p;
char *lastcolon = NULL;
for (p = ip; *p != '\0'; p++)
if (*p == ':')
lastcolon = p;
if (*(p - 1) == ']')
lastcolon = NULL;
if (lastcolon != NULL) {
long lport;
char *endp = NULL;
*lastcolon = '\0';
lport = strtol(lastcolon + 1, &endp, 10);
if (lport < 1 || lport > UINT16_MAX || endp != p) {
char errbuf[40];
snprintf(errbuf, sizeof(errbuf),
"invalid port number '%s'", lastcolon + 1);
return(ra_strdup(rtr->a, errbuf));
}
port = lport;
}
if (*ip == '[') {
ip++;
if (lastcolon != NULL && *(lastcolon - 1) == ']') {
*(lastcolon - 1) = '\0';
} else if (lastcolon == NULL && *(p - 1) == ']') {
*(p - 1) = '\0';
} else {
return(ra_strdup(rtr->a, "expected ']'"));
}
}
if (lastcolon == ip) {
if (allowanyaddr) {
ip = NULL; /* only resolve port, e.g. any interface */
} else {
return(ra_strdup(rtr->a, "host or IP-address required"));
}
}
memset(&hint, 0, sizeof(hint));
saddr = NULL;
/* try to see if this is a "numeric" IP address, which means
* re-resolving lateron will make no difference */
hint.ai_family = PF_UNSPEC;
hint.ai_socktype = proto == CON_UDP ? SOCK_DGRAM : SOCK_STREAM;
hint.ai_protocol = proto == CON_UDP ? IPPROTO_UDP : IPPROTO_TCP;
hint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV | AI_PASSIVE;
snprintf(sport, sizeof(sport), "%u", port);
*rethint = NULL;
if (getaddrinfo(ip, sport, &hint, &saddr) != 0) {
int err;
/* now resolve this the normal way to check validity */
hint.ai_flags = AI_NUMERICSERV;
if ((err = getaddrinfo(ip, sport, &hint, &saddr)) != 0)
saddr = NULL;
/* no ra_malloc, it gets owned by server */
*rethint = malloc(sizeof(hint));
if (*rethint == NULL) {
if (saddr != NULL)
freeaddrinfo(saddr);
return(ra_strdup(rtr->a, "out of memory copying hint structure"));
}
memcpy(*rethint, &hint, sizeof(hint));
}
*retip = ip;
*retport = port;
*retsaddr = saddr;
return NULL;
}
/**
* Parse path string and validate it can be written to.
*/
char *
router_validate_path(router *rtr, char *path)
{
struct stat st;
char fileexists = 1;
int probefd;
/* if the file doesn't exist, remove it after trying to create it */
if (stat(path, &st) == -1)
fileexists = 0;
if ((probefd = open(path,
O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
{
char errbuf[512];
if (errno == EOPNOTSUPP && st.st_mode & S_IFSOCK) {
/* This is pretty lame, but I don't know a better way: we
* can't open sockets in use, and as it stands we can't
* check it is *us* who have the socket in use. So, we
* assume we do, and ignore this silently. In the worst
* case this yields an error when it opens up for real. */
return NULL;
}
snprintf(errbuf, sizeof(errbuf),
"failed to open file '%s' for writing: %s",
path, strerror(errno));
return ra_strdup(rtr->a, errbuf);
}
close(probefd);
if (!fileexists)
unlink(path);
return NULL;
}
/**
* Checks pat against regular expression syntax (or being '*') and
* returns a route struct with the parsed info. If retr is NULL a route
* struct is allocated.
*/
char *
router_validate_expression(router *rtr, route **retr, char *pat)
{
route *r = *retr;
if (r == NULL) {
r = *retr = ra_malloc(rtr->a, sizeof(route));
if (r == NULL)
return ra_strdup(rtr->a, "out of memory allocating route");
r->next = NULL;
r->dests = NULL;
}
if (strcmp(pat, "*") == 0) {
r->pattern = NULL;
r->strmatch = NULL;
r->matchtype = MATCHALL;
} else {
int err = determine_if_regex(rtr->a, rtr->conf.workercnt, r, pat);
if (err != 0) {
char ebuf[512];
size_t s = snprintf(ebuf, sizeof(ebuf),
"invalid expression '%s': ", pat);
regerror(err, &r->rule[0], ebuf + s, sizeof(ebuf) - s);
return ra_strdup(rtr->a, ebuf);
}
}
return NULL;
}
/**
* Check if cluster is defined, and set the pointer to it.
*/
char *
router_validate_cluster(router *rtr, cluster **clptr, char *cl)
{
cluster *w;
/* lookup cluster */
for (w = rtr->clusters; w != NULL; w = w->next) {
if (w->type != GROUP &&
w->type != AGGRSTUB &&
w->type != STATSTUB &&
w->type != AGGREGATION &&
w->type != REWRITE &&
w->type != VALIDATION &&
strcmp(w->name, cl) == 0)
break;
}
if (w == NULL)
return ra_strdup(rtr->a, "unknown cluster");
*clptr = w;
return NULL;
}
/**
* Adds a server to the chain in router, expands if necessary.
*/
char *
router_add_server(
router *ret,
char *ip,
unsigned short port,
char *inst,
con_type type,
con_trnsp transport,
char *mtlspemcert,
char *mtlspemkey,
con_proto proto,
struct addrinfo *saddrs,
struct addrinfo *hint,
char useall,
cluster *cl)
{
struct addrinfo *walk;
struct addrinfo *lwalk;
char hnbuf[INET6_ADDRSTRLEN];
char errbuf[512];
server *newserver;
servers *w = NULL;
walk = saddrs; /* NULL if file */
do {
servers *s;
lwalk = NULL;
/* Since #293 unresolvable targets are allowed, but when there's
* only one target such as in #455 we end up here crashing
* because walk is NULL. So, in that scenario, basically ignore
* the useall, and if the address lateron becomes resolvable,
* treat it as single target. */
if (useall && walk != NULL) {
/* serialise the IP address, to make the targets explicit
* (since we're expanding all A/AAAA records) */
saddr_ntop(walk, hnbuf);
if (hnbuf[0] != '\0')
ip = hnbuf;
/* now we've expanded, don't trigger re-resolving */
if (hint)
free(hint);
hint = NULL;
/* and turn this into a separate entry */
lwalk = walk->ai_next;
walk->ai_next = NULL;
}
newserver = NULL;
for (s = ret->srvrs; s != NULL; s = s->next) {
if (server_cmp(s->server, walk, ip) == 0) {
newserver = s->server;
s->refcnt++;
break;
}
}
if (newserver == NULL) {
newserver = server_new(ip, port,
type, transport, mtlspemcert, mtlspemkey,
proto, walk, hint,
ret->conf.queuesize, ret->conf.batchsize,
ret->conf.maxstalls, ret->conf.iotimeout,
ret->conf.sockbufsize);
if (newserver == NULL) {
freeaddrinfo(saddrs);
if (hint)
free(hint);
snprintf(errbuf, sizeof(errbuf),
"failed to add server %s:%d (%s) "
"to cluster %s", ip, port,
con_proto_str[proto], cl->name);
return ra_strdup(ret->a, errbuf);
}
if (ret->srvrs == NULL) {
s = ret->srvrs = ra_malloc(ret->a, sizeof(servers));
} else {
for (s = ret->srvrs; s->next != NULL; s = s->next)
;
s = s->next = ra_malloc(ret->a, sizeof(servers));
}
s->next = NULL;
s->refcnt = 1;
s->server = newserver;
}
/* check if the type matches */
if (s->refcnt > 1 && type != server_type(newserver)) {
freeaddrinfo(saddrs);
if (hint)
free(hint);
snprintf(errbuf, sizeof(errbuf),
"cannot set type '%s' for "
"server %s:%d: server was previously "
"defined with type '%s'",
con_type_str[type], serverip(newserver),
server_port(newserver),
con_type_str[server_type(newserver)]);
return ra_strdup(ret->a, errbuf);
}
/* check instance matches the existing server */
if (s->refcnt > 1) {
char *sinst = server_instance(newserver);
if (sinst == NULL && inst != NULL) {
freeaddrinfo(saddrs);
if (hint)
free(hint);
snprintf(errbuf, sizeof(errbuf),
"cannot set instance '%s' for "
"server %s:%d: server was previously "
"defined without instance",
inst, serverip(newserver),
server_port(newserver));
return ra_strdup(ret->a, errbuf);
} else if (sinst != NULL && inst == NULL) {
freeaddrinfo(saddrs);
if (hint)
free(hint);
snprintf(errbuf, sizeof(errbuf),
"cannot define server %s:%d without "
"instance: server was previously "
"defined with instance '%s'",
serverip(newserver),
server_port(newserver), sinst);
return ra_strdup(ret->a, errbuf);
} else if (sinst != NULL && inst != NULL &&
strcmp(sinst, inst) != 0)
{
freeaddrinfo(saddrs);
if (hint)
free(hint);
snprintf(errbuf, sizeof(errbuf),
"cannot set instance '%s' for "
"server %s:%d: server was previously "
"defined with instance '%s'",
inst, serverip(newserver),
server_port(newserver), sinst);
return ra_strdup(ret->a, errbuf);
} /* else: sinst == inst == NULL */
}
if (inst != NULL)
server_set_instance(newserver, inst);
if (cl->type == CARBON_CH ||
cl->type == FNV1A_CH ||
cl->type == JUMP_CH)
{
if (cl->members.ch->servers == NULL) {
cl->members.ch->servers = w =
ra_malloc(ret->a, sizeof(servers));
} else {
for (w = cl->members.ch->servers; w->next != NULL; w = w->next)
;
w = w->next = ra_malloc(ret->a, sizeof(servers));
}
if (w == NULL) {
snprintf(errbuf, sizeof(errbuf), "malloc failed for %s_ch %s",
cl->type == CARBON_CH ? "carbon" :
cl->type == FNV1A_CH ? "fnv1a" :
"jump_fnv1a", ip);
freeaddrinfo(saddrs);
if (hint)
free(hint);
return ra_strdup(ret->a, errbuf);
}
w->next = NULL;
w->server = newserver;
cl->members.ch->ring = ch_addnode(
cl->members.ch->ring,
w->server);
if (cl->members.ch->ring == NULL) {
freeaddrinfo(saddrs);
if (hint)
free(hint);
snprintf(errbuf, sizeof(errbuf),
"failed to add server %s:%d "
"to ring for cluster %s: out of memory",
ip, port, cl->name);
return ra_strdup(ret->a, errbuf);
}
} else if (cl->type == FORWARD ||
cl->type == FILELOG ||
cl->type == FILELOGIP)
{
if (cl->members.forward == NULL) {
cl->members.forward = w =
ra_malloc(ret->a, sizeof(servers));
} else {
for (w = cl->members.forward; w->next != NULL; w = w->next)
;
w = w->next = ra_malloc(ret->a, sizeof(servers));
}
if (w == NULL) {
snprintf(errbuf, sizeof(errbuf), "malloc failed for %s %s",
cl->type == FORWARD ? "forward" :
cl->type == FILELOG ? "file" :
"file ip", ip);
freeaddrinfo(saddrs);
if (hint)
free(hint);
return ra_strdup(ret->a, errbuf);
}
w->next = NULL;
w->server = newserver;
} else if (cl->type == ANYOF ||
cl->type == FAILOVER)
{
if (cl->members.anyof == NULL) {
cl->members.anyof = ra_malloc(ret->a, sizeof(serverlist));
if (cl->members.anyof != NULL) {
cl->members.anyof->list = w =
ra_malloc(ret->a, sizeof(servers));
cl->members.anyof->count = 1;
cl->members.anyof->servers = NULL;
}
} else {
for (w = cl->members.anyof->list; w->next != NULL; w = w->next)
;
w = w->next = ra_malloc(ret->a, sizeof(servers));
cl->members.anyof->count++;
}
if (w == NULL) {
snprintf(errbuf, sizeof(errbuf), "malloc failed for %s %s",
cl->type == ANYOF ? "any_of" :
"failover", ip);
freeaddrinfo(saddrs);
if (hint)
free(hint);
return ra_strdup(ret->a, errbuf);
}
w->next = NULL;
w->server = newserver;
if (s->refcnt > 1) {
freeaddrinfo(saddrs);
if (hint)
free(hint);
snprintf(errbuf, sizeof(errbuf),
"cannot share server %s:%d with "
"any_of/failover cluster '%s'",
serverip(newserver),
server_port(newserver),
cl->name);
return ra_strdup(ret->a, errbuf);
}
}
walk = lwalk;
} while (walk != NULL);
return NULL;
}
char *
router_add_cluster(router *r, cluster *cl)
{
cluster *cw;
cluster *last = NULL;
servers *w;
for (cw = r->clusters; cw != NULL; last = cw, cw = cw->next)
if (cw->name != NULL && cl->name != NULL &&
strcmp(cw->name, cl->name) == 0)
return ra_strdup(r->a, "cluster with the same name already defined");
if (last == NULL)
last = r->clusters;
last->next = cl;
/* post checks/fixups */
if (cl->type == ANYOF || cl->type == FAILOVER) {
size_t i = 0;
cl->members.anyof->servers =
ra_malloc(r->a, sizeof(server *) * cl->members.anyof->count);
if (cl->members.anyof->servers == NULL)
return ra_strdup(r->a, "malloc failed for anyof servers");
for (w = cl->members.anyof->list; w != NULL; w = w->next)
cl->members.anyof->servers[i++] = w->server;
for (w = cl->members.anyof->list; w != NULL; w = w->next) {
server_add_secondaries(w->server,
cl->members.anyof->servers,
cl->members.anyof->count);
if (cl->type == FAILOVER)
server_set_failover(w->server);
}
} else if (cl->type == CARBON_CH ||
cl->type == FNV1A_CH ||
cl->type == JUMP_CH)
{
/* check that replication count is actually <= the
* number of servers */
size_t i = 0;
char errbuf[512];
server **secondaries;
for (w = cl->members.ch->servers; w != NULL; w = w->next)
i++;
if (i < cl->members.ch->repl_factor) {
snprintf(errbuf, sizeof(errbuf),
"invalid cluster '%s': replication count (%u) is "
"larger than the number of servers (%zu)\n",
cl->name, cl->members.ch->repl_factor, i);
return ra_strdup(r->a, errbuf);
}
if (cl->isdynamic) {
secondaries = ra_malloc(r->a, sizeof(server *) * i);
if (secondaries == NULL)
return ra_strdup(r->a, "malloc failed for dynamic servers");
for (i = 0, w = cl->members.ch->servers; w != NULL; w = w->next)
secondaries[i++] = w->server;
for (w = cl->members.ch->servers; w != NULL; w = w->next)
server_add_secondaries(w->server, secondaries, i);
}
}
return NULL;
}
char *
router_add_route(router *rtr, route *rte)
{
route *rw;
route *last = NULL;
route *matchallstop = NULL;
for (rw = rtr->routes; rw != NULL; last = rw, rw = rw->next)
if (rw->matchtype == MATCHALL && rw->stop)
matchallstop = rw;
if (last == NULL) {
rtr->routes = rte;
return NULL;
}
#define matchtype(r) \
r->dests->cl->type == AGGREGATION ? "aggregate" : \
r->dests->cl->type == REWRITE ? "rewrite" : \
"match"
if (matchallstop != NULL) {
logerr("warning: %s %s will never match "
"due to preceding %s * ... stop\n",
matchtype(rte), rte->pattern == NULL ? "*" : rte->pattern,
matchtype(matchallstop));
}
last->next = rte;
return NULL;
}
char *
router_add_aggregator(router *rtr, aggregator *a)
{
aggregator *aw;
aggregator *last = NULL;
for (aw = rtr->aggregators; aw != NULL; last = aw, aw = aw->next)
;
if (last == NULL) {
rtr->aggregators = a;
return NULL;
}
last->next = a;
return NULL;
}
char *
router_add_stubroute(
router *rtr,
enum clusttype type,
cluster *w,
destinations *dw)
{
char stubname[48];
route *m;
destinations *d;
cluster *cl;
char *err;
m = ra_malloc(rtr->a, sizeof(route));
if (m == NULL)
return ra_strdup(rtr->a, "malloc failed for stub route");
m->pattern = NULL;
m->strmatch = NULL;
m->dests = dw;
m->stop = 1;
m->matchtype = MATCHALL;
m->next = NULL;
/* inject stub route for dests */
d = ra_malloc(rtr->a, sizeof(destinations));
if (d == NULL)
return ra_strdup(rtr->a, "malloc failed for stub destinations");
d->next = NULL;
cl = d->cl = ra_malloc(rtr->a, sizeof(cluster));
if (cl == NULL)
return ra_strdup(rtr->a, "malloc failed for stub cluster");
cl->name = NULL;
cl->type = type;
cl->members.routes = m;
cl->next = NULL;
err = router_add_cluster(rtr, cl);
if (err != NULL)
return err;
if (type == AGGRSTUB) {
snprintf(stubname, sizeof(stubname),
STUB_AGGR "%p__", w->members.aggregation);
} else if (type == STATSTUB) {
snprintf(stubname, sizeof(stubname),
STUB_STATS "%p__", rtr);
} else {
return ra_strdup(rtr->a, "unknown stub type");
}
m = ra_malloc(rtr->a, sizeof(route));
if (m == NULL)
return ra_strdup(rtr->a, "malloc failed for catch stub route");
m->pattern = ra_strdup(rtr->a, stubname);
m->strmatch = ra_strdup(rtr->a, stubname);
if (m->pattern == NULL || m->strmatch == NULL)