droboticflow/internal/store/query_replica_source.go

166 lines
5.4 KiB
Go

package store
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"strings"
"time"
)
// All projector source mutations pass this seam. Database grants are scoped to
// collections, not fields; neither raw replacements nor business fields belong
// in this writer. These checks remain on even when optional pacing is disabled.
func validateReplicaMetadataWrite(collection string, model mongo.WriteModel) error {
m, ok := model.(*mongo.UpdateOneModel)
if !ok || m.Upsert != nil && *m.Upsert {
return errors.New("source metadata write rejected")
}
var f, u bson.M
b, e := bson.Marshal(m.Filter)
if e != nil {
return e
}
if e = bson.Unmarshal(b, &f); e != nil {
return e
}
b, e = bson.Marshal(m.Update)
if e != nil {
return e
}
if e = bson.Unmarshal(b, &u); e != nil {
return e
}
if f["_id"] == nil {
return errors.New("source metadata identity required")
}
if id := deletionDoc(f["_id"]); len(id) > 0 {
for key := range id {
if strings.HasPrefix(key, "$") {
return errors.New("metadata identity selector rejected")
}
}
}
for op, raw := range u {
if op != "$set" && op != "$unset" {
return errors.New("source metadata operator rejected")
}
fields := deletionDoc(raw)
if len(fields) == 0 {
return errors.New("empty metadata update")
}
for path := range fields {
allowed := false
if collection == querySyncDeleteCollection {
allowed = op == "$set" && (path == "phase" || path == "expireAt")
} else if queryReplicaFields[collection] != "" {
switch path {
case "_querySync.dirty", "_querySync.revision", "_querySync.signature", "_querySync.mode", "_querySync.producer":
allowed = op == "$set"
}
}
if !allowed {
return errors.New("source business field write rejected")
}
}
}
if queryReplicaFields[collection] != "" && f[querySyncField+".revision"] == nil {
return errors.New("metadata revision CAS required")
}
return nil
}
func (q *QueryReplica) sourceMetadataBulk(ctx context.Context, n string, models []mongo.WriteModel) error {
if len(models) == 0 {
return nil
}
for _, m := range models {
if e := validateReplicaMetadataWrite(n, m); e != nil {
return e
}
}
if _, e := q.sourcePermit(ctx, 0); e != nil {
return e
}
ctx, cancel := q.sourceContext(ctx)
defer cancel()
_, e := q.Source.db.Collection(n).BulkWrite(ctx, models, options.BulkWrite().SetOrdered(false))
return e
}
func (q *QueryReplica) sourceIntentUpdate(ctx context.Context, filter, update bson.M) (*mongo.UpdateResult, error) {
m := mongo.NewUpdateOneModel().SetFilter(filter).SetUpdate(update)
if e := validateReplicaMetadataWrite(querySyncDeleteCollection, m); e != nil {
return nil, e
}
if _, e := q.sourcePermit(ctx, 0); e != nil {
return nil, e
}
ctx, cancel := q.sourceContext(ctx)
defer cancel()
return q.Source.db.Collection(querySyncDeleteCollection).UpdateOne(ctx, filter, update)
}
func (q *QueryReplica) sourceAuditIntent(ctx context.Context, i queryDeleteIntent) error {
if !i.Audit || i.Phase != "reconcile" || queryReplicaFields[i.Collection] == "" || i.ID == "" {
return errors.New("invalid source audit intent")
}
if _, e := q.sourcePermit(ctx, 0); e != nil {
return e
}
ctx, cancel := q.sourceContext(ctx)
defer cancel()
_, e := q.Source.db.Collection(querySyncDeleteCollection).InsertOne(ctx, i)
return e
}
// Cleanup is bounded even for a historical DeleteMany intent. We never replay
// the source delete. A cancelled old writer cannot claim additional rows.
func (q *QueryReplica) recoverSourceClaims(ctx context.Context, i queryDeleteIntent) (bool, error) {
if i.Phase == "prepared" || i.Phase == "deleting" {
now := time.Now().UTC()
if i.LeaseUntil.After(now) {
return false, nil
}
r, e := q.sourceIntentUpdate(ctx, bson.M{"_id": i.ID, "phase": i.Phase, "leaseUntil": bson.M{"$lte": now}}, bson.M{"$set": bson.M{"phase": "cancelled"}})
if e != nil || r.MatchedCount != 1 {
return false, e
}
} else if i.Phase != "complete" && i.Phase != "cancelled" && i.Phase != "reconcile" {
return false, nil
}
limit, e := q.sourcePermit(ctx, q.limit())
if e != nil {
return false, e
}
work, cancel := q.sourceContext(ctx)
defer cancel()
cur, e := q.Source.db.Collection(i.Collection).Find(work, bson.M{querySyncField + ".deleteClaim": i.ID}, options.Find().SetHint("query_sync_delete_claim_v1").SetProjection(bson.M{"_id": 1}).SetLimit(limit).SetBatchSize(int32(limit)).SetMaxTime(1500*time.Millisecond))
if e != nil {
return false, e
}
var rows []bson.M
if e = cur.All(work, &rows); e != nil {
return false, e
}
if len(rows) > 0 {
if e = q.paceRows(ctx, len(rows)); e != nil {
return false, e
}
if _, e = q.sourcePermit(ctx, 0); e != nil {
return false, e
}
ids := bson.A{}
for _, r := range rows {
ids = append(ids, r["_id"])
}
// This is the only UpdateMany: exact bounded identities and exact claim,
// with only its private claim field removed; no upsert or business delete.
work, cancel := q.sourceContext(ctx)
defer cancel()
_, e = q.Source.db.Collection(i.Collection).UpdateMany(work, bson.M{"_id": bson.M{"$in": ids}, querySyncField + ".deleteClaim": i.ID}, bson.M{"$unset": bson.M{querySyncField + ".deleteClaim": ""}})
return false, e
}
_, e = q.sourceIntentUpdate(ctx, bson.M{"_id": i.ID, "phase": bson.M{"$in": bson.A{"complete", "cancelled", "reconcile"}}}, bson.M{"$set": bson.M{"phase": "reconcile"}})
return e == nil, e
}