-
Notifications
You must be signed in to change notification settings - Fork 3
/
agent.go
245 lines (218 loc) · 6.32 KB
/
agent.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
package agent
import (
"context"
"errors"
"fmt"
"github.com/paust-team/pirius/agent/config"
"github.com/paust-team/pirius/agent/pubsub"
"github.com/paust-team/pirius/agent/storage"
"github.com/paust-team/pirius/bootstrapping"
"github.com/paust-team/pirius/constants"
"github.com/paust-team/pirius/coordinating"
"github.com/paust-team/pirius/helper"
"github.com/paust-team/pirius/logger"
"github.com/paust-team/pirius/proto/pb"
"go.uber.org/zap"
"google.golang.org/grpc"
"net"
"os"
"sync"
)
type instance struct {
shouldQuit chan struct{}
db *storage.DB
config config.AgentConfig
running bool
meta *storage.AgentMeta
wg sync.WaitGroup
coordClient coordinating.CoordClient
bootstrapper *bootstrapping.BootstrapService
grpcServer *grpc.Server
}
func (s *instance) Start() error {
if s.config.RetentionPeriod() < constants.MinRetentionPeriod ||
s.config.RetentionPeriod() > constants.MaxRetentionPeriod {
logger.Error("Invalid retention period", zap.Uint32("retention", s.config.RetentionPeriod()))
return errors.New(fmt.Sprintf("Retention period should not be less than %d and should not be greater than %d", constants.MinRetentionPeriod, constants.MaxRetentionPeriod))
}
if err := os.MkdirAll(s.config.DataDir(), os.ModePerm); err != nil {
return err
}
if err := os.MkdirAll(s.config.LogDir(), os.ModePerm); err != nil {
return err
}
meta, err := storage.LoadAgentMeta(s.GetMetaPath())
if err != nil {
logger.Error(err.Error())
return err
}
s.meta = &meta
db, err := storage.NewDB(s.config.DBName(), s.config.DataDir())
if err != nil {
logger.Error(err.Error())
return err
}
s.db = db
s.coordClient = helper.BuildCoordClient(s.config.ZKQuorum(), s.config.ZKTimeout())
if err := s.coordClient.Connect(); err != nil {
logger.Error(err.Error())
return err
}
s.bootstrapper = bootstrapping.NewBootStrapService(s.coordClient)
s.running = true
s.shouldQuit = make(chan struct{})
s.wg = sync.WaitGroup{}
logger.Info("agent started with ",
zap.String("publisher-id", meta.PublisherID),
zap.String("subscriber-id", meta.SubscriberID),
zap.Uint("port", s.config.Port()))
return nil
}
func (s *instance) Stop() {
close(s.shouldQuit)
s.running = false
if s.grpcServer != nil {
s.grpcServer.Stop()
}
// gracefully stop
s.wg.Wait()
s.db.Close()
s.coordClient.Close()
storage.SaveAgentMeta(s.GetMetaPath(), *s.meta)
s.grpcServer = nil
s.bootstrapper = nil
logger.Info("agent finished")
}
func (s *instance) GetMetaPath() string {
return s.config.DataDir() + "/" + constants.AgentMetaFileName
}
func (s *instance) GetPublisherID() string {
return s.meta.PublisherID
}
func (s *instance) GetSubscriberID() string {
return s.meta.SubscriberID
}
func (s *instance) CleanAllData() {
logger.Warn("this func made for test purpose only")
if !s.running {
_ = s.db.Destroy()
os.RemoveAll(s.config.LogDir())
os.RemoveAll(s.config.DataDir())
}
}
type PubSubAgent struct {
instance
subscriber pubsub.Subscriber
publisher pubsub.Publisher
}
func NewPubSubAgent(config config.AgentConfig) *PubSubAgent {
return &PubSubAgent{
instance: instance{
config: config,
},
}
}
func (s *PubSubAgent) StartWithServer() error {
if err := s.instance.Start(); err != nil {
return err
}
agentAddress := fmt.Sprintf("%s:%d", s.config.Host(), s.config.Port())
s.subscriber = pubsub.NewSubscriber(s.meta.SubscriberID, s.bootstrapper, s.meta.SubscribedOffsets)
s.publisher = pubsub.NewPublisher(s.meta.PublisherID, agentAddress, s.db, s.bootstrapper, s.meta.PublishedOffsets, s.meta.LastFetchedOffset)
var opts []grpc.ServerOption
grpcServer := grpc.NewServer(opts...)
pb.RegisterPubSubServer(grpcServer, &s.publisher)
lis, err := net.Listen("tcp", agentAddress)
logger.Debug("grpc server listening", zap.String("publisher-id", s.meta.SubscriberID), zap.String("address", agentAddress))
if err != nil {
s.running = false
return err
}
s.grpcServer = grpcServer
s.wg.Add(1)
go func() {
defer s.wg.Done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
//start retention scheduler
retentionScheduler := storage.NewRetentionScheduler(s.db, s.config.RetentionCheckInterval())
retentionScheduler.Run(ctx)
if err = grpcServer.Serve(lis); err != nil {
logger.Error(err.Error(), zap.String("publisher-id", s.meta.PublisherID))
} else {
logger.Info("grpc server stopped", zap.String("publisher-id", s.meta.PublisherID))
}
s.running = false
}()
return nil
}
func (s *PubSubAgent) Start() error {
if err := s.instance.Start(); err != nil {
return err
}
agentAddress := fmt.Sprintf("%s:%d", s.config.Host(), s.config.Port())
s.subscriber = pubsub.NewSubscriber(s.meta.SubscriberID, s.bootstrapper, s.meta.SubscribedOffsets)
s.publisher = pubsub.NewPublisher(s.meta.PublisherID, agentAddress, s.db, s.bootstrapper, s.meta.PublishedOffsets, s.meta.LastFetchedOffset)
return nil
}
func (s *PubSubAgent) StartPublish(ctx context.Context, topicName string, sendChan chan pubsub.TopicData) error {
if !s.running || s.grpcServer == nil {
return errors.New("not running state")
}
retentionPeriod := uint64(s.config.RetentionPeriod() * 60 * 60 * 24)
ctx, cancel := context.WithCancel(ctx)
errCh, err := s.publisher.StartTopicPublication(ctx, topicName, retentionPeriod, sendChan)
if err != nil {
cancel()
return err
}
s.wg.Add(1)
go func() {
defer s.wg.Done()
defer s.publisher.Wait()
defer cancel()
for {
select {
case err = <-errCh:
if err != nil {
logger.Error(err.Error())
}
return
case <-s.shouldQuit:
logger.Info("stop publish from agent stopped")
return
}
}
}()
return nil
}
func (s *PubSubAgent) StartSubscribe(ctx context.Context, topicName string, batchSize, flushInterval uint32) (chan []pubsub.SubscriptionResult, error) {
if !s.running {
return nil, errors.New("not running state")
}
ctx, cancel := context.WithCancel(ctx)
recvCh, errCh, err := s.subscriber.StartTopicSubscription(ctx, topicName, batchSize, flushInterval)
if err != nil {
cancel()
return nil, err
}
s.wg.Add(1)
go func() {
defer s.wg.Done()
defer s.subscriber.Wait()
defer cancel()
for {
select {
case err = <-errCh:
if err != nil {
logger.Error(err.Error())
}
return
case <-s.shouldQuit:
logger.Info("stop subscribe from agent stopped")
return
}
}
}()
return recvCh, nil
}