Skip to content

Commit

Permalink
Allow to specify total memory on agent configuration
Browse files Browse the repository at this point in the history
Allow to set the total memory of an agent in its configuration file. This
can be used in case the automatic detection doesn't work or in specific
environments when memory overcommit (using swap for example) can be
desirable.
  • Loading branch information
mildred committed Jan 22, 2018
1 parent a2eecfd commit 0156db7
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 8 deletions.
4 changes: 4 additions & 0 deletions client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ type Config struct {
// dynamically. It should be given as Cores * MHz (2 Cores * 2 Ghz = 4000)
CpuCompute int

// MemoryMB is the default node total memory if it cannot be determined
// dynamically.
MemoryMB int

// MaxKillTimeout allows capping the user-specifiable KillTimeout. If the
// task's KillTimeout is greater than the MaxKillTimeout, MaxKillTimeout is
// used.
Expand Down
25 changes: 17 additions & 8 deletions client/fingerprint/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,28 @@ func NewMemoryFingerprint(logger *log.Logger) Fingerprint {
}

func (f *MemoryFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
memInfo, err := mem.VirtualMemory()
if err != nil {
f.logger.Printf("[WARN] Error reading memory information: %s", err)
return false, err
}
var totalMemory int

if cfg.MemoryMB != 0 {
totalMemory = cfg.MemoryMB
} else {
memInfo, err := mem.VirtualMemory()
if err != nil {
f.logger.Printf("[WARN] Error reading memory information: %s", err)
return false, err
}

if memInfo.Total > 0 {
node.Attributes["memory.totalbytes"] = fmt.Sprintf("%d", memInfo.Total)
if memInfo.Total > 0 {
node.Attributes["memory.totalbytes"] = fmt.Sprintf("%d", memInfo.Total)
totalMemory = int(memInfo.Total / 1024 / 1024)
}
}

if totalMemory > 0 {
if node.Resources == nil {
node.Resources = &structs.Resources{}
}
node.Resources.MemoryMB = int(memInfo.Total / 1024 / 1024)
node.Resources.MemoryMB = totalMemory
}

return true, nil
Expand Down
3 changes: 3 additions & 0 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ func (a *Agent) clientConfig() (*clientconfig.Config, error) {
if a.config.Client.CpuCompute != 0 {
conf.CpuCompute = a.config.Client.CpuCompute
}
if a.config.Client.MemoryMB != 0 {
conf.MemoryMB = a.config.Client.MemoryMB
}
if a.config.Client.MaxKillTimeout != "" {
dur, err := time.ParseDuration(a.config.Client.MaxKillTimeout)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions command/agent/config-test-fixtures/basic.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ client {
network_interface = "eth0"
network_speed = 100
cpu_total_compute = 4444
total_memory = 5555
reserved {
cpu = 10
memory = 10
Expand Down
6 changes: 6 additions & 0 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ type ClientConfig struct {
// CpuCompute is used to override any detected or default total CPU compute.
CpuCompute int `mapstructure:"cpu_total_compute"`

// MemoryMB is used to override any detected or default total memory.
MemoryMB int `mapstructure:"total_memory"`

// MaxKillTimeout allows capping the user-specifiable KillTimeout.
MaxKillTimeout string `mapstructure:"max_kill_timeout"`

Expand Down Expand Up @@ -1079,6 +1082,9 @@ func (a *ClientConfig) Merge(b *ClientConfig) *ClientConfig {
if b.CpuCompute != 0 {
result.CpuCompute = b.CpuCompute
}
if b.MemoryMB != 0 {
result.MemoryMB = b.MemoryMB
}
if b.MaxKillTimeout != "" {
result.MaxKillTimeout = b.MaxKillTimeout
}
Expand Down
1 change: 1 addition & 0 deletions command/agent/config_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ func parseClient(result **ClientConfig, list *ast.ObjectList) error {
"chroot_env",
"network_interface",
"network_speed",
"total_memory",
"cpu_total_compute",
"max_kill_timeout",
"client_max_port",
Expand Down
1 change: 1 addition & 0 deletions command/agent/config_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func TestConfig_Parse(t *testing.T) {
NetworkInterface: "eth0",
NetworkSpeed: 100,
CpuCompute: 4444,
MemoryMB: 5555,
MaxKillTimeout: "10s",
ClientMinPort: 1000,
ClientMaxPort: 2000,
Expand Down
2 changes: 2 additions & 0 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestConfig_Merge(t *testing.T) {
},
NetworkSpeed: 100,
CpuCompute: 100,
MemoryMB: 200,
MaxKillTimeout: "20s",
ClientMaxPort: 19996,
Reserved: &Resources{
Expand Down Expand Up @@ -227,6 +228,7 @@ func TestConfig_Merge(t *testing.T) {
ClientMinPort: 22000,
NetworkSpeed: 105,
CpuCompute: 105,
MemoryMB: 205,
MaxKillTimeout: "50s",
Reserved: &Resources{
CPU: 15,
Expand Down

0 comments on commit 0156db7

Please sign in to comment.