-
Notifications
You must be signed in to change notification settings - Fork 82
/
service.go
134 lines (115 loc) · 4.06 KB
/
service.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
// 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 (
"context"
"math"
"path"
"github.com/pkg/errors"
"github.com/apache/skywalking-banyandb/api/data"
commonv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/common/v1"
databasev1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/database/v1"
"github.com/apache/skywalking-banyandb/banyand/metadata"
"github.com/apache/skywalking-banyandb/banyand/observability"
"github.com/apache/skywalking-banyandb/banyand/queue"
"github.com/apache/skywalking-banyandb/pkg/bus"
"github.com/apache/skywalking-banyandb/pkg/logger"
"github.com/apache/skywalking-banyandb/pkg/run"
resourceSchema "github.com/apache/skywalking-banyandb/pkg/schema"
)
var (
errEmptyRootPath = errors.New("root path is empty")
// ErrStreamNotExist denotes a stream doesn't exist in the metadata repo.
ErrStreamNotExist = errors.New("stream doesn't exist")
)
// Service allows inspecting the stream elements.
type Service interface {
run.PreRunner
run.Config
run.Service
Query
}
var _ Service = (*service)(nil)
type service struct {
schemaRepo schemaRepo
writeListener bus.MessageListener
metadata metadata.Repo
pipeline queue.Server
localPipeline queue.Queue
l *logger.Logger
root string
option option
}
func (s *service) Stream(metadata *commonv1.Metadata) (Stream, error) {
sm, ok := s.schemaRepo.loadStream(metadata)
if !ok {
return nil, errors.WithStack(ErrStreamNotExist)
}
return sm, nil
}
func (s *service) LoadGroup(name string) (resourceSchema.Group, bool) {
return s.schemaRepo.LoadGroup(name)
}
func (s *service) FlagSet() *run.FlagSet {
flagS := run.NewFlagSet("storage")
flagS.StringVar(&s.root, "stream-root-path", "/tmp", "the root path of database")
flagS.DurationVar(&s.option.flushTimeout, "stream-flush-timeout", defaultFlushTimeout, "the memory data timeout of stream")
flagS.DurationVar(&s.option.elementIndexFlushTimeout, "element-index-flush-timeout", defaultFlushTimeout, "the elementIndex timeout of stream")
s.option.mergePolicy = newDefaultMergePolicy()
flagS.Uint64Var(&s.option.mergePolicy.maxFanOutSize, "max-fan-out-size", math.MaxUint64, "the upper bound of a single file size after merge")
return flagS
}
func (s *service) Validate() error {
if s.root == "" {
return errEmptyRootPath
}
return nil
}
func (s *service) Name() string {
return "stream"
}
func (s *service) Role() databasev1.Role {
return databasev1.Role_ROLE_DATA
}
func (s *service) PreRun(_ context.Context) error {
s.l = logger.GetLogger(s.Name())
path := path.Join(s.root, s.Name())
observability.UpdatePath(path)
s.localPipeline = queue.Local()
s.schemaRepo = newSchemaRepo(path, s)
// run a serial watcher
s.writeListener = setUpWriteCallback(s.l, &s.schemaRepo)
err := s.pipeline.Subscribe(data.TopicStreamWrite, s.writeListener)
if err != nil {
return err
}
return s.localPipeline.Subscribe(data.TopicStreamWrite, s.writeListener)
}
func (s *service) Serve() run.StopNotify {
return s.schemaRepo.StopCh()
}
func (s *service) GracefulStop() {
s.localPipeline.GracefulStop()
s.schemaRepo.Close()
}
// NewService returns a new service.
func NewService(_ context.Context, metadata metadata.Repo, pipeline queue.Server) (Service, error) {
return &service{
metadata: metadata,
pipeline: pipeline,
}, nil
}