diff --git a/services/search/pkg/search/service.go b/services/search/pkg/search/service.go index 425296b458..7c06debd83 100644 --- a/services/search/pkg/search/service.go +++ b/services/search/pkg/search/service.go @@ -1214,8 +1214,10 @@ type unreachableExtractions struct { last error } +// err reports the outage as soon as one file could not be extracted because +// tika or the file was out of reach. The error matches content.ErrUnreachable. func (u *unreachableExtractions) err() error { - if !u.tripped { + if u.files == 0 { return nil } return fmt.Errorf("tika or the files were out of reach, %d files indexed by name only: %w", u.files, u.last) diff --git a/services/search/pkg/search/service_test.go b/services/search/pkg/search/service_test.go index e250161601..adc62d11cc 100644 --- a/services/search/pkg/search/service_test.go +++ b/services/search/pkg/search/service_test.go @@ -316,7 +316,7 @@ var _ = Describe("Searchprovider", func() { Expect(r.ExtractionFailure).To(BeNil()) }) - It("indexes a file still out of reach by name without failing the walk", func() { + It("indexes a file still out of reach by name and reports it, even below the streak limit", func() { files = []string{"gone.pdf", "report.pdf"} extract = func(name string, _ int) (content.Document, error) { if name == "gone.pdf" { @@ -325,8 +325,11 @@ var _ = Describe("Searchprovider", func() { return content.Document{Name: name}, nil } - Expect(s.IndexSpace(&sprovider.StorageSpaceId{OpaqueId: "storageid$spaceid!spaceid"}, false)).To(Succeed()) + err := s.IndexSpace(&sprovider.StorageSpaceId{OpaqueId: "storageid$spaceid!spaceid"}, false) + Expect(err).To(MatchError(content.ErrUnreachable)) + Expect(err).To(MatchError(ContainSubstring("1 files indexed by name only"))) Expect(attempts).To(Equal(map[string]int{"gone.pdf": 6, "report.pdf": 1})) + Expect(upserted).To(HaveKey("storageid$spaceid!report.pdf")) Expect(upserted["storageid$spaceid!gone.pdf"].ExtractionFailure).To(Equal(&search.ExtractionFailure{})) }) diff --git a/services/search/pkg/service/event/debouncer.go b/services/search/pkg/service/event/debouncer.go index a815c5d80d..7665e7eeb6 100644 --- a/services/search/pkg/service/event/debouncer.go +++ b/services/search/pkg/service/event/debouncer.go @@ -1,19 +1,21 @@ package event import ( + "errors" "sync" "time" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" "github.com/opencloud-eu/opencloud/pkg/log" + "github.com/opencloud-eu/opencloud/services/search/pkg/content" ) // SpaceDebouncer debounces operations on spaces for a configurable amount of time type SpaceDebouncer struct { after time.Duration timeout time.Duration - f func(id *provider.StorageSpaceId, force bool) + f func(id *provider.StorageSpaceId, force bool) error pending map[string]*workItem inProgress sync.Map @@ -32,7 +34,7 @@ type workItem struct { type AckFunc func() error // NewSpaceDebouncer returns a new SpaceDebouncer instance -func NewSpaceDebouncer(d time.Duration, timeout time.Duration, f func(id *provider.StorageSpaceId, force bool), logger log.Logger) *SpaceDebouncer { +func NewSpaceDebouncer(d time.Duration, timeout time.Duration, f func(id *provider.StorageSpaceId, force bool) error, logger log.Logger) *SpaceDebouncer { return &SpaceDebouncer{ after: d, timeout: timeout, @@ -45,8 +47,9 @@ func NewSpaceDebouncer(d time.Duration, timeout time.Duration, f func(id *provid // Debounce restarts the debounce timer for the given space. If force is set, // the scheduled run is a forced one. A pending run stays forced even if it is -// debounced again without force. -func (d *SpaceDebouncer) Debounce(id *provider.StorageSpaceId, ack AckFunc, force bool) { +// debounced again without force. When the run fails because tika or the files +// are unreachable, nack is called instead of ack so the event is redelivered. +func (d *SpaceDebouncer) Debounce(id *provider.StorageSpaceId, ack AckFunc, nack AckFunc, force bool) { d.mutex.Lock() defer d.mutex.Unlock() @@ -83,8 +86,16 @@ func (d *SpaceDebouncer) Debounce(id *provider.StorageSpaceId, ack AckFunc, forc }() d.mutex.Unlock() // release the lock early to allow other goroutines to debounce - d.f(id, force) + err := d.f(id, force) go func() { + if nack != nil && errors.Is(err, content.ErrUnreachable) { + d.log.Warn().Err(err).Msg("nacking event because tika or the files are unreachable") + nackErr := nack() + if nackErr != nil { + d.log.Error().Err(nackErr).Msg("error while nacking event") + } + return + } if ack != nil { if err := ack(); err != nil { d.log.Error().Err(err).Msg("error while acknowledging event") diff --git a/services/search/pkg/service/event/debouncer_test.go b/services/search/pkg/service/event/debouncer_test.go index eed4111e52..d649388387 100644 --- a/services/search/pkg/service/event/debouncer_test.go +++ b/services/search/pkg/service/event/debouncer_test.go @@ -1,6 +1,8 @@ package event_test import ( + "errors" + "fmt" "sync/atomic" "time" @@ -9,6 +11,7 @@ import ( . "github.com/onsi/gomega" "github.com/opencloud-eu/opencloud/pkg/log" + "github.com/opencloud-eu/opencloud/services/search/pkg/content" "github.com/opencloud-eu/opencloud/services/search/pkg/service/event" ) @@ -25,30 +28,31 @@ var _ = Describe("SpaceDebouncer", func() { BeforeEach(func() { callCount = atomic.Int32{} - debouncer = event.NewSpaceDebouncer(50*time.Millisecond, 10*time.Second, func(id *sprovider.StorageSpaceId, _ bool) { + debouncer = event.NewSpaceDebouncer(50*time.Millisecond, 10*time.Second, func(id *sprovider.StorageSpaceId, _ bool) error { if id.OpaqueId == "spaceid" { callCount.Add(1) } + return nil }, log.NewLogger()) }) It("debounces", func() { - debouncer.Debounce(spaceid, nil, false) - debouncer.Debounce(spaceid, nil, false) - debouncer.Debounce(spaceid, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) Eventually(func() int { return int(callCount.Load()) }, "200ms").Should(Equal(1)) }) It("works multiple times", func() { - debouncer.Debounce(spaceid, nil, false) - debouncer.Debounce(spaceid, nil, false) - debouncer.Debounce(spaceid, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) time.Sleep(100 * time.Millisecond) - debouncer.Debounce(spaceid, nil, false) - debouncer.Debounce(spaceid, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) Eventually(func() int { return int(callCount.Load()) @@ -59,18 +63,19 @@ var _ = Describe("SpaceDebouncer", func() { // runs block until released, so the first one is still in progress for // as long as the test needs it, however slow the machine is release := make(chan struct{}) - debouncer = event.NewSpaceDebouncer(50*time.Millisecond, 5*time.Second, func(id *sprovider.StorageSpaceId, _ bool) { + debouncer = event.NewSpaceDebouncer(50*time.Millisecond, 5*time.Second, func(id *sprovider.StorageSpaceId, _ bool) error { if id.OpaqueId == "spaceid" { callCount.Add(1) } <-release + return nil }, log.NewLogger()) - debouncer.Debounce(spaceid, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) Eventually(func() int { return int(callCount.Load()) }, "2000ms").Should(Equal(1)) - debouncer.Debounce(spaceid, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) // shouldn't trigger as the other run is still in progress Consistently(func() int { return int(callCount.Load()) @@ -83,10 +88,11 @@ var _ = Describe("SpaceDebouncer", func() { }) It("fires at the timeout even when continuously debounced", func() { - debouncer = event.NewSpaceDebouncer(100*time.Millisecond, 250*time.Millisecond, func(id *sprovider.StorageSpaceId, _ bool) { + debouncer = event.NewSpaceDebouncer(100*time.Millisecond, 250*time.Millisecond, func(id *sprovider.StorageSpaceId, _ bool) error { if id.OpaqueId == "spaceid" { callCount.Add(1) } + return nil }, log.NewLogger()) // Reset the debounce timer every 50ms (shorter than the 100ms debounce @@ -95,11 +101,11 @@ var _ = Describe("SpaceDebouncer", func() { // a Debounce call arriving right after the timeout fires would find // pending empty and schedule a second workItem, breaking the assertion // below that the work function is invoked exactly once. - debouncer.Debounce(spaceid, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) for i := 0; i < 4 && callCount.Load() == 0; i++ { time.Sleep(50 * time.Millisecond) if callCount.Load() == 0 { - debouncer.Debounce(spaceid, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) } } @@ -116,29 +122,31 @@ var _ = Describe("SpaceDebouncer", func() { }) It("runs once when the debounce timer and the timeout are due together", func() { - debouncer = event.NewSpaceDebouncer(20*time.Millisecond, 20*time.Millisecond, func(id *sprovider.StorageSpaceId, _ bool) { + debouncer = event.NewSpaceDebouncer(20*time.Millisecond, 20*time.Millisecond, func(id *sprovider.StorageSpaceId, _ bool) error { if id.OpaqueId == "spaceid" { callCount.Add(1) } + return nil }, log.NewLogger()) for i := 0; i < 10; i++ { callCount.Store(0) - debouncer.Debounce(spaceid, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) time.Sleep(60 * time.Millisecond) Expect(int(callCount.Load())).To(Equal(1), "iteration %d", i) } }) It("doesn't run the timeout function if the work function has been called", func() { - debouncer = event.NewSpaceDebouncer(100*time.Millisecond, 250*time.Millisecond, func(id *sprovider.StorageSpaceId, _ bool) { + debouncer = event.NewSpaceDebouncer(100*time.Millisecond, 250*time.Millisecond, func(id *sprovider.StorageSpaceId, _ bool) error { if id.OpaqueId == "spaceid" { callCount.Add(1) } + return nil }, log.NewLogger()) // Initial call to start the timers - debouncer.Debounce(spaceid, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) // Wait for the debounce timer to fire Eventually(func() int { @@ -157,7 +165,7 @@ var _ = Describe("SpaceDebouncer", func() { return nil } - debouncer.Debounce(spaceid, ackFunc, false) + debouncer.Debounce(spaceid, ackFunc, nil, false) Eventually(func() int { return int(callCount.Load()) @@ -181,12 +189,12 @@ var _ = Describe("SpaceDebouncer", func() { } // First call, sets up the trigger - debouncer.Debounce(spaceid, firstAckFunc, false) + debouncer.Debounce(spaceid, firstAckFunc, nil, false) Expect(firstAckCalled.Load()).To(BeFalse()) Expect(secondAckCalled.Load()).To(BeFalse()) // Second call, should call its ack immediately - debouncer.Debounce(spaceid, secondAckFunc, false) + debouncer.Debounce(spaceid, secondAckFunc, nil, false) Eventually(func() bool { return secondAckCalled.Load() }, "50ms").Should(BeTrue()) @@ -202,21 +210,95 @@ var _ = Describe("SpaceDebouncer", func() { }, "200ms").Should(BeTrue()) }) + Describe("nack", func() { + var ( + ackCalled atomic.Bool + nackCalled atomic.Bool + ackFunc = func() error { + ackCalled.Store(true) + return nil + } + nackFunc = func() error { + nackCalled.Store(true) + return nil + } + ) + + BeforeEach(func() { + ackCalled = atomic.Bool{} + nackCalled = atomic.Bool{} + }) + + DescribeTable("acks or nacks the event depending on the result of the run", + func(result error, expectNack bool) { + debouncer = event.NewSpaceDebouncer(50*time.Millisecond, 10*time.Second, func(_ *sprovider.StorageSpaceId, _ bool) error { + callCount.Add(1) + return result + }, log.NewLogger()) + + debouncer.Debounce(spaceid, ackFunc, nackFunc, false) + + Eventually(func() int { + return int(callCount.Load()) + }, "200ms").Should(Equal(1)) + Eventually(func() bool { + return ackCalled.Load() || nackCalled.Load() + }, "200ms").Should(BeTrue()) + Expect(nackCalled.Load()).To(Equal(expectNack)) + Expect(ackCalled.Load()).To(Equal(!expectNack)) + }, + Entry("acks when the run succeeds", nil, false), + Entry("acks when the run fails with another error", errors.New("boom"), false), + Entry("nacks when the run fails because tika or the file is unreachable", fmt.Errorf("%w: connection refused", content.ErrUnreachable), true), + ) + + It("acks, not nacks, an event that is coalesced into a pending run", func() { + debouncer = event.NewSpaceDebouncer(50*time.Millisecond, 10*time.Second, func(_ *sprovider.StorageSpaceId, _ bool) error { + callCount.Add(1) + return content.ErrUnreachable + }, log.NewLogger()) + + var secondAckCalled, secondNackCalled atomic.Bool + debouncer.Debounce(spaceid, nil, nil, false) + debouncer.Debounce( + spaceid, + func() error { + secondAckCalled.Store(true) + return nil + }, + func() error { + secondNackCalled.Store(true) + return nil + }, + false, + ) + + Eventually(func() bool { + return secondAckCalled.Load() + }, "50ms").Should(BeTrue()) + Eventually(func() int { + return int(callCount.Load()) + }, "200ms").Should(Equal(1)) + Expect(secondNackCalled.Load()).To(BeFalse()) + }) + }) + Describe("force", func() { var forced atomic.Bool BeforeEach(func() { forced = atomic.Bool{} - debouncer = event.NewSpaceDebouncer(50*time.Millisecond, 10*time.Second, func(id *sprovider.StorageSpaceId, force bool) { + debouncer = event.NewSpaceDebouncer(50*time.Millisecond, 10*time.Second, func(id *sprovider.StorageSpaceId, force bool) error { if id.OpaqueId == "spaceid" { forced.Store(force) callCount.Add(1) } + return nil }, log.NewLogger()) }) It("is not forced by default", func() { - debouncer.Debounce(spaceid, nil, false) + debouncer.Debounce(spaceid, nil, nil, false) Eventually(func() int { return int(callCount.Load()) }, "200ms").Should(Equal(1)) @@ -224,7 +306,7 @@ var _ = Describe("SpaceDebouncer", func() { }) It("passes the force flag", func() { - debouncer.Debounce(spaceid, nil, true) + debouncer.Debounce(spaceid, nil, nil, true) Eventually(func() int { return int(callCount.Load()) }, "200ms").Should(Equal(1)) @@ -232,8 +314,8 @@ var _ = Describe("SpaceDebouncer", func() { }) It("keeps a pending run forced when debounced again without force", func() { - debouncer.Debounce(spaceid, nil, true) - debouncer.Debounce(spaceid, nil, false) + debouncer.Debounce(spaceid, nil, nil, true) + debouncer.Debounce(spaceid, nil, nil, false) Eventually(func() int { return int(callCount.Load()) }, "200ms").Should(Equal(1)) @@ -241,8 +323,8 @@ var _ = Describe("SpaceDebouncer", func() { }) It("upgrades a pending run to forced", func() { - debouncer.Debounce(spaceid, nil, false) - debouncer.Debounce(spaceid, nil, true) + debouncer.Debounce(spaceid, nil, nil, false) + debouncer.Debounce(spaceid, nil, nil, true) Eventually(func() int { return int(callCount.Load()) }, "200ms").Should(Equal(1)) diff --git a/services/search/pkg/service/event/service.go b/services/search/pkg/service/event/service.go index 86c277b423..c1712448ac 100644 --- a/services/search/pkg/service/event/service.go +++ b/services/search/pkg/service/event/service.go @@ -79,10 +79,12 @@ func New(ctx context.Context, stream raw.Stream, logger log.Logger, tp trace.Tra svc.events = append(svc.events, events.FileUploaded{}) } - svc.indexSpaceDebouncer = NewSpaceDebouncer(time.Duration(debounceDuration)*time.Millisecond, 30*time.Second, func(id *provider.StorageSpaceId, force bool) { - if err := svc.index.IndexSpace(id, force); err != nil { + svc.indexSpaceDebouncer = NewSpaceDebouncer(time.Duration(debounceDuration)*time.Millisecond, 30*time.Second, func(id *provider.StorageSpaceId, force bool) error { + err := svc.index.IndexSpace(id, force) + if err != nil { svc.log.Error().Err(err).Interface("spaceID", id).Msg("error while indexing a space") } + return err }, svc.log) return svc, nil @@ -179,7 +181,7 @@ func (s Service) processEvent(e raw.Event) error { debounce := func(id *provider.StorageSpaceId, force bool) { ack = func() error { return nil } - s.indexSpaceDebouncer.Debounce(id, e.Ack, force) + s.indexSpaceDebouncer.Debounce(id, e.Ack, e.Nak, force) } var err error