/
indexer.go
330 lines (314 loc) · 9.14 KB
/
indexer.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
323
324
325
326
327
328
329
330
//
// Copyright (C) 2019-2024 vdaas.org vald team <vald@vdaas.org>
//
// 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 service
package service
import (
"context"
"math"
"reflect"
"sync/atomic"
"time"
agent "github.com/vdaas/vald/apis/grpc/v1/agent/core"
"github.com/vdaas/vald/apis/grpc/v1/payload"
"github.com/vdaas/vald/internal/client/v1/client/discoverer"
"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/codes"
"github.com/vdaas/vald/internal/net/grpc/status"
"github.com/vdaas/vald/internal/observability/trace"
"github.com/vdaas/vald/internal/safety"
"github.com/vdaas/vald/internal/sync"
"github.com/vdaas/vald/internal/sync/errgroup"
)
type Indexer interface {
Start(ctx context.Context) (<-chan error, error)
NumberOfUUIDs() uint32
NumberOfUncommittedUUIDs() uint32
IsIndexing() bool
}
type index struct {
client discoverer.Client
eg errgroup.Group
creationPoolSize uint32
indexDuration time.Duration
indexDurationLimit time.Duration
saveIndexDurationLimit time.Duration
saveIndexWaitDuration time.Duration
saveIndexTargetAddrCh chan string
schMap sync.Map[string, any]
concurrency int
indexInfos sync.Map[string, *payload.Info_Index_Count]
indexing atomic.Value // bool
minUncommitted uint32
uuidsCount uint32
uncommittedUUIDsCount uint32
}
func New(opts ...Option) (idx Indexer, err error) {
i := new(index)
for _, opt := range append(defaultOptions, opts...) {
if err := opt(i); err != nil {
return nil, errors.ErrOptionFailed(err, reflect.ValueOf(opt))
}
}
i.indexing.Store(false)
if i.indexDuration+i.indexDurationLimit+i.saveIndexDurationLimit == 0 {
return nil, errors.ErrInvalidConfig
}
return i, nil
}
func (idx *index) Start(ctx context.Context) (<-chan error, error) {
dech, err := idx.client.Start(ctx)
if err != nil {
return nil, err
}
err = idx.loadInfos(ctx)
if err != nil {
return nil, err
}
ech := make(chan error, 100)
sech := make(chan error, 10)
idx.saveIndexTargetAddrCh = make(chan string, len(idx.client.GetAddrs(ctx))*2)
idx.eg.Go(safety.RecoverFunc(func() (err error) {
defer close(ech)
if idx.indexDuration <= 0 {
idx.indexDuration = math.MaxInt64
}
if idx.indexDurationLimit <= 0 {
idx.indexDurationLimit = math.MaxInt64
}
if idx.saveIndexDurationLimit <= 0 {
idx.saveIndexDurationLimit = math.MaxInt64
}
it := time.NewTicker(idx.indexDuration)
itl := time.NewTicker(idx.indexDurationLimit)
stl := time.NewTicker(idx.saveIndexDurationLimit)
defer it.Stop()
defer itl.Stop()
defer stl.Stop()
finalize := func() (err error) {
err = ctx.Err()
if err != nil && err != context.Canceled {
return err
}
return nil
}
for {
select {
case <-ctx.Done():
return finalize()
case err = <-dech:
ech <- err
case err = <-sech:
ech <- err
case <-it.C:
err = idx.execute(grpc.WithGRPCMethod(ctx, "core.v1.Agent/CreateIndex"), true, false)
if err != nil {
ech <- err
log.Error("an error occurred during indexing", err)
err = nil
}
it.Reset(idx.indexDuration)
case <-itl.C:
err = idx.execute(grpc.WithGRPCMethod(ctx, "core.v1.Agent/CreateIndex"), false, false)
if err != nil {
ech <- err
log.Error("an error occurred during indexing", err)
err = nil
}
itl.Reset(idx.indexDurationLimit)
case <-stl.C:
err = idx.execute(grpc.WithGRPCMethod(ctx, "core.v1.Agent/CreateAndSaveIndex"), false, true)
if err != nil {
ech <- err
log.Error("an error occurred during indexing and saving", err)
err = nil
}
stl.Reset(idx.saveIndexDurationLimit)
}
if err != nil {
log.Error(err)
select {
case <-ctx.Done():
return finalize()
case ech <- err:
}
}
}
}))
idx.eg.Go(safety.RecoverFunc(func() (err error) {
defer close(sech)
for {
select {
case <-ctx.Done():
return
case addr := <-idx.saveIndexTargetAddrCh:
idx.schMap.Delete(addr)
_, err := idx.client.GetClient().
Do(grpc.WithGRPCMethod(ctx, "core.v1.Agent/SaveIndex"), addr, func(ctx context.Context, conn *grpc.ClientConn, copts ...grpc.CallOption) (interface{}, error) {
return agent.NewAgentClient(conn).SaveIndex(ctx, &payload.Empty{}, copts...)
})
if err != nil {
log.Warnf("an error occurred while calling SaveIndex of %s: %s", addr, err)
select {
case <-ctx.Done():
return nil
case sech <- err:
}
}
}
idx.waitForNextSaving(ctx)
}
}))
return ech, nil
}
func (idx *index) execute(ctx context.Context, enableLowIndexSkip, immediateSaving bool) (err error) {
ctx, span := trace.StartSpan(ctx, "vald/manager-index/service/Indexer.execute")
defer func() {
if span != nil {
span.End()
}
}()
if idx.indexing.Load().(bool) {
return nil
}
idx.indexing.Store(true)
defer idx.indexing.Store(false)
addrs := idx.client.GetAddrs(ctx)
err = idx.client.GetClient().OrderedRangeConcurrent(ctx, addrs,
idx.concurrency,
func(ctx context.Context,
addr string, conn *grpc.ClientConn, copts ...grpc.CallOption,
) (err error) {
select {
case <-ctx.Done():
return nil
default:
}
info, ok := idx.indexInfos.Load(addr)
if ok && (info.GetUncommitted() == 0 || (enableLowIndexSkip && info.GetUncommitted() < idx.minUncommitted)) {
return nil
}
ac := agent.NewAgentClient(conn)
req := &payload.Control_CreateIndexRequest{
PoolSize: idx.creationPoolSize,
}
if !immediateSaving {
_, err = ac.CreateIndex(ctx, req, copts...)
if err != nil {
st, ok := status.FromError(err)
if ok && st != nil && st.Code() == codes.FailedPrecondition {
log.Debugf("CreateIndex of %s skipped, message: %s, err: %v", addr, st.Message(), errors.Join(st.Err(), err))
return nil
}
log.Warnf("an error occurred while calling CreateIndex of %s: %s", addr, err)
return err
}
_, ok := idx.schMap.Load(addr)
if !ok {
select {
case <-ctx.Done():
case idx.saveIndexTargetAddrCh <- addr:
idx.schMap.Store(addr, struct{}{})
}
}
return nil
}
_, err = ac.CreateAndSaveIndex(ctx, req, copts...)
if err != nil {
st, ok := status.FromError(err)
if ok && st != nil && st.Code() == codes.FailedPrecondition {
log.Debugf("CreateIndex of %s skipped, message: %s, err: %v", addr, st.Message(), errors.Join(st.Err(), err))
return nil
}
log.Warnf("an error occurred while calling CreateAndSaveIndex of %s: %s", addr, err)
return err
}
idx.waitForNextSaving(ctx)
return nil
})
if err != nil {
return err
}
return idx.loadInfos(ctx)
}
func (idx *index) waitForNextSaving(ctx context.Context) {
if idx.saveIndexWaitDuration > 0 {
timer := time.NewTimer(idx.saveIndexWaitDuration)
select {
case <-ctx.Done():
case <-timer.C:
}
timer.Stop()
}
}
func (idx *index) loadInfos(ctx context.Context) (err error) {
ctx, span := trace.StartSpan(grpc.WithGRPCMethod(ctx, "core.v1.Agent/IndexInfo"), "vald/manager-index/service/Indexer.loadInfos")
defer func() {
if span != nil {
span.End()
}
}()
var u, ucu uint32
var infoMap sync.Map[string, *payload.Info_Index_Count]
err = idx.client.GetClient().RangeConcurrent(ctx, len(idx.client.GetAddrs(ctx)),
func(ctx context.Context,
addr string, conn *grpc.ClientConn, copts ...grpc.CallOption,
) (err error) {
select {
case <-ctx.Done():
return nil
default:
info, err := agent.NewAgentClient(conn).IndexInfo(ctx, new(payload.Empty), copts...)
if err != nil {
log.Warnf("an error occurred while calling IndexInfo of %s: %s", addr, err)
return nil
}
infoMap.Store(addr, info)
atomic.AddUint32(&u, info.GetStored())
atomic.AddUint32(&ucu, info.GetUncommitted())
}
return nil
})
if err != nil {
return err
}
atomic.StoreUint32(&idx.uuidsCount, atomic.LoadUint32(&u))
atomic.StoreUint32(&idx.uncommittedUUIDsCount, atomic.LoadUint32(&ucu))
idx.indexInfos.Range(func(addr string, _ *payload.Info_Index_Count) bool {
info, ok := infoMap.Load(addr)
if !ok {
idx.indexInfos.Delete(addr)
}
idx.indexInfos.Store(addr, info)
infoMap.Delete(addr)
return true
})
infoMap.Range(func(addr string, info *payload.Info_Index_Count) bool {
idx.indexInfos.Store(addr, info)
return true
})
return nil
}
func (idx *index) IsIndexing() bool {
return idx.indexing.Load().(bool)
}
func (idx *index) NumberOfUUIDs() uint32 {
return atomic.LoadUint32(&idx.uuidsCount)
}
func (idx *index) NumberOfUncommittedUUIDs() uint32 {
return atomic.LoadUint32(&idx.uncommittedUUIDsCount)
}