From 786beb8fc8528b80b4587dc3e789a4df6230a6c2 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 20 Jun 2019 14:52:48 +0300 Subject: [PATCH] Makefile: enable golangci-lint in `make check_all` --- Makefile | 7 ++++- lib/memory/limiter.go | 41 ++++++++++++++++++++++++++++ lib/memory/limiter_test.go | 56 ++++++++++++++++++++++++++++++++++++++ lib/storage/search_test.go | 2 +- 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 lib/memory/limiter.go create mode 100644 lib/memory/limiter_test.go diff --git a/Makefile b/Makefile index 64afbe7fb4..0c942df7d4 100644 --- a/Makefile +++ b/Makefile @@ -56,10 +56,15 @@ errcheck: install-errcheck install-errcheck: which errcheck || GO111MODULE=off go get -u github.com/kisielk/errcheck +check_all: fmt vet lint errcheck golangci-lint + test: GO111MODULE=on go test -mod=vendor ./lib/... GO111MODULE=on go test -mod=vendor ./app/... +test_full: + GO111MODULE=on go test -mod=vendor -coverprofile=coverage.txt -covermode=atomic ./lib/... ./app/... + benchmark: GO111MODULE=on go test -mod=vendor -bench=. ./lib/... GO111MODULE=on go test -mod=vendor -bench=. ./app/... @@ -81,7 +86,7 @@ install-qtc: golangci-lint: install-golangci-lint - golangci-lint run + golangci-lint run --exclude '(SA4003|SA1019):' -D errcheck install-golangci-lint: which golangci-lint || GO111MODULE=off go get -u github.com/golangci/golangci-lint/cmd/golangci-lint diff --git a/lib/memory/limiter.go b/lib/memory/limiter.go new file mode 100644 index 0000000000..a8e11d66ac --- /dev/null +++ b/lib/memory/limiter.go @@ -0,0 +1,41 @@ +package memory + +import ( + "sync" + + "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" +) + +// Limiter is the memory limiter. +// +// It limits memory to MaxSize. +type Limiter struct { + // The maximum allowed memory + MaxSize uint64 + + mu sync.Mutex + usage uint64 +} + +// Get obtains n bytes of memory from ml. +// +// It returns true on success, false on error. +func (ml *Limiter) Get(n uint64) bool { + ml.mu.Lock() + ok := n <= ml.MaxSize && ml.MaxSize-n >= ml.usage + if ok { + ml.usage += n + } + ml.mu.Unlock() + return ok +} + +// Put returns back n bytes of memory to ml. +func (ml *Limiter) Put(n uint64) { + ml.mu.Lock() + if n > ml.usage { + logger.Panicf("BUG: n=%d cannot exceed %d", n, ml.usage) + } + ml.usage -= n + ml.mu.Unlock() +} diff --git a/lib/memory/limiter_test.go b/lib/memory/limiter_test.go new file mode 100644 index 0000000000..d039d0d873 --- /dev/null +++ b/lib/memory/limiter_test.go @@ -0,0 +1,56 @@ +package memory + +import ( + "testing" +) + +func TestLimiter(t *testing.T) { + var ml Limiter + ml.MaxSize = 100 + + // Allocate memory + if !ml.Get(10) { + t.Fatalf("cannot get 10 out of %d bytes", ml.MaxSize) + } + if ml.usage != 10 { + t.Fatalf("unexpected usage; got %d; want %d", ml.usage, 10) + } + if !ml.Get(20) { + t.Fatalf("cannot get 20 out of 90 bytes") + } + if ml.usage != 30 { + t.Fatalf("unexpected usage; got %d; want %d", ml.usage, 30) + } + if ml.Get(1000) { + t.Fatalf("unexpected get for 1000 bytes") + } + if ml.usage != 30 { + t.Fatalf("unexpected usage; got %d; want %d", ml.usage, 30) + } + if ml.Get(71) { + t.Fatalf("unexpected get for 71 bytes") + } + if ml.usage != 30 { + t.Fatalf("unexpected usage; got %d; want %d", ml.usage, 30) + } + if !ml.Get(70) { + t.Fatalf("cannot get 70 bytes") + } + if ml.usage != 100 { + t.Fatalf("unexpected usage; got %d; want %d", ml.usage, 100) + } + + // Return memory back + ml.Put(10) + ml.Put(70) + if ml.usage != 20 { + t.Fatalf("unexpected usage; got %d; want %d", ml.usage, 20) + } + if !ml.Get(30) { + t.Fatalf("cannot get 30 bytes") + } + ml.Put(50) + if ml.usage != 0 { + t.Fatalf("unexpected usage; got %d; want %d", ml.usage, 0) + } +} diff --git a/lib/storage/search_test.go b/lib/storage/search_test.go index 140f17c9f2..69c9512c3d 100644 --- a/lib/storage/search_test.go +++ b/lib/storage/search_test.go @@ -38,7 +38,7 @@ func TestSearchQueryMarshalUnmarshal(t *testing.T) { if len(tail) > 0 { t.Fatalf("unexpected tail left after SearchQuery unmarshaling; tail (len=%d): %q", len(tail), tail) } - if sq1.AccountID != sq1.AccountID { + if sq2.AccountID != sq1.AccountID { t.Fatalf("unexpected AccountID; got %d; want %d", sq2.AccountID, sq1.AccountID) } if sq2.ProjectID != sq1.ProjectID {