Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request vitessio#6762 from planetscale/ds-set-readonly
Browse files Browse the repository at this point in the history
tm: call setReadOnly inside ChangeTabletType
  • Loading branch information
rafael authored and ameetkotian committed Sep 29, 2020
1 parent e2b100c commit b4900b4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
28 changes: 28 additions & 0 deletions go/test/endtoend/tabletmanager/tablet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,40 @@ import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"vitess.io/vitess/go/test/endtoend/cluster"
"vitess.io/vitess/go/vt/log"
)

// TestEnsureDB tests that vttablet creates the db as needed
func TestEnsureDB(t *testing.T) {
defer cluster.PanicHandler(t)

// Create new tablet
tablet := clusterInstance.NewVttabletInstance("replica", 0, "")
tablet.MysqlctlProcess = *cluster.MysqlCtlProcessInstance(tablet.TabletUID, tablet.MySQLPort, clusterInstance.TmpDirectory)
err := tablet.MysqlctlProcess.Start()
require.NoError(t, err)

log.Info(fmt.Sprintf("Started vttablet %v", tablet))
// Start vttablet process as replica. It won't be able to serve because there's no db.
err = clusterInstance.StartVttablet(tablet, "NOT_SERVING", false, cell, "dbtest", hostname, "0")
require.NoError(t, err)

// Make it the master.
err = clusterInstance.VtctlclientProcess.ExecuteCommand("TabletExternallyReparented", tablet.Alias)
require.NoError(t, err)

// It goes SERVING because TER calls ChangeTabletType which will also set the database to read-write
assert.Equal(t, "SERVING", tablet.VttabletProcess.GetTabletStatus())
status := tablet.VttabletProcess.GetStatusDetails()
assert.Contains(t, status, "Serving")

killTablets(t, tablet)
}

// TestLocalMetadata tests the contents of local_metadata table after vttablet startup
func TestLocalMetadata(t *testing.T) {
defer cluster.PanicHandler(t)
Expand Down
10 changes: 0 additions & 10 deletions go/vt/vttablet/tabletmanager/rpc_replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,6 @@ func (tm *TabletManager) InitMaster(ctx context.Context) (string, error) {
// Set the server read-write, from now on we can accept real
// client writes. Note that if semi-sync replication is enabled,
// we'll still need some replicas to be able to commit transactions.
if err := tm.MysqlDaemon.SetReadOnly(false); err != nil {
return "", err
}

if err := tm.changeTypeLocked(ctx, topodatapb.TabletType_MASTER); err != nil {
return "", err
}
Expand Down Expand Up @@ -739,12 +735,6 @@ func (tm *TabletManager) PromoteReplica(ctx context.Context) (string, error) {
return "", err
}

// We call SetReadOnly only after the topo has been updated to avoid
// situations where two tablets are master at the DB level but not at the vitess level
if err := tm.MysqlDaemon.SetReadOnly(false); err != nil {
return "", err
}

return mysql.EncodePosition(pos), nil
}

Expand Down
5 changes: 5 additions & 0 deletions go/vt/vttablet/tabletmanager/tm_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ func (ts *tmState) ChangeTabletType(ctx context.Context, tabletType topodatapb.T
if err != nil {
return err
}
// We call SetReadOnly only after the topo has been updated to avoid
// situations where two tablets are master at the DB level but not at the vitess level
if err := ts.tm.MysqlDaemon.SetReadOnly(false); err != nil {
return err
}

ts.tablet.Type = tabletType
ts.tablet.MasterTermStartTime = masterTermStartTime
Expand Down

0 comments on commit b4900b4

Please sign in to comment.