From 11f85472a8b7b023dc4c03e2caabb610087d1fb4 Mon Sep 17 00:00:00 2001 From: Songlin Yang Date: Tue, 5 Dec 2023 06:53:17 +0800 Subject: [PATCH] chore(*): enable prealloc & predel linters (#395) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 开启 prealloc、predeclared Signed-off-by: Soren Yang --- .golangci.yaml | 4 ++-- cmd/client/cmd.go | 4 ++-- cmd/client/cmd_test.go | 6 +++--- cmd/client/{delete => del}/cmd.go | 2 +- cmd/client/{delete => del}/cmd_test.go | 2 +- common/pprof.go | 2 +- coordinator/impl/cluster_rebalance.go | 2 +- coordinator/impl/shard_controller.go | 6 +++--- oxia/internal/metrics/metrics.go | 8 ++++---- server/wal/wal_rw_segment.go | 6 +++--- 10 files changed, 21 insertions(+), 21 deletions(-) rename cmd/client/{delete => del}/cmd.go (99%) rename cmd/client/{delete => del}/cmd_test.go (99%) diff --git a/.golangci.yaml b/.golangci.yaml index 9a576cdc..84127bec 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -44,8 +44,8 @@ linters: # - nilnil # - noctx - nolintlint - # - prealloc - # - predeclared + - prealloc + - predeclared - promlinter # - reassign - rowserrcheck diff --git a/cmd/client/cmd.go b/cmd/client/cmd.go index 899517a0..e9e2e736 100644 --- a/cmd/client/cmd.go +++ b/cmd/client/cmd.go @@ -20,7 +20,7 @@ import ( "github.com/spf13/cobra" "github.com/streamnative/oxia/cmd/client/common" - "github.com/streamnative/oxia/cmd/client/delete" + "github.com/streamnative/oxia/cmd/client/del" "github.com/streamnative/oxia/cmd/client/get" "github.com/streamnative/oxia/cmd/client/list" "github.com/streamnative/oxia/cmd/client/notifications" @@ -46,7 +46,7 @@ func init() { Cmd.PersistentFlags().DurationVar(&common.Config.RequestTimeout, "request-timeout", oxia.DefaultRequestTimeout, "Requests timeout") Cmd.AddCommand(put.Cmd) - Cmd.AddCommand(delete.Cmd) + Cmd.AddCommand(del.Cmd) Cmd.AddCommand(get.Cmd) Cmd.AddCommand(list.Cmd) Cmd.AddCommand(notifications.Cmd) diff --git a/cmd/client/cmd_test.go b/cmd/client/cmd_test.go index 60633354..27a312f9 100644 --- a/cmd/client/cmd_test.go +++ b/cmd/client/cmd_test.go @@ -21,7 +21,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/streamnative/oxia/cmd/client/delete" + "github.com/streamnative/oxia/cmd/client/del" "github.com/streamnative/oxia/cmd/client/get" "github.com/streamnative/oxia/cmd/client/list" "github.com/streamnative/oxia/cmd/client/put" @@ -169,7 +169,7 @@ func TestClientCmd(t *testing.T) { "\\{\\}", "^$", }, - {"delete-range-with-expected", []string{"delete", "--key-min", "q", "--key-max", "s", "-e", "0"}, "", delete.ErrExpectedVersionInconsistent, + {"delete-range-with-expected", []string{"delete", "--key-min", "q", "--key-max", "s", "-e", "0"}, "", del.ErrExpectedVersionInconsistent, ".*", "Error: inconsistent flags; zero or all keys must have an expected version", }, @@ -182,7 +182,7 @@ func TestClientCmd(t *testing.T) { put.Config.Reset() get.Config.Reset() list.Config.Reset() - delete.Config.Reset() + del.Config.Reset() stdin.WriteString(test.stdin) Cmd.SetArgs(append([]string{"-a", serviceAddress}, test.args...)) diff --git a/cmd/client/delete/cmd.go b/cmd/client/del/cmd.go similarity index 99% rename from cmd/client/delete/cmd.go rename to cmd/client/del/cmd.go index a7932c6d..c7232396 100644 --- a/cmd/client/delete/cmd.go +++ b/cmd/client/del/cmd.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package delete +package del import ( "encoding/json" diff --git a/cmd/client/delete/cmd_test.go b/cmd/client/del/cmd_test.go similarity index 99% rename from cmd/client/delete/cmd_test.go rename to cmd/client/del/cmd_test.go index 09be6fb6..857a5b58 100644 --- a/cmd/client/delete/cmd_test.go +++ b/cmd/client/del/cmd_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package delete +package del import ( "bytes" diff --git a/common/pprof.go b/common/pprof.go index 5117668b..9587de4b 100644 --- a/common/pprof.go +++ b/common/pprof.go @@ -33,7 +33,7 @@ var ( // DoWithLabels attaches the labels to the current go-routine Pprof context, // for the duration of the call to f. func DoWithLabels(labels map[string]string, f func()) { - var l []string + l := make([]string, 0, len(labels)*2) for k, v := range labels { l = append(l, k, v) } diff --git a/coordinator/impl/cluster_rebalance.go b/coordinator/impl/cluster_rebalance.go index 190fe0ea..8b82b331 100644 --- a/coordinator/impl/cluster_rebalance.go +++ b/coordinator/impl/cluster_rebalance.go @@ -193,7 +193,7 @@ func getServerRanking(shardsPerServer map[model.ServerAddress]common.Set[int64]) } func getFirstEntry(m map[model.ServerAddress]common.Set[int64]) (model.ServerAddress, common.Set[int64]) { - var keys []model.ServerAddress + keys := make([]model.ServerAddress, 0, len(m)) for k := range m { keys = append(keys, k) } diff --git a/coordinator/impl/shard_controller.go b/coordinator/impl/shard_controller.go index f89017a0..4d310fb0 100644 --- a/coordinator/impl/shard_controller.go +++ b/coordinator/impl/shard_controller.go @@ -791,14 +791,14 @@ func mergeLists[T any](lists ...[]T) []T { return res } -func replaceInList(list []model.ServerAddress, old, new model.ServerAddress) []model.ServerAddress { +func replaceInList(list []model.ServerAddress, oldServerAddress, newServerAddress model.ServerAddress) []model.ServerAddress { var res []model.ServerAddress for _, item := range list { - if item.Public != old.Public && item.Internal != old.Internal { + if item.Public != oldServerAddress.Public && item.Internal != oldServerAddress.Internal { res = append(res, item) } } - res = append(res, new) + res = append(res, newServerAddress) return res } diff --git a/oxia/internal/metrics/metrics.go b/oxia/internal/metrics/metrics.go index 7bc019f4..1a7b55e5 100644 --- a/oxia/internal/metrics/metrics.go +++ b/oxia/internal/metrics/metrics.go @@ -70,15 +70,15 @@ func (m *Metrics) DecoratePut(put model.PutCall) model.PutCall { return put } -func (m *Metrics) DecorateDelete(delete model.DeleteCall) model.DeleteCall { - callback := delete.Callback +func (m *Metrics) DecorateDelete(deleteCall model.DeleteCall) model.DeleteCall { + callback := deleteCall.Callback metricContext := m.metricContextFunc("delete") - delete.Callback = func(response *proto.DeleteResponse, err error) { + deleteCall.Callback = func(response *proto.DeleteResponse, err error) { callback(response, err) ctx, start, _attrs := metricContext(err) m.opTime.Record(ctx, m.sinceFunc(start), _attrs) } - return delete + return deleteCall } func (m *Metrics) DecorateDeleteRange(deleteRange model.DeleteRangeCall) model.DeleteRangeCall { diff --git a/server/wal/wal_rw_segment.go b/server/wal/wal_rw_segment.go index 83cedb1c..2b4dc244 100644 --- a/server/wal/wal_rw_segment.go +++ b/server/wal/wal_rw_segment.go @@ -32,7 +32,7 @@ type ReadWriteSegment interface { Truncate(lastSafeOffset int64) error - HasSpace(len int) bool + HasSpace(l int) bool Flush() error } @@ -121,8 +121,8 @@ func (ms *readWriteSegment) Read(offset int64) ([]byte, error) { return entry, nil } -func (ms *readWriteSegment) HasSpace(len int) bool { - return ms.currentFileOffset+4+uint32(len) <= ms.segmentSize +func (ms *readWriteSegment) HasSpace(l int) bool { + return ms.currentFileOffset+4+uint32(l) <= ms.segmentSize } func (ms *readWriteSegment) Append(offset int64, data []byte) error {