|
|
|
@@ -3,6 +3,9 @@ package search_test
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
|
|
|
|
|
userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
|
|
|
|
@@ -200,6 +203,164 @@ var _ = Describe("Searchprovider", func() {
|
|
|
|
|
err := s.IndexSpace(&sprovider.StorageSpaceId{OpaqueId: "storageid$spaceid!spaceid"})
|
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("descends into already-indexed directories to index unvisited children", func() {
|
|
|
|
|
// Simulate a large tree where all directories are already indexed
|
|
|
|
|
// (mtime matches) but each contains one unindexed file — e.g. after
|
|
|
|
|
// a Tika crash interrupted a previous reindex run. The fix ensures
|
|
|
|
|
// we never SkipDir for directories, so children are always visited.
|
|
|
|
|
//
|
|
|
|
|
// 10,000 directories: pessimistic-case benchmark confirming the walker
|
|
|
|
|
// scales linearly even when every directory is already indexed.
|
|
|
|
|
const numDirs = 10000
|
|
|
|
|
|
|
|
|
|
gatewayClient.On("GetUserByClaim", mock.Anything, mock.Anything).Return(&userv1beta1.GetUserByClaimResponse{
|
|
|
|
|
Status: status.NewOK(context.Background()),
|
|
|
|
|
User: user,
|
|
|
|
|
}, nil)
|
|
|
|
|
extractor.On("Extract", mock.Anything, mock.Anything, mock.Anything).Return(content.Document{}, nil)
|
|
|
|
|
|
|
|
|
|
// The indexed mtime that the engine returns for directories.
|
|
|
|
|
indexedMtime := time.Unix(5000, 0).UTC().Format(time.RFC3339Nano)
|
|
|
|
|
|
|
|
|
|
// Build root: a container that lists numDirs child directories.
|
|
|
|
|
rootInfo := &sprovider.ResourceInfo{
|
|
|
|
|
Id: &sprovider.ResourceId{StorageId: "storageid", SpaceId: "spaceid", OpaqueId: "spaceid"},
|
|
|
|
|
Type: sprovider.ResourceType_RESOURCE_TYPE_CONTAINER,
|
|
|
|
|
Path: ".",
|
|
|
|
|
Mtime: &typesv1beta1.Timestamp{Seconds: 5000},
|
|
|
|
|
}
|
|
|
|
|
dirInfos := make([]*sprovider.ResourceInfo, numDirs)
|
|
|
|
|
for i := 0; i < numDirs; i++ {
|
|
|
|
|
dirInfos[i] = &sprovider.ResourceInfo{
|
|
|
|
|
Id: &sprovider.ResourceId{StorageId: "storageid", SpaceId: "spaceid", OpaqueId: fmt.Sprintf("dir-%d", i)},
|
|
|
|
|
Type: sprovider.ResourceType_RESOURCE_TYPE_CONTAINER,
|
|
|
|
|
Path: fmt.Sprintf("dir-%d", i),
|
|
|
|
|
Mtime: &typesv1beta1.Timestamp{Seconds: 5000},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pre-build file infos for each directory (map for O(1) dispatch).
|
|
|
|
|
fileInfoByDir := make(map[string]*sprovider.ResourceInfo, numDirs)
|
|
|
|
|
for i := 0; i < numDirs; i++ {
|
|
|
|
|
dirID := fmt.Sprintf("dir-%d", i)
|
|
|
|
|
fileInfoByDir[dirID] = &sprovider.ResourceInfo{
|
|
|
|
|
Id: &sprovider.ResourceId{StorageId: "storageid", SpaceId: "spaceid", OpaqueId: fmt.Sprintf("file-%d", i)},
|
|
|
|
|
ParentId: &sprovider.ResourceId{StorageId: "storageid", SpaceId: "spaceid", OpaqueId: dirID},
|
|
|
|
|
Type: sprovider.ResourceType_RESOURCE_TYPE_FILE,
|
|
|
|
|
Path: fmt.Sprintf("file-%d.pdf", i),
|
|
|
|
|
Size: 1024,
|
|
|
|
|
Mtime: &typesv1beta1.Timestamp{Seconds: 5000},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build path-to-fileInfo map for O(1) Stat dispatch.
|
|
|
|
|
fileInfoByPath := make(map[string]*sprovider.ResourceInfo, numDirs)
|
|
|
|
|
for dirID, fi := range fileInfoByDir {
|
|
|
|
|
fileInfoByPath["./"+dirID+"/"+fi.Path] = fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Single Stat mock dispatching via map — avoids 10k separate matchers.
|
|
|
|
|
gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(
|
|
|
|
|
func(_ context.Context, sreq *sprovider.StatRequest, _ ...grpc.CallOption) *sprovider.StatResponse {
|
|
|
|
|
if sreq.Ref.ResourceId != nil && sreq.Ref.ResourceId.OpaqueId == "spaceid" {
|
|
|
|
|
return &sprovider.StatResponse{
|
|
|
|
|
Status: status.NewOK(context.Background()),
|
|
|
|
|
Info: rootInfo,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if fi, ok := fileInfoByPath[sreq.Ref.Path]; ok {
|
|
|
|
|
return &sprovider.StatResponse{
|
|
|
|
|
Status: status.NewOK(context.Background()),
|
|
|
|
|
Info: fi,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return &sprovider.StatResponse{Status: status.NewOK(context.Background()), Info: rootInfo}
|
|
|
|
|
},
|
|
|
|
|
func(_ context.Context, _ *sprovider.StatRequest, _ ...grpc.CallOption) error {
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Single ListContainer mock dispatching via map.
|
|
|
|
|
gatewayClient.On("ListContainer", mock.Anything, mock.Anything).Return(
|
|
|
|
|
func(_ context.Context, req *sprovider.ListContainerRequest, _ ...grpc.CallOption) *sprovider.ListContainerResponse {
|
|
|
|
|
opaqueID := req.Ref.ResourceId.OpaqueId
|
|
|
|
|
if opaqueID == "spaceid" {
|
|
|
|
|
return &sprovider.ListContainerResponse{
|
|
|
|
|
Status: status.NewOK(context.Background()),
|
|
|
|
|
Infos: dirInfos,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if fi, ok := fileInfoByDir[opaqueID]; ok {
|
|
|
|
|
return &sprovider.ListContainerResponse{
|
|
|
|
|
Status: status.NewOK(context.Background()),
|
|
|
|
|
Infos: []*sprovider.ResourceInfo{fi},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return &sprovider.ListContainerResponse{
|
|
|
|
|
Status: status.NewOK(context.Background()),
|
|
|
|
|
Infos: nil,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
func(_ context.Context, _ *sprovider.ListContainerRequest, _ ...grpc.CallOption) error {
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Lookup: root and directories are already indexed with matching mtime.
|
|
|
|
|
// Use a map for O(1) lookup instead of linear scan (matters at 10k dirs).
|
|
|
|
|
indexedIDs := make(map[string]bool, numDirs+1)
|
|
|
|
|
indexedIDs["storageid$spaceid!spaceid"] = true
|
|
|
|
|
for i := 0; i < numDirs; i++ {
|
|
|
|
|
indexedIDs[fmt.Sprintf("storageid$spaceid!dir-%d", i)] = true
|
|
|
|
|
}
|
|
|
|
|
indexedResource := &engine.Resource{Document: content.Document{Mtime: indexedMtime}, Extracted: true}
|
|
|
|
|
indexClient.On("Lookup", mock.MatchedBy(func(id string) bool {
|
|
|
|
|
return indexedIDs[id] || len(id) > 18 // dir-N or file-N IDs
|
|
|
|
|
})).Return(func(id string) (*engine.Resource, error) {
|
|
|
|
|
if indexedIDs[id] {
|
|
|
|
|
return indexedResource, nil
|
|
|
|
|
}
|
|
|
|
|
// Files are not indexed.
|
|
|
|
|
return nil, engine.ErrResourceNotFound
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Track Upsert calls — each unindexed file should trigger one.
|
|
|
|
|
var upsertCount atomic.Int32
|
|
|
|
|
indexClient.On("Upsert", mock.Anything, mock.Anything).Run(func(_ mock.Arguments) {
|
|
|
|
|
upsertCount.Add(1)
|
|
|
|
|
}).Return(nil)
|
|
|
|
|
|
|
|
|
|
err := s.IndexSpace(&sprovider.StorageSpaceId{OpaqueId: "storageid$spaceid!spaceid"})
|
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
|
Expect(int(upsertCount.Load())).To(Equal(numDirs), "every unindexed file inside an already-indexed directory must be visited")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("scales linearly when a single directory contains many unindexed files", func() {
|
|
|
|
|
// Pessimistic case for a heavy user: one directory holding N files,
|
|
|
|
|
// the directory itself already indexed. Runs at three scales and
|
|
|
|
|
// prints wall-clock durations so the reviewer can eyeball linearity.
|
|
|
|
|
scales := []int{100, 1000, 10000}
|
|
|
|
|
|
|
|
|
|
type result struct {
|
|
|
|
|
numFiles int
|
|
|
|
|
duration time.Duration
|
|
|
|
|
}
|
|
|
|
|
results := make([]result, 0, len(scales))
|
|
|
|
|
|
|
|
|
|
for _, numFiles := range scales {
|
|
|
|
|
upserts, dur, err := runSingleDirScaleTest(numFiles)
|
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
|
Expect(upserts).To(Equal(numFiles), fmt.Sprintf("all %d unindexed files must be visited", numFiles))
|
|
|
|
|
results = append(results, result{numFiles: numFiles, duration: dur})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Fprintf(GinkgoWriter, "\nScale benchmark — single dir with N unindexed files:\n")
|
|
|
|
|
for _, r := range results {
|
|
|
|
|
fmt.Fprintf(GinkgoWriter, " %6d files → %s\n", r.numFiles, r.duration)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Describe("UpdateTags", func() {
|
|
|
|
@@ -602,6 +763,129 @@ var _ = Describe("Searchprovider", func() {
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// runSingleDirScaleTest creates a fresh service with mocks and runs IndexSpace
|
|
|
|
|
// against a tree: root → big-dir → N files. Root and big-dir are already
|
|
|
|
|
// indexed (mtime matches); files are not. Returns the number of Upsert calls,
|
|
|
|
|
// the wall-clock duration of IndexSpace, and any error.
|
|
|
|
|
func runSingleDirScaleTest(numFiles int) (int, time.Duration, error) {
|
|
|
|
|
logger := log.NewLogger()
|
|
|
|
|
user := &userv1beta1.User{Id: &userv1beta1.UserId{OpaqueId: "user"}}
|
|
|
|
|
|
|
|
|
|
selectorKey := fmt.Sprintf("ScaleBench-%d", numFiles)
|
|
|
|
|
pool.RemoveSelector(selectorKey + "com.owncloud.api.gateway")
|
|
|
|
|
gw := &cs3mocks.GatewayAPIClient{}
|
|
|
|
|
gwSelector := pool.GetSelector[gateway.GatewayAPIClient](
|
|
|
|
|
selectorKey,
|
|
|
|
|
"com.owncloud.api.gateway",
|
|
|
|
|
func(cc grpc.ClientConnInterface) gateway.GatewayAPIClient {
|
|
|
|
|
return gw
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
idx := &engineMocks.Engine{}
|
|
|
|
|
ext := &contentMocks.Extractor{}
|
|
|
|
|
svc := search.NewService(gwSelector, idx, ext, logger, &config.Config{})
|
|
|
|
|
|
|
|
|
|
gw.On("Authenticate", mock.Anything, mock.Anything).Return(&gateway.AuthenticateResponse{
|
|
|
|
|
Status: status.NewOK(context.Background()),
|
|
|
|
|
Token: "authtoken",
|
|
|
|
|
}, nil)
|
|
|
|
|
gw.On("GetUserByClaim", mock.Anything, mock.Anything).Return(&userv1beta1.GetUserByClaimResponse{
|
|
|
|
|
Status: status.NewOK(context.Background()),
|
|
|
|
|
User: user,
|
|
|
|
|
}, nil)
|
|
|
|
|
ext.On("Extract", mock.Anything, mock.Anything, mock.Anything).Return(content.Document{}, nil)
|
|
|
|
|
idx.On("DocCount").Return(uint64(1), nil)
|
|
|
|
|
|
|
|
|
|
indexedMtime := time.Unix(5000, 0).UTC().Format(time.RFC3339Nano)
|
|
|
|
|
|
|
|
|
|
rootInfo := &sprovider.ResourceInfo{
|
|
|
|
|
Id: &sprovider.ResourceId{StorageId: "storageid", SpaceId: "spaceid", OpaqueId: "spaceid"},
|
|
|
|
|
Type: sprovider.ResourceType_RESOURCE_TYPE_CONTAINER,
|
|
|
|
|
Path: ".",
|
|
|
|
|
Mtime: &typesv1beta1.Timestamp{Seconds: 5000},
|
|
|
|
|
}
|
|
|
|
|
bigDirInfo := &sprovider.ResourceInfo{
|
|
|
|
|
Id: &sprovider.ResourceId{StorageId: "storageid", SpaceId: "spaceid", OpaqueId: "big-dir"},
|
|
|
|
|
Type: sprovider.ResourceType_RESOURCE_TYPE_CONTAINER,
|
|
|
|
|
Path: "big-dir",
|
|
|
|
|
Mtime: &typesv1beta1.Timestamp{Seconds: 5000},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileInfos := make([]*sprovider.ResourceInfo, numFiles)
|
|
|
|
|
fileInfoByPath := make(map[string]*sprovider.ResourceInfo, numFiles)
|
|
|
|
|
for i := range numFiles {
|
|
|
|
|
fi := &sprovider.ResourceInfo{
|
|
|
|
|
Id: &sprovider.ResourceId{StorageId: "storageid", SpaceId: "spaceid", OpaqueId: fmt.Sprintf("file-%d", i)},
|
|
|
|
|
ParentId: &sprovider.ResourceId{StorageId: "storageid", SpaceId: "spaceid", OpaqueId: "big-dir"},
|
|
|
|
|
Type: sprovider.ResourceType_RESOURCE_TYPE_FILE,
|
|
|
|
|
Path: fmt.Sprintf("file-%d.pdf", i),
|
|
|
|
|
Size: 1024,
|
|
|
|
|
Mtime: &typesv1beta1.Timestamp{Seconds: 5000},
|
|
|
|
|
}
|
|
|
|
|
fileInfos[i] = fi
|
|
|
|
|
fileInfoByPath["./big-dir/"+fi.Path] = fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gw.On("Stat", mock.Anything, mock.Anything).Return(
|
|
|
|
|
func(_ context.Context, sreq *sprovider.StatRequest, _ ...grpc.CallOption) *sprovider.StatResponse {
|
|
|
|
|
if sreq.Ref.ResourceId != nil && sreq.Ref.ResourceId.OpaqueId == "spaceid" {
|
|
|
|
|
return &sprovider.StatResponse{Status: status.NewOK(context.Background()), Info: rootInfo}
|
|
|
|
|
}
|
|
|
|
|
if fi, ok := fileInfoByPath[sreq.Ref.Path]; ok {
|
|
|
|
|
return &sprovider.StatResponse{Status: status.NewOK(context.Background()), Info: fi}
|
|
|
|
|
}
|
|
|
|
|
return &sprovider.StatResponse{Status: status.NewOK(context.Background()), Info: rootInfo}
|
|
|
|
|
},
|
|
|
|
|
func(_ context.Context, _ *sprovider.StatRequest, _ ...grpc.CallOption) error { return nil },
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
gw.On("ListContainer", mock.Anything, mock.Anything).Return(
|
|
|
|
|
func(_ context.Context, req *sprovider.ListContainerRequest, _ ...grpc.CallOption) *sprovider.ListContainerResponse {
|
|
|
|
|
switch req.Ref.ResourceId.OpaqueId {
|
|
|
|
|
case "spaceid":
|
|
|
|
|
return &sprovider.ListContainerResponse{
|
|
|
|
|
Status: status.NewOK(context.Background()),
|
|
|
|
|
Infos: []*sprovider.ResourceInfo{bigDirInfo},
|
|
|
|
|
}
|
|
|
|
|
case "big-dir":
|
|
|
|
|
return &sprovider.ListContainerResponse{
|
|
|
|
|
Status: status.NewOK(context.Background()),
|
|
|
|
|
Infos: fileInfos,
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return &sprovider.ListContainerResponse{
|
|
|
|
|
Status: status.NewOK(context.Background()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
func(_ context.Context, _ *sprovider.ListContainerRequest, _ ...grpc.CallOption) error { return nil },
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
indexedIDs := map[string]bool{
|
|
|
|
|
"storageid$spaceid!spaceid": true,
|
|
|
|
|
"storageid$spaceid!big-dir": true,
|
|
|
|
|
}
|
|
|
|
|
indexedResource := &engine.Resource{Document: content.Document{Mtime: indexedMtime}, Extracted: true}
|
|
|
|
|
idx.On("Lookup", mock.Anything).Return(func(id string) (*engine.Resource, error) {
|
|
|
|
|
if indexedIDs[id] {
|
|
|
|
|
return indexedResource, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, engine.ErrResourceNotFound
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
var upsertCount atomic.Int32
|
|
|
|
|
idx.On("Upsert", mock.Anything, mock.Anything).Run(func(_ mock.Arguments) {
|
|
|
|
|
upsertCount.Add(1)
|
|
|
|
|
}).Return(nil)
|
|
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
err := svc.IndexSpace(&sprovider.StorageSpaceId{OpaqueId: "storageid$spaceid!spaceid"})
|
|
|
|
|
dur := time.Since(start)
|
|
|
|
|
|
|
|
|
|
return int(upsertCount.Load()), dur, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ = DescribeTable("Parse Scope",
|
|
|
|
|
func(pattern, wantSearch, wantScope string) {
|
|
|
|
|
gotSearch, gotScope := search.ParseScope(pattern)
|
|
|
|
|