Documentation
¶
Overview ¶
Package protector provides a set of protectors that stop the query services when the resource usage exceeds the limit.
Index ¶
- Variables
- func SetHostMemoryForTest(total func() uint64) (restore func())
- func WaitWhileHigh(ctx context.Context, pm Memory, maxWait time.Duration, l *logger.Logger, ...) error
- type Memory
- type Nop
- func (Nop) AcquireResource(_ context.Context, _ uint64) error
- func (Nop) AvailableBytes() int64
- func (Nop) FlagSet() *run.FlagSet
- func (Nop) GetLimit() uint64
- func (Nop) GracefulStop()
- func (Nop) Name() string
- func (Nop) PreRun(context.Context) error
- func (Nop) Serve() run.StopNotify
- func (Nop) ShouldCache(int64) bool
- func (n Nop) State() State
- func (Nop) Validate() error
- type QueryBudget
- func (b *QueryBudget) Admit(ctx context.Context, limit, offset, defaultLimit uint32) (func(), error)
- func (b *QueryBudget) AdmitContext(ctx context.Context, limit, offset, defaultLimit uint32) (context.Context, func(), error)
- func (b *QueryBudget) AdmitRequestContext(ctx context.Context, limit, offset, defaultLimit uint32) (context.Context, func(), error)
- func (b *QueryBudget) AdmitScanContext(ctx context.Context) (context.Context, func(), error)
- func (b *QueryBudget) Limit() uint64
- func (b *QueryBudget) MaxQuery() uint64
- func (b *QueryBudget) Reserve(ctx context.Context, bytes uint64) (func(), error)
- func (b *QueryBudget) Reserved() uint64
- type QueryReservation
- type State
Constants ¶
This section is empty.
Variables ¶
var ( // ErrQueryTooLarge indicates that a request exceeds the configured hard limit. ErrQueryTooLarge = errors.New("query memory request exceeds per-query limit") // ErrQueryResourceExhausted indicates exhaustion of a query allowance or the shared pool. ErrQueryResourceExhausted = errors.New("query memory pool exhausted") )
Functions ¶
func SetHostMemoryForTest ¶ added in v0.11.2
func SetHostMemoryForTest(total func() uint64) (restore func())
SetHostMemoryForTest replaces the host memory reader used to size the default query pool and returns a function restoring the previous reader.
func WaitWhileHigh ¶ added in v0.11.0
func WaitWhileHigh(ctx context.Context, pm Memory, maxWait time.Duration, l *logger.Logger, stage string) error
WaitWhileHigh blocks while memory pressure is high, bounded by maxWait. It only creates a timeout context when it actually has to wait. It returns the timeout/cancel error if ctx or maxWait elapses before recovery.
Types ¶
type Memory ¶
type Memory interface {
AvailableBytes() int64
GetLimit() uint64
AcquireResource(ctx context.Context, size uint64) error
// ShouldCache returns true if the file size is smaller than the threshold.
ShouldCache(fileSize int64) bool
run.PreRunner
run.Config
run.Service
// State returns the current memory pressure state.
State() State
}
Memory is an interface for monitoring and limiting memory usage to prevent OOM.
func NewMemory ¶
func NewMemory(omr observability.MetricsRegistry) Memory
NewMemory creates a new Memory protector.
type Nop ¶ added in v0.9.0
type Nop struct{}
Nop is a no-op implementation of Memory interface for testing.
func (Nop) AcquireResource ¶ added in v0.9.0
AcquireResource always succeeds.
func (Nop) AvailableBytes ¶ added in v0.9.0
AvailableBytes always returns -1 (unlimited).
func (Nop) Serve ¶ added in v0.9.0
func (Nop) Serve() run.StopNotify
Serve returns a closed channel.
func (Nop) ShouldCache ¶ added in v0.9.0
ShouldCache always returns false.
type QueryBudget ¶ added in v0.11.1
type QueryBudget struct {
// contains filtered or unexported fields
}
QueryBudget provides an atomic, finite reservation pool for one service. Reservations are estimates and must remain held until response materialization is complete. It intentionally does not modify Memory.AcquireResource semantics.
func NewQueryBudget ¶ added in v0.11.1
func NewQueryBudget(pm Memory) *QueryBudget
NewQueryBudget derives a bounded query pool from the protector. A disabled or unavailable protector uses a finite fallback pool rather than becoming unlimited.
func QueryBudgetFor ¶ added in v0.11.1
func QueryBudgetFor(pm Memory) *QueryBudget
QueryBudgetFor returns the process-wide budget associated with a protector. It avoids independent pools for the liaison's public services.
func (*QueryBudget) Admit ¶ added in v0.11.1
func (b *QueryBudget) Admit(ctx context.Context, limit, offset, defaultLimit uint32) (func(), error)
Admit reserves the complete dynamic assignment for a query after deriving a result-window ceiling from current sampled memory and outstanding reservations.
func (*QueryBudget) AdmitContext ¶ added in v0.11.1
func (b *QueryBudget) AdmitContext(ctx context.Context, limit, offset, defaultLimit uint32) (context.Context, func(), error)
AdmitContext admits a query and attaches its chargeable reservation to ctx.
func (*QueryBudget) AdmitRequestContext ¶ added in v0.11.1
func (b *QueryBudget) AdmitRequestContext(ctx context.Context, limit, offset, defaultLimit uint32) (context.Context, func(), error)
AdmitRequestContext admits a query window, treating MaxUint32 with zero offset as an unbounded scan.
func (*QueryBudget) AdmitScanContext ¶ added in v0.11.1
AdmitScanContext admits an incremental scan without reserving from its requested limit. Callers must charge every retained result with query.ChargeResult before allocation.
func (*QueryBudget) Limit ¶ added in v0.11.1
func (b *QueryBudget) Limit() uint64
Limit returns the finite pool size available to all admitted queries.
func (*QueryBudget) MaxQuery ¶ added in v0.11.1
func (b *QueryBudget) MaxQuery() uint64
MaxQuery returns the hard per-query reservation limit.
func (*QueryBudget) Reserve ¶ added in v0.11.1
func (b *QueryBudget) Reserve(ctx context.Context, bytes uint64) (func(), error)
Reserve atomically reserves bytes, returning a release function. It rejects oversized requests and pressure, and honors cancellation before admission.
func (*QueryBudget) Reserved ¶ added in v0.11.1
func (b *QueryBudget) Reserved() uint64
Reserved returns the current total of outstanding query reservations.
type QueryReservation ¶ added in v0.11.1
type QueryReservation struct {
// contains filtered or unexported fields
}
QueryReservation tracks charged allocations and shared ownership of an admitted query.
func (*QueryReservation) Charge ¶ added in v0.11.1
func (r *QueryReservation) Charge(bytes uint64) error
Charge accounts bytes before allocation, rejecting charges beyond the assignment.
func (*QueryReservation) ChargeResult ¶ added in v0.11.1
func (r *QueryReservation) ChargeResult(bytes uint64) error
ChargeResult accounts one retained result and its bytes before allocation. Exhausting either allowance rejects the result without changing either counter.
func (*QueryReservation) Limit ¶ added in v0.11.1
func (r *QueryReservation) Limit() uint64
Limit returns the reservation's assigned byte budget.
func (*QueryReservation) Owner ¶ added in v0.11.1
func (r *QueryReservation) Owner() any
Owner identifies the shared query pool owning this reservation.
func (*QueryReservation) Refund ¶ added in v0.11.2
func (r *QueryReservation) Refund(bytes uint64)
Refund returns bytes previously charged to the reservation, never taking its usage below zero. It does not affect the retained-result counter.
func (*QueryReservation) Used ¶ added in v0.11.1
func (r *QueryReservation) Used() uint64
Used returns the bytes currently charged to the reservation (charges minus refunds).