Merge pull request #6239 from hashicorp/b-32bitmem

Fix memory fingerprinting on 32bit
This commit is contained in:
Danielle 2019-09-04 17:39:07 +02:00 committed by GitHub
commit 2564a1dabd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 24 deletions

View file

@ -54,6 +54,13 @@ workflows:
name: "test-shared-exec" name: "test-shared-exec"
test_packages: "./drivers/shared/executor" test_packages: "./drivers/shared/executor"
<<: *IGNORE_FOR_UI_BRANCHES <<: *IGNORE_FOR_UI_BRANCHES
- test-machine:
name: "test-32bit"
# Currently we only explicitly test fingerprinting on 32bit
# architectures.
test_packages: "./client/fingerprint"
goarch: "386"
<<: *IGNORE_FOR_UI_BRANCHES
- test-rkt: - test-rkt:
<<: *IGNORE_FOR_UI_BRANCHES <<: *IGNORE_FOR_UI_BRANCHES
- test-e2e: - test-e2e:
@ -118,11 +125,15 @@ jobs:
exclude_packages: exclude_packages:
type: string type: string
default: "" default: ""
goarch:
type: string
default: "amd64"
environment: environment:
<<: *COMMON_ENVS <<: *COMMON_ENVS
GOTEST_PKGS: "<< parameters.test_packages >>" GOTEST_PKGS: "<< parameters.test_packages >>"
GOTEST_PKGS_EXCLUDE: "<< parameters.exclude_packages >>" GOTEST_PKGS_EXCLUDE: "<< parameters.exclude_packages >>"
GOPATH: /go GOPATH: /go
GOTESTARCH: "<< parameters.goarch >>"
steps: steps:
- checkout - checkout
- run: make deps - run: make deps
@ -191,17 +202,30 @@ jobs:
executor: executor:
type: string type: string
default: "go-machine-recent" default: "go-machine-recent"
goarch:
type: string
default: "amd64"
environment: environment:
<<: *COMMON_ENVS <<: *COMMON_ENVS
GOTEST_PKGS_EXCLUDE: "<< parameters.exclude_packages >>" GOTEST_PKGS_EXCLUDE: "<< parameters.exclude_packages >>"
GOTEST_PKGS: "<< parameters.test_packages >>" GOTEST_PKGS: "<< parameters.test_packages >>"
GOPATH: /home/circleci/go GOPATH: /home/circleci/go
GOTESTARCH: "<< parameters.goarch >>"
steps: steps:
- checkout - checkout
- install-golang - install-golang
- install-protoc - install-protoc
- install-consul - install-consul
- install-vault - install-vault
- run:
name: Install 32bit gcc libs
command: |
if [ ! -z $GOTESTARCH ] && [ $GOTESTARCH == "386" ]; then
sudo apt-get update
sudo apt-get install -y gcc-multilib
else
echo "Skipping 32bit lib installation while building for not 386"
fi
- run: PATH="$GOPATH/bin:/usr/local/go/bin:$PATH" make bootstrap - run: PATH="$GOPATH/bin:/usr/local/go/bin:$PATH" make bootstrap
- run-tests - run-tests
- store_test_results: - store_test_results:
@ -330,6 +354,10 @@ commands:
unset GOTEST_PKGS unset GOTEST_PKGS
fi fi
if [ ! -z $GOTESTARCH ]; then
export GOARCH="$GOTESTARCH";
fi
mkdir -p /tmp/test-reports mkdir -p /tmp/test-reports
sudo -E PATH="$GOPATH/bin:/usr/local/go/bin:$PATH" make generate-structs sudo -E PATH="$GOPATH/bin:/usr/local/go/bin:$PATH" make generate-structs
sudo -E PATH="$GOPATH/bin:/usr/local/go/bin:$PATH" make test-nomad sudo -E PATH="$GOPATH/bin:/usr/local/go/bin:$PATH" make test-nomad

View file

@ -8,7 +8,7 @@ import (
"github.com/shirou/gopsutil/mem" "github.com/shirou/gopsutil/mem"
) )
const bytesInMB = 1024 * 1024 const bytesInMB int64 = 1024 * 1024
// MemoryFingerprint is used to fingerprint the available memory on the node // MemoryFingerprint is used to fingerprint the available memory on the node
type MemoryFingerprint struct { type MemoryFingerprint struct {
@ -25,10 +25,10 @@ func NewMemoryFingerprint(logger log.Logger) Fingerprint {
} }
func (f *MemoryFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintResponse) error { func (f *MemoryFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintResponse) error {
var totalMemory int var totalMemory int64
cfg := req.Config cfg := req.Config
if cfg.MemoryMB != 0 { if cfg.MemoryMB != 0 {
totalMemory = cfg.MemoryMB * bytesInMB totalMemory = int64(cfg.MemoryMB) * bytesInMB
} else { } else {
memInfo, err := mem.VirtualMemory() memInfo, err := mem.VirtualMemory()
if err != nil { if err != nil {
@ -36,21 +36,23 @@ func (f *MemoryFingerprint) Fingerprint(req *FingerprintRequest, resp *Fingerpri
return err return err
} }
if memInfo.Total > 0 { if memInfo.Total > 0 {
totalMemory = int(memInfo.Total) totalMemory = int64(memInfo.Total)
} }
} }
if totalMemory > 0 { if totalMemory > 0 {
resp.AddAttribute("memory.totalbytes", fmt.Sprintf("%d", totalMemory)) resp.AddAttribute("memory.totalbytes", fmt.Sprintf("%d", totalMemory))
// COMPAT(0.10): Remove in 0.10 memoryMB := totalMemory / bytesInMB
// COMPAT(0.10): Unused since 0.9.
resp.Resources = &structs.Resources{ resp.Resources = &structs.Resources{
MemoryMB: totalMemory / bytesInMB, MemoryMB: int(memoryMB),
} }
resp.NodeResources = &structs.NodeResources{ resp.NodeResources = &structs.NodeResources{
Memory: structs.NodeMemoryResources{ Memory: structs.NodeMemoryResources{
MemoryMB: int64(totalMemory / bytesInMB), MemoryMB: memoryMB,
}, },
} }
} }

View file

@ -11,6 +11,8 @@ import (
) )
func TestMemoryFingerprint(t *testing.T) { func TestMemoryFingerprint(t *testing.T) {
require := require.New(t)
f := NewMemoryFingerprint(testlog.HCLogger(t)) f := NewMemoryFingerprint(testlog.HCLogger(t))
node := &structs.Node{ node := &structs.Node{
Attributes: make(map[string]string), Attributes: make(map[string]string),
@ -19,24 +21,13 @@ func TestMemoryFingerprint(t *testing.T) {
request := &FingerprintRequest{Config: &config.Config{}, Node: node} request := &FingerprintRequest{Config: &config.Config{}, Node: node}
var response FingerprintResponse var response FingerprintResponse
err := f.Fingerprint(request, &response) err := f.Fingerprint(request, &response)
if err != nil { require.NoError(err)
t.Fatalf("err: %v", err)
}
assertNodeAttributeContains(t, response.Attributes, "memory.totalbytes") assertNodeAttributeContains(t, response.Attributes, "memory.totalbytes")
require.NotNil(response.Resources, "expected response Resources to not be nil")
if response.Resources == nil { require.NotZero(response.Resources.MemoryMB, "expected memory to be non-zero")
t.Fatalf("response resources should not be nil") require.NotNil(response.NodeResources, "expected response NodeResources to not be nil")
} require.NotZero(response.NodeResources.Memory.MemoryMB, "expected memory to be non-zero")
// COMPAT(0.10): Remove in 0.10
if response.Resources.MemoryMB == 0 {
t.Fatalf("Expected node.Resources.MemoryMB to be non-zero")
}
if response.NodeResources.Memory.MemoryMB == 0 {
t.Fatalf("Expected node.Resources.MemoryMB to be non-zero")
}
} }
func TestMemoryFingerprint_Override(t *testing.T) { func TestMemoryFingerprint_Override(t *testing.T) {
@ -56,6 +47,7 @@ func TestMemoryFingerprint_Override(t *testing.T) {
assertNodeAttributeContains(t, response.Attributes, "memory.totalbytes") assertNodeAttributeContains(t, response.Attributes, "memory.totalbytes")
require := require.New(t) require := require.New(t)
require.NotNil(response.Resources) require.NotNil(response.Resources)
require.Equal(response.Resources.MemoryMB, memoryMB) require.EqualValues(response.Resources.MemoryMB, memoryMB)
require.NotNil(response.NodeResources)
require.EqualValues(response.NodeResources.Memory.MemoryMB, memoryMB) require.EqualValues(response.NodeResources.Memory.MemoryMB, memoryMB)
} }