本文整理汇总了Golang中github.com/youtube/vitess/go/vt/topo.Impl.DeleteKeyspaceShards方法的典型用法代码示例。如果您正苦于以下问题:Golang Impl.DeleteKeyspaceShards方法的具体用法?Golang Impl.DeleteKeyspaceShards怎么用?Golang Impl.DeleteKeyspaceShards使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/youtube/vitess/go/vt/topo.Impl
的用法示例。
在下文中一共展示了Impl.DeleteKeyspaceShards方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CheckKeyspace
// CheckKeyspace tests the keyspace part of the API
func CheckKeyspace(ctx context.Context, t *testing.T, ts topo.Impl) {
keyspaces, err := ts.GetKeyspaces(ctx)
if err != nil {
t.Errorf("GetKeyspaces(empty): %v", err)
}
if len(keyspaces) != 0 {
t.Errorf("len(GetKeyspaces()) != 0: %v", keyspaces)
}
if err := ts.CreateKeyspace(ctx, "test_keyspace", &topodatapb.Keyspace{}); err != nil {
t.Errorf("CreateKeyspace: %v", err)
}
if err := ts.CreateKeyspace(ctx, "test_keyspace", &topodatapb.Keyspace{}); err != topo.ErrNodeExists {
t.Errorf("CreateKeyspace(again) is not ErrNodeExists: %v", err)
}
// Delete and re-create.
if err := ts.DeleteKeyspace(ctx, "test_keyspace"); err != nil {
t.Errorf("DeleteKeyspace: %v", err)
}
if err := ts.CreateKeyspace(ctx, "test_keyspace", &topodatapb.Keyspace{}); err != nil {
t.Errorf("CreateKeyspace: %v", err)
}
keyspaces, err = ts.GetKeyspaces(ctx)
if err != nil {
t.Errorf("GetKeyspaces: %v", err)
}
if len(keyspaces) != 1 || keyspaces[0] != "test_keyspace" {
t.Errorf("GetKeyspaces: want %v, got %v", []string{"test_keyspace"}, keyspaces)
}
k := &topodatapb.Keyspace{
ShardingColumnName: "user_id",
ShardingColumnType: topodatapb.KeyspaceIdType_UINT64,
ServedFroms: []*topodatapb.Keyspace_ServedFrom{
{
TabletType: topodatapb.TabletType_REPLICA,
Cells: []string{"c1", "c2"},
Keyspace: "test_keyspace3",
},
{
TabletType: topodatapb.TabletType_MASTER,
Cells: nil,
Keyspace: "test_keyspace3",
},
},
SplitShardCount: 64,
}
if err := ts.CreateKeyspace(ctx, "test_keyspace2", k); err != nil {
t.Errorf("CreateKeyspace: %v", err)
}
keyspaces, err = ts.GetKeyspaces(ctx)
if err != nil {
t.Errorf("GetKeyspaces: %v", err)
}
if len(keyspaces) != 2 ||
keyspaces[0] != "test_keyspace" ||
keyspaces[1] != "test_keyspace2" {
t.Errorf("GetKeyspaces: want %v, got %v", []string{"test_keyspace", "test_keyspace2"}, keyspaces)
}
// Call delete shards and make sure the keyspace still exists.
if err := ts.DeleteKeyspaceShards(ctx, "test_keyspace2"); err != nil {
t.Errorf("DeleteKeyspaceShards: %v", err)
}
storedK, storedVersion, err := ts.GetKeyspace(ctx, "test_keyspace2")
if err != nil {
t.Fatalf("GetKeyspace: %v", err)
}
if !reflect.DeepEqual(storedK, k) {
t.Fatalf("returned keyspace doesn't match: got %v expected %v", storedK, k)
}
storedK.ShardingColumnName = "other_id"
var newServedFroms []*topodatapb.Keyspace_ServedFrom
for _, ksf := range storedK.ServedFroms {
if ksf.TabletType == topodatapb.TabletType_MASTER {
continue
}
if ksf.TabletType == topodatapb.TabletType_REPLICA {
ksf.Keyspace = "test_keyspace4"
}
newServedFroms = append(newServedFroms, ksf)
}
storedK.ServedFroms = newServedFroms
_, err = ts.UpdateKeyspace(ctx, "test_keyspace2", storedK, storedVersion)
if err != nil {
t.Fatalf("UpdateKeyspace: %v", err)
}
// unconditional update
storedK.ShardingColumnType = topodatapb.KeyspaceIdType_BYTES
_, err = ts.UpdateKeyspace(ctx, "test_keyspace2", storedK, -1)
if err != nil {
t.Fatalf("UpdateKeyspace(-1): %v", err)
}
storedK, storedVersion, err = ts.GetKeyspace(ctx, "test_keyspace2")
//.........这里部分代码省略.........
示例2: checkShard
// checkShard verifies the Shard operations work correctly
func checkShard(t *testing.T, ts topo.Impl) {
ctx := context.Background()
tts := topo.Server{Impl: ts}
if err := ts.CreateKeyspace(ctx, "test_keyspace", &topodatapb.Keyspace{}); err != nil {
t.Fatalf("CreateKeyspace: %v", err)
}
shard := &topodatapb.Shard{
KeyRange: newKeyRange("b0-c0"),
}
if err := ts.CreateShard(ctx, "test_keyspace", "b0-c0", shard); err != nil {
t.Fatalf("CreateShard: %v", err)
}
if err := ts.CreateShard(ctx, "test_keyspace", "b0-c0", shard); err != topo.ErrNodeExists {
t.Errorf("CreateShard called second time, got: %v", err)
}
// Delete shard and see if we can re-create it.
if err := ts.DeleteShard(ctx, "test_keyspace", "b0-c0"); err != nil {
t.Fatalf("DeleteShard: %v", err)
}
if err := ts.CreateShard(ctx, "test_keyspace", "b0-c0", shard); err != nil {
t.Fatalf("CreateShard: %v", err)
}
// Delete ALL shards.
if err := ts.DeleteKeyspaceShards(ctx, "test_keyspace"); err != nil {
t.Fatalf("DeleteKeyspaceShards: %v", err)
}
if err := ts.CreateShard(ctx, "test_keyspace", "b0-c0", shard); err != nil {
t.Fatalf("CreateShard: %v", err)
}
if _, _, err := ts.GetShard(ctx, "test_keyspace", "666"); err != topo.ErrNoNode {
t.Errorf("GetShard(666): %v", err)
}
shard, version, err := ts.GetShard(ctx, "test_keyspace", "b0-c0")
if err != nil {
t.Errorf("GetShard: %v", err)
}
if want := newKeyRange("b0-c0"); !key.KeyRangeEqual(shard.KeyRange, want) {
t.Errorf("shard.KeyRange: want %v, got %v", want, shard.KeyRange)
}
master := &topodatapb.TabletAlias{Cell: "ny", Uid: 1}
shard.MasterAlias = master
shard.KeyRange = newKeyRange("b0-c0")
shard.ServedTypes = []*topodatapb.Shard_ServedType{
{
TabletType: topodatapb.TabletType_MASTER,
},
{
TabletType: topodatapb.TabletType_REPLICA,
Cells: []string{"c1"},
},
{
TabletType: topodatapb.TabletType_RDONLY,
},
}
shard.SourceShards = []*topodatapb.Shard_SourceShard{
{
Uid: 1,
Keyspace: "source_ks",
Shard: "b8-c0",
KeyRange: newKeyRange("b8-c0"),
Tables: []string{"table1", "table2"},
},
}
shard.TabletControls = []*topodatapb.Shard_TabletControl{
{
TabletType: topodatapb.TabletType_MASTER,
Cells: []string{"c1", "c2"},
BlacklistedTables: []string{"black1", "black2"},
},
{
TabletType: topodatapb.TabletType_REPLICA,
DisableQueryService: true,
},
}
if _, err := ts.UpdateShard(ctx, "test_keyspace", "b0-c0", shard, version); err != nil {
t.Errorf("UpdateShard: %v", err)
}
other := &topodatapb.TabletAlias{Cell: "ny", Uid: 82873}
_, err = tts.UpdateShardFields(ctx, "test_keyspace", "b0-c0", func(si *topo.ShardInfo) error {
si.MasterAlias = other
return nil
})
if err != nil {
t.Fatalf("UpdateShardFields error: %v", err)
}
s, _, err := ts.GetShard(ctx, "test_keyspace", "b0-c0")
if err != nil {
t.Fatalf("GetShard: %v", err)
}
if *s.MasterAlias != *other {
t.Fatalf("shard.MasterAlias = %v, want %v", s.MasterAlias, other)
//.........这里部分代码省略.........
示例3: CopyShards
// CopyShards will create the shards in the destination topo
func CopyShards(ctx context.Context, fromTS, toTS topo.Impl, deleteKeyspaceShards bool) {
keyspaces, err := fromTS.GetKeyspaces(ctx)
if err != nil {
log.Fatalf("fromTS.GetKeyspaces: %v", err)
}
wg := sync.WaitGroup{}
rec := concurrency.AllErrorRecorder{}
for _, keyspace := range keyspaces {
wg.Add(1)
go func(keyspace string) {
defer wg.Done()
shards, err := fromTS.GetShardNames(ctx, keyspace)
if err != nil {
rec.RecordError(fmt.Errorf("GetShardNames(%v): %v", keyspace, err))
return
}
if deleteKeyspaceShards {
if err := toTS.DeleteKeyspaceShards(ctx, keyspace); err != nil {
rec.RecordError(fmt.Errorf("DeleteKeyspaceShards(%v): %v", keyspace, err))
return
}
}
for _, shard := range shards {
wg.Add(1)
go func(keyspace, shard string) {
defer wg.Done()
if err := toTS.CreateShard(ctx, keyspace, shard, &topodatapb.Shard{}); err != nil {
if err == topo.ErrNodeExists {
log.Warningf("shard %v/%v already exists", keyspace, shard)
} else {
rec.RecordError(fmt.Errorf("CreateShard(%v, %v): %v", keyspace, shard, err))
return
}
}
s, _, err := fromTS.GetShard(ctx, keyspace, shard)
if err != nil {
rec.RecordError(fmt.Errorf("GetShard(%v, %v): %v", keyspace, shard, err))
return
}
_, toV, err := toTS.GetShard(ctx, keyspace, shard)
if err != nil {
rec.RecordError(fmt.Errorf("toTS.GetShard(%v, %v): %v", keyspace, shard, err))
return
}
if _, err := toTS.UpdateShard(ctx, keyspace, shard, s, toV); err != nil {
rec.RecordError(fmt.Errorf("UpdateShard(%v, %v): %v", keyspace, shard, err))
}
}(keyspace, shard)
}
}(keyspace)
}
wg.Wait()
if rec.HasErrors() {
log.Fatalf("copyShards failed: %v", rec.Error())
}
}