forked from Netflix/dynomite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyn_stats.c
1769 lines (1454 loc) · 54.3 KB
/
dyn_stats.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
/*
* Dynomite - A thin, distributed replication layer for multi non-distributed
* storages. Copyright (C) 2014 Netflix, Inc.
*/
/*
* twemproxy - A fast and lightweight proxy for memcached protocol.
* Copyright (C) 2011 Twitter, Inc.
*
* 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 <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <netinet/in.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "dyn_conf.h"
#include "dyn_connection.h"
#include "dyn_core.h"
#include "dyn_gossip.h"
#include "dyn_histogram.h"
#include "dyn_node_snitch.h"
#include "dyn_ring_queue.h"
#include "dyn_server.h"
struct stats_desc {
char *name; /* stats name */
char *desc; /* stats description */
};
#define DEFINE_ACTION(_name, _type, _desc) \
{.type = _type, .name = string(#_name)},
static struct stats_metric stats_pool_codec[] = {
STATS_POOL_CODEC(DEFINE_ACTION)};
static struct stats_metric stats_server_codec[] = {
STATS_SERVER_CODEC(DEFINE_ACTION)};
#undef DEFINE_ACTION
#define DEFINE_ACTION(_name, _type, _desc) {.name = #_name, .desc = _desc},
static struct stats_desc stats_pool_desc[] = {STATS_POOL_CODEC(DEFINE_ACTION)};
static struct stats_desc stats_server_desc[] = {
STATS_SERVER_CODEC(DEFINE_ACTION)};
#undef DEFINE_ACTION
#define MAX_HTTP_HEADER_SIZE 1024
static struct string header_str = string(
"HTTP/1.1 200 OK \nContent-Type: application/json; charset=utf-8 "
"\nContent-Length:");
// static struct string endline = string("\r\n");
static struct string ok = string("OK\r\n");
static struct string err_resp = string("ERR");
static struct string all = string("all");
void stats_describe(void) {
uint32_t i;
log_stderr("pool stats:");
for (i = 0; i < NELEMS(stats_pool_desc); i++) {
log_stderr(" %-20s\"%s\"", stats_pool_desc[i].name,
stats_pool_desc[i].desc);
}
log_stderr("");
log_stderr("server stats:");
for (i = 0; i < NELEMS(stats_server_desc); i++) {
log_stderr(" %-20s\"%s\"", stats_server_desc[i].name,
stats_server_desc[i].desc);
}
}
static void stats_metric_init(struct stats_metric *stm) {
switch (stm->type) {
case STATS_COUNTER:
stm->value.counter = 0LL;
break;
case STATS_GAUGE:
stm->value.counter = 0LL;
break;
case STATS_TIMESTAMP:
stm->value.timestamp = 0LL;
break;
case STATS_STRING:
string_deinit(&stm->value.str); // first free the existing data
string_init(&stm->value.str);
break;
default:
NOT_REACHED();
}
}
static void stats_metric_reset(struct array *stats_metric) {
uint32_t i, nmetric;
nmetric = array_n(stats_metric);
ASSERT(nmetric == STATS_POOL_NFIELD || nmetric == STATS_SERVER_NFIELD);
for (i = 0; i < nmetric; i++) {
struct stats_metric *stm = array_get(stats_metric, i);
stats_metric_init(stm);
}
}
static rstatus_t stats_pool_metric_init(struct array *stats_metric) {
uint32_t i, nfield = STATS_POOL_NFIELD;
THROW_STATUS(array_init(stats_metric, nfield, sizeof(struct stats_metric)));
for (i = 0; i < nfield; i++) {
struct stats_metric *stm = array_push(stats_metric);
/* initialize from pool codec first */
*stm = stats_pool_codec[i];
/* initialize individual metric */
stats_metric_init(stm);
}
return DN_OK;
}
static rstatus_t stats_server_metric_init(struct stats_server *sts) {
uint32_t i, nfield = STATS_SERVER_NFIELD;
THROW_STATUS(array_init(&sts->metric, nfield, sizeof(struct stats_metric)));
for (i = 0; i < nfield; i++) {
struct stats_metric *stm = array_push(&sts->metric);
/* initialize from server codec first */
*stm = stats_server_codec[i];
/* initialize individual metric */
stats_metric_init(stm);
}
return DN_OK;
}
static void stats_metric_deinit(struct array *metric) {
uint32_t i, nmetric;
nmetric = array_n(metric);
for (i = 0; i < nmetric; i++) {
array_pop(metric);
}
array_deinit(metric);
}
static rstatus_t stats_server_init(struct stats_server *sts,
struct datastore *s) {
sts->name = s->name;
array_null(&sts->metric);
THROW_STATUS(stats_server_metric_init(sts));
log_debug(LOG_VVVERB, "init stats server '%.*s' with %" PRIu32 " metric",
sts->name.len, sts->name.data, array_n(&sts->metric));
return DN_OK;
}
static rstatus_t stats_server_map(struct stats_server *sts,
struct datastore *datastore) {
ASSERT(datastore != NULL);
THROW_STATUS(stats_server_init(sts, datastore));
log_debug(LOG_VVVERB, "mapped stats servers");
return DN_OK;
}
static void stats_server_unmap(struct stats_server *sts) {
stats_metric_deinit(&sts->metric);
log_debug(LOG_VVVERB, "unmap stats servers");
}
static rstatus_t stats_pool_init(struct stats_pool *stp,
struct server_pool *sp) {
rstatus_t status;
stp->name = sp->name;
array_null(&stp->metric);
THROW_STATUS(stats_pool_metric_init(&stp->metric));
status = stats_server_map(&stp->server, sp->datastore);
if (status != DN_OK) {
stats_metric_deinit(&stp->metric);
return status;
}
log_debug(LOG_VVVERB, "init stats pool '%.*s' with %" PRIu32 " metric",
stp->name.len, stp->name.data, array_n(&stp->metric));
return DN_OK;
}
static void stats_pool_reset(struct stats_pool *stp) {
stats_metric_reset(&stp->metric);
struct stats_server *sts = &stp->server;
stats_metric_reset(&sts->metric);
}
static void stats_pool_unmap(struct stats_pool *stp) {
stats_metric_deinit(&stp->metric);
stats_server_unmap(&stp->server);
}
static rstatus_t stats_create_bufs(struct stats *st) {
uint32_t int64_max_digits = 20; /* INT64_MAX = 9223372036854775807 */
uint32_t int32_max_digits = 10; /* INT32_MAX = 4294967294 */
uint32_t key_value_extra = 8; /* "key": "value", */
uint32_t pool_extra = 8; /* '"pool_name": { ' + ' }' */
uint32_t server_extra = 8; /* '"server_name": { ' + ' }' */
size_t size = 0;
ASSERT(st->buf.data == NULL && st->buf.size == 0);
/* header */
size += 1;
size += st->service_str.len;
size += st->service.len;
size += key_value_extra;
size += st->source_str.len;
size += st->source.len;
size += key_value_extra;
size += st->version_str.len;
size += st->version.len;
size += key_value_extra;
size += st->uptime_str.len;
size += int64_max_digits;
size += key_value_extra;
size += st->timestamp_str.len;
size += int64_max_digits;
size += key_value_extra;
size += st->rack_str.len;
size += st->rack.len;
size += key_value_extra;
size += st->dc_str.len;
size += st->dc.len;
size += key_value_extra;
size += st->latency_999th_str.len;
size += int64_max_digits;
size += key_value_extra;
size += st->latency_99th_str.len;
size += int64_max_digits;
size += key_value_extra;
size += st->latency_95th_str.len;
size += int64_max_digits;
size += key_value_extra;
size += st->latency_mean_str.len;
size += int64_max_digits;
size += key_value_extra;
size += st->latency_max_str.len;
size += int64_max_digits;
size += key_value_extra;
size += st->payload_size_999th_str.len;
size += int32_max_digits;
size += key_value_extra;
size += st->payload_size_99th_str.len;
size += int32_max_digits;
size += key_value_extra;
size += st->payload_size_95th_str.len;
size += int32_max_digits;
size += key_value_extra;
size += st->payload_size_mean_str.len;
size += int32_max_digits;
size += key_value_extra;
size += st->payload_size_max_str.len;
size += int32_max_digits;
size += key_value_extra;
size += st->alloc_msgs_str.len;
size += int64_max_digits;
size += key_value_extra;
size += st->free_msgs_str.len;
size += int64_max_digits;
size += key_value_extra;
size += st->alloc_mbufs_str.len;
size += int64_max_digits;
size += key_value_extra;
size += st->free_mbufs_str.len;
size += int64_max_digits;
size += key_value_extra;
size += st->dyn_memory_str.len;
size += int64_max_digits;
size += key_value_extra;
struct stats_pool *stp = &st->sum;
uint32_t j;
size += stp->name.len;
size += pool_extra;
for (j = 0; j < array_n(&stp->metric); j++) {
struct stats_metric *stm = array_get(&stp->metric, j);
size += stm->name.len;
size += int64_max_digits;
size += key_value_extra;
}
/* servers per pool */
struct stats_server *sts = &stp->server;
uint32_t k;
size += sts->name.len;
size += server_extra;
for (k = 0; k < array_n(&sts->metric); k++) {
struct stats_metric *stm = array_get(&sts->metric, k);
size += stm->name.len;
size += int64_max_digits;
size += key_value_extra;
}
/* footer */
size += 2;
// Accommodate for new fields that are directly added using stats_add_num_str
size += 1024;
size = DN_ALIGN(size, DN_ALIGNMENT);
st->buf.data = dn_alloc(size);
if (st->buf.data == NULL) {
log_error("create stats buffer of size %zu failed: %s", size,
strerror(errno));
return DN_ENOMEM;
}
st->buf.size = size;
st->buf.len = 0;
log_debug(LOG_DEBUG, "stats info buffer size %zu", size);
st->clus_desc_buf.len = 0;
st->clus_desc_buf.size = 0;
return DN_OK;
}
static void stats_destroy_buf(struct stats_buffer *buf) {
if (buf->size != 0) {
ASSERT(buf->data != NULL);
dn_free(buf->data);
buf->size = 0;
}
}
static void stats_reset_buf(struct stats_buffer *buf) { buf->len = 0; }
static rstatus_t stats_add_string(struct stats_buffer *buf, struct string *key,
struct string *val) {
uint8_t *pos;
size_t room;
int n;
pos = buf->data + buf->len;
room = buf->size - buf->len - 1;
n = dn_snprintf(pos, room, "\"%.*s\":\"%.*s\",", key->len, key->data,
val->len, val->data);
if (n < 0 || n >= (int)room) {
log_debug(LOG_ERR, "no room size:%u len %u", buf->size, buf->len);
return DN_ERROR;
}
buf->len += (size_t)n;
return DN_OK;
}
static rstatus_t stats_add_num_last(struct stats_buffer *buf,
struct string *key, int64_t val,
bool last) {
uint8_t *pos;
size_t room;
int n;
pos = buf->data + buf->len;
room = buf->size - buf->len - 1;
if (!last) {
n = dn_snprintf(pos, room, "\"%.*s\":%" PRId64 ",\n", key->len, key->data,
val);
} else {
n = dn_snprintf(pos, room, "\"%.*s\":%" PRId64 "\n", key->len, key->data,
val);
}
if (n < 0 || n >= (int)room) {
log_debug(LOG_ERR, "no room size:%u len %u", buf->size, buf->len);
return DN_ERROR;
}
buf->len += (size_t)n;
return DN_OK;
}
static rstatus_t stats_add_num_str(struct stats_buffer *buf, const char *key,
int64_t val) {
uint8_t *pos;
size_t room;
int n;
pos = buf->data + buf->len;
room = buf->size - buf->len - 1;
n = dn_snprintf(pos, room, "\"%s\":%" PRId64 ",\n", key, val);
if (n < 0 || n >= (int)room) {
log_debug(LOG_ERR, "no room size:%u len %u", buf->size, buf->len);
return DN_ERROR;
}
buf->len += (size_t)n;
return DN_OK;
}
static rstatus_t stats_add_num(struct stats_buffer *buf, struct string *key,
int64_t val) {
if (stats_add_num_last(buf, key, val, false) == DN_ERROR) {
return DN_ERROR;
}
return DN_OK;
}
static rstatus_t stats_add_header(struct stats *st) {
struct stats_buffer *buf;
int64_t cur_ts, uptime;
buf = &st->buf;
buf->data[0] = '{';
buf->len = 1;
cur_ts = (int64_t)time(NULL);
uptime = cur_ts - st->start_ts;
THROW_STATUS(stats_add_string(&st->buf, &st->service_str, &st->service));
THROW_STATUS(stats_add_string(&st->buf, &st->source_str, &st->source));
THROW_STATUS(stats_add_string(&st->buf, &st->version_str, &st->version));
THROW_STATUS(stats_add_num(&st->buf, &st->uptime_str, uptime));
THROW_STATUS(stats_add_num(&st->buf, &st->timestamp_str, cur_ts));
THROW_STATUS(stats_add_string(&st->buf, &st->rack_str, &st->rack));
THROW_STATUS(stats_add_string(&st->buf, &st->dc_str, &st->dc));
// latency histogram
THROW_STATUS(stats_add_num(&st->buf, &st->latency_max_str,
(int64_t)st->latency_histo.val_max))
THROW_STATUS(stats_add_num(&st->buf, &st->latency_999th_str,
(int64_t)st->latency_histo.val_999th));
THROW_STATUS(stats_add_num(&st->buf, &st->latency_99th_str,
(int64_t)st->latency_histo.val_99th));
THROW_STATUS(stats_add_num(&st->buf, &st->latency_95th_str,
(int64_t)st->latency_histo.val_95th));
THROW_STATUS(stats_add_num(&st->buf, &st->latency_mean_str,
(int64_t)st->latency_histo.mean));
// payload size histogram
THROW_STATUS(stats_add_num(&st->buf, &st->payload_size_max_str,
(int64_t)st->payload_size_histo.val_max));
THROW_STATUS(stats_add_num(&st->buf, &st->payload_size_999th_str,
(int64_t)st->payload_size_histo.val_999th));
THROW_STATUS(stats_add_num(&st->buf, &st->payload_size_99th_str,
(int64_t)st->payload_size_histo.val_99th));
THROW_STATUS(stats_add_num(&st->buf, &st->payload_size_95th_str,
(int64_t)st->payload_size_histo.val_95th));
THROW_STATUS(stats_add_num(&st->buf, &st->payload_size_mean_str,
(int64_t)st->payload_size_histo.mean));
THROW_STATUS(stats_add_num_str(&st->buf, "average_cross_region_rtt",
(int64_t)st->cross_region_latency_histo.mean));
THROW_STATUS(
stats_add_num_str(&st->buf, "99_cross_region_rtt",
(int64_t)st->cross_region_latency_histo.val_99th));
THROW_STATUS(stats_add_num_str(&st->buf, "average_cross_zone_latency",
(int64_t)st->cross_zone_latency_histo.mean));
THROW_STATUS(
stats_add_num_str(&st->buf, "99_cross_zone_latency",
(int64_t)st->cross_zone_latency_histo.val_99th));
THROW_STATUS(stats_add_num_str(&st->buf, "average_server_latency",
(int64_t)st->server_latency_histo.mean));
THROW_STATUS(stats_add_num_str(&st->buf, "99_server_latency",
(int64_t)st->server_latency_histo.val_99th));
THROW_STATUS(
stats_add_num_str(&st->buf, "average_cross_region_queue_wait",
(int64_t)st->cross_region_queue_wait_time_histo.mean));
THROW_STATUS(stats_add_num_str(
&st->buf, "99_cross_region_queue_wait",
(int64_t)st->cross_region_queue_wait_time_histo.val_99th));
THROW_STATUS(
stats_add_num_str(&st->buf, "average_cross_zone_queue_wait",
(int64_t)st->cross_zone_queue_wait_time_histo.mean));
THROW_STATUS(stats_add_num_str(
&st->buf, "99_cross_zone_queue_wait",
(int64_t)st->cross_zone_queue_wait_time_histo.val_99th));
THROW_STATUS(
stats_add_num_str(&st->buf, "average_server_queue_wait",
(int64_t)st->server_queue_wait_time_histo.mean));
THROW_STATUS(
stats_add_num_str(&st->buf, "99_server_queue_wait",
(int64_t)st->server_queue_wait_time_histo.val_99th));
THROW_STATUS(stats_add_num(&st->buf, &st->client_out_queue_99,
(int64_t)st->client_out_queue.val_99th));
THROW_STATUS(stats_add_num(&st->buf, &st->server_in_queue_99,
(int64_t)st->server_in_queue.val_99th));
THROW_STATUS(stats_add_num(&st->buf, &st->server_out_queue_99,
(int64_t)st->server_out_queue.val_99th));
THROW_STATUS(stats_add_num(&st->buf, &st->dnode_client_out_queue_99,
(int64_t)st->dnode_client_out_queue.val_99th));
THROW_STATUS(stats_add_num(&st->buf, &st->peer_in_queue_99,
(int64_t)st->peer_in_queue.val_99th));
THROW_STATUS(stats_add_num(&st->buf, &st->peer_out_queue_99,
(int64_t)st->peer_out_queue.val_99th));
THROW_STATUS(stats_add_num(&st->buf, &st->remote_peer_out_queue_99,
(int64_t)st->remote_peer_out_queue.val_99th));
THROW_STATUS(stats_add_num(&st->buf, &st->remote_peer_in_queue_99,
(int64_t)st->remote_peer_in_queue.val_99th));
THROW_STATUS(
stats_add_num(&st->buf, &st->alloc_msgs_str, (int64_t)st->alloc_msgs));
THROW_STATUS(
stats_add_num(&st->buf, &st->free_msgs_str, (int64_t)st->free_msgs));
THROW_STATUS(
stats_add_num(&st->buf, &st->alloc_mbufs_str, (int64_t)st->alloc_mbufs));
THROW_STATUS(
stats_add_num(&st->buf, &st->free_mbufs_str, (int64_t)st->free_mbufs));
THROW_STATUS(
stats_add_num(&st->buf, &st->dyn_memory_str, (int64_t)st->dyn_memory));
return DN_OK;
}
static rstatus_t stats_add_footer(struct stats_buffer *buf) {
uint8_t *pos;
if (buf->len == buf->size) {
return DN_ERROR;
}
/* overwrite the last byte and add a new byte */
pos = buf->data + buf->len - 1;
pos[0] = '}';
pos[1] = '\n';
buf->len += 1;
return DN_OK;
}
static rstatus_t stats_begin_nesting(struct stats_buffer *buf,
struct string *key, bool arr) {
uint8_t *pos;
size_t room;
int n;
pos = buf->data + buf->len;
room = buf->size - buf->len - 1;
if (key)
n = dn_snprintf(pos, room, "\"%.*s\": %c", key->len, key->data,
arr ? '[' : '{');
else
n = dn_snprintf(pos, room, "%c", arr ? '[' : '{');
if (n < 0 || n >= (int)room) {
log_debug(LOG_ERR, "failed, len:%u size %u", buf->len, buf->size);
return DN_ERROR;
}
buf->len += (size_t)n;
return DN_OK;
}
static rstatus_t stats_end_nesting(struct stats_buffer *buf, bool arr) {
uint8_t *pos;
pos = buf->data + buf->len;
// if last non-white space character is , remove it
// first count white spaces at end
int space_count = 0;
while (isspace(*(pos - space_count - 1))) {
space_count++;
}
if (*(pos - space_count - 1) == ',') {
// now remove , from end
pos -= (space_count + 1);
buf->len--;
// put white spaces back
while (space_count > 0) {
*pos = *(pos + 1);
pos++;
space_count--;
}
}
// append "},"
if ((buf->len + 2) > buf->size) {
return DN_ERROR;
}
pos[0] = arr ? ']' : '}';
pos[1] = ',';
buf->len += 2;
return DN_OK;
}
static rstatus_t stats_copy_metric(struct stats *st, struct array *metric,
bool trim_comma) {
uint32_t i;
// Do not include last element in loop as we need to check if it gets a comma
for (i = 0; i < array_n(metric) - 1; i++) {
struct stats_metric *stm = array_get(metric, i);
THROW_STATUS(stats_add_num(&st->buf, &stm->name, stm->value.counter));
}
// Last metric inside dyn_o_mite:{} does not get a comma
struct stats_metric *stm = array_get(metric, array_n(metric) - 1);
THROW_STATUS(
stats_add_num_last(&st->buf, &stm->name, stm->value.counter, trim_comma));
return DN_OK;
}
static void stats_aggregate_metric(struct array *dst, struct array *src) {
uint32_t i;
for (i = 0; i < array_n(src); i++) {
struct stats_metric *stm1, *stm2;
stm1 = array_get(src, i);
stm2 = array_get(dst, i);
ASSERT(stm1->type == stm2->type);
switch (stm1->type) {
case STATS_COUNTER:
stm2->value.counter += stm1->value.counter;
break;
case STATS_GAUGE:
stm2->value.counter += stm1->value.counter;
break;
case STATS_TIMESTAMP:
if (stm1->value.timestamp) {
stm2->value.timestamp = stm1->value.timestamp;
}
break;
default:
NOT_REACHED();
}
}
}
static void stats_aggregate(struct stats *st) {
if (st->aggregate == 0) {
log_debug(LOG_PVERB,
"skip aggregate of shadow to sum as generator is slow");
return;
}
log_debug(LOG_PVERB, "aggregate stats shadow %p to sum %p", &st->shadow,
&st->sum);
struct stats_pool *stp1 = &st->shadow;
struct stats_pool *stp2 = &st->sum;
stats_aggregate_metric(&st->sum.metric, &st->shadow.metric);
struct stats_server *sts1, *sts2;
sts1 = &stp1->server;
sts2 = &stp2->server;
stats_aggregate_metric(&sts2->metric, &sts1->metric);
static msec_t last_reset = 0;
if (!last_reset) last_reset = dn_msec_now();
if ((last_reset + 5 * 60 * 1000) < dn_msec_now()) {
st->reset_histogram = 1;
last_reset = dn_msec_now();
}
if (st->reset_histogram) {
st->reset_histogram = 0;
histo_reset(&st->latency_histo);
histo_reset(&st->payload_size_histo);
histo_reset(&st->server_latency_histo);
histo_reset(&st->cross_zone_latency_histo);
histo_reset(&st->cross_region_latency_histo);
histo_reset(&st->server_queue_wait_time_histo);
histo_reset(&st->cross_zone_queue_wait_time_histo);
histo_reset(&st->cross_region_queue_wait_time_histo);
histo_reset(&st->server_in_queue);
histo_reset(&st->server_out_queue);
histo_reset(&st->client_out_queue);
histo_reset(&st->dnode_client_out_queue);
histo_reset(&st->peer_in_queue);
histo_reset(&st->peer_out_queue);
histo_reset(&st->remote_peer_in_queue);
histo_reset(&st->remote_peer_out_queue);
}
st->aggregate = 0;
}
static rstatus_t stats_make_info_rsp(struct stats *st) {
THROW_STATUS(stats_add_header(st));
struct stats_pool *stp = &st->sum;
THROW_STATUS(stats_begin_nesting(&st->buf, &stp->name, false));
/* copy pool metric from sum(c) to buffer */
THROW_STATUS(stats_copy_metric(st, &stp->metric, false));
struct stats_server *sts = &stp->server;
THROW_STATUS(stats_begin_nesting(&st->buf, &sts->name, false));
/* copy server metric from sum(c) to buffer */
THROW_STATUS(stats_copy_metric(st, &sts->metric, true));
THROW_STATUS(stats_end_nesting(&st->buf, false));
THROW_STATUS(stats_end_nesting(&st->buf, false));
THROW_STATUS(stats_add_footer(&st->buf));
return DN_OK;
}
static rstatus_t get_host_from_pname(struct string *host,
struct string *pname) {
uint8_t *found = dn_strchr(pname->data, &pname->data[pname->len], ':');
string_init(host);
if (found) {
size_t hostlen = found - pname->data;
THROW_STATUS(string_copy(host, pname->data, hostlen));
return DN_OK;
}
THROW_STATUS(string_copy(host, pname->data, pname->len));
return DN_OK;
}
static rstatus_t stats_add_node_host(struct stats *st,
struct gossip_node *node) {
struct string host_str;
string_set_text(&host_str, "host");
struct server_pool *sp = &st->ctx->pool;
struct string host;
// pname is host:port. for local its 0.0.0.0:port
// so try to get the hostname if local otherwise use whats in pname
char *hn = NULL;
if (node->is_local && (hn = get_public_hostname(sp))) {
THROW_STATUS(string_copy(&host, hn, dn_strlen(hn)));
} else
get_host_from_pname(&host, &node->pname);
THROW_STATUS(stats_add_string(&st->clus_desc_buf, &host_str, &host));
string_deinit(&host);
return DN_OK;
}
static rstatus_t stats_add_node_name(struct stats *st,
struct gossip_node *node) {
struct string name_str;
string_set_text(&name_str, "name");
struct server_pool *sp = &st->ctx->pool;
// name is the ip address
if (node->is_local) {
// get the ip aka name
struct string ip;
char *ip4 = get_public_ip4(sp);
if (ip4) {
string_set_raw(&ip, ip4);
THROW_STATUS(stats_add_string(&st->clus_desc_buf, &name_str, &ip));
} else
THROW_STATUS(
stats_add_string(&st->clus_desc_buf, &name_str, &node->name));
} else {
THROW_STATUS(stats_add_string(&st->clus_desc_buf, &name_str, &node->name));
}
return DN_OK;
}
static rstatus_t stats_add_node_details(struct stats *st,
struct gossip_node *node) {
struct string port_str, token_str;
string_set_text(&port_str, "port");
string_set_text(&token_str, "token");
THROW_STATUS(stats_add_node_name(st, node));
THROW_STATUS(stats_add_node_host(st, node));
THROW_STATUS(stats_add_num(&st->clus_desc_buf, &port_str, node->port));
THROW_STATUS(stats_add_num_last(&st->clus_desc_buf, &token_str,
*(node->token.mag), true));
return DN_OK;
}
static rstatus_t stats_add_rack_details(struct stats *st,
struct gossip_rack *rack) {
struct string name_str, servers_str;
string_set_text(&name_str, "name");
string_set_text(&servers_str, "servers");
THROW_STATUS(stats_add_string(&st->clus_desc_buf, &name_str, &rack->name));
// servers : [
THROW_STATUS(stats_begin_nesting(&st->clus_desc_buf, &servers_str, true));
uint32_t ni;
for (ni = 0; ni < array_n(&rack->nodes); ni++) {
struct gossip_node *node = array_get(&rack->nodes, ni);
THROW_STATUS(stats_begin_nesting(&st->clus_desc_buf, NULL, false));
THROW_STATUS(stats_add_node_details(st, node));
THROW_STATUS(stats_end_nesting(&st->clus_desc_buf, false));
}
THROW_STATUS(stats_end_nesting(&st->clus_desc_buf, true));
return DN_OK;
}
static rstatus_t stats_add_dc_details(struct stats *st, struct gossip_dc *dc) {
struct string name_str, racks_str;
string_set_text(&name_str, "name");
string_set_text(&racks_str, "racks");
THROW_STATUS(stats_add_string(&st->clus_desc_buf, &name_str, &dc->name));
// racks : [
THROW_STATUS(stats_begin_nesting(&st->clus_desc_buf, &racks_str, true));
uint32_t ri;
for (ri = 0; ri < array_n(&dc->racks); ri++) {
struct gossip_rack *rack = array_get(&dc->racks, ri);
THROW_STATUS(stats_begin_nesting(&st->clus_desc_buf, NULL, false));
THROW_STATUS(stats_add_rack_details(st, rack));
THROW_STATUS(stats_end_nesting(&st->clus_desc_buf, false));
}
THROW_STATUS(stats_end_nesting(&st->clus_desc_buf, true));
return DN_OK;
}
static rstatus_t stats_resize_clus_desc_buf(struct stats *st) {
struct server_pool *sp = &st->ctx->pool;
ASSERT(sp);
size_t size = 1024 * array_n(&sp->peers);
size = DN_ALIGN(size, DN_ALIGNMENT);
if (st->clus_desc_buf.size < size) {
stats_destroy_buf(&st->clus_desc_buf);
st->clus_desc_buf.data = dn_alloc(size);
if (st->clus_desc_buf.data == NULL) {
log_error("create cluster desc buffer of size %zu failed: %s", size,
strerror(errno));
return DN_ENOMEM;
}
st->clus_desc_buf.size = size;
}
stats_reset_buf(&st->clus_desc_buf);
return DN_OK;
}
static rstatus_t stats_make_cl_desc_rsp(struct stats *st) {
THROW_STATUS(stats_resize_clus_desc_buf(st));
THROW_STATUS(stats_begin_nesting(&st->clus_desc_buf, NULL, false));
struct string dcs_str;
string_set_text(&dcs_str, "dcs");
THROW_STATUS(stats_begin_nesting(&st->clus_desc_buf, &dcs_str, true));
uint32_t di;
for (di = 0; di < array_n(&gn_pool.datacenters); di++) {
struct gossip_dc *dc = array_get(&gn_pool.datacenters, di);
THROW_STATUS(stats_begin_nesting(&st->clus_desc_buf, NULL, false));
THROW_STATUS(stats_add_dc_details(st, dc));
THROW_STATUS(stats_end_nesting(&st->clus_desc_buf, false));
}
THROW_STATUS(stats_end_nesting(&st->clus_desc_buf, true));
THROW_STATUS(stats_add_footer(&st->clus_desc_buf));
return DN_OK;
}
static void parse_request(int sd, struct stats_cmd *st_cmd) {
size_t max_buf_size = 65535;
char mesg[max_buf_size], *reqline[3];
int rcvd;
memset((void *)mesg, (int)'\0', max_buf_size);
rcvd = recv(sd, mesg, max_buf_size, 0);
if (rcvd < 0) {
log_debug(LOG_VERB, "stats recv error");
} else if (rcvd == 0) { // receive socket closed
log_debug(LOG_VERB, "Client disconnected upexpectedly");
} else { // message received
log_debug(LOG_VERB, "%s", mesg);
reqline[0] = strtok(mesg, " \t\n");
if (!reqline[0]) {
return;
}
if (strncmp(reqline[0], "GET\0", 4) == 0) {
reqline[1] = strtok(NULL, " \t");
reqline[2] = strtok(NULL, " \t\n");
log_debug(LOG_VERB, "0: %s\n", reqline[0]);
log_debug(LOG_VERB, "1: %s\n", reqline[1]);
log_debug(LOG_VERB, "2: %s\n", reqline[2]);
if (!reqline[1] || !reqline[2] ||
(strncmp(reqline[2], "HTTP/1.0", 8) != 0 &&
strncmp(reqline[2], "HTTP/1.1", 8) != 0)) {
ssize_t wrote = write(sd, "HTTP/1.0 400 Bad Request\n", 25);
IGNORE_RET_VAL(wrote);
st_cmd->cmd = CMD_UNKNOWN;
return;
} else {
if (strncmp(reqline[1], "/\0", 2) == 0) {
reqline[1] = "/info";
return;
} else if (strcmp(reqline[1], "/info") == 0) {
st_cmd->cmd = CMD_INFO;
return;
} else if (strcmp(reqline[1], "/help") == 0) {
st_cmd->cmd = CMD_HELP;
return;
} else if (strcmp(reqline[1], "/ping") == 0) {
st_cmd->cmd = CMD_PING;
return;
} else if (strncmp(reqline[1], "/setloglevel",
dn_strlen("/setloglevel")) == 0) {
st_cmd->cmd = CMD_SET_LOG_LEVEL;
log_notice("Setting loglevel: %s", reqline[1]);
char *val = reqline[1] + dn_strlen("/setloglevel");
if (*val != '/') {
st_cmd->cmd = CMD_UNKNOWN;
return;
} else {
val++;
string_init(&st_cmd->req_data);
string_copy_c(&st_cmd->req_data, val);
}
return;
} else if (strcmp(reqline[1], "/loglevelup") == 0) {
st_cmd->cmd = CMD_LOG_LEVEL_UP;
return;
} else if (strcmp(reqline[1], "/logleveldown") == 0) {
st_cmd->cmd = CMD_LOG_LEVEL_DOWN;
return;
} else if (strcmp(reqline[1], "/historeset") == 0) {
st_cmd->cmd = CMD_HISTO_RESET;
return;
} else if (strcmp(reqline[1], "/cluster_describe") == 0) {
st_cmd->cmd = CMD_CL_DESCRIBE;
return;
} else if (strcmp(reqline[1], "/get_consistency") == 0) {
st_cmd->cmd = CMD_GET_CONSISTENCY;
return;
} else if (strncmp(reqline[1], "/set_consistency", 16) == 0) {
st_cmd->cmd = CMD_SET_CONSISTENCY;
log_notice("Setting consistency parameters: %s", reqline[1]);