Hyper.Node.Budget.Hard (Hyper v0.1.0)

Copy Markdown View Source

Hard per-node resource accounting. One Hard runs per BEAM node (named __MODULE__, started under Hyper.Node.Budget.Supervisor) and tracks how much memory and disk the VMs scheduled onto this machine have reserved.

"Hard" means the limits are inviolable: a reservation that would push the running total past the node's configured mem_max/disk_max (Hyper.Cfg.Budget) is refused outright. Callers reserve through with_budget/2, which holds the budget for the duration of the callback and releases it afterwards.

Summary

Functions

Can this node run the given vm spec? :ok if yes, {:error, reason} otherwise.

Returns a specification to start this module under a supervisor.

Configured caps minus what is currently reserved.

Reserve spec's budget for the lifetime of owner.

Reserve vm_spec's budget, run callable, and release the budget afterwards.

Functions

can_run(vm_spec)

@spec can_run(Hyper.Vm.Instance.Spec.t()) :: :ok | {:error, term()}

Can this node run the given vm spec? :ok if yes, {:error, reason} otherwise.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

headroom()

@spec headroom() :: %{mem: Unit.Information.t(), disk: Unit.Information.t()}

Configured caps minus what is currently reserved.

reserve(spec, owner)

@spec reserve(Hyper.Vm.Instance.Spec.t(), pid()) :: :ok | {:error, term()}

Reserve spec's budget for the lifetime of owner.

Atomic: refuses ({:error, reason}) if spec does not fit remaining headroom. On success the reservation releases automatically when owner dies.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

with_budget(vm_spec, callable)

@spec with_budget(Hyper.Vm.Instance.Spec.t(), (-> result)) ::
  result | {:error, term()}
when result: var

Reserve vm_spec's budget, run callable, and release the budget afterwards.

Returns callable's value if the reservation succeeds, or {:error, reason} if the node cannot fit the spec. The budget is released even if callable raises.