Skip to content
This repository was archived by the owner on Apr 29, 2020. It is now read-only.

Commit 448d860

Browse files
Merge pull request #339 from anthonybishopric/abishopric/manifest_memory
Allow shorthand for memory limits.
2 parents 98acbf1 + 5b5c004 commit 448d860

File tree

5 files changed

+75
-6
lines changed

5 files changed

+75
-6
lines changed

pkg/cgroups/cgroups.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,11 @@ func (subsys Subsystems) Write(config Config) error {
169169
if err != nil {
170170
return err
171171
}
172-
return subsys.SetMemory(config.Name, config.Memory)
172+
memory, err := config.MemoryByteCount()
173+
if err != nil {
174+
return err
175+
}
176+
return subsys.SetMemory(config.Name, int(memory))
173177
}
174178

175179
func (subsys Subsystems) AddPID(name string, pid int) error {

pkg/cgroups/config.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@ package cgroups
22

33
import (
44
"fmt"
5+
6+
"github.com/square/p2/pkg/util/size"
57
)
68

79
type Config struct {
810
Name string `yaml:"-"` // The name of the cgroup in cgroupfs
911
CPUs int `yaml:"cpus,omitempty"` // The number of logical CPUs
10-
Memory int `yaml:"memory,omitempty"` // The number of bytes of memory
12+
Memory string `yaml:"memory,omitempty"` // The number of bytes of memory
13+
}
14+
15+
func NewCGroup(cpus int, memory size.ByteCount) Config {
16+
return Config{
17+
CPUs: cpus,
18+
Memory: memory.String(),
19+
}
20+
}
21+
22+
func (config Config) MemoryByteCount() (size.ByteCount, error) {
23+
if config.Memory == "" {
24+
return size.Byte * 0, nil
25+
}
26+
return size.Parse(config.Memory)
1127
}
1228

1329
func (config Config) CgexecArgs() []string {

pkg/cgroups/config_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cgroups
2+
3+
import (
4+
"testing"
5+
6+
"github.com/square/p2/pkg/util/size"
7+
8+
. "github.com/square/p2/Godeps/_workspace/src/github.com/anthonybishopric/gotcha"
9+
"github.com/square/p2/Godeps/_workspace/src/gopkg.in/yaml.v2"
10+
)
11+
12+
func TestMarshalIntegerByteCount(t *testing.T) {
13+
integeredMemory := []byte(`memory: 500`)
14+
config := Config{}
15+
yaml.Unmarshal(integeredMemory, &config)
16+
memory, err := config.MemoryByteCount()
17+
Assert(t).IsNil(err, "Should not have erred getting the byte count")
18+
Assert(t).AreEqual(memory, size.ByteCount(500), "Should have unmarshaled the integer representation of bytes")
19+
}
20+
21+
func TestMarshalStringByteCount(t *testing.T) {
22+
integeredMemory := []byte(`memory: 500G`)
23+
config := Config{}
24+
yaml.Unmarshal(integeredMemory, &config)
25+
memory, err := config.MemoryByteCount()
26+
Assert(t).IsNil(err, "Should not have erred getting the byte count")
27+
Assert(t).AreEqual(memory, 500*size.Gibibyte, "Should have unmarshaled the integer representation of bytes")
28+
}

pkg/pods/manifest_test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"runtime"
77
"testing"
88

9+
"github.com/square/p2/pkg/cgroups"
10+
"github.com/square/p2/pkg/util/size"
11+
912
. "github.com/square/p2/Godeps/_workspace/src/github.com/anthonybishopric/gotcha"
1013
)
1114

@@ -35,6 +38,9 @@ launchables:
3538
launchable_type: hoist
3639
launchable_id: web
3740
location: https://localhost:4444/foo/bar/baz.tar.gz
41+
cgroup:
42+
cpus: 4
43+
memory: 1.0G
3844
config:
3945
ENVIRONMENT: staging
4046
status_port: 8000
@@ -76,6 +82,7 @@ func TestPodManifestCanBeWritten(t *testing.T) {
7682
LaunchableType: "hoist",
7783
LaunchableId: "web",
7884
Location: "https://localhost:4444/foo/bar/baz.tar.gz",
85+
CgroupConfig: cgroups.NewCGroup(4, 1*size.Gibibyte),
7986
},
8087
}
8188
builder.SetLaunchables(launchables)
@@ -107,11 +114,25 @@ func TestPodManifestCanWriteItsConfigStanzaSeparately(t *testing.T) {
107114

108115
func TestPodManifestCanReportItsSHA(t *testing.T) {
109116
config := testPod()
110-
manifest, err := ManifestFromBytes(bytes.NewBufferString(config).Bytes())
117+
manifest, err := ManifestFromBytes([]byte(config))
111118
Assert(t).IsNil(err, "should not have erred when building manifest")
112119
val, err := manifest.SHA()
113120
Assert(t).IsNil(err, "should not have erred when getting SHA")
114-
Assert(t).AreEqual("17acfa1ce4bdd9674524f8faed383bf365d168c81d9d981d63173a33a7fed5a1", val, "SHA mismatched expectations")
121+
Assert(t).AreEqual("b3b8aa6c2e7b52ace2fd4b524d84aaa71bc39eb0ef7a254ffe1752011a84e97a", val, "SHA mismatched expectations - if this was expected, change the assertion value")
122+
}
123+
124+
func TestPodManifestLaunchablesCGroups(t *testing.T) {
125+
config := testPod()
126+
manifest, _ := ManifestFromBytes([]byte(config))
127+
launchables := manifest.GetLaunchableStanzas()
128+
Assert(t).AreEqual(len(launchables), 1, "Expected exactly one launchable in the manifest")
129+
for _, launchable := range launchables {
130+
cgroup := launchable.CgroupConfig
131+
Assert(t).AreEqual(cgroup.CPUs, 4, "Expected cgroup to have 4 CPUs")
132+
memory, err := cgroup.MemoryByteCount()
133+
Assert(t).IsNil(err, "Should not have erred parsing cgroup memory size")
134+
Assert(t).AreEqual(memory, 1*size.Gibibyte, "Should have matched on memory bytecount")
135+
}
115136
}
116137

117138
func TestNilPodManifestHasEmptySHA(t *testing.T) {

pkg/pods/pod_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ launchables:
9191
location: https://localhost:4444/foo/bar/baz.tar.gz
9292
cgroup:
9393
cpus: 4
94-
memory: 4294967296
94+
memory: 4G
9595
config:
9696
ENVIRONMENT: staging
9797
`
@@ -134,7 +134,7 @@ config:
134134
expectedPlatConfig := `web:
135135
cgroup:
136136
cpus: 4
137-
memory: 4294967296
137+
memory: 4G
138138
`
139139
Assert(t).AreEqual(expectedPlatConfig, string(platConfig), "the platform config didn't match")
140140

0 commit comments

Comments
 (0)