Skip to content

Commit

Permalink
test: add test for verifying we mark demote primary stalled if it tak…
Browse files Browse the repository at this point in the history
…es too longs

Signed-off-by: Manan Gupta <[email protected]>
  • Loading branch information
GuptaManan100 committed Dec 19, 2024
1 parent 7258704 commit 50f311a
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions go/vt/vttablet/tabletmanager/rpc_replication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
"time"

"github.com/stretchr/testify/require"
"golang.org/x/sync/semaphore"

"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vttablet/tabletserver"
)

// TestWaitForGrantsToHaveApplied tests that waitForGrantsToHaveApplied only succeeds after waitForDBAGrants has been called.
Expand All @@ -42,3 +46,50 @@ func TestWaitForGrantsToHaveApplied(t *testing.T) {
err = tm.waitForGrantsToHaveApplied(secondContext)
require.NoError(t, err)
}

type demotePrimaryStallQS struct {
tabletserver.Controller
waitTime time.Duration
primaryStalled bool
}

func (d *demotePrimaryStallQS) SetDemotePrimaryStalled() {
d.primaryStalled = true
}

func (d *demotePrimaryStallQS) IsServing() bool {
time.Sleep(d.waitTime)
return false
}

// TestDemotePrimaryStalled checks that if demote primary takes too long, then we mark it as stalled.
func TestDemotePrimaryStalled(t *testing.T) {
// Set remote operation timeout to a very low value.
origVal := topo.RemoteOperationTimeout
topo.RemoteOperationTimeout = 100 * time.Millisecond
defer func() {
topo.RemoteOperationTimeout = origVal
}()

// Create a fake query service control to intercept calls from DemotePrimary function.
qsc := &demotePrimaryStallQS{
waitTime: 2 * time.Second,
primaryStalled: false,
}
// Create a tablet manager with a replica type tablet.
tm := &TabletManager{
actionSema: semaphore.NewWeighted(1),
MysqlDaemon: newTestMysqlDaemon(t, 1),
tmState: &tmState{
displayState: displayState{
tablet: newTestTablet(t, 100, "ks", "-", map[string]string{}),
},
},
QueryServiceControl: qsc,
}

// We make IsServing stall for over 2 seconds, which is longer than 10 * remote operation timeout.
// This should cause the demote primary operation to be stalled.
tm.demotePrimary(context.Background(), false)
require.True(t, qsc.primaryStalled)
}

0 comments on commit 50f311a

Please sign in to comment.