forked from grobian/carbon-c-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
846 lines (786 loc) · 20.9 KB
/
server.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
/*
* Copyright 2013-2016 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 <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <pthread.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/resource.h>
#include "relay.h"
#include "queue.h"
#include "dispatcher.h"
#include "collector.h"
#include "server.h"
struct _server {
const char *ip;
unsigned short port;
char *instance;
struct addrinfo *saddr;
int fd;
int disp_conn;
queue *queue;
size_t bsize;
short iotimeout;
const char **batch;
serv_ctype ctype;
pthread_t tid;
struct _server **secondaries;
size_t secondariescnt;
char failover:1;
char failure:5;
char running:1;
char keep_running:1;
size_t metrics;
size_t dropped;
size_t stalls;
size_t ticks;
size_t prevmetrics;
size_t prevdropped;
size_t prevstalls;
size_t prevticks;
};
/**
* Reads from the queue and sends items to the remote server. This
* function is designed to be a thread. Data sending is attempted to be
* batched, but sent one by one to reduce loss on sending failure.
* A connection with the server is maintained for as long as there is
* data to be written. As soon as there is none, the connection is
* dropped.
*/
static void *
server_queuereader(void *d)
{
server *self = (server *)d;
size_t len;
ssize_t slen;
const char **metric = self->batch;
struct timeval start, stop;
struct timeval timeout;
int timeoutms;
queue *queue;
char idle = 0;
size_t *secpos = NULL;
*metric = NULL;
self->metrics = 0;
self->ticks = 0;
#define FAIL_WAIT_TIME 6 /* 6 * 250ms = 1.5s */
#define DISCONNECT_WAIT_TIME 12 /* 12 * 250ms = 3s */
#define LEN_CRITICAL(Q) (queue_free(Q) < self->bsize)
self->running = 1;
while (1) {
if (queue_len(self->queue) == 0) {
/* if we're idling, close the TCP connection, this allows us
* to reduce connections, while keeping the connection alive
* if we're writing a lot */
gettimeofday(&start, NULL);
if (self->ctype == CON_TCP && self->fd >= 0 &&
idle++ > DISCONNECT_WAIT_TIME)
{
close(self->fd);
self->fd = -1;
}
gettimeofday(&stop, NULL);
self->ticks += timediff(start, stop);
if (!self->keep_running)
break;
/* nothing to do, so slow down for a bit */
usleep((200 + (rand() % 100)) * 1000); /* 200ms - 300ms */
/* if we are in failure mode, keep checking if we can
* connect, this avoids unnecessary queue moves */
if (!self->failure)
/* it makes no sense to try and do something, so skip */
continue;
} else if (self->secondariescnt > 0 &&
(self->failure >= FAIL_WAIT_TIME ||
(!self->failover && LEN_CRITICAL(self->queue))))
{
size_t i;
gettimeofday(&start, NULL);
if (self->secondariescnt > 0) {
if (secpos == NULL) {
secpos = malloc(sizeof(size_t) * self->secondariescnt);
if (secpos == NULL) {
logerr("server: failed to allocate memory "
"for secpos\n");
gettimeofday(&stop, NULL);
self->ticks += timediff(start, stop);
continue;
}
for (i = 0; i < self->secondariescnt; i++)
secpos[i] = i;
}
if (!self->failover) {
/* randomise the failover list such that in the
* grand scheme of things we don't punish the first
* working server in the list to deal with all
* traffic meant for a now failing server */
for (i = 0; i < self->secondariescnt; i++) {
size_t n = rand() % (self->secondariescnt - i);
if (n != i) {
size_t t = secpos[n];
secpos[n] = secpos[i];
secpos[i] = t;
}
}
}
}
/* offload data from our queue to our secondaries
* when doing so, observe the following:
* - avoid nodes that are in failure mode
* - avoid nodes which queues are >= critical_len
* when no nodes remain given the above
* - send to nodes which queue size < critical_len
* where there are no such nodes
* - do nothing (we will overflow, since we can't send
* anywhere) */
*metric = NULL;
queue = NULL;
for (i = 0; i < self->secondariescnt; i++) {
/* both conditions below make sure we skip ourself */
if (self->secondaries[secpos[i]]->failure)
continue;
queue = self->secondaries[secpos[i]]->queue;
if (!self->failover && LEN_CRITICAL(queue)) {
queue = NULL;
continue;
}
if (*metric == NULL) {
/* send up to batch size of our queue to this queue */
len = queue_dequeue_vector(
self->batch, self->queue, self->bsize);
self->batch[len] = NULL;
metric = self->batch;
}
for (; *metric != NULL; metric++)
if (!queue_putback(queue, *metric))
break;
/* try to put back stuff that didn't fit */
for (; *metric != NULL; metric++)
if (!queue_putback(self->queue, *metric))
break;
}
for (; *metric != NULL; metric++) {
if (mode == DEBUG)
logerr("dropping metric: %s", *metric);
free((char *)*metric);
self->dropped++;
}
gettimeofday(&stop, NULL);
self->ticks += timediff(start, stop);
if (queue == NULL) {
/* we couldn't do anything, take it easy for a bit */
if (self->failure)
self->failure = 1;
if (!self->keep_running)
break;
usleep((200 + (rand() % 100)) * 1000); /* 200ms - 300ms */
}
} else if (self->failure) {
if (!self->keep_running)
break;
usleep((200 + (rand() % 100)) * 1000); /* 200ms - 300ms */
}
/* at this point we've got work to do, if we're instructed to
* shut down, however, try to get everything out of the door
* (until we fail, see top of this loop) */
gettimeofday(&start, NULL);
/* try to connect */
if (self->fd < 0) {
if (self->ctype == CON_PIPE) {
int intconn[2];
if (pipe(intconn) < 0) {
if (!self->failure)
logerr("failed to create pipe: %s\n", strerror(errno));
self->failure += self->failure >= FAIL_WAIT_TIME ? 0 : 1;
continue;
}
self->disp_conn = dispatch_addconnection(intconn[0]);
self->fd = intconn[1];
} else if (self->ctype == CON_UDP) {
if ((self->fd = socket(self->saddr->ai_family,
self->saddr->ai_socktype,
self->saddr->ai_protocol)) < 0)
{
if (!self->failure)
logerr("failed to create udp socket: %s\n",
strerror(errno));
self->failure += self->failure >= FAIL_WAIT_TIME ? 0 : 1;
continue;
}
if (connect(self->fd,
self->saddr->ai_addr, self->saddr->ai_addrlen) < 0)
{
if (!self->failure)
logerr("failed to connect udp socket: %s\n",
strerror(errno));
close(self->fd);
self->fd = -1;
self->failure += self->failure >= FAIL_WAIT_TIME ? 0 : 1;
continue;
}
} else if (self->ctype == CON_FILE) {
if ((self->fd = open(self->ip,
O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
{
if (!self->failure)
logerr("failed to open file '%s': %s\n",
self->ip, strerror(errno));
self->failure += self->failure >= FAIL_WAIT_TIME ? 0 : 1;
continue;
}
} else {
int ret;
int args;
if ((self->fd = socket(self->saddr->ai_family,
self->saddr->ai_socktype,
self->saddr->ai_protocol)) < 0)
{
if (!self->failure)
logerr("failed to create socket: %s\n",
strerror(errno));
self->failure += self->failure >= FAIL_WAIT_TIME ? 0 : 1;
continue;
}
/* put socket in non-blocking mode such that we can
* select() (time-out) on the connect() call */
args = fcntl(self->fd, F_GETFL, NULL);
(void) fcntl(self->fd, F_SETFL, args | O_NONBLOCK);
ret = connect(self->fd,
self->saddr->ai_addr, self->saddr->ai_addrlen);
if (ret < 0 && errno == EINPROGRESS) {
/* wait for connection to succeed if the OS thinks
* it can succeed */
fd_set fds;
FD_ZERO(&fds);
FD_SET(self->fd, &fds);
timeoutms = self->iotimeout + (rand() % 100);
timeout.tv_sec = timeoutms / 1000;
timeout.tv_usec = (timeoutms % 1000) * 1000;
ret = select(self->fd + 1, NULL, &fds, NULL, &timeout);
if (ret == 0) {
/* time limit expired */
if (!self->failure)
logerr("failed to connect() to "
"%s:%u: Operation timed out\n",
self->ip, self->port);
close(self->fd);
self->fd = -1;
self->disp_conn = -1;
self->failure += self->failure >= FAIL_WAIT_TIME ? 0 : 1;
continue;
} else if (ret < 0) {
/* some select error occurred */
if (!self->failure)
logerr("failed to select() for %s:%u: %s\n",
self->ip, self->port, strerror(errno));
close(self->fd);
self->fd = -1;
self->disp_conn = -1;
self->failure += self->failure >= FAIL_WAIT_TIME ? 0 : 1;
continue;
} else {
int serr = 0;
socklen_t serrlen = sizeof(serr);
if (getsockopt(self->fd, SOL_SOCKET, SO_ERROR,
(void *)(&serr), &serrlen) < 0)
{
if (!self->failure)
logerr("failed to getsockopt() for %s:%u: %s\n",
self->ip, self->port, strerror(errno));
close(self->fd);
self->fd = -1;
self->disp_conn = -1;
self->failure += self->failure >= FAIL_WAIT_TIME ? 0 : 1;
continue;
}
if (serr != 0) {
if (!self->failure) {
logerr("failed to connect() to %s:%u: %s "
"(after select())\n",
self->ip, self->port, strerror(serr));
dispatch_check_rlimit_and_warn();
}
close(self->fd);
self->fd = -1;
self->disp_conn = -1;
self->failure += self->failure >= FAIL_WAIT_TIME ? 0 : 1;
continue;
}
}
} else if (ret < 0) {
if (!self->failure) {
logerr("failed to connect() to %s:%u: %s\n",
self->ip, self->port, strerror(errno));
dispatch_check_rlimit_and_warn();
}
close(self->fd);
self->fd = -1;
self->disp_conn = -1;
self->failure += self->failure >= FAIL_WAIT_TIME ? 0 : 1;
continue;
}
/* make socket blocking again */
(void) fcntl(self->fd, F_SETFL, args);
}
/* ensure we will break out of connections being stuck */
timeoutms = self->iotimeout + (rand() % 100);
timeout.tv_sec = timeoutms / 1000;
timeout.tv_usec = (timeoutms % 1000) * 1000;
setsockopt(self->fd, SOL_SOCKET, SO_SNDTIMEO,
&timeout, sizeof(timeout));
#ifdef SO_NOSIGPIPE
setsockopt(self->fd, SOL_SOCKET, SO_NOSIGPIPE, NULL, 0);
#endif
}
/* send up to batch size */
len = queue_dequeue_vector(self->batch, self->queue, self->bsize);
self->batch[len] = NULL;
metric = self->batch;
if (len != 0 && !self->keep_running) {
/* be noisy during shutdown so we can track any slowing down
* servers, possibly preventing us to shut down */
logerr("shutting down %s:%u: waiting for %zd metrics\n",
self->ip, self->port, len + queue_len(self->queue));
}
if (len == 0 && self->failure) {
/* if we don't have anything to send, we have at least a
* connection succeed, so assume the server is up again,
* this is in particular important for recovering this
* node by probes, to avoid starvation of this server since
* its queue is possibly being offloaded to secondaries */
if (self->ctype != CON_UDP)
logerr("server %s:%u: OK after probe\n", self->ip, self->port);
self->failure = 0;
}
for (; *metric != NULL; metric++) {
len = strlen(*metric);
if ((slen = write(self->fd, *metric, len)) != len) {
/* not fully sent, or failure, close connection
* regardless so we don't get synchonisation problems,
* partially sent data is an error for us, since we use
* blocking sockets, and hence partial sent is
* indication of a failure */
if (self->ctype != CON_UDP && !self->failure)
logerr("failed to write() to %s:%u: %s\n",
self->ip, self->port,
(slen < 0 ? strerror(errno) : "uncomplete write"));
close(self->fd);
self->fd = -1;
self->disp_conn = -1;
self->failure += self->failure >= FAIL_WAIT_TIME ? 0 : 1;
/* put back stuff we couldn't process */
for (; *metric != NULL; metric++) {
if (!queue_putback(self->queue, *metric)) {
if (mode == DEBUG)
logerr("server %s:%u: dropping metric: %s",
self->ip, self->port, *metric);
free((char *)*metric);
self->dropped++;
}
}
break;
} else if (self->failure) {
if (self->ctype != CON_UDP)
logerr("server %s:%u: OK\n", self->ip, self->port);
self->failure = 0;
}
free((char *)*metric);
self->metrics++;
}
gettimeofday(&stop, NULL);
self->ticks += timediff(start, stop);
idle = 0;
}
self->running = 0;
if (self->fd >= 0)
close(self->fd);
return NULL;
}
/**
* Allocate a new (outbound) server. Effectively this means a thread
* that reads from the queue and sends this as good as it can to the ip
* address and port associated.
*/
server *
server_new(
const char *ip,
unsigned short port,
serv_ctype ctype,
struct addrinfo *saddr,
size_t qsize,
size_t bsize,
unsigned short iotimeout)
{
server *ret;
if ((ret = malloc(sizeof(server))) == NULL)
return NULL;
ret->ctype = ctype;
ret->tid = 0;
ret->secondaries = NULL;
ret->secondariescnt = 0;
ret->ip = strdup(ip);
ret->port = port;
ret->instance = NULL;
ret->bsize = bsize;
ret->iotimeout = iotimeout < 250 ? 600 : iotimeout;
if ((ret->batch = malloc(sizeof(char *) * (bsize + 1))) == NULL) {
free(ret);
return NULL;
}
ret->fd = -1;
ret->disp_conn = -1;
ret->saddr = saddr;
ret->queue = queue_new(qsize);
if (ret->queue == NULL) {
free((char *)ret->ip);
free(ret);
return NULL;
}
ret->failover = 0;
ret->failure = 0;
ret->running = 0;
ret->keep_running = 1;
ret->metrics = 0;
ret->dropped = 0;
ret->stalls = 0;
ret->ticks = 0;
ret->prevmetrics = 0;
ret->prevdropped = 0;
ret->prevstalls = 0;
ret->prevticks = 0;
if (pthread_create(&ret->tid, NULL, &server_queuereader, ret) != 0) {
free((char *)ret->ip);
free(ret);
return NULL;
}
return ret;
}
/**
* Adds a list of secondary servers to this server. A secondary server
* is a server which' queue will be checked when this server has nothing
* to do. This is different from a backup server in that all servers
* involved have their own queue which they are supposed to deal with.
*/
void
server_add_secondaries(server *self, server **secondaries, size_t count)
{
self->secondaries = secondaries;
self->secondariescnt = count;
}
/**
* Flags this server as part of a failover cluster, which means the
* secondaries are used only to offload on failure, not on queue stress.
*/
void
server_set_failover(server *self)
{
self->failover = 1;
}
/**
* Sets instance name only used for carbon_ch cluster type.
*/
void
server_set_instance(server *self, char *instance)
{
self->instance = strdup(instance);
}
/**
* Thin wrapper around the associated queue with the server object.
* Returns true if the metric could be queued for sending, or the metric
* was dropped because the associated server is down. Returns false
* otherwise (when a retry seems like it could succeed shortly).
*/
inline char
server_send(server *s, const char *d, char force)
{
if (queue_free(s->queue) == 0) {
char failure = s->failure;
if (!force && s->secondariescnt > 0) {
size_t i;
/* don't immediately drop if we know there are others that
* back us up */
for (i = 0; i < s->secondariescnt; i++) {
if (!s->secondaries[i]->failure) {
failure = 0;
break;
}
}
}
if (failure || force) {
s->dropped++;
/* excess event will be dropped by the enqueue below */
} else {
s->stalls++;
return 0;
}
}
queue_enqueue(s->queue, d);
return 1;
}
/**
* Signals this server to stop whatever it's doing.
*/
void
server_stop(server *s)
{
if (s->secondariescnt == 0)
s->keep_running = 0;
}
/**
* Waits for this server to finish sending pending items from its queue.
*/
void
server_shutdown(server *s)
{
int i;
pthread_t tid;
size_t failures;
size_t inqueue;
int err;
if (s->tid == 0)
return;
tid = s->tid;
s->tid = 0;
if (s->secondariescnt > 0) {
/* if we have a working connection, or we still have stuff in
* our queue, wait for our secondaries, as they might need us,
* or we need them */
do {
failures = 0;
inqueue = 0;
for (i = 0; i < s->secondariescnt; i++) {
if (s->secondaries[i]->failure)
failures++;
if (s->secondaries[i]->running)
inqueue += queue_len(s->secondaries[i]->queue);
}
/* loop until we all failed, or nothing is in the queues */
} while (failures != s->secondariescnt &&
inqueue != 0 &&
logerr("any_of cluster pending %zd metrics "
"(with %zd failed nodes)\n", inqueue, failures) >= -1 &&
usleep((200 + (rand() % 100)) * 1000) <= 0);
/* shut down entire cluster */
for (i = 0; i < s->secondariescnt; i++)
s->secondaries[i]->keep_running = 0;
/* to pretend to be dead for above loop (just in case) */
if (inqueue != 0)
for (i = 0; i < s->secondariescnt; i++)
s->secondaries[i]->failure = 1;
}
s->keep_running = 0;
if ((err = pthread_join(tid, NULL)) != 0)
logerr("%s:%u: failed to join server thread: %s\n",
s->ip, s->port, strerror(err));
if (s->ctype == CON_TCP) {
size_t qlen = queue_len(s->queue);
if (qlen > 0)
logerr("dropping %zd metrics for %s:%u\n",
qlen, s->ip, s->port);
}
queue_destroy(s->queue);
free(s->batch);
if (s->instance)
free(s->instance);
if (s->saddr != NULL)
freeaddrinfo(s->saddr);
free((char *)s->ip);
s->ip = NULL;
}
/**
* Returns the ip address this server points to.
*/
inline const char *
server_ip(server *s)
{
if (s == NULL)
return NULL;
return s->ip;
}
/**
* Returns the port this server connects at.
*/
inline unsigned short
server_port(server *s)
{
if (s == NULL)
return 0;
return s->port;
}
/**
* Returns the instance associated with this server.
*/
inline char *
server_instance(server *s)
{
return s->instance;
}
/**
* Returns the connection type of this server.
*/
inline serv_ctype
server_ctype(server *s)
{
if (s == NULL)
return CON_PIPE;
return s->ctype;
}
/**
* Returns the dispatcher connection handle for this server or -1. The
* handle is valid when this server is of type CON_PIPE, and -1 in that
* case means no connection is currently present.
*/
inline int
server_disp_conn(server *s)
{
if (s == NULL)
return -1;
return s->disp_conn;
}
/**
* Returns whether the last action on this server caused a failure.
*/
inline char
server_failed(server *s)
{
if (s == NULL)
return 0;
return s->failure;
}
/**
* Returns the wall-clock time in microseconds (us) consumed sending metrics.
*/
inline size_t
server_get_ticks(server *s)
{
if (s == NULL)
return 0;
return s->ticks;
}
/**
* Returns the wall-clock time in microseconds (us) consumed since last
* call to this function.
*/
inline size_t
server_get_ticks_sub(server *s)
{
size_t d;
if (s == NULL)
return 0;
d = s->ticks - s->prevticks;
s->prevticks += d;
return d;
}
/**
* Returns the number of metrics sent since start.
*/
inline size_t
server_get_metrics(server *s)
{
if (s == NULL)
return 0;
return s->metrics;
}
/**
* Returns the number of metrics sent since last call to this function.
*/
inline size_t
server_get_metrics_sub(server *s)
{
size_t d;
if (s == NULL)
return 0;
d = s->metrics - s->prevmetrics;
s->prevmetrics += d;
return d;
}
/**
* Returns the number of metrics dropped since start.
*/
inline size_t
server_get_dropped(server *s)
{
if (s == NULL)
return 0;
return s->dropped;
}
/**
* Returns the number of metrics dropped since last call to this function.
*/
inline size_t
server_get_dropped_sub(server *s)
{
size_t d;
if (s == NULL)
return 0;
d = s->dropped - s->prevdropped;
s->prevdropped += d;
return d;
}
/**
* Returns the number of stalls since start. A stall happens when the
* queue is full, but it appears as if it would be a good idea to wait
* for a brief period and retry.
*/
inline size_t
server_get_stalls(server *s)
{
if (s == NULL)
return 0;
return s->stalls;
}
/**
* Returns the number of stalls since last call to this function.
*/
inline size_t
server_get_stalls_sub(server *s)
{
size_t d;
if (s == NULL)
return 0;
d = s->stalls - s->prevstalls;
s->prevstalls += d;
return d;
}
/**
* Returns the (approximate) number of metrics waiting to be sent.
*/
inline size_t
server_get_queue_len(server *s)
{
if (s == NULL)
return 0;
return queue_len(s->queue);
}
/**
* Returns the allocated size of the queue backing metrics waiting to be
* sent.
*/
inline size_t
server_get_queue_size(server *s)
{
if (s == NULL)
return 0;
return queue_size(s->queue);
}