fix(search): count extraction timeouts against the file, retry on 429

This commit is contained in:
Florian Schade committed 2026-11-25 13:21:45 +01:00
1 parent 12e7b86344
commit 960ff63db3
8 files changed
+38 -17

No files matched your search

+2 -2
View File
@@ -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:
+1 -1
View File
@@ -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%%"`
}
@@ -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{
+1 -1
View File
@@ -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
+1
View File
@@ -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),
+9 -3
View File
@@ -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) {
+17 -6
View File
@@ -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.
+3 -2
View File
@@ -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() {