forked from longtao.wu/droboticflow
103 lines
2.7 KiB
Go
103 lines
2.7 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"droboticflow/internal/domain"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type queryBatchFacts struct {
|
|
once sync.Once
|
|
permit chan struct{}
|
|
mu sync.Mutex
|
|
epoch int64
|
|
rows map[string]*domain.HumanBatchRecord
|
|
}
|
|
|
|
func cloneReplicaBatch(v domain.HumanBatchRecord) domain.HumanBatchRecord {
|
|
v.Stages = append([]domain.HumanStageDefinition(nil), v.Stages...)
|
|
for i := range v.Stages {
|
|
v.Stages[i].RejectTargets = append([]string(nil), v.Stages[i].RejectTargets...)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Only clean, majority-committed query facts use this cache. Source pending
|
|
// policy rows bypass it in replicaBatchAccess; account/grant checks never enter
|
|
// it. Unknown IDs are negative-cached only at this collection's fact epoch.
|
|
func (s *MongoStore) cachedReplicaBatchAccess(ctx context.Context, org string, ids []string) ([]domain.HumanBatchRecord, error) {
|
|
b, ok := ctx.Value(queryReplicaPoolReadKey{}).(*queryReplicaPoolBarrier)
|
|
if !ok || !b.counts {
|
|
return s.readBatchAccess(ctx, org, ids)
|
|
}
|
|
b.batchEpochOnce.Do(func() {
|
|
var st struct {
|
|
Epochs map[string]int64 `bson:"collectionEpoch"`
|
|
}
|
|
b.batchEpochErr = s.db.Collection(queryReplicaControl).FindOne(ctx, bson.M{"_id": "state"}, options.FindOne().SetProjection(bson.M{"collectionEpoch.humanBatches": 1})).Decode(&st)
|
|
b.batchEpoch = st.Epochs["humanBatches"]
|
|
})
|
|
if b.batchEpochErr != nil {
|
|
return nil, b.batchEpochErr
|
|
}
|
|
c := &s.queryReplicaBatchFacts
|
|
c.once.Do(func() {
|
|
c.permit = make(chan struct{}, 1)
|
|
c.permit <- struct{}{}
|
|
c.rows = map[string]*domain.HumanBatchRecord{}
|
|
})
|
|
// One cold batch fill per process, cancelable while waiting. Shared facts
|
|
// avoid decoding the same 26k batch policies separately for every actor.
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
case <-c.permit:
|
|
}
|
|
defer func() { c.permit <- struct{}{} }()
|
|
c.mu.Lock()
|
|
if b.batchEpoch > c.epoch {
|
|
clear(c.rows)
|
|
c.epoch = b.batchEpoch
|
|
}
|
|
missing := []string{}
|
|
seen := map[string]bool{}
|
|
for _, id := range ids {
|
|
key := org + ":" + id
|
|
if _, ok := c.rows[key]; !ok && !seen[id] {
|
|
missing = append(missing, id)
|
|
seen[id] = true
|
|
}
|
|
}
|
|
c.mu.Unlock()
|
|
if len(missing) > 0 {
|
|
rows, e := s.readBatchAccess(ctx, org, missing)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
c.mu.Lock()
|
|
if len(c.rows)+len(missing) > 32768 {
|
|
clear(c.rows)
|
|
}
|
|
for _, id := range missing {
|
|
c.rows[org+":"+id] = nil
|
|
}
|
|
for _, v := range rows {
|
|
r := cloneReplicaBatch(v)
|
|
c.rows[org+":"+v.ID] = &r
|
|
}
|
|
c.mu.Unlock()
|
|
}
|
|
out := make([]domain.HumanBatchRecord, 0, len(ids))
|
|
c.mu.Lock()
|
|
for _, id := range ids {
|
|
if r := c.rows[org+":"+id]; r != nil {
|
|
out = append(out, cloneReplicaBatch(*r))
|
|
}
|
|
}
|
|
c.mu.Unlock()
|
|
return out, nil
|
|
}
|