-
Notifications
You must be signed in to change notification settings - Fork 83
/
tstable.go
436 lines (397 loc) · 10.6 KB
/
tstable.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
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you 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 stream
import (
"container/heap"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"path/filepath"
"sort"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/apache/skywalking-banyandb/api/common"
"github.com/apache/skywalking-banyandb/pkg/fs"
"github.com/apache/skywalking-banyandb/pkg/logger"
pbv1 "github.com/apache/skywalking-banyandb/pkg/pb/v1"
"github.com/apache/skywalking-banyandb/pkg/run"
"github.com/apache/skywalking-banyandb/pkg/timestamp"
"github.com/apache/skywalking-banyandb/pkg/watcher"
)
const (
snapshotSuffix = ".snp"
filePermission = 0o600
dirPermission = 0o700
)
type tsTable struct {
index *elementIndex
fileSystem fs.FileSystem
option option
l *logger.Logger
snapshot *snapshot
introductions chan *introduction
loopCloser *run.Closer
p common.Position
root string
gc garbageCleaner
curPartID uint64
sync.RWMutex
}
func (tst *tsTable) loadSnapshot(epoch uint64, loadedParts []uint64) {
parts := tst.mustReadSnapshot(epoch)
snp := snapshot{
epoch: epoch,
}
needToPersist := false
for _, id := range loadedParts {
var find bool
for j := range parts {
if id == parts[j] {
find = true
break
}
}
if !find {
tst.gc.removePart(id)
continue
}
err := validatePartMetadata(tst.fileSystem, partPath(tst.root, id))
if err != nil {
tst.l.Info().Err(err).Uint64("id", id).Msg("cannot validate part metadata. skip and delete it")
tst.gc.removePart(id)
needToPersist = true
continue
}
p := mustOpenFilePart(id, tst.root, tst.fileSystem)
p.partMetadata.ID = id
snp.parts = append(snp.parts, newPartWrapper(nil, p))
if tst.curPartID < id {
tst.curPartID = id
}
}
tst.gc.registerSnapshot(&snp)
tst.gc.clean()
if len(snp.parts) < 1 {
return
}
snp.incRef()
tst.snapshot = &snp
if needToPersist {
tst.persistSnapshot(&snp)
}
}
func (tst *tsTable) startLoop(cur uint64) {
tst.loopCloser = run.NewCloser(1 + 3)
tst.introductions = make(chan *introduction)
flushCh := make(chan *flusherIntroduction)
mergeCh := make(chan *mergerIntroduction)
introducerWatcher := make(watcher.Channel, 1)
flusherWatcher := make(watcher.Channel, 1)
go tst.introducerLoop(flushCh, mergeCh, introducerWatcher, cur+1)
go tst.flusherLoop(flushCh, mergeCh, introducerWatcher, flusherWatcher, cur)
go tst.mergeLoop(mergeCh, flusherWatcher)
}
func parseEpoch(epochStr string) (uint64, error) {
p, err := strconv.ParseUint(epochStr, 16, 64)
if err != nil {
return 0, fmt.Errorf("cannot parse path %s: %w", epochStr, err)
}
return p, nil
}
func (tst *tsTable) mustWriteSnapshot(snapshot uint64, partNames []string) {
data, err := json.Marshal(partNames)
if err != nil {
logger.Panicf("cannot marshal partNames to JSON: %s", err)
}
snapshotPath := filepath.Join(tst.root, snapshotName(snapshot))
lf, err := tst.fileSystem.CreateLockFile(snapshotPath, filePermission)
if err != nil {
logger.Panicf("cannot create lock file %s: %s", snapshotPath, err)
}
n, err := lf.Write(data)
if err != nil {
logger.Panicf("cannot write snapshot %s: %s", snapshotPath, err)
}
if n != len(data) {
logger.Panicf("unexpected number of bytes written to %s; got %d; want %d", snapshotPath, n, len(data))
}
}
func (tst *tsTable) mustReadSnapshot(snapshot uint64) []uint64 {
snapshotPath := filepath.Join(tst.root, snapshotName(snapshot))
data, err := tst.fileSystem.Read(snapshotPath)
if err != nil {
logger.Panicf("cannot read %s: %s", snapshotPath, err)
}
var partNames []string
if err := json.Unmarshal(data, &partNames); err != nil {
logger.Panicf("cannot parse %s: %s", snapshotPath, err)
}
var result []uint64
for i := range partNames {
e, err := parseEpoch(partNames[i])
if err != nil {
logger.Panicf("cannot parse %s: %s", partNames[i], err)
}
result = append(result, e)
}
return result
}
func newTSTable(fileSystem fs.FileSystem, rootPath string, p common.Position,
l *logger.Logger, _ timestamp.TimeRange, option option,
) (*tsTable, error) {
index, err := newElementIndex(context.TODO(), rootPath, option.elementIndexFlushTimeout.Nanoseconds()/int64(time.Second))
if err != nil {
return nil, err
}
tst := tsTable{
index: index,
fileSystem: fileSystem,
root: rootPath,
option: option,
l: l,
p: p,
}
tst.gc.init(&tst)
ee := fileSystem.ReadDir(rootPath)
if len(ee) == 0 {
t := &tst
t.startLoop(uint64(time.Now().UnixNano()))
return t, nil
}
var loadedParts []uint64
var loadedSnapshots []uint64
var needToDelete []string
for i := range ee {
if ee[i].IsDir() {
if ee[i].Name() == elementIndexFilename {
continue
}
p, err := parseEpoch(ee[i].Name())
if err != nil {
l.Info().Err(err).Msg("cannot parse part file name. skip and delete it")
needToDelete = append(needToDelete, ee[i].Name())
continue
}
err = validatePartMetadata(fileSystem, filepath.Join(rootPath, ee[i].Name()))
if err != nil {
l.Info().Err(err).Msg("cannot validate part metadata. skip and delete it")
needToDelete = append(needToDelete, ee[i].Name())
continue
}
loadedParts = append(loadedParts, p)
continue
}
if filepath.Ext(ee[i].Name()) != snapshotSuffix {
continue
}
snapshot, err := parseSnapshot(ee[i].Name())
if err != nil {
l.Info().Err(err).Msg("cannot parse snapshot file name. skip and delete it")
needToDelete = append(needToDelete, ee[i].Name())
continue
}
loadedSnapshots = append(loadedSnapshots, snapshot)
}
for i := range needToDelete {
l.Info().Str("path", filepath.Join(rootPath, needToDelete[i])).Msg("delete invalid directory or file")
fileSystem.MustRMAll(filepath.Join(rootPath, needToDelete[i]))
}
if len(loadedParts) == 0 || len(loadedSnapshots) == 0 {
t := &tst
t.startLoop(uint64(time.Now().UnixNano()))
return t, nil
}
sort.Slice(loadedSnapshots, func(i, j int) bool {
return loadedSnapshots[i] > loadedSnapshots[j]
})
epoch := loadedSnapshots[0]
t := &tst
t.loadSnapshot(epoch, loadedParts)
t.startLoop(epoch)
return t, nil
}
func (tst *tsTable) Index() *elementIndex {
return tst.index
}
func (tst *tsTable) Close() error {
if tst.loopCloser != nil {
tst.loopCloser.Done()
tst.loopCloser.CloseThenWait()
}
tst.RLock()
defer tst.RUnlock()
if tst.snapshot == nil {
return tst.index.Close()
}
tst.snapshot.decRef()
tst.snapshot = nil
return tst.index.Close()
}
func (tst *tsTable) mustAddElements(es *elements) {
if len(es.seriesIDs) == 0 {
return
}
mp := generateMemPart()
mp.mustInitFromElements(es)
p := openMemPart(mp)
ind := generateIntroduction()
defer releaseIntroduction(ind)
ind.applied = make(chan struct{})
ind.memPart = newPartWrapper(mp, p)
ind.memPart.p.partMetadata.ID = atomic.AddUint64(&tst.curPartID, 1)
select {
case tst.introductions <- ind:
case <-tst.loopCloser.CloseNotify():
return
}
select {
case <-ind.applied:
case <-tst.loopCloser.CloseNotify():
}
}
func (tst *tsTable) getElement(seriesID common.SeriesID, timestamp int64, tagProjection []pbv1.TagProjection) (*element, int, error) {
s := tst.currentSnapshot()
if s == nil {
return nil, 0, fmt.Errorf("snapshot is absent, cannot find element with seriesID %d and timestamp %d", seriesID, timestamp)
}
defer s.decRef()
for _, p := range s.parts {
if !p.p.containTimestamp(timestamp) {
continue
}
elem, count, err := p.p.getElement(seriesID, timestamp, tagProjection)
if err == nil {
return elem, count, nil
}
}
return nil, 0, fmt.Errorf("cannot find element with seriesID %d and timestamp %d", seriesID, timestamp)
}
type tstIter struct {
err error
parts []*part
piPool []partIter
piHeap partIterHeap
nextBlockNoop bool
}
func (ti *tstIter) reset() {
for i := range ti.parts {
ti.parts[i] = nil
}
ti.parts = ti.parts[:0]
for i := range ti.piPool {
ti.piPool[i].reset()
}
ti.piPool = ti.piPool[:0]
for i := range ti.piHeap {
ti.piHeap[i] = nil
}
ti.piHeap = ti.piHeap[:0]
ti.err = nil
ti.nextBlockNoop = false
}
func (ti *tstIter) init(bma *blockMetadataArray, parts []*part, sids []common.SeriesID, minTimestamp, maxTimestamp int64) {
ti.reset()
ti.parts = parts
if n := len(ti.parts) - cap(ti.piPool); n > 0 {
ti.piPool = append(ti.piPool[:cap(ti.piPool)], make([]partIter, n)...)
}
ti.piPool = ti.piPool[:len(ti.parts)]
for i, p := range ti.parts {
ti.piPool[i].init(bma, p, sids, minTimestamp, maxTimestamp)
}
ti.piHeap = ti.piHeap[:0]
for i := range ti.piPool {
ps := &ti.piPool[i]
if !ps.nextBlock() {
if err := ps.error(); err != nil {
ti.err = fmt.Errorf("cannot initialize tsTable iteration: %w", err)
return
}
continue
}
ti.piHeap = append(ti.piHeap, ps)
}
if len(ti.piHeap) == 0 {
ti.err = io.EOF
return
}
heap.Init(&ti.piHeap)
ti.nextBlockNoop = true
}
func (ti *tstIter) nextBlock() bool {
if ti.err != nil {
return false
}
if ti.nextBlockNoop {
ti.nextBlockNoop = false
return true
}
ti.err = ti.next()
if ti.err != nil {
if errors.Is(ti.err, io.EOF) {
ti.err = fmt.Errorf("cannot obtain the next block to search in the partition: %w", ti.err)
}
return false
}
return true
}
func (ti *tstIter) next() error {
psMin := ti.piHeap[0]
if psMin.nextBlock() {
heap.Fix(&ti.piHeap, 0)
return nil
}
if err := psMin.error(); err != nil {
return err
}
heap.Pop(&ti.piHeap)
if len(ti.piHeap) == 0 {
return io.EOF
}
return nil
}
func (ti *tstIter) Error() error {
if errors.Is(ti.err, io.EOF) {
return nil
}
return ti.err
}
type partIterHeap []*partIter
func (pih *partIterHeap) Len() int {
return len(*pih)
}
func (pih *partIterHeap) Less(i, j int) bool {
x := *pih
return x[i].curBlock.less(x[j].curBlock)
}
func (pih *partIterHeap) Swap(i, j int) {
x := *pih
x[i], x[j] = x[j], x[i]
}
func (pih *partIterHeap) Push(x any) {
*pih = append(*pih, x.(*partIter))
}
func (pih *partIterHeap) Pop() any {
a := *pih
v := a[len(a)-1]
*pih = a[:len(a)-1]
return v
}