forked from parca-dev/parca-agent
/
write_client.go
153 lines (129 loc) · 4.21 KB
/
write_client.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
// Copyright (c) 2022 The Parca Authors
// 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.
//
package agent
import (
"context"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
profilestorepb "github.com/parca-dev/parca/gen/proto/go/parca/profilestore/v1alpha1"
"google.golang.org/grpc"
)
type Batcher struct {
logger log.Logger
writeClient profilestorepb.ProfileStoreServiceClient
writeInterval time.Duration
mtx *sync.RWMutex
series []*profilestorepb.RawProfileSeries
lastBatchSentAt time.Time
lastBatchSendError error
}
func NewBatchWriteClient(logger log.Logger, wc profilestorepb.ProfileStoreServiceClient, writeInterval time.Duration) *Batcher {
return &Batcher{
logger: logger,
writeClient: wc,
writeInterval: writeInterval,
series: []*profilestorepb.RawProfileSeries{},
mtx: &sync.RWMutex{},
}
}
func (b *Batcher) loopReport(lastBatchSentAt time.Time, lastBatchSendError error) {
b.mtx.Lock()
defer b.mtx.Unlock()
b.lastBatchSentAt = lastBatchSentAt
b.lastBatchSendError = lastBatchSendError
}
func (b *Batcher) Run(ctx context.Context) error {
ticker := time.NewTicker(b.writeInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
b.loopReport(time.Now(), b.batchLoop(ctx))
}
}
func (b *Batcher) batchLoop(ctx context.Context) error {
b.mtx.Lock()
batch := b.series
b.series = []*profilestorepb.RawProfileSeries{}
b.mtx.Unlock()
expbackOff := backoff.NewExponentialBackOff()
expbackOff.MaxElapsedTime = b.writeInterval // TODO: Subtract ~10% of interval to account for overhead in loop
expbackOff.InitialInterval = 500 * time.Millisecond // Let's not retry to aggressively to start with.
err := backoff.Retry(func() error {
_, err := b.writeClient.WriteRaw(ctx, &profilestorepb.WriteRawRequest{
Series: batch,
Normalized: true, // Since everything generated by the agent is normalized, we can assume this to be true.
})
// Only log error if retrying, otherwise it will be logged outside the retry
if err != nil && expbackOff.NextBackOff().Nanoseconds() > 0 {
level.Debug(b.logger).Log(
"msg", "batch write client failed to send profiles",
"retry", expbackOff.NextBackOff(),
"count", len(batch),
"err", err,
)
}
return err
}, expbackOff)
if err != nil {
// TODO: Add metric and increase with every backoff iteration.
level.Warn(b.logger).Log("msg", "batch write client failed to send profiles", "count", len(batch), "err", err)
return err
}
if len(batch) > 0 {
level.Debug(b.logger).Log("msg", "batch write client sent profiles", "count", len(batch))
}
return nil
}
func isEqualLabel(a, b *profilestorepb.LabelSet) bool {
if len(a.Labels) != len(b.Labels) {
return false
}
ret := true
for i := range a.Labels {
if (a.Labels[i].Name != b.Labels[i].Name) || (a.Labels[i].Value != b.Labels[i].Value) {
ret = false
}
}
return ret
}
func findIndex(arr []*profilestorepb.RawProfileSeries, p *profilestorepb.RawProfileSeries) (int, bool) {
for i, val := range arr {
if isEqualLabel(val.Labels, p.Labels) {
return i, true
}
}
return -1, false
}
func (b *Batcher) WriteRaw(ctx context.Context, r *profilestorepb.WriteRawRequest, opts ...grpc.CallOption) (*profilestorepb.WriteRawResponse, error) {
b.mtx.Lock()
defer b.mtx.Unlock()
for _, profileSeries := range r.Series {
if j, ok := findIndex(b.series, profileSeries); ok {
b.series[j].Samples = append(b.series[j].Samples, profileSeries.Samples...)
continue
}
b.series = append(b.series, &profilestorepb.RawProfileSeries{
Labels: profileSeries.Labels,
Samples: profileSeries.Samples,
})
}
return &profilestorepb.WriteRawResponse{}, nil
}