This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnanocube_node.go
392 lines (332 loc) · 9.87 KB
/
nanocube_node.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package distnano
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
type TBin struct {
startTime time.Time
binSize time.Duration
}
// ByTime implements sort.Interface for []*TBin based on the TBin's startTime.
type ByTime []*TBin
type NanocubeNode struct {
addr string
tBin *TBin
relativeBin int
}
type SpecialTimeQuery struct {
queryOne string
queryTwo string
bucketOffset int
node *NanocubeNode
}
func (t ByTime) Len() int { return len(t) }
func (t ByTime) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t ByTime) Less(i, j int) bool { return t[i].startTime.Before(t[j].startTime) }
// nanocubeNodesFromAddrs converts addrs to NanocubeNodes by querying the
// schema of each address and creating NanocubeNodes accordingly.
func nanocubeNodesFromAddrs(addrs []string) ([]*NanocubeNode, error) {
// Get each schema and the tbin string to determine each node's notion of
// time.
tbins := make([]*TBin, len(addrs), len(addrs))
for i, addr := range addrs {
response, err := (&NanocubeNode{addr: addr}).Query("/schema")
if err != nil {
return nil, err
}
for _, md := range (response.(*SchemaResponse)).Metadata {
if md.Key == "tbin" {
tbin := newTBin(md.Value)
if tbin == nil {
return nil, errors.New("error creating time bin")
}
tbins[i] = tbin
}
}
}
sort.Sort(ByTime(tbins))
nanocubeNodes := make([]*NanocubeNode, len(addrs), len(addrs))
var firstNode *NanocubeNode = nil
for i, addr := range addrs {
nanocubeNodes[i] = newNanocubeNode(addr, tbins[i])
if firstNode == nil {
firstNode = nanocubeNodes[i]
}
// Get number of bins since the firstNode's startTime.
nanocubeNodes[i].relativeBin =
int(nanocubeNodes[i].tBin.startTime.Sub(
firstNode.tBin.startTime,
).Hours() / firstNode.tBin.binSize.Hours())
}
return nanocubeNodes, nil
}
// splitTBString returns the time (start) in tbstring and the Duration that
// specifies the size of a time bin.
func newTBin(tbstring string) *TBin {
// layout is needed by the time module to interpret the format of our time
// strings.
layout := "2006-01-02_15:04:05"
splitString := strings.Split(tbstring, "_")
if len(splitString) != 3 {
return nil
}
t, err := time.Parse(
layout,
fmt.Sprintf("%v_%v", splitString[0], splitString[1]),
)
if err != nil {
return nil
}
d, err := time.ParseDuration(splitString[2])
if err != nil {
return nil
}
tbin := new(TBin)
tbin.startTime = t
tbin.binSize = d
return tbin
}
func newNanocubeNode(addr string, tBin *TBin) *NanocubeNode {
nn := new(NanocubeNode)
nn.addr = addr
nn.tBin = tBin
return nn
}
// Query does some preprocessing of the url before querying the NanocubeNode.
func (n *NanocubeNode) Query(url string) (JSONResponse, error) {
var spTimeQuery *SpecialTimeQuery
// If this is a time query, we have to convert it from an absolute to a
// relative time.
if strings.Contains(url, "interval") {
var queryOutsideRange bool
url, queryOutsideRange, spTimeQuery = n.mustAbsToRelTimeQuery(url)
if queryOutsideRange {
return new(NanocubeResponse), nil
}
}
if spTimeQuery != nil {
return spTimeQuery.query()
}
return n.query(url)
}
// query queries both urls of SpecialTimeQuery and merges them together to
// return one NanocubeResponse.
func (s *SpecialTimeQuery) query() (*NanocubeResponse, error) {
// Query the second query first. This is the query that has the same
// resolution as the global query and fits nicely into the global buckets.
response, err := s.node.query(s.queryTwo)
if err != nil {
return nil, err
}
responseTwo := response.(*NanocubeResponse)
// If our n.relativeBin is not divisible by the number of tbins in a global
// bucket.
if s.queryOne != "" {
response, err = s.node.query(s.queryOne)
if err != nil {
return nil, err
}
responseOne := response.(*NanocubeResponse)
if len(responseOne.Root.Children) > 0 {
// And now merge the responses: add everything in the queryOne
// bucket to the first bucket of queryTwo.
for _, child := range responseTwo.Root.Children {
// Shift over to make space for the "0" bucket.
child.Path[0] += 1
}
// Add the 0 bucket.
responseTwo.Root.Children = append(
responseTwo.Root.Children,
Child{
Path: []uint{0},
Val: responseOne.Root.Children[0].Val,
},
)
}
}
// Adjust for bucket offset.
for _, child := range responseTwo.Root.Children {
child.Path[0] += uint(s.bucketOffset)
}
return responseTwo, nil
}
// query queries the NanocubeNode by sending an HTTP GET request to the url
// endpoint. Examples are: "/count", "/schema".
func (n *NanocubeNode) query(url string) (JSONResponse, error) {
// Note what kind of request this is.
schemaRequest := strings.HasPrefix(url, "/schema")
var response JSONResponse
if schemaRequest {
response = new(SchemaResponse)
} else {
response = new(NanocubeResponse)
}
rawResponse, err := http.Get(fmt.Sprintf("%v%v", n.addr, url))
if err != nil {
return nil, err
}
defer rawResponse.Body.Close()
content, _ := ioutil.ReadAll(rawResponse.Body)
err = response.Unmarshal(content)
if err != nil {
return nil, err
}
return response, nil
}
// absToRelTimeQuery converts a query from a distribution-agnostic client to
// a query that takes into account a NanocubeNode's relativeBin.
// The return values are:
//
// string: the relative query
// bool: whether the query lies outside the NanocubeNode's range
//
// The special time query is for queries where n.relativeBin falls into a
// bucket. It will have queryOne set to what the query should be to query the
// NanocubeNode until the start of the next bucket and then the query from the
// start of that bucket until the end global bucket.
//
// With this information, the caller can reconstruct a global answer with
// arbitrary relative offsets.
func (n *NanocubeNode) mustAbsToRelTimeQuery(query string) (
string,
bool,
*SpecialTimeQuery,
) {
timeQueries := []*regexp.Regexp{
regexp.MustCompile("mt_interval_sequence[(][0-9]*,[0-9]*,[0-9]*[)]"),
regexp.MustCompile("interval[(][0-9]*,[0-9]*[)]"),
}
queryOutsideRange := true
relative := query
for i, timeQuery := range timeQueries {
for _, substr := range timeQuery.FindAllString(query, -1) {
switch i {
case 0:
// mt_interval_sequence case.
abstbins := mustSplitAndGetInts(substr, "mt_interval_sequence")
reltbins := make([]int, len(abstbins), len(abstbins))
reltbins[1] = abstbins[1]
// Semantics are: first value is start time bin, second value
// is how many time bins are in a bucket, third value is how
// many buckets to get.
startOffset := abstbins[0] - n.relativeBin
endOffset := startOffset + (abstbins[1] * abstbins[2])
if startOffset >= 0 {
reltbins[0] = startOffset
} else if endOffset > 0 {
// Create a SpecialTimeQuery. First query goes to
spTimeQuery := new(SpecialTimeQuery)
numTBinsToStart := abstbins[1] -
((n.relativeBin - abstbins[0]) % abstbins[1])
if numTBinsToStart == abstbins[1] {
// Case where n.relativBin is divisible by abstbins[1].
numTBinsToStart = 0
}
// How many buckets are we going to miss?
bucketOffset := (n.relativeBin - abstbins[0]) / abstbins[1]
spTimeQuery.queryTwo = strings.Replace(
relative,
substr,
fmt.Sprintf(
"mt_interval_sequence(%v,%v,%v)",
// Start from the start of the next bucket.
strconv.Itoa(0+numTBinsToStart),
// And use the same bucket resolution.
strconv.Itoa(abstbins[1]),
// And subtract however many buckets we can't
// answer plus the one that will be answered by
// our first partial query.
strconv.Itoa(abstbins[2]-(bucketOffset+1)),
),
-1,
)
if numTBinsToStart > 0 {
spTimeQuery.queryOne = strings.Replace(
relative,
substr,
fmt.Sprintf(
"mt_interval_sequence(%v,%v,%v)",
// Start from the start of our NanocubeNode.
strconv.Itoa(0),
strconv.Itoa(numTBinsToStart),
// Get one multiple of what we have above.
strconv.Itoa(1),
),
-1,
)
}
spTimeQuery.bucketOffset = bucketOffset
spTimeQuery.node = n
return "", false, spTimeQuery
}
reltbins[2] = int((endOffset - reltbins[0]) / reltbins[1])
queryOutsideRange = endOffset < 0
relative = strings.Replace(
relative,
substr,
fmt.Sprintf(
"mt_interval_sequence(%v,%v,%v)",
strconv.Itoa(reltbins[0]),
strconv.Itoa(reltbins[1]),
strconv.Itoa(reltbins[2]),
),
-1,
)
case 1:
// interval case.
abstbins := mustSplitAndGetInts(substr, "interval")
reltbins := make([]int, len(abstbins), len(abstbins))
// Semantics are: first value is start time bin, second value
// is end time bin.
startOffset := abstbins[0] - n.relativeBin
endOffset := abstbins[1] - n.relativeBin
if startOffset > 0 {
reltbins[0] = startOffset
} else {
reltbins[0] = 0
}
reltbins[1] = endOffset
queryOutsideRange = endOffset < 0
relative = strings.Replace(
relative,
substr,
fmt.Sprintf(
"interval(%v,%v)",
strconv.Itoa(reltbins[0]),
strconv.Itoa(reltbins[1]),
),
-1,
)
}
}
}
return relative, queryOutsideRange, nil
}
func mustSliceAtoi(slice []string) []int {
result := make([]int, len(slice), len(slice))
for j, a := range slice {
if i, err := strconv.Atoi(a); err != nil {
panic(err)
} else {
result[j] = i
}
}
return result
}
// mustSplitAndGetInts is a utility function to be used solely for strings of
// the form sep(0,...,9) to get a list of integers separated by commas.
func mustSplitAndGetInts(str, sep string) []int {
split := strings.Split(str, sep)
// Trim the parentheses then split by comma to get the three
// values.
return mustSliceAtoi(
strings.Split(strings.Trim(split[1], "()"), ","),
)
}