-
Notifications
You must be signed in to change notification settings - Fork 681
/
commit_logger.go
583 lines (481 loc) · 14.7 KB
/
commit_logger.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: hello@weaviate.io
//
package hnsw
import (
"context"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/weaviate/weaviate/adapters/repos/db/vector/compressionhelpers"
"github.com/weaviate/weaviate/adapters/repos/db/vector/hnsw/commitlog"
"github.com/weaviate/weaviate/entities/cyclemanager"
"github.com/weaviate/weaviate/entities/errorcompounder"
"github.com/weaviate/weaviate/usecases/memwatch"
)
const defaultCommitLogSize = 500 * 1024 * 1024
func commitLogFileName(rootPath, indexName, fileName string) string {
return fmt.Sprintf("%s/%s", commitLogDirectory(rootPath, indexName), fileName)
}
func commitLogDirectory(rootPath, name string) string {
return fmt.Sprintf("%s/%s.hnsw.commitlog.d", rootPath, name)
}
func NewCommitLogger(rootPath, name string, logger logrus.FieldLogger,
maintenanceCallbacks cyclemanager.CycleCallbackGroup, opts ...CommitlogOption,
) (*hnswCommitLogger, error) {
l := &hnswCommitLogger{
rootPath: rootPath,
id: name,
condensor: NewMemoryCondensor(logger),
logger: logger,
// both can be overwritten using functional options
maxSizeIndividual: defaultCommitLogSize / 5,
maxSizeCombining: defaultCommitLogSize,
}
for _, o := range opts {
if err := o(l); err != nil {
return nil, err
}
}
fd, err := getLatestCommitFileOrCreate(rootPath, name)
if err != nil {
return nil, err
}
id := func(elems ...string) string {
elems = append([]string{"commit_logger"}, elems...)
elems = append(elems, l.id)
return strings.Join(elems, "/")
}
l.commitLogger = commitlog.NewLoggerWithFile(fd)
l.switchLogsCallbackCtrl = maintenanceCallbacks.Register(id("switch_logs"), l.startSwitchLogs)
l.condenseLogsCallbackCtrl = maintenanceCallbacks.Register(id("condense_logs"), l.startCombineAndCondenseLogs)
return l, nil
}
func getLatestCommitFileOrCreate(rootPath, name string) (*os.File, error) {
dir := commitLogDirectory(rootPath, name)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return nil, errors.Wrap(err, "create commit logger directory")
}
fileName, ok, err := getCurrentCommitLogFileName(dir)
if err != nil {
return nil, errors.Wrap(err, "find commit logger file in directory")
}
if !ok {
// this is a new commit log, initialize with the current time stamp
fileName = fmt.Sprintf("%d", time.Now().Unix())
}
fd, err := os.OpenFile(commitLogFileName(rootPath, name, fileName),
os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o666)
if err != nil {
return nil, errors.Wrap(err, "create commit log file")
}
return fd, nil
}
// getCommitFileNames in order, from old to new
func getCommitFileNames(rootPath, name string) ([]string, error) {
dir := commitLogDirectory(rootPath, name)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return nil, errors.Wrap(err, "create commit logger directory")
}
files, err := os.ReadDir(dir)
if err != nil {
return nil, errors.Wrap(err, "browse commit logger directory")
}
files = removeTmpScratchOrHiddenFiles(files)
files, err = removeTmpCombiningFiles(dir, files)
if err != nil {
return nil, errors.Wrap(err, "remove temporary files")
}
if len(files) == 0 {
return nil, nil
}
ec := &errorcompounder.ErrorCompounder{}
sort.Slice(files, func(a, b int) bool {
ts1, err := asTimeStamp(files[a].Name())
if err != nil {
ec.Add(err)
}
ts2, err := asTimeStamp(files[b].Name())
if err != nil {
ec.Add(err)
}
return ts1 < ts2
})
if err := ec.ToError(); err != nil {
return nil, err
}
out := make([]string, len(files))
for i, file := range files {
out[i] = commitLogFileName(rootPath, name, file.Name())
}
return out, nil
}
// getCurrentCommitLogFileName returns the fileName and true if a file was
// present. If no file was present, the second arg is false.
func getCurrentCommitLogFileName(dirPath string) (string, bool, error) {
files, err := os.ReadDir(dirPath)
if err != nil {
return "", false, errors.Wrap(err, "browse commit logger directory")
}
if len(files) == 0 {
return "", false, nil
}
files = removeTmpScratchOrHiddenFiles(files)
files, err = removeTmpCombiningFiles(dirPath, files)
if err != nil {
return "", false, errors.Wrap(err, "clean up tmp combining files")
}
ec := &errorcompounder.ErrorCompounder{}
sort.Slice(files, func(a, b int) bool {
ts1, err := asTimeStamp(files[a].Name())
if err != nil {
ec.Add(err)
}
ts2, err := asTimeStamp(files[b].Name())
if err != nil {
ec.Add(err)
}
return ts1 > ts2
})
if err := ec.ToError(); err != nil {
return "", false, err
}
return files[0].Name(), true, nil
}
func removeTmpScratchOrHiddenFiles(in []os.DirEntry) []os.DirEntry {
out := make([]os.DirEntry, len(in))
i := 0
for _, info := range in {
if strings.HasSuffix(info.Name(), ".scratch.tmp") {
continue
}
if strings.HasPrefix(info.Name(), ".") {
continue
}
out[i] = info
i++
}
return out[:i]
}
func removeTmpCombiningFiles(dirPath string,
in []os.DirEntry,
) ([]os.DirEntry, error) {
out := make([]os.DirEntry, len(in))
i := 0
for _, info := range in {
if strings.HasSuffix(info.Name(), ".combined.tmp") {
// a temporary combining file was found which means that the combining
// process never completed, this file is thus considered corrupt (too
// short) and must be deleted. The original sources still exist (because
// the only get deleted after the .tmp file is removed), so it's safe to
// delete this without data loss.
if err := os.Remove(filepath.Join(dirPath, info.Name())); err != nil {
return out, errors.Wrap(err, "remove tmp combining file")
}
continue
}
out[i] = info
i++
}
return out[:i], nil
}
func asTimeStamp(in string) (int64, error) {
return strconv.ParseInt(strings.TrimSuffix(in, ".condensed"), 10, 64)
}
type Condensor interface {
Do(filename string) error
}
type hnswCommitLogger struct {
// protect against concurrent attempts to write in the underlying file or
// buffer
sync.Mutex
rootPath string
id string
condensor Condensor
logger logrus.FieldLogger
maxSizeIndividual int64
maxSizeCombining int64
commitLogger *commitlog.Logger
switchLogsCallbackCtrl cyclemanager.CycleCallbackCtrl
condenseLogsCallbackCtrl cyclemanager.CycleCallbackCtrl
allocChecker memwatch.AllocChecker
}
type HnswCommitType uint8 // 256 options, plenty of room for future extensions
const (
AddNode HnswCommitType = iota
SetEntryPointMaxLevel
AddLinkAtLevel
ReplaceLinksAtLevel
AddTombstone
RemoveTombstone
ClearLinks
DeleteNode
ResetIndex
ClearLinksAtLevel // added in v1.8.0-rc.1, see https://github.com/weaviate/weaviate/issues/1701
AddLinksAtLevel // added in v1.8.0-rc.1, see https://github.com/weaviate/weaviate/issues/1705
AddPQ
)
func (t HnswCommitType) String() string {
switch t {
case AddNode:
return "AddNode"
case SetEntryPointMaxLevel:
return "SetEntryPointWithMaxLayer"
case AddLinkAtLevel:
return "AddLinkAtLevel"
case AddLinksAtLevel:
return "AddLinksAtLevel"
case ReplaceLinksAtLevel:
return "ReplaceLinksAtLevel"
case AddTombstone:
return "AddTombstone"
case RemoveTombstone:
return "RemoveTombstone"
case ClearLinks:
return "ClearLinks"
case DeleteNode:
return "DeleteNode"
case ResetIndex:
return "ResetIndex"
case ClearLinksAtLevel:
return "ClearLinksAtLevel"
case AddPQ:
return "AddProductQuantizer"
}
return "unknown commit type"
}
func (l *hnswCommitLogger) ID() string {
return l.id
}
func (l *hnswCommitLogger) AddPQ(data compressionhelpers.PQData) error {
l.Lock()
defer l.Unlock()
return l.commitLogger.AddPQ(data)
}
// AddNode adds an empty node
func (l *hnswCommitLogger) AddNode(node *vertex) error {
l.Lock()
defer l.Unlock()
return l.commitLogger.AddNode(node.id, node.level)
}
func (l *hnswCommitLogger) SetEntryPointWithMaxLayer(id uint64, level int) error {
l.Lock()
defer l.Unlock()
return l.commitLogger.SetEntryPointWithMaxLayer(id, level)
}
func (l *hnswCommitLogger) ReplaceLinksAtLevel(nodeid uint64, level int, targets []uint64) error {
l.Lock()
defer l.Unlock()
return l.commitLogger.ReplaceLinksAtLevel(nodeid, level, targets)
}
func (l *hnswCommitLogger) AddLinkAtLevel(nodeid uint64, level int,
target uint64,
) error {
l.Lock()
defer l.Unlock()
return l.commitLogger.AddLinkAtLevel(nodeid, level, target)
}
func (l *hnswCommitLogger) AddTombstone(nodeid uint64) error {
l.Lock()
defer l.Unlock()
return l.commitLogger.AddTombstone(nodeid)
}
func (l *hnswCommitLogger) RemoveTombstone(nodeid uint64) error {
l.Lock()
defer l.Unlock()
return l.commitLogger.RemoveTombstone(nodeid)
}
func (l *hnswCommitLogger) ClearLinks(nodeid uint64) error {
l.Lock()
defer l.Unlock()
return l.commitLogger.ClearLinks(nodeid)
}
func (l *hnswCommitLogger) ClearLinksAtLevel(nodeid uint64, level uint16) error {
l.Lock()
defer l.Unlock()
return l.commitLogger.ClearLinksAtLevel(nodeid, level)
}
func (l *hnswCommitLogger) DeleteNode(nodeid uint64) error {
l.Lock()
defer l.Unlock()
return l.commitLogger.DeleteNode(nodeid)
}
func (l *hnswCommitLogger) Reset() error {
l.Lock()
defer l.Unlock()
return l.commitLogger.Reset()
}
// Shutdown waits for ongoing maintenance processes to stop, then cancels their
// scheduling. The caller can be sure that state on disk is immutable after
// calling Shutdown().
func (l *hnswCommitLogger) Shutdown(ctx context.Context) error {
if err := l.switchLogsCallbackCtrl.Unregister(ctx); err != nil {
return errors.Wrap(err, "failed to unregister commitlog switch from maintenance cycle")
}
if err := l.condenseLogsCallbackCtrl.Unregister(ctx); err != nil {
return errors.Wrap(err, "failed to unregister commitlog condense from maintenance cycle")
}
return nil
}
func (l *hnswCommitLogger) RootPath() string {
return l.rootPath
}
func (l *hnswCommitLogger) startSwitchLogs(shouldAbort cyclemanager.ShouldAbortCallback) bool {
executed, err := l.switchCommitLogs(false)
if err != nil {
l.logger.WithError(err).
WithField("action", "hnsw_commit_log_maintenance").
Error("hnsw commit log maintenance failed")
}
return executed
}
func (l *hnswCommitLogger) startCombineAndCondenseLogs(shouldAbort cyclemanager.ShouldAbortCallback) bool {
executed1, err := l.combineLogs()
if err != nil {
l.logger.WithError(err).
WithField("action", "hnsw_commit_log_combining").
Error("hnsw commit log maintenance (combining) failed")
}
executed2, err := l.condenseOldLogs()
if err != nil {
l.logger.WithError(err).
WithField("action", "hnsw_commit_log_condensing").
Error("hnsw commit log maintenance (condensing) failed")
}
return executed1 || executed2
}
func (l *hnswCommitLogger) SwitchCommitLogs(force bool) error {
_, err := l.switchCommitLogs(force)
return err
}
func (l *hnswCommitLogger) switchCommitLogs(force bool) (bool, error) {
l.Lock()
defer l.Unlock()
size, err := l.commitLogger.FileSize()
if err != nil {
return false, err
}
if size <= l.maxSizeIndividual && !force {
return false, nil
}
oldFileName, err := l.commitLogger.FileName()
if err != nil {
return false, err
}
if err := l.commitLogger.Close(); err != nil {
return true, err
}
// this is a new commit log, initialize with the current time stamp
fileName := fmt.Sprintf("%d", time.Now().Unix())
if force {
l.logger.WithField("action", "commit_log_file_switched").
WithField("id", l.id).
WithField("old_file_name", oldFileName).
WithField("old_file_size", size).
WithField("new_file_name", fileName).
Debug("commit log switched forced")
} else {
l.logger.WithField("action", "commit_log_file_switched").
WithField("id", l.id).
WithField("old_file_name", oldFileName).
WithField("old_file_size", size).
WithField("new_file_name", fileName).
Info("commit log size crossed threshold, switching to new file")
}
fd, err := os.OpenFile(commitLogFileName(l.rootPath, l.id, fileName),
os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o666)
if err != nil {
return true, errors.Wrap(err, "create commit log file")
}
l.commitLogger = commitlog.NewLoggerWithFile(fd)
return true, nil
}
func (l *hnswCommitLogger) condenseOldLogs() (bool, error) {
files, err := getCommitFileNames(l.rootPath, l.id)
if err != nil {
return false, err
}
if len(files) <= 1 {
// if there are no files there is nothing to do
// if there is only a single file, it must still be in use, we can't do
// anything yet
return false, nil
}
// cut off last element, as that's never a candidate
candidates := files[:len(files)-1]
for _, candidate := range candidates {
if strings.HasSuffix(candidate, ".condensed") {
// don't attempt to condense logs which are already condensed
continue
}
if l.allocChecker != nil {
// allocChecker is optional, so we can only check this if it's actually set
s, err := os.Stat(candidate)
if err != nil {
return false, fmt.Errorf("stat candidate file %q: %w", candidate, err)
}
// We're estimating here that the in-mem condensor needs about 1B of
// memory for every byte of data in the log file. This estimate can
// probably be refined.
if err := l.allocChecker.CheckAlloc(s.Size()); err != nil {
l.logger.WithFields(logrus.Fields{
"action": "hnsw_commit_log_condensing",
"event": "condensing_skipped_oom",
"path": candidate,
"size": s.Size(),
}).WithError(err).
Warnf("skipping hnsw condensing due to memory pressure")
return false, nil
}
}
return true, l.condensor.Do(candidate)
}
return false, nil
}
func (l *hnswCommitLogger) combineLogs() (bool, error) {
// maxSize is the desired final size, since we assume a lot of redundancy we
// can set the combining threshold higher than the final threshold under the
// assumption that the combined file will be considerably smaller than the
// sum of both input files
threshold := int64(float64(l.maxSizeCombining) * 1.75)
return NewCommitLogCombiner(l.rootPath, l.id, threshold, l.logger).Do()
}
func (l *hnswCommitLogger) Drop(ctx context.Context) error {
if err := l.commitLogger.Close(); err != nil {
return errors.Wrap(err, "close hnsw commit logger prior to delete")
}
// stop all goroutines
if err := l.Shutdown(ctx); err != nil {
return errors.Wrap(err, "drop commitlog")
}
// remove commit log directory if exists
dir := commitLogDirectory(l.rootPath, l.id)
if _, err := os.Stat(dir); err == nil {
err := os.RemoveAll(dir)
if err != nil {
return errors.Wrap(err, "delete commit files directory")
}
}
return nil
}
func (l *hnswCommitLogger) Flush() error {
l.Lock()
defer l.Unlock()
return l.commitLogger.Flush()
}