From 8909b50adc1d62435c1b1ed94fe12e59ce0a7080 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Thu, 10 Aug 2023 16:39:18 +0300 Subject: [PATCH] *: Use `sync/atomic` instead of `go.uber.org/atomic` Signed-off-by: Pavel Karpy --- cmd/neofs-node/config.go | 31 ++++++++++--------- cmd/neofs-node/netmap.go | 5 ++- go.mod | 2 +- pkg/innerring/innerring.go | 2 +- .../blobovnicza/blobovnicza.go | 2 +- pkg/local_object_storage/blobovnicza/sizes.go | 2 +- pkg/local_object_storage/engine/engine.go | 6 ++-- .../engine/engine_test.go | 4 +-- pkg/local_object_storage/engine/shards.go | 4 +-- pkg/local_object_storage/metabase/put_test.go | 12 ++++--- .../writecache/flush_test.go | 4 +-- pkg/local_object_storage/writecache/state.go | 11 ++++--- pkg/morph/deploy/util.go | 2 +- pkg/services/audit/auditor/context.go | 2 +- pkg/util/worker_pool.go | 3 +- 15 files changed, 48 insertions(+), 44 deletions(-) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index d45a0a0c91..3933849222 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -10,6 +10,7 @@ import ( "os/signal" "path/filepath" "sync" + "sync/atomic" atomicstd "sync/atomic" "syscall" "time" @@ -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" @@ -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 } @@ -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 { @@ -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) @@ -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, @@ -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) { diff --git a/cmd/neofs-node/netmap.go b/cmd/neofs-node/netmap.go index 5a3a431c74..9807be7d58 100644 --- a/cmd/neofs-node/netmap.go +++ b/cmd/neofs-node/netmap.go @@ -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" @@ -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 @@ -37,7 +37,6 @@ func newNetworkState() *networkState { nmStatus.Store(control.NetmapStatus_STATUS_UNDEFINED) return &networkState{ - epoch: atomic.NewUint64(0), controlNetStatus: nmStatus, } } diff --git a/go.mod b/go.mod index 1c079a667d..465841c351 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index c97fe312b7..28e5e6a84b 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -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" @@ -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" ) diff --git a/pkg/local_object_storage/blobovnicza/blobovnicza.go b/pkg/local_object_storage/blobovnicza/blobovnicza.go index c9ba170b80..5b7b5ca9a0 100644 --- a/pkg/local_object_storage/blobovnicza/blobovnicza.go +++ b/pkg/local_object_storage/blobovnicza/blobovnicza.go @@ -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" ) diff --git a/pkg/local_object_storage/blobovnicza/sizes.go b/pkg/local_object_storage/blobovnicza/sizes.go index aa0f9b4dce..81a7a7f999 100644 --- a/pkg/local_object_storage/blobovnicza/sizes.go +++ b/pkg/local_object_storage/blobovnicza/sizes.go @@ -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 { diff --git a/pkg/local_object_storage/engine/engine.go b/pkg/local_object_storage/engine/engine.go index 6e0efa3021..6c39532333 100644 --- a/pkg/local_object_storage/engine/engine.go +++ b/pkg/local_object_storage/engine/engine.go @@ -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" ) @@ -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) } @@ -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...) } diff --git a/pkg/local_object_storage/engine/engine_test.go b/pkg/local_object_storage/engine/engine_test.go index d0e5be29f5..a4dd99b28b 100644 --- a/pkg/local_object_storage/engine/engine_test.go +++ b/pkg/local_object_storage/engine/engine_test.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "sync/atomic" "testing" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor" @@ -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" ) @@ -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 diff --git a/pkg/local_object_storage/engine/shards.go b/pkg/local_object_storage/engine/shards.go index 773dc61d14..9a2dbd2674 100644 --- a/pkg/local_object_storage/engine/shards.go +++ b/pkg/local_object_storage/engine/shards.go @@ -2,6 +2,7 @@ package engine import ( "fmt" + "sync/atomic" "github.com/google/uuid" "github.com/nspcc-dev/hrw" @@ -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" ) @@ -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, } diff --git a/pkg/local_object_storage/metabase/put_test.go b/pkg/local_object_storage/metabase/put_test.go index f6b020d1b0..4481f46b8b 100644 --- a/pkg/local_object_storage/metabase/put_test.go +++ b/pkg/local_object_storage/metabase/put_test.go @@ -3,6 +3,7 @@ package meta_test import ( "runtime" "strconv" + "sync/atomic" "testing" "time" @@ -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 { @@ -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) } } @@ -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) } } diff --git a/pkg/local_object_storage/writecache/flush_test.go b/pkg/local_object_storage/writecache/flush_test.go index a7e63ae9cc..f3be34aaa3 100644 --- a/pkg/local_object_storage/writecache/flush_test.go +++ b/pkg/local_object_storage/writecache/flush_test.go @@ -3,6 +3,7 @@ package writecache import ( "os" "path/filepath" + "sync/atomic" "testing" objectCore "github.com/nspcc-dev/neofs-node/pkg/core/object" @@ -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" ) @@ -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)) diff --git a/pkg/local_object_storage/writecache/state.go b/pkg/local_object_storage/writecache/state.go index 1ba5a4bd3b..ac4ca6f162 100644 --- a/pkg/local_object_storage/writecache/state.go +++ b/pkg/local_object_storage/writecache/state.go @@ -2,9 +2,10 @@ package writecache import ( "fmt" + "math" + "sync/atomic" "go.etcd.io/bbolt" - "go.uber.org/atomic" ) func (c *cache) estimateCacheSize() uint64 { @@ -24,11 +25,11 @@ 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 { @@ -36,11 +37,11 @@ func (x *counters) DB() uint64 { } 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 { diff --git a/pkg/morph/deploy/util.go b/pkg/morph/deploy/util.go index 30d37d4e86..692da27356 100644 --- a/pkg/morph/deploy/util.go +++ b/pkg/morph/deploy/util.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "strings" + "sync/atomic" "time" "github.com/nspcc-dev/neo-go/pkg/core/block" @@ -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" ) diff --git a/pkg/services/audit/auditor/context.go b/pkg/services/audit/auditor/context.go index b40d7c7d90..863418b7ba 100644 --- a/pkg/services/audit/auditor/context.go +++ b/pkg/services/audit/auditor/context.go @@ -3,6 +3,7 @@ package auditor import ( "context" "sync" + "sync/atomic" "time" "github.com/nspcc-dev/neofs-node/pkg/services/audit" @@ -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" ) diff --git a/pkg/util/worker_pool.go b/pkg/util/worker_pool.go index 145fd1a5a9..97d76c4927 100644 --- a/pkg/util/worker_pool.go +++ b/pkg/util/worker_pool.go @@ -1,8 +1,9 @@ package util import ( + "sync/atomic" + "github.com/panjf2000/ants/v2" - "go.uber.org/atomic" ) // WorkerPool represents a tool to control