Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
skudasov committed Jan 21, 2025
1 parent b38076c commit 4e91758
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
11 changes: 9 additions & 2 deletions book/src/framework/components/resources.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Components Resources

You can use `resources` to limit containers CPU/Memory for `NodeSet`, `Blockchain` and `PostgreSQL` components.

```toml
[blockchain_a.resources]
cpus = 0.5
Expand All @@ -19,6 +20,12 @@ Read more about resource constraints [here](https://docs.docker.com/engine/conta

We are using `cpu-period` and `cpu-quota` for simplicity, and because it's working with an arbitrary amount of containers, it is absolute.

Memory swapping is off if you specify `resources` key.
How quota and period works:

- To allocate `1 CPU`, we set `CPUQuota = 100000` and `CPUPeriod = 100000` (1 full period).
- To allocate `0.5 CPU`, we set `CPUQuota = 50000` and `CPUPeriod = 100000`.
- To allocate `2 CPUs`, we set `CPUQuota = 200000` and `CPUPeriod = 100000`.

When the `resources` key is not empty, we disable swap, ensuring the container goes OOM when memory is exhausted, allowing for more precise detection of sudden memory spikes.

Full configuration [example]()
Full configuration [example]()
8 changes: 4 additions & 4 deletions framework/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,22 +322,22 @@ func ExecContainer(containerName string, command []string) (string, error) {
}

type ContainerResources struct {
CPUs float64 `toml:"cpus"`
MemoryMb int `toml:"memory_mb"`
CPUs float64 `toml:"cpus" validate:"gte=0"`
MemoryMb uint `toml:"memory_mb"`
}

// ResourceLimitsFunc returns a function to configure container resources based on the human-readable CPUs and memory in Mb
func ResourceLimitsFunc(h *container.HostConfig, resources *ContainerResources) {
if resources == nil {
return
}
if resources.MemoryMb != 0 {
if resources.MemoryMb > 0 {
h.Memory = int64(resources.MemoryMb) * 1024 * 1024 // Memory in Mb
h.MemoryReservation = int64(resources.MemoryMb) * 1024 * 1024 // Total memory that can be reserved (soft) in Mb
// https://docs.docker.com/engine/containers/resource_constraints/ if both values are equal swap is off, read the docs
h.MemorySwap = h.Memory
}
if resources.CPUs != 0 {
if resources.CPUs > 0 {
// Set CPU limits using CPUQuota and CPUPeriod
// we don't use runtime.NumCPU or docker API to get CPUs because h.CPUShares is relative to amount of containers you run
// CPUPeriod and CPUQuota are absolute and easier to control
Expand Down

0 comments on commit 4e91758

Please sign in to comment.