-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathexportation.go
192 lines (173 loc) · 4.95 KB
/
exportation.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
// Copyright (C) 2019-2025 vdaas.org vald team <[email protected]>
//
// 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"
"os"
"syscall"
"github.com/vdaas/vald/internal/client/v1/client/vald"
iconf "github.com/vdaas/vald/internal/config"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/log"
"github.com/vdaas/vald/internal/net/grpc"
"github.com/vdaas/vald/internal/net/grpc/interceptor/server/recover"
"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/internal/sync/errgroup"
"github.com/vdaas/vald/pkg/index/job/exportation/config"
"github.com/vdaas/vald/pkg/index/job/exportation/service"
)
type run struct {
eg errgroup.Group
cfg *config.Data
observability observability.Observability
server starter.Server
exporter service.Exporter
}
// New returns Runner instance.
func New(cfg *config.Data) (_ runner.Runner, err error) {
eg := errgroup.Get()
gOpts, err := cfg.Exporter.Gateway.Opts()
if err != nil {
return nil, err
}
// skipcq: CRT-D0001
gOpts = append(gOpts, grpc.WithErrGroup(eg))
gateway, err := vald.New(vald.WithClient(grpc.New(gOpts...)))
if err != nil {
return nil, err
}
exporter, err := service.New(
service.WithStreamListConcurrency(cfg.Exporter.Concurrency),
service.WithIndexPath(cfg.Exporter.IndexPath),
service.WithGateway(gateway),
)
if err != nil {
return nil, err
}
var obs observability.Observability
if cfg.Observability.Enabled {
obs, err = observability.NewWithConfig(
cfg.Observability,
)
if err != nil {
return nil, err
}
}
grpcServerOptions := []server.Option{
server.WithGRPCOption(
grpc.ChainUnaryInterceptor(recover.RecoverInterceptor()),
grpc.ChainStreamInterceptor(recover.RecoverStreamInterceptor()),
),
}
// For health check and metrics
srv, err := starter.New(starter.WithConfig(cfg.Server),
starter.WithGRPC(func(_ *iconf.Server) []server.Option {
return grpcServerOptions
}),
)
if err != nil {
return nil, err
}
return &run{
eg: eg,
cfg: cfg,
observability: obs,
server: srv,
exporter: exporter,
}, nil
}
// PreStart is a method called before execution of Start, and it invokes the PreStart method of observability.
func (r *run) PreStart(ctx context.Context) error {
if r.observability != nil {
return r.observability.PreStart(ctx)
}
return nil
}
// Start is a method used to initiate an operation in the run, and it returns a channel for receiving errors
// during the operation and an error representing any initialization errors.
func (r *run) Start(ctx context.Context) (<-chan error, error) {
ech := make(chan error, 3)
var sech, oech <-chan error
if r.observability != nil {
oech = r.observability.Start(ctx)
}
sech = r.server.ListenAndServe(ctx)
cech, err := r.exporter.StartClient(ctx)
if err != nil {
close(ech)
return nil, err
}
r.eg.Go(safety.RecoverFunc(func() (err error) {
defer func() {
p, err := os.FindProcess(os.Getpid())
if err != nil {
// using Fatal to avoid this process to be zombie
// skipcq: RVV-A0003
log.Fatalf("failed to find my pid to kill %v", err)
return
}
log.Info("sending SIGTERM to myself to stop this job")
if err := p.Signal(syscall.SIGTERM); err != nil {
log.Error(err)
}
}()
return r.exporter.Start(ctx)
}))
r.eg.Go(safety.RecoverFunc(func() (err error) {
defer close(ech)
for {
select {
case <-ctx.Done():
return ctx.Err()
case err = <-oech:
case err = <-sech:
case err = <-cech:
}
if err != nil {
select {
case <-ctx.Done():
return errors.Join(ctx.Err(), err)
case ech <- err:
}
}
}
}))
return ech, nil
}
// PreStop is a method called before execution of Stop.
func (r *run) PreStop(ctx context.Context) error {
return r.exporter.PreStop(ctx)
}
// Stop is a method used to stop an operation in the run.
func (r *run) Stop(ctx context.Context) (errs error) {
if r.observability != nil {
if err := r.observability.Stop(ctx); err != nil {
errs = errors.Join(errs, err)
}
}
if r.server != nil {
if err := r.server.Shutdown(ctx); err != nil {
errs = errors.Join(errs, err)
}
}
return errs
}
// PostStop is a method called after execution of Stop.
func (*run) PostStop(_ context.Context) error {
return nil
}