forked from vitessio/vitess
/
agent.go
162 lines (142 loc) · 4.98 KB
/
agent.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// 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.
// Package vttablet contains the meat of the vttablet binary.
package vttablet
// This file handles the agent initialization.
import (
"encoding/json"
"fmt"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/jscfg"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/vt/dbconfigs"
"github.com/youtube/vitess/go/vt/mysqlctl"
"github.com/youtube/vitess/go/vt/sqlparser"
tm "github.com/youtube/vitess/go/vt/tabletmanager"
ts "github.com/youtube/vitess/go/vt/tabletserver"
"github.com/youtube/vitess/go/vt/topo"
)
var (
agent *tm.ActionAgent
binlogServer *mysqlctl.BinlogServer
binlogPlayerMap *BinlogPlayerMap
)
func loadSchemaOverrides(overridesFile string) []ts.SchemaOverride {
var schemaOverrides []ts.SchemaOverride
if err := jscfg.ReadJson(overridesFile, &schemaOverrides); err != nil {
log.Warningf("can't read overridesFile %v: %v", overridesFile, err)
} else {
data, _ := json.MarshalIndent(schemaOverrides, "", " ")
log.Infof("schemaOverrides: %s\n", data)
}
return schemaOverrides
}
// InitAgent initializes the agent within vttablet.
func InitAgent(
tabletAlias topo.TabletAlias,
dbcfgs dbconfigs.DBConfigs,
mycnf *mysqlctl.Mycnf,
dbConfigsFile, dbCredentialsFile string,
port, securePort int,
mycnfFile, overridesFile string) (err error) {
schemaOverrides := loadSchemaOverrides(overridesFile)
topoServer := topo.GetServer()
mysqld := mysqlctl.NewMysqld(mycnf, dbcfgs.Dba, dbcfgs.Repl)
// Start the binlog server service, disabled at start.
binlogServer = mysqlctl.NewBinlogServer(mysqld)
mysqlctl.RegisterBinlogServerService(binlogServer)
// Start the binlog player services, not playing at start.
binlogPlayerMap = NewBinlogPlayerMap(topoServer, dbcfgs.App.MysqlParams(), mysqld)
RegisterBinlogPlayerMap(binlogPlayerMap)
// Compute the bind addresses
bindAddr := fmt.Sprintf(":%v", port)
secureAddr := ""
if securePort != 0 {
secureAddr = fmt.Sprintf(":%v", securePort)
}
statsType := stats.NewString("TabletType")
statsKeyspace := stats.NewString("TabletKeyspace")
statsShard := stats.NewString("TabletShard")
statsKeyRangeStart := stats.NewString("TabletKeyRangeStart")
statsKeyRangeEnd := stats.NewString("TabletKeyRangeEnd")
// Action agent listens to changes in zookeeper and makes
// modifications to this tablet.
agent, err = tm.NewActionAgent(topoServer, tabletAlias, mycnfFile, dbConfigsFile, dbCredentialsFile)
if err != nil {
return err
}
agent.AddChangeCallback(func(oldTablet, newTablet topo.Tablet) {
if newTablet.IsServingType() {
if dbcfgs.App.DbName == "" {
dbcfgs.App.DbName = newTablet.DbName()
}
dbcfgs.App.Keyspace = newTablet.Keyspace
dbcfgs.App.Shard = newTablet.Shard
// Transitioning from replica to master, first disconnect
// existing connections. "false" indicateds that clients must
// re-resolve their endpoint before reconnecting.
if newTablet.Type == topo.TYPE_MASTER && oldTablet.Type != topo.TYPE_MASTER {
ts.DisallowQueries()
}
qrs := ts.LoadCustomRules()
if newTablet.KeyRange.IsPartial() {
qr := ts.NewQueryRule("enforce keyspace_id range", "keyspace_id_not_in_range", ts.QR_FAIL_QUERY)
qr.AddPlanCond(sqlparser.PLAN_INSERT_PK)
err = qr.AddBindVarCond("keyspace_id", true, true, ts.QR_NOTIN, newTablet.KeyRange)
if err != nil {
log.Warningf("Unable to add keyspace rule: %v", err)
} else {
qrs.Add(qr)
}
}
ts.AllowQueries(dbcfgs.App, schemaOverrides, qrs)
mysqlctl.EnableUpdateStreamService(string(newTablet.Type), dbcfgs)
if newTablet.Type != topo.TYPE_MASTER {
ts.StartRowCacheInvalidation()
}
} else {
ts.DisallowQueries()
ts.StopRowCacheInvalidation()
mysqlctl.DisableUpdateStreamService()
}
statsType.Set(string(newTablet.Type))
statsKeyspace.Set(newTablet.Keyspace)
statsShard.Set(newTablet.Shard)
statsKeyRangeStart.Set(string(newTablet.KeyRange.Start.Hex()))
statsKeyRangeEnd.Set(string(newTablet.KeyRange.End.Hex()))
// BinlogServer is only enabled for replicas
if newTablet.Type == topo.TYPE_REPLICA {
if !mysqlctl.IsBinlogServerEnabled(binlogServer) {
mysqlctl.EnableBinlogServerService(binlogServer, dbcfgs.App.DbName)
}
} else {
if mysqlctl.IsBinlogServerEnabled(binlogServer) {
mysqlctl.DisableBinlogServerService(binlogServer)
}
}
// See if we need to start or stop any binlog player
if newTablet.Type == topo.TYPE_MASTER {
binlogPlayerMap.RefreshMap(newTablet)
} else {
binlogPlayerMap.StopAllPlayers()
}
})
if err := agent.Start(bindAddr, secureAddr, mysqld.Addr()); err != nil {
return err
}
// register the RPC services from the agent
agent.RegisterQueryService(mysqld)
return nil
}
func CloseAgent() {
if agent != nil {
agent.Stop()
}
if binlogServer != nil {
mysqlctl.DisableBinlogServerService(binlogServer)
}
if binlogPlayerMap != nil {
binlogPlayerMap.StopAllPlayers()
}
}