From ae7935a10fc1e60c2e4736699c81c8cc5d1ae0bc Mon Sep 17 00:00:00 2001 From: Matt Lord Date: Thu, 30 Sep 2021 00:05:28 -0400 Subject: [PATCH] Add some basic tests for SHOW VITESS_REPLICATION_STATUS Signed-off-by: Matt Lord --- go/vt/sqlparser/parse_test.go | 2 + go/vt/vtgate/executor.go | 126 ++++++++++++++++++++++++---------- go/vt/vtgate/executor_test.go | 12 ++++ 3 files changed, 102 insertions(+), 38 deletions(-) diff --git a/go/vt/sqlparser/parse_test.go b/go/vt/sqlparser/parse_test.go index 0a18b1eb33d..9d57f7b4a3b 100644 --- a/go/vt/sqlparser/parse_test.go +++ b/go/vt/sqlparser/parse_test.go @@ -1639,6 +1639,8 @@ var ( }, { input: "show vitess_keyspaces like '%'", output: "show keyspaces like '%'", + }, { + input: "show vitess_replication_status", }, { input: "show vitess_shards", }, { diff --git a/go/vt/vtgate/executor.go b/go/vt/vtgate/executor.go index 77bb46a862e..2c10f7790e0 100644 --- a/go/vt/vtgate/executor.go +++ b/go/vt/vtgate/executor.go @@ -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{ diff --git a/go/vt/vtgate/executor_test.go b/go/vt/vtgate/executor_test.go index 9818a79f978..0d24cb315ae 100644 --- a/go/vt/vtgate/executor_test.go +++ b/go/vt/vtgate/executor_test.go @@ -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)