Skip to content

Commit

Permalink
Add some basic tests for SHOW VITESS_REPLICATION_STATUS
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Lord <[email protected]>
  • Loading branch information
mattlord committed Sep 30, 2021
1 parent 41ff773 commit ae7935a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 38 deletions.
2 changes: 2 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,8 @@ var (
}, {
input: "show vitess_keyspaces like '%'",
output: "show keyspaces like '%'",
}, {
input: "show vitess_replication_status",
}, {
input: "show vitess_shards",
}, {
Expand Down
126 changes: 88 additions & 38 deletions go/vt/vtgate/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,48 +1029,98 @@ func (e *Executor) showTablets(show *sqlparser.ShowLegacy) (*sqltypes.Result, er

func (e *Executor) showVitessReplicationStatus(show *sqlparser.ShowLegacy) (*sqltypes.Result, error) {
rows := [][]sqltypes.Value{}
status := e.scatterConn.GetHealthCheckCacheStatus()
for _, s := range status {
for _, ts := range s.TabletsStats {
if ts.Tablet.Type != topodatapb.TabletType_REPLICA {
continue
}

tabletHostPort := ts.GetTabletHostPort()
throttlerStatus, err := getTabletThrottlerStatus(tabletHostPort)
if err != nil {
log.Warningf("Could not get throttler status from %s: %v", tabletHostPort, err)
}
if UsingLegacyGateway() {
status := e.scatterConn.GetLegacyHealthCheckCacheStatus()

replSourceHost := ""
replSourcePort := int64(0)
replIOThreadHealth := ""
replSQLThreadHealth := ""
replLastError := ""
sql := "show slave status"
results, err := e.txConn.gateway.Execute(context.TODO(), ts.Target, sql, nil, 0, 0, nil)
if err != nil {
log.Warningf("Could not get replication status from %s: %v", tabletHostPort, err)
for _, s := range status {
for _, ts := range s.TabletsStats {
if ts.Tablet.Type != topodatapb.TabletType_REPLICA {
continue
}

tabletHostPort := ts.GetTabletHostPort()
throttlerStatus, err := getTabletThrottlerStatus(tabletHostPort)
if err != nil {
log.Warningf("Could not get throttler status from %s: %v", tabletHostPort, err)
}

replSourceHost := ""
replSourcePort := int64(0)
replIOThreadHealth := ""
replSQLThreadHealth := ""
replLastError := ""
sql := "show slave status"
results, err := e.txConn.gateway.Execute(context.TODO(), ts.Target, sql, nil, 0, 0, nil)
if err != nil {
log.Warningf("Could not get replication status from %s: %v", tabletHostPort, err)
}
if len(results.Rows) == 1 {
replSourceHost = results.Rows[0][1].ToString()
replSourcePort, _ = results.Rows[0][3].ToInt64()
replIOThreadHealth = results.Rows[0][10].ToString()
replSQLThreadHealth = results.Rows[0][11].ToString()
replLastError = results.Rows[0][19].ToString()
}
replicationHealth := fmt.Sprintf("{\"EventStreamRunning\":\"%s\",\"EventApplierRunning\":\"%s\",\"LastError\":\"%s\"}", replIOThreadHealth, replSQLThreadHealth, replLastError)

rows = append(rows, buildVarCharRow(
s.Target.Keyspace,
s.Target.Shard,
topoproto.TabletAliasString(ts.Tablet.Alias),
ts.Tablet.Hostname,
fmt.Sprintf("%s:%d", replSourceHost, replSourcePort),
replicationHealth,
fmt.Sprintf("%d", ts.Stats.ReplicationLagSeconds),
throttlerStatus,
))
}
if len(results.Rows) == 1 {
replSourceHost = results.Rows[0][1].ToString()
replSourcePort, _ = results.Rows[0][3].ToInt64()
replIOThreadHealth = results.Rows[0][10].ToString()
replSQLThreadHealth = results.Rows[0][11].ToString()
replLastError = results.Rows[0][19].ToString()
}
} else {
status := e.scatterConn.GetHealthCheckCacheStatus()

for _, s := range status {
for _, ts := range s.TabletsStats {
if ts.Tablet.Type != topodatapb.TabletType_REPLICA {
continue
}

tabletHostPort := ts.GetTabletHostPort()
throttlerStatus, err := getTabletThrottlerStatus(tabletHostPort)
if err != nil {
log.Warningf("Could not get throttler status from %s: %v", tabletHostPort, err)
}

replSourceHost := ""
replSourcePort := int64(0)
replIOThreadHealth := ""
replSQLThreadHealth := ""
replLastError := ""
sql := "show slave status"
results, err := e.txConn.gateway.Execute(context.TODO(), ts.Target, sql, nil, 0, 0, nil)
if err != nil {
log.Warningf("Could not get replication status from %s: %v", tabletHostPort, err)
}
if len(results.Rows) == 1 {
replSourceHost = results.Rows[0][1].ToString()
replSourcePort, _ = results.Rows[0][3].ToInt64()
replIOThreadHealth = results.Rows[0][10].ToString()
replSQLThreadHealth = results.Rows[0][11].ToString()
replLastError = results.Rows[0][19].ToString()
}
replicationHealth := fmt.Sprintf("{\"EventStreamRunning\":\"%s\",\"EventApplierRunning\":\"%s\",\"LastError\":\"%s\"}", replIOThreadHealth, replSQLThreadHealth, replLastError)

rows = append(rows, buildVarCharRow(
s.Target.Keyspace,
s.Target.Shard,
topoproto.TabletAliasString(ts.Tablet.Alias),
ts.Tablet.Hostname,
fmt.Sprintf("%s:%d", replSourceHost, replSourcePort),
replicationHealth,
fmt.Sprintf("%d", ts.Stats.ReplicationLagSeconds),
throttlerStatus,
))
}
replicationHealth := fmt.Sprintf("{\"EventStreamRunning\":\"%s\",\"EventApplierRunning\":\"%s\",\"LastError\":\"%s\"}", replIOThreadHealth, replSQLThreadHealth, replLastError)

rows = append(rows, buildVarCharRow(
s.Target.Keyspace,
s.Target.Shard,
topoproto.TabletAliasString(ts.Tablet.Alias),
ts.Tablet.Hostname,
fmt.Sprintf("%s:%d", replSourceHost, replSourcePort),
replicationHealth,
fmt.Sprintf("%d", ts.Stats.ReplicationLagSeconds),
throttlerStatus,
))
}
}
return &sqltypes.Result{
Expand Down
12 changes: 12 additions & 0 deletions go/vt/vtgate/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,18 @@ func TestExecutorShow(t *testing.T) {
}
utils.MustMatch(t, wantqr, qr, query)

// The TestTablets don't have support for these columns/values
// So let's be sure the statement works and we get the expected results
query = "show vitess_replication_status"
qr, err = executor.Execute(ctx, "TestExecute", session, query, nil)
require.NoError(t, err)
qr.Rows = [][]sqltypes.Value{}
wantqr = &sqltypes.Result{
Fields: buildVarCharFields("Keyspace", "Shard", "Alias", "Hostname", "Replication_Source", "Replication_Health", "Replication_Lag", "Throttler_Status"),
Rows: [][]sqltypes.Value{},
}
utils.MustMatch(t, wantqr, qr, query)

query = "show vitess_tablets"
qr, err = executor.Execute(ctx, "TestExecute", session, query, nil)
require.NoError(t, err)
Expand Down

0 comments on commit ae7935a

Please sign in to comment.