Skip to content

Commit

Permalink
*: Use sync/atomic instead of go.uber.org/atomic
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Karpy <[email protected]>
  • Loading branch information
carpawell committed Sep 18, 2023
1 parent a7723ce commit 8909b50
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 44 deletions.
31 changes: 16 additions & 15 deletions cmd/neofs-node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/signal"
"path/filepath"
"sync"
"sync/atomic"
atomicstd "sync/atomic"
"syscall"
"time"
Expand Down Expand Up @@ -68,7 +69,6 @@ import (
"github.com/nspcc-dev/neofs-sdk-go/version"
"github.com/panjf2000/ants/v2"
"go.etcd.io/bbolt"
"go.uber.org/atomic"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
Expand Down Expand Up @@ -248,7 +248,7 @@ type internals struct {
closers []func()

apiVersion version.Version
healthStatus *atomic.Int32
healthStatus atomic.Int32
// is node under maintenance
isMaintenance atomic.Bool
}
Expand Down Expand Up @@ -405,8 +405,8 @@ type cfgNetmap struct {
state *networkState

needBootstrap bool
reBoostrapTurnedOff *atomic.Bool // managed by control service in runtime
startEpoch uint64 // epoch number when application is started
reBoostrapTurnedOff atomic.Bool // managed by control service in runtime
startEpoch uint64 // epoch number when application is started
}

type cfgNodeInfo struct {
Expand Down Expand Up @@ -505,13 +505,13 @@ func initCfg(appCfg *config.Config) *cfg {
fatalOnErr(err)

c.internals = internals{
ctx: context.Background(),
appCfg: appCfg,
internalErr: make(chan error, 10), // We only need one error, but we can have multiple senders.
wg: new(sync.WaitGroup),
apiVersion: version.Current(),
healthStatus: atomic.NewInt32(int32(control.HealthStatus_HEALTH_STATUS_UNDEFINED)),
ctx: context.Background(),
appCfg: appCfg,
internalErr: make(chan error, 10), // We only need one error, but we can have multiple senders.
wg: new(sync.WaitGroup),
apiVersion: version.Current(),
}
c.internals.healthStatus.Store(int32(control.HealthStatus_HEALTH_STATUS_UNDEFINED))

c.internals.logLevel, err = zap.ParseAtomicLevel(c.LoggerCfg.level)
fatalOnErr(err)
Expand Down Expand Up @@ -550,11 +550,10 @@ func initCfg(appCfg *config.Config) *cfg {
workerPool: containerWorkerPool,
}
c.cfgNetmap = cfgNetmap{
scriptHash: contractsconfig.Netmap(appCfg),
state: netState,
workerPool: netmapWorkerPool,
needBootstrap: !relayOnly,
reBoostrapTurnedOff: atomic.NewBool(relayOnly),
scriptHash: contractsconfig.Netmap(appCfg),
state: netState,
workerPool: netmapWorkerPool,
needBootstrap: !relayOnly,
}
c.cfgGRPC = cfgGRPC{
maxChunkSize: maxChunkSize,
Expand All @@ -572,6 +571,8 @@ func initCfg(appCfg *config.Config) *cfg {
workerPool: reputationWorkerPool,
}

c.cfgNetmap.reBoostrapTurnedOff.Store(nodeconfig.Relay(appCfg))

c.ownerIDFromKey = user.ResolveFromECDSAPublicKey(key.PrivateKey.PublicKey)

if metricsconfig.Enabled(c.appCfg) {
Expand Down
5 changes: 2 additions & 3 deletions cmd/neofs-node/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"sync/atomic"

netmapGRPC "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
Expand All @@ -17,13 +18,12 @@ import (
netmapService "github.com/nspcc-dev/neofs-node/pkg/services/netmap"
netmapSDK "github.com/nspcc-dev/neofs-sdk-go/netmap"
"github.com/nspcc-dev/neofs-sdk-go/version"
"go.uber.org/atomic"
"go.uber.org/zap"
)

// primary solution of local network state dump.
type networkState struct {
epoch *atomic.Uint64
epoch atomic.Uint64

controlNetStatus atomic.Value // control.NetmapStatus

Expand All @@ -37,7 +37,6 @@ func newNetworkState() *networkState {
nmStatus.Store(control.NetmapStatus_STATUS_UNDEFINED)

return &networkState{
epoch: atomic.NewUint64(0),
controlNetStatus: nmStatus,
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ require (
github.com/spf13/viper v1.14.0
github.com/stretchr/testify v1.8.4
go.etcd.io/bbolt v1.3.7
go.uber.org/atomic v1.10.0
go.uber.org/zap v1.24.0
golang.org/x/sys v0.11.0
golang.org/x/term v0.11.0
Expand Down Expand Up @@ -92,6 +91,7 @@ require (
github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954 // indirect
github.com/twmb/murmur3 v1.1.5 // indirect
github.com/urfave/cli v1.22.5 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b // indirect
Expand Down
2 changes: 1 addition & 1 deletion pkg/innerring/innerring.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net"
"sync/atomic"

"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
Expand Down Expand Up @@ -52,7 +53,6 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/util/state"
"github.com/panjf2000/ants/v2"
"github.com/spf13/viper"
"go.uber.org/atomic"
"go.uber.org/zap"
"google.golang.org/grpc"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/local_object_storage/blobovnicza/blobovnicza.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package blobovnicza
import (
"io/fs"
"os"
"sync/atomic"
"time"

"go.etcd.io/bbolt"
"go.uber.org/atomic"
"go.uber.org/zap"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/local_object_storage/blobovnicza/sizes.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (b *Blobovnicza) incSize(sz uint64) {
}

func (b *Blobovnicza) decSize(sz uint64) {
b.filled.Sub(sz)
b.filled.Add(^(sz - 1))
}

func (b *Blobovnicza) full() bool {
Expand Down
6 changes: 3 additions & 3 deletions pkg/local_object_storage/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package engine
import (
"errors"
"sync"
"sync/atomic"

"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr"
"github.com/nspcc-dev/neofs-node/pkg/util"
"go.uber.org/atomic"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -126,7 +126,7 @@ func (e *StorageEngine) reportShardErrorBackground(id string, msg string, err er
return
}

errCount := sh.errorCount.Inc()
errCount := sh.errorCount.Add(1)
e.reportShardErrorWithFlags(sh.Shard, errCount, false, msg, err)
}

Expand All @@ -144,7 +144,7 @@ func (e *StorageEngine) reportShardError(
return
}

errCount := sh.errorCount.Inc()
errCount := sh.errorCount.Add(1)
e.reportShardErrorWithFlags(sh.Shard, errCount, true, msg, err, fields...)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/local_object_storage/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"sync/atomic"
"testing"

"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
Expand All @@ -23,7 +24,6 @@ import (
"github.com/nspcc-dev/tzhash/tz"
"github.com/panjf2000/ants/v2"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -86,7 +86,7 @@ func testNewEngineWithShards(shards ...*shard.Shard) *StorageEngine {
}

engine.shards[s.ID().String()] = shardWrapper{
errorCount: atomic.NewUint32(0),
errorCount: new(atomic.Uint32),
Shard: s,
}
engine.shardPools[s.ID().String()] = pool
Expand Down
4 changes: 2 additions & 2 deletions pkg/local_object_storage/engine/shards.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package engine

import (
"fmt"
"sync/atomic"

"github.com/google/uuid"
"github.com/nspcc-dev/hrw"
Expand All @@ -10,7 +11,6 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"github.com/panjf2000/ants/v2"
"go.uber.org/atomic"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -128,7 +128,7 @@ func (e *StorageEngine) addShard(sh *shard.Shard) error {
}

e.shards[strID] = shardWrapper{
errorCount: atomic.NewUint32(0),
errorCount: new(atomic.Uint32),
Shard: sh,
}

Expand Down
12 changes: 7 additions & 5 deletions pkg/local_object_storage/metabase/put_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package meta_test
import (
"runtime"
"strconv"
"sync/atomic"
"testing"
"time"

Expand All @@ -13,7 +14,6 @@ import (
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
objecttest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
)

func prepareObjects(t testing.TB, n int) []*objectSDK.Object {
Expand Down Expand Up @@ -47,13 +47,14 @@ func BenchmarkPut(b *testing.B) {
// Ensure the benchmark is bound by CPU and not waiting batch-delay time.
b.SetParallelism(1)

index := atomic.NewInt64(-1)
index := new(atomic.Int64)
index.Store(-1)
objs := prepareObjects(b, b.N)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := metaPut(db, objs[index.Inc()], nil); err != nil {
if err := metaPut(db, objs[index.Add(1)], nil); err != nil {
b.Fatal(err)
}
}
Expand All @@ -63,12 +64,13 @@ func BenchmarkPut(b *testing.B) {
db := newDB(b,
meta.WithMaxBatchDelay(time.Millisecond*10),
meta.WithMaxBatchSize(1))
index := atomic.NewInt64(-1)
index := new(atomic.Int64)
index.Store(-1)
objs := prepareObjects(b, b.N)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := metaPut(db, objs[index.Inc()], nil); err != nil {
if err := metaPut(db, objs[index.Add(1)], nil); err != nil {
b.Fatal(err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/local_object_storage/writecache/flush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package writecache
import (
"os"
"path/filepath"
"sync/atomic"
"testing"

objectCore "github.com/nspcc-dev/neofs-node/pkg/core/object"
Expand All @@ -21,7 +22,6 @@ import (
versionSDK "github.com/nspcc-dev/neofs-sdk-go/version"
"github.com/stretchr/testify/require"
"go.etcd.io/bbolt"
"go.uber.org/atomic"
"go.uber.org/zap/zaptest"
)

Expand Down Expand Up @@ -157,7 +157,7 @@ func TestFlush(t *testing.T) {
testIgnoreErrors := func(t *testing.T, f func(*cache)) {
var errCount atomic.Uint32
wc, bs, mb := newCache(t, WithReportErrorFunc(func(message string, err error) {
errCount.Inc()
errCount.Add(1)
}))
objects := putObjects(t, wc)
f(wc.(*cache))
Expand Down
11 changes: 6 additions & 5 deletions pkg/local_object_storage/writecache/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package writecache

import (
"fmt"
"math"
"sync/atomic"

"go.etcd.io/bbolt"
"go.uber.org/atomic"
)

func (c *cache) estimateCacheSize() uint64 {
Expand All @@ -24,23 +25,23 @@ type counters struct {
}

func (x *counters) IncDB() {
x.cDB.Inc()
x.cDB.Add(1)
}

func (x *counters) DecDB() {
x.cDB.Dec()
x.cDB.Add(math.MaxUint32)
}

func (x *counters) DB() uint64 {
return x.cDB.Load()
}

func (x *counters) IncFS() {
x.cFS.Inc()
x.cFS.Add(1)
}

func (x *counters) DecFS() {
x.cFS.Dec()
x.cFS.Add(math.MaxUint32)
}

func (x *counters) FS() uint64 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/morph/deploy/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"strings"
"sync/atomic"
"time"

"github.com/nspcc-dev/neo-go/pkg/core/block"
Expand All @@ -25,7 +26,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neofs-contract/common"
"go.uber.org/atomic"
"go.uber.org/zap"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/services/audit/auditor/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package auditor
import (
"context"
"sync"
"sync/atomic"
"time"

"github.com/nspcc-dev/neofs-node/pkg/services/audit"
Expand All @@ -12,7 +13,6 @@ import (
"github.com/nspcc-dev/neofs-sdk-go/netmap"
"github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"go.uber.org/atomic"
"go.uber.org/zap"
)

Expand Down
Loading

0 comments on commit 8909b50

Please sign in to comment.