本文整理汇总了Golang中github.com/youtube/vitess/go/vt/topo.UpdateShard函数的典型用法代码示例。如果您正苦于以下问题:Golang UpdateShard函数的具体用法?Golang UpdateShard怎么用?Golang UpdateShard使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了UpdateShard函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: finishReparent
func (wr *Wrangler) finishReparent(si *topo.ShardInfo, masterElect *topo.TabletInfo, majorityRestart, leaveMasterReadOnly bool) error {
// If the majority of slaves restarted, move ahead.
if majorityRestart {
if leaveMasterReadOnly {
wr.logger.Warningf("leaving master-elect read-only, change with: vtctl SetReadWrite %v", masterElect.Alias)
} else {
wr.logger.Infof("marking master-elect read-write %v", masterElect.Alias)
if err := wr.tmc.SetReadWrite(masterElect, wr.ActionTimeout()); err != nil {
wr.logger.Warningf("master master-elect read-write failed, leaving master-elect read-only, change with: vtctl SetReadWrite %v", masterElect.Alias)
}
}
} else {
wr.logger.Warningf("minority reparent, manual fixes are needed, leaving master-elect read-only, change with: vtctl SetReadWrite %v", masterElect.Alias)
}
// save the new master in the shard info
si.MasterAlias = masterElect.Alias
if err := topo.UpdateShard(wr.ts, si); err != nil {
wr.logger.Errorf("Failed to save new master into shard: %v", err)
return err
}
// We rebuild all the cells, as we may have taken tablets in and
// out of the graph.
wr.logger.Infof("rebuilding shard serving graph data")
return topotools.RebuildShard(wr.logger, wr.ts, masterElect.Keyspace, masterElect.Shard, nil, wr.lockTimeout, interrupted)
}
开发者ID:nangong92t,项目名称:go_src,代码行数:27,代码来源:reparent_action.go
示例2: replicaMigrateServedFrom
// replicaMigrateServedFrom handles the slave (replica, rdonly) migration.
func (wr *Wrangler) replicaMigrateServedFrom(ctx context.Context, ki *topo.KeyspaceInfo, sourceShard *topo.ShardInfo, destinationShard *topo.ShardInfo, servedType pb.TabletType, cells []string, reverse bool, tables []string, ev *events.MigrateServedFrom) error {
// Save the destination keyspace (its ServedFrom has been changed)
event.DispatchUpdate(ev, "updating keyspace")
if err := topo.UpdateKeyspace(ctx, wr.ts, ki); err != nil {
return err
}
// Save the source shard (its blacklisted tables field has changed)
event.DispatchUpdate(ev, "updating source shard")
if err := sourceShard.UpdateSourceBlacklistedTables(servedType, cells, reverse, tables); err != nil {
return fmt.Errorf("UpdateSourceBlacklistedTables(%v/%v) failed: %v", sourceShard.Keyspace(), sourceShard.ShardName(), err)
}
if err := topo.UpdateShard(ctx, wr.ts, sourceShard); err != nil {
return fmt.Errorf("UpdateShard(%v/%v) failed: %v", sourceShard.Keyspace(), sourceShard.ShardName(), err)
}
// Now refresh the source servers so they reload their
// blacklisted table list
event.DispatchUpdate(ev, "refreshing sources tablets state so they update their blacklisted tables")
if err := wr.RefreshTablesByShard(ctx, sourceShard, servedType, cells); err != nil {
return err
}
return nil
}
开发者ID:haoqoo,项目名称:vitess,代码行数:26,代码来源:keyspace.go
示例3: replicaMigrateServedFrom
// replicaMigrateServedFrom handles the slave (replica, rdonly) migration.
func (wr *Wrangler) replicaMigrateServedFrom(ki *topo.KeyspaceInfo, sourceShard *topo.ShardInfo, destinationShard *topo.ShardInfo, servedType topo.TabletType, reverse bool, tables []string, ev *events.MigrateServedFrom) error {
// Save the destination keyspace (its ServedFrom has been changed)
event.DispatchUpdate(ev, "updating keyspace")
if err := topo.UpdateKeyspace(wr.ts, ki); err != nil {
return err
}
// Save the source shard (its blacklisted tables field has changed)
event.DispatchUpdate(ev, "updating source shard")
if sourceShard.BlacklistedTablesMap == nil {
sourceShard.BlacklistedTablesMap = make(map[topo.TabletType][]string)
}
if reverse {
delete(sourceShard.BlacklistedTablesMap, servedType)
} else {
sourceShard.BlacklistedTablesMap[servedType] = tables
}
if err := topo.UpdateShard(wr.ts, sourceShard); err != nil {
return err
}
// Now refresh the source servers so they reload their
// blacklisted table list
event.DispatchUpdate(ev, "refreshing sources tablets state so they update their blacklisted tables")
if err := wr.RefreshTablesByShard(sourceShard.Keyspace(), sourceShard.ShardName(), servedType); err != nil {
return err
}
return nil
}
开发者ID:nangong92t,项目名称:go_src,代码行数:31,代码来源:keyspace.go
示例4: setShardServedTypes
func (wr *Wrangler) setShardServedTypes(keyspace, shard string, servedTypes []topo.TabletType) error {
shardInfo, err := wr.ts.GetShard(keyspace, shard)
if err != nil {
return err
}
shardInfo.ServedTypes = servedTypes
return topo.UpdateShard(wr.ts, shardInfo)
}
开发者ID:chinna1986,项目名称:vitess,代码行数:9,代码来源:shard.go
示例5: setShardServedTypes
func (wr *Wrangler) setShardServedTypes(ctx context.Context, keyspace, shard string, cells []string, servedType topo.TabletType, remove bool) error {
si, err := wr.ts.GetShard(ctx, keyspace, shard)
if err != nil {
return err
}
if err := si.UpdateServedTypesMap(servedType, cells, remove); err != nil {
return err
}
return topo.UpdateShard(ctx, wr.ts, si)
}
开发者ID:afrolovskiy,项目名称:vitess,代码行数:11,代码来源:shard.go
示例6: updateShardCellsAndMaster
// updateShardCellsAndMaster will update the 'Cells' and possibly
// MasterAlias records for the shard, if needed.
func (wr *Wrangler) updateShardCellsAndMaster(ctx context.Context, si *topo.ShardInfo, tabletAlias topo.TabletAlias, tabletType topo.TabletType, force bool) error {
// See if we need to update the Shard:
// - add the tablet's cell to the shard's Cells if needed
// - change the master if needed
shardUpdateRequired := false
if !si.HasCell(tabletAlias.Cell) {
shardUpdateRequired = true
}
if tabletType == topo.TYPE_MASTER && si.MasterAlias != tabletAlias {
shardUpdateRequired = true
}
if !shardUpdateRequired {
return nil
}
actionNode := actionnode.UpdateShard()
keyspace := si.Keyspace()
shard := si.ShardName()
lockPath, err := wr.lockShard(ctx, keyspace, shard, actionNode)
if err != nil {
return err
}
// re-read the shard with the lock
si, err = wr.ts.GetShard(ctx, keyspace, shard)
if err != nil {
return wr.unlockShard(ctx, keyspace, shard, actionNode, lockPath, err)
}
// update it
wasUpdated := false
if !si.HasCell(tabletAlias.Cell) {
si.Cells = append(si.Cells, tabletAlias.Cell)
wasUpdated = true
}
if tabletType == topo.TYPE_MASTER && si.MasterAlias != tabletAlias {
if !si.MasterAlias.IsZero() && !force {
return wr.unlockShard(ctx, keyspace, shard, actionNode, lockPath, fmt.Errorf("creating this tablet would override old master %v in shard %v/%v", si.MasterAlias, keyspace, shard))
}
si.MasterAlias = tabletAlias
wasUpdated = true
}
if wasUpdated {
// write it back
if err := topo.UpdateShard(ctx, wr.ts, si); err != nil {
return wr.unlockShard(ctx, keyspace, shard, actionNode, lockPath, err)
}
}
// and unlock
return wr.unlockShard(ctx, keyspace, shard, actionNode, lockPath, err)
}
开发者ID:afrolovskiy,项目名称:vitess,代码行数:55,代码来源:shard.go
示例7: removeShardCell
func (wr *Wrangler) removeShardCell(keyspace, shard, cell string, force bool) error {
shardInfo, err := wr.ts.GetShardCritical(keyspace, shard)
if err != nil {
return err
}
// check the cell is in the list already
if !topo.InCellList(cell, shardInfo.Cells) {
return fmt.Errorf("cell %v in not in shard info", cell)
}
// check the master alias is not in the cell
if shardInfo.MasterAlias.Cell == cell {
return fmt.Errorf("master %v is in the cell '%v' we want to remove", shardInfo.MasterAlias, cell)
}
// get the ShardReplication object in the cell
sri, err := wr.ts.GetShardReplication(cell, keyspace, shard)
switch err {
case nil:
if len(sri.ReplicationLinks) > 0 {
return fmt.Errorf("cell %v has %v possible tablets in replication graph", cell, len(sri.ReplicationLinks))
}
// ShardReplication object is now useless, remove it
if err := wr.ts.DeleteShardReplication(cell, keyspace, shard); err != nil {
return fmt.Errorf("error deleting ShardReplication object in cell %v: %v", cell, err)
}
// we keep going
case topo.ErrNoNode:
// no ShardReplication object, we keep going
default:
// we can't get the object, assume topo server is down there,
// so we look at force flag
if !force {
return err
}
log.Warningf("Cannot get ShardReplication from cell %v, assuming cell topo server is down, and forcing the removal", cell)
}
// now we can update the shard
log.Infof("Removing cell %v from shard %v/%v", cell, keyspace, shard)
newCells := make([]string, 0, len(shardInfo.Cells)-1)
for _, c := range shardInfo.Cells {
if c != cell {
newCells = append(newCells, c)
}
}
shardInfo.Cells = newCells
return topo.UpdateShard(wr.ts, shardInfo)
}
开发者ID:chinna1986,项目名称:vitess,代码行数:53,代码来源:shard.go
示例8: TestReparentTablet
func TestReparentTablet(t *testing.T) {
ctx := context.Background()
ts := zktopo.NewTestServer(t, []string{"cell1", "cell2"})
wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), time.Second)
// create shard and tablets
if err := topo.CreateShard(ctx, ts, "test_keyspace", "0"); err != nil {
t.Fatalf("CreateShard failed: %v", err)
}
master := NewFakeTablet(t, wr, "cell1", 1, pb.TabletType_MASTER)
slave := NewFakeTablet(t, wr, "cell1", 2, pb.TabletType_REPLICA)
// mark the master inside the shard
si, err := ts.GetShard(ctx, "test_keyspace", "0")
if err != nil {
t.Fatalf("GetShard failed: %v", err)
}
si.MasterAlias = master.Tablet.Alias
if err := topo.UpdateShard(ctx, ts, si); err != nil {
t.Fatalf("UpdateShard failed: %v", err)
}
// master action loop (to initialize host and port)
master.StartActionLoop(t, wr)
defer master.StopActionLoop(t)
// slave loop
slave.FakeMysqlDaemon.SetMasterCommandsInput = fmt.Sprintf("%v:%v", master.Tablet.Hostname, master.Tablet.PortMap["mysql"])
slave.FakeMysqlDaemon.SetMasterCommandsResult = []string{"set master cmd 1"}
slave.FakeMysqlDaemon.ExpectedExecuteSuperQueryList = []string{
"set master cmd 1",
}
slave.StartActionLoop(t, wr)
defer slave.StopActionLoop(t)
// run ReparentTablet
if err := wr.ReparentTablet(ctx, slave.Tablet.Alias); err != nil {
t.Fatalf("ReparentTablet failed: %v", err)
}
// check what was run
if err := slave.FakeMysqlDaemon.CheckSuperQueryList(); err != nil {
t.Fatalf("slave.FakeMysqlDaemon.CheckSuperQueryList failed: %v", err)
}
}
开发者ID:springlee,项目名称:vitess,代码行数:45,代码来源:reparent_utils_test.go
示例9: setShardTabletControl
func (wr *Wrangler) setShardTabletControl(ctx context.Context, keyspace, shard string, tabletType topo.TabletType, cells []string, remove, disableQueryService bool, tables []string) error {
shardInfo, err := wr.ts.GetShard(ctx, keyspace, shard)
if err != nil {
return err
}
if len(tables) == 0 && !remove {
// we are setting the DisableQueryService flag only
if err := shardInfo.UpdateDisableQueryService(tabletType, cells, disableQueryService); err != nil {
return fmt.Errorf("UpdateDisableQueryService(%v/%v) failed: %v", shardInfo.Keyspace(), shardInfo.ShardName(), err)
}
} else {
// we are setting / removing the blacklisted tables only
if err := shardInfo.UpdateSourceBlacklistedTables(tabletType, cells, remove, tables); err != nil {
return fmt.Errorf("UpdateSourceBlacklistedTables(%v/%v) failed: %v", shardInfo.Keyspace(), shardInfo.ShardName(), err)
}
}
return topo.UpdateShard(ctx, wr.ts, shardInfo)
}
开发者ID:afrolovskiy,项目名称:vitess,代码行数:19,代码来源:shard.go
示例10: SetSourceShards
// SetSourceShards is a utility function to override the SourceShards fields
// on a Shard.
func (wr *Wrangler) SetSourceShards(ctx context.Context, keyspace, shard string, sources []topo.TabletAlias, tables []string) error {
// read the shard
shardInfo, err := wr.ts.GetShard(ctx, keyspace, shard)
if err != nil {
return err
}
// If the shard already has sources, maybe it's already been restored,
// so let's be safe and abort right here.
if len(shardInfo.SourceShards) > 0 {
return fmt.Errorf("Shard %v/%v already has SourceShards, not overwriting them", keyspace, shard)
}
// read the source tablets
sourceTablets, err := topo.GetTabletMap(ctx, wr.TopoServer(), sources)
if err != nil {
return err
}
// Insert their KeyRange in the SourceShards array.
// We use a linear 0-based id, that matches what mysqlctld/split.go
// inserts into _vt.blp_checkpoint.
shardInfo.SourceShards = make([]*pb.Shard_SourceShard, len(sourceTablets))
i := 0
for _, ti := range sourceTablets {
shardInfo.SourceShards[i] = &pb.Shard_SourceShard{
Uid: uint32(i),
Keyspace: ti.Keyspace,
Shard: ti.Shard,
KeyRange: key.KeyRangeToProto(ti.KeyRange),
Tables: tables,
}
i++
}
// and write the shard
if err = topo.UpdateShard(ctx, wr.ts, shardInfo); err != nil {
return err
}
return nil
}
开发者ID:haoqoo,项目名称:vitess,代码行数:44,代码来源:split.go
示例11: sourceShardDelete
func (wr *Wrangler) sourceShardDelete(ctx context.Context, keyspace, shard string, uid uint32) error {
si, err := wr.ts.GetShard(ctx, keyspace, shard)
if err != nil {
return err
}
newSourceShards := make([]topo.SourceShard, 0, 0)
for _, ss := range si.SourceShards {
if ss.Uid != uid {
newSourceShards = append(newSourceShards, ss)
}
}
if len(newSourceShards) == len(si.SourceShards) {
return fmt.Errorf("no SourceShard with uid %v", uid)
}
if len(newSourceShards) == 0 {
newSourceShards = nil
}
si.SourceShards = newSourceShards
return topo.UpdateShard(ctx, wr.ts, si)
}
开发者ID:afrolovskiy,项目名称:vitess,代码行数:20,代码来源:shard.go
示例12: sourceShardAdd
func (wr *Wrangler) sourceShardAdd(ctx context.Context, keyspace, shard string, uid uint32, skeyspace, sshard string, keyRange key.KeyRange, tables []string) error {
si, err := wr.ts.GetShard(ctx, keyspace, shard)
if err != nil {
return err
}
// check the uid is not used already
for _, ss := range si.SourceShards {
if ss.Uid == uid {
return fmt.Errorf("uid %v is already in use", uid)
}
}
si.SourceShards = append(si.SourceShards, topo.SourceShard{
Uid: uid,
Keyspace: skeyspace,
Shard: sshard,
KeyRange: keyRange,
Tables: tables,
})
return topo.UpdateShard(ctx, wr.ts, si)
}
开发者ID:afrolovskiy,项目名称:vitess,代码行数:22,代码来源:shard.go
示例13: setShardBlacklistedTables
func (wr *Wrangler) setShardBlacklistedTables(keyspace, shard string, tabletType topo.TabletType, tables []string) error {
shardInfo, err := wr.ts.GetShard(keyspace, shard)
if err != nil {
return err
}
if len(tables) == 0 {
// it's a removal
if shardInfo.BlacklistedTablesMap != nil {
delete(shardInfo.BlacklistedTablesMap, tabletType)
if len(shardInfo.BlacklistedTablesMap) == 0 {
shardInfo.BlacklistedTablesMap = nil
}
}
} else {
// it's an addition
if shardInfo.BlacklistedTablesMap == nil {
shardInfo.BlacklistedTablesMap = make(map[topo.TabletType][]string)
}
shardInfo.BlacklistedTablesMap[tabletType] = tables
}
return topo.UpdateShard(wr.ts, shardInfo)
}
开发者ID:nangong92t,项目名称:go_src,代码行数:23,代码来源:shard.go
示例14: migrateServedTypes
// migrateServedTypes operates with all concerned shards locked.
func (wr *Wrangler) migrateServedTypes(keyspace string, sourceShards, destinationShards []*topo.ShardInfo, servedType topo.TabletType, reverse bool, shardCache map[string]*topo.ShardInfo) (err error) {
// re-read all the shards so we are up to date
for i, si := range sourceShards {
if sourceShards[i], err = wr.ts.GetShard(si.Keyspace(), si.ShardName()); err != nil {
return err
}
shardCache[si.ShardName()] = sourceShards[i]
}
for i, si := range destinationShards {
if destinationShards[i], err = wr.ts.GetShard(si.Keyspace(), si.ShardName()); err != nil {
return err
}
shardCache[si.ShardName()] = destinationShards[i]
}
ev := &events.MigrateServedTypes{
Keyspace: *topo.NewKeyspaceInfo(keyspace, nil, -1),
SourceShards: sourceShards,
DestinationShards: destinationShards,
ServedType: servedType,
Reverse: reverse,
}
event.DispatchUpdate(ev, "start")
defer func() {
if err != nil {
event.DispatchUpdate(ev, "failed: "+err.Error())
}
}()
// check and update all shard records, in memory only
for _, si := range sourceShards {
if reverse {
// need to add to source
if topo.IsTypeInList(servedType, si.ServedTypes) {
return fmt.Errorf("Source shard %v/%v is already serving type %v", si.Keyspace(), si.ShardName(), servedType)
}
si.ServedTypes = append(si.ServedTypes, servedType)
} else {
// need to remove from source
var found bool
if si.ServedTypes, found = removeType(servedType, si.ServedTypes); !found {
return fmt.Errorf("Source shard %v/%v is not serving type %v", si.Keyspace(), si.ShardName(), servedType)
}
}
}
for _, si := range destinationShards {
if reverse {
// need to remove from destination
var found bool
if si.ServedTypes, found = removeType(servedType, si.ServedTypes); !found {
return fmt.Errorf("Destination shard %v/%v is not serving type %v", si.Keyspace(), si.ShardName(), servedType)
}
} else {
// need to add to destination
if topo.IsTypeInList(servedType, si.ServedTypes) {
return fmt.Errorf("Destination shard %v/%v is already serving type %v", si.Keyspace(), si.ShardName(), servedType)
}
si.ServedTypes = append(si.ServedTypes, servedType)
}
}
// For master type migration, need to:
// - switch the source shards to read-only
// - gather all replication points
// - wait for filtered replication to catch up before we continue
// - disable filtered replication after the fact
if servedType == topo.TYPE_MASTER {
event.DispatchUpdate(ev, "setting all source masters read-only")
err := wr.makeMastersReadOnly(sourceShards)
if err != nil {
return err
}
event.DispatchUpdate(ev, "getting positions of source masters")
masterPositions, err := wr.getMastersPosition(sourceShards)
if err != nil {
return err
}
event.DispatchUpdate(ev, "waiting for destination masters to catch up")
if err := wr.waitForFilteredReplication(masterPositions, destinationShards); err != nil {
return err
}
for _, si := range destinationShards {
si.SourceShards = nil
}
}
// All is good, we can save the shards now
event.DispatchUpdate(ev, "updating source shards")
for _, si := range sourceShards {
if err := topo.UpdateShard(wr.ts, si); err != nil {
return err
}
shardCache[si.ShardName()] = si
}
event.DispatchUpdate(ev, "updating destination shards")
//.........这里部分代码省略.........
开发者ID:chinna1986,项目名称:vitess,代码行数:101,代码来源:keyspace.go
示例15: migrateServedFrom
func (wr *Wrangler) migrateServedFrom(ki *topo.KeyspaceInfo, si *topo.ShardInfo, servedType topo.TabletType, reverse bool) (err error) {
// re-read and update keyspace info record
ki, err = wr.ts.GetKeyspace(ki.KeyspaceName())
if err != nil {
return err
}
if reverse {
if _, ok := ki.ServedFrom[servedType]; ok {
return fmt.Errorf("Destination Keyspace %s is not serving type %v", ki.KeyspaceName(), servedType)
}
ki.ServedFrom[servedType] = si.SourceShards[0].Keyspace
} else {
if _, ok := ki.ServedFrom[servedType]; !ok {
return fmt.Errorf("Destination Keyspace %s is already serving type %v", ki.KeyspaceName(), servedType)
}
delete(ki.ServedFrom, servedType)
}
// re-read and check the destination shard
si, err = wr.ts.GetShard(si.Keyspace(), si.ShardName())
if err != nil {
return err
}
if len(si.SourceShards) != 1 {
return fmt.Errorf("Destination shard %v/%v is not a vertical split target", si.Keyspace(), si.ShardName())
}
tables := si.SourceShards[0].Tables
// read the source shard, we'll need its master
sourceShard, err := wr.ts.GetShard(si.SourceShards[0].Keyspace, si.SourceShards[0].Shard)
if err != nil {
return err
}
ev := &events.MigrateServedFrom{
Keyspace: *ki,
SourceShard: *sourceShard,
DestinationShard: *si,
ServedType: servedType,
Reverse: reverse,
}
event.DispatchUpdate(ev, "start")
defer func() {
if err != nil {
event.DispatchUpdate(ev, "failed: "+err.Error())
}
}()
// For master type migration, need to:
// - switch the source shard to read-only
// - gather the replication point
// - wait for filtered replication to catch up before we continue
// - disable filtered replication after the fact
var sourceMasterTabletInfo *topo.TabletInfo
if servedType == topo.TYPE_MASTER {
// set master to read-only
event.DispatchUpdate(ev, "setting source shard master to read-only")
actionPath, err := wr.ai.SetReadOnly(sourceShard.MasterAlias)
if err != nil {
return err
}
if err := wr.WaitForCompletion(actionPath); err != nil {
return err
}
// get the position
event.DispatchUpdate(ev, "getting master position")
sourceMasterTabletInfo, err = wr.ts.GetTablet(sourceShard.MasterAlias)
if err != nil {
return err
}
masterPosition, err := wr.ai.MasterPosition(sourceMasterTabletInfo, wr.ActionTimeout())
if err != nil {
return err
}
// wait for it
event.DispatchUpdate(ev, "waiting for destination master to catch up to source master")
if err := wr.ai.WaitBlpPosition(si.MasterAlias, blproto.BlpPosition{
Uid: 0,
Position: masterPosition,
}, wr.ActionTimeout()); err != nil {
return err
}
// and clear the shard record
si.SourceShards = nil
}
// All is good, we can save the keyspace and shard (if needed) now
event.DispatchUpdate(ev, "updating keyspace")
if err = topo.UpdateKeyspace(wr.ts, ki); err != nil {
return err
}
event.DispatchUpdate(ev, "updating destination shard")
if servedType == topo.TYPE_MASTER {
if err := topo.UpdateShard(wr.ts, si); err != nil {
return err
}
//.........这里部分代码省略.........
开发者ID:chinna1986,项目名称:vitess,代码行数:101,代码来源:keyspace.go
示例16: TestInitMasterShardOneSlaveFails
// TestInitMasterShardOneSlaveFails makes sure that if one slave fails to
// proceed, the action completes anyway
func TestInitMasterShardOneSlaveFails(t *testing.T) {
ctx := context.Background()
ts := zktopo.NewTestServer(t, []string{"cell1", "cell2"})
wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), time.Second)
// Create a master, a couple slaves
master := NewFakeTablet(t, wr, "cell1", 0, pb.TabletType_MASTER)
goodSlave := NewFakeTablet(t, wr, "cell1", 1, pb.TabletType_REPLICA)
badSlave := NewFakeTablet(t, wr, "cell2", 2, pb.TabletType_REPLICA)
// Master: set a plausible ReplicationPosition to return,
// and expect to add entry in _vt.reparent_journal
master.FakeMysqlDaemon.CurrentMasterPosition = myproto.ReplicationPosition{
GTIDSet: myproto.MariadbGTID{
Domain: 5,
Server: 456,
Sequence: 890,
},
}
master.FakeMysqlDaemon.ReadOnly = true
master.FakeMysqlDaemon.ExpectedExecuteSuperQueryList = []string{
"CREATE DATABASE IF NOT EXISTS _vt",
"SUBCREATE TABLE IF NOT EXISTS _vt.reparent_journal",
"CREATE DATABASE IF NOT EXISTS _vt",
"SUBCREATE TABLE IF NOT EXISTS _vt.reparent_journal",
"SUBINSERT INTO _vt.reparent_journal (time_created_ns, action_name, master_alias, replication_position) VALUES",
}
master.StartActionLoop(t, wr)
defer master.StopActionLoop(t)
// goodSlave: expect to be re-parented
goodSlave.FakeMysqlDaemon.ReadOnly = true
goodSlave.FakeMysqlDaemon.StartReplicationCommandsStatus = &myproto.ReplicationStatus{
Position: master.FakeMysqlDaemon.CurrentMasterPosition,
MasterHost: master.Tablet.Hostname,
MasterPort: int(master.Tablet.PortMap["mysql"]),
MasterConnectRetry: 10,
}
goodSlave.FakeMysqlDaemon.StartReplicationCommandsResult = []string{"cmd1"}
goodSlave.FakeMysqlDaemon.ExpectedExecuteSuperQueryList = goodSlave.FakeMysqlDaemon.StartReplicationCommandsResult
goodSlave.StartActionLoop(t, wr)
defer goodSlave.StopActionLoop(t)
// badSlave: insert an error by failing the ReplicationStatus input
// on purpose
badSlave.FakeMysqlDaemon.ReadOnly = true
badSlave.FakeMysqlDaemon.StartReplicationCommandsStatus = &myproto.ReplicationStatus{
Position: master.FakeMysqlDaemon.CurrentMasterPosition,
MasterHost: "",
MasterPort: 0,
MasterConnectRetry: 10,
}
badSlave.StartActionLoop(t, wr)
defer badSlave.StopActionLoop(t)
// also change the master alias in the Shard object, to make sure it
// is set back.
si, err := ts.GetShard(ctx, master.Tablet.Keyspace, master.Tablet.Shard)
if err != nil {
t.Fatalf("GetShard failed: %v", err)
}
si.MasterAlias.Uid++
if err := topo.UpdateShard(ctx, ts, si); err != nil {
t.Fatalf("UpdateShard failed: %v", err)
}
// run InitShardMaster without force, it fails because master is
// changing.
if err := wr.InitShardMaster(ctx, master.Tablet.Keyspace, master.Tablet.Shard, master.Tablet.Alias, false /*force*/, 10*time.Second); err == nil || !strings.Contains(err.Error(), "is not the shard master") {
t.Errorf("InitShardMaster with mismatched new master returned wrong error: %v", err)
}
// run InitShardMaster
if err := wr.InitShardMaster(ctx, master.Tablet.Keyspace, master.Tablet.Shard, master.Tablet.Alias, true /*force*/, 10*time.Second); err == nil || !strings.Contains(err.Error(), "wrong status for StartReplicationCommands") {
t.Errorf("InitShardMaster with one failed slave returned wrong error: %v", err)
}
// check what was run: master should still be good
if master.FakeMysqlDaemon.ReadOnly {
t.Errorf("master was not turned read-write")
}
si, err = ts.GetShard(ctx, master.Tablet.Keyspace, master.Tablet.Shard)
if err != nil {
t.Fatalf("GetShard failed: %v", err)
}
if !topo.TabletAliasEqual(si.MasterAlias, master.Tablet.Alias) {
t.Errorf("unexpected shard master alias, got %v expected %v", si.MasterAlias, master.Tablet.Alias)
}
}
开发者ID:springlee,项目名称:vitess,代码行数:91,代码来源:init_shard_master_test.go
示例17: TestTabletControl
// TestTabletControl verifies the shard's TabletControl record can disable
// query service in a tablet.
func TestTabletControl(t *testing.T) {
ctx := context.Background()
agent := createTestAgent(ctx, t)
targetTabletType := topo.TYPE_REPLICA
// first health check, should change us to replica
before := time.Now()
agent.runHealthCheck(targetTabletType)
ti, err := agent.TopoServer.GetTablet(ctx, tabletAlias)
if err != nil {
t.Fatalf("GetTablet failed: %v", err)
}
if ti.Type != targetTabletType {
t.Errorf("First health check failed to go to replica: %v", ti.Type)
}
if !agent.QueryServiceControl.IsServing() {
t.Errorf("Query service should be running")
}
if agent._healthyTime.Sub(before) < 0 {
t.Errorf("runHealthCheck did not update agent._healthyTime")
}
// now update the shard
si, err := agent.TopoServer.GetShard(ctx, keyspace, shard)
if err != nil {
t.Fatalf("GetShard failed: %v", err)
}
si.TabletControls = []*pb.Shard_TabletControl{
&pb.Shard_TabletControl{
TabletType: topo.TabletTypeToProto(targetTabletType),
DisableQueryService: true,
},
}
if err := topo.UpdateShard(ctx, agent.TopoServer, si); err != nil {
t.Fatalf("UpdateShard failed: %v", err)
}
// now refresh the tablet state, as the resharding process would do
agent.RPCWrapLockAction(ctx, actionnode.TabletActionRefreshState, "", "", true, func() error {
agent.RefreshState(ctx)
return nil
})
// check we shutdown query service
if agent.QueryServiceControl.IsServing() {
t.Errorf("Query service should not be running")
}
// check running a health check will not start it again
before = time.Now()
agent.runHealthCheck(targetTabletType)
ti, err = agent.TopoServer.GetTablet(ctx, tabletAlias)
if err != nil {
t.Fatalf("GetTablet failed: %v", err)
}
if ti.Type != targetTabletType {
t.Errorf("Health check failed to go to replica: %v", ti.Type)
}
if agent.QueryServiceControl.IsServing() {
t.Errorf("Query service should not be running")
}
if agent._healthyTime.Sub(before) < 0 {
t.Errorf("runHealthCheck did not update agent._healthyTime")
}
// go unhealthy, check we go to spare and QS is not running
agent.HealthReporter.(*fakeHealthCheck).reportError = fmt.Errorf("tablet is unhealthy")
before = time.Now()
agent.runHealthCheck(targetTabletType)
ti, err = agent.TopoServer.GetTablet(ctx, tabletAlias)
if err != nil {
t.Fatalf("GetTablet failed: %v", err)
}
if ti.Type != topo.TYPE_SPARE {
t.Errorf("Unhealthy health check should go to spare: %v", ti.Type)
}
if agent.QueryServiceControl.IsServing() {
t.Errorf("Query service should not be running")
}
if agent._healthyTime.Sub(before) < 0 {
t.Errorf("runHealthCheck did not update agent._healthyTime")
}
// go back healthy, check QS is still not running
agent.HealthReporter.(*fakeHealthCheck).reportError = nil
before = time.Now()
agent.runHealthCheck(targetTabletType)
ti, err = agent.TopoServer.GetTablet(ctx, tabletAlias)
if err != nil {
t.Fatalf("GetTablet failed: %v", err)
}
if ti.Type != targetTabletType {
t.Errorf("Healthy health check should go to replica: %v", ti.Type)
}
if agent.QueryServiceControl.IsServing() {
t.Errorf("Query service should not be running")
}
if agent._healthyTime.Sub(before) < 0 {
//.........这里部分代码省略.........
开发者ID:haoqoo,项目名称:vitess,代码行数:101,代码来源:healthcheck_test.go
示例18: migrateServedTypes
// migrateServedTypes operates with all concerned shards locked.
func (wr *Wrangler) migrateServedTypes(ctx context.Context, keyspace string, sourceShards, destinationShards []*topo.ShardInfo, cells []string, servedType pb.TabletType, reverse bool, filteredReplicationWaitTime time.Duration) (err error) {
// re-read all the shards so we are up to date
wr.Logger().Infof("Re-reading all shards")
for i, si := range sourceShards {
if sourceShards[i], err = wr.ts.GetShard(ctx, si.Keyspace(), si.ShardName()); err != nil {
return err
}
}
for i, si := range destinationShards {
if destinationShards[i], err = wr.ts.GetShard(ctx, si.Keyspace(), si.ShardName()); err != nil {
return err
}
}
ev := &events.MigrateServedTypes{
Keyspace: *topo.NewKeyspaceInfo(keyspace, nil, -1),
SourceShards: sourceShards,
DestinationShards: destinationShards,
ServedType: servedType,
Reverse: reverse,
}
event.DispatchUpdate(ev, "start")
defer func() {
if err != nil {
event.DispatchUpdate(ev, "failed: "+err.Error())
}
}()
// For master type migration, need to:
// - switch the source shards to read-only by disabling query service
// - gather all replication points
// - wait for filtered replication to catch up before we continue
// - disable filtered replication after the fact
if servedType == pb.TabletType_MASTER {
event.DispatchUpdate(ev, "disabling query service on all source masters")
for _, si := range sourceShards {
if err := si.UpdateDisableQueryService(pb.TabletType_MASTER, nil, true); err != nil {
return err
}
if err := topo.UpdateShard(ctx, wr.ts, si); err != nil {
return err
}
}
if err := wr.refreshMasters(ctx, sourceShards); err != nil {
return err
}
event.DispatchUpdate(ev, "getting positions of source masters")
masterPositions, err := wr.getMastersPosition(ctx, sourceShards)
if err != nil {
return err
}
event.DispatchUpdate(ev, "waiting for destination masters to catch up")
if err := wr.waitForFilteredReplication(ctx, masterPositions, destinationShards, filteredReplicationWaitTime); err != nil {
return err
}
for _, si := range destinationShards {
si.SourceShards = nil
}
}
// Check and update all shard records, in memory only.
// We remember if we need to refresh the state of the source tablets
// so their query service is enabled again, for reverse migration.
needToRefreshSourceTablets := false
for _, si := range sourceShards {
if err := si.UpdateServedTypesMap(servedType, cells, !reverse); err != nil {
return err
}
if tc := si.GetTabletControl(servedType); reverse && tc != nil && tc.DisableQueryService {
// this is a backward migration, where the
// source tablets were disabled previously, so
// we need to refresh them
if err := si.UpdateDisableQueryService(servedType, cells, false); err != nil {
return err
}
needToRefreshSourceTablets = true
}
if !reverse && servedType != pb.TabletType_MASTER {
// this is a forward migration, we need to disable
// query service on the source shards.
// (this was already done for masters earlier)
if err := si.UpdateDisableQueryService(servedType, cells, true); err != nil {
return err
}
}
}
// We remember if we need to refresh the state of the destination tablets
// so their query service will be enabled.
needToRefreshDestinationTablets := false
for _, si := range destinationShards {
if err := si.UpdateServedTypesMap(servedType, cells, reverse); err != nil {
return err
}
if tc := si.GetTabletControl(servedType); !reverse && tc != nil && tc.DisableQueryService {
// This is a forwards migration, and the destination query service was already in a disabled state.
//.........这里部分代码省略.........
开发者ID:haoqoo,项目名称:vitess,代码行数:101,代码来源:keyspace.go
示例19: TestInitTablet
//.........这里部分代码省略.........
t.Fatalf("GetTablet failed: %v", err)
}
if ti.Portmap["vt"] != port {
t.Errorf("wrong port for tablet: %v", ti.Portmap["vt"])
}
if ti.Portmap["grpc"] != gRPCPort {
t.Errorf("wrong gRPC port for tablet: %v", ti.Portmap["grpc"])
}
// try with a keyspace and shard on the previously idle tablet,
// should fail
*initTabletType = "replica"
*initKeyspace = "test_keyspace"
*initShard = "-80"
if err := agent.InitTablet(port, gRPCPort); err == nil || !strings.Contains(err.Error(), "InitTablet failed because existing tablet keyspace and shard / differ from the provided ones test_keyspace/-80") {
t.Fatalf("InitTablet(type over idle) didn't fail correctly: %v", err)
}
// now let's use a different real tablet in a shard, that will create
// the keyspace and shard.
tabletAlias = topo.TabletAlias{
Cell: "cell1",
Uid: 2,
}
agent.TabletAlias = tabletAlias
if err := agent.InitTablet(port, gRPCPort); err != nil {
t.Fatalf("InitTablet(type) failed: %v", err)
}
si, err := ts.GetShard(ctx, "test_keyspace", "-80")
if err != nil {
t.Fatalf("GetShard failed: %v", err)
}
if len(si.Cells) != 1 || si.Cells[0] != "cell1" {
t.Errorf("shard.Cells not updated properly: %v", si)
}
ti, err = ts.GetTablet(ctx, tabletAlias)
if err != nil {
t.Fatalf("GetTablet failed: %v", err)
}
if ti.Type != topo.TYPE_REPLICA {
t.Errorf("wrong tablet type: %v", ti.Type)
}
// try to init again, this time with health check on
*initTabletType = ""
*targetTabletType = "replica"
if err := agent.InitTablet(port, gRPCPort); err != nil {
t.Fatalf("InitTablet(type, healthcheck) failed: %v", err)
}
ti, err = ts.GetTablet(ctx, tabletAlias)
if err != nil {
t.Fatalf("GetTablet failed: %v", err)
}
if ti.Type != topo.TYPE_SPARE {
t.Errorf("wrong tablet type: %v", ti.Type)
}
// update shard's master to our alias, then try to init again
si, err = ts.GetShard(ctx, "test_keyspace", "-80")
if err != nil {
t.Fatalf("GetShard failed: %v", err)
}
si.MasterAlias = topo.TabletAliasToProto(tabletAlias)
if err := topo.UpdateShard(ctx, ts, si); err != nil {
t.Fatalf("UpdateShard failed: %v", err)
}
if err := agent.InitTablet(port, gRPCPort); err != nil {
t.Fatalf("InitTablet(type, healthcheck) failed: %v", err)
}
ti, err = ts.GetTablet(ctx, tabletAlias)
if err != nil {
t.Fatalf("GetTablet failed: %v", err)
}
if ti.Type != topo.TYPE_MASTER {
t.Errorf("wrong tablet type: %v", ti.Type)
}
// init again with the tablet_type set, no healthcheck
// (also check db name override and tags here)
*initTabletType = "replica"
*targetTabletType = ""
*initDbNameOverride = "DBNAME"
initTags.Set("aaa:bbb")
if err := agent.InitTablet(port, gRPCPort); err != nil {
t.Fatalf("InitTablet(type, healthcheck) failed: %v", err)
}
ti, err = ts.GetTablet(ctx, tabletAlias)
if err != nil {
t.Fatalf("GetTablet failed: %v", err)
}
if ti.Type != topo.TYPE_MASTER {
t.Errorf("wrong tablet type: %v", ti.Type)
}
if ti.DbNameOverride != "DBNAME" {
t.Errorf("wrong tablet DbNameOverride: %v", ti.DbNameOverride)
}
if len(ti.Tags) != 1 || ti.Tags["aaa"] != "bbb" {
t.Errorf("wrong tablet tags: %v", ti.Tags)
}
}
开发者ID:haoqoo,项目名称:vitess,代码行数:101,代码来源:init_tablet_test.go
示例20: masterMigrateServedFrom
// masterMigrateServedFrom handles the master migration. The ordering is
// a bit different than for rdonly / replica to guarantee a smooth transition.
//
// The order is as follows:
// - Add BlacklistedTables on the source shard map for master
// - Refresh the source ma
|
请发表评论