forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zkns.go
220 lines (197 loc) · 6.39 KB
/
zkns.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package wrangler
import (
"fmt"
"path"
"sort"
"strings"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/jscfg"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/zktopo"
"github.com/youtube/vitess/go/zk"
"github.com/youtube/vitess/go/zk/zkns"
"launchpad.net/gozk/zookeeper"
)
// Export addresses from the VT serving graph to a legacy zkns server.
// Note these functions only work with a zktopo.
func (wr *Wrangler) ExportZkns(cell string) error {
zkTopo, ok := wr.ts.(*zktopo.Server)
if !ok {
return fmt.Errorf("ExportZkns only works with zktopo")
}
zconn := zkTopo.GetZConn()
vtNsPath := fmt.Sprintf("/zk/%v/vt/ns", cell)
zknsRootPath := fmt.Sprintf("/zk/%v/zkns/vt", cell)
children, err := zk.ChildrenRecursive(zconn, vtNsPath)
if err != nil {
return err
}
for _, child := range children {
addrPath := path.Join(vtNsPath, child)
zknsAddrPath := path.Join(zknsRootPath, child)
_, stat, err := zconn.Get(addrPath)
if err != nil {
return err
}
// Leaf nodes correspond to zkns vdns files in the old setup.
if stat.NumChildren() > 0 {
continue
}
if _, err = wr.exportVtnsToZkns(zconn, addrPath, zknsAddrPath); err != nil {
return err
}
}
return nil
}
// Export addresses from the VT serving graph to a legacy zkns server.
func (wr *Wrangler) ExportZknsForKeyspace(keyspace string) error {
zkTopo, ok := wr.ts.(*zktopo.Server)
if !ok {
return fmt.Errorf("ExportZknsForKeyspace only works with zktopo")
}
zconn := zkTopo.GetZConn()
shardNames, err := wr.ts.GetShardNames(keyspace)
if err != nil {
return err
}
// Scan the first shard to discover which cells need local serving data.
aliases, err := topo.FindAllTabletAliasesInShard(wr.ts, keyspace, shardNames[0])
if err != nil {
return err
}
cellMap := make(map[string]bool)
for _, alias := range aliases {
cellMap[alias.Cell] = true
}
for cell := range cellMap {
vtnsRootPath := fmt.Sprintf("/zk/%v/vt/ns/%v", cell, keyspace)
zknsRootPath := fmt.Sprintf("/zk/%v/zkns/vt/%v", cell, keyspace)
// Get the existing list of zkns children. If they don't get rewritten,
// delete them as stale entries.
zknsChildren, err := zk.ChildrenRecursive(zconn, zknsRootPath)
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
zknsChildren = make([]string, 0)
} else {
return err
}
}
staleZknsPaths := make(map[string]bool)
for _, child := range zknsChildren {
staleZknsPaths[path.Join(zknsRootPath, child)] = true
}
vtnsChildren, err := zk.ChildrenRecursive(zconn, vtnsRootPath)
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
vtnsChildren = make([]string, 0)
} else {
return err
}
}
for _, child := range vtnsChildren {
vtnsAddrPath := path.Join(vtnsRootPath, child)
zknsAddrPath := path.Join(zknsRootPath, child)
_, stat, err := zconn.Get(vtnsAddrPath)
if err != nil {
return err
}
// Leaf nodes correspond to zkns vdns files in the old setup.
if stat.NumChildren() > 0 {
continue
}
zknsPathsWritten, err := wr.exportVtnsToZkns(zconn, vtnsAddrPath, zknsAddrPath)
if err != nil {
return err
}
log.V(6).Infof("zknsPathsWritten: %v", zknsPathsWritten)
for _, zkPath := range zknsPathsWritten {
delete(staleZknsPaths, zkPath)
}
}
log.V(6).Infof("staleZknsPaths: %v", staleZknsPaths)
prunePaths := make([]string, 0, len(staleZknsPaths))
for prunePath := range staleZknsPaths {
prunePaths = append(prunePaths, prunePath)
}
sort.Strings(prunePaths)
// Prune paths in reverse order so we remove children first
for i := len(prunePaths) - 1; i >= 0; i-- {
log.Infof("prune stale zkns path %v", prunePaths[i])
if err := zconn.Delete(prunePaths[i], -1); err != nil && !zookeeper.IsError(err, zookeeper.ZNOTEMPTY) {
return err
}
}
}
return nil
}
func (wr *Wrangler) exportVtnsToZkns(zconn zk.Conn, vtnsAddrPath, zknsAddrPath string) ([]string, error) {
zknsPaths := make([]string, 0, 32)
parts := strings.Split(vtnsAddrPath, "/")
if len(parts) != 8 {
return nil, fmt.Errorf("Invalid leaf zk path: %v", vtnsAddrPath)
}
cell := parts[2]
keyspace := parts[5]
shard := parts[6]
tabletType := topo.TabletType(parts[7])
addrs, err := wr.ts.GetEndPoints(cell, keyspace, shard, tabletType)
if err != nil {
return nil, err
}
// Write the individual endpoints and compute the SRV entries.
vtoccAddrs := LegacyZknsAddrs{make([]string, 0, 8)}
defaultAddrs := LegacyZknsAddrs{make([]string, 0, 8)}
for i, entry := range addrs.Entries {
zknsAddrPath := fmt.Sprintf("%v/%v", zknsAddrPath, i)
zknsPaths = append(zknsPaths, zknsAddrPath)
zknsAddr := zkns.ZknsAddr{Host: entry.Host, Port: entry.NamedPortMap["_mysql"], NamedPortMap: entry.NamedPortMap}
err := WriteAddr(zconn, zknsAddrPath, &zknsAddr)
if err != nil {
return nil, err
}
defaultAddrs.Endpoints = append(defaultAddrs.Endpoints, zknsAddrPath)
vtoccAddrs.Endpoints = append(vtoccAddrs.Endpoints, zknsAddrPath+":_vtocc")
}
// Prune any zkns entries that are no longer referenced by the
// shard graph.
deleteIdx := len(addrs.Entries)
for {
zknsStaleAddrPath := fmt.Sprintf("%v/%v", zknsAddrPath, deleteIdx)
// A "delete" is a write of sorts - just communicate up that nothing
// needs to be done to this node.
zknsPaths = append(zknsPaths, zknsStaleAddrPath)
err := zconn.Delete(zknsStaleAddrPath, -1)
if zookeeper.IsError(err, zookeeper.ZNONODE) {
break
}
if err != nil {
return nil, err
}
deleteIdx++
}
// Write the VDNS entries for both vtocc and mysql
vtoccVdnsPath := fmt.Sprintf("%v/_vtocc.vdns", zknsAddrPath)
zknsPaths = append(zknsPaths, vtoccVdnsPath)
if err = WriteAddrs(zconn, vtoccVdnsPath, &vtoccAddrs); err != nil {
return nil, err
}
defaultVdnsPath := fmt.Sprintf("%v.vdns", zknsAddrPath)
zknsPaths = append(zknsPaths, defaultVdnsPath)
if err = WriteAddrs(zconn, defaultVdnsPath, &defaultAddrs); err != nil {
return nil, err
}
return zknsPaths, nil
}
type LegacyZknsAddrs struct {
Endpoints []string `json:"endpoints"`
}
func WriteAddr(zconn zk.Conn, zkPath string, addr *zkns.ZknsAddr) error {
data := jscfg.ToJson(addr)
_, err := zk.CreateOrUpdate(zconn, zkPath, data, 0, zookeeper.WorldACL(zookeeper.PERM_ALL), true)
return err
}
func WriteAddrs(zconn zk.Conn, zkPath string, addrs *LegacyZknsAddrs) error {
data := jscfg.ToJson(addrs)
_, err := zk.CreateOrUpdate(zconn, zkPath, data, 0, zookeeper.WorldACL(zookeeper.PERM_ALL), true)
return err
}