-
Notifications
You must be signed in to change notification settings - Fork 11
feat(async-search): fetch fractions one by one to avoid global lock #424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ type fractionRegistry struct { | |
| muAll sync.RWMutex // protects active, all, and oldestTotal fields | ||
| active *activeProxy // currently active writable fraction | ||
| all []frac.Fraction // all fractions in creation order (read-only view) | ||
| allMap map[string]frac.Fraction | ||
| } | ||
|
|
||
| // NewFractionRegistry creates and initializes a new fraction registry instance. | ||
|
|
@@ -82,6 +83,14 @@ func (r *fractionRegistry) Active() *activeProxy { | |
| return r.active | ||
| } | ||
|
|
||
| func (r *fractionRegistry) AcquireFraction(name string) (frac.Fraction, func(), bool) { | ||
| r.muAll.RLock() | ||
| defer r.muAll.RUnlock() | ||
|
|
||
| f, ok := r.allMap[name] | ||
| return f, func() {}, ok | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why it always returns empty function? is it gonna be used later? |
||
| } | ||
|
|
||
| // AllFractions returns a read-only view of all fractions in creation order. | ||
| func (r *fractionRegistry) AllFractions() []frac.Fraction { | ||
| r.muAll.RLock() | ||
|
|
@@ -215,6 +224,7 @@ func (r *fractionRegistry) addActive(a *activeProxy) { | |
|
|
||
| r.active = a | ||
| r.all = append(r.all, a.proxy) | ||
| r.allMap[a.instance.Info().Name()] = a.proxy | ||
| } | ||
|
|
||
| // trimAll removes the oldest fractions from the complete fractions list. | ||
|
|
@@ -223,6 +233,9 @@ func (r *fractionRegistry) trimAll(count int) { | |
| r.muAll.Lock() | ||
| defer r.muAll.Unlock() | ||
|
|
||
| for _, f := range r.all[:count] { | ||
| delete(r.allMap, f.Info().Name()) | ||
| } | ||
| r.all = r.all[count:] | ||
| r.updateOldestTotal() | ||
| } | ||
|
|
@@ -439,26 +452,34 @@ func (r *fractionRegistry) removeFromOffloading(sealed *sealedProxy) { | |
| // Expensive O(n) operation used when direct list modification is insufficient. | ||
| func (r *fractionRegistry) rebuildAllFractions() { | ||
| all := make([]frac.Fraction, 0, len(r.all)) | ||
| allMap := make(map[string]frac.Fraction, len(r.all)) | ||
|
|
||
| add := func(f frac.Fraction) { | ||
| all = append(all, f) | ||
| allMap[f.Info().Name()] = f | ||
| } | ||
|
|
||
| // collect fractions in correct chronological order: from oldest (remote) to newest (active) | ||
| for _, remote := range r.remotes { | ||
| all = append(all, remote.proxy) | ||
| add(remote.proxy) | ||
| } | ||
| for _, offloaded := range r.offloading { | ||
| all = append(all, offloaded.proxy) | ||
| add(offloaded.proxy) | ||
| } | ||
| for _, sealed := range r.sealed { | ||
| all = append(all, sealed.proxy) | ||
| add(sealed.proxy) | ||
| } | ||
| for _, active := range r.sealing { | ||
| all = append(all, active.proxy) | ||
| add(active.proxy) | ||
| } | ||
| all = append(all, r.active.proxy) | ||
|
|
||
| add(r.active.proxy) | ||
|
|
||
| r.muAll.Lock() | ||
| defer r.muAll.Unlock() | ||
|
|
||
| r.all = all | ||
| r.allMap = allMap | ||
| r.updateOldestTotal() | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,11 @@ type MappingProvider interface { | |
| GetMapping() seq.Mapping | ||
| } | ||
|
|
||
| type fractionAcquirer interface { | ||
| Fractions() fracmanager.List | ||
| AcquireFraction(name string) (_ frac.Fraction, release func(), ok bool) | ||
| } | ||
|
|
||
| // Config holds configuration parameters for SkipMaskManager. | ||
| type Config struct { | ||
| DataDir string // Directory to store skip mask files | ||
|
|
@@ -137,15 +142,15 @@ func New( | |
| // - Begins asynchronous processing of all skip mask queries | ||
| // | ||
| // This method must be called before using the manager. | ||
| func (smm *SkipMaskManager) Start(fracs fracmanager.List) { | ||
| func (smm *SkipMaskManager) Start(fracs fractionAcquirer) { | ||
| smm.createDataDir() | ||
|
|
||
| err := smm.loadSkipMasks() | ||
| if err != nil { | ||
| logger.Fatal("failed to load previous skip masks", zap.Error(err)) | ||
| } | ||
|
|
||
| err = smm.buildQueue(fracs) | ||
| err = smm.buildQueue(fracs.Fractions()) | ||
| if err != nil { | ||
| logger.Fatal("failed to build skip mask manager queue", zap.Error(err)) | ||
| } | ||
|
|
@@ -166,7 +171,7 @@ func (smm *SkipMaskManager) Start(fracs fracmanager.List) { | |
| } | ||
| sm.ast = ast | ||
|
|
||
| smm.processSkipMask(sm, fracs.FilterInRange(sm.params.From, sm.params.To)) | ||
| smm.processSkipMask(sm, fracs) | ||
| } | ||
| }() | ||
| } | ||
|
|
@@ -434,17 +439,7 @@ func (smm *SkipMaskManager) buildQueue(fracs fracmanager.List) error { | |
| // It processes each fraction with a .queue file, running search queries in parallel | ||
| // (limited by the rate limiter). Each successful search writes results to a .skipmask | ||
| // file. The skip mask status is set to Done when all fractions are processed. | ||
| func (smm *SkipMaskManager) processSkipMask(skipMask *SkipMask, fracs fracmanager.List) { | ||
| if len(fracs) == 0 { | ||
| skipMask.setStatus(StatusDone) | ||
| return | ||
| } | ||
|
|
||
| fracsByName := make(map[string]frac.Fraction) | ||
| for _, f := range fracs { | ||
| fracsByName[f.Info().Name()] = f | ||
| } | ||
|
|
||
| func (smm *SkipMaskManager) processSkipMask(skipMask *SkipMask, fracs fractionAcquirer) { | ||
| skipMaskDes, err := os.ReadDir(skipMask.dirPath) | ||
| if err != nil { | ||
| panic(fmt.Errorf("BUG: reading directory must be successful: %s", err)) | ||
|
|
@@ -453,11 +448,6 @@ func (smm *SkipMaskManager) processSkipMask(skipMask *SkipMask, fracs fracmanage | |
| inProgress.Add(1) | ||
|
|
||
| processFracInQueue := func(name string) error { | ||
| f, ok := fracsByName[fracNameFromFilePath(name)] | ||
| if !ok { // skip missing fracs | ||
| return nil | ||
| } | ||
|
|
||
| select { | ||
| case <-smm.ctx.Done(): | ||
| return nil | ||
|
|
@@ -466,6 +456,13 @@ func (smm *SkipMaskManager) processSkipMask(skipMask *SkipMask, fracs fracmanage | |
| go func() { | ||
| defer skipMask.processWg.Done() | ||
| defer func() { <-smm.rateLimit }() | ||
|
|
||
| f, release, ok := fracs.AcquireFraction(fracNameFromFilePath(name)) | ||
| defer release() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now,
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe defer release after |
||
| if !ok { // skip missing fracs | ||
| return | ||
| } | ||
|
|
||
| if err := smm.processFrac(f, skipMask); err != nil { | ||
| if errors.Is(err, context.Canceled) { | ||
| logger.Info("skip mask manager refresh frac context cancelled") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the same question here.
https://github.com/ozontech/seq-db/pull/424/changes#r3247151983