forked from skynetservices/skynet-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
276 lines (210 loc) · 5.04 KB
/
query.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
package skynet
import (
"encoding/json"
"fmt"
"github.com/4ad/doozer"
"log"
"path"
"strings"
)
type Query struct {
Service string
Version string
Host string
Port string
Region string
UUID string
Registered *bool
DoozerConn *DoozerConnection
doozerRev int64
// Internal use only
pathLength int
paths map[string]*doozer.FileInfo
files map[string]*doozer.FileInfo
}
func (q *Query) VisitDir(path string, f *doozer.FileInfo) bool {
parts := strings.Split(path, "/")
// If we know we are looking for dir's at a specified level no need to dig deeper
if q.pathLength > 0 && len(parts) > q.pathLength {
return false
}
// If we know the length we need for a proper path we don't need any leading paths
if q.pathLength <= 0 || q.pathLength == len(parts) {
q.paths[path] = f
}
return true
}
func (q *Query) VisitFile(path string, f *doozer.FileInfo) {
q.files[path] = f
}
func (q *Query) makePath() (path string) {
path = "/services"
if q.Service == "" {
return
}
path += "/" + q.Service
if q.Version == "" {
return
}
path += "/" + q.Version
if q.Region == "" {
return
}
path += "/" + q.Region
if q.Host == "" {
return
}
path += "/" + q.Host
if q.Port == "" {
return
}
path += "/" + q.Port
return
}
func (q *Query) search() {
q.paths = make(map[string]*doozer.FileInfo, 0)
q.files = make(map[string]*doozer.FileInfo, 0)
q.doozerRev = q.getCurrentDoozerRevision()
path := q.makePath()
q.DoozerConn.Walk(q.doozerRev, path, q, nil)
}
func (q *Query) FindHosts() []string {
q.pathLength = 6
q.search()
return q.matchingPaths()
}
func (q *Query) FindRegions() []string {
q.pathLength = 5
q.search()
return q.matchingPaths()
}
func (q *Query) FindServices() []string {
q.pathLength = 3
q.search()
return q.matchingPaths()
}
func (q *Query) FindServiceVersions() []string {
q.pathLength = 4
q.search()
return q.matchingPaths()
}
func (q *Query) FindInstances() []*ServiceInfo {
q.search()
results := make([]*ServiceInfo, 0)
// At this point we don't know which values were supplied
// if all prefixed dir's were supplied no filtering is needed, but this may be all nodes
for path, _ := range q.files {
var s ServiceInfo
data, _, err := q.DoozerConn.Get(path, q.doozerRev)
if err != nil {
log.Panic(err.Error())
}
err = json.Unmarshal(data, &s)
if !q.ServiceMatches(s) {
continue
}
results = append(results, &s)
}
// make sure we can garbage collect
// who knows how long the app might keep the query object around for
q.paths = nil
q.files = nil
return results
}
func (q *Query) matchingPaths() []string {
results := make([]string, 0)
unique := make(map[string]string, 0)
for p, dir := range q.paths {
if !q.pathMatches(p) {
continue
}
if _, ok := unique[dir.Name]; !ok {
pathMatches := true
// If Port or Registered supplied, we have to inspect files to ensure the path has a match in it
if q.Port != "" || q.Registered != nil {
pathMatches = false
rev := q.DoozerConn.GetCurrentRevision()
files, _ := q.DoozerConn.Getdirinfo(p, rev, 0, -1)
if files != nil {
for _, file := range files {
data, _, err := q.DoozerConn.Get(path.Join(p, file.Name), rev)
if err == nil {
s := ServiceInfo{}
err = json.Unmarshal(data, &s)
if q.ServiceMatches(s) {
pathMatches = true
break
}
}
}
}
}
if pathMatches {
unique[dir.Name] = dir.Name
results = append(results, dir.Name)
}
}
}
// reset internal variables also make sure we can garbage collect
// who knows how long the app might keep the query object around for
q.paths = nil
q.files = nil
q.pathLength = 0
return results
}
// We aren't able to match a path for a query on port or registered
func (q *Query) pathMatches(path string) bool {
parts := strings.Split(path, "/")
if len(parts) >= 3 && q.Service != "" && parts[2] != q.Service {
return false
}
if len(parts) >= 4 && q.Version != "" && parts[3] != q.Version {
return false
}
if len(parts) >= 5 && q.Region != "" && parts[4] != q.Region {
return false
}
if len(parts) >= 6 && q.Host != "" && parts[5] != q.Host {
return false
}
return true
}
func (q *Query) ServiceMatches(s ServiceInfo) bool {
if q.Service != "" && s.Config.Name != q.Service {
return false
}
if q.Version != "" && s.Config.Version != q.Version {
return false
}
if q.Region != "" && s.Config.Region != q.Region {
return false
}
if q.Host != "" && s.Config.ServiceAddr.IPAddress != q.Host {
return false
}
if q.Port != "" && fmt.Sprintf("%d", s.Config.ServiceAddr.Port) != q.Port {
return false
}
if q.Registered != nil && s.Registered != *q.Registered {
return false
}
if q.UUID != "" && s.Config.UUID != q.UUID {
return false
}
return true
}
func (q *Query) Reset() {
q.Service = ""
q.Version = ""
q.Region = ""
q.Host = ""
q.Registered = nil
q.Port = ""
}
func (q *Query) getCurrentDoozerRevision() int64 {
revision, err := q.DoozerConn.Rev()
if err != nil {
log.Panic(err.Error())
}
return revision
}