Skip to content

Commit bd9e5d0

Browse files
vtorc: move shard primary timestamp to time type (vitessio#18401)
Signed-off-by: Tim Vaillancourt <[email protected]>
1 parent d429368 commit bd9e5d0

File tree

7 files changed

+33
-31
lines changed

7 files changed

+33
-31
lines changed

go/vt/external/golib/sqlutils/sqlutils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
"vitess.io/vitess/go/vt/log"
3434
)
3535

36-
const DateTimeFormat = "2006-01-02 15:04:05.999999"
36+
const DateTimeFormat = "2006-01-02 15:04:05.999999 +0000 UTC"
3737

3838
// RowMap represents one row in a result set. Its objective is to allow
3939
// for easy, typed getters by column name.

go/vt/external/golib/sqlutils/sqlutils_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
)
2626

2727
func TestRowMap(t *testing.T) {
28+
getTimeExpected := time.Date(2024, time.January, 24, 12, 34, 56, 789000000, time.UTC)
2829
tt := []struct {
2930
name string
3031
rowMap RowMap
@@ -82,8 +83,8 @@ func TestRowMap(t *testing.T) {
8283
},
8384
{
8485
"GetTime",
85-
RowMap{"key": CellData{String: "2024-01-24 12:34:56.789"}},
86-
time.Date(2024, time.January, 24, 12, 34, 56, 789000000, time.UTC),
86+
RowMap{"key": CellData{String: getTimeExpected.UTC().String()}},
87+
getTimeExpected,
8788
},
8889
{
8990
"GetTime Error",

go/vt/vtorc/inst/analysis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ type ReplicationAnalysis struct {
9494
AnalyzedKeyspace string
9595
AnalyzedShard string
9696
// ShardPrimaryTermTimestamp is the primary term start time stored in the shard record.
97-
ShardPrimaryTermTimestamp string
97+
ShardPrimaryTermTimestamp time.Time
9898
AnalyzedInstanceBinlogCoordinates BinlogCoordinates
9999
IsPrimary bool
100100
IsClusterPrimary bool

go/vt/vtorc/inst/analysis_dao.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func GetReplicationAnalysis(keyspace string, shard string, hints *ReplicationAna
313313
return nil
314314
}
315315

316-
a.ShardPrimaryTermTimestamp = m.GetString("shard_primary_term_timestamp")
316+
a.ShardPrimaryTermTimestamp = m.GetTime("shard_primary_term_timestamp")
317317
a.IsPrimary = m.GetBool("is_primary")
318318
a.AnalyzedInstanceAlias = topoproto.TabletAliasString(tablet.Alias)
319319
a.AnalyzedInstancePrimaryAlias = topoproto.TabletAliasString(primaryTablet.Alias)
@@ -456,12 +456,12 @@ func GetReplicationAnalysis(keyspace string, shard string, hints *ReplicationAna
456456
} else if topo.IsReplicaType(a.TabletType) && a.ErrantGTID != "" {
457457
a.Analysis = ErrantGTIDDetected
458458
a.Description = "Tablet has errant GTIDs"
459-
} else if topo.IsReplicaType(a.TabletType) && ca.primaryAlias == "" && a.ShardPrimaryTermTimestamp == "" {
459+
} else if topo.IsReplicaType(a.TabletType) && ca.primaryAlias == "" && a.ShardPrimaryTermTimestamp.IsZero() {
460460
// ClusterHasNoPrimary should only be detected when the shard record doesn't have any primary term start time specified either.
461461
a.Analysis = ClusterHasNoPrimary
462462
a.Description = "Cluster has no primary"
463463
ca.hasShardWideAction = true
464-
} else if topo.IsReplicaType(a.TabletType) && ca.primaryAlias == "" && a.ShardPrimaryTermTimestamp != "" {
464+
} else if topo.IsReplicaType(a.TabletType) && ca.primaryAlias == "" && !a.ShardPrimaryTermTimestamp.IsZero() {
465465
// If there are no primary tablets, but the shard primary start time isn't empty, then we know
466466
// the primary tablet was deleted.
467467
a.Analysis = PrimaryTabletDeleted

go/vt/vtorc/inst/analysis_dao_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ var (
3838
`INSERT INTO database_instance VALUES('zone1-0000000100','localhost',6711,2,'zone1','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,'vt-0000000100-relay-bin.000002',15815,1,0,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,false,false);`,
3939
`INSERT INTO database_instance VALUES('zone1-0000000101','localhost',6714,1,'zone1','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,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,false,false);`,
4040
`INSERT INTO database_instance VALUES('zone2-0000000200','localhost',6756,2,'zone2','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,'vt-0000000200-relay-bin.000002',15815,1,0,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,false,false);`,
41-
`INSERT INTO vitess_tablet VALUES('zone1-0000000100','localhost',6711,'ks','0','zone1',2,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3130307d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363731307d20706f72745f6d61703a7b6b65793a227674222076616c75653a363730397d206b657973706163653a226b73222073686172643a22302220747970653a5245504c494341206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363731312064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`,
42-
`INSERT INTO vitess_tablet VALUES('zone1-0000000101','localhost',6714,'ks','0','zone1',1,'2022-12-28 07:23:25.129898+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3130317d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363731337d20706f72745f6d61703a7b6b65793a227674222076616c75653a363731327d206b657973706163653a226b73222073686172643a22302220747970653a5052494d415259206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a36373134207072696d6172795f7465726d5f73746172745f74696d653a7b7365636f6e64733a31363732323132323035206e616e6f7365636f6e64733a3132393839383030307d2064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`,
43-
`INSERT INTO vitess_tablet VALUES('zone1-0000000112','localhost',6747,'ks','0','zone1',3,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3131327d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363734367d20706f72745f6d61703a7b6b65793a227674222076616c75653a363734357d206b657973706163653a226b73222073686172643a22302220747970653a52444f4e4c59206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363734372064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`,
44-
`INSERT INTO vitess_tablet VALUES('zone2-0000000200','localhost',6756,'ks','0','zone2',2,'0001-01-01 00:00:00+00:00',X'616c6961733a7b63656c6c3a227a6f6e653222207569643a3230307d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363735357d20706f72745f6d61703a7b6b65793a227674222076616c75653a363735347d206b657973706163653a226b73222073686172643a22302220747970653a5245504c494341206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363735362064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`,
45-
`INSERT INTO vitess_shard VALUES('ks','0','zone1-0000000101','2022-12-28 07:23:25.129898+00:00');`,
41+
`INSERT INTO vitess_tablet VALUES('zone1-0000000100','localhost',6711,'ks','0','zone1',2,'0001-01-01 00:00:00 +0000 UTC',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3130307d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363731307d20706f72745f6d61703a7b6b65793a227674222076616c75653a363730397d206b657973706163653a226b73222073686172643a22302220747970653a5245504c494341206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363731312064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`,
42+
`INSERT INTO vitess_tablet VALUES('zone1-0000000101','localhost',6714,'ks','0','zone1',1,'2022-12-28 07:23:25.129898 +0000 UTC',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3130317d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363731337d20706f72745f6d61703a7b6b65793a227674222076616c75653a363731327d206b657973706163653a226b73222073686172643a22302220747970653a5052494d415259206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a36373134207072696d6172795f7465726d5f73746172745f74696d653a7b7365636f6e64733a31363732323132323035206e616e6f7365636f6e64733a3132393839383030307d2064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`,
43+
`INSERT INTO vitess_tablet VALUES('zone1-0000000112','localhost',6747,'ks','0','zone1',3,'0001-01-01 00:00:00 +0000 UTC',X'616c6961733a7b63656c6c3a227a6f6e653122207569643a3131327d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363734367d20706f72745f6d61703a7b6b65793a227674222076616c75653a363734357d206b657973706163653a226b73222073686172643a22302220747970653a52444f4e4c59206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363734372064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`,
44+
`INSERT INTO vitess_tablet VALUES('zone2-0000000200','localhost',6756,'ks','0','zone2',2,'0001-01-01 00:00:00 +0000 UTC',X'616c6961733a7b63656c6c3a227a6f6e653222207569643a3230307d20686f73746e616d653a226c6f63616c686f73742220706f72745f6d61703a7b6b65793a2267727063222076616c75653a363735357d20706f72745f6d61703a7b6b65793a227674222076616c75653a363735347d206b657973706163653a226b73222073686172643a22302220747970653a5245504c494341206d7973716c5f686f73746e616d653a226c6f63616c686f737422206d7973716c5f706f72743a363735362064625f7365727665725f76657273696f6e3a22382e302e3331222064656661756c745f636f6e6e5f636f6c6c6174696f6e3a3435');`,
45+
`INSERT INTO vitess_shard VALUES('ks','0','zone1-0000000101','2025-06-25 23:48:57.306096 +0000 UTC');`,
4646
`INSERT INTO vitess_keyspace VALUES('ks',0,'semi_sync');`,
4747
}
4848
)
@@ -89,7 +89,7 @@ func TestGetReplicationAnalysisDecision(t *testing.T) {
8989
MysqlHostname: "localhost",
9090
MysqlPort: 6709,
9191
},
92-
ShardPrimaryTermTimestamp: "2022-12-28 07:23:25.129898+00:00",
92+
ShardPrimaryTermTimestamp: "2022-12-28 07:23:25.129898 +0000 UTC",
9393
DurabilityPolicy: policy.DurabilityNone,
9494
LastCheckValid: 1,
9595
}},

go/vt/vtorc/inst/shard_dao.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package inst
1818

1919
import (
2020
"errors"
21+
"time"
2122

2223
"vitess.io/vitess/go/protoutil"
2324
"vitess.io/vitess/go/vt/external/golib/sqlutils"
@@ -42,36 +43,36 @@ func ReadShardNames(keyspaceName string) (shardNames []string, err error) {
4243
}
4344

4445
// ReadShardPrimaryInformation reads the vitess shard record and gets the shard primary alias and timestamp.
45-
func ReadShardPrimaryInformation(keyspaceName, shardName string) (primaryAlias string, primaryTimestamp string, err error) {
46+
func ReadShardPrimaryInformation(keyspaceName, shardName string) (primaryAlias string, primaryTimestamp time.Time, err error) {
4647
if err = topo.ValidateKeyspaceName(keyspaceName); err != nil {
4748
return
4849
}
4950
if _, _, err = topo.ValidateShardName(shardName); err != nil {
5051
return
5152
}
5253

53-
query := `
54-
select
54+
query := `SELECT
5555
primary_alias, primary_timestamp
56-
from
56+
FROM
5757
vitess_shard
58-
where keyspace=? and shard=?
59-
`
58+
WHERE
59+
keyspace = ?
60+
AND shard = ?`
6061
args := sqlutils.Args(keyspaceName, shardName)
6162
shardFound := false
6263
err = db.QueryVTOrc(query, args, func(row sqlutils.RowMap) error {
6364
shardFound = true
6465
primaryAlias = row.GetString("primary_alias")
65-
primaryTimestamp = row.GetString("primary_timestamp")
66+
primaryTimestamp = row.GetTime("primary_timestamp")
6667
return nil
6768
})
6869
if err != nil {
6970
return
7071
}
7172
if !shardFound {
72-
return "", "", ErrShardNotFound
73+
err = ErrShardNotFound
7374
}
74-
return primaryAlias, primaryTimestamp, nil
75+
return primaryAlias, primaryTimestamp, err
7576
}
7677

7778
// SaveShard saves the shard record against the shard name.
@@ -87,7 +88,7 @@ func SaveShard(shard *topo.ShardInfo) error {
8788
shard.Keyspace(),
8889
shard.ShardName(),
8990
getShardPrimaryAliasString(shard),
90-
getShardPrimaryTermStartTimeString(shard),
91+
getShardPrimaryTermStartTime(shard),
9192
)
9293
return err
9394
}
@@ -100,12 +101,12 @@ func getShardPrimaryAliasString(shard *topo.ShardInfo) string {
100101
return topoproto.TabletAliasString(shard.PrimaryAlias)
101102
}
102103

103-
// getShardPrimaryAliasString gets the shard primary term start time to be stored as a string in the database.
104-
func getShardPrimaryTermStartTimeString(shard *topo.ShardInfo) string {
104+
// getShardPrimaryTermStartTime gets the shard primary term start time to be stored as a string in the database.
105+
func getShardPrimaryTermStartTime(shard *topo.ShardInfo) time.Time {
105106
if shard.PrimaryTermStartTime == nil {
106-
return ""
107+
return time.Time{}
107108
}
108-
return protoutil.TimeFromProto(shard.PrimaryTermStartTime).UTC().String()
109+
return protoutil.TimeFromProto(shard.PrimaryTermStartTime).UTC()
109110
}
110111

111112
// DeleteShard deletes a shard using a keyspace and shard name.

go/vt/vtorc/inst/shard_dao_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestSaveReadAndDeleteShard(t *testing.T) {
4040
shardName string
4141
shard *topodatapb.Shard
4242
primaryAliasWanted string
43-
primaryTimestampWanted string
43+
primaryTimestampWanted time.Time
4444
err string
4545
}{
4646
{
@@ -54,7 +54,7 @@ func TestSaveReadAndDeleteShard(t *testing.T) {
5454
},
5555
PrimaryTermStartTime: protoutil.TimeToProto(timeToUse.Add(1 * time.Hour)),
5656
},
57-
primaryTimestampWanted: "2023-07-24 06:00:05.000001 +0000 UTC",
57+
primaryTimestampWanted: timeToUse.Add(1 * time.Hour).UTC(),
5858
primaryAliasWanted: "zone1-0000000301",
5959
}, {
6060
name: "Success with empty primary alias",
@@ -63,7 +63,7 @@ func TestSaveReadAndDeleteShard(t *testing.T) {
6363
shard: &topodatapb.Shard{
6464
PrimaryTermStartTime: protoutil.TimeToProto(timeToUse),
6565
},
66-
primaryTimestampWanted: "2023-07-24 05:00:05.000001 +0000 UTC",
66+
primaryTimestampWanted: timeToUse.UTC(),
6767
primaryAliasWanted: "",
6868
}, {
6969
name: "Success with empty primary term start time",
@@ -75,7 +75,7 @@ func TestSaveReadAndDeleteShard(t *testing.T) {
7575
Uid: 301,
7676
},
7777
},
78-
primaryTimestampWanted: "",
78+
primaryTimestampWanted: time.Time{},
7979
primaryAliasWanted: "zone1-0000000301",
8080
},
8181
{

0 commit comments

Comments
 (0)