Skip to content

Commit

Permalink
feat: add uptime implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Manan Gupta <manan@planetscale.com>
  • Loading branch information
GuptaManan100 committed May 15, 2024
1 parent 61061cb commit fd515c3
Show file tree
Hide file tree
Showing 22 changed files with 2,496 additions and 1,367 deletions.
5 changes: 5 additions & 0 deletions go/test/endtoend/tabletmanager/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ func tmcUnlockTables(ctx context.Context, tabletGrpcPort int) error {
return tmClient.UnlockTables(ctx, vtablet)
}

func tmcUptime(ctx context.Context, tabletGrpcPort int) (uint64, error) {
vtablet := getTablet(tabletGrpcPort)
return tmClient.Uptime(ctx, vtablet)
}

func tmcStopReplication(ctx context.Context, tabletGrpcPort int) error {
vtablet := getTablet(tabletGrpcPort)
return tmClient.StopReplication(ctx, vtablet)
Expand Down
16 changes: 16 additions & 0 deletions go/test/endtoend/tabletmanager/tablet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -100,3 +101,18 @@ func TestResetReplicationParameters(t *testing.T) {
require.NoError(t, err)
require.Len(t, res.Rows, 0)
}

// TestUptime tests the Uptime RPC
func TestUptime(t *testing.T) {
ctx := context.Background()
uptimeInitial, err := tmcUptime(ctx, replicaTablet.GrpcPort)
require.NoError(t, err)
require.Greater(t, uptimeInitial, uint64(0))
time.Sleep(1500 * time.Millisecond)
uptimeFinal, err := tmcUptime(ctx, replicaTablet.GrpcPort)
require.NoError(t, err)
require.Greater(t, uptimeFinal, uint64(0))
uptimeDiff := uptimeFinal - uptimeInitial
require.LessOrEqual(t, uptimeDiff, uint64(2))
require.GreaterOrEqual(t, uptimeDiff, uint64(1))
}
5 changes: 5 additions & 0 deletions go/vt/mysqlctl/fakemysqldaemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ func (fmd *FakeMysqlDaemon) GetServerUUID(ctx context.Context) (string, error) {
return "000000", nil
}

// Uptime is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) Uptime(ctx context.Context) (uint64, error) {
return 100, nil
}

// CurrentPrimaryPositionLocked is thread-safe.
func (fmd *FakeMysqlDaemon) CurrentPrimaryPositionLocked(pos replication.Position) {
fmd.mu.Lock()
Expand Down
3 changes: 3 additions & 0 deletions go/vt/mysqlctl/mysql_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ type MysqlDaemon interface {
// GetServerUUID returns the servers UUID
GetServerUUID(ctx context.Context) (string, error)

// Uptime gets the uptime for the server
Uptime(ctx context.Context) (uint64, error)

// replication related methods
StartReplication(ctx context.Context, hookExtraEnv map[string]string) error
RestartReplication(ctx context.Context, hookExtraEnv map[string]string) error
Expand Down
12 changes: 12 additions & 0 deletions go/vt/mysqlctl/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ func (mysqld *Mysqld) GetServerUUID(ctx context.Context) (string, error) {
return conn.Conn.GetServerUUID()
}

// Uptime returns the uptime for the server
func (mysqld *Mysqld) Uptime(ctx context.Context) (uint64, error) {
qr, err := mysqld.FetchSuperQuery(ctx, "SHOW STATUS LIKE 'uptime'")
if err != nil {
return 0, err
}
if len(qr.Rows) != 1 {
return 0, nil
}
return qr.Rows[0][1].ToCastUint64()
}

// IsReadOnly return true if the instance is read only
func (mysqld *Mysqld) IsReadOnly(ctx context.Context) (bool, error) {
qr, err := mysqld.FetchSuperQuery(ctx, "SHOW VARIABLES LIKE 'read_only'")
Expand Down
Loading

0 comments on commit fd515c3

Please sign in to comment.