Skip to content

Commit

Permalink
DeleteTablet: allow deletion of old master tablet without -allow_mast…
Browse files Browse the repository at this point in the history
…er flag

Signed-off-by: deepthi <deepthi@planetscale.com>
  • Loading branch information
deepthi committed Jan 2, 2020
1 parent 85fc967 commit 510b1fe
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
8 changes: 7 additions & 1 deletion go/vt/wrangler/tablet.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ func (wr *Wrangler) DeleteTablet(ctx context.Context, tabletAlias *topodatapb.Ta
if err != nil {
return err
}
wasMaster := ti.Type == topodatapb.TabletType_MASTER
si, err := wr.ts.GetShard(ctx, ti.Keyspace, ti.Shard)
if err != nil {
return err
}

// the true master tablet will have the same MasterTermStartTime as the shard
wasMaster := ti.Type == topodatapb.TabletType_MASTER && topoproto.TabletAliasEqual(si.MasterAlias, tabletAlias) && logutil.ProtoToTime(ti.MasterTermStartTime).Equal(logutil.ProtoToTime(si.MasterTermStartTime))
if wasMaster && !allowMaster {
return fmt.Errorf("cannot delete tablet %v as it is a master, use allow_master flag", topoproto.TabletAliasString(tabletAlias))
}
Expand Down
101 changes: 101 additions & 0 deletions go/vt/wrangler/tablet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ limitations under the License.
package wrangler

import (
"strings"
"testing"

"golang.org/x/net/context"
"vitess.io/vitess/go/vt/logutil"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/memorytopo"
)

Expand Down Expand Up @@ -56,3 +58,102 @@ func TestInitTabletShardConversion(t *testing.T) {
t.Errorf("Got wrong tablet.KeyRange, got %v expected 80-c0", ti.KeyRange)
}
}

// TestDeleteTabletBasic tests delete of non-master tablet
func TestDeleteTabletBasic(t *testing.T) {
cell := "cell1"
ts := memorytopo.NewServer(cell)
wr := New(logutil.NewConsoleLogger(), ts, nil)

tablet := &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: cell,
Uid: 1,
},
Shard: "0",
}

if err := wr.InitTablet(context.Background(), tablet, false /*allowMasterOverride*/, true /*createShardAndKeyspace*/, false /*allowUpdate*/); err != nil {
t.Fatalf("InitTablet failed: %v", err)
}

if _, err := ts.GetTablet(context.Background(), tablet.Alias); err != nil {
t.Fatalf("GetTablet failed: %v", err)
}

if err := wr.DeleteTablet(context.Background(), tablet.Alias, false); err != nil {
t.Fatalf("DeleteTablet failed: %v", err)
}
}

// TestDeleteTabletTrueMaster tests that you can delete a true master tablet
// only if allowMaster is set to true
func TestDeleteTabletTrueMaster(t *testing.T) {
cell := "cell1"
ts := memorytopo.NewServer(cell)
wr := New(logutil.NewConsoleLogger(), ts, nil)

_, err := ts.GetOrCreateShard(context.Background(), "test", "0")
tablet := &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: cell,
Uid: 1,
},
Keyspace: "test",
Shard: "0",
Type: topodatapb.TabletType_MASTER,
}

if err := wr.InitTablet(context.Background(), tablet, false /*allowMasterOverride*/, false /*createShardAndKeyspace*/, false /*allowUpdate*/); err != nil {
t.Fatalf("InitTablet failed: %v", err)
}
if _, err := ts.GetTablet(context.Background(), tablet.Alias); err != nil {
t.Fatalf("GetTablet failed: %v", err)
}

// set MasterAlias and MasterTermStartTime on shard to match chosen master tablet
_, err = ts.UpdateShardFields(context.Background(), "test", "0", func(si *topo.ShardInfo) error {
si.MasterAlias = tablet.Alias
si.MasterTermStartTime = tablet.MasterTermStartTime
return nil
})

err = wr.DeleteTablet(context.Background(), tablet.Alias, false)
wantError := "as it is a master, use allow_master flag"
if err == nil || !strings.Contains(err.Error(), wantError) {
t.Fatalf("DeleteTablet on master: want error = %v, got error = %v", wantError, err)
}

if err := wr.DeleteTablet(context.Background(), tablet.Alias, true); err != nil {
t.Fatalf("DeleteTablet failed: %v", err)
}
}

// TestDeleteTabletFalseMaster tests that you can delete a false master tablet
// with allowMaster set to false
func TestDeleteTabletFalseMaster(t *testing.T) {
cell := "cell1"
ts := memorytopo.NewServer(cell)
wr := New(logutil.NewConsoleLogger(), ts, nil)

tablet := &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: cell,
Uid: 1,
},
Keyspace: "test",
Shard: "0",
Type: topodatapb.TabletType_MASTER,
}

if err := wr.InitTablet(context.Background(), tablet, false /*allowMasterOverride*/, true /*createShardAndKeyspace*/, false /*allowUpdate*/); err != nil {
t.Fatalf("InitTablet failed: %v", err)
}
if _, err := ts.GetTablet(context.Background(), tablet.Alias); err != nil {
t.Fatalf("GetTablet failed: %v", err)
}

if err := wr.DeleteTablet(context.Background(), tablet.Alias, false); err != nil {
t.Fatalf("DeleteTablet failed: %v", err)
}
}

0 comments on commit 510b1fe

Please sign in to comment.