forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
197 lines (175 loc) · 5.08 KB
/
config.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
// 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 zk
import (
"encoding/json"
"flag"
"fmt"
"os"
"sort"
"strings"
"time"
"unicode"
)
const (
DEFAULT_BASE_TIMEOUT = 5 * time.Second
)
var (
// DefaultZkConfigPaths is the default list of config files to check.
DefaultZkConfigPaths = []string{"/etc/zookeeper/zk_client.json"}
// MagicPrefix is the Default name for the root note in the zookeeper tree.
MagicPrefix = "zk"
localCell = flag.String("zk.local-cell", "", "closest zk cell used for /zk/local paths")
localAddrs = flag.String("zk.local-addrs", "", "list of zookeeper servers (host:port, ...)")
globalAddrs = flag.String("zk.global-addrs", "", "list of global zookeeper servers (host:port, ...)")
baseTimeout = flag.Duration("zk.base-timeout", DEFAULT_BASE_TIMEOUT, "zk base timeout (see zkconn.go)")
connectTimeout = flag.Duration("zk.connect-timeout", 30*time.Second, "zk connect timeout")
)
// Read the cell from -zk.local-cell, or the environment
// ZK_CLIENT_LOCAL_CELL or guess the cell by the hostname. The
// letter-only prefix of the string is used as the cell name. For
// instance:
//
// pa1 -> pa
// sjl-1 -> sjl
// lwc1 -> lwc
func GuessLocalCell() string {
if *localCell != "" {
return *localCell
}
envCell := os.Getenv("ZK_CLIENT_LOCAL_CELL")
if envCell != "" {
return envCell
}
hostname, err := os.Hostname()
if err != nil {
panic(err) // should never happen
}
shortHostname := strings.Split(hostname, ".")[0]
return letterPrefix(shortHostname)
}
// Return the string prefix up to the first non-letter.
func letterPrefix(str string) string {
for i, rune := range str {
if !unicode.IsLetter(rune) {
return str[:i]
}
}
return str
}
// ZkCellFromZkPath extracts the cell name from a zkPath.
func ZkCellFromZkPath(zkPath string) (string, error) {
pathParts := strings.Split(zkPath, "/")
if len(pathParts) < 3 {
return "", fmt.Errorf("no cell name in path: %v", zkPath)
}
if pathParts[0] != "" || pathParts[1] != MagicPrefix {
return "", fmt.Errorf("path should start with /%v: %v", MagicPrefix, zkPath)
}
cell := pathParts[2]
if cell == "" || strings.Contains(cell, "-") {
return "", fmt.Errorf("invalid cell name %q", cell)
}
return cell, nil
}
func getConfigPaths() []string {
zkConfigPath := os.Getenv("ZK_CLIENT_CONFIG")
if zkConfigPath != "" {
return []string{zkConfigPath}
}
return DefaultZkConfigPaths
}
func getCellAddrMap() (map[string]string, error) {
var cellAddrMap map[string]string
for _, configPath := range getConfigPaths() {
file, err := os.Open(configPath)
if err != nil {
return nil, fmt.Errorf("error reading config file %v: %v", configPath, err)
}
err = json.NewDecoder(file).Decode(&cellAddrMap)
file.Close()
if err != nil {
return nil, fmt.Errorf("error decoding config file %v: %v", configPath, err)
}
return cellAddrMap, nil
}
return nil, fmt.Errorf("no config file paths found")
}
func ZkPathToZkAddr(zkPath string) (string, error) {
cell, err := ZkCellFromZkPath(zkPath)
if err != nil {
return "", err
}
cellAddrMap, err := getCellAddrMap()
if err != nil {
return "", err
}
if cell == "local" {
cell = GuessLocalCell()
} else if cell == "global" {
if *globalAddrs != "" {
return *globalAddrs, nil
} else if _, ok := cellAddrMap[cell]; !ok {
// if there is no "global" cell, look for a dc-specific
// address for the global cell
cell = GuessLocalCell() + "-global"
}
}
addr := cellAddrMap[cell]
if addr != "" {
return addr, nil
}
return "", fmt.Errorf("no addr found for zk cell: %#v", cell)
}
// returns all the known cells, alphabetically ordered. It will
// include 'global' if there is a dc-specific global cell or a global cell
func ZkKnownCells() ([]string, error) {
localCell := GuessLocalCell()
cellAddrMap, err := getCellAddrMap()
if err != nil {
return nil, err
}
result := make([]string, 0, len(cellAddrMap))
foundGlobal := false
for cell := range cellAddrMap {
// handle global, we just remember it
if cell == "global" {
foundGlobal = true
continue
}
// skip global cells, remember if we have our global
if strings.HasSuffix(cell, "-global") {
if cell == localCell+"-global" {
foundGlobal = true
}
continue
}
// non-global cell
result = append(result, cell)
}
if foundGlobal {
result = append(result, "global")
}
sort.Strings(result)
return result, nil
}
// GetZkSubprocessFlags returns the flags necessary to run a sub-process
// that would connect to the same zk servers as this process
// (assuming the environment of the sub-process is the same as ours)
func GetZkSubprocessFlags() []string {
result := make([]string, 0, 0)
if *localCell != "" {
result = append(result, "-zk.local-cell", *localCell)
}
if *localAddrs != "" {
result = append(result, "-zk.local-addrs", *localAddrs)
}
if *globalAddrs != "" {
result = append(result, "-zk.global-addrs", *globalAddrs)
}
if *baseTimeout != DEFAULT_BASE_TIMEOUT {
result = append(result, "-zk.base-timeout", baseTimeout.String())
}
return result
}