/
eventpersisterops.go
322 lines (298 loc) · 9.89 KB
/
eventpersisterops.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
// Copyright 2024 The Bucketeer Authors.
//
// Licensed 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 server
import (
"context"
"errors"
"time"
"go.uber.org/zap"
kingpin "gopkg.in/alecthomas/kingpin.v2"
aoclient "github.com/bucketeer-io/bucketeer/pkg/autoops/client"
"github.com/bucketeer-io/bucketeer/pkg/cache"
cachev3 "github.com/bucketeer-io/bucketeer/pkg/cache/v3"
"github.com/bucketeer-io/bucketeer/pkg/cli"
"github.com/bucketeer-io/bucketeer/pkg/eventpersisterops/persister"
featureclient "github.com/bucketeer-io/bucketeer/pkg/feature/client"
"github.com/bucketeer-io/bucketeer/pkg/health"
"github.com/bucketeer-io/bucketeer/pkg/metrics"
"github.com/bucketeer-io/bucketeer/pkg/pubsub"
redisv3 "github.com/bucketeer-io/bucketeer/pkg/redis/v3"
"github.com/bucketeer-io/bucketeer/pkg/rpc"
"github.com/bucketeer-io/bucketeer/pkg/rpc/client"
"github.com/bucketeer-io/bucketeer/pkg/storage/v2/mysql"
)
const (
command = "server"
evalEvtSvcName = "event-persister-evaluation-events-ops"
evalGoalSvcName = "event-persister-goal-events-ops"
)
var errUnknownSvcName = errors.New("persister: unknown service name")
type server struct {
*kingpin.CmdClause
serviceName *string
// egress services
featureService *string
autoOpsService *string
// rpc
port *int
serviceTokenPath *string
certPath *string
keyPath *string
// pubsub
project *string
subscription *string
topic *string
pullerNumGoroutines *int
pullerMaxOutstandingMessages *int
pullerMaxOutstandingBytes *int
// redis
redisServerName *string
redisAddr *string
redisPoolMaxIdle *int
redisPoolMaxActive *int
// mysql
mysqlUser *string
mysqlPass *string
mysqlHost *string
mysqlPort *int
mysqlDbName *string
// batch options
maxMPS *int
numWorkers *int
flushSize *int
checkInterval *time.Duration
flushInterval *time.Duration
flushTimeout *time.Duration
}
func RegisterServerCommand(r cli.CommandRegistry, p cli.ParentCommand) cli.Command {
cmd := p.Command(command, "Start the server")
server := &server{
CmdClause: cmd,
serviceName: cmd.Flag("service-name", "Service name.").Required().String(),
project: cmd.Flag("project", "Google Cloud project name.").Required().String(),
port: cmd.Flag("port", "Port to bind to.").Required().Int(),
featureService: cmd.Flag("feature-service", "bucketeer-feature-service address.").Required().String(),
autoOpsService: cmd.Flag("auto-ops-service", "bucketeer-auto-ops-service address.").Required().String(),
certPath: cmd.Flag("cert", "Path to TLS certificate.").Required().String(),
keyPath: cmd.Flag("key", "Path to TLS key.").Required().String(),
serviceTokenPath: cmd.Flag("service-token", "Path to service token.").Required().String(),
subscription: cmd.Flag("subscription", "Google PubSub subscription name.").Required().String(),
topic: cmd.Flag("topic", "Google PubSub topic name.").Required().String(),
pullerNumGoroutines: cmd.Flag(
"puller-num-goroutines",
"Number of goroutines will be spawned to pull messages.",
).Required().Int(),
pullerMaxOutstandingMessages: cmd.Flag(
"puller-max-outstanding-messages",
"Maximum number of unprocessed messages.",
).Required().Int(),
pullerMaxOutstandingBytes: cmd.Flag(
"puller-max-outstanding-bytes",
"Maximum size of unprocessed messages.",
).Required().Int(),
redisServerName: cmd.Flag("redis-server-name", "Name of the redis.").Required().String(),
redisAddr: cmd.Flag("redis-addr", "Address of the redis.").Required().String(),
redisPoolMaxIdle: cmd.Flag(
"redis-pool-max-idle",
"Maximum number of idle connections in the pool.",
).Required().Int(),
redisPoolMaxActive: cmd.Flag(
"redis-pool-max-active",
"Maximum number of connections allocated by the pool at a given time.",
).Required().Int(),
mysqlUser: cmd.Flag("mysql-user", "MySQL user.").Required().String(),
mysqlPass: cmd.Flag("mysql-pass", "MySQL password.").Required().String(),
mysqlHost: cmd.Flag("mysql-host", "MySQL host.").Required().String(),
mysqlPort: cmd.Flag("mysql-port", "MySQL port.").Required().Int(),
mysqlDbName: cmd.Flag("mysql-db-name", "MySQL database name.").Required().String(),
maxMPS: cmd.Flag("max-mps", "Maximum messages should be handled in a second.").Required().Int(),
numWorkers: cmd.Flag("num-workers", "Number of workers.").Required().Int(),
flushSize: cmd.Flag(
"flush-size",
"Maximum number of messages to batch before writing to datastore.",
).Required().Int(),
checkInterval: cmd.Flag(
"check-interval",
"Interval to check if there are auto ops rules to be handled.",
).Required().Duration(),
flushInterval: cmd.Flag("flush-interval", "Maximum interval between two flushes.").Required().Duration(),
flushTimeout: cmd.Flag("flush-timeout", "Maximum time for a flush to finish.").Required().Duration(),
}
r.RegisterCommand(server)
return server
}
func (s *server) Run(ctx context.Context, metrics metrics.Metrics, logger *zap.Logger) error {
registerer := metrics.DefaultRegisterer()
pubsubClient, err := s.createPubsubClient(ctx, logger)
if err != nil {
return err
}
// mysqlClient
mysqlClient, err := s.createMySQLClient(ctx, registerer, logger)
if err != nil {
return err
}
creds, err := client.NewPerRPCCredentials(*s.serviceTokenPath)
if err != nil {
return err
}
featureClient, err := featureclient.NewClient(*s.featureService, *s.certPath,
client.WithPerRPCCredentials(creds),
client.WithDialTimeout(30*time.Second),
client.WithBlock(),
client.WithMetrics(registerer),
client.WithLogger(logger),
)
if err != nil {
return err
}
defer featureClient.Close()
autoOpsClient, err := aoclient.NewClient(*s.autoOpsService, *s.certPath,
client.WithPerRPCCredentials(creds),
client.WithDialTimeout(30*time.Second),
client.WithBlock(),
client.WithMetrics(registerer),
client.WithLogger(logger),
)
if err != nil {
return err
}
defer autoOpsClient.Close()
redisV3Client, err := redisv3.NewClient(
*s.redisAddr,
redisv3.WithPoolSize(*s.redisPoolMaxActive),
redisv3.WithMinIdleConns(*s.redisPoolMaxIdle),
redisv3.WithServerName(*s.redisServerName),
redisv3.WithMetrics(registerer),
redisv3.WithLogger(logger),
)
if err != nil {
return err
}
defer redisV3Client.Close()
redisV3Cache := cachev3.NewRedisCache(redisV3Client)
updater, err := s.newUserCountUpdater(
ctx,
featureClient,
autoOpsClient,
redisV3Cache,
registerer,
logger,
)
if err != nil {
return err
}
p := persister.NewPersister(
updater,
mysqlClient,
pubsubClient,
*s.subscription,
*s.topic,
*s.pullerNumGoroutines,
*s.pullerMaxOutstandingMessages,
*s.pullerMaxOutstandingBytes,
persister.WithMaxMPS(*s.maxMPS),
persister.WithNumWorkers(*s.numWorkers),
persister.WithFlushSize(*s.flushSize),
persister.WithCheckInterval(*s.checkInterval),
persister.WithFlushInterval(*s.flushInterval),
persister.WithFlushTimeout(*s.flushTimeout),
persister.WithMetrics(registerer),
persister.WithLogger(logger),
)
defer p.Stop()
go p.Run() // nolint:errcheck
healthChecker := health.NewGrpcChecker(
health.WithTimeout(time.Second),
health.WithCheck("metrics", metrics.Check),
health.WithCheck("persister", p.Check),
health.WithCheck("redis", redisV3Client.Check),
)
go healthChecker.Run(ctx)
server := rpc.NewServer(healthChecker, *s.certPath, *s.keyPath,
"event-persister-ops",
rpc.WithPort(*s.port),
rpc.WithMetrics(registerer),
rpc.WithLogger(logger),
rpc.WithHandler("/health", healthChecker),
)
defer server.Stop(10 * time.Second)
go server.Run()
// Ensure to stop the health check before stopping the application
// so the Kubernetes Readiness can detect it faster and remove the pod
// from the service load balancer.
defer healthChecker.Stop()
<-ctx.Done()
return nil
}
func (s *server) createPubsubClient(ctx context.Context, logger *zap.Logger) (*pubsub.Client, error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
pubsubClient, err := pubsub.NewClient(ctx, *s.project, pubsub.WithLogger(logger))
if err != nil {
logger.Error("Failed to create PubSub client", zap.Error(err))
return nil, err
}
return pubsubClient, nil
}
func (s *server) newUserCountUpdater(
ctx context.Context,
featureClient featureclient.Client,
autoOpsClient aoclient.Client,
redis cache.MultiGetDeleteCountCache,
registerer metrics.Registerer,
logger *zap.Logger,
) (persister.Updater, error) {
var updater persister.Updater
var err error
switch *s.serviceName {
case evalEvtSvcName:
updater = persister.NewEvalUserCountUpdater(
ctx,
featureClient,
autoOpsClient,
cachev3.NewEventCountCache(redis),
cachev3.NewAutoOpsRulesCache(redis),
logger,
)
case evalGoalSvcName:
updater = persister.NewGoalUserCountUpdater(
ctx,
featureClient,
autoOpsClient,
cachev3.NewEventCountCache(redis),
cachev3.NewAutoOpsRulesCache(redis),
logger,
)
default:
return nil, errUnknownSvcName
}
return updater, err
}
func (s *server) createMySQLClient(
ctx context.Context,
registerer metrics.Registerer,
logger *zap.Logger,
) (mysql.Client, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
return mysql.NewClient(
ctx,
*s.mysqlUser, *s.mysqlPass, *s.mysqlHost,
*s.mysqlPort,
*s.mysqlDbName,
mysql.WithLogger(logger),
mysql.WithMetrics(registerer),
)
}