Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add created timestamp to querypb.StreamHealthResponse #16611

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions go/vt/discovery/healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"vitess.io/vitess/go/protoutil"
"vitess.io/vitess/go/test/utils"
"vitess.io/vitess/go/vt/grpcclient"
"vitess.io/vitess/go/vt/topo"
Expand Down Expand Up @@ -76,6 +77,7 @@ func TestHealthCheck(t *testing.T) {
tablet.Type = topodatapb.TabletType_REPLICA
input := make(chan *querypb.StreamHealthResponse)
conn := createFakeConn(tablet, input)
now := time.Now()

// create a channel and subscribe to healthcheck
resultChan := hc.Subscribe()
Expand All @@ -90,6 +92,7 @@ func TestHealthCheck(t *testing.T) {
Serving: false,
Stats: nil,
PrimaryTermStartTime: 0,
Timestamp: protoutil.TimeToProto(now),
}
result := <-resultChan
mustMatch(t, want, result, "Wrong TabletHealth data")
Expand All @@ -101,6 +104,7 @@ func TestHealthCheck(t *testing.T) {

PrimaryTermStartTimestamp: 0,
RealtimeStats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.5},
Timestamp: protoutil.TimeToProto(now),
}
input <- shr
result = <-resultChan
Expand All @@ -110,6 +114,7 @@ func TestHealthCheck(t *testing.T) {
Serving: true,
Stats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.5},
PrimaryTermStartTime: 0,
Timestamp: protoutil.TimeToProto(now),
}
// create a context with timeout and select on it and channel
mustMatch(t, want, result, "Wrong TabletHealth data")
Expand All @@ -124,6 +129,7 @@ func TestHealthCheck(t *testing.T) {
Serving: true,
Stats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.5},
PrimaryTermStartTime: 0,
Timestamp: protoutil.TimeToProto(now),
}},
}}
// we can't use assert.Equal here because of the special way we want to compare equality
Expand All @@ -137,6 +143,7 @@ func TestHealthCheck(t *testing.T) {
Serving: true,
PrimaryTermStartTimestamp: 10,
RealtimeStats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.2},
Timestamp: protoutil.TimeToProto(now),
}
want = &TabletHealth{
Tablet: tablet,
Expand All @@ -149,6 +156,7 @@ func TestHealthCheck(t *testing.T) {
Conn: conn,
Stats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.2},
PrimaryTermStartTime: 10,
Timestamp: protoutil.TimeToProto(now),
}
input <- shr
result = <-resultChan
Expand All @@ -166,13 +174,15 @@ func TestHealthCheck(t *testing.T) {
Serving: false,
PrimaryTermStartTimestamp: 0,
RealtimeStats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.3},
Timestamp: protoutil.TimeToProto(now),
}
want = &TabletHealth{
Tablet: tablet,
Target: &querypb.Target{Keyspace: "k", Shard: "s", TabletType: topodatapb.TabletType_REPLICA},
Serving: false,
Stats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.3},
PrimaryTermStartTime: 0,
Timestamp: protoutil.TimeToProto(now),
}
input <- shr
result = <-resultChan
Expand All @@ -186,6 +196,7 @@ func TestHealthCheck(t *testing.T) {
Serving: true,
PrimaryTermStartTimestamp: 0,
RealtimeStats: &querypb.RealtimeStats{HealthError: "some error", ReplicationLagSeconds: 1, CpuUsage: 0.3},
Timestamp: protoutil.TimeToProto(now),
}
want = &TabletHealth{
Tablet: tablet,
Expand All @@ -194,6 +205,7 @@ func TestHealthCheck(t *testing.T) {
Stats: &querypb.RealtimeStats{HealthError: "some error", ReplicationLagSeconds: 1, CpuUsage: 0.3},
PrimaryTermStartTime: 0,
LastError: fmt.Errorf("vttablet error: some error"),
Timestamp: protoutil.TimeToProto(now),
}
input <- shr
result = <-resultChan
Expand Down
12 changes: 9 additions & 3 deletions go/vt/discovery/tablet_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ import (
"bytes"
"encoding/json"
"strings"

"vitess.io/vitess/go/vt/vttablet/queryservice"
"time"

"google.golang.org/protobuf/proto"

"vitess.io/vitess/go/netutil"
"vitess.io/vitess/go/protoutil"
"vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/proto/vttime"
"vitess.io/vitess/go/vt/vttablet/queryservice"
)

// TabletHealth represents simple tablet health data that is returned to users of healthcheck.
Expand All @@ -40,6 +42,7 @@ type TabletHealth struct {
PrimaryTermStartTime int64
LastError error
Serving bool
Timestamp *vttime.Time
}

func (th *TabletHealth) MarshalJSON() ([]byte, error) {
Expand All @@ -50,13 +53,15 @@ func (th *TabletHealth) MarshalJSON() ([]byte, error) {
PrimaryTermStartTime int64
Stats *query.RealtimeStats
LastError error
Timestamp time.Time
}{
Tablet: th.Tablet,
Target: th.Target,
Serving: th.Serving,
PrimaryTermStartTime: th.PrimaryTermStartTime,
Stats: th.Stats,
LastError: th.LastError,
Timestamp: protoutil.TimeFromProto(th.Timestamp),
})
}

Expand All @@ -69,7 +74,8 @@ func (th *TabletHealth) DeepEqual(other *TabletHealth) bool {
th.PrimaryTermStartTime == other.PrimaryTermStartTime &&
proto.Equal(th.Stats, other.Stats) &&
((th.LastError == nil && other.LastError == nil) ||
(th.LastError != nil && other.LastError != nil && th.LastError.Error() == other.LastError.Error()))
(th.LastError != nil && other.LastError != nil && th.LastError.Error() == other.LastError.Error())) &&
proto.Equal(th.Timestamp, other.Timestamp)
}

// GetTabletHostPort formats a tablet host port address.
Expand Down
18 changes: 12 additions & 6 deletions go/vt/discovery/tablet_health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@ import (
"sync/atomic"
"time"

"google.golang.org/grpc"
"google.golang.org/protobuf/proto"

"vitess.io/vitess/go/protoutil"
"vitess.io/vitess/go/vt/grpcclient"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/proto/vttime"
"vitess.io/vitess/go/vt/topo/topoproto"
"vitess.io/vitess/go/vt/topotools"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vttablet/queryservice"
"vitess.io/vitess/go/vt/vttablet/tabletconn"

"google.golang.org/grpc"
"google.golang.org/protobuf/proto"

"vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/proto/topodata"
)

// withDialerContextOnce ensures grpc.WithDialContext() is added once to the options.
Expand Down Expand Up @@ -73,6 +74,8 @@ type tabletHealthCheck struct {
// LastError is the error we last saw when trying to get the
// tablet's healthcheck.
LastError error
// Timestamp represents the time the healthcheck data was produced.
Timestamp *vttime.Time
// possibly delete both these
loggedServingState bool
lastResponseTimestamp time.Time // timestamp of the last healthcheck response
Expand All @@ -98,6 +101,7 @@ func (thc *tabletHealthCheck) SimpleCopy() *TabletHealth {
LastError: thc.LastError,
PrimaryTermStartTime: thc.PrimaryTermStartTime,
Serving: thc.Serving,
Timestamp: thc.Timestamp,
}
}

Expand All @@ -124,6 +128,7 @@ func (thc *tabletHealthCheck) setServingState(serving bool, reason string) {
thc.loggedServingState = true
}
thc.Serving = serving
thc.Timestamp = protoutil.TimeToProto(time.Now())
}

// stream streams healthcheck responses to callback.
Expand Down Expand Up @@ -217,6 +222,7 @@ func (thc *tabletHealthCheck) processResponse(hc *HealthCheckImpl, shr *query.St
thc.PrimaryTermStartTime = shr.PrimaryTermStartTimestamp
thc.Stats = shr.RealtimeStats
thc.LastError = healthErr
thc.Timestamp = shr.Timestamp
reason := "healthCheck update"
if healthErr != nil {
reason = "healthCheck update error: " + healthErr.Error()
Expand Down
Loading