Documentation
¶
Overview ¶
Package protector provides a set of protectors that stop the query services when the resource usage exceeds the limit.
Index ¶
- Constants
- Variables
- func CountMemories() int
- func ForceHighByIndex(i int, high bool) bool
- func QueryFailure(cause error, format string, args ...any) *common.Error
- func SetHostMemoryForTest(totalBytes uint64) (restore func())
- func SetNextQueryPool(poolBytes uint64)
- func SetQueryPoolAfter(skip int, poolBytes uint64)
- func WaitWhileHigh(ctx context.Context, pm Memory, maxWait time.Duration, l *logger.Logger, ...) error
- type ExhaustedError
- 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) AdmitMeasureContext(ctx context.Context) (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
- func (r *QueryReservation) Charge(bytes uint64) error
- func (r *QueryReservation) ChargeResult(bytes uint64) error
- func (r *QueryReservation) Limit() uint64
- func (r *QueryReservation) Owner() any
- func (r *QueryReservation) Peak() uint64
- func (r *QueryReservation) Refund(bytes uint64)
- func (r *QueryReservation) Used() uint64
- type State
Constants ¶
const ( // ReasonPerQueryLimit means the query's own assignment was used up. ReasonPerQueryLimit = "per_query_limit" ReasonSharedPool = "shared_pool" // ReasonNodeHeadroom means the protector's sampled available memory left no room. ReasonNodeHeadroom = "node_headroom" // ReasonResultWindow means the query retained the most results one query may. ReasonResultWindow = "result_window" // ReasonReleased means the reservation had already been released. ReasonReleased = "released" )
Reasons an allowance was exhausted, as carried by ExhaustedError.
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 CountMemories ¶ added in v0.12.0
func CountMemories() int
CountMemories returns the number of protectors created by NewMemory in this process. It is intended for tests.
func ForceHighByIndex ¶ added in v0.12.0
ForceHighByIndex makes the i-th protector created by NewMemory report StateHigh while high is true, regardless of its real usage. It reports whether such a protector exists. It is intended for tests.
func QueryFailure ¶ added in v0.12.0
QueryFailure builds the error response of a query. A query that ran out of its memory allowance carries STATUS_RESOURCE_EXHAUSTED, so the caller can tell it apart from an internal error and report RESOURCE_EXHAUSTED to the client. The message is formatted from format and args.
func SetHostMemoryForTest ¶ added in v0.11.2
func SetHostMemoryForTest(totalBytes uint64) (restore func())
SetHostMemoryForTest makes the query budget see totalBytes as the host's memory, whatever the machine has, and returns a function that restores the previous reading. Zero stands for unreadable host memory, which gives the fixed 64 MiB fallback pool (8 MiB per query). It is intended for tests.
func SetNextQueryPool ¶ added in v0.12.0
func SetNextQueryPool(poolBytes uint64)
SetNextQueryPool makes the next protector created by NewMemory size its query pool to poolBytes (so a query may reserve at most an eighth of it) and treat all of that pool as available, whatever the process uses. An in-process test calls it right before it starts the node whose queries it wants to run out of budget. It is intended for tests.
func SetQueryPoolAfter ¶ added in v0.12.0
SetQueryPoolAfter is SetNextQueryPool for the protector NewMemory creates after skip others. A data node starts an embedded schema server that creates its own protector first, so a test that wants the data node itself to run out of budget skips that one. It is intended for tests.
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 ExhaustedError ¶ added in v0.12.0
ExhaustedError says which allowance ran out and by how much. It matches ErrQueryResourceExhausted under errors.Is, so callers that test for exhaustion are unchanged. Limit, Used and Requested are bytes, except for ReasonResultWindow, where they count results; for ReasonReleased only Requested is set.
func ExhaustionReason ¶ added in v0.12.0
func ExhaustionReason(err error) (*ExhaustedError, bool)
ExhaustionReason returns the *ExhaustedError in err's chain, which names the allowance that ran out and its figures, or false when err carries none.
func (*ExhaustedError) Error ¶ added in v0.12.0
func (e *ExhaustedError) Error() string
func (*ExhaustedError) Unwrap ¶ added in v0.12.0
func (e *ExhaustedError) Unwrap() error
Unwrap reports ErrQueryResourceExhausted as the cause.
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 the assignment 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) AdmitMeasureContext ¶ added in v0.12.0
AdmitMeasureContext admits a measure scan with incremental shared-pool assignment. The hard per-query limit is unchanged; successful charges grow the pool commitment monotonically until the last query or transport owner releases it.
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) Peak ¶ added in v0.12.0
func (r *QueryReservation) Peak() uint64
Peak returns the most bytes the reservation has held at once.
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. An incremental reservation also hands the bytes back to the shared pool. 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).