Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Topo splitter #4

Merged
merged 6 commits into from
Jul 20, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ unit_test:
cd go/bytes2; go test
cd go/cache; go test
cd go/cgzip; go test
cd go/cmd/topo2topo; go test
cd go/cmd/zkns2pdns; go test
cd go/hack; go test
# cd go/logfile; go test
Expand All @@ -49,6 +48,7 @@ unit_test:
cd go/vt/mysqlctl; go test
cd go/vt/sqlparser; go test
cd go/vt/tabletserver; go test
cd go/vt/topotools; go test
cd go/zk; go test
cd go/zk/fakezk; go test
cd go/zk/zkctl; go test
Expand Down
143 changes: 4 additions & 139 deletions go/cmd/topo2topo/topo2topo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ package main
import (
"flag"
"os"
"sync"

"github.com/youtube/vitess/go/relog"
"github.com/youtube/vitess/go/tb"
"github.com/youtube/vitess/go/vt/concurrency"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/topotools"
)

var fromTopo = flag.String("from", "", "topology to copy data from")
Expand All @@ -26,140 +25,6 @@ var deleteKeyspaceShards = flag.Bool("delete-keyspace-shards", false, "when copy

var logLevel = flag.String("log.level", "INFO", "set log level")

// copyKeyspaces will create the keyspaces in the destination topo
func copyKeyspaces(fromTS, toTS topo.Server) {
keyspaces, err := fromTS.GetKeyspaces()
if err != nil {
relog.Fatal("fromTS.GetKeyspaces failed: %v", err)
}

wg := sync.WaitGroup{}
er := concurrency.AllErrorRecorder{}
for _, keyspace := range keyspaces {
wg.Add(1)
go func(keyspace string) {
defer wg.Done()
if err := toTS.CreateKeyspace(keyspace); err != nil {
if err == topo.ErrNodeExists {
relog.Warning("Keyspace %v already exists", keyspace)
} else {
er.RecordError(err)
}
}
}(keyspace)
}
wg.Wait()
if er.HasErrors() {
relog.Fatal("copyKeyspaces failed: %v", err)
}
}

// copyShards will create the keyspaces in the destination topo
func copyShards(fromTS, toTS topo.Server, deleteKeyspaceShards bool) {
keyspaces, err := fromTS.GetKeyspaces()
if err != nil {
relog.Fatal("fromTS.GetKeyspaces failed: %v", err)
}

wg := sync.WaitGroup{}
er := concurrency.AllErrorRecorder{}
for _, keyspace := range keyspaces {
wg.Add(1)
go func(keyspace string) {
defer wg.Done()
shards, err := fromTS.GetShardNames(keyspace)
if err != nil {
er.RecordError(err)
return
}

if deleteKeyspaceShards {
if err := toTS.DeleteKeyspaceShards(keyspace); err != nil {
er.RecordError(err)
return
}
}

for _, shard := range shards {
wg.Add(1)
go func(keyspace, shard string) {
defer wg.Done()
if err := toTS.CreateShard(keyspace, shard); err != nil {
if err == topo.ErrNodeExists {
relog.Warning("Shard %v/%v already exists", keyspace, shard)
} else {
er.RecordError(err)
}
}
}(keyspace, shard)
}
}(keyspace)
}
wg.Wait()
if er.HasErrors() {
relog.Fatal("copyShards failed: %v", err)
}
}

// copyTablets will create the tablets in the destination topo
func copyTablets(fromTS, toTS topo.Server) {
cells, err := fromTS.GetKnownCells()
if err != nil {
relog.Fatal("fromTS.GetKnownCells failed: %v", err)
}

wg := sync.WaitGroup{}
er := concurrency.AllErrorRecorder{}
for _, cell := range cells {
wg.Add(1)
go func(cell string) {
defer wg.Done()
tabletAliases, err := fromTS.GetTabletsByCell(cell)
if err != nil {
er.RecordError(err)
} else {
for _, tabletAlias := range tabletAliases {
wg.Add(1)
go func(tabletAlias topo.TabletAlias) {
defer wg.Done()

// read the source tablet
ti, err := fromTS.GetTablet(tabletAlias)
if err != nil {
er.RecordError(err)
return
}

// try to create the destination
err = toTS.CreateTablet(ti.Tablet)
if err == topo.ErrNodeExists {
// update the destination tablet
_, err = toTS.UpdateTablet(ti, -1)

}
if err != nil {
er.RecordError(err)
return
}

// create the replication paths
// for masters only here
if ti.Type == topo.TYPE_MASTER {
if err = toTS.CreateReplicationPath(ti.Keyspace, ti.Shard, ti.Alias().String()); err != nil && err != topo.ErrNodeExists {
er.RecordError(err)
}
}
}(tabletAlias)
}
}
}(cell)
}
wg.Wait()
if er.HasErrors() {
relog.Fatal("copyTablets failed: %v", er.Error())
}
}

func main() {
defer func() {
if panicErr := recover(); panicErr != nil {
Expand Down Expand Up @@ -188,12 +53,12 @@ func main() {
toTS := topo.GetServerByName(*toTopo)

if *doKeyspaces {
copyKeyspaces(fromTS, toTS)
topotools.CopyKeyspaces(fromTS, toTS)
}
if *doShards {
copyShards(fromTS, toTS, *deleteKeyspaceShards)
topotools.CopyShards(fromTS, toTS, *deleteKeyspaceShards)
}
if *doTablets {
copyTablets(fromTS, toTS)
topotools.CopyTablets(fromTS, toTS)
}
}
File renamed without changes.
149 changes: 149 additions & 0 deletions go/vt/topotools/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright 2013, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write a one line explanation of what topotools does.

// topotools package contains a few utility classes to handle topo.Server
// objects, and transitions.
package topotools

import (
"sync"

"github.com/youtube/vitess/go/relog"
"github.com/youtube/vitess/go/vt/concurrency"
"github.com/youtube/vitess/go/vt/topo"
)

// CopyKeyspaces will create the keyspaces in the destination topo
func CopyKeyspaces(fromTS, toTS topo.Server) {
keyspaces, err := fromTS.GetKeyspaces()
if err != nil {
relog.Fatal("fromTS.GetKeyspaces failed: %v", err)
}

wg := sync.WaitGroup{}
rec := concurrency.AllErrorRecorder{}
for _, keyspace := range keyspaces {
wg.Add(1)
go func(keyspace string) {
defer wg.Done()
if err := toTS.CreateKeyspace(keyspace); err != nil {
if err == topo.ErrNodeExists {
relog.Warning("keyspace %v already exists", keyspace)
} else {
rec.RecordError(err)
}
}
}(keyspace)
}
wg.Wait()
if rec.HasErrors() {
relog.Fatal("copyKeyspaces failed: %v", rec.Error())
}
}

// CopyShards will create the keyspaces in the destination topo
func CopyShards(fromTS, toTS topo.Server, deleteKeyspaceShards bool) {
keyspaces, err := fromTS.GetKeyspaces()
if err != nil {
relog.Fatal("fromTS.GetKeyspaces failed: %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(keyspace)
if err != nil {
rec.RecordError(err)
return
}

if deleteKeyspaceShards {
if err := toTS.DeleteKeyspaceShards(keyspace); err != nil {
rec.RecordError(err)
return
}
}

for _, shard := range shards {
wg.Add(1)
go func(keyspace, shard string) {
defer wg.Done()
if err := toTS.CreateShard(keyspace, shard); err != nil {
if err == topo.ErrNodeExists {
relog.Warning("shard %v/%v already exists", keyspace, shard)
} else {
rec.RecordError(err)
}
}
}(keyspace, shard)
}
}(keyspace)
}
wg.Wait()
if rec.HasErrors() {
relog.Fatal("copyShards failed: %v", rec.Error())
}
}

// CopyTablets will create the tablets in the destination topo
func CopyTablets(fromTS, toTS topo.Server) {
cells, err := fromTS.GetKnownCells()
if err != nil {
relog.Fatal("fromTS.GetKnownCells failed: %v", err)
}

wg := sync.WaitGroup{}
rec := concurrency.AllErrorRecorder{}
for _, cell := range cells {
wg.Add(1)
go func(cell string) {
defer wg.Done()
tabletAliases, err := fromTS.GetTabletsByCell(cell)
if err != nil {
rec.RecordError(err)
} else {
for _, tabletAlias := range tabletAliases {
wg.Add(1)
go func(tabletAlias topo.TabletAlias) {
defer wg.Done()

// read the source tablet
ti, err := fromTS.GetTablet(tabletAlias)
if err != nil {
rec.RecordError(err)
return
}

// try to create the destination
err = toTS.CreateTablet(ti.Tablet)
if err == topo.ErrNodeExists {
// update the destination tablet
_, err = toTS.UpdateTablet(ti, -1)

}
if err != nil {
rec.RecordError(err)
return
}

// create the replication paths
// for masters only here
if ti.Type == topo.TYPE_MASTER {
if err = toTS.CreateReplicationPath(ti.Keyspace, ti.Shard, ti.Alias().String()); err != nil && err != topo.ErrNodeExists {
rec.RecordError(err)
}
}
}(tabletAlias)
}
}
}(cell)
}
wg.Wait()
if rec.HasErrors() {
relog.Fatal("copyTablets failed: %v", rec.Error())
}
}
Loading