-
Notifications
You must be signed in to change notification settings - Fork 693
Expand file tree
/
Copy pathappend_block.go
More file actions
255 lines (209 loc) · 6.39 KB
/
append_block.go
File metadata and controls
255 lines (209 loc) · 6.39 KB
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
package wal
import (
"context"
"fmt"
"math"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/grafana/tempo/pkg/model"
"github.com/grafana/tempo/tempodb/backend"
"github.com/grafana/tempo/tempodb/encoding/common"
v2 "github.com/grafana/tempo/tempodb/encoding/v2"
)
const maxDataEncodingLength = 32
// AppendBlock is a block that is actively used to append new objects to. It stores all data in the appendFile
// in the order it was received and an in memory sorted index.
type AppendBlock struct {
meta *backend.BlockMeta
ingestionSlack time.Duration
appendFile *os.File
appender v2.Appender
filepath string
readFile *os.File
once sync.Once
}
func newAppendBlock(id uuid.UUID, tenantID string, filepath string, e backend.Encoding, dataEncoding string, ingestionSlack time.Duration) (*AppendBlock, error) {
if strings.ContainsRune(dataEncoding, ':') ||
len([]rune(dataEncoding)) > maxDataEncodingLength {
return nil, fmt.Errorf("dataEncoding %s is invalid", dataEncoding)
}
h := &AppendBlock{
meta: backend.NewBlockMeta(tenantID, id, v2.VersionString, e, dataEncoding),
filepath: filepath,
ingestionSlack: ingestionSlack,
}
name := h.fullFilename()
f, err := os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return nil, err
}
h.appendFile = f
dataWriter, err := v2.NewDataWriter(f, e)
if err != nil {
return nil, err
}
h.appender = v2.NewAppender(dataWriter)
return h, nil
}
// newAppendBlockFromFile returns an AppendBlock that can not be appended to, but can
// be completed. It can return a warning or a fatal error
func newAppendBlockFromFile(filename string, path string, ingestionSlack time.Duration, additionalStartSlack time.Duration, fn RangeFunc) (*AppendBlock, error, error) {
var warning error
blockID, tenantID, version, e, dataEncoding, err := ParseFilename(filename)
if err != nil {
return nil, nil, fmt.Errorf("parsing wal filename: %w", err)
}
b := &AppendBlock{
meta: backend.NewBlockMeta(tenantID, blockID, version, e, dataEncoding),
filepath: path,
ingestionSlack: ingestionSlack,
}
// replay file to extract records
f, err := b.file()
if err != nil {
return nil, nil, fmt.Errorf("accessing file: %w", err)
}
blockStart := uint32(math.MaxUint32)
blockEnd := uint32(0)
records, warning, err := ReplayWALAndGetRecords(f, e, func(bytes []byte) error {
start, end, err := fn(bytes, dataEncoding)
if err != nil {
return err
}
start, end = b.adjustTimeRangeForSlack(start, end, additionalStartSlack)
if start < blockStart {
blockStart = start
}
if end > blockEnd {
blockEnd = end
}
return nil
})
if err != nil {
return nil, nil, err
}
b.appender = v2.NewRecordAppender(records)
b.meta.TotalObjects = b.appender.Length()
b.meta.StartTime = time.Unix(int64(blockStart), 0)
b.meta.EndTime = time.Unix(int64(blockEnd), 0)
return b, warning, nil
}
// Append adds an id and object to this wal block. start/end should indicate the time range
// associated with the past object. They are unix epoch seconds.
func (a *AppendBlock) Append(id common.ID, b []byte, start, end uint32) error {
err := a.appender.Append(id, b)
if err != nil {
return err
}
start, end = a.adjustTimeRangeForSlack(start, end, 0)
a.meta.ObjectAdded(id, start, end)
return nil
}
func (a *AppendBlock) BlockID() uuid.UUID {
return a.meta.BlockID
}
func (a *AppendBlock) DataLength() uint64 {
return a.appender.DataLength()
}
func (a *AppendBlock) Meta() *backend.BlockMeta {
return a.meta
}
func (a *AppendBlock) Iterator(combiner model.ObjectCombiner) (common.Iterator, error) {
if a.appendFile != nil {
err := a.appendFile.Close()
if err != nil {
return nil, err
}
a.appendFile = nil
}
records := a.appender.Records()
readFile, err := a.file()
if err != nil {
return nil, err
}
dataReader, err := v2.NewDataReader(backend.NewContextReaderWithAllReader(readFile), a.meta.Encoding)
if err != nil {
return nil, err
}
iterator := v2.NewRecordIterator(records, dataReader, v2.NewObjectReaderWriter())
iterator, err = v2.NewDedupingIterator(iterator, combiner, a.meta.DataEncoding)
if err != nil {
return nil, err
}
return iterator, nil
}
func (a *AppendBlock) Find(id common.ID, combiner model.ObjectCombiner) ([]byte, error) {
records := a.appender.RecordsForID(id)
file, err := a.file()
if err != nil {
return nil, err
}
if len(records) == 0 {
return nil, nil
}
dataReader, err := v2.NewDataReader(backend.NewContextReaderWithAllReader(file), a.meta.Encoding)
if err != nil {
return nil, err
}
defer dataReader.Close()
finder := v2.NewPagedFinder(common.Records(records), dataReader, combiner, v2.NewObjectReaderWriter(), a.meta.DataEncoding)
return finder.Find(context.Background(), id)
}
func (a *AppendBlock) Clear() error {
if a.readFile != nil {
_ = a.readFile.Close()
a.readFile = nil
}
if a.appendFile != nil {
_ = a.appendFile.Close()
a.appendFile = nil
}
// ignore error, it's important to remove the file above all else
_ = a.appender.Complete()
name := a.fullFilename()
return os.Remove(name)
}
func (a *AppendBlock) fullFilename() string {
if a.meta.Version == "v0" {
return filepath.Join(a.filepath, fmt.Sprintf("%v:%v", a.meta.BlockID, a.meta.TenantID))
}
var filename string
if a.meta.DataEncoding == "" {
filename = fmt.Sprintf("%v:%v:%v:%v", a.meta.BlockID, a.meta.TenantID, a.meta.Version, a.meta.Encoding)
} else {
filename = fmt.Sprintf("%v:%v:%v:%v:%v", a.meta.BlockID, a.meta.TenantID, a.meta.Version, a.meta.Encoding, a.meta.DataEncoding)
}
return filepath.Join(a.filepath, filename)
}
func (a *AppendBlock) file() (*os.File, error) {
var err error
a.once.Do(func() {
if a.readFile == nil {
name := a.fullFilename()
a.readFile, err = os.OpenFile(name, os.O_RDONLY, 0644)
}
})
return a.readFile, err
}
func (a *AppendBlock) adjustTimeRangeForSlack(start uint32, end uint32, additionalStartSlack time.Duration) (uint32, uint32) {
now := time.Now()
startOfRange := uint32(now.Add(-a.ingestionSlack).Add(-additionalStartSlack).Unix())
endOfRange := uint32(now.Add(a.ingestionSlack).Unix())
warn := false
if start < startOfRange {
warn = true
start = uint32(now.Unix())
}
if end > endOfRange {
warn = true
end = uint32(now.Unix())
}
if warn {
metricWarnings.WithLabelValues(a.meta.TenantID, reasonOutsideIngestionSlack).Inc()
}
return start, end
}