-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve_dao.go
366 lines (329 loc) · 10.4 KB
/
resolve_dao.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
Copyright 2014 Outbrain Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package inst
import (
"github.com/openark/golib/log"
"github.com/openark/golib/sqlutils"
"github.com/openark/orchestrator/go/config"
"github.com/openark/orchestrator/go/db"
"github.com/rcrowley/go-metrics"
)
var writeResolvedHostnameCounter = metrics.NewCounter()
var writeUnresolvedHostnameCounter = metrics.NewCounter()
var readResolvedHostnameCounter = metrics.NewCounter()
var readUnresolvedHostnameCounter = metrics.NewCounter()
var readAllResolvedHostnamesCounter = metrics.NewCounter()
func init() {
metrics.Register("resolve.write_resolved", writeResolvedHostnameCounter)
metrics.Register("resolve.write_unresolved", writeUnresolvedHostnameCounter)
metrics.Register("resolve.read_resolved", readResolvedHostnameCounter)
metrics.Register("resolve.read_unresolved", readUnresolvedHostnameCounter)
metrics.Register("resolve.read_resolved_all", readAllResolvedHostnamesCounter)
}
// WriteResolvedHostname stores a hostname and the resolved hostname to backend database
func WriteResolvedHostname(hostname string, resolvedHostname string) error {
writeFunc := func() error {
_, err := db.ExecOrchestrator(`
insert into
hostname_resolve (hostname, resolved_hostname, resolved_timestamp)
values
(?, ?, NOW())
on duplicate key update
resolved_hostname = VALUES(resolved_hostname),
resolved_timestamp = VALUES(resolved_timestamp)
`,
hostname,
resolvedHostname)
if err != nil {
return log.Errore(err)
}
if hostname != resolvedHostname {
// history is only interesting when there's actually something to resolve...
_, err = db.ExecOrchestrator(`
insert into
hostname_resolve_history (hostname, resolved_hostname, resolved_timestamp)
values
(?, ?, NOW())
on duplicate key update
hostname=values(hostname),
resolved_timestamp=values(resolved_timestamp)
`,
hostname,
resolvedHostname)
}
writeResolvedHostnameCounter.Inc(1)
return nil
}
return ExecDBWriteFunc(writeFunc)
}
// ReadResolvedHostname returns the resolved hostname given a hostname, or empty if not exists
func ReadResolvedHostname(hostname string) (string, error) {
var resolvedHostname string = ""
query := `
select
resolved_hostname
from
hostname_resolve
where
hostname = ?
`
err := db.QueryOrchestrator(query, sqlutils.Args(hostname), func(m sqlutils.RowMap) error {
resolvedHostname = m.GetString("resolved_hostname")
return nil
})
readResolvedHostnameCounter.Inc(1)
if err != nil {
log.Errore(err)
}
return resolvedHostname, err
}
func ReadAllHostnameResolves() ([]HostnameResolve, error) {
res := []HostnameResolve{}
query := `
select
hostname,
resolved_hostname
from
hostname_resolve
`
err := db.QueryOrchestratorRowsMap(query, func(m sqlutils.RowMap) error {
hostnameResolve := HostnameResolve{hostname: m.GetString("hostname"), resolvedHostname: m.GetString("resolved_hostname")}
res = append(res, hostnameResolve)
return nil
})
readAllResolvedHostnamesCounter.Inc(1)
if err != nil {
log.Errore(err)
}
return res, err
}
// ReadAllHostnameUnresolves returns the content of the hostname_unresolve table
func ReadAllHostnameUnresolves() ([]HostnameUnresolve, error) {
unres := []HostnameUnresolve{}
query := `
select
hostname,
unresolved_hostname
from
hostname_unresolve
`
err := db.QueryOrchestratorRowsMap(query, func(m sqlutils.RowMap) error {
hostnameUnresolve := HostnameUnresolve{hostname: m.GetString("hostname"), unresolvedHostname: m.GetString("unresolved_hostname")}
unres = append(unres, hostnameUnresolve)
return nil
})
return unres, log.Errore(err)
}
// ReadAllHostnameUnresolves returns the content of the hostname_unresolve table
func ReadAllHostnameUnresolvesRegistrations() (registrations []HostnameRegistration, err error) {
unresolves, err := ReadAllHostnameUnresolves()
if err != nil {
return registrations, err
}
for _, unresolve := range unresolves {
registration := NewHostnameRegistration(&InstanceKey{Hostname: unresolve.hostname}, unresolve.unresolvedHostname)
registrations = append(registrations, *registration)
}
return registrations, nil
}
// readUnresolvedHostname reverse-reads hostname resolve. It returns a hostname which matches given pattern and resovles to resolvedHostname,
// or, in the event no such hostname is found, the given resolvedHostname, unchanged.
func readUnresolvedHostname(hostname string) (string, error) {
unresolvedHostname := hostname
query := `
select
unresolved_hostname
from
hostname_unresolve
where
hostname = ?
`
err := db.QueryOrchestrator(query, sqlutils.Args(hostname), func(m sqlutils.RowMap) error {
unresolvedHostname = m.GetString("unresolved_hostname")
return nil
})
readUnresolvedHostnameCounter.Inc(1)
if err != nil {
log.Errore(err)
}
return unresolvedHostname, err
}
// readMissingHostnamesToResolve gets those (unresolved, e.g. VIP) hostnames that *should* be present in
// the hostname_resolve table, but aren't.
func readMissingKeysToResolve() (result InstanceKeyMap, err error) {
query := `
select
hostname_unresolve.unresolved_hostname,
database_instance.port
from
database_instance
join hostname_unresolve on (database_instance.hostname = hostname_unresolve.hostname)
left join hostname_resolve on (database_instance.hostname = hostname_resolve.resolved_hostname)
where
hostname_resolve.hostname is null
`
err = db.QueryOrchestratorRowsMap(query, func(m sqlutils.RowMap) error {
instanceKey := InstanceKey{Hostname: m.GetString("unresolved_hostname"), Port: m.GetInt("port")}
result.AddKey(instanceKey)
return nil
})
if err != nil {
log.Errore(err)
}
return result, err
}
// WriteHostnameUnresolve upserts an entry in hostname_unresolve
func WriteHostnameUnresolve(instanceKey *InstanceKey, unresolvedHostname string) error {
writeFunc := func() error {
_, err := db.ExecOrchestrator(`
insert into hostname_unresolve (
hostname,
unresolved_hostname,
last_registered)
values (?, ?, NOW())
on duplicate key update
unresolved_hostname=values(unresolved_hostname),
last_registered=now()
`, instanceKey.Hostname, unresolvedHostname,
)
if err != nil {
return log.Errore(err)
}
_, err = db.ExecOrchestrator(`
replace into hostname_unresolve_history (
hostname,
unresolved_hostname,
last_registered)
values (?, ?, NOW())
`, instanceKey.Hostname, unresolvedHostname,
)
writeUnresolvedHostnameCounter.Inc(1)
return nil
}
return ExecDBWriteFunc(writeFunc)
}
// DeleteHostnameUnresolve removes an unresolve entry
func DeleteHostnameUnresolve(instanceKey *InstanceKey) error {
writeFunc := func() error {
_, err := db.ExecOrchestrator(`
delete from hostname_unresolve
where hostname=?
`, instanceKey.Hostname,
)
return log.Errore(err)
}
return ExecDBWriteFunc(writeFunc)
}
// ExpireHostnameUnresolve expires hostname_unresolve entries that haven't been updated recently.
func ExpireHostnameUnresolve() error {
writeFunc := func() error {
_, err := db.ExecOrchestrator(`
delete from hostname_unresolve
where last_registered < NOW() - INTERVAL ? MINUTE
`, config.Config.ExpiryHostnameResolvesMinutes,
)
return log.Errore(err)
}
return ExecDBWriteFunc(writeFunc)
}
// ForgetExpiredHostnameResolves
func ForgetExpiredHostnameResolves() error {
_, err := db.ExecOrchestrator(`
delete
from hostname_resolve
where
resolved_timestamp < NOW() - interval ? minute`,
2*config.Config.ExpiryHostnameResolvesMinutes,
)
return err
}
// DeleteInvalidHostnameResolves removes invalid resolves. At this time these are:
// - infinite loop resolves (A->B and B->A), remove earlier mapping
func DeleteInvalidHostnameResolves() error {
var invalidHostnames []string
query := `
select
early.hostname
from
hostname_resolve as latest
join hostname_resolve early on (latest.resolved_hostname = early.hostname and latest.hostname = early.resolved_hostname)
where
latest.hostname != latest.resolved_hostname
and latest.resolved_timestamp > early.resolved_timestamp
`
err := db.QueryOrchestratorRowsMap(query, func(m sqlutils.RowMap) error {
invalidHostnames = append(invalidHostnames, m.GetString("hostname"))
return nil
})
if err != nil {
return err
}
for _, invalidHostname := range invalidHostnames {
_, err = db.ExecOrchestrator(`
delete
from hostname_resolve
where
hostname = ?`,
invalidHostname,
)
log.Errore(err)
}
return err
}
// deleteHostnameResolves compeltely erases the database cache
func deleteHostnameResolves() error {
_, err := db.ExecOrchestrator(`
delete
from hostname_resolve`,
)
return err
}
// writeHostnameIPs stroes an ipv4 and ipv6 associated witha hostname, if available
func writeHostnameIPs(hostname string, ipv4String string, ipv6String string) error {
writeFunc := func() error {
_, err := db.ExecOrchestrator(`
insert into
hostname_ips (hostname, ipv4, ipv6, last_updated)
values
(?, ?, ?, NOW())
on duplicate key update
ipv4 = VALUES(ipv4),
ipv6 = VALUES(ipv6),
last_updated = VALUES(last_updated)
`,
hostname,
ipv4String,
ipv6String,
)
return log.Errore(err)
}
return ExecDBWriteFunc(writeFunc)
}
// readUnresolvedHostname reverse-reads hostname resolve. It returns a hostname which matches given pattern and resovles to resolvedHostname,
// or, in the event no such hostname is found, the given resolvedHostname, unchanged.
func readHostnameIPs(hostname string) (ipv4 string, ipv6 string, err error) {
query := `
select
ipv4, ipv6
from
hostname_ips
where
hostname = ?
`
err = db.QueryOrchestrator(query, sqlutils.Args(hostname), func(m sqlutils.RowMap) error {
ipv4 = m.GetString("ipv4")
ipv6 = m.GetString("ipv6")
return nil
})
return ipv4, ipv6, log.Errore(err)
}