From d5456a246b9531c9902ba1d1f6a0bb9d31681c62 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 3 Dec 2024 02:21:07 +0100 Subject: [PATCH 01/15] `vtorc`: fetch shard names only every `--topo-information-refresh-duration` Signed-off-by: Tim Vaillancourt --- go/vt/vtorc/logic/keyspace_shard_discovery.go | 44 ++++++++++++++++++- .../logic/keyspace_shard_discovery_test.go | 17 +++++++ go/vt/vtorc/logic/tablet_discovery.go | 10 +---- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/go/vt/vtorc/logic/keyspace_shard_discovery.go b/go/vt/vtorc/logic/keyspace_shard_discovery.go index b1e93fe2a01..78d468c397f 100644 --- a/go/vt/vtorc/logic/keyspace_shard_discovery.go +++ b/go/vt/vtorc/logic/keyspace_shard_discovery.go @@ -22,12 +22,43 @@ import ( "strings" "sync" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/log" - "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/vtorc/inst" ) +var ( + // keyspaceShardNames stores the current names of shards by keyspace. + keyspaceShardNames = make(map[string][]string) + keyspaceShardNamesMu sync.Mutex + statsKeyspaceShardsWatched = stats.NewGaugesFuncWithMultiLabels("KeyspaceShardsWatched", + "The keyspace/shards watched by VTOrc", + []string{"Keyspace", "Shard"}, + getKeyspaceShardsStats, + ) +) + +// getKeyspaceShardsStats returns the current keyspace/shards watched in stats format. +func getKeyspaceShardsStats() map[string]int64 { + keyspaceShardNamesMu.Lock() + defer keyspaceShardNamesMu.Unlock() + keyspaceShards := make(map[string]int64) + for ks, shards := range keyspaceShardNames { + for _, shard := range shards { + keyspaceShards[ks+"."+shard] = 1 + } + } + return keyspaceShards +} + +// GetKeyspaceShardNames returns the names of the shards in a given keyspace. +func GetKeyspaceShardNames(keyspaceName string) []string { + keyspaceShardNamesMu.Lock() + defer keyspaceShardNamesMu.Unlock() + return keyspaceShardNames[keyspaceName] +} + // RefreshAllKeyspacesAndShards reloads the keyspace and shard information for the keyspaces that vtorc is concerned with. func RefreshAllKeyspacesAndShards() { var keyspaces []string @@ -134,13 +165,22 @@ func refreshAllShards(ctx context.Context, keyspaceName string) error { log.Error(err) return err } - for _, shardInfo := range shardInfos { + + shardNames := make([]string, 0, len(shardInfos)) + for shardName, shardInfo := range shardInfos { err = inst.SaveShard(shardInfo) if err != nil { log.Error(err) return err } + shardNames = append(shardNames, shardName) } + sort.Strings(shardNames) + + keyspaceShardNamesMu.Lock() + defer keyspaceShardNamesMu.Unlock() + keyspaceShardNames[keyspaceName] = shardNames + return nil } diff --git a/go/vt/vtorc/logic/keyspace_shard_discovery_test.go b/go/vt/vtorc/logic/keyspace_shard_discovery_test.go index 097865db84a..5ecee3980a9 100644 --- a/go/vt/vtorc/logic/keyspace_shard_discovery_test.go +++ b/go/vt/vtorc/logic/keyspace_shard_discovery_test.go @@ -52,6 +52,12 @@ var ( ) func TestRefreshAllKeyspaces(t *testing.T) { + // reset keyspaceShardNames + keyspaceShardNames = make(map[string][]string) + defer func() { + keyspaceShardNames = make(map[string][]string) + }() + // Store the old flags and restore on test completion oldTs := ts oldClustersToWatch := clustersToWatch @@ -119,6 +125,17 @@ func TestRefreshAllKeyspaces(t *testing.T) { verifyKeyspaceInfo(t, "ks4", keyspaceDurabilityTest, "") verifyPrimaryAlias(t, "ks4", "80-", "zone_ks4-0000000101", "") + // Confirm caching of shard names + require.Equal(t, map[string][]string{ + "ks1": {"-80", "80-"}, + "ks2": {"-80", "80-"}, + "ks3": {"-80", "80-"}, + "ks4": {"-80", "80-"}, + }, keyspaceShardNames) + for _, ksName := range keyspaceNames { + require.Equal(t, []string{"-80", "80-"}, GetKeyspaceShardNames(ksName)) + } + require.Len(t, GetKeyspaceShardNames("does-not-exist"), 0) } func TestRefreshKeyspace(t *testing.T) { diff --git a/go/vt/vtorc/logic/tablet_discovery.go b/go/vt/vtorc/logic/tablet_discovery.go index 1ee9e2cfefa..785e0bba506 100644 --- a/go/vt/vtorc/logic/tablet_discovery.go +++ b/go/vt/vtorc/logic/tablet_discovery.go @@ -118,15 +118,7 @@ func refreshTabletsUsing(loader func(tabletAlias string), forceRefresh bool) { input := strings.Split(ks, "/") keyspaceShards = append(keyspaceShards, &topo.KeyspaceShard{Keyspace: input[0], Shard: input[1]}) } else { - // Assume this is a keyspace and find all shards in keyspace - ctx, cancel := context.WithTimeout(context.Background(), topo.RemoteOperationTimeout) - defer cancel() - shards, err := ts.GetShardNames(ctx, ks) - if err != nil { - // Log the errr and continue - log.Errorf("Error fetching shards for keyspace: %v", ks) - continue - } + shards := GetKeyspaceShardNames(ks) if len(shards) == 0 { log.Errorf("Topo has no shards for ks: %v", ks) continue From 67c0d6821c8eac5de7a4996559109893680eaae4 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 3 Dec 2024 07:08:38 +0100 Subject: [PATCH 02/15] empty commit Signed-off-by: Tim Vaillancourt From 03d774e1f442620c46a1a00d13b262b5f39e614d Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 17 Dec 2024 01:51:11 +0100 Subject: [PATCH 03/15] use backend Signed-off-by: Tim Vaillancourt --- go/vt/vtorc/db/generate_base.go | 16 +++-- go/vt/vtorc/inst/shard_dao.go | 58 +++++++++++++++---- go/vt/vtorc/inst/shard_dao_test.go | 34 +++++++++++ go/vt/vtorc/logic/keyspace_shard_discovery.go | 50 ++++++---------- .../logic/keyspace_shard_discovery_test.go | 29 +++++++--- go/vt/vtorc/logic/tablet_discovery.go | 6 +- 6 files changed, 134 insertions(+), 59 deletions(-) diff --git a/go/vt/vtorc/db/generate_base.go b/go/vt/vtorc/db/generate_base.go index f997dc6ac0a..34bf2506de3 100644 --- a/go/vt/vtorc/db/generate_base.go +++ b/go/vt/vtorc/db/generate_base.go @@ -305,24 +305,28 @@ CREATE TABLE vitess_shard ( shard varchar(128) NOT NULL, primary_alias varchar(512) NOT NULL, primary_timestamp varchar(512) NOT NULL, + updated_timestamp timestamp NOT NULL, PRIMARY KEY (keyspace, shard) )`, ` -CREATE INDEX source_host_port_idx_database_instance_database_instance on database_instance (source_host, source_port) +CREATE INDEX source_host_port_idx_database_instance_database_instance ON database_instance (source_host, source_port) `, ` -CREATE INDEX keyspace_shard_idx_topology_recovery on topology_recovery (keyspace, shard) +CREATE INDEX keyspace_shard_idx_topology_recovery ON topology_recovery (keyspace, shard) `, ` -CREATE INDEX end_recovery_idx_topology_recovery on topology_recovery (end_recovery) +CREATE INDEX end_recovery_idx_topology_recovery ON topology_recovery (end_recovery) `, ` -CREATE INDEX instance_timestamp_idx_database_instance_analysis_changelog on database_instance_analysis_changelog (alias, analysis_timestamp) +CREATE INDEX instance_timestamp_idx_database_instance_analysis_changelog ON database_instance_analysis_changelog (alias, analysis_timestamp) `, ` -CREATE INDEX detection_idx_topology_recovery on topology_recovery (detection_id) +CREATE INDEX detection_idx_topology_recovery ON topology_recovery (detection_id) `, ` -CREATE INDEX recovery_id_idx_topology_recovery_steps ON topology_recovery_steps(recovery_id) +CREATE INDEX recovery_id_idx_topology_recovery_steps ON topology_recovery_steps (recovery_id) + `, + ` +CREATE INDEX keyspace_updated_timestamp_idx_vitess_shard ON vitess_shard (keyspace, updated_timestamp) `, } diff --git a/go/vt/vtorc/inst/shard_dao.go b/go/vt/vtorc/inst/shard_dao.go index a90eed0f509..f7fdc0dafc0 100644 --- a/go/vt/vtorc/inst/shard_dao.go +++ b/go/vt/vtorc/inst/shard_dao.go @@ -18,6 +18,7 @@ package inst import ( "errors" + "time" "vitess.io/vitess/go/protoutil" "vitess.io/vitess/go/vt/external/golib/sqlutils" @@ -38,13 +39,12 @@ func ReadShardPrimaryInformation(keyspaceName, shardName string) (primaryAlias s return } - query := ` - select + query := `SELECT primary_alias, primary_timestamp - from + FROM vitess_shard - where keyspace=? and shard=? - ` + WHERE + keyspace = ? AND shard = ?` args := sqlutils.Args(keyspaceName, shardName) shardFound := false err = db.QueryVTOrc(query, args, func(row sqlutils.RowMap) error { @@ -62,14 +62,38 @@ func ReadShardPrimaryInformation(keyspaceName, shardName string) (primaryAlias s return primaryAlias, primaryTimestamp, nil } +// GetAllShardNames returns the names of all keyspace/shards. +func GetAllShardNames() (map[string][]string, error) { + shards := make(map[string][]string, 0) + query := `SELECT keyspace, shard FROM vitess_shard` + err := db.QueryVTOrc(query, nil, func(row sqlutils.RowMap) error { + keyspace := row.GetString("keyspace") + shards[keyspace] = append(shards[keyspace], row.GetString("shard")) + return nil + }) + return shards, err +} + +// GetKeyspaceShardNames returns the names of all shards in a keyspace. +func GetKeyspaceShardNames(keyspaceName string) ([]string, error) { + shards := make([]string, 0) + query := `SELECT shard FROM vitess_shard WHERE keyspace = ?` + args := sqlutils.Args(keyspaceName) + err := db.QueryVTOrc(query, args, func(row sqlutils.RowMap) error { + shards = append(shards, row.GetString("shard")) + return nil + }) + return shards, err +} + // SaveShard saves the shard record against the shard name. func SaveShard(shard *topo.ShardInfo) error { _, err := db.ExecVTOrc(` - replace - into vitess_shard ( - keyspace, shard, primary_alias, primary_timestamp - ) values ( - ?, ?, ?, ? + REPLACE + INTO vitess_shard ( + keyspace, shard, primary_alias, primary_timestamp, updated_timestamp + ) VALUES ( + ?, ?, ?, ?, DATETIME('now') ) `, shard.Keyspace(), @@ -80,6 +104,20 @@ func SaveShard(shard *topo.ShardInfo) error { return err } +// DeleteStaleKeyspaceShards deletes shard records that have not been updated since a provided time. +func DeleteStaleKeyspaceShards(keyspace string, staleTime time.Time) error { + _, err := db.ExecVTOrc(`DELETE FROM vitess_shard + WHERE + keyspace = ? + AND + updated_timestamp < DATETIME(?, 'unixepoch') + `, + keyspace, + staleTime.Unix(), + ) + return err +} + // getShardPrimaryAliasString gets the shard primary alias to be stored as a string in the database. func getShardPrimaryAliasString(shard *topo.ShardInfo) string { if shard.PrimaryAlias == nil { diff --git a/go/vt/vtorc/inst/shard_dao_test.go b/go/vt/vtorc/inst/shard_dao_test.go index 84f6aef7a4a..bbc781ae40c 100644 --- a/go/vt/vtorc/inst/shard_dao_test.go +++ b/go/vt/vtorc/inst/shard_dao_test.go @@ -104,3 +104,37 @@ func TestSaveAndReadShard(t *testing.T) { }) } } + +func TestGetAllShardNames(t *testing.T) { + // Clear the database after the test. The easiest way to do that is to run all the initialization commands again. + defer func() { + db.ClearVTOrcDatabase() + }() + + shardInfo := topo.NewShardInfo("ks1", "-80", &topodatapb.Shard{}, nil) + err := SaveShard(shardInfo) + require.NoError(t, err) + + shardNames, err := GetAllShardNames() + require.NoError(t, err) + require.Equal(t, map[string][]string{ + "ks1": {"-80"}, + }, shardNames) +} + +func TestGetKeyspaceShardNames(t *testing.T) { + // Clear the database after the test. The easiest way to do that is to run all the initialization commands again. + defer func() { + db.ClearVTOrcDatabase() + }() + + for _, shardName := range []string{"-80", "80-"} { + shardInfo := topo.NewShardInfo("ks1", shardName, &topodatapb.Shard{}, nil) + err := SaveShard(shardInfo) + require.NoError(t, err) + } + + shardNames, err := GetKeyspaceShardNames("ks1") + require.NoError(t, err) + require.Equal(t, []string{"-80", "80-"}, shardNames) +} diff --git a/go/vt/vtorc/logic/keyspace_shard_discovery.go b/go/vt/vtorc/logic/keyspace_shard_discovery.go index 31b525e4665..4473e993c37 100644 --- a/go/vt/vtorc/logic/keyspace_shard_discovery.go +++ b/go/vt/vtorc/logic/keyspace_shard_discovery.go @@ -21,6 +21,7 @@ import ( "sort" "strings" "sync" + "time" "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/log" @@ -28,35 +29,26 @@ import ( "vitess.io/vitess/go/vt/vtorc/inst" ) -var ( - // keyspaceShardNames stores the current names of shards by keyspace. - keyspaceShardNames = make(map[string][]string) - keyspaceShardNamesMu sync.Mutex - statsKeyspaceShardsWatched = stats.NewGaugesFuncWithMultiLabels("KeyspaceShardsWatched", - "The keyspace/shards watched by VTOrc", - []string{"Keyspace", "Shard"}, - getKeyspaceShardsStats, - ) +var statsKeyspaceShardsWatched = stats.NewGaugesFuncWithMultiLabels("KeyspaceShardsWatched", + "The keyspace/shards watched by VTOrc", + []string{"Keyspace", "Shard"}, + getKeyspaceShardsStats, ) // getKeyspaceShardsStats returns the current keyspace/shards watched in stats format. func getKeyspaceShardsStats() map[string]int64 { - keyspaceShardNamesMu.Lock() - defer keyspaceShardNamesMu.Unlock() - keyspaceShards := make(map[string]int64) - for ks, shards := range keyspaceShardNames { + ksShardNames, err := inst.GetAllShardNames() + if err != nil { + log.Errorf("Failed to get shards from backend: %+v", err) + return nil + } + stats := make(map[string]int64, 0) + for keyspace, shards := range ksShardNames { for _, shard := range shards { - keyspaceShards[ks+"."+shard] = 1 + stats[keyspace+"."+shard] = 1 } } - return keyspaceShards -} - -// GetKeyspaceShardNames returns the names of the shards in a given keyspace. -func GetKeyspaceShardNames(keyspaceName string) []string { - keyspaceShardNamesMu.Lock() - defer keyspaceShardNamesMu.Unlock() - return keyspaceShardNames[keyspaceName] + return stats } // RefreshAllKeyspacesAndShards reloads the keyspace and shard information for the keyspaces that vtorc is concerned with. @@ -166,23 +158,15 @@ func refreshAllShards(ctx context.Context, keyspaceName string) error { log.Error(err) return err } - - shardNames := make([]string, 0, len(shardInfos)) - for shardName, shardInfo := range shardInfos { + beginSaveTime := time.Now() + for _, shardInfo := range shardInfos { err = inst.SaveShard(shardInfo) if err != nil { log.Error(err) return err } - shardNames = append(shardNames, shardName) } - sort.Strings(shardNames) - - keyspaceShardNamesMu.Lock() - defer keyspaceShardNamesMu.Unlock() - keyspaceShardNames[keyspaceName] = shardNames - - return nil + return inst.DeleteStaleKeyspaceShards(keyspace, beginSaveTime) } // refreshSingleShardHelper is a helper function that refreshes the shard record of the given keyspace/shard. diff --git a/go/vt/vtorc/logic/keyspace_shard_discovery_test.go b/go/vt/vtorc/logic/keyspace_shard_discovery_test.go index 82370560561..9763551b928 100644 --- a/go/vt/vtorc/logic/keyspace_shard_discovery_test.go +++ b/go/vt/vtorc/logic/keyspace_shard_discovery_test.go @@ -51,13 +51,26 @@ var ( } ) -func TestRefreshAllKeyspaces(t *testing.T) { - // reset keyspaceShardNames - keyspaceShardNames = make(map[string][]string) +func TestGetKeyspaceShardsStats(t *testing.T) { + db.ClearVTOrcDatabase() defer func() { - keyspaceShardNames = make(map[string][]string) + db.ClearVTOrcDatabase() }() + for _, shardName := range []string{"-80", "80-"} { + shardInfo := topo.NewShardInfo("ks1", shardName, &topodatapb.Shard{}, nil) + err := inst.SaveShard(shardInfo) + require.NoError(t, err) + } + + // test using the metric var that calls getKeyspaceShardsStats() + require.Equal(t, map[string]int64{ + "ks1.-80": 1, + "ks1.80-": 1, + }, statsKeyspaceShardsWatched.Counts()) +} + +func TestRefreshAllKeyspaces(t *testing.T) { // Store the old flags and restore on test completion oldTs := ts oldClustersToWatch := clustersToWatch @@ -125,17 +138,15 @@ func TestRefreshAllKeyspaces(t *testing.T) { verifyKeyspaceInfo(t, "ks4", keyspaceDurabilityTest, "") verifyPrimaryAlias(t, "ks4", "80-", "zone_ks4-0000000101", "") - // Confirm caching of shard names + // Confirm GetAllShardNames + keyspaceShardNames, err := inst.GetAllShardNames() + require.NoError(t, err) require.Equal(t, map[string][]string{ "ks1": {"-80", "80-"}, "ks2": {"-80", "80-"}, "ks3": {"-80", "80-"}, "ks4": {"-80", "80-"}, }, keyspaceShardNames) - for _, ksName := range keyspaceNames { - require.Equal(t, []string{"-80", "80-"}, GetKeyspaceShardNames(ksName)) - } - require.Len(t, GetKeyspaceShardNames("does-not-exist"), 0) } func TestRefreshKeyspace(t *testing.T) { diff --git a/go/vt/vtorc/logic/tablet_discovery.go b/go/vt/vtorc/logic/tablet_discovery.go index ab3cc2ae44b..6641db5ecde 100644 --- a/go/vt/vtorc/logic/tablet_discovery.go +++ b/go/vt/vtorc/logic/tablet_discovery.go @@ -113,7 +113,11 @@ func refreshTabletsUsing(ctx context.Context, loader func(tabletAlias string), f input := strings.Split(ks, "/") keyspaceShards = append(keyspaceShards, &topo.KeyspaceShard{Keyspace: input[0], Shard: input[1]}) } else { - shards := GetKeyspaceShardNames(ks) + shards, err := inst.GetKeyspaceShardNames(ks) + if err != nil { + log.Errorf("Failed to get shards for ks %s: %+v", ks, err) + continue + } if len(shards) == 0 { log.Errorf("Topo has no shards for ks: %v", ks) continue From 7c88a97af2f6442d364ca34979b1e6a6c8684236 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 17 Dec 2024 02:12:29 +0100 Subject: [PATCH 04/15] more tests Signed-off-by: Tim Vaillancourt --- go/vt/vtorc/inst/shard_dao.go | 6 +++--- go/vt/vtorc/inst/shard_dao_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/go/vt/vtorc/inst/shard_dao.go b/go/vt/vtorc/inst/shard_dao.go index f7fdc0dafc0..89a9652bf71 100644 --- a/go/vt/vtorc/inst/shard_dao.go +++ b/go/vt/vtorc/inst/shard_dao.go @@ -105,14 +105,14 @@ func SaveShard(shard *topo.ShardInfo) error { } // DeleteStaleKeyspaceShards deletes shard records that have not been updated since a provided time. -func DeleteStaleKeyspaceShards(keyspace string, staleTime time.Time) error { +func DeleteStaleKeyspaceShards(keyspaceName string, staleTime time.Time) error { _, err := db.ExecVTOrc(`DELETE FROM vitess_shard WHERE keyspace = ? AND - updated_timestamp < DATETIME(?, 'unixepoch') + updated_timestamp <= DATETIME(?, 'unixepoch') `, - keyspace, + keyspaceName, staleTime.Unix(), ) return err diff --git a/go/vt/vtorc/inst/shard_dao_test.go b/go/vt/vtorc/inst/shard_dao_test.go index bbc781ae40c..cc6734e30e9 100644 --- a/go/vt/vtorc/inst/shard_dao_test.go +++ b/go/vt/vtorc/inst/shard_dao_test.go @@ -138,3 +138,29 @@ func TestGetKeyspaceShardNames(t *testing.T) { require.NoError(t, err) require.Equal(t, []string{"-80", "80-"}, shardNames) } + +func TestDeleteStaleKeyspaceShards(t *testing.T) { + // Clear the database after the test. The easiest way to do that is to run all the initialization commands again. + defer func() { + db.ClearVTOrcDatabase() + }() + + shardInfo := topo.NewShardInfo("ks1", "-80", &topodatapb.Shard{}, nil) + err := SaveShard(shardInfo) + require.NoError(t, err) + shards, err := GetKeyspaceShardNames("ks1") + require.NoError(t, err) + require.Len(t, shards, 1) + + // test a staletime before save causes no delete + require.NoError(t, DeleteStaleKeyspaceShards("ks1", time.Now().Add(-time.Hour))) + shards, err = GetKeyspaceShardNames("ks1") + require.NoError(t, err) + require.Len(t, shards, 1) + + // test statetime of now deletes everything + require.NoError(t, DeleteStaleKeyspaceShards("ks1", time.Now())) + shards, err = GetKeyspaceShardNames("ks1") + require.NoError(t, err) + require.Len(t, shards, 0) +} From bc1348812ac1482d339b95a4641a85d9ed1452f9 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 17 Dec 2024 02:37:30 +0100 Subject: [PATCH 05/15] fix error Signed-off-by: Tim Vaillancourt --- go/vt/vtorc/logic/keyspace_shard_discovery.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/vt/vtorc/logic/keyspace_shard_discovery.go b/go/vt/vtorc/logic/keyspace_shard_discovery.go index 4473e993c37..cff5b9fa2ac 100644 --- a/go/vt/vtorc/logic/keyspace_shard_discovery.go +++ b/go/vt/vtorc/logic/keyspace_shard_discovery.go @@ -166,7 +166,7 @@ func refreshAllShards(ctx context.Context, keyspaceName string) error { return err } } - return inst.DeleteStaleKeyspaceShards(keyspace, beginSaveTime) + return inst.DeleteStaleKeyspaceShards(keyspaceName, beginSaveTime) } // refreshSingleShardHelper is a helper function that refreshes the shard record of the given keyspace/shard. From 43c5eeb877435b991fb038dd3e3d1847ed0a20c9 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 17 Dec 2024 03:54:14 +0100 Subject: [PATCH 06/15] fix ci Signed-off-by: Tim Vaillancourt --- go/vt/vtorc/inst/analysis_dao_test.go | 4 +-- go/vt/vtorc/inst/keyspace_dao.go | 40 ++++++++++++++++----------- go/vt/vtorc/inst/shard_dao.go | 7 ++--- go/vt/vtorc/inst/shard_dao_test.go | 12 ++++---- 4 files changed, 34 insertions(+), 29 deletions(-) diff --git a/go/vt/vtorc/inst/analysis_dao_test.go b/go/vt/vtorc/inst/analysis_dao_test.go index c061d54ebb3..00ed4d4e9df 100644 --- a/go/vt/vtorc/inst/analysis_dao_test.go +++ b/go/vt/vtorc/inst/analysis_dao_test.go @@ -41,8 +41,8 @@ var ( `INSERT INTO vitess_tablet VALUES('zone1-0000000101','localhost',6714,'ks','0','zone1',1,'2022-12-28 07:23:25.129898+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3130317d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363731337d20706f72745f6d61703a7b6b65793a227674222076616c75653a363731327d206b657973706163653a226b73222073686172643a22302220747970653a5052494d415259206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a36373134207072696d6172795f7465726d5f73746172745f74696d653a7b7365636f6e64733a31363732323132323035206e616e6f7365636f6e64733a3132393839383030307d2064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`, `INSERT INTO vitess_tablet VALUES('zone1-0000000112','localhost',6747,'ks','0','zone1',3,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3131327d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363734367d20706f72745f6d61703a7b6b65793a227674222076616c75653a363734357d206b657973706163653a226b73222073686172643a22302220747970653a52444f4e4c59206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363734372064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`, `INSERT INTO vitess_tablet VALUES('zone2-0000000200','localhost',6756,'ks','0','zone2',2,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653222207569643a3230307d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363735357d20706f72745f6d61703a7b6b65793a227674222076616c75653a363735347d206b657973706163653a226b73222073686172643a22302220747970653a5245504c494341206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363735362064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`, - `INSERT INTO vitess_shard VALUES('ks','0','zone1-0000000101','2022-12-28 07:23:25.129898+00:00');`, - `INSERT INTO vitess_keyspace VALUES('ks',0,'semi_sync');`, + `INSERT INTO vitess_shard VALUES('ks','0','zone1-0000000101','2022-12-28 07:23:25.129898+00:00','2022-12-28 07:23:25.129898+00:00');`, + `INSERT INTO vitess_keyspace VALUES('ks',0,'semi_sync','2022-12-28 07:23:25.129898+00:00');`, } ) diff --git a/go/vt/vtorc/inst/keyspace_dao.go b/go/vt/vtorc/inst/keyspace_dao.go index d764e3fc56a..ce2d075d38b 100644 --- a/go/vt/vtorc/inst/keyspace_dao.go +++ b/go/vt/vtorc/inst/keyspace_dao.go @@ -18,6 +18,7 @@ package inst import ( "errors" + "time" "vitess.io/vitess/go/vt/external/golib/sqlutils" topodatapb "vitess.io/vitess/go/vt/proto/topodata" @@ -35,14 +36,12 @@ func ReadKeyspace(keyspaceName string) (*topo.KeyspaceInfo, error) { return nil, err } - query := ` - select - keyspace_type, - durability_policy - from - vitess_keyspace - where keyspace=? - ` + query := `SELECT + keyspace_type, + durability_policy + FROM + vitess_keyspace + WHERE keyspace = ?` args := sqlutils.Args(keyspaceName) keyspace := &topo.KeyspaceInfo{ Keyspace: &topodatapb.Keyspace{}, @@ -64,14 +63,12 @@ func ReadKeyspace(keyspaceName string) (*topo.KeyspaceInfo, error) { // SaveKeyspace saves the keyspace record against the keyspace name. func SaveKeyspace(keyspace *topo.KeyspaceInfo) error { - _, err := db.ExecVTOrc(` - replace - into vitess_keyspace ( - keyspace, keyspace_type, durability_policy - ) values ( - ?, ?, ? - ) - `, + _, err := db.ExecVTOrc(`REPLACE + INTO vitess_keyspace ( + keyspace, keyspace_type, durability_policy, updated_timestamp + ) VALUES ( + ?, ?, ?, DATETIME('now') + )`, keyspace.KeyspaceName(), int(keyspace.KeyspaceType), keyspace.GetDurabilityPolicy(), @@ -79,6 +76,17 @@ func SaveKeyspace(keyspace *topo.KeyspaceInfo) error { return err } +// DeleteStaleKeyspaces deletes keyspace records that have not been updated since a provided time. +func DeleteStaleKeyspaces(staleTime time.Time) error { + _, err := db.ExecVTOrc(`DELETE FROM vitess_keyspace + WHERE + updated_timestamp <= DATETIME(?, 'unixepoch') + `, + staleTime.Unix(), + ) + return err +} + // GetDurabilityPolicy gets the durability policy for the given keyspace. func GetDurabilityPolicy(keyspace string) (reparentutil.Durabler, error) { ki, err := ReadKeyspace(keyspace) diff --git a/go/vt/vtorc/inst/shard_dao.go b/go/vt/vtorc/inst/shard_dao.go index 89a9652bf71..c1c23dc135a 100644 --- a/go/vt/vtorc/inst/shard_dao.go +++ b/go/vt/vtorc/inst/shard_dao.go @@ -104,15 +104,12 @@ func SaveShard(shard *topo.ShardInfo) error { return err } -// DeleteStaleKeyspaceShards deletes shard records that have not been updated since a provided time. -func DeleteStaleKeyspaceShards(keyspaceName string, staleTime time.Time) error { +// DeleteStaleShards deletes shard records that have not been updated since a provided time. +func DeleteStaleShards(staleTime time.Time) error { _, err := db.ExecVTOrc(`DELETE FROM vitess_shard WHERE - keyspace = ? - AND updated_timestamp <= DATETIME(?, 'unixepoch') `, - keyspaceName, staleTime.Unix(), ) return err diff --git a/go/vt/vtorc/inst/shard_dao_test.go b/go/vt/vtorc/inst/shard_dao_test.go index cc6734e30e9..2d96b042d2d 100644 --- a/go/vt/vtorc/inst/shard_dao_test.go +++ b/go/vt/vtorc/inst/shard_dao_test.go @@ -139,7 +139,7 @@ func TestGetKeyspaceShardNames(t *testing.T) { require.Equal(t, []string{"-80", "80-"}, shardNames) } -func TestDeleteStaleKeyspaceShards(t *testing.T) { +func TestDeleteStaleShards(t *testing.T) { // Clear the database after the test. The easiest way to do that is to run all the initialization commands again. defer func() { db.ClearVTOrcDatabase() @@ -148,19 +148,19 @@ func TestDeleteStaleKeyspaceShards(t *testing.T) { shardInfo := topo.NewShardInfo("ks1", "-80", &topodatapb.Shard{}, nil) err := SaveShard(shardInfo) require.NoError(t, err) - shards, err := GetKeyspaceShardNames("ks1") + shards, err := GetAllShardNames() require.NoError(t, err) require.Len(t, shards, 1) // test a staletime before save causes no delete - require.NoError(t, DeleteStaleKeyspaceShards("ks1", time.Now().Add(-time.Hour))) - shards, err = GetKeyspaceShardNames("ks1") + require.NoError(t, DeleteStaleShards(time.Now().Add(-time.Hour))) + shards, err = GetAllShardNames() require.NoError(t, err) require.Len(t, shards, 1) // test statetime of now deletes everything - require.NoError(t, DeleteStaleKeyspaceShards("ks1", time.Now())) - shards, err = GetKeyspaceShardNames("ks1") + require.NoError(t, DeleteStaleShards(time.Now())) + shards, err = GetAllShardNames() require.NoError(t, err) require.Len(t, shards, 0) } From 343f4347ea6bf8d0f50779f3c72bc515116dd134 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 17 Dec 2024 03:56:11 +0100 Subject: [PATCH 07/15] more tweaks Signed-off-by: Tim Vaillancourt --- go/vt/vtorc/db/generate_base.go | 6 ++- go/vt/vtorc/logic/keyspace_shard_discovery.go | 49 ++++++++++++------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/go/vt/vtorc/db/generate_base.go b/go/vt/vtorc/db/generate_base.go index 34bf2506de3..6538175dc14 100644 --- a/go/vt/vtorc/db/generate_base.go +++ b/go/vt/vtorc/db/generate_base.go @@ -294,6 +294,7 @@ CREATE TABLE vitess_keyspace ( keyspace varchar(128) NOT NULL, keyspace_type smallint(5) NOT NULL, durability_policy varchar(512) NOT NULL, + updated_timestamp timestamp NOT NULL, PRIMARY KEY (keyspace) )`, ` @@ -327,6 +328,9 @@ CREATE INDEX detection_idx_topology_recovery ON topology_recovery (detection_id) CREATE INDEX recovery_id_idx_topology_recovery_steps ON topology_recovery_steps (recovery_id) `, ` -CREATE INDEX keyspace_updated_timestamp_idx_vitess_shard ON vitess_shard (keyspace, updated_timestamp) +CREATE INDEX updated_timestamp_idx_vitess_keyspace ON vitess_keyspace (updated_timestamp) + `, + ` +CREATE INDEX updated_timestamp_idx_vitess_shard ON vitess_shard (updated_timestamp) `, } diff --git a/go/vt/vtorc/logic/keyspace_shard_discovery.go b/go/vt/vtorc/logic/keyspace_shard_discovery.go index cff5b9fa2ac..4b5b8beae68 100644 --- a/go/vt/vtorc/logic/keyspace_shard_discovery.go +++ b/go/vt/vtorc/logic/keyspace_shard_discovery.go @@ -20,15 +20,18 @@ import ( "context" "sort" "strings" - "sync" "time" + "golang.org/x/sync/errgroup" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/vtorc/inst" ) +var lastAllKeyspaceShardsRefreshTime time.Time + var statsKeyspaceShardsWatched = stats.NewGaugesFuncWithMultiLabels("KeyspaceShardsWatched", "The keyspace/shards watched by VTOrc", []string{"Keyspace", "Shard"}, @@ -53,13 +56,13 @@ func getKeyspaceShardsStats() map[string]int64 { // RefreshAllKeyspacesAndShards reloads the keyspace and shard information for the keyspaces that vtorc is concerned with. func RefreshAllKeyspacesAndShards(ctx context.Context) error { + var err error var keyspaces []string if len(clustersToWatch) == 0 { // all known keyspaces - ctx, cancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout) - defer cancel() - var err error + getCtx, getCancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout) + defer getCancel() // Get all the keyspaces - keyspaces, err = ts.GetKeyspaces(ctx) + keyspaces, err = ts.GetKeyspaces(getCtx) if err != nil { return err } @@ -84,9 +87,11 @@ func RefreshAllKeyspacesAndShards(ctx context.Context) error { // Sort the list of keyspaces. // The list can have duplicates because the input to clusters to watch may have multiple shards of the same keyspace sort.Strings(keyspaces) + refreshCtx, refreshCancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout) defer refreshCancel() - var wg sync.WaitGroup + + eg, egCtx := errgroup.WithContext(refreshCtx) for idx, keyspace := range keyspaces { // Check if the current keyspace name is the same as the last one. // If it is, then we know we have already refreshed its information. @@ -94,17 +99,26 @@ func RefreshAllKeyspacesAndShards(ctx context.Context) error { if idx != 0 && keyspace == keyspaces[idx-1] { continue } - wg.Add(2) - go func(keyspace string) { - defer wg.Done() - _ = refreshKeyspaceHelper(refreshCtx, keyspace) - }(keyspace) - go func(keyspace string) { - defer wg.Done() - _ = refreshAllShards(refreshCtx, keyspace) - }(keyspace) + eg.Go(func() error { + return refreshKeyspaceHelper(egCtx, keyspace) + }) + eg.Go(func() error { + return refreshAllShards(egCtx, keyspace) + }) + } + + if err = eg.Wait(); err == nil { + // delete stale records from the previous success or older + if staleTime := lastAllKeyspaceShardsRefreshTime; !staleTime.IsZero() { + if err := inst.DeleteStaleShards(staleTime); err != nil { + return err + } + if err := inst.DeleteStaleKeyspaces(staleTime); err != nil { + return err + } + } + lastAllKeyspaceShardsRefreshTime = time.Now() } - wg.Wait() return nil } @@ -158,7 +172,6 @@ func refreshAllShards(ctx context.Context, keyspaceName string) error { log.Error(err) return err } - beginSaveTime := time.Now() for _, shardInfo := range shardInfos { err = inst.SaveShard(shardInfo) if err != nil { @@ -166,7 +179,7 @@ func refreshAllShards(ctx context.Context, keyspaceName string) error { return err } } - return inst.DeleteStaleKeyspaceShards(keyspaceName, beginSaveTime) + return nil } // refreshSingleShardHelper is a helper function that refreshes the shard record of the given keyspace/shard. From 54433aad5a3c7c0fbd46e7a6c8ef2b8d3da9e6a0 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 17 Dec 2024 04:04:41 +0100 Subject: [PATCH 08/15] fix test Signed-off-by: Tim Vaillancourt --- go/vt/vtorc/inst/keyspace_dao_test.go | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/go/vt/vtorc/inst/keyspace_dao_test.go b/go/vt/vtorc/inst/keyspace_dao_test.go index dda3ffaa9d2..33d52453bbc 100644 --- a/go/vt/vtorc/inst/keyspace_dao_test.go +++ b/go/vt/vtorc/inst/keyspace_dao_test.go @@ -18,6 +18,7 @@ package inst import ( "testing" + "time" "github.com/stretchr/testify/require" @@ -124,3 +125,37 @@ func TestSaveAndReadKeyspace(t *testing.T) { }) } } + +func TestDeleteStaleKeyspaces(t *testing.T) { + // Clear the database after the test. The easiest way to do that is to run all the initialization commands again. + defer func() { + db.ClearVTOrcDatabase() + }() + + keyspaceInfo := &topo.KeyspaceInfo{ + Keyspace: &topodatapb.Keyspace{ + KeyspaceType: topodatapb.KeyspaceType_NORMAL, + DurabilityPolicy: "none", + BaseKeyspace: "baseKeyspace", + }, + } + keyspaceInfo.SetKeyspaceName("ks1") + err := SaveKeyspace(keyspaceInfo) + require.NoError(t, err) + + readKeyspaceInfo, err := ReadKeyspace("ks1") + require.NoError(t, err) + require.NotNil(t, readKeyspaceInfo) + + // test a staletime before save causes no delete + require.NoError(t, DeleteStaleKeyspaces(time.Now().Add(-time.Hour))) + readKeyspaceInfo, err = ReadKeyspace("ks1") + require.NoError(t, err) + require.NotNil(t, readKeyspaceInfo) + + // test statetime of now deletes everything + require.NoError(t, DeleteStaleKeyspaces(time.Now())) + readKeyspaceInfo, err = ReadKeyspace("ks1") + require.Error(t, err) + require.Nil(t, readKeyspaceInfo) +} From 482e66affc680b55c70a7552f548786e6a3df3de Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 17 Dec 2024 04:46:33 +0100 Subject: [PATCH 09/15] fix test Signed-off-by: Tim Vaillancourt --- go/vt/vtorc/inst/keyspace_dao_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/go/vt/vtorc/inst/keyspace_dao_test.go b/go/vt/vtorc/inst/keyspace_dao_test.go index 33d52453bbc..fea625ce1b7 100644 --- a/go/vt/vtorc/inst/keyspace_dao_test.go +++ b/go/vt/vtorc/inst/keyspace_dao_test.go @@ -139,23 +139,23 @@ func TestDeleteStaleKeyspaces(t *testing.T) { BaseKeyspace: "baseKeyspace", }, } - keyspaceInfo.SetKeyspaceName("ks1") + keyspaceInfo.SetKeyspaceName(t.Name()) err := SaveKeyspace(keyspaceInfo) require.NoError(t, err) - readKeyspaceInfo, err := ReadKeyspace("ks1") + readKeyspaceInfo, err := ReadKeyspace(t.Name()) require.NoError(t, err) require.NotNil(t, readKeyspaceInfo) // test a staletime before save causes no delete require.NoError(t, DeleteStaleKeyspaces(time.Now().Add(-time.Hour))) - readKeyspaceInfo, err = ReadKeyspace("ks1") + readKeyspaceInfo, err = ReadKeyspace(t.Name()) require.NoError(t, err) require.NotNil(t, readKeyspaceInfo) // test statetime of now deletes everything require.NoError(t, DeleteStaleKeyspaces(time.Now())) - readKeyspaceInfo, err = ReadKeyspace("ks1") + readKeyspaceInfo, err = ReadKeyspace(t.Name()) require.Error(t, err) require.Nil(t, readKeyspaceInfo) } From 603809e989773a802f9d3918bd5aa37a9e82f8a7 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 17 Dec 2024 04:46:38 +0100 Subject: [PATCH 10/15] fix test Signed-off-by: Tim Vaillancourt --- go/vt/vtorc/logic/keyspace_shard_discovery.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/go/vt/vtorc/logic/keyspace_shard_discovery.go b/go/vt/vtorc/logic/keyspace_shard_discovery.go index 4b5b8beae68..34a7fcc9ced 100644 --- a/go/vt/vtorc/logic/keyspace_shard_discovery.go +++ b/go/vt/vtorc/logic/keyspace_shard_discovery.go @@ -91,7 +91,7 @@ func RefreshAllKeyspacesAndShards(ctx context.Context) error { refreshCtx, refreshCancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout) defer refreshCancel() - eg, egCtx := errgroup.WithContext(refreshCtx) + eg, refreshCtx := errgroup.WithContext(refreshCtx) for idx, keyspace := range keyspaces { // Check if the current keyspace name is the same as the last one. // If it is, then we know we have already refreshed its information. @@ -100,16 +100,16 @@ func RefreshAllKeyspacesAndShards(ctx context.Context) error { continue } eg.Go(func() error { - return refreshKeyspaceHelper(egCtx, keyspace) + return refreshKeyspaceHelper(refreshCtx, keyspace) }) eg.Go(func() error { - return refreshAllShards(egCtx, keyspace) + return refreshAllShards(refreshCtx, keyspace) }) } if err = eg.Wait(); err == nil { // delete stale records from the previous success or older - if staleTime := lastAllKeyspaceShardsRefreshTime; !staleTime.IsZero() { + if staleTime := lastAllKeyspaceShardsRefreshTime; !staleTime.IsZero() && staleTime.Unix() != time.Now().Unix() { if err := inst.DeleteStaleShards(staleTime); err != nil { return err } From 4566f5f22e29b1562808e3345c6ff6c73940c0ee Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 17 Dec 2024 04:48:15 +0100 Subject: [PATCH 11/15] fix test Signed-off-by: Tim Vaillancourt --- go/vt/vtorc/logic/keyspace_shard_discovery.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/go/vt/vtorc/logic/keyspace_shard_discovery.go b/go/vt/vtorc/logic/keyspace_shard_discovery.go index 34a7fcc9ced..a3fca7aff97 100644 --- a/go/vt/vtorc/logic/keyspace_shard_discovery.go +++ b/go/vt/vtorc/logic/keyspace_shard_discovery.go @@ -109,7 +109,10 @@ func RefreshAllKeyspacesAndShards(ctx context.Context) error { if err = eg.Wait(); err == nil { // delete stale records from the previous success or older - if staleTime := lastAllKeyspaceShardsRefreshTime; !staleTime.IsZero() && staleTime.Unix() != time.Now().Unix() { + if staleTime := lastAllKeyspaceShardsRefreshTime; !staleTime.IsZero() { + if staleTime.Unix() != time.Now().Unix() { + return nil + } if err := inst.DeleteStaleShards(staleTime); err != nil { return err } From 3198115980513ac701fcba61fbc0a8bf5114840e Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 17 Dec 2024 04:48:53 +0100 Subject: [PATCH 12/15] fix cond Signed-off-by: Tim Vaillancourt --- go/vt/vtorc/logic/keyspace_shard_discovery.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/vt/vtorc/logic/keyspace_shard_discovery.go b/go/vt/vtorc/logic/keyspace_shard_discovery.go index a3fca7aff97..74d9b5f7333 100644 --- a/go/vt/vtorc/logic/keyspace_shard_discovery.go +++ b/go/vt/vtorc/logic/keyspace_shard_discovery.go @@ -110,7 +110,7 @@ func RefreshAllKeyspacesAndShards(ctx context.Context) error { if err = eg.Wait(); err == nil { // delete stale records from the previous success or older if staleTime := lastAllKeyspaceShardsRefreshTime; !staleTime.IsZero() { - if staleTime.Unix() != time.Now().Unix() { + if staleTime.Unix() == time.Now().Unix() { return nil } if err := inst.DeleteStaleShards(staleTime); err != nil { From 8a9c9443489a7f62acfdb0041cbf709ad1bd20d5 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 17 Dec 2024 04:53:03 +0100 Subject: [PATCH 13/15] fix cond Signed-off-by: Tim Vaillancourt --- go/vt/vtorc/logic/keyspace_shard_discovery.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/go/vt/vtorc/logic/keyspace_shard_discovery.go b/go/vt/vtorc/logic/keyspace_shard_discovery.go index 74d9b5f7333..c663b110cc7 100644 --- a/go/vt/vtorc/logic/keyspace_shard_discovery.go +++ b/go/vt/vtorc/logic/keyspace_shard_discovery.go @@ -109,10 +109,8 @@ func RefreshAllKeyspacesAndShards(ctx context.Context) error { if err = eg.Wait(); err == nil { // delete stale records from the previous success or older - if staleTime := lastAllKeyspaceShardsRefreshTime; !staleTime.IsZero() { - if staleTime.Unix() == time.Now().Unix() { - return nil - } + now := time.Now() + if staleTime := lastAllKeyspaceShardsRefreshTime; !staleTime.IsZero() && now.Unix() > staleTime.Unix() { if err := inst.DeleteStaleShards(staleTime); err != nil { return err } @@ -120,7 +118,7 @@ func RefreshAllKeyspacesAndShards(ctx context.Context) error { return err } } - lastAllKeyspaceShardsRefreshTime = time.Now() + lastAllKeyspaceShardsRefreshTime = now } return nil From 022190488a89ea14c94d5f9225e10e2a82867f1f Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 17 Dec 2024 05:16:46 +0100 Subject: [PATCH 14/15] add updated_timestamp to tablet table Signed-off-by: Tim Vaillancourt --- go/vt/vtorc/db/generate_base.go | 4 ++++ go/vt/vtorc/inst/analysis_dao_test.go | 8 ++++---- go/vt/vtorc/inst/instance_dao_test.go | 4 ++-- go/vt/vtorc/inst/tablet_dao.go | 9 +++++---- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/go/vt/vtorc/db/generate_base.go b/go/vt/vtorc/db/generate_base.go index 6538175dc14..d3b7d1d3222 100644 --- a/go/vt/vtorc/db/generate_base.go +++ b/go/vt/vtorc/db/generate_base.go @@ -278,6 +278,7 @@ CREATE TABLE vitess_tablet ( tablet_type smallint(5) NOT NULL, primary_timestamp timestamp NOT NULL, info varchar(512) NOT NULL, + updated_timestamp timestamp NOT NULL, PRIMARY KEY (alias) )`, ` @@ -333,4 +334,7 @@ CREATE INDEX updated_timestamp_idx_vitess_keyspace ON vitess_keyspace (updated_t ` CREATE INDEX updated_timestamp_idx_vitess_shard ON vitess_shard (updated_timestamp) `, + ` +CREATE INDEX updated_timestamp_idx_vitess_tablet ON vitess_tablet (updated_timestamp) + `, } diff --git a/go/vt/vtorc/inst/analysis_dao_test.go b/go/vt/vtorc/inst/analysis_dao_test.go index 00ed4d4e9df..7ec6d910f46 100644 --- a/go/vt/vtorc/inst/analysis_dao_test.go +++ b/go/vt/vtorc/inst/analysis_dao_test.go @@ -37,10 +37,10 @@ var ( `INSERT INTO database_instance VALUES('zone1-0000000100','localhost',6711,'2022-12-28 07:26:04','2022-12-28 07:26:04',1094500338,'8.0.31','ROW',1,1,'vt-0000000100-bin.000001',15963,'localhost',6714,8,4.0,1,1,'vt-0000000101-bin.000001',15583,'vt-0000000101-bin.000001',15583,0,0,1,'','',1,0,'vt-0000000100-relay-bin.000002',15815,0,1,0,'zone1','',0,0,0,1,'729a4cc4-8680-11ed-a104-47706090afbd:1-54','729a5138-8680-11ed-acf8-d6b0ef9f4eaa','2022-12-28 07:26:04','',1,0,0,'Homebrew','8.0','FULL',10103920,0,1,'ON',1,'729a4cc4-8680-11ed-a104-47706090afbd','','729a4cc4-8680-11ed-a104-47706090afbd,729a5138-8680-11ed-acf8-d6b0ef9f4eaa',1,1,'',1000000000000000000,1,0,1,0);`, `INSERT INTO database_instance VALUES('zone1-0000000101','localhost',6714,'2022-12-28 07:26:04','2022-12-28 07:26:04',390954723,'8.0.31','ROW',1,1,'vt-0000000101-bin.000001',15583,'',0,0,0,0,0,'',0,'',0,NULL,NULL,0,'','',0,0,'',0,0,0,0,'zone1','',0,0,0,1,'729a4cc4-8680-11ed-a104-47706090afbd:1-54','729a4cc4-8680-11ed-a104-47706090afbd','2022-12-28 07:26:04','',0,0,0,'Homebrew','8.0','FULL',11366095,1,1,'ON',1,'','','729a4cc4-8680-11ed-a104-47706090afbd',-1,-1,'',1000000000000000000,1,1,0,2);`, `INSERT INTO database_instance VALUES('zone2-0000000200','localhost',6756,'2022-12-28 07:26:05','2022-12-28 07:26:05',444286571,'8.0.31','ROW',1,1,'vt-0000000200-bin.000001',15963,'localhost',6714,8,4.0,1,1,'vt-0000000101-bin.000001',15583,'vt-0000000101-bin.000001',15583,0,0,1,'','',1,0,'vt-0000000200-relay-bin.000002',15815,0,1,0,'zone2','',0,0,0,1,'729a4cc4-8680-11ed-a104-47706090afbd:1-54','729a497c-8680-11ed-8ad4-3f51d747db75','2022-12-28 07:26:05','',1,0,0,'Homebrew','8.0','FULL',10443112,0,1,'ON',1,'729a4cc4-8680-11ed-a104-47706090afbd','','729a4cc4-8680-11ed-a104-47706090afbd,729a497c-8680-11ed-8ad4-3f51d747db75',1,1,'',1000000000000000000,1,0,1,0);`, - `INSERT INTO vitess_tablet VALUES('zone1-0000000100','localhost',6711,'ks','0','zone1',2,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3130307d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363731307d20706f72745f6d61703a7b6b65793a227674222076616c75653a363730397d206b657973706163653a226b73222073686172643a22302220747970653a5245504c494341206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363731312064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`, - `INSERT INTO vitess_tablet VALUES('zone1-0000000101','localhost',6714,'ks','0','zone1',1,'2022-12-28 07:23:25.129898+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3130317d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363731337d20706f72745f6d61703a7b6b65793a227674222076616c75653a363731327d206b657973706163653a226b73222073686172643a22302220747970653a5052494d415259206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a36373134207072696d6172795f7465726d5f73746172745f74696d653a7b7365636f6e64733a31363732323132323035206e616e6f7365636f6e64733a3132393839383030307d2064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`, - `INSERT INTO vitess_tablet VALUES('zone1-0000000112','localhost',6747,'ks','0','zone1',3,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3131327d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363734367d20706f72745f6d61703a7b6b65793a227674222076616c75653a363734357d206b657973706163653a226b73222073686172643a22302220747970653a52444f4e4c59206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363734372064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`, - `INSERT INTO vitess_tablet VALUES('zone2-0000000200','localhost',6756,'ks','0','zone2',2,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653222207569643a3230307d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363735357d20706f72745f6d61703a7b6b65793a227674222076616c75653a363735347d206b657973706163653a226b73222073686172643a22302220747970653a5245504c494341206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363735362064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`, + `INSERT INTO vitess_tablet VALUES('zone1-0000000100','localhost',6711,'ks','0','zone1',2,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3130307d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363731307d20706f72745f6d61703a7b6b65793a227674222076616c75653a363730397d206b657973706163653a226b73222073686172643a22302220747970653a5245504c494341206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363731312064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435','2022-12-28 07:23:25.129898+00:00');`, + `INSERT INTO vitess_tablet VALUES('zone1-0000000101','localhost',6714,'ks','0','zone1',1,'2022-12-28 07:23:25.129898+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3130317d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363731337d20706f72745f6d61703a7b6b65793a227674222076616c75653a363731327d206b657973706163653a226b73222073686172643a22302220747970653a5052494d415259206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a36373134207072696d6172795f7465726d5f73746172745f74696d653a7b7365636f6e64733a31363732323132323035206e616e6f7365636f6e64733a3132393839383030307d2064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435','2022-12-28 07:23:25.129898+00:00');`, + `INSERT INTO vitess_tablet VALUES('zone1-0000000112','localhost',6747,'ks','0','zone1',3,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3131327d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363734367d20706f72745f6d61703a7b6b65793a227674222076616c75653a363734357d206b657973706163653a226b73222073686172643a22302220747970653a52444f4e4c59206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363734372064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435','2022-12-28 07:23:25.129898+00:00');`, + `INSERT INTO vitess_tablet VALUES('zone2-0000000200','localhost',6756,'ks','0','zone2',2,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653222207569643a3230307d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363735357d20706f72745f6d61703a7b6b65793a227674222076616c75653a363735347d206b657973706163653a226b73222073686172643a22302220747970653a5245504c494341206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363735362064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435','2022-12-28 07:23:25.129898+00:00');`, `INSERT INTO vitess_shard VALUES('ks','0','zone1-0000000101','2022-12-28 07:23:25.129898+00:00','2022-12-28 07:23:25.129898+00:00');`, `INSERT INTO vitess_keyspace VALUES('ks',0,'semi_sync','2022-12-28 07:23:25.129898+00:00');`, } diff --git a/go/vt/vtorc/inst/instance_dao_test.go b/go/vt/vtorc/inst/instance_dao_test.go index cc3217442ed..e02343c4155 100644 --- a/go/vt/vtorc/inst/instance_dao_test.go +++ b/go/vt/vtorc/inst/instance_dao_test.go @@ -442,7 +442,7 @@ func TestReadOutdatedInstanceKeys(t *testing.T) { name: "One instance doesn't have myql data", sql: []string{ "update database_instance set last_checked = DATETIME('now')", - `INSERT INTO vitess_tablet VALUES('zone1-0000000103','localhost',7706,'ks','0','zone1',2,'0001-01-01 00:00:00+00:00','');`, + `INSERT INTO vitess_tablet VALUES('zone1-0000000103','localhost',7706,'ks','0','zone1',2,'0001-01-01 00:00:00+00:00','',DATETIME('now'));`, }, instancesRequired: []string{"zone1-0000000103"}, }, { @@ -450,7 +450,7 @@ func TestReadOutdatedInstanceKeys(t *testing.T) { sql: []string{ "update database_instance set last_checked = DATETIME('now')", "update database_instance set last_checked = DATETIME('now', '-1 hour') where alias = 'zone1-0000000100'", - `INSERT INTO vitess_tablet VALUES('zone1-0000000103','localhost',7706,'ks','0','zone1',2,'0001-01-01 00:00:00+00:00','');`, + `INSERT INTO vitess_tablet VALUES('zone1-0000000103','localhost',7706,'ks','0','zone1',2,'0001-01-01 00:00:00+00:00','',DATETIME('now'));`, }, instancesRequired: []string{"zone1-0000000103", "zone1-0000000100"}, }, diff --git a/go/vt/vtorc/inst/tablet_dao.go b/go/vt/vtorc/inst/tablet_dao.go index f48f2b97370..bc9c7faf2ea 100644 --- a/go/vt/vtorc/inst/tablet_dao.go +++ b/go/vt/vtorc/inst/tablet_dao.go @@ -61,8 +61,7 @@ func ReadTablet(tabletAlias string) (*topodatapb.Tablet, error) { FROM vitess_tablet WHERE - alias = ? - ` + alias = ?` args := sqlutils.Args(tabletAlias) tablet := &topodatapb.Tablet{} opts := prototext.UnmarshalOptions{DiscardUnknown: true} @@ -94,7 +93,8 @@ func SaveTablet(tablet *topodatapb.Tablet) error { shard, tablet_type, primary_timestamp, - info + info, + updated_timestamp ) VALUES ( ?, ?, @@ -104,7 +104,8 @@ func SaveTablet(tablet *topodatapb.Tablet) error { ?, ?, ?, - ? + ?, + DATETIME('now') )`, topoproto.TabletAliasString(tablet.Alias), tablet.MysqlHostname, From 44b4f2d6262166cb51db4a1720838e4c125d1271 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Thu, 19 Dec 2024 02:19:24 +0100 Subject: [PATCH 15/15] WIP Signed-off-by: Tim Vaillancourt --- go/vt/vtorc/db/generate_base.go | 4 ---- go/vt/vtorc/inst/analysis_dao_test.go | 8 ++++---- go/vt/vtorc/inst/instance_dao_test.go | 4 ++-- go/vt/vtorc/inst/keyspace_dao.go | 5 +++-- go/vt/vtorc/inst/shard_dao.go | 5 +++-- go/vt/vtorc/inst/tablet_dao.go | 6 ++---- go/vt/vtorc/logic/keyspace_shard_discovery.go | 6 +++--- 7 files changed, 17 insertions(+), 21 deletions(-) diff --git a/go/vt/vtorc/db/generate_base.go b/go/vt/vtorc/db/generate_base.go index d3b7d1d3222..6538175dc14 100644 --- a/go/vt/vtorc/db/generate_base.go +++ b/go/vt/vtorc/db/generate_base.go @@ -278,7 +278,6 @@ CREATE TABLE vitess_tablet ( tablet_type smallint(5) NOT NULL, primary_timestamp timestamp NOT NULL, info varchar(512) NOT NULL, - updated_timestamp timestamp NOT NULL, PRIMARY KEY (alias) )`, ` @@ -334,7 +333,4 @@ CREATE INDEX updated_timestamp_idx_vitess_keyspace ON vitess_keyspace (updated_t ` CREATE INDEX updated_timestamp_idx_vitess_shard ON vitess_shard (updated_timestamp) `, - ` -CREATE INDEX updated_timestamp_idx_vitess_tablet ON vitess_tablet (updated_timestamp) - `, } diff --git a/go/vt/vtorc/inst/analysis_dao_test.go b/go/vt/vtorc/inst/analysis_dao_test.go index 7ec6d910f46..00ed4d4e9df 100644 --- a/go/vt/vtorc/inst/analysis_dao_test.go +++ b/go/vt/vtorc/inst/analysis_dao_test.go @@ -37,10 +37,10 @@ var ( `INSERT INTO database_instance VALUES('zone1-0000000100','localhost',6711,'2022-12-28 07:26:04','2022-12-28 07:26:04',1094500338,'8.0.31','ROW',1,1,'vt-0000000100-bin.000001',15963,'localhost',6714,8,4.0,1,1,'vt-0000000101-bin.000001',15583,'vt-0000000101-bin.000001',15583,0,0,1,'','',1,0,'vt-0000000100-relay-bin.000002',15815,0,1,0,'zone1','',0,0,0,1,'729a4cc4-8680-11ed-a104-47706090afbd:1-54','729a5138-8680-11ed-acf8-d6b0ef9f4eaa','2022-12-28 07:26:04','',1,0,0,'Homebrew','8.0','FULL',10103920,0,1,'ON',1,'729a4cc4-8680-11ed-a104-47706090afbd','','729a4cc4-8680-11ed-a104-47706090afbd,729a5138-8680-11ed-acf8-d6b0ef9f4eaa',1,1,'',1000000000000000000,1,0,1,0);`, `INSERT INTO database_instance VALUES('zone1-0000000101','localhost',6714,'2022-12-28 07:26:04','2022-12-28 07:26:04',390954723,'8.0.31','ROW',1,1,'vt-0000000101-bin.000001',15583,'',0,0,0,0,0,'',0,'',0,NULL,NULL,0,'','',0,0,'',0,0,0,0,'zone1','',0,0,0,1,'729a4cc4-8680-11ed-a104-47706090afbd:1-54','729a4cc4-8680-11ed-a104-47706090afbd','2022-12-28 07:26:04','',0,0,0,'Homebrew','8.0','FULL',11366095,1,1,'ON',1,'','','729a4cc4-8680-11ed-a104-47706090afbd',-1,-1,'',1000000000000000000,1,1,0,2);`, `INSERT INTO database_instance VALUES('zone2-0000000200','localhost',6756,'2022-12-28 07:26:05','2022-12-28 07:26:05',444286571,'8.0.31','ROW',1,1,'vt-0000000200-bin.000001',15963,'localhost',6714,8,4.0,1,1,'vt-0000000101-bin.000001',15583,'vt-0000000101-bin.000001',15583,0,0,1,'','',1,0,'vt-0000000200-relay-bin.000002',15815,0,1,0,'zone2','',0,0,0,1,'729a4cc4-8680-11ed-a104-47706090afbd:1-54','729a497c-8680-11ed-8ad4-3f51d747db75','2022-12-28 07:26:05','',1,0,0,'Homebrew','8.0','FULL',10443112,0,1,'ON',1,'729a4cc4-8680-11ed-a104-47706090afbd','','729a4cc4-8680-11ed-a104-47706090afbd,729a497c-8680-11ed-8ad4-3f51d747db75',1,1,'',1000000000000000000,1,0,1,0);`, - `INSERT INTO vitess_tablet VALUES('zone1-0000000100','localhost',6711,'ks','0','zone1',2,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3130307d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363731307d20706f72745f6d61703a7b6b65793a227674222076616c75653a363730397d206b657973706163653a226b73222073686172643a22302220747970653a5245504c494341206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363731312064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435','2022-12-28 07:23:25.129898+00:00');`, - `INSERT INTO vitess_tablet VALUES('zone1-0000000101','localhost',6714,'ks','0','zone1',1,'2022-12-28 07:23:25.129898+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3130317d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363731337d20706f72745f6d61703a7b6b65793a227674222076616c75653a363731327d206b657973706163653a226b73222073686172643a22302220747970653a5052494d415259206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a36373134207072696d6172795f7465726d5f73746172745f74696d653a7b7365636f6e64733a31363732323132323035206e616e6f7365636f6e64733a3132393839383030307d2064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435','2022-12-28 07:23:25.129898+00:00');`, - `INSERT INTO vitess_tablet VALUES('zone1-0000000112','localhost',6747,'ks','0','zone1',3,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3131327d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363734367d20706f72745f6d61703a7b6b65793a227674222076616c75653a363734357d206b657973706163653a226b73222073686172643a22302220747970653a52444f4e4c59206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363734372064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435','2022-12-28 07:23:25.129898+00:00');`, - `INSERT INTO vitess_tablet VALUES('zone2-0000000200','localhost',6756,'ks','0','zone2',2,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653222207569643a3230307d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363735357d20706f72745f6d61703a7b6b65793a227674222076616c75653a363735347d206b657973706163653a226b73222073686172643a22302220747970653a5245504c494341206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363735362064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435','2022-12-28 07:23:25.129898+00:00');`, + `INSERT INTO vitess_tablet VALUES('zone1-0000000100','localhost',6711,'ks','0','zone1',2,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3130307d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363731307d20706f72745f6d61703a7b6b65793a227674222076616c75653a363730397d206b657973706163653a226b73222073686172643a22302220747970653a5245504c494341206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363731312064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`, + `INSERT INTO vitess_tablet VALUES('zone1-0000000101','localhost',6714,'ks','0','zone1',1,'2022-12-28 07:23:25.129898+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3130317d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363731337d20706f72745f6d61703a7b6b65793a227674222076616c75653a363731327d206b657973706163653a226b73222073686172643a22302220747970653a5052494d415259206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a36373134207072696d6172795f7465726d5f73746172745f74696d653a7b7365636f6e64733a31363732323132323035206e616e6f7365636f6e64733a3132393839383030307d2064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`, + `INSERT INTO vitess_tablet VALUES('zone1-0000000112','localhost',6747,'ks','0','zone1',3,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3131327d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363734367d20706f72745f6d61703a7b6b65793a227674222076616c75653a363734357d206b657973706163653a226b73222073686172643a22302220747970653a52444f4e4c59206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363734372064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`, + `INSERT INTO vitess_tablet VALUES('zone2-0000000200','localhost',6756,'ks','0','zone2',2,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653222207569643a3230307d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363735357d20706f72745f6d61703a7b6b65793a227674222076616c75653a363735347d206b657973706163653a226b73222073686172643a22302220747970653a5245504c494341206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363735362064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`, `INSERT INTO vitess_shard VALUES('ks','0','zone1-0000000101','2022-12-28 07:23:25.129898+00:00','2022-12-28 07:23:25.129898+00:00');`, `INSERT INTO vitess_keyspace VALUES('ks',0,'semi_sync','2022-12-28 07:23:25.129898+00:00');`, } diff --git a/go/vt/vtorc/inst/instance_dao_test.go b/go/vt/vtorc/inst/instance_dao_test.go index e02343c4155..cc3217442ed 100644 --- a/go/vt/vtorc/inst/instance_dao_test.go +++ b/go/vt/vtorc/inst/instance_dao_test.go @@ -442,7 +442,7 @@ func TestReadOutdatedInstanceKeys(t *testing.T) { name: "One instance doesn't have myql data", sql: []string{ "update database_instance set last_checked = DATETIME('now')", - `INSERT INTO vitess_tablet VALUES('zone1-0000000103','localhost',7706,'ks','0','zone1',2,'0001-01-01 00:00:00+00:00','',DATETIME('now'));`, + `INSERT INTO vitess_tablet VALUES('zone1-0000000103','localhost',7706,'ks','0','zone1',2,'0001-01-01 00:00:00+00:00','');`, }, instancesRequired: []string{"zone1-0000000103"}, }, { @@ -450,7 +450,7 @@ func TestReadOutdatedInstanceKeys(t *testing.T) { sql: []string{ "update database_instance set last_checked = DATETIME('now')", "update database_instance set last_checked = DATETIME('now', '-1 hour') where alias = 'zone1-0000000100'", - `INSERT INTO vitess_tablet VALUES('zone1-0000000103','localhost',7706,'ks','0','zone1',2,'0001-01-01 00:00:00+00:00','',DATETIME('now'));`, + `INSERT INTO vitess_tablet VALUES('zone1-0000000103','localhost',7706,'ks','0','zone1',2,'0001-01-01 00:00:00+00:00','');`, }, instancesRequired: []string{"zone1-0000000103", "zone1-0000000100"}, }, diff --git a/go/vt/vtorc/inst/keyspace_dao.go b/go/vt/vtorc/inst/keyspace_dao.go index ce2d075d38b..36810bf2d5f 100644 --- a/go/vt/vtorc/inst/keyspace_dao.go +++ b/go/vt/vtorc/inst/keyspace_dao.go @@ -62,16 +62,17 @@ func ReadKeyspace(keyspaceName string) (*topo.KeyspaceInfo, error) { } // SaveKeyspace saves the keyspace record against the keyspace name. -func SaveKeyspace(keyspace *topo.KeyspaceInfo) error { +func SaveKeyspace(keyspace *topo.KeyspaceInfo, updatedTimestamp time.Time) error { _, err := db.ExecVTOrc(`REPLACE INTO vitess_keyspace ( keyspace, keyspace_type, durability_policy, updated_timestamp ) VALUES ( - ?, ?, ?, DATETIME('now') + ?, ?, ?, DATETIME(?, 'unixepoch') )`, keyspace.KeyspaceName(), int(keyspace.KeyspaceType), keyspace.GetDurabilityPolicy(), + updatedTimestamp.Unix(), ) return err } diff --git a/go/vt/vtorc/inst/shard_dao.go b/go/vt/vtorc/inst/shard_dao.go index c1c23dc135a..0c26d8d196b 100644 --- a/go/vt/vtorc/inst/shard_dao.go +++ b/go/vt/vtorc/inst/shard_dao.go @@ -87,19 +87,20 @@ func GetKeyspaceShardNames(keyspaceName string) ([]string, error) { } // SaveShard saves the shard record against the shard name. -func SaveShard(shard *topo.ShardInfo) error { +func SaveShard(shard *topo.ShardInfo, updatedTimestamp time.Time) error { _, err := db.ExecVTOrc(` REPLACE INTO vitess_shard ( keyspace, shard, primary_alias, primary_timestamp, updated_timestamp ) VALUES ( - ?, ?, ?, ?, DATETIME('now') + ?, ?, ?, ?, DATETIME(?, 'unixepoch') ) `, shard.Keyspace(), shard.ShardName(), getShardPrimaryAliasString(shard), getShardPrimaryTermStartTimeString(shard), + updatedTimestamp.Unix(), ) return err } diff --git a/go/vt/vtorc/inst/tablet_dao.go b/go/vt/vtorc/inst/tablet_dao.go index bc9c7faf2ea..79ec0f98eed 100644 --- a/go/vt/vtorc/inst/tablet_dao.go +++ b/go/vt/vtorc/inst/tablet_dao.go @@ -93,8 +93,7 @@ func SaveTablet(tablet *topodatapb.Tablet) error { shard, tablet_type, primary_timestamp, - info, - updated_timestamp + info ) VALUES ( ?, ?, @@ -104,8 +103,7 @@ func SaveTablet(tablet *topodatapb.Tablet) error { ?, ?, ?, - ?, - DATETIME('now') + ? )`, topoproto.TabletAliasString(tablet.Alias), tablet.MysqlHostname, diff --git a/go/vt/vtorc/logic/keyspace_shard_discovery.go b/go/vt/vtorc/logic/keyspace_shard_discovery.go index c663b110cc7..02eb88309ca 100644 --- a/go/vt/vtorc/logic/keyspace_shard_discovery.go +++ b/go/vt/vtorc/logic/keyspace_shard_discovery.go @@ -154,7 +154,7 @@ func refreshKeyspaceHelper(ctx context.Context, keyspaceName string) error { log.Error(err) return err } - err = inst.SaveKeyspace(keyspaceInfo) + err = inst.SaveKeyspace(keyspaceInfo, time.Now() /* updated_timestamp */) if err != nil { log.Error(err) } @@ -174,7 +174,7 @@ func refreshAllShards(ctx context.Context, keyspaceName string) error { return err } for _, shardInfo := range shardInfos { - err = inst.SaveShard(shardInfo) + err = inst.SaveShard(shardInfo, time.Now() /* updated_timestamp */) if err != nil { log.Error(err) return err @@ -190,7 +190,7 @@ func refreshSingleShardHelper(ctx context.Context, keyspaceName string, shardNam log.Error(err) return err } - err = inst.SaveShard(shardInfo) + err = inst.SaveShard(shardInfo, time.Now() /* updated_timestamp */) if err != nil { log.Error(err) }