-
Notifications
You must be signed in to change notification settings - Fork 85
/
batch.go
211 lines (177 loc) · 5.76 KB
/
batch.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
package client
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
"github.com/prometheus/common/model"
"golang.org/x/exp/slices"
"github.com/grafana/alloy/internal/component/common/loki"
"github.com/grafana/loki/pkg/logproto"
)
const (
errMaxStreamsLimitExceeded = "streams limit exceeded, streams: %d exceeds limit: %d, stream: '%s'"
)
// SentDataMarkerHandler is a slice of the MarkerHandler interface, that the batch interacts with to report the event that
// all data in the batch has been delivered or a client failed to do so.
type SentDataMarkerHandler interface {
UpdateSentData(segmentId, dataCount int)
}
// batch holds pending log streams waiting to be sent to Loki, and it's used
// to reduce the number of push requests to Loki aggregating multiple log streams
// and entries in a single batch request. In case of multi-tenant Promtail, log
// streams for each tenant are stored in a dedicated batch.
type batch struct {
streams map[string]*logproto.Stream
// totalBytes holds the total amounts of bytes, across the log lines in this batch.
totalBytes int
createdAt time.Time
maxStreams int
// segmentCounter tracks the amount of entries for each segment present in this batch.
segmentCounter map[int]int
}
func newBatch(maxStreams int, entries ...loki.Entry) *batch {
b := &batch{
streams: map[string]*logproto.Stream{},
totalBytes: 0,
createdAt: time.Now(),
maxStreams: maxStreams,
segmentCounter: map[int]int{},
}
// Add entries to the batch
for _, entry := range entries {
//never error here
_ = b.add(entry)
}
return b
}
// add an entry to the batch
func (b *batch) add(entry loki.Entry) error {
b.totalBytes += entrySize(entry.Entry)
// Append the entry to an already existing stream (if any)
labels := labelsMapToString(entry.Labels, ReservedLabelTenantID)
if stream, ok := b.streams[labels]; ok {
stream.Entries = append(stream.Entries, entry.Entry)
return nil
}
streams := len(b.streams)
if b.maxStreams > 0 && streams >= b.maxStreams {
return fmt.Errorf(errMaxStreamsLimitExceeded, streams, b.maxStreams, labels)
}
// Add the entry as a new stream
b.streams[labels] = &logproto.Stream{
Labels: labels,
Entries: []logproto.Entry{entry.Entry},
}
return nil
}
// addFromWAL adds an entry to the batch, tracking that the data being added comes from segment segmentNum read from the
// WAL.
func (b *batch) addFromWAL(lbs model.LabelSet, entry logproto.Entry, segmentNum int) error {
b.totalBytes += len(entry.Line)
// Append the entry to an already existing stream (if any)
labels := labelsMapToString(lbs, ReservedLabelTenantID)
if stream, ok := b.streams[labels]; ok {
stream.Entries = append(stream.Entries, entry)
b.countForSegment(segmentNum)
return nil
}
streams := len(b.streams)
if b.maxStreams > 0 && streams >= b.maxStreams {
return fmt.Errorf(errMaxStreamsLimitExceeded, streams, b.maxStreams, labels)
}
// Add the entry as a new stream
b.streams[labels] = &logproto.Stream{
Labels: labels,
Entries: []logproto.Entry{entry},
}
b.countForSegment(segmentNum)
return nil
}
// labelsMapToString encodes an entry's label set as a string, ignoring the without label.
func labelsMapToString(ls model.LabelSet, without model.LabelName) string {
var b strings.Builder
totalSize := 2
lstrs := make([]model.LabelName, 0, len(ls))
for l, v := range ls {
if l == without {
continue
}
lstrs = append(lstrs, l)
// guess size increase: 2 for `, ` between labels and 3 for the `=` and quotes around label value
totalSize += len(l) + 2 + len(v) + 3
}
b.Grow(totalSize)
b.WriteByte('{')
slices.Sort(lstrs)
for i, l := range lstrs {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(string(l))
b.WriteString(`=`)
b.WriteString(strconv.Quote(string(ls[l])))
}
b.WriteByte('}')
return b.String()
}
// sizeBytes returns the current batch size in bytes
func (b *batch) sizeBytes() int {
return b.totalBytes
}
// sizeBytesAfter returns the size of the batch after the input entry
// will be added to the batch itself
func (b *batch) sizeBytesAfter(entry logproto.Entry) int {
return b.totalBytes + entrySize(entry)
}
// age of the batch since its creation
func (b *batch) age() time.Duration {
return time.Since(b.createdAt)
}
// encode the batch as snappy-compressed push request, and returns
// the encoded bytes and the number of encoded entries
func (b *batch) encode() ([]byte, int, error) {
req, entriesCount := b.createPushRequest()
buf, err := proto.Marshal(req)
if err != nil {
return nil, 0, err
}
buf = snappy.Encode(nil, buf)
return buf, entriesCount, nil
}
// creates push request and returns it, together with number of entries
func (b *batch) createPushRequest() (*logproto.PushRequest, int) {
req := logproto.PushRequest{
Streams: make([]logproto.Stream, 0, len(b.streams)),
}
entriesCount := 0
for _, stream := range b.streams {
req.Streams = append(req.Streams, *stream)
entriesCount += len(stream.Entries)
}
return &req, entriesCount
}
// countForSegment tracks that one data item has been read from a certain WAL segment.
func (b *batch) countForSegment(segmentNum int) {
if curr, ok := b.segmentCounter[segmentNum]; ok {
b.segmentCounter[segmentNum] = curr + 1
return
}
b.segmentCounter[segmentNum] = 1
}
// reportAsSentData will report for all segments whose data is part of this batch, the amount of that data as sent to
// the provided SentDataMarkerHandler
func (b *batch) reportAsSentData(h SentDataMarkerHandler) {
for seg, data := range b.segmentCounter {
h.UpdateSentData(seg, data)
}
}
func entrySize(entry logproto.Entry) int {
structuredMetadataSize := 0
for _, label := range entry.StructuredMetadata {
structuredMetadataSize += label.Size()
}
return len(entry.Line) + structuredMetadataSize
}