From 960ff63db3c42837659bf42607d41fd07a99bcf5 Mon Sep 17 00:00:00 2001 From: Florian Schade Date: Wed, 25 Nov 2026 13:19:42 +0100 Subject: [PATCH] fix(search): count extraction timeouts against the file, retry on 429 --- services/search/README.md | 4 ++-- services/search/pkg/config/content.go | 2 +- .../pkg/config/defaults/defaultconfig.go | 6 +++-- services/search/pkg/content/cs3.go | 2 +- services/search/pkg/content/cs3_test.go | 1 + services/search/pkg/content/extractor.go | 12 +++++++--- services/search/pkg/content/tika.go | 23 ++++++++++++++----- services/search/pkg/content/tika_test.go | 5 ++-- 8 files changed, 38 insertions(+), 17 deletions(-) diff --git a/services/search/README.md b/services/search/README.md index 0651a5fc28..f071445dfc 100644 --- a/services/search/README.md +++ b/services/search/README.md @@ -160,11 +160,11 @@ The following settings must be set: Additionally, the following optional settings can be set: * `SEARCH_EXTRACTOR_TIKA_CLEAN_STOP_WORDS=true` (default: `true`): ignore stop words like `I`, `you`, `the` during content extraction. -* `SEARCH_EXTRACTOR_TIKA_TIMEOUT=5m` (default: `5m`): the time one extraction may take, from downloading the file to the answer of Tika. A longer one counts as Tika being unreachable, see below. Raise it if OCR of large files takes longer, `0` disables the limit. +* `SEARCH_EXTRACTOR_TIKA_TIMEOUT=5m` (default: `5m`): the time one extraction may take, from downloading the file to the answer of Tika. A longer one counts as a failure of the file, see below. Raise it if OCR of large files takes longer, `0` disables the limit. #### 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 same goes for a file the storage refuses to hand out, for example one deleted meanwhile. The content is tried again on the first reindex of its space at least one hour after the failure, and on the first one at least 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 failed connection, a timeout or a server error), a space reindex retries the extraction up to five times, one second apart, so a restarting Tika does not leave gaps. A file still unreachable after that is indexed by name as well, this does not count as a failure and every later reindex tries again. After five such files in a row the reindex assumes Tika is down: it indexes the remaining files by name without retries until an extraction succeeds again, and reports an error naming the number of files indexed by name only once the space is done. +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 same goes for a file the storage refuses to hand out, for example one deleted meanwhile, and for a file whose extraction takes longer than `SEARCH_EXTRACTOR_TIKA_TIMEOUT`. The content is tried again on the first reindex of its space at least one hour after the failure, and on the first one at least 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 failed connection, a server error or too many requests), a space reindex retries the extraction up to five times, one second apart, so a restarting Tika does not leave gaps. A file still unreachable after that is indexed by name as well, this does not count as a failure and every later reindex tries again. After five such files in a row the reindex assumes Tika is down: it indexes the remaining files by name without retries until an extraction succeeds again, and reports an error naming the number of files indexed by name only once the space is done. To list the files the service gave up on, search for: diff --git a/services/search/pkg/config/content.go b/services/search/pkg/config/content.go index 645dc88a6e..d6904f4d87 100644 --- a/services/search/pkg/config/content.go +++ b/services/search/pkg/config/content.go @@ -13,5 +13,5 @@ type Extractor struct { type ExtractorTika struct { TikaURL string `yaml:"tika_url" env:"SEARCH_EXTRACTOR_TIKA_TIKA_URL" desc:"URL of the tika server." introductionVersion:"1.0.0"` CleanStopWords bool `yaml:"clean_stop_words" env:"SEARCH_EXTRACTOR_TIKA_CLEAN_STOP_WORDS" desc:"Defines if stop words should be cleaned or not. See the documentation for more details." introductionVersion:"1.0.0"` - Timeout time.Duration `yaml:"timeout" env:"SEARCH_EXTRACTOR_TIKA_TIMEOUT" desc:"The time one content extraction may take, from downloading the file to the answer of Tika. An extraction that takes longer counts as Tika being unreachable and is retried. 0 disables the limit. See the documentation for more details." introductionVersion:"%%NEXT%%"` + Timeout time.Duration `yaml:"timeout" env:"SEARCH_EXTRACTOR_TIKA_TIMEOUT" desc:"The time one content extraction may take, from downloading the file to the answer of Tika. An extraction that takes longer counts as a failure of the file. 0 disables the limit. See the documentation for more details." introductionVersion:"%%NEXT%%"` } diff --git a/services/search/pkg/config/defaults/defaultconfig.go b/services/search/pkg/config/defaults/defaultconfig.go index 4d7ab3c77e..00c4025f25 100644 --- a/services/search/pkg/config/defaults/defaultconfig.go +++ b/services/search/pkg/config/defaults/defaultconfig.go @@ -10,6 +10,9 @@ import ( "github.com/opencloud-eu/opencloud/services/search/pkg/config" ) +// the task timeout of tika server 3, tika 4 allows up to an hour per file +const defaultTikaTimeout = 5 * time.Minute + // FullDefaultConfig returns a fully initialized default configuration func FullDefaultConfig() *config.Config { cfg := DefaultConfig() @@ -51,8 +54,7 @@ func DefaultConfig() *config.Config { Tika: config.ExtractorTika{ TikaURL: "http://127.0.0.1:9998", CleanStopWords: false, - // the task timeout of tika server 3, tika 4 allows up to an hour per file - Timeout: 5 * time.Minute, + Timeout: defaultTikaTimeout, }, }, Events: config.Events{ diff --git a/services/search/pkg/content/cs3.go b/services/search/pkg/content/cs3.go index 9087f84608..8b63df480e 100644 --- a/services/search/pkg/content/cs3.go +++ b/services/search/pkg/content/cs3.go @@ -85,7 +85,7 @@ func (s cs3) Retrieve(ctx context.Context, rID *provider.ResourceId) (io.ReadClo if cres.StatusCode != http.StatusOK { err := fmt.Errorf("could not download resource. Request returned with statuscode %d ", cres.StatusCode) - if cres.StatusCode >= http.StatusInternalServerError { + if transientStatus(cres.StatusCode) { err = fmt.Errorf("%w: %w", ErrUnreachable, err) } return nil, err diff --git a/services/search/pkg/content/cs3_test.go b/services/search/pkg/content/cs3_test.go index 8ee04f0dce..3dc6614f89 100644 --- a/services/search/pkg/content/cs3_test.go +++ b/services/search/pkg/content/cs3_test.go @@ -84,6 +84,7 @@ var _ = Describe("cs3 retriever", func() { }, true), Entry("storage unavailable", func() { download(rpc.Code_CODE_UNAVAILABLE) }, true), Entry("data server error", func() { download(rpc.Code_CODE_OK); downloadStatus = http.StatusBadGateway }, true), + Entry("data server busy", func() { download(rpc.Code_CODE_OK); downloadStatus = http.StatusTooManyRequests }, true), Entry("data server down", func() { download(rpc.Code_CODE_OK); downloadSrv.Close() }, true), Entry("file not found", func() { download(rpc.Code_CODE_NOT_FOUND) }, false), Entry("permission denied", func() { download(rpc.Code_CODE_PERMISSION_DENIED) }, false), diff --git a/services/search/pkg/content/extractor.go b/services/search/pkg/content/extractor.go index 263298bb35..518ae5bf4b 100644 --- a/services/search/pkg/content/extractor.go +++ b/services/search/pkg/content/extractor.go @@ -12,8 +12,8 @@ import ( "google.golang.org/grpc/status" ) -// ErrUnreachable marks a failure to reach tika or the file, a failed connection, -// a timeout or a server error, the next attempt may well get through +// ErrUnreachable marks a failure to reach tika or the file, a failed connection +// or a server error, the next attempt may well get through var ErrUnreachable = errors.New("tika or the file is unreachable") // Extractor is responsible to extract content and meta information from documents. @@ -25,7 +25,7 @@ type Extractor interface { // other side answered that the request itself is wrong (not found, a 4xx) func unreachable(err error) error { var tikaErr tika.ClientError - if errors.As(err, &tikaErr) && tikaErr.StatusCode < http.StatusInternalServerError { + if errors.As(err, &tikaErr) && !transientStatus(tikaErr.StatusCode) { return err } if s, ok := status.FromError(err); ok { @@ -38,6 +38,12 @@ func unreachable(err error) error { return fmt.Errorf("%w: %w", ErrUnreachable, err) } +// transientStatus tells an http status the next attempt may get past, a server +// error or too many requests, from an answer about the request itself +func transientStatus(code int) bool { + return code >= http.StatusInternalServerError || code == http.StatusTooManyRequests +} + // getFirstValue returns the first metadata value present among keys, trying them // in order. It errors when the map is nil or none of the keys holds a value. func getFirstValue(m map[string][]string, keys ...string) (string, error) { diff --git a/services/search/pkg/content/tika.go b/services/search/pkg/content/tika.go index bcc9ccab77..c3f529f20d 100644 --- a/services/search/pkg/content/tika.go +++ b/services/search/pkg/content/tika.go @@ -81,21 +81,22 @@ func (t Tika) Extract(ctx context.Context, ri *provider.ResourceInfo) (Document, } // a tika that hangs instead of refusing connections would block the walk + attemptCtx := ctx if t.Timeout > 0 { var cancel context.CancelFunc - ctx, cancel = context.WithTimeout(ctx, t.Timeout) + attemptCtx, cancel = context.WithTimeout(ctx, t.Timeout) defer cancel() } - data, err := t.Retrieve(ctx, ri.Id) + data, err := t.Retrieve(attemptCtx, ri.GetId()) if err != nil { - return doc, err + return doc, t.attemptError(ctx, attemptCtx, ri, err) } defer data.Close() - metas, err := t.tika.MetaRecursive(ctx, data) + metas, err := t.tika.MetaRecursive(attemptCtx, data) if err != nil { - return doc, unreachable(err) + return doc, t.attemptError(ctx, attemptCtx, ri, unreachable(err)) } // a file with content always yields metadata, none means tika failed on it if len(metas) == 0 { @@ -145,13 +146,23 @@ func (t Tika) Extract(ctx context.Context, ri *provider.ResourceInfo) (Document, doc.MotionPhoto = t.getMotionPhoto(m0, metas[i+1]) } - if langCode := t.detectLanguage(ctx, doc.Content); langCode != "" && t.CleanStopWords { + if langCode := t.detectLanguage(attemptCtx, doc.Content); langCode != "" && t.CleanStopWords { doc.Content = CleanString(doc.Content, langCode) } return doc, nil } +// attemptError turns an attempt that ran out of time into a failure of the file: +// tika accepted the request, so a retry would most likely run as long again and +// stack more work on a tika that may still be parsing +func (t Tika) attemptError(ctx, attemptCtx context.Context, ri *provider.ResourceInfo, err error) error { + if ctx.Err() == nil && errors.Is(attemptCtx.Err(), context.DeadlineExceeded) { + return fmt.Errorf("extracting %q took longer than %s: %w", ri.GetName(), t.Timeout, context.DeadlineExceeded) + } + return err +} + // detectLanguage asks tika for the language of content. Tika 4 moved the // endpoint from /language/string to /language, so try the new path first and // fall back for an older tika. diff --git a/services/search/pkg/content/tika_test.go b/services/search/pkg/content/tika_test.go index 357ad8ad04..a2f4224ad0 100644 --- a/services/search/pkg/content/tika_test.go +++ b/services/search/pkg/content/tika_test.go @@ -157,10 +157,11 @@ var _ = Describe("Tika", func() { Expect(errors.Is(err, content.ErrUnreachable)).To(Equal(unreachable)) }, Entry("server error", http.StatusServiceUnavailable, true), + Entry("too many requests", http.StatusTooManyRequests, true), Entry("unprocessable file", http.StatusUnprocessableEntity, false), ) - It("gives up on a tika that does not answer in time", func() { + It("counts a tika that does not answer in time as a failure of the file", func() { rmetaHangs = true tika.Timeout = 10 * time.Millisecond @@ -168,8 +169,8 @@ var _ = Describe("Tika", func() { Type: provider.ResourceType_RESOURCE_TYPE_FILE, Size: 1, }) - Expect(err).To(MatchError(content.ErrUnreachable)) Expect(err).To(MatchError(context.DeadlineExceeded)) + Expect(errors.Is(err, content.ErrUnreachable)).To(BeFalse()) }) It("adds the title", func() {