From 2cc1640085746c7cbbe5ec5f53b736e4e69d5808 Mon Sep 17 00:00:00 2001 From: Paul Faure Date: Mon, 16 Mar 2026 00:23:36 -0400 Subject: [PATCH 1/4] fix(search): always descend into directories during IndexSpace walk The SkipDir optimization skipped entire subtrees when a directory was already indexed with a matching mtime. After a failed indexing run (e.g. Tika crash), this permanently prevented unindexed children from being visited since the parent directory's mtime doesn't change when child extraction fails. With O(1) DocID lookups the per-file skip check is negligible, making SkipDir unnecessary for correctness. Co-Authored-By: Claude Opus 4.6 --- changelog/unreleased/fix-search-skipdir-reindex.md | 11 +++++++++++ services/search/pkg/search/service.go | 7 +++++-- 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 changelog/unreleased/fix-search-skipdir-reindex.md diff --git a/changelog/unreleased/fix-search-skipdir-reindex.md b/changelog/unreleased/fix-search-skipdir-reindex.md new file mode 100644 index 00000000000..a1d0fde76d9 --- /dev/null +++ b/changelog/unreleased/fix-search-skipdir-reindex.md @@ -0,0 +1,11 @@ +Bugfix: Always descend into directories during space reindexing + +The search indexer's `IndexSpace` walk previously used `filepath.SkipDir` +to skip entire directory subtrees when the directory itself was already +indexed. After a failed or interrupted indexing run (e.g. Tika crash), +this caused thousands of unindexed files to be permanently skipped +because the parent directory's mtime had not changed. The indexer now +always descends into directories, relying on the O(1) per-file DocID +lookup to skip already-indexed files efficiently. + +https://github.com/owncloud/ocis/pull/XXXX diff --git a/services/search/pkg/search/service.go b/services/search/pkg/search/service.go index a4692c33e11..0161dce1392 100644 --- a/services/search/pkg/search/service.go +++ b/services/search/pkg/search/service.go @@ -469,8 +469,11 @@ func (s *Service) IndexSpace(spaceID *provider.StorageSpaceId) error { docMtime, parseErr := time.Parse(time.RFC3339Nano, r.Mtime) if parseErr == nil && !docMtime.Before(fileMtime) { if info.Type == provider.ResourceType_RESOURCE_TYPE_CONTAINER { - s.logger.Debug().Str("path", ref.Path).Msg("subtree hasn't changed. Skipping.") - return filepath.SkipDir + // Always descend into directories — a directory's + // mtime only reflects direct child add/remove, not + // whether all children were successfully indexed. + // With O(1) DocID lookups the cost is negligible. + return nil } s.logger.Debug().Str("path", ref.Path).Msg("element hasn't changed. Skipping.") return nil From 0e5233dd507763cd83ac467aad6461dd2db54fff Mon Sep 17 00:00:00 2001 From: Paul Faure Date: Mon, 16 Mar 2026 00:28:18 -0400 Subject: [PATCH 2/4] docs: update changelog with PR number Co-Authored-By: Claude Opus 4.6 --- changelog/unreleased/fix-search-skipdir-reindex.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/fix-search-skipdir-reindex.md b/changelog/unreleased/fix-search-skipdir-reindex.md index a1d0fde76d9..4a03447c592 100644 --- a/changelog/unreleased/fix-search-skipdir-reindex.md +++ b/changelog/unreleased/fix-search-skipdir-reindex.md @@ -8,4 +8,4 @@ because the parent directory's mtime had not changed. The indexer now always descends into directories, relying on the O(1) per-file DocID lookup to skip already-indexed files efficiently. -https://github.com/owncloud/ocis/pull/XXXX +https://github.com/owncloud/ocis/pull/12119 From 9bcf14a8970c42f36ccb68bf7367f42849f77f44 Mon Sep 17 00:00:00 2001 From: Paul Faure Date: Mon, 16 Mar 2026 11:07:04 -0400 Subject: [PATCH 3/4] test(search): add unit test for descending into already-indexed directories Verifies that IndexSpace visits children inside 100 already-indexed directories whose mtimes match the index. Before the SkipDir fix these subtrees would be permanently skipped after an interrupted reindex run. Co-Authored-By: Claude Opus 4.6 --- services/search/pkg/search/service_test.go | 109 +++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/services/search/pkg/search/service_test.go b/services/search/pkg/search/service_test.go index 5f46f8e9f72..485ee2caecc 100644 --- a/services/search/pkg/search/service_test.go +++ b/services/search/pkg/search/service_test.go @@ -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,112 @@ 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. + const numDirs = 100 + + 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}, + } + } + + // Stat on root returns the root container. + gatewayClient.On("Stat", mock.Anything, mock.MatchedBy(func(sreq *sprovider.StatRequest) bool { + return sreq.Ref.ResourceId.StorageId == "storageid" && + sreq.Ref.ResourceId.SpaceId == "spaceid" && + sreq.Ref.ResourceId.OpaqueId == "spaceid" + })).Return(&sprovider.StatResponse{ + Status: status.NewOK(context.Background()), + Info: rootInfo, + }, nil) + + // ListContainer on root returns the directories. + gatewayClient.On("ListContainer", mock.Anything, mock.MatchedBy(func(req *sprovider.ListContainerRequest) bool { + return req.Ref.ResourceId.OpaqueId == "spaceid" + })).Return(&sprovider.ListContainerResponse{ + Status: status.NewOK(context.Background()), + Infos: dirInfos, + }, nil) + + // Each directory's ListContainer returns one file. + for i := 0; i < numDirs; i++ { + dirID := fmt.Sprintf("dir-%d", i) + fileInfo := &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}, + } + gatewayClient.On("ListContainer", mock.Anything, mock.MatchedBy(func(req *sprovider.ListContainerRequest) bool { + return req.Ref.ResourceId.OpaqueId == dirID + })).Return(&sprovider.ListContainerResponse{ + Status: status.NewOK(context.Background()), + Infos: []*sprovider.ResourceInfo{fileInfo}, + }, nil) + + // Stat calls from UpsertItem for each file. + gatewayClient.On("Stat", mock.Anything, mock.MatchedBy(func(sreq *sprovider.StatRequest) bool { + return sreq.Ref.Path == fmt.Sprintf("./dir-%d/file-%d.pdf", i, i) + })).Return(&sprovider.StatResponse{ + Status: status.NewOK(context.Background()), + Info: fileInfo, + }, nil) + } + + // Lookup: root and directories are already indexed with matching mtime. + indexClient.On("Lookup", mock.MatchedBy(func(id string) bool { + return id == "storageid$spaceid!spaceid" || len(id) > 18 // dir-N IDs + })).Return(func(id string) (*engine.Resource, error) { + // Check if this is a directory lookup. + for i := 0; i < numDirs; i++ { + if id == fmt.Sprintf("storageid$spaceid!dir-%d", i) { + return &engine.Resource{Document: content.Document{Mtime: indexedMtime}, Extracted: true}, nil + } + } + if id == "storageid$spaceid!spaceid" { + return &engine.Resource{Document: content.Document{Mtime: indexedMtime}, Extracted: true}, 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") + }) }) Describe("UpdateTags", func() { From 5ccb09f4d27d9640913f2197e384eaf9476ddab5 Mon Sep 17 00:00:00 2001 From: Paul Faure Date: Tue, 17 Mar 2026 16:38:22 -0400 Subject: [PATCH 4/4] test(search): scale SkipDir test to 10,000 directories for pessimistic-case benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Increases the "descends into already-indexed directories" test from 100 to 10,000 directories. Restructures mocks to use map-based dispatch instead of per-directory mock.MatchedBy registrations, keeping the test at O(n) instead of O(n²). Result: 10,000 already-indexed dirs with 10,000 unindexed child files completes in ~14 seconds, confirming linear scaling. Co-Authored-By: Claude Opus 4.6 --- services/search/pkg/search/service_test.go | 119 +++++++++++++-------- 1 file changed, 73 insertions(+), 46 deletions(-) diff --git a/services/search/pkg/search/service_test.go b/services/search/pkg/search/service_test.go index 485ee2caecc..5e0c24fcd51 100644 --- a/services/search/pkg/search/service_test.go +++ b/services/search/pkg/search/service_test.go @@ -209,7 +209,10 @@ var _ = Describe("Searchprovider", func() { // (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. - const numDirs = 100 + // + // 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()), @@ -237,28 +240,11 @@ var _ = Describe("Searchprovider", func() { } } - // Stat on root returns the root container. - gatewayClient.On("Stat", mock.Anything, mock.MatchedBy(func(sreq *sprovider.StatRequest) bool { - return sreq.Ref.ResourceId.StorageId == "storageid" && - sreq.Ref.ResourceId.SpaceId == "spaceid" && - sreq.Ref.ResourceId.OpaqueId == "spaceid" - })).Return(&sprovider.StatResponse{ - Status: status.NewOK(context.Background()), - Info: rootInfo, - }, nil) - - // ListContainer on root returns the directories. - gatewayClient.On("ListContainer", mock.Anything, mock.MatchedBy(func(req *sprovider.ListContainerRequest) bool { - return req.Ref.ResourceId.OpaqueId == "spaceid" - })).Return(&sprovider.ListContainerResponse{ - Status: status.NewOK(context.Background()), - Infos: dirInfos, - }, nil) - - // Each directory's ListContainer returns one file. + // 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) - fileInfo := &sprovider.ResourceInfo{ + 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, @@ -266,34 +252,75 @@ var _ = Describe("Searchprovider", func() { Size: 1024, Mtime: &typesv1beta1.Timestamp{Seconds: 5000}, } - gatewayClient.On("ListContainer", mock.Anything, mock.MatchedBy(func(req *sprovider.ListContainerRequest) bool { - return req.Ref.ResourceId.OpaqueId == dirID - })).Return(&sprovider.ListContainerResponse{ - Status: status.NewOK(context.Background()), - Infos: []*sprovider.ResourceInfo{fileInfo}, - }, nil) - - // Stat calls from UpsertItem for each file. - gatewayClient.On("Stat", mock.Anything, mock.MatchedBy(func(sreq *sprovider.StatRequest) bool { - return sreq.Ref.Path == fmt.Sprintf("./dir-%d/file-%d.pdf", i, i) - })).Return(&sprovider.StatResponse{ - Status: status.NewOK(context.Background()), - Info: fileInfo, - }, nil) } - // Lookup: root and directories are already indexed with matching mtime. - indexClient.On("Lookup", mock.MatchedBy(func(id string) bool { - return id == "storageid$spaceid!spaceid" || len(id) > 18 // dir-N IDs - })).Return(func(id string) (*engine.Resource, error) { - // Check if this is a directory lookup. - for i := 0; i < numDirs; i++ { - if id == fmt.Sprintf("storageid$spaceid!dir-%d", i) { - return &engine.Resource{Document: content.Document{Mtime: indexedMtime}, Extracted: true}, nil + // 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 id == "storageid$spaceid!spaceid" { - return &engine.Resource{Document: content.Document{Mtime: indexedMtime}, Extracted: true}, nil + 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