/
meta.go
173 lines (158 loc) · 4.12 KB
/
meta.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
//
// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
//
// 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
//
// https://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 usecase
import (
"context"
"github.com/vdaas/vald/apis/grpc/meta"
iconf "github.com/vdaas/vald/internal/config"
"github.com/vdaas/vald/internal/errgroup"
"github.com/vdaas/vald/internal/log"
"github.com/vdaas/vald/internal/net/grpc"
"github.com/vdaas/vald/internal/net/grpc/metric"
"github.com/vdaas/vald/internal/observability"
"github.com/vdaas/vald/internal/runner"
"github.com/vdaas/vald/internal/safety"
"github.com/vdaas/vald/internal/servers/server"
"github.com/vdaas/vald/internal/servers/starter"
"github.com/vdaas/vald/pkg/meta/redis/config"
handler "github.com/vdaas/vald/pkg/meta/redis/handler/grpc"
"github.com/vdaas/vald/pkg/meta/redis/handler/rest"
"github.com/vdaas/vald/pkg/meta/redis/router"
"github.com/vdaas/vald/pkg/meta/redis/service"
)
type run struct {
eg errgroup.Group
cfg *config.Data
rd service.Redis
server starter.Server
observability observability.Observability
}
func New(cfg *config.Data) (r runner.Runner, err error) {
rd, err := service.New(cfg.Redis)
if err != nil {
return nil, err
}
g := handler.New(handler.WithRedis(rd))
eg := errgroup.Get()
grpcServerOptions := []server.Option{
server.WithGRPCRegistFunc(func(srv *grpc.Server) {
meta.RegisterMetaServer(srv, g)
}),
server.WithPreStartFunc(func() error {
// TODO check unbackupped upstream
return nil
}),
server.WithPreStopFunction(func() error {
// TODO backup all index data here
return nil
}),
}
var obs observability.Observability
if cfg.Observability.Enabled {
obs, err = observability.NewWithConfig(cfg.Observability)
if err != nil {
return nil, err
}
grpcServerOptions = append(
grpcServerOptions,
server.WithGRPCOption(
grpc.StatsHandler(metric.NewServerHandler()),
),
)
}
srv, err := starter.New(
starter.WithConfig(cfg.Server),
starter.WithREST(func(sc *iconf.Server) []server.Option {
return []server.Option{
server.WithHTTPHandler(
router.New(
router.WithTimeout(sc.HTTP.HandlerTimeout),
router.WithErrGroup(eg),
router.WithHandler(
rest.New(
rest.WithMeta(g),
),
),
)),
}
}),
starter.WithGRPC(func(sc *iconf.Server) []server.Option {
return grpcServerOptions
}),
// TODO add GraphQL handler
)
if err != nil {
return nil, err
}
return &run{
eg: eg,
cfg: cfg,
rd: rd,
server: srv,
observability: obs,
}, nil
}
func (r *run) PreStart(ctx context.Context) error {
log.Info("daemon pre-start")
err := r.rd.Connect(ctx)
if err != nil {
return err
}
if r.observability != nil {
return r.observability.PreStart(ctx)
}
return nil
}
func (r *run) Start(ctx context.Context) (<-chan error, error) {
ech := make(chan error, 2)
var oech, sech <-chan error
r.eg.Go(safety.RecoverFunc(func() (err error) {
log.Info("daemon start")
defer close(ech)
if r.observability != nil {
oech = r.observability.Start(ctx)
}
sech = r.server.ListenAndServe(ctx)
for {
select {
case <-ctx.Done():
return ctx.Err()
case err = <-oech:
case err = <-sech:
}
if err != nil {
select {
case <-ctx.Done():
return ctx.Err()
case ech <- err:
}
}
}
}))
return ech, nil
}
func (r *run) PreStop(ctx context.Context) error {
return nil
}
func (r *run) Stop(ctx context.Context) error {
if r.observability != nil {
r.observability.Stop(ctx)
}
return r.server.Shutdown(ctx)
}
func (r *run) PostStop(ctx context.Context) error {
return r.rd.Disconnect()
}