Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/tidb-server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ go_test(
srcs = ["main_test.go"],
embed = [":tidb-server_lib"],
flaky = True,
shard_count = 7,
shard_count = 9,
deps = [
"//pkg/config",
"//pkg/config/deploymode",
Expand Down
42 changes: 42 additions & 0 deletions cmd/tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"flag"
"fmt"
"io/fs"
"maps"
"os"
"runtime"
"strconv"
Expand Down Expand Up @@ -339,6 +340,7 @@ func main() {
}

var standbyController server.StandbyController
var activationMetadata map[string]string
if config.GetGlobalConfig().Standby.StandByMode {
standbyController = standby.NewLoadKeyspaceController()
}
Expand All @@ -355,11 +357,18 @@ func main() {
defer standbyController.EndStandby(err)
// need to validate config again in case of config change via standby
terror.MustNil(config.GetGlobalConfig().Valid())
if c, ok := standbyController.(*standby.LoadKeyspaceController); ok {
activationMetadata = c.ActivationMetadata()
}
}

signal.SetupUSR1Handler()
err = registerStores()
terror.MustNil(err)
if deploymode.IsStarter() {
err = prepareKeyspaceObservabilityForStarter(activationMetadata)
terror.MustNil(err)
}
err = metricsutil.RegisterMetrics()
terror.MustNil(err)

Expand Down Expand Up @@ -1233,6 +1242,39 @@ func closeStmtSummary() {
}
}

const (
keyspaceNameMetricLabel = "keyspace_name"
)

func prepareKeyspaceObservabilityForStarter(metadata map[string]string) error {
cfg := config.GetGlobalConfig()

if cfg.Store != config.StoreTypeTiKV {
return nil
}

resolvedValues := config.KeyspaceObservabilityValues{
MetricLabels: map[string]string{
keyspaceNameMetricLabel: cfg.KeyspaceName,
},
}

copiedConfig := *config.GetGlobalConfig()
if err := copiedConfig.ResolveKeyspaceObservability(metadata); err != nil {
return err
}
configuredValues := copiedConfig.KeyspaceObservabilityValues.Clone()
maps.Copy(resolvedValues.MetricLabels, configuredValues.MetricLabels)
resolvedValues.SlowLogFields = configuredValues.SlowLogFields
resolvedValues.StmtLogFields = configuredValues.StmtLogFields

config.UpdateGlobal(func(conf *config.Config) {
conf.KeyspaceObservabilityValues = resolvedValues
})

return nil
}

func enablePyroscope() {
if os.Getenv("PYROSCOPE_SERVER_ADDRESS") != "" {
runtime.SetMutexProfileFraction(5)
Expand Down
68 changes: 68 additions & 0 deletions cmd/tidb-server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,71 @@ func TestSetVersionByConfigNormalizeLegacyPlaceholderForNextGen(t *testing.T) {
require.Equal(t, "v26.3.0", mysql.TiDBReleaseVersion)
require.Equal(t, "8.0.11-TiDB-CLOUD.202603.0", mysql.ServerVersion)
}

func TestSetupKeyspaceObservabilityForStarter(t *testing.T) {
if kerneltype.IsClassic() {
t.Skip("only for nextgen kernel")
}
restore := config.RestoreFunc()
defer restore()

originalMode := deploymode.Get()
t.Cleanup(func() {
require.NoError(t, deploymode.Set(originalMode))
})
require.NoError(t, deploymode.Set(deploymode.Starter))

config.UpdateGlobal(func(conf *config.Config) {
conf.Store = config.StoreTypeTiKV
conf.KeyspaceName = "ks"
})
err := prepareKeyspaceObservabilityForStarter(nil)
require.NoError(t, err)
require.Equal(t, map[string]string{"keyspace_name": "ks"}, config.GetGlobalConfig().GetKeyspaceObservabilityMetricLabels())

config.UpdateGlobal(func(conf *config.Config) {
conf.KeyspaceObservability = config.KeyspaceObservability{
Fields: []config.KeyspaceObservabilityField{{
Source: "meta_a",
MetricLabel: "keyspace_meta_label_a",
SlowLogField: "Keyspace_meta_slow_a",
StmtLogField: "stmt_meta_a",
Required: true,
}},
}
})

err = prepareKeyspaceObservabilityForStarter(map[string]string{
"meta_a": "value_a",
})
require.NoError(t, err)

cfg := config.GetGlobalConfig()
require.Equal(t, map[string]string{"keyspace_name": "ks", "keyspace_meta_label_a": "value_a"}, cfg.GetKeyspaceObservabilityMetricLabels())
require.Equal(t, []config.KeyspaceObservabilityLogField{
{Name: "Keyspace_meta_slow_a", Value: "value_a"},
}, cfg.GetKeyspaceObservabilitySlowLogFields())
require.Equal(t, map[string]string{"stmt_meta_a": "value_a"}, cfg.GetKeyspaceObservabilityStmtLogFields())
}

func TestSetupKeyspaceObservabilityForStarterSkipsNonTiKV(t *testing.T) {
if kerneltype.IsClassic() {
t.Skip("only for nextgen kernel")
}
restore := config.RestoreFunc()
defer restore()
originalMode := deploymode.Get()
t.Cleanup(func() {
require.NoError(t, deploymode.Set(originalMode))
})
require.NoError(t, deploymode.Set(deploymode.Starter))

config.UpdateGlobal(func(conf *config.Config) {
conf.Store = config.StoreTypeUniStore
conf.Path = "invalid-pd-path"
conf.KeyspaceName = "test_keyspace"
})

require.NoError(t, prepareKeyspaceObservabilityForStarter(nil))
require.Empty(t, config.GetGlobalConfig().GetKeyspaceObservabilityMetricLabels())
}
4 changes: 3 additions & 1 deletion pkg/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go_library(
"config.go",
"config_util.go",
"const.go",
"keyspace_observability.go",
"store.go",
"tiflash.go",
],
Expand All @@ -23,6 +24,7 @@ go_library(
"@com_github_burntsushi_toml//:toml",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_log//:log",
"@com_github_prometheus_common//model",
"@com_github_tikv_client_go_v2//config",
"@com_github_uber_jaeger_client_go//config",
"@org_uber_go_atomic//:atomic",
Expand All @@ -42,7 +44,7 @@ go_test(
data = glob(["**"]),
embed = [":config"],
flaky = True,
shard_count = 32,
shard_count = 34,
deps = [
"//pkg/config/deploymode",
"//pkg/config/kerneltype",
Expand Down
9 changes: 9 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ type Config struct {
// key will be the default value of the session variable `txn_scope` for this tidb-server.
Labels map[string]string `toml:"labels" json:"labels"`

KeyspaceObservability KeyspaceObservability `toml:"keyspace-observability" json:"keyspace-observability"`
KeyspaceObservabilityValues KeyspaceObservabilityValues `toml:"-" json:"-"`

// EnableGlobalIndex is deprecated.
EnableGlobalIndex bool `toml:"enable-global-index" json:"enable-global-index"`

Expand Down Expand Up @@ -1561,6 +1564,9 @@ func (c *Config) Valid() error {
if !kerneltype.IsNextGen() && c.DeployMode != deploymode.Premium {
return fmt.Errorf("deploy-mode can only be configured for nextgen TiDB")
}
if len(c.KeyspaceObservability.Fields) > 0 && c.DeployMode != deploymode.Starter {
return fmt.Errorf("keyspace-observability.fields can only be configured when deploy-mode is starter")
}
if c.DXFResourceLimit < MinDXFResourceLimit || c.DXFResourceLimit > MaxDXFResourceLimit {
return fmt.Errorf("dxf-resource-limit should be between %d and %d", MinDXFResourceLimit, MaxDXFResourceLimit)
}
Expand All @@ -1570,6 +1576,9 @@ func (c *Config) Valid() error {
if c.DeployMode == deploymode.Starter && !validMaxAllowedPacket(c.MaxAllowedPacket) {
return fmt.Errorf("max-allowed-packet should be [%d, %d] and a multiple of %d", minMaxAllowedPacket, maxOfMaxAllowedPacket, maxAllowedPacketUnit)
}
if err := c.KeyspaceObservability.Valid(); err != nil {
return err
}
if c.Store == StoreTypeMockTiKV && !c.Instance.TiDBEnableDDL.Load() {
return fmt.Errorf("can't disable DDL on mocktikv")
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/config/config.toml.nextgen.example
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,15 @@ allow-expression-index = false
# engines means allow the tidb server read data from which types of engines. options: "tikv", "tiflash", "tidb".
engines = ["tikv", "tiflash", "tidb"]

# Map selected keyspace metadata entries to observability outputs.
# Only valid when deploy-mode is starter.
# [[keyspace-observability.fields]]
# source = "meta_key"
# metric-label = "keyspace_meta_metric_label"
# slow-log-field = "Keyspace_meta_slow_log_field"
# stmt-log-field = "stmt_log_field"
# required = false

# instance scope variables
# These options are also available as a system variable for online configuration
# changes to the system variable do not persist to the cluster. You must make changes
Expand Down
Loading
Loading