github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/vector/hnsw/backup_test.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package hnsw
    13  
    14  import (
    15  	"context"
    16  	"fmt"
    17  	"os"
    18  	"path"
    19  	"regexp"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/sirupsen/logrus"
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  	"github.com/weaviate/weaviate/adapters/repos/db/vector/hnsw/distancer"
    27  	"github.com/weaviate/weaviate/entities/cyclemanager"
    28  	enthnsw "github.com/weaviate/weaviate/entities/vectorindex/hnsw"
    29  )
    30  
    31  func TestBackup_SwitchCommitLogs(t *testing.T) {
    32  	ctx := context.Background()
    33  
    34  	dirName := t.TempDir()
    35  	indexID := "backup-switch-commitlogs-test"
    36  
    37  	idx, err := New(Config{
    38  		RootPath:         dirName,
    39  		ID:               indexID,
    40  		Logger:           logrus.New(),
    41  		DistanceProvider: distancer.NewCosineDistanceProvider(),
    42  		VectorForIDThunk: testVectorForID,
    43  		MakeCommitLoggerThunk: func() (CommitLogger, error) {
    44  			return NewCommitLogger(dirName, indexID, logrus.New(), cyclemanager.NewCallbackGroupNoop())
    45  		},
    46  	}, enthnsw.NewDefaultUserConfig(), cyclemanager.NewCallbackGroupNoop(), cyclemanager.NewCallbackGroupNoop(), cyclemanager.NewCallbackGroupNoop(), nil)
    47  	require.Nil(t, err)
    48  	idx.PostStartup()
    49  
    50  	ctx, cancel := context.WithTimeout(ctx, time.Second)
    51  	defer cancel()
    52  
    53  	err = idx.SwitchCommitLogs(ctx)
    54  	assert.Nil(t, err)
    55  
    56  	err = idx.Shutdown(ctx)
    57  	require.Nil(t, err)
    58  }
    59  
    60  func TestBackup_ListFiles(t *testing.T) {
    61  	ctx := context.Background()
    62  
    63  	dirName := t.TempDir()
    64  	indexID := "backup-list-files-test"
    65  
    66  	idx, err := New(Config{
    67  		RootPath:         dirName,
    68  		ID:               indexID,
    69  		Logger:           logrus.New(),
    70  		DistanceProvider: distancer.NewCosineDistanceProvider(),
    71  		VectorForIDThunk: testVectorForID,
    72  		MakeCommitLoggerThunk: func() (CommitLogger, error) {
    73  			return NewCommitLogger(dirName, indexID, logrus.New(), cyclemanager.NewCallbackGroupNoop())
    74  		},
    75  	}, enthnsw.NewDefaultUserConfig(), cyclemanager.NewCallbackGroupNoop(), cyclemanager.NewCallbackGroupNoop(), cyclemanager.NewCallbackGroupNoop(), nil)
    76  	require.Nil(t, err)
    77  	idx.PostStartup()
    78  
    79  	t.Run("assert expected index contents", func(t *testing.T) {
    80  		files, err := idx.ListFiles(ctx, dirName)
    81  		assert.Nil(t, err)
    82  
    83  		// should return empty, because the only file which
    84  		// exists in the commitlog root is the current active
    85  		// log file.
    86  		assert.Len(t, files, 0)
    87  
    88  		// checking to ensure that the commitlog root does
    89  		// contain a file. this is the one that was ignored
    90  		// in the check above.
    91  		ls, err := os.ReadDir(path.Join(dirName, fmt.Sprintf("%s.hnsw.commitlog.d", indexID)))
    92  		require.Nil(t, err)
    93  		require.Len(t, ls, 1)
    94  		// filename should just be a 10 digit int
    95  		matched, err := regexp.MatchString("[0-9]{10}", ls[0].Name())
    96  		assert.Nil(t, err)
    97  		assert.True(t, matched, "regex does not match")
    98  	})
    99  
   100  	err = idx.Shutdown(ctx)
   101  	require.Nil(t, err)
   102  }