forked from longtao.wu/droboticflow
86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"golang.org/x/time/rate"
|
|
)
|
|
|
|
const queryBuildBatchBytes = 64 << 10
|
|
|
|
// This admission budget belongs ONLY to a verified independent read-store
|
|
// builder. It never gates authority commands or customer requests. It leaves
|
|
// IO headroom for journal/control writes; the kernel hard limits still apply.
|
|
type queryBuildWriteGate struct {
|
|
limiter *rate.Limiter
|
|
mu sync.Mutex
|
|
next time.Time
|
|
checkpoint bool
|
|
inspect func(context.Context) (bool, error)
|
|
}
|
|
|
|
func (s *MongoStore) newQueryBuildWriteGate() *queryBuildWriteGate {
|
|
return &queryBuildWriteGate{limiter: rate.NewLimiter(512<<10, queryBuildBatchBytes), inspect: func(ctx context.Context) (bool, error) {
|
|
var state struct {
|
|
WT struct {
|
|
Checkpoint struct {
|
|
Progress int `bson:"progress state"`
|
|
} `bson:"checkpoint"`
|
|
Transaction struct {
|
|
Running int `bson:"transaction checkpoint currently running"`
|
|
} `bson:"transaction"`
|
|
} `bson:"wiredTiger"`
|
|
}
|
|
err := s.db.Client().Database("admin").RunCommand(ctx, bson.M{"serverStatus": 1}).Decode(&state)
|
|
return state.WT.Checkpoint.Progress != 0 || state.WT.Transaction.Running != 0, err
|
|
}}
|
|
}
|
|
|
|
func (g *queryBuildWriteGate) wait(ctx context.Context, bytes int) error {
|
|
if g == nil {
|
|
return ctx.Err()
|
|
}
|
|
for bytes > 0 {
|
|
n := min(bytes, queryBuildBatchBytes)
|
|
if err := g.limiter.WaitN(ctx, n); err != nil {
|
|
return err
|
|
}
|
|
bytes -= n
|
|
}
|
|
for {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
g.mu.Lock()
|
|
if !time.Now().Before(g.next) {
|
|
probe, stop := context.WithTimeout(ctx, 250*time.Millisecond)
|
|
busy, err := g.inspect(probe)
|
|
stop()
|
|
// Unsupported metrics/monitor privileges do not become another
|
|
// admission certificate. Keep byte pacing, never block core work.
|
|
g.checkpoint = err == nil && busy
|
|
g.next = time.Now().Add(time.Second)
|
|
}
|
|
busy, next := g.checkpoint, g.next
|
|
g.mu.Unlock()
|
|
if !busy {
|
|
break
|
|
}
|
|
timer := time.NewTimer(max(time.Millisecond, time.Until(next)))
|
|
select {
|
|
case <-ctx.Done():
|
|
timer.Stop()
|
|
return ctx.Err()
|
|
case <-timer.C:
|
|
}
|
|
}
|
|
return ctx.Err()
|
|
}
|
|
|
|
func (s *MongoStore) paceQueryBuild(ctx context.Context, bytes int) error {
|
|
return s.queryBuildWrites.wait(ctx, bytes)
|
|
}
|