From c6656c053bbe185cf4765cfbb7d00257d4491dd4 Mon Sep 17 00:00:00 2001 From: irfan sharif Date: Mon, 5 Dec 2022 18:08:33 -0500 Subject: [PATCH] server: introduce COCKROACH_FALLBACK_SPANCONFIG_NUM_REPLICAS_OVERRIDE It controls what replication factor is used for ranges with no explicit span configs set. This is a backportable form of the spanconfig.store.fallback_config_override we added in #92466. Release note: None --- pkg/kv/kvserver/BUILD.bazel | 1 + pkg/kv/kvserver/client_spanconfigs_test.go | 18 ++++++++++++++++++ pkg/server/server.go | 7 +++++++ 3 files changed, 26 insertions(+) diff --git a/pkg/kv/kvserver/BUILD.bazel b/pkg/kv/kvserver/BUILD.bazel index b2b79c085ea1..88e6d2700c0e 100644 --- a/pkg/kv/kvserver/BUILD.bazel +++ b/pkg/kv/kvserver/BUILD.bazel @@ -415,6 +415,7 @@ go_test( "//pkg/util/contextutil", "//pkg/util/ctxgroup", "//pkg/util/encoding", + "//pkg/util/envutil", "//pkg/util/hlc", "//pkg/util/humanizeutil", "//pkg/util/leaktest", diff --git a/pkg/kv/kvserver/client_spanconfigs_test.go b/pkg/kv/kvserver/client_spanconfigs_test.go index d5d28435f1a2..1aa5b8f5ffdc 100644 --- a/pkg/kv/kvserver/client_spanconfigs_test.go +++ b/pkg/kv/kvserver/client_spanconfigs_test.go @@ -26,6 +26,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/sqlutil" "github.com/cockroachdb/cockroach/pkg/testutils" "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" + "github.com/cockroachdb/cockroach/pkg/util/envutil" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/leaktest" "github.com/cockroachdb/errors" @@ -100,6 +101,23 @@ func TestSpanConfigUpdateAppliedToReplica(t *testing.T) { }) } +// TestFallbackSpanConfigOverride ensures that +// COCKROACH_FALLBACK_SPANCONFIG_NUM_REPLICAS_OVERRIDE works as expected. +func TestFallbackSpanConfigNumReplicasOverride(t *testing.T) { + defer leaktest.AfterTest(t)() + defer envutil.TestSetEnv(t, + "COCKROACH_FALLBACK_SPANCONFIG_NUM_REPLICAS_OVERRIDE", "42")() + + ctx := context.Background() + s, _, _ := serverutils.StartServer(t, base.TestServerArgs{}) + defer s.Stopper().Stop(context.Background()) + scKVSubscriber := s.SpanConfigKVSubscriber().(spanconfig.KVSubscriber) + + conf, err := scKVSubscriber.GetSpanConfigForKey(ctx, roachpb.RKey("non-existent")) + require.NoError(t, err) + require.Equal(t, int32(42), conf.NumReplicas) +} + type mockSpanConfigSubscriber struct { callback func(ctx context.Context, config roachpb.Span) spanconfig.Store diff --git a/pkg/server/server.go b/pkg/server/server.go index 3bd6c9f8442b..df35c6abc03e 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -79,6 +79,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/ts" "github.com/cockroachdb/cockroach/pkg/util" "github.com/cockroachdb/cockroach/pkg/util/admission" + "github.com/cockroachdb/cockroach/pkg/util/envutil" "github.com/cockroachdb/cockroach/pkg/util/goschedstats" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/log" @@ -600,6 +601,12 @@ func NewServer(cfg Config, stopper *stop.Stopper) (*Server, error) { // really only applies to user table ranges fallbackConf.GCPolicy.IgnoreStrictEnforcement = true + // fallbackSpanConfigNumReplicasOverride controls what replication + // factor is used for ranges with no explicit span configs set. + var fallbackSpanConfigNumReplicasOverride = envutil.EnvOrDefaultInt( + "COCKROACH_FALLBACK_SPANCONFIG_NUM_REPLICAS_OVERRIDE", int(fallbackConf.NumReplicas)) + fallbackConf.NumReplicas = int32(fallbackSpanConfigNumReplicasOverride) + spanConfig.subscriber = spanconfigkvsubscriber.New( clock, rangeFeedFactory,