Skip to content

Commit 809a8d6

Browse files
committed
Replace sync/atomic with uber-go/atomic
Replaces the usage of sync/atomic with uber-go/atomic across the whole codebase. This makes all access in the atomic variables atomic everywhere. Related with: #2928 Signed-off-by: Javier Palomo <[email protected]>
1 parent 9b2343f commit 809a8d6

File tree

7 files changed

+28
-27
lines changed

7 files changed

+28
-27
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ We use *breaking* word for marking changes that are not backward compatible (rel
3030

3131
- [#2893](https://github.com/thanos-io/thanos/pull/2893) Store: Rename metric `thanos_bucket_store_cached_postings_compression_time_seconds` to `thanos_bucket_store_cached_postings_compression_time_seconds_total`.
3232
- [#2915](https://github.com/thanos-io/thanos/pull/2915) Receive,Ruler: Enable TSDB directory locking by default. Add a new flag (`--tsdb.no-lockfile`) to override behavior.
33+
- [#2935](https://github.com/thanos-io/thanos/pull/2935) Replace sync/atomic with uber-go/atomic
3334

3435
## [v0.14.0](https://github.com/thanos-io/thanos/releases/tag/v0.14.0) - 2020.07.10
3536

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ require (
5454
github.com/uber/jaeger-lib v2.2.0+incompatible
5555
go.elastic.co/apm v1.5.0
5656
go.elastic.co/apm/module/apmot v1.5.0
57+
go.uber.org/atomic v1.6.0
5758
go.uber.org/automaxprocs v1.2.0
5859
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
5960
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d

pkg/alert/alert.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"net/url"
1515
"path"
1616
"sync"
17-
"sync/atomic"
1817
"time"
1918

2019
"github.com/go-kit/kit/log"
@@ -25,6 +24,7 @@ import (
2524
"github.com/prometheus/client_golang/prometheus"
2625
"github.com/prometheus/client_golang/prometheus/promauto"
2726
"github.com/prometheus/prometheus/pkg/labels"
27+
"go.uber.org/atomic"
2828

2929
"github.com/thanos-io/thanos/pkg/runutil"
3030
"github.com/thanos-io/thanos/pkg/tracing"
@@ -370,7 +370,7 @@ func (s *Sender) Send(ctx context.Context, alerts []*Alert) {
370370

371371
var (
372372
wg sync.WaitGroup
373-
numSuccess uint64
373+
numSuccess atomic.Uint64
374374
)
375375
for _, am := range s.alertmanagers {
376376
for _, u := range am.dispatcher.Endpoints() {
@@ -396,14 +396,14 @@ func (s *Sender) Send(ctx context.Context, alerts []*Alert) {
396396
s.latency.WithLabelValues(u.Host).Observe(time.Since(start).Seconds())
397397
s.sent.WithLabelValues(u.Host).Add(float64(len(alerts)))
398398

399-
atomic.AddUint64(&numSuccess, 1)
399+
numSuccess.Inc()
400400
})
401401
}(am, *u)
402402
}
403403
}
404404
wg.Wait()
405405

406-
if numSuccess > 0 {
406+
if numSuccess.Load() > 0 {
407407
return
408408
}
409409

pkg/prober/http.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ package prober
66
import (
77
"io"
88
"net/http"
9-
"sync/atomic"
109

1110
"github.com/go-kit/kit/log"
1211
"github.com/go-kit/kit/log/level"
12+
"go.uber.org/atomic"
1313
)
1414

1515
type check func() bool
1616

1717
// HTTPProbe represents health and readiness status of given component, and provides HTTP integration.
1818
type HTTPProbe struct {
19-
ready uint32
20-
healthy uint32
19+
ready atomic.Uint32
20+
healthy atomic.Uint32
2121
}
2222

2323
// NewHTTP returns HTTPProbe representing readiness and healthiness of given component.
@@ -49,34 +49,33 @@ func (p *HTTPProbe) handler(logger log.Logger, c check) http.HandlerFunc {
4949

5050
// isReady returns true if component is ready.
5151
func (p *HTTPProbe) isReady() bool {
52-
ready := atomic.LoadUint32(&p.ready)
52+
ready := p.ready.Load()
5353
return ready > 0
5454
}
5555

5656
// isHealthy returns true if component is healthy.
5757
func (p *HTTPProbe) isHealthy() bool {
58-
healthy := atomic.LoadUint32(&p.healthy)
58+
healthy := p.healthy.Load()
5959
return healthy > 0
6060
}
6161

6262
// Ready sets components status to ready.
6363
func (p *HTTPProbe) Ready() {
64-
atomic.SwapUint32(&p.ready, 1)
64+
p.ready.Swap(1)
6565
}
6666

6767
// NotReady sets components status to not ready with given error as a cause.
6868
func (p *HTTPProbe) NotReady(err error) {
69-
atomic.SwapUint32(&p.ready, 0)
69+
p.ready.Swap(0)
7070

7171
}
7272

7373
// Healthy sets components status to healthy.
7474
func (p *HTTPProbe) Healthy() {
75-
atomic.SwapUint32(&p.healthy, 1)
76-
75+
p.healthy.Swap(1)
7776
}
7877

7978
// NotHealthy sets components status to not healthy with given error as a cause.
8079
func (p *HTTPProbe) NotHealthy(err error) {
81-
atomic.SwapUint32(&p.healthy, 0)
80+
p.healthy.Swap(0)
8281
}

pkg/reloader/reloader_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import (
1515
"path/filepath"
1616
"strings"
1717
"sync"
18-
"sync/atomic"
1918
"testing"
2019
"time"
2120

2221
"github.com/fortytw2/leaktest"
2322
"github.com/thanos-io/thanos/pkg/testutil"
23+
"go.uber.org/atomic"
2424
)
2525

2626
func TestReloader_ConfigApply(t *testing.T) {

pkg/store/bucket_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"sort"
2020
"strconv"
2121
"sync"
22-
"sync/atomic"
2322
"testing"
2423
"time"
2524

@@ -52,6 +51,7 @@ import (
5251
storetestutil "github.com/thanos-io/thanos/pkg/store/storepb/testutil"
5352
"github.com/thanos-io/thanos/pkg/testutil"
5453
"github.com/thanos-io/thanos/pkg/testutil/e2eutil"
54+
"go.uber.org/atomic"
5555
)
5656

5757
var emptyRelabelConfig = make([]*relabel.Config, 0)
@@ -1281,10 +1281,10 @@ func benchBucketSeries(t testutil.TB, samplesPerSeries, totalSeries int, request
12811281

12821282
if !t.IsBenchmark() {
12831283
// Make sure the pool is correctly used. This is expected for 200k numbers.
1284-
testutil.Equals(t, numOfBlocks, int(chunkPool.(*mockedPool).gets))
1284+
testutil.Equals(t, numOfBlocks, int(chunkPool.(*mockedPool).gets.Load()))
12851285
// TODO(bwplotka): This is wrong negative for large number of samples (1mln). Investigate.
1286-
testutil.Equals(t, 0, int(chunkPool.(*mockedPool).balance))
1287-
chunkPool.(*mockedPool).gets = 0
1286+
testutil.Equals(t, 0, int(chunkPool.(*mockedPool).balance.Load()))
1287+
chunkPool.(*mockedPool).gets.Store(0)
12881288

12891289
for _, b := range blocks {
12901290
// NOTE(bwplotka): It is 4 x 1.0 for 100mln samples. Kind of make sense: long series.
@@ -1306,22 +1306,22 @@ func (m fakePool) Put(_ *[]byte) {}
13061306

13071307
type mockedPool struct {
13081308
parent pool.BytesPool
1309-
balance uint64
1310-
gets uint64
1309+
balance atomic.Uint64
1310+
gets atomic.Uint64
13111311
}
13121312

13131313
func (m *mockedPool) Get(sz int) (*[]byte, error) {
13141314
b, err := m.parent.Get(sz)
13151315
if err != nil {
13161316
return nil, err
13171317
}
1318-
atomic.AddUint64(&m.balance, uint64(cap(*b)))
1319-
atomic.AddUint64(&m.gets, uint64(1))
1318+
m.balance.Add(uint64(cap(*b)))
1319+
m.gets.Add(uint64(1))
13201320
return b, nil
13211321
}
13221322

13231323
func (m *mockedPool) Put(b *[]byte) {
1324-
atomic.AddUint64(&m.balance, ^uint64(cap(*b)-1))
1324+
m.balance.Sub(uint64(cap(*b)))
13251325
m.parent.Put(b)
13261326
}
13271327

pkg/store/limiter.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ package store
55

66
import (
77
"sync"
8-
"sync/atomic"
98

109
"github.com/pkg/errors"
1110
"github.com/prometheus/client_golang/prometheus"
11+
"go.uber.org/atomic"
1212
)
1313

1414
type ChunksLimiter interface {
@@ -25,7 +25,7 @@ type ChunksLimiterFactory func(failedCounter prometheus.Counter) ChunksLimiter
2525
// Limiter is a simple mechanism for checking if something has passed a certain threshold.
2626
type Limiter struct {
2727
limit uint64
28-
reserved uint64
28+
reserved atomic.Uint64
2929

3030
// Counter metric which we will increase if limit is exceeded.
3131
failedCounter prometheus.Counter
@@ -42,7 +42,7 @@ func (l *Limiter) Reserve(num uint64) error {
4242
if l.limit == 0 {
4343
return nil
4444
}
45-
if reserved := atomic.AddUint64(&l.reserved, num); reserved > l.limit {
45+
if reserved := l.reserved.Add(num); reserved > l.limit {
4646
// We need to protect from the counter being incremented twice due to concurrency
4747
// while calling Reserve().
4848
l.failedOnce.Do(l.failedCounter.Inc)

0 commit comments

Comments
 (0)