From 57838636dc330c95150a3093bcf52798bc0ee98e Mon Sep 17 00:00:00 2001 From: Alain Jobart Date: Mon, 10 Aug 2015 11:16:54 -0700 Subject: [PATCH] Use topo.ParseTabletType everywhere now. --- go/vt/etcdtopo/serving_graph.go | 5 ++--- go/vt/tabletmanager/healthcheck.go | 11 +++++------ go/vt/tabletmanager/init_tablet.go | 13 +++++-------- go/vt/topo/proto3.go | 7 ++++--- go/vt/vtctl/vtctl.go | 8 ++++---- go/vt/zktopo/serving_graph.go | 4 ++-- 6 files changed, 22 insertions(+), 26 deletions(-) diff --git a/go/vt/etcdtopo/serving_graph.go b/go/vt/etcdtopo/serving_graph.go index 3b85f2cfbf..46c74c7e3e 100644 --- a/go/vt/etcdtopo/serving_graph.go +++ b/go/vt/etcdtopo/serving_graph.go @@ -8,7 +8,6 @@ import ( "encoding/json" "fmt" "path" - "strings" "time" "github.com/coreos/go-etcd/etcd" @@ -43,8 +42,8 @@ func (s *Server) GetSrvTabletTypesPerShard(ctx context.Context, cellName, keyspa tabletTypes := make([]pb.TabletType, 0, len(resp.Node.Nodes)) for _, n := range resp.Node.Nodes { strType := path.Base(n.Key) - if tt, ok := pb.TabletType_value[strings.ToUpper(strType)]; ok { - tabletTypes = append(tabletTypes, pb.TabletType(tt)) + if tt, err := topo.ParseTabletType(strType); err == nil { + tabletTypes = append(tabletTypes, tt) } } return tabletTypes, nil diff --git a/go/vt/tabletmanager/healthcheck.go b/go/vt/tabletmanager/healthcheck.go index abd7e965f2..cb007daf59 100644 --- a/go/vt/tabletmanager/healthcheck.go +++ b/go/vt/tabletmanager/healthcheck.go @@ -13,7 +13,6 @@ import ( "fmt" "html/template" "reflect" - "strings" "time" log "github.com/golang/glog" @@ -104,9 +103,9 @@ func (agent *ActionAgent) initHealthCheck() { return } - tt, ok := pbt.TabletType_value[strings.ToUpper(*targetTabletType)] - if !ok { - log.Fatalf("Invalid target tablet type: %v", *targetTabletType) + tt, err := topo.ParseTabletType(*targetTabletType) + if err != nil { + log.Fatalf("Invalid target tablet type %v: %v", *targetTabletType, err) } log.Infof("Starting periodic health check every %v with target_tablet_type=%v", *healthCheckInterval, *targetTabletType) @@ -119,10 +118,10 @@ func (agent *ActionAgent) initHealthCheck() { t.Stop() // Now we can finish up and force ourselves to not healthy. - agent.terminateHealthChecks(pbt.TabletType(tt)) + agent.terminateHealthChecks(tt) }) t.Start(func() { - agent.runHealthCheck(pbt.TabletType(tt)) + agent.runHealthCheck(tt) }) t.Trigger() } diff --git a/go/vt/tabletmanager/init_tablet.go b/go/vt/tabletmanager/init_tablet.go index cc6c28678e..964c87ad07 100644 --- a/go/vt/tabletmanager/init_tablet.go +++ b/go/vt/tabletmanager/init_tablet.go @@ -53,16 +53,13 @@ func (agent *ActionAgent) InitTablet(port, gRPCPort int32) error { log.Fatalf("cannot specify both target_tablet_type and init_tablet_type parameters (as they might conflict)") } - itt, ok := pb.TabletType_value[strings.ToUpper(*initTabletType)] - if !ok { - log.Fatalf("Invalid init tablet type: %v", *initTabletType) + // use the type specified on the command line + var err error + tabletType, err = topo.ParseTabletType(*initTabletType) + if err != nil { + log.Fatalf("Invalid init tablet type %v: %v", *initTabletType, err) } - // use the type specified on the command line - tabletType = pb.TabletType(itt) - if !topo.IsTypeInList(tabletType, topo.AllTabletTypes) { - log.Fatalf("InitTablet encountered unknown init_tablet_type '%v'", *initTabletType) - } if tabletType == pb.TabletType_MASTER || tabletType == pb.TabletType_SCRAP { // We disallow TYPE_MASTER, so we don't have to change // shard.MasterAlias, and deal with the corner cases. diff --git a/go/vt/topo/proto3.go b/go/vt/topo/proto3.go index 2e2e2b9838..94c3bd9b4f 100644 --- a/go/vt/topo/proto3.go +++ b/go/vt/topo/proto3.go @@ -18,10 +18,11 @@ import ( // TabletTypeToProto turns a TabletType into a proto func TabletTypeToProto(t TabletType) pb.TabletType { - if result, ok := pb.TabletType_value[strings.ToUpper(string(t))]; ok { - return pb.TabletType(result) + if result, err := ParseTabletType(string(t)); err != nil { + panic(fmt.Errorf("unknown tablet type: %v", t)) + } else { + return result } - panic(fmt.Errorf("unknown tablet type: %v", t)) } // ProtoToTabletType turns a proto to a TabletType diff --git a/go/vt/vtctl/vtctl.go b/go/vt/vtctl/vtctl.go index 37aee17671..e01e1412a7 100644 --- a/go/vt/vtctl/vtctl.go +++ b/go/vt/vtctl/vtctl.go @@ -557,14 +557,14 @@ func tabletParamsToTabletAliases(params []string) ([]*pb.TabletAlias, error) { // parseTabletType parses the string tablet type and verifies // it is an accepted one func parseTabletType(param string, types []pb.TabletType) (pb.TabletType, error) { - tabletType, ok := pb.TabletType_value[strings.ToUpper(param)] - if !ok { - return pb.TabletType_UNKNOWN, fmt.Errorf("invalid tablet type %v", param) + tabletType, err := topo.ParseTabletType(param) + if err != nil { + return pb.TabletType_UNKNOWN, fmt.Errorf("invalid tablet type %v: %v", param, err) } if !topo.IsTypeInList(pb.TabletType(tabletType), types) { return pb.TabletType_UNKNOWN, fmt.Errorf("Type %v is not one of: %v", tabletType, strings.Join(topo.MakeStringTypeList(types), " ")) } - return pb.TabletType(tabletType), nil + return tabletType, nil } // parseKeyspaceIdType parses the keyspace id type into the enum diff --git a/go/vt/zktopo/serving_graph.go b/go/vt/zktopo/serving_graph.go index 219f2caa39..b58f44b6bf 100644 --- a/go/vt/zktopo/serving_graph.go +++ b/go/vt/zktopo/serving_graph.go @@ -63,8 +63,8 @@ func (zkts *Server) GetSrvTabletTypesPerShard(ctx context.Context, cell, keyspac if tt == "action" || tt == "actionlog" { continue } - if ptt, ok := pb.TabletType_value[strings.ToUpper(tt)]; ok { - result = append(result, pb.TabletType(ptt)) + if ptt, err := topo.ParseTabletType(tt); err == nil { + result = append(result, ptt) } } return result, nil