From a88477aa7308fe1c111babd7efbcdd9e5fbf1c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Duffeck?= Date: Wed, 4 Nov 2026 15:47:44 +0100 Subject: [PATCH] Write one bleve batch at a time bleve keeps the analysis of a whole batch in memory until it is written, so batches of spaces reindexed in parallel stacked up their memory. --- services/search/pkg/bleve/backend.go | 22 +++++++++++- services/search/pkg/bleve/batch_test.go | 46 +++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/services/search/pkg/bleve/backend.go b/services/search/pkg/bleve/backend.go index d8426e5435..a92fb7e16f 100644 --- a/services/search/pkg/bleve/backend.go +++ b/services/search/pkg/bleve/backend.go @@ -3,6 +3,7 @@ package bleve import ( "context" "math" + "sync" "time" "github.com/blevesearch/bleve/v2" @@ -34,12 +35,31 @@ type Backend struct { func NewBackend(index bleve.Index, queryCreator searchQuery.Creator[query.Query], log log.Logger) *Backend { return &Backend{ - index: index, + index: &serialIndex{bleveIndex: index}, queryCreator: queryCreator, log: log, } } +// bleveIndex lets serialIndex embed bleve.Index, whose Index method would clash +// with an embedded field of the same name +type bleveIndex = bleve.Index + +// serialIndex runs one batch at a time. bleve analyzes a batch through a pool of +// workers shared by all batches and keeps the whole analysis in memory until the +// batch is written, so batches running side by side (e.g. spaces reindexed +// concurrently) add little throughput but stack up their memory. +type serialIndex struct { + bleveIndex + batchMu sync.Mutex +} + +func (i *serialIndex) Batch(b *bleve.Batch) error { + i.batchMu.Lock() + defer i.batchMu.Unlock() + return i.bleveIndex.Batch(b) +} + // Search executes a search request operation within the index. // Returns a SearchIndexResponse object or an error. func (b *Backend) Search(_ context.Context, sir *searchService.SearchIndexRequest) (*searchService.SearchIndexResponse, error) { diff --git a/services/search/pkg/bleve/batch_test.go b/services/search/pkg/bleve/batch_test.go index 9282689da1..bf42ff787f 100644 --- a/services/search/pkg/bleve/batch_test.go +++ b/services/search/pkg/bleve/batch_test.go @@ -6,28 +6,48 @@ package bleve_test import ( "fmt" "strings" + "sync" + "time" bleveSearch "github.com/blevesearch/bleve/v2" sprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "github.com/opencloud-eu/opencloud/pkg/log" "github.com/opencloud-eu/opencloud/services/search/pkg/bleve" "github.com/opencloud-eu/opencloud/services/search/pkg/content" + bleveQuery "github.com/opencloud-eu/opencloud/services/search/pkg/query/bleve" "github.com/opencloud-eu/opencloud/services/search/pkg/search" ) type bleveIndex = bleveSearch.Index -// batchProbe records the batches written to the index +// batchProbe records the batches written to the index and how many ran at the +// same time type batchProbe struct { bleveIndex - sizes []int + mu sync.Mutex + sizes []int + running int + peak int } func (p *batchProbe) Batch(b *bleveSearch.Batch) error { + p.mu.Lock() p.sizes = append(p.sizes, b.Size()) - return p.bleveIndex.Batch(b) + p.running++ + p.peak = max(p.peak, p.running) + p.mu.Unlock() + + // long enough for batches pushed together to overlap if nothing serializes them + time.Sleep(20 * time.Millisecond) + err := p.bleveIndex.Batch(b) + + p.mu.Lock() + p.running-- + p.mu.Unlock() + return err } var _ = Describe("Batch", func() { @@ -102,4 +122,24 @@ var _ = Describe("Batch", func() { Expect(idx.DocCount()).To(Equal(uint64(3))) }) }) + + Describe("Push", func() { + It("runs one batch at a time when several are pushed together", func() { + probe := &batchProbe{bleveIndex: idx} + eng := bleve.NewBackend(probe, bleveQuery.DefaultCreator, log.NopLogger()) + + var wg sync.WaitGroup + for i := range 4 { + wg.Go(func() { + defer GinkgoRecover() + r := file(i, "lorem ipsum") + Expect(eng.Upsert(r.ID, r)).To(Succeed()) + }) + } + wg.Wait() + + Expect(probe.peak).To(Equal(1)) + Expect(eng.DocCount()).To(Equal(uint64(4))) + }) + }) })