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 24, 2018
1 parent f5d0b87 commit 2be24f0
Show file tree
Hide file tree
Showing 8 changed files with 92 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 in megabytes 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
26 changes: 18 additions & 8 deletions client/fingerprint/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,29 @@ 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
node.Attributes["memory.totalbytes"] = fmt.Sprintf("%d", totalMemory*1024*1024)
} 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
3 changes: 3 additions & 0 deletions command/agent/config-test-fixtures/non-optional.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
client {
memory_total_mb = 5555
}
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:"memory_total_mb"`

// 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",
"memory_total_mb",
"cpu_total_compute",
"max_kill_timeout",
"client_max_port",
Expand Down
55 changes: 55 additions & 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: 0,
MaxKillTimeout: "10s",
ClientMinPort: 1000,
ClientMaxPort: 2000,
Expand Down Expand Up @@ -200,6 +201,60 @@ func TestConfig_Parse(t *testing.T) {
},
false,
},
{
"non-optional.hcl",
&Config{
Region: "",
Datacenter: "",
NodeName: "",
DataDir: "",
LogLevel: "",
BindAddr: "",
EnableDebug: false,
Ports: nil,
Addresses: nil,
AdvertiseAddrs: nil,
Client: &ClientConfig{
Enabled: false,
StateDir: "",
AllocDir: "",
Servers: []string{},
NodeClass: "",
Meta: map[string]string{},
Options: map[string]string{},
ChrootEnv: map[string]string{},
NetworkInterface: "",
NetworkSpeed: 0,
CpuCompute: 0,
MemoryMB: 5555,
MaxKillTimeout: "",
ClientMinPort: 0,
ClientMaxPort: 0,
Reserved: nil,
GCInterval: 0,
GCParallelDestroys: 0,
GCDiskUsageThreshold: 0,
GCInodeUsageThreshold: 0,
GCMaxAllocs: 0,
NoHostUUID: nil,
},
Server: nil,
ACL: nil,
Telemetry: nil,
LeaveOnInt: false,
LeaveOnTerm: false,
EnableSyslog: false,
SyslogFacility: "",
DisableUpdateCheck: false,
DisableAnonymousSignature: false,
Consul: nil,
Vault: nil,
TLSConfig: nil,
HTTPAPIResponseHeaders: map[string]string{},
Sentinel: nil,
},
false,
},
}

for _, tc := range cases {
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: 100,
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: 105,
MaxKillTimeout: "50s",
Reserved: &Resources{
CPU: 15,
Expand Down

0 comments on commit 2be24f0

Please sign in to comment.