droboticflow/internal/store/query_replica_pool.go

251 lines
7.8 KiB
Go

package store
import (
"context"
"droboticflow/internal/domain"
"droboticflow/internal/queryserving"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
type queryReplicaPoolReadKey struct{}
// WithQueryReplicaPoolRead is used only by list/count GET handlers, never by
// claims, session renewals or business jobs looking for executable work.
func WithQueryReplicaPoolRead(ctx context.Context) context.Context {
if _, ok := ctx.Value(queryReplicaPoolReadKey{}).(*queryReplicaPoolBarrier); ok {
return ctx
}
return context.WithValue(ctx, queryReplicaPoolReadKey{}, &queryReplicaPoolBarrier{})
}
func (s *MongoStore) UsesQueryReplicaPool(ctx context.Context) bool {
b, v := ctx.Value(queryReplicaPoolReadKey{}).(*queryReplicaPoolBarrier)
return v && s.queryReplicaReads != nil && (s.queryReplicaReads.pool || b.counts && s.queryReplicaReads.counts)
}
func (s *MongoStore) poolReplica(ctx context.Context) (*MongoStore, error) {
if !s.UsesQueryReplicaPool(ctx) {
return nil, nil
}
return s.reportReplica(ctx)
}
func (s *MongoStore) EnableQueryReplicaPoolReads(enabled bool) {
if s.queryReplicaReads != nil {
s.queryReplicaReads.pool = enabled
}
}
// Count-only rollout leaves list discovery and all business mutations on the
// authority. The legacy pool switch continues to enable both lists and counts.
func (s *MongoStore) EnableQueryReplicaCountReads(enabled bool) {
if s.queryReplicaReads != nil {
s.queryReplicaReads.counts = enabled
}
}
// Query candidates come from the read store; current task scope/status/version
// are fetched by bounded IDs before API authorization and canClaim computation.
// Missing/changed candidates keep an internal cursor placeholder so deletion
// cannot falsely end pagination. They never enter the public response.
func (s *MongoStore) replicaPoolCandidates(ctx context.Context, target *MongoStore, org string, q domain.HumanItemQuery) ([]domain.HumanItemRecord, error) {
rows, e := target.replicaPoolPageIdentities(ctx, org, q)
if e != nil || len(rows) == 0 {
return rows, e
}
ids := make([]string, 0, len(rows))
for _, r := range rows {
ids = append(ids, r.ID)
}
current := map[string]domain.HumanItemRecord{}
for start := 0; start < len(ids); start += 200 {
end := start + 200
if end > len(ids) {
end = len(ids)
}
cur, e := s.humanItems.Find(ctx, organizationScopedFilter(org, bson.M{"id": bson.M{"$in": ids[start:end]}}), options.Find().SetProjection(humanItemListProjection(domain.HumanItemQuery{PoolCandidateProjection: true})))
if e != nil {
return nil, e
}
for cur.Next(ctx) {
var item domain.HumanItemRecord
if e = cur.Decode(&item); e != nil {
cur.Close(ctx)
return nil, e
}
current[item.ID] = item
}
e = cur.Err()
cur.Close(ctx)
if e != nil {
return nil, e
}
}
for i, old := range rows {
item, exists := current[old.ID]
if !exists || !humanItemMatches(item, q) {
rows[i] = domain.HumanItemRecord{ID: old.ID, CreatedAt: old.CreatedAt, QueryReplicaOmit: true}
continue
}
rows[i] = item
}
return rows, nil
}
type queryPoolPageIdentity struct {
ID string `json:"id"`
CreatedAt string `json:"createdAt"`
}
type queryPoolPage struct {
Key string `json:"key"`
Items []queryPoolPageIdentity `json:"items"`
}
// Cache only discovery identities, never payloads, permissions or canClaim.
// The source still fetches the current page before applying business rules.
func (s *MongoStore) replicaPoolPageIdentities(ctx context.Context, org string, q domain.HumanItemQuery) ([]domain.HumanItemRecord, error) {
if s.SharedQueryCache() == nil || q.Limit <= 0 || q.Limit > 200 {
return s.ListHumanItems(ctx, org, q)
}
epoch, err := s.countFactEpoch(ctx)
if err != nil {
return nil, err
}
key := queryserving.Key("pool-page-identities/v1", s.db.Name(), org, epoch, q)
page, err := s.shareReplicaPoolPage(ctx, key, func(work context.Context) (queryPoolPage, error) {
return s.loadReplicaPoolPage(work, key, org, q)
})
if err != nil {
return nil, err
}
rows := make([]domain.HumanItemRecord, 0, len(page.Items))
for _, item := range page.Items {
rows = append(rows, domain.HumanItemRecord{ID: item.ID, CreatedAt: item.CreatedAt})
}
return rows, nil
}
// Match the existing shared-name lookup contract: only in-flight data reads
// coalesce, one canceled waiter cannot cancel another, and work stays bounded.
func (s *MongoStore) shareReplicaPoolPage(ctx context.Context, key string, read func(context.Context) (queryPoolPage, error)) (queryPoolPage, error) {
if err := ctx.Err(); err != nil {
return queryPoolPage{}, err
}
flight := s.queryReplicaPageFlights.DoChan(key, func() (any, error) {
work, cancel := context.WithTimeout(context.WithoutCancel(ctx), 8*time.Second)
defer cancel()
return read(work)
})
select {
case <-ctx.Done():
return queryPoolPage{}, ctx.Err()
case result := <-flight:
if result.Err != nil {
return queryPoolPage{}, result.Err
}
page := result.Val.(queryPoolPage)
page.Items = append([]queryPoolPageIdentity(nil), page.Items...)
return page, nil
}
}
func (s *MongoStore) loadReplicaPoolPage(ctx context.Context, key, org string, q domain.HumanItemQuery) (queryPoolPage, error) {
var page queryPoolPage
if queryserving.GetJSON(ctx, s.SharedQueryCache(), key, &page) && page.Key == key && len(page.Items) <= q.Limit {
valid := true
for _, item := range page.Items {
if item.ID == "" {
valid = false
break
}
}
if valid {
return page, nil
}
}
rows, err := s.ListHumanItems(ctx, org, q)
if err != nil {
return queryPoolPage{}, err
}
page = queryPoolPage{Key: key, Items: make([]queryPoolPageIdentity, 0, len(rows))}
for _, item := range rows {
page.Items = append(page.Items, queryPoolPageIdentity{ID: item.ID, CreatedAt: item.CreatedAt})
}
if len(rows) <= q.Limit {
queryserving.PutJSON(ctx, s.SharedQueryCache(), key, page, 2*time.Second)
}
return page, nil
}
func filterAuthoritativeAdminPoolPage(candidates, hydrated []domain.HumanItemRecord, q AdminAnnotationPoolQuery) []domain.HumanItemRecord {
byID := make(map[string]domain.HumanItemRecord, len(hydrated))
for _, item := range hydrated {
byID[item.ID] = item
}
out := make([]domain.HumanItemRecord, 0, len(candidates))
for _, cand := range candidates {
item, ok := byID[cand.ID]
if !ok || !adminPoolScope(item) || !adminPoolSelected(item, q) {
continue
}
out = append(out, item)
}
return out
}
func (s *MongoStore) replicaAdminPool(ctx context.Context, target *MongoStore, org string, q AdminAnnotationPoolQuery) ([]domain.HumanItemRecord, error) {
collected := []domain.HumanItemRecord{}
seen := map[string]bool{}
stepQ := q
for pass := 0; pass < 4; pass++ {
if q.Limit > 0 {
need := q.Limit - len(collected)
if need <= 0 {
break
}
if need > stepQ.Limit {
stepQ.Limit = need
} else if stepQ.Limit < q.Limit {
stepQ.Limit = q.Limit
}
}
rows, e := target.ListAdminAnnotationPool(ctx, org, stepQ)
if e != nil {
return nil, e
}
if len(rows) == 0 {
break
}
ids := make([]string, 0, len(rows))
for _, r := range rows {
ids = append(ids, r.ID)
}
current, e := s.hydrateHumanPoolSearch(ctx, org, domain.HumanItemQuery{ListProjection: true}, ids)
if e != nil {
return nil, e
}
if len(current) > 0 {
if e = s.EnrichAnnotationSourceStatuses(ctx, current); e != nil {
return nil, e
}
}
for _, item := range filterAuthoritativeAdminPoolPage(rows, current, q) {
if seen[item.ID] {
continue
}
seen[item.ID] = true
collected = append(collected, item)
if q.Limit > 0 && len(collected) >= q.Limit {
return collected, nil
}
}
if q.Limit <= 0 || len(rows) < stepQ.Limit {
break
}
last := rows[len(rows)-1]
if last.ID == "" {
break
}
stepQ.Cursor = &domain.HumanItemCursor{CreatedAt: last.CreatedAt, ID: last.ID}
}
return collected, nil
}