-
Notifications
You must be signed in to change notification settings - Fork 16
/
aggregator_handlers.go
957 lines (900 loc) · 45.3 KB
/
aggregator_handlers.go
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
// Copyright 2022 V Kontakte LLC
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package aggregator
import (
"context"
"encoding/binary"
"fmt"
"math/rand"
"time"
"go4.org/mem"
"github.com/pierrec/lz4"
"github.com/vkcom/statshouse/internal/vkgo/basictl"
"github.com/vkcom/statshouse/internal/vkgo/rpc"
"github.com/vkcom/statshouse/internal/data_model"
"github.com/vkcom/statshouse/internal/data_model/gen2/constants"
"github.com/vkcom/statshouse/internal/data_model/gen2/tlstatshouse"
"github.com/vkcom/statshouse/internal/format"
)
type userData struct {
sendSourceBucket tlstatshouse.SendSourceBucketBytes
sendSourceBucket2 tlstatshouse.SendSourceBucket2Bytes
sourceBucket tlstatshouse.SourceBucketBytes
sourceBucket2 tlstatshouse.SourceBucket2Bytes
sendKeepAlive tlstatshouse.SendKeepAliveBytes
sendKeepAlive2 tlstatshouse.SendKeepAlive2Bytes
getMetrics tlstatshouse.GetMetrics
getMetrics2 tlstatshouse.GetMetrics2
getMetrics3 tlstatshouse.GetMetrics3
getTagMapping tlstatshouse.GetTagMappingBytes
getTagMapping2 tlstatshouse.GetTagMapping2Bytes
getTagBoostrap tlstatshouse.GetTagMappingBootstrapBytes
getConfig tlstatshouse.GetConfig
getConfig2 tlstatshouse.GetConfig2
testConnection tlstatshouse.TestConnectionBytes
testConneection2 tlstatshouse.TestConnection2Bytes
getTargets tlstatshouse.GetTargetsBytes
getTargets2 tlstatshouse.GetTargets2Bytes
autoCreate tlstatshouse.AutoCreate
uncompressed []byte
}
func getUserData(hctx *rpc.HandlerContext) *userData {
ud, ok := hctx.UserData.(*userData)
if !ok {
ud = &userData{}
hctx.UserData = ud
}
return ud
}
func (a *Aggregator) handleClient(ctx context.Context, hctx *rpc.HandlerContext) error {
tag, _ := basictl.NatPeekTag(hctx.Request)
keyID := hctx.KeyID()
keyIDTag := int32(binary.BigEndian.Uint32(keyID[:4]))
key := data_model.AggKey(0, format.BuiltinMetricIDRPCRequests, [16]int32{0, format.TagValueIDComponentAggregator, int32(tag), format.TagValueIDRPCRequestsStatusOK, 0, 0, keyIDTag}, a.aggregatorHost, a.shardKey, a.replicaKey)
err := a.handleClientImpl(ctx, hctx)
if rpc.IsHijackedResponse(err) {
key.Keys[3] = format.TagValueIDRPCRequestsStatusHijack
} else if err != nil {
key.Keys[3] = format.TagValueIDRPCRequestsStatusErrLocal
}
a.sh2.AddValueCounter(key, float64(len(hctx.Request)), 1, nil)
return err
}
func (a *Aggregator) handleClientImpl(ctx context.Context, hctx *rpc.HandlerContext) error {
var tag uint32
tag, hctx.Request, _ = basictl.NatReadTag(hctx.Request)
switch tag {
case constants.StatshouseGetConfig:
ud := getUserData(hctx)
_, err := ud.getConfig.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.getConfig request: %w", err)
}
return a.handleGetConfig(ctx, hctx, ud.getConfig)
case constants.StatshouseGetConfig2:
ud := getUserData(hctx)
_, err := ud.getConfig2.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.getConfig2 request: %w", err)
}
return a.handleGetConfig2(ctx, hctx, ud.getConfig2)
case constants.StatshouseGetMetrics:
ud := getUserData(hctx)
_, err := ud.getMetrics.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.getMetrics request: %w", err)
}
gm2 := tlstatshouse.GetMetrics2{
Header: tlstatshouse.CommonProxyHeader{
ShardReplica: ud.getMetrics.ShardReplica,
ShardReplicaTotal: ud.getMetrics.ShardReplicaTotal,
HostName: ud.getMetrics.HostName,
BuildArch: ud.getMetrics.BuildArch,
},
Version: ud.getMetrics.Version,
}
if ud.getMetrics.IsSetIngressProxy() {
gm2.Header.SetIngressProxy(true, &gm2.FieldsMask)
}
// TODO - remove ASAP, because if fields mask is added to getMetrics2, we are in trouble
return a.metricStorage.Journal().HandleGetMetrics(ctx, hctx, gm2)
case constants.StatshouseGetMetrics2:
ud := getUserData(hctx)
_, err := ud.getMetrics2.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.getMetrics2 request: %w", err)
}
return a.metricStorage.Journal().HandleGetMetrics(ctx, hctx, ud.getMetrics2)
case constants.StatshouseGetMetrics3:
ud := getUserData(hctx)
_, err := ud.getMetrics3.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.getMetrics3 request: %w", err)
}
return a.metricStorage.Journal().HandleGetMetrics3(ctx, hctx, ud.getMetrics3)
case constants.StatshouseGetTagMapping:
ud := getUserData(hctx)
_, err := ud.getTagMapping.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.getTagMapping request: %w", err)
}
gt2 := tlstatshouse.GetTagMapping2Bytes{
Header: tlstatshouse.CommonProxyHeaderBytes{
ShardReplica: ud.getTagMapping.ShardReplica,
ShardReplicaTotal: ud.getTagMapping.ShardReplicaTotal,
HostName: ud.getTagMapping.HostName,
BuildArch: ud.getTagMapping.BuildArch,
},
Metric: ud.getTagMapping.Metric,
Key: ud.getTagMapping.Key,
TagIdKey: ud.getTagMapping.TagIdKey,
ClientEnv: ud.getTagMapping.ClientEnv,
}
if ud.getTagMapping.IsSetIngressProxy() {
gt2.Header.SetIngressProxy(true, >2.FieldsMask)
}
if ud.getTagMapping.IsSetCreate() {
gt2.SetCreate(true)
}
if !ud.getTagMapping.IsSetCanonicalMetricName() {
return fmt.Errorf("non-canonical metric names no more supported, please upgrade statshouse")
}
return a.tagsMapper.handleCreateTagMapping(ctx, hctx, gt2)
case constants.StatshouseGetTagMapping2:
ud := getUserData(hctx)
_, err := ud.getTagMapping2.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.getTagMapping2 request: %w", err)
}
return a.tagsMapper.handleCreateTagMapping(ctx, hctx, ud.getTagMapping2)
case constants.StatshouseGetTagMappingBootstrap:
ud := getUserData(hctx)
_, err := ud.getTagBoostrap.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.getTagMappingBootstrap request: %w", err)
}
hctx.Response = append(hctx.Response, a.tagMappingBootstrapResponse...)
return nil
case constants.StatshouseSendKeepAlive:
{
ud := getUserData(hctx)
_, err := ud.sendKeepAlive.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.sendKeepAlive request: %w", err)
}
return a.handleKeepAlive(ctx, hctx, ud.sendKeepAlive)
}
case constants.StatshouseSendKeepAlive2:
{
ud := getUserData(hctx)
_, err := ud.sendKeepAlive2.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.sendKeepAlive2 request: %w", err)
}
return a.handleKeepAlive2(ctx, hctx, ud.sendKeepAlive2)
}
case constants.StatshouseSendSourceBucket:
{
ud := getUserData(hctx)
rawSize := len(hctx.Request)
if rawSize < 4 {
return fmt.Errorf("expecting compressed data with size at least 4, actual size %d", rawSize)
}
_, err := ud.sendSourceBucket.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.sendSourceBucket request: %w", err)
}
readFrom := ud.sendSourceBucket.CompressedData
if int(ud.sendSourceBucket.OriginalSize) != len(ud.sendSourceBucket.CompressedData) {
if ud.sendSourceBucket.OriginalSize > data_model.MaxUncompressedBucketSize {
return fmt.Errorf("failed to deserialize compressed statshouse.sourceBucket - uncompressed size %d too big", ud.sendSourceBucket.OriginalSize)
}
ud.uncompressed = append(ud.uncompressed[:0], make([]byte, int(ud.sendSourceBucket.OriginalSize))...)
s, err := lz4.UncompressBlock(ud.sendSourceBucket.CompressedData, ud.uncompressed)
if err != nil {
return fmt.Errorf("failed to deserialize compressed statshouse.sourceBucket: %w", err)
}
if s != int(ud.sendSourceBucket.OriginalSize) {
return fmt.Errorf("failed to deserialize compressed statshouse.sourceBucket request: expected size %d actual %d", ud.sendSourceBucket.OriginalSize, s)
}
ud.uncompressed = append(ud.uncompressed, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) // Append MissedSeconds, AgentEnv, IngestionStatusOK (TODO remove when all statshouses are updated)
readFrom = ud.uncompressed
}
tag2, _ := basictl.NatPeekTag(readFrom)
if tag2 == constants.StatshouseSourceBucket2 { // new version of bucket
_, err = ud.sourceBucket2.ReadBoxed(readFrom)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.sourceBucket2: %w", err)
}
ssb2 := tlstatshouse.SendSourceBucket2Bytes{
Header: tlstatshouse.CommonProxyHeaderBytes{
ShardReplica: ud.sendSourceBucket.ShardReplica,
ShardReplicaTotal: ud.sendSourceBucket.ShardReplicaTotal,
HostName: ud.sendSourceBucket.HostName,
BuildArch: ud.sendSourceBucket.BuildArch,
},
Time: ud.sendSourceBucket.Time,
BuildCommit: ud.sendSourceBucket.BuildCommit,
BuildCommitDate: ud.sendSourceBucket.BuildCommitDate,
BuildCommitTs: ud.sendSourceBucket.BuildCommitTs,
QueueSizeDisk: ud.sendSourceBucket.QueueSizeDisk,
QueueSizeMemory: ud.sendSourceBucket.QueueSizeMemory,
OriginalSize: ud.sendSourceBucket.OriginalSize,
CompressedData: ud.sendSourceBucket.CompressedData,
}
if ud.sendSourceBucket.IsSetIngressProxy() {
ssb2.Header.SetIngressProxy(true, &ssb2.FieldsMask)
}
if ud.sendSourceBucket.IsSetHistoric() {
ssb2.SetHistoric(true)
}
if ud.sendSourceBucket.IsSetSpare() {
ssb2.SetSpare(true)
}
return a.handleClientBucket2(ctx, hctx, ssb2, ud.sendSourceBucket.IsSetShardReplica(), ud.sourceBucket2, rawSize)
}
_, err = ud.sourceBucket.ReadBoxed(readFrom)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.sourceBucket: %w", err)
}
return a.handleClientBucket(ctx, hctx, ud.sendSourceBucket, ud.sourceBucket, rawSize)
}
case constants.StatshouseSendSourceBucket2:
{
ud := getUserData(hctx)
rawSize := len(hctx.Request)
_, err := ud.sendSourceBucket2.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.sendSourceBucket2 request: %w", err)
}
var readFrom []byte
if int(ud.sendSourceBucket2.OriginalSize) != len(ud.sendSourceBucket2.CompressedData) {
if ud.sendSourceBucket2.OriginalSize > data_model.MaxUncompressedBucketSize {
return fmt.Errorf("failed to deserialize compressed statshouse.sourceBucket - uncompressed size %d too big", ud.sendSourceBucket2.OriginalSize)
}
ud.uncompressed = append(ud.uncompressed[:0], make([]byte, int(ud.sendSourceBucket2.OriginalSize))...)
s, err := lz4.UncompressBlock(ud.sendSourceBucket2.CompressedData, ud.uncompressed)
if err != nil {
return fmt.Errorf("failed to deserialize compressed statshouse.sourceBucket: %w", err)
}
if s != int(ud.sendSourceBucket2.OriginalSize) {
return fmt.Errorf("failed to deserialize compressed statshouse.sourceBucket request: expected size %d actual %d", ud.sendSourceBucket2.OriginalSize, s)
}
ud.uncompressed = append(ud.uncompressed, 0, 0, 0, 0) // ingestion_status_ok2, TODO - remove when all agent are updated to version 1.0
readFrom = ud.uncompressed
} else {
ud.sendSourceBucket2.CompressedData = append(ud.sendSourceBucket2.CompressedData, 0, 0, 0, 0) // ingestion_status_ok2, TODO - remove when all agent are updated to version 1.0
readFrom = ud.sendSourceBucket2.CompressedData
}
// Uncomment if you change compressed bucket format
// tag, _ := basictl.NatPeekTag(readFrom)
// if tag == constants.StatshouseSourceBucket {
// _, err = ud.sourceBucket.ReadBoxed(readFrom)
// if err != nil {
// return fmt.Errorf("failed to deserialize statshouse.sourceBucket: %w", err)
// }
// return a.handleClientBucket(ctx, hctx, ud.sendSourceBucket, ud.sourceBucket, rawSize)
// }
_, err = ud.sourceBucket2.ReadBoxed(readFrom)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.sourceBucket2: %w", err)
}
return a.handleClientBucket2(ctx, hctx, ud.sendSourceBucket2, true, ud.sourceBucket2, rawSize)
}
case constants.StatshouseTestConnection:
{
ud := getUserData(hctx)
_, err := ud.testConnection.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.testConnection request: %w", err)
}
tc2 := tlstatshouse.TestConnection2Bytes{
Header: tlstatshouse.CommonProxyHeaderBytes{
ShardReplica: ud.testConnection.ShardReplica,
ShardReplicaTotal: ud.testConnection.ShardReplicaTotal,
},
Payload: ud.testConnection.Payload,
ResponseSize: ud.testConnection.ResponseSize,
ResponseTimeoutSec: ud.testConnection.ResponseTimeoutSec,
}
if ud.testConnection.IsSetIngressProxy() {
tc2.Header.SetIngressProxy(true, &tc2.FieldsMask)
}
return a.testConnection.handleTestConnection(ctx, hctx, tc2)
}
case constants.StatshouseTestConnection2:
{
ud := getUserData(hctx)
_, err := ud.testConneection2.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.testConneection2 request: %w", err)
}
return a.testConnection.handleTestConnection(ctx, hctx, ud.testConneection2)
}
case constants.StatshouseGetTargets:
{
if a.promUpdater == nil {
return fmt.Errorf("aggregator not configured for prometheus")
}
ud := getUserData(hctx)
_, err := ud.getTargets.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.getTargets request: %w", err)
}
// We do not set host name here in header, because it was wrong host name
gt2 := tlstatshouse.GetTargets2Bytes{
Header: tlstatshouse.CommonProxyHeaderBytes{
ShardReplica: ud.getTargets.ShardReplica,
ShardReplicaTotal: ud.getTargets.ShardReplicaTotal,
BuildArch: ud.getTargets.BuildArch,
},
OldHash: ud.getTargets.OldHash,
PromHostName: ud.getTargets.HostName,
}
if ud.getTargets.IsSetIngressProxy() {
gt2.Header.SetIngressProxy(true, >2.FieldsMask)
}
return a.promUpdater.HandleGetTargets(ctx, hctx, gt2)
}
case constants.StatshouseGetTargets2:
{
if a.promUpdater == nil {
return fmt.Errorf("aggregator not configured for prometheus")
}
ud := getUserData(hctx)
_, err := ud.getTargets2.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.getTargets2 request: %w", err)
}
return a.promUpdater.HandleGetTargets(ctx, hctx, ud.getTargets2)
}
case constants.StatshouseAutoCreate:
{
if a.autoCreate == nil {
return rpc.Error{
Code: data_model.RPCErrorNoAutoCreate,
Description: "aggregator not configured for auto-create",
}
}
ud := getUserData(hctx)
_, err := ud.autoCreate.Read(hctx.Request)
if err != nil {
return fmt.Errorf("failed to deserialize statshouse.autoCreate request: %w", err)
}
return a.autoCreate.handleAutoCreate(ctx, hctx, ud.autoCreate)
}
}
return rpc.ErrNoHandler
}
func (a *Aggregator) getConfigResult() tlstatshouse.GetConfigResult {
return tlstatshouse.GetConfigResult{
Addresses: a.addresses,
MaxAddressesCount: int32(len(a.addresses)), // TODO - support reducing list,
PreviousAddresses: int32(a.config.PreviousNumShards),
}
}
func (a *Aggregator) getAgentEnv(isEnvStaging bool) int32 {
if isEnvStaging {
return format.TagValueIDStaging
}
return format.TagValueIDProduction
}
func (a *Aggregator) handleGetConfig(_ context.Context, hctx *rpc.HandlerContext, args tlstatshouse.GetConfig) (err error) {
now := time.Now()
host := a.tagsMapper.mapHost(now, []byte(args.HostName), format.BuiltinMetricNameBudgetHost, false)
// hack - we pass host through key0, because we can not yet set per metric host
agentEnv := int32(format.TagValueIDProduction)
buildArch := format.FilterBuildArch(args.BuildArch)
route := int32(format.TagValueIDRouteDirect) // all config routes are direct
if args.IsSetCluster() && args.Cluster != a.config.Cluster {
key := data_model.AggKey(0, format.BuiltinMetricIDAutoConfig, [16]int32{0, 0, 0, 0, format.TagValueIDAutoConfigWrongCluster}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.sh2.AddCounterHost(key.WithAgentEnvRouteArch(agentEnv, route, buildArch), 1, host, nil)
return fmt.Errorf("Statshouse misconfiguration! cluster requested %q does not match actual cluster connected %q", args.Cluster, a.config.Cluster)
}
if !args.IsSetCluster() && a.config.Cluster != DefaultConfigAggregator().Cluster {
key := data_model.AggKey(0, format.BuiltinMetricIDAutoConfig, [16]int32{0, 0, 0, 0, format.TagValueIDAutoConfigWrongCluster}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.sh2.AddCounterHost(key.WithAgentEnvRouteArch(agentEnv, route, buildArch), 1, host, nil)
return fmt.Errorf("Statshouse misconfiguration! configuration request without cluster is supported only by default %q cluster, but cluster connected is %q", DefaultConfigAggregator().Cluster, a.config.Cluster)
}
key := data_model.AggKey(0, format.BuiltinMetricIDAutoConfig, [16]int32{0, 0, 0, 0, format.TagValueIDAutoConfigOK}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.sh2.AddCounterHost(key.WithAgentEnvRouteArch(agentEnv, route, buildArch), 1, host, nil)
ret := a.getConfigResult()
hctx.Response, err = args.WriteResult(hctx.Response, ret)
return err
}
func (a *Aggregator) handleGetConfig2(_ context.Context, hctx *rpc.HandlerContext, args tlstatshouse.GetConfig2) (err error) {
now := time.Now()
host := a.tagsMapper.mapHost(now, []byte(args.Header.HostName), format.BuiltinMetricNameBudgetHost, false)
// hack - we pass host through key0, because we can not yet set per metric host
agentEnv := a.getAgentEnv(args.Header.IsSetAgentEnvStaging(args.FieldsMask))
buildArch := format.FilterBuildArch(args.Header.BuildArch)
route := int32(format.TagValueIDRouteDirect) // all config routes are direct
if args.Cluster != a.config.Cluster {
key := data_model.AggKey(0, format.BuiltinMetricIDAutoConfig, [16]int32{0, 0, 0, 0, format.TagValueIDAutoConfigWrongCluster}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.sh2.AddCounterHost(key.WithAgentEnvRouteArch(agentEnv, route, buildArch), 1, host, nil)
return fmt.Errorf("Statshouse misconfiguration! cluster requested %q does not match actual cluster connected %q", args.Cluster, a.config.Cluster)
}
key := data_model.AggKey(0, format.BuiltinMetricIDAutoConfig, [16]int32{0, 0, 0, 0, format.TagValueIDAutoConfigOK}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.sh2.AddCounterHost(key.WithAgentEnvRouteArch(agentEnv, route, buildArch), 1, host, nil)
ret := a.getConfigResult()
hctx.Response, err = args.WriteResult(hctx.Response, ret)
return err
}
func (a *Aggregator) handleClientBucket(_ context.Context, hctx *rpc.HandlerContext, args tlstatshouse.SendSourceBucketBytes, bucket tlstatshouse.SourceBucketBytes, rawSize int) (err error) {
if !args.IsSetHistoric() && a.config.SimulateRandomErrors > 0 && rand.Float64() < a.config.SimulateRandomErrors { // SimulateRandomErrors > 0 is optimization
a.mu.Lock()
defer a.mu.Unlock()
aggBucket := a.recentBuckets[len(a.recentBuckets)-1]
aggBucket.contributorsSimulatedErrors = append(aggBucket.contributorsSimulatedErrors, hctx)
return hctx.HijackResponse()
}
now := time.Now()
receiveDelay := now.Sub(time.Unix(int64(args.Time), 0)).Seconds()
host := a.tagsMapper.mapHost(now, args.HostName, format.BuiltinMetricNameBudgetHost, false)
agentEnv := int32(format.TagValueIDProduction)
buildArch := format.FilterBuildArch(args.BuildArch)
// opportunistic mapping. We do not map addrStr. To find hosts with hostname not set use internal_log
var aggBucket *aggregatorBucket
a.mu.Lock()
if args.IsSetShardReplica() { // Skip old versions not yet updated
if err := a.checkShardConfiguration(args.ShardReplica, args.ShardReplicaTotal); err != nil {
key := data_model.AggKey(0, format.BuiltinMetricIDAutoConfig, [16]int32{0, 0, 0, 0, format.TagValueIDAutoConfigErrorSend, args.ShardReplica, args.ShardReplicaTotal}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.mu.Unlock()
a.sh2.AddCounterHost(key, 1, host, nil)
return err // TODO - return code so clients will print into log and discard data
}
}
OldestTime := a.recentBuckets[0].time
NewestTime := a.recentBuckets[len(a.recentBuckets)-1].time
if args.IsSetHistoric() {
if args.Time > NewestTime {
key := data_model.AggKey(0, format.BuiltinMetricIDTimingErrors, [16]int32{0, format.TagValueIDTimingFutureBucketHistoric}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.mu.Unlock()
a.sh2.AddValueCounterHost(key, float64(args.Time-NewestTime), 1, host)
// We discard, because otherwise clients will flood aggregators with this data
hctx.Response, err = args.WriteResult(hctx.Response, []byte("historic bucket time is too far in the future"))
return err
}
if OldestTime >= data_model.MaxHistoricWindow+data_model.MaxHistoricWindowLag && args.Time < OldestTime-data_model.MaxHistoricWindow-data_model.MaxHistoricWindowLag {
key := data_model.AggKey(0, format.BuiltinMetricIDTimingErrors, [16]int32{0, format.TagValueIDTimingLongWindowThrownAggregator}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.mu.Unlock()
a.sh2.AddValueCounterHost(key, float64(NewestTime-args.Time), 1, host)
hctx.Response, err = args.WriteResult(hctx.Response, []byte("Successfully discarded historic bucket beyond historic window"))
return err
}
if args.Time < OldestTime {
aggBucket = a.historicBuckets[args.Time]
if aggBucket == nil {
aggBucket = &aggregatorBucket{time: args.Time}
a.historicBuckets[args.Time] = aggBucket
if a.sendHistoricCondition() {
a.cond.Broadcast() // we are not sure that Signal is enough
}
}
} else {
// If source receives error from recent conveyor quickly, it will come to spare while bucket is still recent
// This is useful optimization, because can save half inserts
aggBucket = a.recentBuckets[args.Time-OldestTime]
}
} else {
if args.Time > NewestTime { // AgentShard too far in a future
key := data_model.AggKey(0, format.BuiltinMetricIDTimingErrors, [16]int32{0, format.TagValueIDTimingFutureBucketRecent}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.mu.Unlock()
a.sh2.AddValueCounterHost(key, float64(args.Time-NewestTime), 1, host)
// We discard, because otherwise clients will flood aggregators with this data
hctx.Response, err = args.WriteResult(hctx.Response, []byte("bucket time is too far in the future"))
return err
}
if args.Time < OldestTime {
key := data_model.AggKey(0, format.BuiltinMetricIDTimingErrors, [16]int32{0, format.TagValueIDTimingLateRecent}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.mu.Unlock()
a.sh2.AddValueCounterHost(key, float64(NewestTime-args.Time), 1, host)
return rpc.Error{
Code: data_model.RPCErrorMissedRecentConveyor,
Description: "bucket time is too far in the past for recent conveyor",
}
}
aggBucket = a.recentBuckets[args.Time-OldestTime]
}
aggBucket.sendMu.RLock()
// This lock order ensures, that if sender gets a.mu.Lock(), then all aggregating clients already have aggBucket.sendMu.RLock()
if args.IsSetSpare() {
aggBucket.contributorsSpare.AddValueCounterHost(0, 1, host) // protected by a.mu
} else {
aggBucket.contributorsOriginal.AddValueCounterHost(0, 1, host) // protected by a.mu
}
aggBucket.contributors = append(aggBucket.contributors, hctx) // protected by a.mu
aggBucket.rawSize += rawSize // protected by a.mu
aggHost := a.aggregatorHost
a.mu.Unlock()
defer aggBucket.sendMu.RUnlock()
lockedShard := -1
sampleFactors := map[int32]float32{}
var newKeys []data_model.Key
var usedMetrics []int32
for _, v := range bucket.SampleFactors {
sampleFactors[v.Metric] = v.Value
}
conveyor := int32(format.TagValueIDConveyorRecent)
if args.IsSetHistoric() {
conveyor = format.TagValueIDConveyorHistoric
}
spare := int32(format.TagValueIDAggregatorOriginal)
if args.IsSetSpare() {
spare = format.TagValueIDAggregatorSpare
}
route := int32(format.TagValueIDRouteDirect)
if args.IsSetIngressProxy() {
route = int32(format.TagValueIDRouteIngressProxy)
}
for _, item := range bucket.Metrics {
k, sID := data_model.KeyFromStatshouseItem(item, args.Time)
if k.Metric < 0 {
k = k.WithAgentEnvRouteArch(agentEnv, route, buildArch)
}
s := aggBucket.lockShard(&lockedShard, sID)
created := false
mi := data_model.MapKeyItemMultiItem(&s.multiItems, k, data_model.AggregatorStringTopCapacity, &created)
mi.MergeWithTLItem(&item, host)
if created {
if !args.IsSetSpare() { // Data from spares should not affect cardinality estimations
newKeys = append(newKeys, k)
}
usedMetrics = append(usedMetrics, k.Metric)
}
}
aggBucket.lockShard(&lockedShard, -1)
aggBucket.mu.Lock()
if aggBucket.usedMetrics == nil {
aggBucket.usedMetrics = map[int32]struct{}{}
}
for _, m := range usedMetrics {
aggBucket.usedMetrics[m] = struct{}{}
}
aggBucket.mu.Unlock()
// newKeys will not be large, if average cardinality is low
// we update estimators under sendMu.RLock so that sample factors used for inserting will be already updated
a.estimator.UpdateWithKeys(args.Time, newKeys)
now2 := time.Now()
// Write meta metrics. They all simply go to shard 0 independent of their keys.
s := aggBucket.lockShard(&lockedShard, 0)
getMultiItem := func(t uint32, m int32, keys [16]int32) *data_model.MultiItem {
key := data_model.AggKey(t, m, keys, aggHost, a.shardKey, a.replicaKey).WithAgentEnvRouteArch(agentEnv, route, buildArch)
return data_model.MapKeyItemMultiItem(&s.multiItems, key, data_model.AggregatorStringTopCapacity, nil)
}
getMultiItem(args.Time, format.BuiltinMetricIDAggSizeCompressed, [16]int32{0, 0, 0, 0, conveyor, spare}).Tail.AddValueCounterHost(float64(rawSize), 1, host)
getMultiItem(args.Time, format.BuiltinMetricIDAggSizeUncompressed, [16]int32{0, 0, 0, 0, conveyor, spare}).Tail.AddValueCounterHost(float64(args.OriginalSize), 1, host)
getMultiItem(args.Time, format.BuiltinMetricIDAggBucketReceiveDelaySec, [16]int32{0, 0, 0, 0, conveyor, spare, format.TagValueIDSecondReal}).Tail.AddValueCounterHost(receiveDelay, 1, host)
for i := uint32(0); i < bucket.MissedSeconds && i < data_model.MaxMissedSecondsIntoContributors; i++ {
// as all statistics of missed second is sent in the next second, we think this situation should not lead to holes in # of agents who sent the second
d := receiveDelay - float64(i+1)
getMultiItem(args.Time+i, format.BuiltinMetricIDAggBucketReceiveDelaySec, [16]int32{0, 0, 0, 0, conveyor, spare, format.TagValueIDSecondPhantom}).Tail.AddValueCounterHost(d, 1, host)
}
getMultiItem(args.Time, format.BuiltinMetricIDAggBucketAggregateTimeSec, [16]int32{0, 0, 0, 0, conveyor, spare}).Tail.AddValueCounterHost(now2.Sub(now).Seconds(), 1, host)
getMultiItem(args.Time, format.BuiltinMetricIDAggAdditionsToEstimator, [16]int32{0, 0, 0, 0, conveyor, spare}).Tail.AddValueCounterHost(float64(len(newKeys)), 1, host)
if bucket.MissedSeconds != 0 {
getMultiItem(args.Time, format.BuiltinMetricIDTimingErrors, [16]int32{0, format.TagValueIDTimingMissedSeconds}).Tail.AddValueCounterHost(float64(bucket.MissedSeconds), 1, host)
}
if args.QueueSizeMemory != 0 {
getMultiItem(args.Time, format.BuiltinMetricIDAgentHistoricQueueSize, [16]int32{0, format.TagValueIDHistoricQueueMemory}).Tail.AddValueCounterHost(float64(args.QueueSizeMemory), 1, host)
}
if args.QueueSizeDisk != 0 {
getMultiItem(args.Time, format.BuiltinMetricIDAgentHistoricQueueSize, [16]int32{0, format.TagValueIDHistoricQueueDisk}).Tail.AddValueCounterHost(float64(args.QueueSizeDisk), 1, host)
}
var bcStr []byte
if format.ValidStringValue(mem.B(args.BuildCommit)) {
bcStr = args.BuildCommit
}
getMultiItem((args.Time/60)*60, format.BuiltinMetricIDVersions, [16]int32{0, 0, format.TagValueIDComponentAgent, args.BuildCommitDate, args.BuildCommitTs}).MapStringTopBytes(bcStr, 1).AddCounterHost(1, host)
for _, v := range bucket.SampleFactors {
// We probably wish to stop splitting by aggregator, because this metric is taking already too much space - about 2% of all data
getMultiItem(args.Time, format.BuiltinMetricIDAgentSamplingFactor, [16]int32{0, v.Metric}).Tail.AddValueCounterHost(float64(v.Value), 1, host)
}
for _, v := range bucket.IngestionStatusOk {
// We do not split by aggregator, because this metric is taking already too much space - about 1% of all data
if v.Count > 0 {
data_model.MapKeyItemMultiItem(&s.multiItems, (data_model.Key{Timestamp: args.Time, Metric: format.BuiltinMetricIDIngestionStatus, Keys: [16]int32{0, v.Metric, format.TagValueIDSrcIngestionStatusOKCached}}).WithAgentEnvRouteArch(agentEnv, route, buildArch), data_model.AggregatorStringTopCapacity, nil).Tail.AddCounterHost(float64(v.Count), host)
} else {
data_model.MapKeyItemMultiItem(&s.multiItems, (data_model.Key{Timestamp: args.Time, Metric: format.BuiltinMetricIDIngestionStatus, Keys: [16]int32{0, v.Metric, format.TagValueIDSrcIngestionStatusOKUncached}}).WithAgentEnvRouteArch(agentEnv, route, buildArch), data_model.AggregatorStringTopCapacity, nil).Tail.AddCounterHost(float64(-v.Count), host)
}
}
aggBucket.lockShard(&lockedShard, -1)
return hctx.HijackResponse()
}
func (a *Aggregator) handleClientBucket2(_ context.Context, hctx *rpc.HandlerContext, args tlstatshouse.SendSourceBucket2Bytes, setShardReplica bool, bucket tlstatshouse.SourceBucket2Bytes, rawSize int) (err error) {
if !args.IsSetHistoric() && a.config.SimulateRandomErrors > 0 && rand.Float64() < a.config.SimulateRandomErrors { // SimulateRandomErrors > 0 is optimization
a.mu.Lock()
defer a.mu.Unlock()
aggBucket := a.recentBuckets[len(a.recentBuckets)-1]
aggBucket.contributorsSimulatedErrors = append(aggBucket.contributorsSimulatedErrors, hctx)
return hctx.HijackResponse()
}
now := time.Now()
receiveDelay := now.Sub(time.Unix(int64(args.Time), 0)).Seconds()
host := a.tagsMapper.mapHost(now, args.Header.HostName, format.BuiltinMetricNameBudgetHost, false)
agentEnv := a.getAgentEnv(args.Header.IsSetAgentEnvStaging(args.FieldsMask))
buildArch := format.FilterBuildArch(args.Header.BuildArch)
addrIPV4, _ := addrIPString(hctx.RemoteAddr())
if args.Header.AgentIp[3] != 0 {
addrIPV4 = uint32(args.Header.AgentIp[3])
}
// opportunistic mapping. We do not map addrStr. To find hosts with hostname not set use internal_log
var aggBucket *aggregatorBucket
a.mu.Lock()
if setShardReplica { // Skip old versions not yet updated
if err := a.checkShardConfiguration(args.Header.ShardReplica, args.Header.ShardReplicaTotal); err != nil {
key := data_model.AggKey(0, format.BuiltinMetricIDAutoConfig, [16]int32{0, 0, 0, 0, format.TagValueIDAutoConfigErrorSend, args.Header.ShardReplica, args.Header.ShardReplicaTotal}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.mu.Unlock()
a.sh2.AddCounterHost(key, 1, host, nil)
return err // TODO - return code so clients will print into log and discard data
}
}
OldestTime := a.recentBuckets[0].time
NewestTime := a.recentBuckets[len(a.recentBuckets)-1].time
if args.IsSetHistoric() {
if args.Time > NewestTime {
key := data_model.AggKey(0, format.BuiltinMetricIDTimingErrors, [16]int32{0, format.TagValueIDTimingFutureBucketHistoric}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.mu.Unlock()
a.sh2.AddValueCounterHost(key, float64(args.Time-NewestTime), 1, host)
// We discard, because otherwise clients will flood aggregators with this data
hctx.Response, err = args.WriteResult(hctx.Response, []byte("historic bucket time is too far in the future"))
return err
}
if OldestTime >= data_model.MaxHistoricWindow+data_model.MaxHistoricWindowLag && args.Time < OldestTime-data_model.MaxHistoricWindow-data_model.MaxHistoricWindowLag {
key := data_model.AggKey(0, format.BuiltinMetricIDTimingErrors, [16]int32{0, format.TagValueIDTimingLongWindowThrownAggregator}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.mu.Unlock()
a.sh2.AddValueCounterHost(key, float64(NewestTime-args.Time), 1, host)
hctx.Response, err = args.WriteResult(hctx.Response, []byte("Successfully discarded historic bucket beyond historic window"))
return err
}
if args.Time < OldestTime {
aggBucket = a.historicBuckets[args.Time]
if aggBucket == nil {
aggBucket = &aggregatorBucket{time: args.Time}
a.historicBuckets[args.Time] = aggBucket
if a.sendHistoricCondition() {
a.cond.Broadcast() // we are not sure that Signal is enough
}
}
} else {
// If source receives error from recent conveyor quickly, it will come to spare while bucket is still recent
// This is useful optimization, because can save half inserts
aggBucket = a.recentBuckets[args.Time-OldestTime]
}
} else {
if args.Time > NewestTime { // AgentShard too far in a future
key := data_model.AggKey(0, format.BuiltinMetricIDTimingErrors, [16]int32{0, format.TagValueIDTimingFutureBucketRecent}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.mu.Unlock()
a.sh2.AddValueCounterHost(key, float64(args.Time-NewestTime), 1, host)
// We discard, because otherwise clients will flood aggregators with this data
hctx.Response, err = args.WriteResult(hctx.Response, []byte("bucket time is too far in the future"))
return err
}
if args.Time < OldestTime {
key := data_model.AggKey(0, format.BuiltinMetricIDTimingErrors, [16]int32{0, format.TagValueIDTimingLateRecent}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.mu.Unlock()
a.sh2.AddValueCounterHost(key, float64(NewestTime-args.Time), 1, host)
return rpc.Error{
Code: data_model.RPCErrorMissedRecentConveyor,
Description: "bucket time is too far in the past for recent conveyor",
}
}
aggBucket = a.recentBuckets[args.Time-OldestTime]
}
aggBucket.sendMu.RLock()
// This lock order ensures, that if sender gets a.mu.Lock(), then all aggregating clients already have aggBucket.sendMu.RLock()
if args.IsSetSpare() {
aggBucket.contributorsSpare.AddValueCounterHost(0, 1, host) // protected by a.mu
} else {
aggBucket.contributorsOriginal.AddValueCounterHost(0, 1, host) // protected by a.mu
}
aggBucket.contributors = append(aggBucket.contributors, hctx) // protected by a.mu
aggBucket.rawSize += rawSize // protected by a.mu
aggHost := a.aggregatorHost
a.mu.Unlock()
defer aggBucket.sendMu.RUnlock()
lockedShard := -1
sampleFactors := map[int32]float32{}
var newKeys []data_model.Key
var usedMetrics []int32
for _, v := range bucket.SampleFactors {
sampleFactors[v.Metric] = v.Value
}
conveyor := int32(format.TagValueIDConveyorRecent)
if args.IsSetHistoric() {
conveyor = format.TagValueIDConveyorHistoric
}
spare := int32(format.TagValueIDAggregatorOriginal)
if args.IsSetSpare() {
spare = format.TagValueIDAggregatorSpare
}
route := int32(format.TagValueIDRouteDirect)
if args.Header.IsSetIngressProxy(args.FieldsMask) {
route = int32(format.TagValueIDRouteIngressProxy)
}
for _, item := range bucket.Metrics {
k, sID := data_model.KeyFromStatshouseMultiItem(&item, args.Time)
if k.Metric < 0 {
k = k.WithAgentEnvRouteArch(agentEnv, route, buildArch)
if k.Metric == format.BuiltinMetricIDAgentHeartbeatVersion {
// Remap legacy metric to a new one
k.Metric = format.BuiltinMetricIDHeartbeatVersion
k.Keys[2] = k.Keys[1]
k.Keys[1] = format.TagValueIDComponentAgent
}
if k.Metric == format.BuiltinMetricIDAgentHeartbeatArgs {
// Remap legacy metric to a new one
k.Metric = format.BuiltinMetricIDHeartbeatArgs
k.Keys[2] = k.Keys[1]
k.Keys[1] = format.TagValueIDComponentAgent
}
if k.Metric == format.BuiltinMetricIDHeartbeatVersion ||
k.Metric == format.BuiltinMetricIDHeartbeatArgs || k.Metric == format.BuiltinMetricIDHeartbeatArgs2 ||
k.Metric == format.BuiltinMetricIDHeartbeatArgs3 || k.Metric == format.BuiltinMetricIDHeartbeatArgs4 {
// We need to set IP anyway, so set other keys here, not by source
k.Keys[5] = args.BuildCommitDate
k.Keys[6] = args.BuildCommitTs
k.Keys[7] = host
k.Keys[8] = int32(addrIPV4)
}
if k.Metric == format.BuiltinMetricIDRPCRequests {
k.Keys[7] = host // agent cannot easily map its own host for now
}
}
s := aggBucket.lockShard(&lockedShard, sID)
created := false
mi := data_model.MapKeyItemMultiItem(&s.multiItems, k, data_model.AggregatorStringTopCapacity, &created)
mi.MergeWithTLMultiItem(&item, host)
if created {
if !args.IsSetSpare() { // Data from spares should not affect cardinality estimations
newKeys = append(newKeys, k)
}
usedMetrics = append(usedMetrics, k.Metric)
}
}
aggBucket.lockShard(&lockedShard, -1)
aggBucket.mu.Lock()
if aggBucket.usedMetrics == nil {
aggBucket.usedMetrics = map[int32]struct{}{}
}
for _, m := range usedMetrics {
aggBucket.usedMetrics[m] = struct{}{}
}
aggBucket.mu.Unlock()
// newKeys will not be large, if average cardinality is low
// we update estimators under sendMu.RLock so that sample factors used for inserting will be already updated
a.estimator.UpdateWithKeys(args.Time, newKeys)
now2 := time.Now()
// Write meta metrics. They all simply go to shard 0 independent of their keys.
s := aggBucket.lockShard(&lockedShard, 0)
getMultiItem := func(t uint32, m int32, keys [16]int32) *data_model.MultiItem {
key := data_model.AggKey(t, m, keys, aggHost, a.shardKey, a.replicaKey).WithAgentEnvRouteArch(agentEnv, route, buildArch)
return data_model.MapKeyItemMultiItem(&s.multiItems, key, data_model.AggregatorStringTopCapacity, nil)
}
getMultiItem(args.Time, format.BuiltinMetricIDAggSizeCompressed, [16]int32{0, 0, 0, 0, conveyor, spare}).Tail.AddValueCounterHost(float64(rawSize), 1, host)
getMultiItem(args.Time, format.BuiltinMetricIDAggSizeUncompressed, [16]int32{0, 0, 0, 0, conveyor, spare}).Tail.AddValueCounterHost(float64(args.OriginalSize), 1, host)
getMultiItem(args.Time, format.BuiltinMetricIDAggBucketReceiveDelaySec, [16]int32{0, 0, 0, 0, conveyor, spare, format.TagValueIDSecondReal}).Tail.AddValueCounterHost(receiveDelay, 1, host)
for i := uint32(0); i < bucket.MissedSeconds && i < data_model.MaxMissedSecondsIntoContributors; i++ {
d := receiveDelay - float64(i+1)
getMultiItem(args.Time+i, format.BuiltinMetricIDAggBucketReceiveDelaySec, [16]int32{0, 0, 0, 0, conveyor, spare, format.TagValueIDSecondPhantom}).Tail.AddValueCounterHost(d, 1, host)
}
getMultiItem(args.Time, format.BuiltinMetricIDAggBucketAggregateTimeSec, [16]int32{0, 0, 0, 0, conveyor, spare}).Tail.AddValueCounterHost(now2.Sub(now).Seconds(), 1, host)
getMultiItem(args.Time, format.BuiltinMetricIDAggAdditionsToEstimator, [16]int32{0, 0, 0, 0, conveyor, spare}).Tail.AddValueCounterHost(float64(len(newKeys)), 1, host)
if bucket.MissedSeconds != 0 {
getMultiItem(args.Time, format.BuiltinMetricIDTimingErrors, [16]int32{0, format.TagValueIDTimingMissedSeconds}).Tail.AddValueCounterHost(float64(bucket.MissedSeconds), 1, host)
}
if args.QueueSizeMemory != 0 {
getMultiItem(args.Time, format.BuiltinMetricIDAgentHistoricQueueSize, [16]int32{0, format.TagValueIDHistoricQueueMemory}).Tail.AddValueCounterHost(float64(args.QueueSizeMemory), 1, host)
}
if args.QueueSizeDisk != 0 {
getMultiItem(args.Time, format.BuiltinMetricIDAgentHistoricQueueSize, [16]int32{0, format.TagValueIDHistoricQueueDisk}).Tail.AddValueCounterHost(float64(args.QueueSizeDisk), 1, host)
}
var bcStr []byte
componentTag := args.Header.ComponentTag
if componentTag != format.TagValueIDComponentAgent && componentTag != format.TagValueIDComponentAggregator &&
componentTag != format.TagValueIDComponentIngressProxy && componentTag != format.TagValueIDComponentAPI {
// TODO - remove this if after release, because no more agents will send crap here
componentTag = format.TagValueIDComponentAgent
}
if format.ValidStringValue(mem.B(args.BuildCommit)) {
bcStr = args.BuildCommit
}
// This cheap version metric is not affected by agent sampling algorithm in contrast with __heartbeat_version
getMultiItem((args.Time/60)*60, format.BuiltinMetricIDVersions, [16]int32{0, 0, componentTag, args.BuildCommitDate, args.BuildCommitTs}).MapStringTopBytes(bcStr, 1).AddCounterHost(1, host)
for _, v := range bucket.SampleFactors {
// We probably wish to stop splitting by aggregator, because this metric is taking already too much space - about 2% of all data
// Counter will be +1 for each agent who sent bucket for this second, so millions.
getMultiItem(args.Time, format.BuiltinMetricIDAgentSamplingFactor, [16]int32{0, v.Metric}).Tail.AddValueCounterHost(float64(v.Value), 1, host)
}
ingestionStatus := func(env int32, metricID int32, status int32, value float32) {
data_model.MapKeyItemMultiItem(&s.multiItems, (data_model.Key{Timestamp: args.Time, Metric: format.BuiltinMetricIDIngestionStatus, Keys: [16]int32{env, metricID, status}}).WithAgentEnvRouteArch(agentEnv, route, buildArch), data_model.AggregatorStringTopCapacity, nil).Tail.AddCounterHost(float64(value), host)
}
for _, v := range bucket.IngestionStatusOk {
// We do not split by aggregator, because this metric is taking already too much space - about 1% of all data
if v.Value > 0 {
ingestionStatus(0, v.Metric, format.TagValueIDSrcIngestionStatusOKCached, v.Value)
} else {
ingestionStatus(0, v.Metric, format.TagValueIDSrcIngestionStatusOKUncached, -v.Value)
}
}
for _, v := range bucket.IngestionStatusOk2 {
// We do not split by aggregator, because this metric is taking already too much space - about 1% of all data
if v.Value > 0 {
ingestionStatus(v.Env, v.Metric, format.TagValueIDSrcIngestionStatusOKCached, v.Value)
} else {
// clients before release used count < 0 for uncached status and count > 0 for cached
// but there is too little uncached statuses for such optimization, and we wanted
// to pass tag that caused uncached loading, so we removed this negative value tweak from agents
ingestionStatus(v.Env, v.Metric, format.TagValueIDSrcIngestionStatusOKUncached, -v.Value)
}
}
aggBucket.lockShard(&lockedShard, -1)
return hctx.HijackResponse()
}
func (a *Aggregator) handleKeepAlive(_ context.Context, hctx *rpc.HandlerContext, args tlstatshouse.SendKeepAliveBytes) error {
now := time.Now()
host := a.tagsMapper.mapHost(now, args.HostName, format.BuiltinMetricNameBudgetHost, false)
agentEnv := int32(format.TagValueIDProduction)
buildArch := format.FilterBuildArch(args.BuildArch)
a.mu.Lock()
if args.IsSetShardReplica() {
if err := a.checkShardConfiguration(args.ShardReplica, args.ShardReplicaTotal); err != nil {
key := data_model.AggKey(0, format.BuiltinMetricIDAutoConfig, [16]int32{0, 0, 0, 0, format.TagValueIDAutoConfigErrorKeepAlive, args.ShardReplica, args.ShardReplicaTotal}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.mu.Unlock()
a.sh2.AddCounterHost(key, 1, host, nil)
return err
}
}
aggBucket := a.recentBuckets[0] // Most ready for insert
aggBucket.sendMu.RLock()
// This lock order ensures, that if sender gets a.mu.Lock(), then all aggregating clients already have aggBucket.sendMu.RLock()
aggBucket.contributors = append(aggBucket.contributors, hctx) // protected by a.mu
aggHost := a.aggregatorHost
a.mu.Unlock()
defer aggBucket.sendMu.RUnlock()
// Write meta statistics
route := int32(format.TagValueIDRouteDirect)
if args.IsSetIngressProxy() {
route = int32(format.TagValueIDRouteIngressProxy)
}
lockedShard := -1
s := aggBucket.lockShard(&lockedShard, 0)
// Counters can contain this metrics while # of contributors is 0. We compensate by adding small fixed budget.
data_model.MapKeyItemMultiItem(&s.multiItems, data_model.AggKey(aggBucket.time, format.BuiltinMetricIDAggKeepAlive, [16]int32{}, aggHost, a.shardKey, a.replicaKey).WithAgentEnvRouteArch(agentEnv, route, buildArch), data_model.AggregatorStringTopCapacity, nil).Tail.AddCounterHost(1, host)
aggBucket.lockShard(&lockedShard, -1)
return hctx.HijackResponse()
}
func (a *Aggregator) handleKeepAlive2(_ context.Context, hctx *rpc.HandlerContext, args tlstatshouse.SendKeepAlive2Bytes) error {
now := time.Now()
host := a.tagsMapper.mapHost(now, args.Header.HostName, format.BuiltinMetricNameBudgetHost, false)
agentEnv := a.getAgentEnv(args.Header.IsSetAgentEnvStaging(args.FieldsMask))
buildArch := format.FilterBuildArch(args.Header.BuildArch)
a.mu.Lock()
if err := a.checkShardConfiguration(args.Header.ShardReplica, args.Header.ShardReplicaTotal); err != nil {
key := data_model.AggKey(0, format.BuiltinMetricIDAutoConfig, [16]int32{0, 0, 0, 0, format.TagValueIDAutoConfigErrorKeepAlive, args.Header.ShardReplica, args.Header.ShardReplicaTotal}, a.aggregatorHost, a.shardKey, a.replicaKey)
a.mu.Unlock()
a.sh2.AddCounterHost(key, 1, host, nil)
return err
}
aggBucket := a.recentBuckets[0] // Most ready for insert
aggBucket.sendMu.RLock()
// This lock order ensures, that if sender gets a.mu.Lock(), then all aggregating clients already have aggBucket.sendMu.RLock()
aggBucket.contributors = append(aggBucket.contributors, hctx) // protected by a.mu
aggHost := a.aggregatorHost
a.mu.Unlock()
defer aggBucket.sendMu.RUnlock()
// Write meta statistics
route := int32(format.TagValueIDRouteDirect)
if args.Header.IsSetIngressProxy(args.FieldsMask) {
route = int32(format.TagValueIDRouteIngressProxy)
}
lockedShard := -1
s := aggBucket.lockShard(&lockedShard, 0)
// Counters can contain this metrics while # of contributors is 0. We compensate by adding small fixed budget.
data_model.MapKeyItemMultiItem(&s.multiItems, data_model.AggKey(aggBucket.time, format.BuiltinMetricIDAggKeepAlive, [16]int32{}, aggHost, a.shardKey, a.replicaKey).WithAgentEnvRouteArch(agentEnv, route, buildArch), data_model.AggregatorStringTopCapacity, nil).Tail.AddCounterHost(1, host)
aggBucket.lockShard(&lockedShard, -1)
return hctx.HijackResponse()
}