forked from vitessio/vitess
/
vttablet.go
135 lines (117 loc) · 3.9 KB
/
vttablet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2012, 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.
// vt tablet server: Serves queries and performs housekeeping jobs.
package main
import (
"flag"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/exit"
"github.com/youtube/vitess/go/vt/dbconfigs"
"github.com/youtube/vitess/go/vt/mysqlctl"
"github.com/youtube/vitess/go/vt/servenv"
"github.com/youtube/vitess/go/vt/tableacl"
"github.com/youtube/vitess/go/vt/tableacl/simpleacl"
"github.com/youtube/vitess/go/vt/tabletmanager"
"github.com/youtube/vitess/go/vt/tabletserver"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/topo/topoproto"
"golang.org/x/net/context"
// import mysql to register mysql connection function
_ "github.com/youtube/vitess/go/mysql"
)
var (
enforceTableACLConfig = flag.Bool("enforce-tableacl-config", false, "if this flag is true, vttablet will fail to start if a valid tableacl config does not exist")
tableAclConfig = flag.String("table-acl-config", "", "path to table access checker config file")
tabletPath = flag.String("tablet-path", "", "tablet alias")
agent *tabletmanager.ActionAgent
)
func init() {
servenv.RegisterDefaultFlags()
}
func main() {
defer exit.Recover()
flags := dbconfigs.AppConfig | dbconfigs.DbaConfig |
dbconfigs.FilteredConfig | dbconfigs.ReplConfig
dbconfigs.RegisterFlags(flags)
mysqlctl.RegisterFlags()
flag.Parse()
tabletserver.Init()
if len(flag.Args()) > 0 {
flag.Usage()
log.Errorf("vttablet doesn't take any positional arguments")
exit.Return(1)
}
servenv.Init()
if *tabletPath == "" {
log.Errorf("tabletPath required")
exit.Return(1)
}
tabletAlias, err := topoproto.ParseTabletAlias(*tabletPath)
if err != nil {
log.Error(err)
exit.Return(1)
}
mycnf, err := mysqlctl.NewMycnfFromFlags(tabletAlias.Uid)
if err != nil {
log.Errorf("mycnf read failed: %v", err)
exit.Return(1)
}
dbcfgs, err := dbconfigs.Init(mycnf.SocketFile, flags)
if err != nil {
log.Warning(err)
}
// creates and registers the query service
qsc := tabletserver.NewServer()
servenv.OnRun(func() {
qsc.Register()
addStatusParts(qsc)
})
servenv.OnClose(func() {
// We now leave the queryservice running during lameduck,
// so stop it in OnClose(), after lameduck is over.
qsc.StopService()
})
if *tableAclConfig != "" {
// To override default simpleacl, other ACL plugins must set themselves to be default ACL factory
tableacl.Register("simpleacl", &simpleacl.Factory{})
} else if *enforceTableACLConfig {
log.Error("table acl config has to be specified with table-acl-config flag because enforce-tableacl-config is set.")
exit.Return(1)
}
// tabletacl.Init loads ACL from file if *tableAclConfig is not empty
err = tableacl.Init(
*tableAclConfig,
func() {
qsc.ClearQueryPlanCache()
},
)
if err != nil {
log.Errorf("Fail to initialize Table ACL: %v", err)
if *enforceTableACLConfig {
log.Error("Need a valid initial Table ACL when enforce-tableacl-config is set, exiting.")
exit.Return(1)
}
}
// Create mysqld and register the health reporter (needs to be done
// before initializing the agent, so the initial health check
// done by the agent has the right reporter)
mysqld := mysqlctl.NewMysqld("Dba", "App", mycnf, &dbcfgs.Dba, &dbcfgs.App.ConnParams, &dbcfgs.Repl)
servenv.OnClose(mysqld.Close)
// Depends on both query and updateStream.
gRPCPort := int32(0)
if servenv.GRPCPort != nil {
gRPCPort = int32(*servenv.GRPCPort)
}
agent, err = tabletmanager.NewActionAgent(context.Background(), mysqld, qsc, tabletAlias, dbcfgs, mycnf, int32(*servenv.Port), gRPCPort)
if err != nil {
log.Error(err)
exit.Return(1)
}
servenv.OnClose(func() {
// We will still use the topo server during lameduck period
// to update our state, so closing it in OnClose()
topo.CloseServers()
})
servenv.RunDefault()
}