Skip to content

Commit 4e91758

Browse files
committed
review fixes
1 parent b38076c commit 4e91758

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

book/src/framework/components/resources.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Components Resources
22

33
You can use `resources` to limit containers CPU/Memory for `NodeSet`, `Blockchain` and `PostgreSQL` components.
4+
45
```toml
56
[blockchain_a.resources]
67
cpus = 0.5
@@ -19,6 +20,12 @@ Read more about resource constraints [here](https://docs.docker.com/engine/conta
1920

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

22-
Memory swapping is off if you specify `resources` key.
23+
How quota and period works:
24+
25+
- To allocate `1 CPU`, we set `CPUQuota = 100000` and `CPUPeriod = 100000` (1 full period).
26+
- To allocate `0.5 CPU`, we set `CPUQuota = 50000` and `CPUPeriod = 100000`.
27+
- To allocate `2 CPUs`, we set `CPUQuota = 200000` and `CPUPeriod = 100000`.
28+
29+
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.
2330

24-
Full configuration [example]()
31+
Full configuration [example]()

framework/docker.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,22 +322,22 @@ func ExecContainer(containerName string, command []string) (string, error) {
322322
}
323323

324324
type ContainerResources struct {
325-
CPUs float64 `toml:"cpus"`
326-
MemoryMb int `toml:"memory_mb"`
325+
CPUs float64 `toml:"cpus" validate:"gte=0"`
326+
MemoryMb uint `toml:"memory_mb"`
327327
}
328328

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

0 commit comments

Comments
 (0)