forked from colinmarc/hdfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.go
244 lines (218 loc) · 6.07 KB
/
conf.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package hdfs
import (
"encoding/xml"
"errors"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
)
// Property is the struct representation of hadoop configuration
// key value pair.
type Property struct {
Name string `xml:"name"`
Value string `xml:"value"`
}
type propertyList struct {
Property []Property `xml:"property"`
}
// HadoopConf represents a map of all the key value configutation
// pairs found in a user's hadoop configuration files.
type HadoopConf map[string]string
var errUnresolvedNamenode = errors.New("no namenode address in configuration")
var errInvalidHDFSFilename = errors.New("invalid HDFS Filename")
// LoadHadoopConf returns a HadoopConf object representing configuration from
// the specified path, or finds the correct path in the environment. If
// path or the env variable HADOOP_CONF_DIR is specified, it should point
// directly to the directory where the xml files are. If neither is specified,
// ${HADOOP_HOME}/conf will be used.
func LoadHadoopConf(path string) HadoopConf {
if path == "" {
path = os.Getenv("HADOOP_CONF_DIR")
if path == "" {
path = filepath.Join(os.Getenv("HADOOP_HOME"), "conf")
}
}
hadoopConf := make(HadoopConf)
for _, file := range []string{"core-site.xml", "hdfs-site.xml"} {
pList := propertyList{}
f, err := ioutil.ReadFile(filepath.Join(path, file))
if err != nil {
continue
}
err = xml.Unmarshal(f, &pList)
if err != nil {
continue
}
for _, prop := range pList.Property {
hadoopConf[prop.Name] = prop.Value
}
}
return hadoopConf
}
// Namenodes returns the default namenode hosts present in the configuration. The
// returned slice will be sorted and deduped.
// This function only works for old single NameService Hadoop conf
// It should be deprecated
func (conf HadoopConf) Namenodes() ([]string, error) {
defNSID := conf.DefaultNSID()
if defNSID != "" {
return conf.AddressesByNameServiceID(defNSID)
}
// fallback to pick up all namenodex in XML
nns := make(map[string]bool)
for key, value := range conf {
if strings.HasPrefix(key, "fs.default") {
nnUrl, _ := url.Parse(value)
nns[nnUrl.Host] = true
} else if strings.HasPrefix(key, "dfs.namenode.rpc-address") {
nns[value] = true
}
}
if len(nns) == 0 {
return nil, errUnresolvedNamenode
}
keys := make([]string, 0, len(nns))
for k, _ := range nns {
keys = append(keys, k)
}
sort.Strings(keys)
return keys, nil
}
// return the NameServiceID of defaultFS
func (conf HadoopConf) DefaultNSID() string {
value := conf.DefaultFS()
if value != "" {
nnUrl, _ := url.Parse(value)
return nnUrl.Host
}
return ""
}
// return the defaultFS
func (conf HadoopConf) DefaultFS() string {
value, _ := conf["fs.defaultFS"]
if value == "" { // fallback to deprecated form
value, _ = conf["fs.default.name"]
}
return value
}
// return the HA Address of namenode
func (conf HadoopConf) AddressesByNameServiceID(nsid string) ([]string, error) {
rets := make([]string, 0, 8)
// for very simple
if nsid == conf.DefaultNSID() {
value := conf.DefaultFS()
nnUrl, err := url.Parse(value)
if err == nil {
return []string{nnUrl.Host}, nil
}
}
// for simple
key := "dfs.nsidnode.rpc-address." + nsid
addr, ok := conf[key]
if ok {
rets = append(rets, addr)
return rets, nil
}
// for HA
haListName := "dfs.ha.namenodes." + nsid
haListStr, ok := conf[haListName]
var haList []string
haList = strings.Split(haListStr, ",")
for _, haName := range haList {
key := "dfs.namenode.rpc-address." + nsid + "." + haName
addr, ok := conf[key]
if ok && addr != "" {
rets = append(rets, addr)
}
}
// sort and return
if len(rets) <= 0 {
return nil, errUnresolvedNamenode
} else {
sort.Strings(rets)
return rets, nil
}
}
// return the actual path on the final namenode of a viewfs path
// if property
// fs.viewfs.mounttable.nsX.link./user = hdfs://SunshineNameNode3/user2
// defaultFS = nsX
// then
// call ("/user/sub") returns ("SunshineNameNode3", "/user/sub", nil)
// call ("hdfs://nsX/user/sub") returns ("SunshineNameNode3", "/user2/sub", nil)
func (conf HadoopConf) ViewfsReparseFilename(rootnsid string, filename string) (string, string, error) {
var nsid, path string
u, err := url.Parse(filename)
if err != nil || (u.Scheme != "hdfs" && u.Scheme != "") {
return "", "", errInvalidHDFSFilename
}
if u.Host != "" && rootnsid != "" { // host and nsid conflict
return "", "", errInvalidHDFSFilename
}
nsid, path = u.Host, u.Path
if nsid == "" {
nsid = rootnsid
}
if nsid == "" {
nsid = conf.DefaultNSID()
}
dirs := strings.Split(path, "/")
if dirs[0] != "" {
dirs = append([]string{""}, dirs...)
}
for i := len(dirs); i > 0; i-- {
prefix := strings.Join(dirs[0:i], "/")
key := "fs.viewfs.mounttable." + nsid + ".link." + prefix
value, ok := conf[key]
if ok {
postfix := filepath.Join(dirs[i:]...)
newurl := value + "/" + postfix
u, _ = url.Parse(newurl)
return u.Host, u.Path, nil
}
}
return nsid, path, nil
}
// type of namenode address string
type TypeOfNamenodeAddressString int
const (
_ TypeOfNamenodeAddressString = iota
TNAS_SimpleAddress
TNAS_SimpleNameServiceID
TNAS_ViewfsNameServiceID
)
// returns the type of namenode/address
// 3 situation
// A. Just a simple hostname (with port)
// B. A NameServiceID without viewfs mount links
// C. A NameServiceID with viewfs mount links
func (conf HadoopConf) CheckTypeOfNameAddressString(maybe_addr string) TypeOfNamenodeAddressString {
// if address is "Host:Port" style
if strings.Contains(maybe_addr, ":") {
return TNAS_SimpleAddress
}
// check is there any mounttable
prefix := "fs.viewfs.mounttable." + maybe_addr + ".link."
for key, _ := range conf {
if strings.HasPrefix(key, prefix) {
return TNAS_ViewfsNameServiceID
}
}
// check is it a NameServiceID
nsids_str, _ := conf["dfs.nameservices"]
nsids := strings.Split(nsids_str, ",")
for _, v := range nsids {
if maybe_addr == v {
return TNAS_SimpleNameServiceID
}
}
// check is there any HA nodes
if _, ok := conf["dfs.ha.namenodes."+maybe_addr]; ok {
return TNAS_SimpleNameServiceID
}
// fallback to simple address
return TNAS_SimpleAddress
}