Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce two key feats to tsdb #177

Merged
merged 2 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions banyand/measure/measure_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *measure) Write(value *measurev1.DataPointValue) error {
}

func (s *measure) write(shardID common.ShardID, seriesHashKey []byte, value *measurev1.DataPointValue, cb index.CallbackFn) error {
t := value.GetTimestamp().AsTime()
t := value.GetTimestamp().AsTime().Local()
if err := timestamp.Check(t); err != nil {
return errors.WithMessage(err, "writing stream")
}
Expand All @@ -81,7 +81,7 @@ func (s *measure) write(shardID common.ShardID, seriesHashKey []byte, value *mea
if err != nil {
return err
}
wp, err := series.Span(timestamp.NewInclusiveTimeRangeDuration(t, 0))
wp, err := series.Create(t)
if err != nil {
if wp != nil {
_ = wp.Close()
Expand Down Expand Up @@ -189,7 +189,7 @@ func (w *writeCallback) Rev(message bus.Message) (resp bus.Message) {
}
err := stm.write(common.ShardID(writeEvent.GetShardId()), writeEvent.GetSeriesHash(), writeEvent.GetRequest().GetDataPoint(), nil)
if err != nil {
w.l.Debug().Err(err).Msg("fail to write entity")
w.l.Error().Err(err).Msg("fail to write entity")
}
return
}
Expand Down
3 changes: 3 additions & 0 deletions banyand/measure/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ func (s *supplier) OpenDB(groupSchema *commonv1.Group) (tsdb.Database, error) {
if opts.SegmentInterval, err = pb_v1.ToIntervalRule(groupSchema.ResourceOpts.SegmentInterval); err != nil {
return nil, err
}
if opts.TTL, err = pb_v1.ToIntervalRule(groupSchema.ResourceOpts.Ttl); err != nil {
return nil, err
}
return tsdb.OpenDatabase(
context.WithValue(context.Background(), common.PositionKey, common.Position{
Module: "measure",
Expand Down
3 changes: 3 additions & 0 deletions banyand/stream/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ func (s *supplier) OpenDB(groupSchema *commonv1.Group) (tsdb.Database, error) {
if opts.SegmentInterval, err = pb_v1.ToIntervalRule(groupSchema.ResourceOpts.SegmentInterval); err != nil {
return nil, err
}
if opts.TTL, err = pb_v1.ToIntervalRule(groupSchema.ResourceOpts.Ttl); err != nil {
return nil, err
}
return tsdb.OpenDatabase(
context.WithValue(context.Background(), common.PositionKey, common.Position{
Module: "stream",
Expand Down
4 changes: 2 additions & 2 deletions banyand/stream/stream_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (s *stream) write(shardID common.ShardID, seriesHashKey []byte, value *stre
return err
}
t := timestamp.MToN(tp)
wp, err := series.Span(timestamp.NewInclusiveTimeRangeDuration(t, 0))
wp, err := series.Create(t)
if err != nil {
if wp != nil {
_ = wp.Close()
Expand Down Expand Up @@ -183,7 +183,7 @@ func (w *writeCallback) Rev(message bus.Message) (resp bus.Message) {
}
err := stm.write(common.ShardID(writeEvent.GetShardId()), writeEvent.GetSeriesHash(), writeEvent.GetRequest().GetElement(), nil)
if err != nil {
w.l.Debug().Err(err).Msg("fail to write entity")
w.l.Error().Err(err).Msg("fail to write entity")
}
return
}
22 changes: 22 additions & 0 deletions banyand/tsdb/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"fmt"
"io"
"os"
"path"
"runtime"
"strconv"
Expand All @@ -38,6 +39,7 @@ import (
"github.com/apache/skywalking-banyandb/pkg/index/lsm"
"github.com/apache/skywalking-banyandb/pkg/logger"
"github.com/apache/skywalking-banyandb/pkg/timestamp"
"github.com/pkg/errors"
)

const (
Expand All @@ -55,6 +57,7 @@ type block struct {
suffix string
ref *atomic.Int32
closed *atomic.Bool
deleted *atomic.Bool
lock sync.RWMutex
position common.Position
memSize int64
Expand Down Expand Up @@ -100,6 +103,7 @@ func newBlock(ctx context.Context, opts blockOpts) (b *block, err error) {
flushCh: make(chan struct{}, 1),
ref: &atomic.Int32{},
closed: &atomic.Bool{},
deleted: &atomic.Bool{},
queue: opts.queue,
}
b.options(ctx)
Expand Down Expand Up @@ -136,6 +140,9 @@ func (b *block) options(ctx context.Context) {
}

func (b *block) open() (err error) {
if b.deleted.Load() {
return nil
}
if b.store, err = kv.OpenTimeSeriesStore(
0,
path.Join(b.path, componentMain),
Expand Down Expand Up @@ -181,6 +188,9 @@ func (b *block) open() (err error) {
}

func (b *block) delegate() (blockDelegate, error) {
if b.deleted.Load() {
return nil, errors.WithMessagef(ErrBlockAbsent, "block %d is deleted", b.blockID)
}
if b.incRef() {
return &bDelegate{
delegate: b,
Expand Down Expand Up @@ -272,6 +282,18 @@ func (b *block) stopThenClose() {
b.close()
}

func (b *block) delete() error {
if b.deleted.Load() {
return nil
}
b.deleted.Store(true)
if b.Reporter != nil {
b.Stop()
}
b.close()
return os.RemoveAll(b.path)
}

func (b *block) Closed() bool {
return b.closed.Load()
}
Expand Down
20 changes: 20 additions & 0 deletions banyand/tsdb/bucket/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type EvictFn func(id interface{})

type Queue interface {
Push(id interface{})
Remove(id interface{})
Len() int
All() []interface{}
}
Expand Down Expand Up @@ -108,6 +109,25 @@ func (q *lruQueue) Push(id interface{}) {
q.recent.Add(id, nil)
}

func (q *lruQueue) Remove(id interface{}) {
q.lock.Lock()
defer q.lock.Unlock()

if q.frequent.Contains(id) {
q.frequent.Remove(id)
return
}

if q.recent.Contains(id) {
q.recent.Remove(id)
return
}

if q.recentEvict.Contains(id) {
q.recentEvict.Remove(id)
}
}

func (q *lruQueue) Len() int {
q.lock.RLock()
defer q.lock.RUnlock()
Expand Down
103 changes: 103 additions & 0 deletions banyand/tsdb/retention.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// 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 tsdb

import (
"sync"
"time"

"github.com/apache/skywalking-banyandb/pkg/logger"
"github.com/robfig/cron/v3"
)

type retentionController struct {
segment *segmentController
scheduler cron.Schedule
stopped bool
stopMux sync.Mutex
stopCh chan struct{}
duration time.Duration
l *logger.Logger
}

func newRetentionController(segment *segmentController, ttl IntervalRule) (*retentionController, error) {
var expr string
switch ttl.Unit {
case HOUR:
// Every hour on the 5th minute
expr = "5 *"
case DAY:
// Every day on 00:05
expr = "5 0"

}
parser := cron.NewParser(cron.Minute | cron.Hour)
scheduler, err := parser.Parse(expr)
if err != nil {
return nil, err
}
return &retentionController{
segment: segment,
scheduler: scheduler,
stopCh: make(chan struct{}),
l: segment.l.Named("retention-controller"),
duration: ttl.EstimatedDuration(),
}, nil
}

func (rc *retentionController) start() {
rc.stopMux.Lock()
if rc.stopped {
return
}
rc.stopMux.Unlock()
go rc.run()
}

func (rc *retentionController) run() {
rc.l.Info().Msg("start")
now := rc.segment.clock.Now()
for {
next := rc.scheduler.Next(now)
timer := rc.segment.clock.Timer(next.Sub(now))
for {
select {
case now = <-timer.C:
rc.l.Info().Time("now", now).Msg("wake")
if err := rc.segment.remove(now.Add(-rc.duration)); err != nil {
rc.l.Error().Err(err)
}
case <-rc.stopCh:
timer.Stop()
rc.l.Info().Msg("stop")
return
}
break
}
}
}

func (rc *retentionController) stop() {
rc.stopMux.Lock()
defer rc.stopMux.Unlock()
if rc.stopped {
return
}
rc.stopped = true
close(rc.stopCh)
}
Loading