From 4919b6a206ad9f448fcb36ae7d574a8f7dd3ad59 Mon Sep 17 00:00:00 2001 From: Aleksey Savchuk Date: Fri, 21 Mar 2025 15:51:22 +0300 Subject: [PATCH] [#1689] node/config: Allow zero `max_ops` in RPC limits config The limiter allows zeros for limits, meaning "this operation is disabled". However, the config didn't allow zero due to the lack of distinction between "no value" and "zero" - cast functions read both `nil` and zero as zero. Now, the config allows a zero limit. Added tests. Managing such cases should be easier after #1610. Change-Id: Ifc840732390b2feb975f230573b34bf479406e05 Signed-off-by: Aleksey Savchuk --- cmd/frostfs-node/config/rpc/config.go | 5 ++-- cmd/frostfs-node/config/rpc/config_test.go | 26 ++++++++++++++++++- .../rpc/testdata/{node.env => no_max_ops.env} | 0 .../testdata/{node.json => no_max_ops.json} | 0 .../testdata/{node.yaml => no_max_ops.yaml} | 0 .../config/rpc/testdata/zero_max_ops.env | 4 +++ .../config/rpc/testdata/zero_max_ops.json | 19 ++++++++++++++ .../config/rpc/testdata/zero_max_ops.yaml | 9 +++++++ 8 files changed, 59 insertions(+), 4 deletions(-) rename cmd/frostfs-node/config/rpc/testdata/{node.env => no_max_ops.env} (100%) rename cmd/frostfs-node/config/rpc/testdata/{node.json => no_max_ops.json} (100%) rename cmd/frostfs-node/config/rpc/testdata/{node.yaml => no_max_ops.yaml} (100%) create mode 100644 cmd/frostfs-node/config/rpc/testdata/zero_max_ops.env create mode 100644 cmd/frostfs-node/config/rpc/testdata/zero_max_ops.json create mode 100644 cmd/frostfs-node/config/rpc/testdata/zero_max_ops.yaml diff --git a/cmd/frostfs-node/config/rpc/config.go b/cmd/frostfs-node/config/rpc/config.go index 197990d07..e0efdfde2 100644 --- a/cmd/frostfs-node/config/rpc/config.go +++ b/cmd/frostfs-node/config/rpc/config.go @@ -31,12 +31,11 @@ func Limits(c *config.Config) []LimitConfig { break } - maxOps := config.IntSafe(sc, "max_ops") - if maxOps == 0 { + if sc.Value("max_ops") == nil { panic("no max operations for method group") } - limits = append(limits, LimitConfig{methods, maxOps}) + limits = append(limits, LimitConfig{methods, config.IntSafe(sc, "max_ops")}) } return limits diff --git a/cmd/frostfs-node/config/rpc/config_test.go b/cmd/frostfs-node/config/rpc/config_test.go index 31a837cee..a6365e19f 100644 --- a/cmd/frostfs-node/config/rpc/config_test.go +++ b/cmd/frostfs-node/config/rpc/config_test.go @@ -38,7 +38,7 @@ func TestRPCSection(t *testing.T) { }) t.Run("no max operations", func(t *testing.T) { - const path = "testdata/node" + const path = "testdata/no_max_ops" fileConfigTest := func(c *config.Config) { require.Panics(t, func() { _ = Limits(c) }) @@ -50,4 +50,28 @@ func TestRPCSection(t *testing.T) { configtest.ForEnvFileType(t, path, fileConfigTest) }) }) + + t.Run("zero max operations", func(t *testing.T) { + const path = "testdata/zero_max_ops" + + fileConfigTest := func(c *config.Config) { + limits := Limits(c) + require.Len(t, limits, 2) + + limit0 := limits[0] + limit1 := limits[1] + + require.ElementsMatch(t, limit0.Methods, []string{"/neo.fs.v2.object.ObjectService/PutSingle", "/neo.fs.v2.object.ObjectService/Put"}) + require.Equal(t, limit0.MaxOps, int64(0)) + + require.ElementsMatch(t, limit1.Methods, []string{"/neo.fs.v2.object.ObjectService/Get"}) + require.Equal(t, limit1.MaxOps, int64(10000)) + } + + configtest.ForEachFileType(path, fileConfigTest) + + t.Run("ENV", func(t *testing.T) { + configtest.ForEnvFileType(t, path, fileConfigTest) + }) + }) } diff --git a/cmd/frostfs-node/config/rpc/testdata/node.env b/cmd/frostfs-node/config/rpc/testdata/no_max_ops.env similarity index 100% rename from cmd/frostfs-node/config/rpc/testdata/node.env rename to cmd/frostfs-node/config/rpc/testdata/no_max_ops.env diff --git a/cmd/frostfs-node/config/rpc/testdata/node.json b/cmd/frostfs-node/config/rpc/testdata/no_max_ops.json similarity index 100% rename from cmd/frostfs-node/config/rpc/testdata/node.json rename to cmd/frostfs-node/config/rpc/testdata/no_max_ops.json diff --git a/cmd/frostfs-node/config/rpc/testdata/node.yaml b/cmd/frostfs-node/config/rpc/testdata/no_max_ops.yaml similarity index 100% rename from cmd/frostfs-node/config/rpc/testdata/node.yaml rename to cmd/frostfs-node/config/rpc/testdata/no_max_ops.yaml diff --git a/cmd/frostfs-node/config/rpc/testdata/zero_max_ops.env b/cmd/frostfs-node/config/rpc/testdata/zero_max_ops.env new file mode 100644 index 000000000..ce7302b0b --- /dev/null +++ b/cmd/frostfs-node/config/rpc/testdata/zero_max_ops.env @@ -0,0 +1,4 @@ +FROSTFS_RPC_LIMITS_0_METHODS="/neo.fs.v2.object.ObjectService/PutSingle /neo.fs.v2.object.ObjectService/Put" +FROSTFS_RPC_LIMITS_0_MAX_OPS=0 +FROSTFS_RPC_LIMITS_1_METHODS="/neo.fs.v2.object.ObjectService/Get" +FROSTFS_RPC_LIMITS_1_MAX_OPS=10000 diff --git a/cmd/frostfs-node/config/rpc/testdata/zero_max_ops.json b/cmd/frostfs-node/config/rpc/testdata/zero_max_ops.json new file mode 100644 index 000000000..16a1c173f --- /dev/null +++ b/cmd/frostfs-node/config/rpc/testdata/zero_max_ops.json @@ -0,0 +1,19 @@ +{ + "rpc": { + "limits": [ + { + "methods": [ + "/neo.fs.v2.object.ObjectService/PutSingle", + "/neo.fs.v2.object.ObjectService/Put" + ], + "max_ops": 0 + }, + { + "methods": [ + "/neo.fs.v2.object.ObjectService/Get" + ], + "max_ops": 10000 + } + ] + } +} diff --git a/cmd/frostfs-node/config/rpc/testdata/zero_max_ops.yaml b/cmd/frostfs-node/config/rpc/testdata/zero_max_ops.yaml new file mode 100644 index 000000000..525d768d4 --- /dev/null +++ b/cmd/frostfs-node/config/rpc/testdata/zero_max_ops.yaml @@ -0,0 +1,9 @@ +rpc: + limits: + - methods: + - /neo.fs.v2.object.ObjectService/PutSingle + - /neo.fs.v2.object.ObjectService/Put + max_ops: 0 + - methods: + - /neo.fs.v2.object.ObjectService/Get + max_ops: 10000