diff --git a/services/search/README.md b/services/search/README.md index 7d60201ebb..8b2bdecc30 100644 --- a/services/search/README.md +++ b/services/search/README.md @@ -163,7 +163,7 @@ Additionally, the following optional settings can be set: #### Files Tika Cannot Parse -A file Tika cannot parse, such as a broken PDF or an image the OCR fails on, is still indexed by name, path and tags, so it stays findable. The content is tried again on later reindexes, the first retry one hour after the failure, the second one day after that. After the third failure the service logs a warning with the path and stops trying until the file changes. If Tika itself is unreachable, nothing is written and the next reindex tries again. +A file Tika cannot parse, such as a broken PDF or an image the OCR fails on, is still indexed by name, path and tags, so it stays findable. The content is tried again on later reindexes, the first retry one hour after the failure, the second one day after that. After the third failure the service logs a warning with the path and stops trying until the file changes. If Tika or the file is unreachable, a new or changed file is indexed by name as well, this does not count as a failure and every later reindex tries again. To list the files the service gave up on, search for: diff --git a/services/search/pkg/search/search.go b/services/search/pkg/search/search.go index dcd283d542..90a9ac6d0e 100644 --- a/services/search/pkg/search/search.go +++ b/services/search/pkg/search/search.go @@ -95,9 +95,11 @@ type Resource struct { // storage: losing it with the index costs retries, never data. Two walks failing // at once may count one failure, that buys one more attempt. type ExtractionFailure struct { - // Count is the number of parse failures of the indexed version + // Count is the number of parse failures of the indexed version, 0 when only + // tika or the file was unreachable and every walk retries it Count int `json:"Count"` - // RetryAt is the earliest next attempt, nil once the attempts are used up + // RetryAt is the earliest next attempt, nil for an immediate retry or once + // the attempts are used up RetryAt *time.Time `json:"RetryAt,omitempty"` } diff --git a/services/search/pkg/search/service.go b/services/search/pkg/search/service.go index ba47f903c3..bbbabdf0c5 100644 --- a/services/search/pkg/search/service.go +++ b/services/search/pkg/search/service.go @@ -837,17 +837,12 @@ func (s *Service) upsertResource(ctx context.Context, ref *provider.Reference, s return } - // an unparsable file still yields the name, path and tags and stays findable; - // any other failure (tika or the file unreachable) writes nothing, the next - // space walk finds the entry missing or outdated and tries again + // a failed extraction still yields the name, path and tags, the resource + // stays findable and a later space walk retries it doc, err := s.extractor.Extract(ctx, stat.GetInfo()) extractionFailed := err != nil - switch { - case errors.Is(err, content.ErrUnparsable): + if err != nil { s.logger.Error().Err(err).Str("path", path).Msg("failed to extract resource content") - case err != nil: - s.logger.Error().Err(err).Str("path", path).Msg("failed to extract resource content, retrying on the next walk") - return } r := Resource{ @@ -863,7 +858,11 @@ func (s *Service) upsertResource(ctx context.Context, ref *provider.Reference, s } r.Hidden = IsHidden(r.Path) if extractionFailed { - r.ExtractionFailure = s.nextExtractionFailure(ctx, r.ID, stat.GetInfo(), path) + var write bool + r.ExtractionFailure, write = s.nextExtractionFailure(ctx, r.ID, stat.GetInfo(), path, errors.Is(err, content.ErrUnparsable)) + if !write { + return + } } if parentID := stat.GetInfo().GetParentId(); parentID != nil { @@ -964,26 +963,45 @@ func (s *Service) isIndexed(ctx context.Context, info *provider.ResourceInfo) bo } // nextExtractionFailure counts a parse failure on top of the ones the indexed -// entry holds for the same version, a new version starts over -func (s *Service) nextExtractionFailure(ctx context.Context, id string, info *provider.ResourceInfo, path string) *ExtractionFailure { - count := 1 +// entry holds for the same version, a new version starts over. Any other failure +// (tika or the file unreachable) says nothing about the file: it is not counted, +// leaves a current entry alone and is retried by every walk. write is false +// when the indexed entry has to stay as it is. +func (s *Service) nextExtractionFailure(ctx context.Context, id string, info *provider.ResourceInfo, path string, unparsable bool) (f *ExtractionFailure, write bool) { + var previous *Resource r, err := s.engine.Get(ctx, id) switch { case errors.Is(err, ErrResourceNotFound): case err != nil: - s.logger.Error().Err(err).Str("id", id).Msg("failed to look up the previous extraction failures, counting this one as the first") - case r.ExtractionFailure != nil && r.Mtime != nil && !r.Mtime.Before(utils.TSToTime(info.GetMtime())): - count = r.ExtractionFailure.Count + 1 + s.logger.Error().Err(err).Str("id", id).Msg("failed to look up the previous extraction failures") + if !unparsable { + // without knowing the entry, a name-only document could replace extracted content + return nil, false + } + case r.Mtime != nil && !r.Mtime.Before(utils.TSToTime(info.GetMtime())): + previous = r } - f := &ExtractionFailure{Count: count} + if !unparsable { + if previous != nil && !previous.Deleted { + return nil, false + } + return &ExtractionFailure{}, true + } + + count := 1 + if previous != nil && previous.ExtractionFailure != nil { + count = previous.ExtractionFailure.Count + 1 + } + + f = &ExtractionFailure{Count: count} if count > len(extractionRetryDelays) { s.logger.Warn().Str("path", path).Int("failures", count).Msg("giving up on extracting the resource content until it changes, opencloud search index --force-rescan tries again") - return f + return f, true } retryAt := time.Now().Add(extractionRetryDelays[count-1]) f.RetryAt = &retryAt - return f + return f, true } func (s *Service) resInfo(ctx context.Context, ref *provider.Reference) (*provider.StatResponse, string) { diff --git a/services/search/pkg/search/service_test.go b/services/search/pkg/search/service_test.go index 9fc87d5885..27724b5176 100644 --- a/services/search/pkg/search/service_test.go +++ b/services/search/pkg/search/service_test.go @@ -163,6 +163,7 @@ var _ = Describe("Searchprovider", func() { info("failed-waiting", sprovider.ResourceType_RESOURCE_TYPE_FILE, 4000), info("failed-given-up", sprovider.ResourceType_RESOURCE_TYPE_FILE, 4000), info("failed-given-up-changed", sprovider.ResourceType_RESOURCE_TYPE_FILE, 5000), + info("failed-unreachable", sprovider.ResourceType_RESOURCE_TYPE_FILE, 4000), } indexed := map[string]*search.Resource{ "storageid$spaceid!spaceid": {Document: content.Document{Mtime: &indexedAt}}, @@ -177,6 +178,8 @@ var _ = Describe("Searchprovider", func() { ExtractionFailure: &search.ExtractionFailure{Count: 3}}, "storageid$spaceid!failed-given-up-changed": {Document: content.Document{Mtime: &indexedAt}, ExtractionFailure: &search.ExtractionFailure{Count: 3}}, + "storageid$spaceid!failed-unreachable": {Document: content.Document{Mtime: &indexedAt}, + ExtractionFailure: &search.ExtractionFailure{}}, } gatewayClient.On("GetUserByClaim", mock.Anything, mock.Anything).Return(&userv1beta1.GetUserByClaimResponse{ @@ -221,6 +224,7 @@ var _ = Describe("Searchprovider", func() { "storageid$spaceid!trashed", "storageid$spaceid!failed-due", "storageid$spaceid!failed-given-up-changed", + "storageid$spaceid!failed-unreachable", )) }) }) @@ -309,16 +313,44 @@ var _ = Describe("Searchprovider", func() { 1, time.Hour), ) - It("writes nothing when tika or the file is unreachable", func() { - gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(&sprovider.StatResponse{ - Status: status.NewOK(context.Background()), - Info: ri, - }, nil) - extractor.On("Extract", mock.Anything, mock.Anything, mock.Anything).Return(content.Document{Name: "scan.png"}, errors.New("connection refused")) + DescribeTable("indexes a resource by name without counting when tika or the file is unreachable", + func(previous *search.Resource, getErr error, written bool) { + gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(&sprovider.StatResponse{ + Status: status.NewOK(context.Background()), + Info: ri, + }, nil) + extractor.On("Extract", mock.Anything, mock.Anything, mock.Anything).Return(content.Document{Name: "scan.png"}, errors.New("connection refused")) + indexClient.On("Get", mock.Anything, mock.Anything).Return(previous, getErr) - s.UpdateMetadata(&sprovider.Reference{ResourceId: ri.GetId()}) - indexClient.AssertNotCalled(GinkgoT(), "Upsert", mock.Anything, mock.Anything) - }) + var indexed []search.Resource + indexClient.On("Upsert", mock.Anything, mock.Anything).Run(func(args mock.Arguments) { + indexed = append(indexed, args.Get(1).(search.Resource)) + }).Return(nil) + + s.UpdateMetadata(&sprovider.Reference{ResourceId: ri.GetId()}) + if !written { + Expect(indexed).To(BeEmpty()) + return + } + Expect(indexed).To(HaveLen(1)) + Expect(indexed[0].Name).To(Equal("scan.png")) + Expect(indexed[0].ExtractionFailure).To(Equal(&search.ExtractionFailure{})) + }, + Entry("a new resource", nil, search.ErrResourceNotFound, true), + Entry("an outdated entry", + &search.Resource{Document: content.Document{Mtime: ptr(time.Unix(3000, 0)), Content: "old"}}, + nil, true), + Entry("an outdated entry with parse failures starts over", + &search.Resource{Document: content.Document{Mtime: ptr(time.Unix(3000, 0))}, ExtractionFailure: &search.ExtractionFailure{Count: 2}}, + nil, true), + Entry("a current entry keeps its content", + &search.Resource{Document: content.Document{Mtime: ptr(time.Unix(4000, 0)), Content: "extracted"}}, + nil, false), + Entry("a current entry keeps its parse failures", + &search.Resource{Document: content.Document{Mtime: ptr(time.Unix(4000, 0))}, ExtractionFailure: &search.ExtractionFailure{Count: 3}}, + nil, false), + Entry("an unknown entry is left alone", nil, errors.New("index unavailable"), false), + ) Context("with facets stored on the resource", func() { var (