From d1d19012363b7340206d6bd03b674ec4b4d1f73c Mon Sep 17 00:00:00 2001 From: Noble Mittal <62551163+beingnoble03@users.noreply.github.com> Date: Thu, 14 Mar 2024 14:27:38 +0530 Subject: [PATCH] Add unmanaged tablet flag at vttablet level (#14871) Signed-off-by: Noble Mittal Signed-off-by: Manan Gupta Co-authored-by: Manan Gupta --- changelog/20.0/20.0.0/summary.md | 7 +++ go/cmd/vttablet/cli/cli.go | 2 +- go/flags/endtoend/vtcombo.txt | 2 +- go/flags/endtoend/vttablet.txt | 4 +- go/flags/endtoend/vttestserver.txt | 1 - go/vt/mysqlctl/mysqld.go | 10 +++- .../vttablet/tabletserver/tabletenv/config.go | 54 +++++++++++++++++++ .../tabletserver/tabletenv/config_test.go | 37 +++++++++++++ 8 files changed, 111 insertions(+), 6 deletions(-) diff --git a/changelog/20.0/20.0.0/summary.md b/changelog/20.0/20.0.0/summary.md index 16863248e68..15455ef502c 100644 --- a/changelog/20.0/20.0.0/summary.md +++ b/changelog/20.0/20.0.0/summary.md @@ -5,6 +5,7 @@ - **[Major Changes](#major-changes)** - **[Breaking changes](#breaking-changes)** - [`shutdown_grace_period` Default Change](#shutdown-grace-period-default) + - [New `unmanaged` Flag and `disable_active_reparents` deprecation](#unmanaged-flag) - **[Query Compatibility](#query-compatibility)** - [Vindex Hints](#vindex-hints) - [Update with Limit Support](#update-limit) @@ -31,6 +32,12 @@ This makes reparenting in Vitess resilient to client errors, and prevents Planne In order to preserve the old behaviour, the users can set the flag back to `0 seconds` causing open transactions to never be shutdown, but in that case, they run the risk of PlannedReparentShard calls timing out. +#### New `unmanaged` Flag and `disable_active_reparents` deprecation + +New flag `--unmanaged` has been introduced in this release to make it easier to flag unmanaged tablets. It also runs validations to make sure the unmanaged tablets are configured properly. `--disable_active_reparents` flag has been deprecated for `vttablet`, `vtcombo` and `vttestserver` binaries and will be removed in future releases. Specifying the `--unmanaged` flag will also block replication commands and replication repairs. + +Starting this release, all unmanaged tablets should specify this flag. + ### Query Compatibility #### Vindex Hints diff --git a/go/cmd/vttablet/cli/cli.go b/go/cmd/vttablet/cli/cli.go index 4e97b7f6fa0..b64c5d92b62 100644 --- a/go/cmd/vttablet/cli/cli.go +++ b/go/cmd/vttablet/cli/cli.go @@ -82,7 +82,7 @@ See "Unmanaged Tablet" for the full guide. Even if a MySQL is external, you can still make vttablet perform some management functions. They are as follows: ` + - "* `--disable_active_reparents`: If this flag is set, then any reparent or replica commands will not be allowed. These are InitShardPrimary, PlannedReparentShard, EmergencyReparentShard, and ReparentTablet. In this mode, you should use the TabletExternallyReparented command to inform vitess of the current primary.\n" + + "* `--unmanaged`: This flag indicates that this tablet is running in unmanaged mode. In this mode, any reparent or replica commands are not allowed. These are InitShardPrimary, PlannedReparentShard, EmergencyReparentShard, and ReparentTablet. You should use the TabletExternallyReparented command to inform vitess of the current primary.\n" + "* `--replication_connect_retry`: This value is give to mysql when it connects a replica to the primary as the retry duration parameter.\n" + "* `--heartbeat_enable` and `--heartbeat_interval duration`: cause vttablet to write heartbeats to the sidecar database. This information is also used by the replication reporter to assess replica lag.\n", Example: ` diff --git a/go/flags/endtoend/vtcombo.txt b/go/flags/endtoend/vtcombo.txt index a508b78dc80..ee759218d9c 100644 --- a/go/flags/endtoend/vtcombo.txt +++ b/go/flags/endtoend/vtcombo.txt @@ -108,7 +108,6 @@ Flags: --ddl_strategy string Set default strategy for DDL statements. Override with @@ddl_strategy session variable (default "direct") --default_tablet_type topodatapb.TabletType The default tablet type to set for queries, when one is not explicitly selected. (default PRIMARY) --degraded_threshold duration replication lag after which a replica is considered degraded (default 30s) - --disable_active_reparents if set, do not allow active reparents. Use this to protect a cluster using external reparents. --emit_stats If set, emit stats to push-based monitoring and stats backends --enable-consolidator Synonym to -enable_consolidator (default true) --enable-consolidator-replicas Synonym to -enable_consolidator_replicas @@ -397,6 +396,7 @@ Flags: --tx_throttler_config string The configuration of the transaction throttler as a text-formatted throttlerdata.Configuration protocol buffer message. (default "target_replication_lag_sec:2 max_replication_lag_sec:10 initial_rate:100 max_increase:1 emergency_decrease:0.5 min_duration_between_increases_sec:40 max_duration_between_increases_sec:62 min_duration_between_decreases_sec:20 spread_backlog_across_sec:20 age_bad_rate_after_sec:180 bad_rate_increase:0.1 max_rate_approach_threshold:0.9") --tx_throttler_healthcheck_cells strings A comma-separated list of cells. Only tabletservers running in these cells will be monitored for replication lag by the transaction throttler. --unhealthy_threshold duration replication lag after which a replica is considered unhealthy (default 2h0m0s) + --unmanaged Indicates an unmanaged tablet, i.e. using an external mysql-compatible database --v Level log level for V logs -v, --version print binary version --vmodule vModuleFlag comma-separated list of pattern=N settings for file-filtered logging diff --git a/go/flags/endtoend/vttablet.txt b/go/flags/endtoend/vttablet.txt index ed326ec68a9..9f089275aba 100644 --- a/go/flags/endtoend/vttablet.txt +++ b/go/flags/endtoend/vttablet.txt @@ -17,7 +17,7 @@ See "Unmanaged Tablet" for the full guide. Even if a MySQL is external, you can still make vttablet perform some management functions. They are as follows: -* `--disable_active_reparents`: If this flag is set, then any reparent or replica commands will not be allowed. These are InitShardPrimary, PlannedReparentShard, EmergencyReparentShard, and ReparentTablet. In this mode, you should use the TabletExternallyReparented command to inform vitess of the current primary. +* `--unmanaged`: This flag indicates that this tablet is running in unmanaged mode. In this mode, any reparent or replica commands are not allowed. These are InitShardPrimary, PlannedReparentShard, EmergencyReparentShard, and ReparentTablet. You should use the TabletExternallyReparented command to inform vitess of the current primary. * `--replication_connect_retry`: This value is give to mysql when it connects a replica to the primary as the retry duration parameter. * `--heartbeat_enable` and `--heartbeat_interval duration`: cause vttablet to write heartbeats to the sidecar database. This information is also used by the replication reporter to assess replica lag. @@ -139,7 +139,6 @@ Flags: --dba_idle_timeout duration Idle timeout for dba connections (default 1m0s) --dba_pool_size int Size of the connection pool for dba connections (default 20) --degraded_threshold duration replication lag after which a replica is considered degraded (default 30s) - --disable_active_reparents if set, do not allow active reparents. Use this to protect a cluster using external reparents. --emit_stats If set, emit stats to push-based monitoring and stats backends --enable-consolidator Synonym to -enable_consolidator (default true) --enable-consolidator-replicas Synonym to -enable_consolidator_replicas @@ -398,6 +397,7 @@ Flags: --tx_throttler_config string The configuration of the transaction throttler as a text-formatted throttlerdata.Configuration protocol buffer message. (default "target_replication_lag_sec:2 max_replication_lag_sec:10 initial_rate:100 max_increase:1 emergency_decrease:0.5 min_duration_between_increases_sec:40 max_duration_between_increases_sec:62 min_duration_between_decreases_sec:20 spread_backlog_across_sec:20 age_bad_rate_after_sec:180 bad_rate_increase:0.1 max_rate_approach_threshold:0.9") --tx_throttler_healthcheck_cells strings A comma-separated list of cells. Only tabletservers running in these cells will be monitored for replication lag by the transaction throttler. --unhealthy_threshold duration replication lag after which a replica is considered unhealthy (default 2h0m0s) + --unmanaged Indicates an unmanaged tablet, i.e. using an external mysql-compatible database --v Level log level for V logs -v, --version print binary version --vmodule vModuleFlag comma-separated list of pattern=N settings for file-filtered logging diff --git a/go/flags/endtoend/vttestserver.txt b/go/flags/endtoend/vttestserver.txt index e650ef536f7..7c0df363c15 100644 --- a/go/flags/endtoend/vttestserver.txt +++ b/go/flags/endtoend/vttestserver.txt @@ -31,7 +31,6 @@ Flags: --dba_idle_timeout duration Idle timeout for dba connections (default 1m0s) --dba_pool_size int Size of the connection pool for dba connections (default 20) --default_schema_dir string Default directory for initial schema files. If no schema is found in schema_dir, default to this location. - --disable_active_reparents if set, do not allow active reparents. Use this to protect a cluster using external reparents. --enable_direct_ddl Allow users to submit direct DDL statements (default true) --enable_online_ddl Allow users to submit, review and control Online DDL (default true) --enable_system_settings This will enable the system settings to be changed per session at the database connection level (default true) diff --git a/go/vt/mysqlctl/mysqld.go b/go/vt/mysqlctl/mysqld.go index 63971339f13..d866aa70f65 100644 --- a/go/vt/mysqlctl/mysqld.go +++ b/go/vt/mysqlctl/mysqld.go @@ -122,9 +122,12 @@ func init() { for _, cmd := range []string{"mysqlctl", "mysqlctld", "vtcombo", "vttablet", "vttestserver"} { servenv.OnParseFor(cmd, registerMySQLDFlags) } - for _, cmd := range []string{"vtcombo", "vttablet", "vttestserver", "vtctld", "vtctldclient"} { + for _, cmd := range []string{"vtctld", "vtctldclient"} { servenv.OnParseFor(cmd, registerReparentFlags) } + for _, cmd := range []string{"vtcombo", "vttablet", "vttestserver"} { + servenv.OnParseFor(cmd, registerDeprecatedReparentFlags) + } for _, cmd := range []string{"mysqlctl", "mysqlctld", "vtcombo", "vttablet", "vttestserver"} { servenv.OnParseFor(cmd, registerPoolFlags) } @@ -141,6 +144,11 @@ func registerReparentFlags(fs *pflag.FlagSet) { fs.BoolVar(&DisableActiveReparents, "disable_active_reparents", DisableActiveReparents, "if set, do not allow active reparents. Use this to protect a cluster using external reparents.") } +func registerDeprecatedReparentFlags(fs *pflag.FlagSet) { + fs.BoolVar(&DisableActiveReparents, "disable_active_reparents", DisableActiveReparents, "if set, do not allow active reparents. Use this to protect a cluster using external reparents.") + fs.MarkDeprecated("disable_active_reparents", "Use --unmanaged flag instead for unmanaged tablets.") +} + func registerPoolFlags(fs *pflag.FlagSet) { fs.IntVar(&dbaPoolSize, "dba_pool_size", dbaPoolSize, "Size of the connection pool for dba connections") fs.DurationVar(&DbaIdleTimeout, "dba_idle_timeout", DbaIdleTimeout, "Idle timeout for dba connections") diff --git a/go/vt/vttablet/tabletserver/tabletenv/config.go b/go/vt/vttablet/tabletserver/tabletenv/config.go index 72682a75e30..7df97400acd 100644 --- a/go/vt/vttablet/tabletserver/tabletenv/config.go +++ b/go/vt/vttablet/tabletserver/tabletenv/config.go @@ -17,6 +17,7 @@ limitations under the License. package tabletenv import ( + "context" "encoding/json" "errors" "fmt" @@ -27,9 +28,11 @@ import ( "google.golang.org/protobuf/encoding/prototext" "vitess.io/vitess/go/flagutil" + "vitess.io/vitess/go/mysql" "vitess.io/vitess/go/streamlog" "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/log" + "vitess.io/vitess/go/vt/mysqlctl" "vitess.io/vitess/go/vt/servenv" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/throttler" @@ -213,6 +216,8 @@ func registerTabletEnvFlags(fs *pflag.FlagSet) { fs.BoolVar(¤tConfig.EnableViews, "queryserver-enable-views", false, "Enable views support in vttablet.") fs.BoolVar(¤tConfig.EnablePerWorkloadTableMetrics, "enable-per-workload-table-metrics", defaultConfig.EnablePerWorkloadTableMetrics, "If true, query counts and query error metrics include a label that identifies the workload") + + fs.BoolVar(¤tConfig.Unmanaged, "unmanaged", false, "Indicates an unmanaged tablet, i.e. using an external mysql-compatible database") } var ( @@ -298,6 +303,8 @@ func Init() { type TabletConfig struct { DB *dbconfigs.DBConfigs `json:"db,omitempty"` + Unmanaged bool `json:"unmanaged,omitempty"` + OltpReadPool ConnPoolConfig `json:"oltpReadPool,omitempty"` OlapReadPool ConnPoolConfig `json:"olapReadPool,omitempty"` TxPool ConnPoolConfig `json:"txPool,omitempty"` @@ -861,6 +868,9 @@ func (c *TabletConfig) TxTimeoutForWorkload(workload querypb.ExecuteOptions_Work // Verify checks for contradicting flags. func (c *TabletConfig) Verify() error { + if err := c.verifyUnmanagedTabletConfig(); err != nil { + return err + } if err := c.verifyTransactionLimitConfig(); err != nil { return err } @@ -882,6 +892,50 @@ func (c *TabletConfig) Verify() error { return nil } +// verifyUnmanagedTabletConfig checks unmanaged tablet related config for sanity +func (c *TabletConfig) verifyUnmanagedTabletConfig() error { + // Skip checks if tablet is not unmanaged + if !c.Unmanaged { + return nil + } + + // Throw error if both host and socket are null + if !c.DB.HasGlobalSettings() { + return errors.New("no connection parameters specified but unmanaged mode specified") + } + if c.DB.App.User == "" { + return errors.New("database app user not specified") + } + if c.DB.App.Password == "" { + return errors.New("database app user password not specified") + } + // Replication fixes should be disabled for Unmanaged tablets. + mysqlctl.DisableActiveReparents = true + + return c.checkConnectionForExternalMysql() +} + +// Test connectivity of external mysql +func (c *TabletConfig) checkConnectionForExternalMysql() error { + params := mysql.ConnParams{ + Host: c.DB.Host, + Port: c.DB.Port, + DbName: c.DB.DBName, + Uname: c.DB.App.User, + Pass: c.DB.App.Password, + UnixSocket: c.DB.Socket, + } + + conn, err := mysql.Connect(context.Background(), ¶ms) + if err != nil { + return err + } + + defer conn.Close() + + return conn.Ping() +} + // verifyTransactionLimitConfig checks TransactionLimitConfig for sanity func (c *TabletConfig) verifyTransactionLimitConfig() error { actual, dryRun := c.EnableTransactionLimit, c.EnableTransactionLimitDryRun diff --git a/go/vt/vttablet/tabletserver/tabletenv/config_test.go b/go/vt/vttablet/tabletserver/tabletenv/config_test.go index ace094ac899..a51a3c599e8 100644 --- a/go/vt/vttablet/tabletserver/tabletenv/config_test.go +++ b/go/vt/vttablet/tabletserver/tabletenv/config_test.go @@ -24,8 +24,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/test/utils" "vitess.io/vitess/go/vt/dbconfigs" + "vitess.io/vitess/go/vt/mysqlctl" "vitess.io/vitess/go/vt/throttler" "vitess.io/vitess/go/vt/topo/topoproto" "vitess.io/vitess/go/vt/vterrors" @@ -444,3 +446,38 @@ func TestVerifyTxThrottlerConfig(t *testing.T) { }) } } + +func TestVerifyUnmanagedTabletConfig(t *testing.T) { + oldDisableActiveReparents := mysqlctl.DisableActiveReparents + defer func() { + mysqlctl.DisableActiveReparents = oldDisableActiveReparents + }() + + config := defaultConfig + + db := fakesqldb.New(t) + defer db.Close() + + params := db.ConnParams() + config.DB = dbconfigs.NewTestDBConfigs(*params, *params, "") + + // By default, unmanaged mode should be false + err := config.verifyUnmanagedTabletConfig() + assert.Nil(t, err) + + config.Unmanaged = true + err = config.verifyUnmanagedTabletConfig() + assert.EqualError(t, err, "no connection parameters specified but unmanaged mode specified") + + config.DB.Socket = db.ConnParams().UnixSocket + err = config.verifyUnmanagedTabletConfig() + assert.EqualError(t, err, "database app user not specified") + + config.DB.App.User = "testUser" + err = config.verifyUnmanagedTabletConfig() + assert.EqualError(t, err, "database app user password not specified") + + config.DB.App.Password = "testPassword" + err = config.verifyUnmanagedTabletConfig() + assert.Nil(t, err) +}