本文整理汇总了Golang中github.com/youtube/vitess/go/vt/topo.Server.GetTabletsByCell方法的典型用法代码示例。如果您正苦于以下问题:Golang Server.GetTabletsByCell方法的具体用法?Golang Server.GetTabletsByCell怎么用?Golang Server.GetTabletsByCell使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/youtube/vitess/go/vt/topo.Server
的用法示例。
在下文中一共展示了Server.GetTabletsByCell方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CopyTablets
// CopyTablets will create the tablets in the destination topo
func CopyTablets(fromTS, toTS topo.Server) {
cells, err := fromTS.GetKnownCells()
if err != nil {
log.Fatalf("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
log.Warningf("tablet %v already exists, updating it", tabletAlias)
err = toTS.UpdateTabletFields(ti.Alias(), func(t *topo.Tablet) error {
*t = *ti.Tablet
return nil
})
}
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() {
log.Fatalf("copyTablets failed: %v", rec.Error())
}
}
示例2: GetAllTablets
// GetAllTablets returns a sorted list of tablets.
func GetAllTablets(ctx context.Context, ts topo.Server, cell string) ([]*topo.TabletInfo, error) {
aliases, err := ts.GetTabletsByCell(ctx, cell)
if err != nil {
return nil, err
}
sort.Sort(topo.TabletAliasList(aliases))
tabletMap, err := topo.GetTabletMap(ctx, ts, aliases)
if err != nil {
// we got another error than topo.ErrNoNode
return nil, err
}
tablets := make([]*topo.TabletInfo, 0, len(aliases))
for _, tabletAlias := range aliases {
tabletInfo, ok := tabletMap[*tabletAlias]
if !ok {
// tablet disappeared on us (GetTabletMap ignores
// topo.ErrNoNode), just echo a warning
log.Warningf("failed to load tablet %v", tabletAlias)
} else {
tablets = append(tablets, tabletInfo)
}
}
return tablets, nil
}
示例3: CopyTablets
// CopyTablets will create the tablets in the destination topo
func CopyTablets(fromTS, toTS topo.Server) {
cells, err := fromTS.GetKnownCells()
if err != nil {
log.Fatalf("fromTS.GetKnownCells: %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(fmt.Errorf("GetTabletsByCell(%v): %v", cell, 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(fmt.Errorf("GetTablet(%v): %v", tabletAlias, err))
return
}
// try to create the destination
err = toTS.CreateTablet(ti.Tablet)
if err == topo.ErrNodeExists {
// update the destination tablet
log.Warningf("tablet %v already exists, updating it", tabletAlias)
err = toTS.UpdateTabletFields(ti.Alias, func(t *topo.Tablet) error {
*t = *ti.Tablet
return nil
})
}
if err != nil {
rec.RecordError(fmt.Errorf("CreateTablet(%v): %v", tabletAlias, err))
return
}
}(tabletAlias)
}
}
}(cell)
}
wg.Wait()
if rec.HasErrors() {
log.Fatalf("copyTablets failed: %v", rec.Error())
}
}
示例4: CheckTablet
// CheckTablet verifies the topo server API is correct for managing tablets.
func CheckTablet(ctx context.Context, t *testing.T, ts topo.Server) {
cell := getLocalCell(ctx, t, ts)
tablet := &topo.Tablet{
Alias: topo.TabletAlias{Cell: cell, Uid: 1},
Hostname: "localhost",
IPAddr: "10.11.12.13",
Portmap: map[string]int{
"vt": 3333,
"mysql": 3334,
},
Tags: map[string]string{"tag": "value"},
Keyspace: "test_keyspace",
Type: topo.TYPE_MASTER,
KeyRange: newKeyRange("-10"),
}
if err := ts.CreateTablet(ctx, tablet); err != nil {
t.Errorf("CreateTablet: %v", err)
}
if err := ts.CreateTablet(ctx, tablet); err != topo.ErrNodeExists {
t.Errorf("CreateTablet(again): %v", err)
}
if _, err := ts.GetTablet(ctx, topo.TabletAlias{Cell: cell, Uid: 666}); err != topo.ErrNoNode {
t.Errorf("GetTablet(666): %v", err)
}
ti, err := ts.GetTablet(ctx, tablet.Alias)
if err != nil {
t.Errorf("GetTablet %v: %v", tablet.Alias, err)
}
if eq, err := tabletEqual(ti.Tablet, tablet); err != nil {
t.Errorf("cannot compare tablets: %v", err)
} else if !eq {
t.Errorf("put and got tablets are not identical:\n%#v\n%#v", tablet, ti.Tablet)
}
if _, err := ts.GetTabletsByCell(ctx, "666"); err != topo.ErrNoNode {
t.Errorf("GetTabletsByCell(666): %v", err)
}
inCell, err := ts.GetTabletsByCell(ctx, cell)
if err != nil {
t.Errorf("GetTabletsByCell: %v", err)
}
if len(inCell) != 1 || inCell[0] != tablet.Alias {
t.Errorf("GetTabletsByCell: want [%v], got %v", tablet.Alias, inCell)
}
ti.Hostname = "remotehost"
if err := topo.UpdateTablet(ctx, ts, ti); err != nil {
t.Errorf("UpdateTablet: %v", err)
}
ti, err = ts.GetTablet(ctx, tablet.Alias)
if err != nil {
t.Errorf("GetTablet %v: %v", tablet.Alias, err)
}
if want := "remotehost"; ti.Hostname != want {
t.Errorf("ti.Hostname: want %v, got %v", want, ti.Hostname)
}
if err := topo.UpdateTabletFields(ctx, ts, tablet.Alias, func(t *topo.Tablet) error {
t.Hostname = "anotherhost"
return nil
}); err != nil {
t.Errorf("UpdateTabletFields: %v", err)
}
ti, err = ts.GetTablet(ctx, tablet.Alias)
if err != nil {
t.Errorf("GetTablet %v: %v", tablet.Alias, err)
}
if want := "anotherhost"; ti.Hostname != want {
t.Errorf("ti.Hostname: want %v, got %v", want, ti.Hostname)
}
if err := ts.DeleteTablet(ctx, tablet.Alias); err != nil {
t.Errorf("DeleteTablet: %v", err)
}
if err := ts.DeleteTablet(ctx, tablet.Alias); err != topo.ErrNoNode {
t.Errorf("DeleteTablet(again): %v", err)
}
if _, err := ts.GetTablet(ctx, tablet.Alias); err != topo.ErrNoNode {
t.Errorf("GetTablet: expected error, tablet was deleted: %v", err)
}
}
示例5: initAPI
func initAPI(ctx context.Context, ts topo.Server, actions *ActionRepository) {
tabletHealthCache := newTabletHealthCache(ts)
// Cells
handleCollection("cells", func(r *http.Request) (interface{}, error) {
if getItemPath(r.URL.Path) != "" {
return nil, errors.New("cells can only be listed, not retrieved")
}
return ts.GetKnownCells(ctx)
})
// Keyspaces
handleCollection("keyspaces", func(r *http.Request) (interface{}, error) {
keyspace := getItemPath(r.URL.Path)
// List all keyspaces.
if keyspace == "" {
return ts.GetKeyspaces(ctx)
}
// Perform an action on a keyspace.
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
return nil, err
}
action := r.FormValue("action")
if action == "" {
return nil, errors.New("must specify action")
}
return actions.ApplyKeyspaceAction(ctx, action, keyspace, r), nil
}
// Get the keyspace record.
return ts.GetKeyspace(ctx, keyspace)
})
// Shards
handleCollection("shards", func(r *http.Request) (interface{}, error) {
shardPath := getItemPath(r.URL.Path)
if !strings.Contains(shardPath, "/") {
return nil, fmt.Errorf("invalid shard path: %q", shardPath)
}
parts := strings.SplitN(shardPath, "/", 2)
keyspace := parts[0]
shard := parts[1]
// List the shards in a keyspace.
if shard == "" {
return ts.GetShardNames(ctx, keyspace)
}
// Perform an action on a shard.
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
return nil, err
}
action := r.FormValue("action")
if action == "" {
return nil, errors.New("must specify action")
}
return actions.ApplyShardAction(ctx, action, keyspace, shard, r), nil
}
// Get the shard record.
return ts.GetShard(ctx, keyspace, shard)
})
// Tablets
handleCollection("tablets", func(r *http.Request) (interface{}, error) {
tabletPath := getItemPath(r.URL.Path)
// List tablets based on query params.
if tabletPath == "" {
if err := r.ParseForm(); err != nil {
return nil, err
}
shardRef := r.FormValue("shard")
cell := r.FormValue("cell")
if shardRef != "" {
// Look up by keyspace/shard, and optionally cell.
keyspace, shard, err := topoproto.ParseKeyspaceShard(shardRef)
if err != nil {
return nil, err
}
if cell != "" {
return ts.FindAllTabletAliasesInShardByCell(ctx, keyspace, shard, []string{cell})
}
return ts.FindAllTabletAliasesInShard(ctx, keyspace, shard)
}
// Get all tablets in a cell.
if cell == "" {
return nil, errors.New("cell param required")
}
return ts.GetTabletsByCell(ctx, cell)
}
// Get tablet health.
if parts := strings.Split(tabletPath, "/"); len(parts) == 2 && parts[1] == "health" {
//.........这里部分代码省略.........
示例6: initAPI
//.........这里部分代码省略.........
// List tablets based on query params.
if tabletPath == "" {
if err := r.ParseForm(); err != nil {
return nil, err
}
shardRef := r.FormValue("shard")
cell := r.FormValue("cell")
if shardRef != "" {
// Look up by keyspace/shard, and optionally cell.
keyspace, shard, err := topoproto.ParseKeyspaceShard(shardRef)
if err != nil {
return nil, err
}
if cell != "" {
result, err := ts.FindAllTabletAliasesInShardByCell(ctx, keyspace, shard, []string{cell})
if err != nil && err != topo.ErrPartialResult {
return result, err
}
return result, nil
}
result, err := ts.FindAllTabletAliasesInShard(ctx, keyspace, shard)
if err != nil && err != topo.ErrPartialResult {
return result, err
}
return result, nil
}
// Get all tablets in a cell.
if cell == "" {
return nil, errors.New("cell param required")
}
return ts.GetTabletsByCell(ctx, cell)
}
// Get tablet health.
if parts := strings.Split(tabletPath, "/"); len(parts) == 2 && parts[1] == "health" {
tabletAlias, err := topoproto.ParseTabletAlias(parts[0])
if err != nil {
return nil, err
}
return tabletHealthCache.Get(ctx, tabletAlias)
}
tabletAlias, err := topoproto.ParseTabletAlias(tabletPath)
if err != nil {
return nil, err
}
// Perform an action on a tablet.
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
return nil, err
}
action := r.FormValue("action")
if action == "" {
return nil, errors.New("must specify action")
}
return actions.ApplyTabletAction(ctx, action, tabletAlias, r), nil
}
// Get the tablet record.
t, err := ts.GetTablet(ctx, tabletAlias)
// Pass the embedded proto directly or jsonpb will panic.
return t.Tablet, err
示例7: initAPI
func initAPI(ctx context.Context, ts topo.Server, actions *ActionRepository) {
tabletHealthCache := newTabletHealthCache(ts)
// Cells
handleCollection("cells", func(r *http.Request) (interface{}, error) {
if getItemPath(r.URL.Path) != "" {
return nil, errors.New("cells can only be listed, not retrieved")
}
return ts.GetKnownCells(ctx)
})
// Keyspaces
handleCollection("keyspaces", func(r *http.Request) (interface{}, error) {
keyspace := getItemPath(r.URL.Path)
// List all keyspaces.
if keyspace == "" {
return ts.GetKeyspaces(ctx)
}
// Perform an action on a keyspace.
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
return nil, err
}
action := r.FormValue("action")
if action == "" {
return nil, errors.New("must specify action")
}
return actions.ApplyKeyspaceAction(ctx, action, keyspace, r), nil
}
// Get the keyspace record.
return ts.GetKeyspace(ctx, keyspace)
})
// Shards
handleCollection("shards", func(r *http.Request) (interface{}, error) {
shardPath := getItemPath(r.URL.Path)
if !strings.Contains(shardPath, "/") {
return nil, fmt.Errorf("invalid shard path: %q", shardPath)
}
parts := strings.SplitN(shardPath, "/", 2)
keyspace := parts[0]
shard := parts[1]
// List the shards in a keyspace.
if shard == "" {
return ts.GetShardNames(ctx, keyspace)
}
// Perform an action on a shard.
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
return nil, err
}
action := r.FormValue("action")
if action == "" {
return nil, errors.New("must specify action")
}
return actions.ApplyShardAction(ctx, action, keyspace, shard, r), nil
}
// Get the shard record.
return ts.GetShard(ctx, keyspace, shard)
})
// Tablets
handleCollection("tablets", func(r *http.Request) (interface{}, error) {
tabletPath := getItemPath(r.URL.Path)
// List tablets based on query params.
if tabletPath == "" {
if err := r.ParseForm(); err != nil {
return nil, err
}
shardRef := r.FormValue("shard")
cell := r.FormValue("cell")
if shardRef != "" {
// Look up by keyspace/shard, and optionally cell.
keyspace, shard, err := topoproto.ParseKeyspaceShard(shardRef)
if err != nil {
return nil, err
}
if cell != "" {
return ts.FindAllTabletAliasesInShardByCell(ctx, keyspace, shard, []string{cell})
}
return ts.FindAllTabletAliasesInShard(ctx, keyspace, shard)
}
// Get all tablets in a cell.
if cell == "" {
return nil, errors.New("cell param required")
}
return ts.GetTabletsByCell(ctx, cell)
}
// Get tablet health.
if parts := strings.Split(tabletPath, "/"); len(parts) == 2 && parts[1] == "health" {
//.........这里部分代码省略.........
示例8: CheckTablet
func CheckTablet(t *testing.T, ts topo.Server) {
cell := getLocalCell(t, ts)
tablet := &topo.Tablet{
Cell: cell,
Uid: 1,
Addr: "localhost:3333",
MysqlAddr: "localhost:3334",
MysqlIpAddr: "10.11.12.13:3334",
Alias: topo.TabletAlias{Cell: cell, Uid: 1},
Hostname: "localhost",
IPAddr: "10.11.12.13",
Portmap: map[string]int{
"vt": 3333,
"mysql": 3334,
},
Tags: map[string]string{"tag": "value"},
Keyspace: "test_keyspace",
Type: topo.TYPE_MASTER,
State: topo.STATE_READ_WRITE,
KeyRange: newKeyRange("-10"),
BlacklistedTables: []string{"black1", "black2"},
}
if err := ts.CreateTablet(tablet); err != nil {
t.Errorf("CreateTablet: %v", err)
}
if err := ts.CreateTablet(tablet); err != topo.ErrNodeExists {
t.Errorf("CreateTablet(again): %v", err)
}
if _, err := ts.GetTablet(topo.TabletAlias{Cell: cell, Uid: 666}); err != topo.ErrNoNode {
t.Errorf("GetTablet(666): %v", err)
}
ti, err := ts.GetTablet(tablet.Alias)
if err != nil {
t.Errorf("GetTablet %v: %v", tablet.Alias, err)
}
if eq, err := tabletEqual(ti.Tablet, tablet); err != nil {
t.Errorf("cannot compare tablets: %v", err)
} else if !eq {
t.Errorf("put and got tablets are not identical:\n%#v\n%#v", tablet, ti.Tablet)
}
if _, err := ts.GetTabletsByCell("666"); err != topo.ErrNoNode {
t.Errorf("GetTabletsByCell(666): %v", err)
}
inCell, err := ts.GetTabletsByCell(cell)
if err != nil {
t.Errorf("GetTabletsByCell: %v", err)
}
if len(inCell) != 1 || inCell[0] != tablet.Alias {
t.Errorf("GetTabletsByCell: want [%v], got %v", tablet.Alias, inCell)
}
ti.State = topo.STATE_READ_ONLY
if err := topo.UpdateTablet(ts, ti); err != nil {
t.Errorf("UpdateTablet: %v", err)
}
ti, err = ts.GetTablet(tablet.Alias)
if err != nil {
t.Errorf("GetTablet %v: %v", tablet.Alias, err)
}
if want := topo.STATE_READ_ONLY; ti.State != want {
t.Errorf("ti.State: want %v, got %v", want, ti.State)
}
if err := ts.UpdateTabletFields(tablet.Alias, func(t *topo.Tablet) error {
t.State = topo.STATE_READ_WRITE
return nil
}); err != nil {
t.Errorf("UpdateTabletFields: %v", err)
}
ti, err = ts.GetTablet(tablet.Alias)
if err != nil {
t.Errorf("GetTablet %v: %v", tablet.Alias, err)
}
if want := topo.STATE_READ_WRITE; ti.State != want {
t.Errorf("ti.State: want %v, got %v", want, ti.State)
}
if err := ts.DeleteTablet(tablet.Alias); err != nil {
t.Errorf("DeleteTablet: %v", err)
}
if err := ts.DeleteTablet(tablet.Alias); err != topo.ErrNoNode {
t.Errorf("DeleteTablet(again): %v", err)
}
if _, err := ts.GetTablet(tablet.Alias); err != topo.ErrNoNode {
t.Errorf("GetTablet: expected error, tablet was deleted: %v", err)
}
}
示例9: initAPI
func initAPI(ctx context.Context, ts topo.Server) {
// Get Cells
handleGet("cells", func(r *http.Request) (interface{}, error) {
if getItemPath(r.URL.Path) != "" {
return nil, errors.New("cells can only be listed, not retrieved")
}
return ts.GetKnownCells(ctx)
})
// Get Keyspaces
handleGet("keyspaces", func(r *http.Request) (interface{}, error) {
keyspace := getItemPath(r.URL.Path)
if keyspace == "" {
return ts.GetKeyspaces(ctx)
}
return ts.GetKeyspace(ctx, keyspace)
})
// Get Shards
handleGet("shards", func(r *http.Request) (interface{}, error) {
shardPath := getItemPath(r.URL.Path)
if !strings.Contains(shardPath, "/") {
return nil, fmt.Errorf("invalid shard path: %q", shardPath)
}
parts := strings.SplitN(shardPath, "/", 2)
if parts[1] == "" {
// It's just a keyspace. List the shards.
return ts.GetShardNames(ctx, parts[0])
}
// It's a keyspace/shard reference.
return ts.GetShard(ctx, parts[0], parts[1])
})
// Get Tablets
handleGet("tablets", func(r *http.Request) (interface{}, error) {
tabletPath := getItemPath(r.URL.Path)
if tabletPath == "" {
// List tablets based on query params.
if err := r.ParseForm(); err != nil {
return nil, err
}
shardRef := r.FormValue("shard")
cell := r.FormValue("cell")
if shardRef != "" {
// Look up by keyspace/shard, and optionally cell.
keyspace, shard, err := topo.ParseKeyspaceShardString(shardRef)
if err != nil {
return nil, err
}
if cell != "" {
return topo.FindAllTabletAliasesInShardByCell(ctx, ts, keyspace, shard, []string{cell})
}
return topo.FindAllTabletAliasesInShard(ctx, ts, keyspace, shard)
}
// Get all tablets in a cell.
if cell == "" {
return nil, errors.New("cell param required")
}
return ts.GetTabletsByCell(ctx, cell)
}
// Get a specific tablet.
tabletAlias, err := topo.ParseTabletAliasString(tabletPath)
if err != nil {
return nil, err
}
return ts.GetTablet(ctx, tabletAlias)
})
// Get EndPoints
handleGet("endpoints", func(r *http.Request) (interface{}, error) {
// We expect cell/keyspace/shard/tabletType.
epPath := getItemPath(r.URL.Path)
parts := strings.Split(epPath, "/")
if len(parts) != 4 {
return nil, fmt.Errorf("invalid cell/keyspace/shard/tabletType: %q", epPath)
}
if parts[3] == "" {
// tabletType is empty, so list the tablet types.
return ts.GetSrvTabletTypesPerShard(ctx, parts[0], parts[1], parts[2])
}
// Get the endpoints object for a specific type.
ep, _, err := ts.GetEndPoints(ctx, parts[0], parts[1], parts[2], topo.TabletType(parts[3]))
return ep, err
})
}
示例10: initAPI
//.........这里部分代码省略.........
if err != nil {
return nil, fmt.Errorf("can't get list of SrvKeyspaceNames for cell %q: GetSrvKeyspaceNames returned: %v", cell, err)
}
for _, keyspaceName := range keyspaceNamesList {
err := addSrvkeyspace(ctx, ts, cell, keyspaceName, srvKeyspaces)
if err != nil {
return nil, err
}
}
return srvKeyspaces, nil
})
// Tablets
handleCollection("tablets", func(r *http.Request) (interface{}, error) {
tabletPath := getItemPath(r.URL.Path)
// List tablets based on query params.
if tabletPath == "" {
if err := r.ParseForm(); err != nil {
return nil, err
}
shardRef := r.FormValue("shard")
cell := r.FormValue("cell")
if shardRef != "" {
// Look up by keyspace/shard, and optionally cell.
keyspace, shard, err := topoproto.ParseKeyspaceShard(shardRef)
if err != nil {
return nil, err
}
if cell != "" {
return ts.FindAllTabletAliasesInShardByCell(ctx, keyspace, shard, []string{cell})
}
return ts.FindAllTabletAliasesInShard(ctx, keyspace, shard)
}
// Get all tablets in a cell.
if cell == "" {
return nil, errors.New("cell param required")
}
return ts.GetTabletsByCell(ctx, cell)
}
// Get tablet health.
if parts := strings.Split(tabletPath, "/"); len(parts) == 2 && parts[1] == "health" {
tabletAlias, err := topoproto.ParseTabletAlias(parts[0])
if err != nil {
return nil, err
}
return tabletHealthCache.Get(ctx, tabletAlias)
}
tabletAlias, err := topoproto.ParseTabletAlias(tabletPath)
if err != nil {
return nil, err
}
// Perform an action on a tablet.
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
return nil, err
}
action := r.FormValue("action")
if action == "" {
return nil, errors.New("must specify action")
}
return actions.ApplyTabletAction(ctx, action, tabletAlias, r), nil
}
// Get the tablet record.
return ts.GetTablet(ctx, tabletAlias)
})
// Schema Change
http.HandleFunc(apiPrefix+"schema/apply", func(w http.ResponseWriter, r *http.Request) {
req := struct {
Keyspace, SQL string
SlaveTimeoutSeconds int
}{}
if err := unmarshalRequest(r, &req); err != nil {
httpErrorf(w, r, "can't unmarshal request: %v", err)
return
}
if req.SlaveTimeoutSeconds <= 0 {
req.SlaveTimeoutSeconds = 10
}
logger := logutil.NewCallbackLogger(func(ev *logutilpb.Event) {
w.Write([]byte(logutil.EventString(ev)))
})
wr := wrangler.New(logger, ts, tmClient)
executor := schemamanager.NewTabletExecutor(
wr, time.Duration(req.SlaveTimeoutSeconds)*time.Second)
schemamanager.Run(ctx,
schemamanager.NewUIController(req.SQL, req.Keyspace, w), executor)
})
}
示例11: initAPI
func initAPI(ctx context.Context, ts topo.Server, actions *ActionRepository) {
tabletHealthCache := newTabletHealthCache(ts)
// Cells
handleCollection("cells", func(r *http.Request) (interface{}, error) {
if getItemPath(r.URL.Path) != "" {
return nil, errors.New("cells can only be listed, not retrieved")
}
return ts.GetKnownCells(ctx)
})
// Keyspaces
handleCollection("keyspaces", func(r *http.Request) (interface{}, error) {
keyspace := getItemPath(r.URL.Path)
// List all keyspaces.
if keyspace == "" {
return ts.GetKeyspaces(ctx)
}
// Perform an action on a keyspace.
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
return nil, err
}
action := r.FormValue("action")
if action == "" {
return nil, errors.New("must specify action")
}
return actions.ApplyKeyspaceAction(ctx, action, keyspace, r), nil
}
// Get the keyspace record.
return ts.GetKeyspace(ctx, keyspace)
})
// Shards
handleCollection("shards", func(r *http.Request) (interface{}, error) {
shardPath := getItemPath(r.URL.Path)
if !strings.Contains(shardPath, "/") {
return nil, fmt.Errorf("invalid shard path: %q", shardPath)
}
parts := strings.SplitN(shardPath, "/", 2)
keyspace := parts[0]
shard := parts[1]
// List the shards in a keyspace.
if shard == "" {
return ts.GetShardNames(ctx, keyspace)
}
// Perform an action on a shard.
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
return nil, err
}
action := r.FormValue("action")
if action == "" {
return nil, errors.New("must specify action")
}
return actions.ApplyShardAction(ctx, action, keyspace, shard, r), nil
}
// Get the shard record.
return ts.GetShard(ctx, keyspace, shard)
})
// Tablets
handleCollection("tablets", func(r *http.Request) (interface{}, error) {
tabletPath := getItemPath(r.URL.Path)
// List tablets based on query params.
if tabletPath == "" {
if err := r.ParseForm(); err != nil {
return nil, err
}
shardRef := r.FormValue("shard")
cell := r.FormValue("cell")
if shardRef != "" {
// Look up by keyspace/shard, and optionally cell.
keyspace, shard, err := topo.ParseKeyspaceShardString(shardRef)
if err != nil {
return nil, err
}
if cell != "" {
return topo.FindAllTabletAliasesInShardByCell(ctx, ts, keyspace, shard, []string{cell})
}
return topo.FindAllTabletAliasesInShard(ctx, ts, keyspace, shard)
}
// Get all tablets in a cell.
if cell == "" {
return nil, errors.New("cell param required")
}
return ts.GetTabletsByCell(ctx, cell)
}
// Get tablet health.
if parts := strings.Split(tabletPath, "/"); len(parts) == 2 && parts[1] == "health" {
//.........这里部分代码省略.........
示例12: CheckTablet
func CheckTablet(t *testing.T, ts topo.Server) {
cell := getLocalCell(t, ts)
tablet := &topo.Tablet{
Cell: cell,
Uid: 1,
Parent: topo.TabletAlias{},
Addr: "localhost:3333",
MysqlAddr: "localhost:3334",
MysqlIpAddr: "10.11.12.13:3334",
Keyspace: "test_keyspace",
Type: topo.TYPE_MASTER,
State: topo.STATE_READ_WRITE,
KeyRange: newKeyRange("-10"),
}
if err := ts.CreateTablet(tablet); err != nil {
t.Errorf("CreateTablet: %v", err)
}
if err := ts.CreateTablet(tablet); err != topo.ErrNodeExists {
t.Errorf("CreateTablet(again): %v", err)
}
if _, err := ts.GetTablet(topo.TabletAlias{cell, 666}); err != topo.ErrNoNode {
t.Errorf("GetTablet(666): %v", err)
}
ti, err := ts.GetTablet(tablet.Alias())
if err != nil {
t.Errorf("GetTablet %v: %v", tablet.Alias(), err)
}
if eq, err := tabletEqual(ti.Tablet, tablet); err != nil {
t.Errorf("cannot compare tablets: %v", err)
} else if !eq {
t.Errorf("put and got tablets are not identical:\n%#v\n%#v", tablet, ti.Tablet)
}
if _, err := ts.GetTabletsByCell("666"); err != topo.ErrNoNode {
t.Errorf("GetTabletsByCell(666): %v", err)
}
inCell, err := ts.GetTabletsByCell(cell)
if err != nil {
t.Errorf("GetTabletsByCell: %v", err)
}
if len(inCell) != 1 || inCell[0] != tablet.Alias() {
t.Errorf("GetTabletsByCell: want [%v], got %v", tablet.Alias(), inCell)
}
ti.State = topo.STATE_READ_ONLY
if err := topo.UpdateTablet(ts, ti); err != nil {
t.Errorf("UpdateTablet: %v", err)
}
ti, err = ts.GetTablet(tablet.Alias())
if err != nil {
t.Errorf("GetTablet %v: %v", tablet.Alias(), err)
}
if want := topo.STATE_READ_ONLY; ti.State != want {
t.Errorf("ti.State: want %v, got %v", want, ti.State)
}
if err := ts.UpdateTabletFields(tablet.Alias(), func(t *topo.Tablet) error {
t.State = topo.STATE_READ_WRITE
return nil
}); err != nil {
t.Errorf("UpdateTabletFields: %v", err)
}
ti, err = ts.GetTablet(tablet.Alias())
if err != nil {
t.Errorf("GetTablet %v: %v", tablet.Alias(), err)
}
if want := topo.STATE_READ_WRITE; ti.State != want {
t.Errorf("ti.State: want %v, got %v", want, ti.State)
}
if err := ts.DeleteTablet(tablet.Alias()); err != nil {
t.Errorf("DeleteTablet: %v", err)
}
if err := ts.DeleteTablet(tablet.Alias()); err != topo.ErrNoNode {
t.Errorf("DeleteTablet(again): %v", err)
}
if _, err := ts.GetTablet(tablet.Alias()); err != topo.ErrNoNode {
t.Errorf("GetTablet: expected error, tablet was deleted: %v", err)
}
}