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.
This commit is contained in:
André Duffeck committed 2026-11-04 15:47:44 +01:00
1 parent 51b7c8b0d3
commit a88477aa73
2 files changed
+64 -4

No files matched your search

+21 -1
View File
@@ -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) {
+43 -3
View File
@@ -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)))
})
})
})