-
Notifications
You must be signed in to change notification settings - Fork 3
/
batch.go
619 lines (526 loc) · 13.4 KB
/
batch.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
package autotown
import (
"bytes"
"compress/gzip"
"encoding/json"
"io"
"net/http"
"strconv"
"time"
"github.com/dustin/go-jsonpointer"
"golang.org/x/net/context"
"golang.org/x/sync/errgroup"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/taskqueue"
)
const (
mapStage1 = "map"
mapStage2 = "map2"
resubmitThreshold = 1000
dayFmt = "2006-01-02"
)
func init() {
http.HandleFunc("/admin/batchForm", handleBatchForm)
http.HandleFunc("/admin/submitMap", handleSubmitMap)
http.HandleFunc("/batch/map", batchMap)
http.HandleFunc("/batch/destroy", batchDestroy)
http.HandleFunc("/batch/logkeys", handleLogKeys)
http.HandleFunc("/batch/indexTunes", handleIndexTunes)
http.HandleFunc("/batch/indexUsage", handleIndexUsage)
http.HandleFunc("/batch/countUsage", handleCountUsage)
http.HandleFunc("/batch/clearCountFlag", handleClearCountFlag)
http.HandleFunc("/batch/processUsage", handleProcessUsage)
http.HandleFunc("/_ah/start", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(204)
})
http.HandleFunc("/_ah/stop", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(204)
})
}
func handleBatchForm(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
kinds, err := datastore.Kinds(c)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
execTemplate(appengine.NewContext(r), w, "batch.html", struct {
Kinds []string
Message string
}{
kinds, r.FormValue("msg")})
}
func handleSubmitMap(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
if r.FormValue("kind") == "" {
http.Redirect(w, r, "/admin/batchForm?msg=Kind+parameter+is+required", http.StatusFound)
return
}
_, err := taskqueue.Add(c, taskqueue.NewPOSTTask("/batch/map", r.Form), mapStage1)
if err != nil {
log.Errorf(c, "Error getting queue stats: %v", err)
http.Error(w, err.Error(), 500)
return
}
if r.Header.Get("X-Appengine-Cron") == "true" {
log.Infof(c, "Submitted on behalf of cron.")
w.WriteHeader(204)
return
}
http.Redirect(w, r, "/admin/batchForm?msg=Started", http.StatusFound)
}
func maybePanic(err error) {
if err != nil {
panic(err)
}
}
func queueMore(c context.Context) bool {
st, err := taskqueue.QueueStats(c, []string{mapStage2})
if err != nil {
log.Errorf(c, "Error getting queue stats: %v", err)
return false
}
log.Debugf(c, "map2 queue stats: %+v", st[0])
return st[0].Tasks < resubmitThreshold
}
func queueMany(c context.Context, queue string, tasks []*taskqueue.Task) error {
g, _ := errgroup.WithContext(c)
for len(tasks) > 0 {
some := tasks
if len(tasks) > 100 {
some = tasks[:100]
tasks = tasks[100:]
} else {
tasks = nil
}
g.Go(func() error {
_, err := taskqueue.AddMulti(c, some, queue)
return err
})
}
return g.Wait()
}
// Params:
// - kind: The kind of thing to query
// - next: http path to process data
func batchMap(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
start := time.Now()
if !queueMore(c) {
log.Debugf(c, "Too many jobs queued, backing off")
http.Error(w, "Busy", 503)
return
}
q := datastore.NewQuery(r.FormValue("kind")).KeysOnly()
if cstr := r.FormValue("cursor"); cstr != "" {
cursor, err := datastore.DecodeCursor(cstr)
maybePanic(err)
log.Debugf(c, "Starting from cursor %v", cstr)
q = q.Start(cursor)
}
keys := []string{}
finished := false
t := q.Run(c)
for i := 0; i < 10000; i++ {
k, err := t.Next(nil)
if err == datastore.Done {
finished = true
break
} else if err != nil {
http.Error(w, err.Error(), 500)
return
}
keys = append(keys, k.Encode())
}
log.Infof(c, "Got %v %v keys in %v, finished=%v",
len(keys), r.FormValue("kind"), time.Since(start), finished)
var tasks []*taskqueue.Task
for len(keys) > 0 && r.FormValue("next") != "" {
subkeys := keys
if len(subkeys) > 100 {
subkeys = keys[:100]
keys = keys[100:]
} else {
keys = nil
}
buf := &bytes.Buffer{}
z := gzip.NewWriter(buf)
e := json.NewEncoder(z)
maybePanic(e.Encode(subkeys))
maybePanic(z.Flush())
maybePanic(z.Close())
log.Debugf(c, "Queueing %v with %v keys compressed to %v bytes",
mapStage2, len(subkeys), buf.Len())
tasks = append(tasks, &taskqueue.Task{
Path: r.FormValue("next"),
Payload: buf.Bytes(),
})
}
if err := queueMany(c, mapStage2, tasks); err != nil {
log.Errorf(c, "Error queueing task sets: %v", err)
http.Error(w, err.Error(), 500)
return
}
if !finished {
cursor, err := t.Cursor()
maybePanic(err)
log.Debugf(c, "Requesting more from %v", cursor.String())
r.Form.Set("cursor", cursor.String())
taskqueue.Add(c, taskqueue.NewPOSTTask("/batch/map", r.Form), mapStage1)
}
w.WriteHeader(201)
}
var destructionWhitelist = map[string]bool{"DailyCounts": true, "FoundController": true}
func batchDestroy(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
keys, err := decodeKeys(r.Body)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if len(keys) == 0 {
log.Debugf(c, "nothing to do")
w.WriteHeader(204)
return
}
log.Infof(c, "Got %v %v keys to destroy", len(keys), keys[0].Kind())
for _, k := range keys {
if !destructionWhitelist[k.Kind()] {
log.Errorf(c, "Refusing to destroy non-whitelisted %v", k.Kind())
http.Error(w, "Not whitelisted: "+k.Kind(), 400)
return
}
}
err = datastore.DeleteMulti(c, keys)
if err != nil {
log.Infof(c, "Error deleting things: %v", err)
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(204)
}
func decodeKeys(r io.Reader) ([]*datastore.Key, error) {
z, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
keyStr := []string{}
if err := json.NewDecoder(z).Decode(&keyStr); err != nil {
return nil, err
}
keys := []*datastore.Key{}
for _, k := range keyStr {
key, err := datastore.DecodeKey(k)
if err != nil {
return nil, err
}
keys = append(keys, key)
}
return keys, nil
}
func handleLogKeys(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
keys, err := decodeKeys(r.Body)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
log.Debugf(c, "Got %v keys to process", len(keys))
for _, k := range keys {
log.Debugf(c, "%v", k)
}
}
func jraw(c context.Context, m *json.RawMessage, path string) []byte {
rv, err := jsonpointer.Find([]byte(*m), path)
if err != nil {
log.Warningf(c, "Error decoding %q from %v: %v", path, m, err)
}
return rv
}
func jptrs(c context.Context, m *json.RawMessage, path string) string {
var rv string
err := jsonpointer.FindDecode([]byte(*m), path, &rv)
if err != nil {
log.Warningf(c, "Error decoding %q from %s: %v", path, m, err)
}
return rv
}
func jptrf(c context.Context, m *json.RawMessage, path string) float64 {
var rv interface{}
err := jsonpointer.FindDecode([]byte(*m), path, &rv)
if err != nil {
log.Warningf(c, "Error decoding %q from %s: %v", path, m, err)
}
switch t := rv.(type) {
case float64:
return t
case string:
f, err := strconv.ParseFloat(t, 64)
if err != nil {
log.Warningf(c, "Error parsing string as float64: %v: %v", t, err)
}
return f
default:
log.Warningf(c, "Unexpected type for %q: %T", path, t)
}
return 0
}
func handleIndexTunes(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
keys, err := decodeKeys(r.Body)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
for _, k := range keys {
tune, err := getTune(c, k)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if err := tune.uncompress(); err != nil {
http.Error(w, err.Error(), 500)
return
}
if err := indexDoc(c, tune); err != nil {
log.Errorf(c, "Error indexing: %v", err)
http.Error(w, err.Error(), 500)
return
}
}
}
func handleIndexUsage(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
keys, err := decodeKeys(r.Body)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
for _, k := range keys {
usage := &UsageStat{}
if err := datastore.Get(c, k, usage); err != nil {
log.Errorf(c, "Error fetching stat details: %v", err)
http.Error(w, err.Error(), 500)
return
}
usage.Key = k
if err := indexUsage(c, k.Encode(), usage); err != nil {
log.Errorf(c, "Error indexing: %v", err)
http.Error(w, err.Error(), 500)
return
}
}
}
func countSomeUsage(c context.Context, fckeys []*datastore.Key) error {
fcs := make([]*FoundController, len(fckeys))
if err := datastore.GetMulti(c, fckeys, fcs); err != nil {
return err
}
incrs := map[string]map[string]int64{}
var fckup []*datastore.Key
var fcup []*FoundController
for i, fc := range fcs {
if fc.Counted {
continue
}
fckup = append(fckup, fckeys[i])
fcup = append(fcup, fc)
fc.Counted = true
ds := fc.Oldest.Format(dayFmt)
if _, ok := incrs[ds]; !ok {
incrs[ds] = map[string]int64{}
}
incrs[ds][fc.Name]++
}
keys := []*datastore.Key{}
for k := range incrs {
keys = append(keys, datastore.NewKey(c, "DailyCounts", k, 0, nil))
}
if len(keys) == 0 {
log.Debugf(c, "Nothing to do")
return nil
}
counts := make([]DailyCounts, len(keys))
err := datastore.GetMulti(c, keys, counts)
if merr, ok := err.(appengine.MultiError); ok {
for i, e := range merr {
if e == datastore.ErrNoSuchEntity {
counts[i].Counts = map[string]int64{}
}
}
err = nil
}
if err != nil {
return err
}
for i, e := range counts {
i := incrs[keys[i].StringID()]
for k, v := range i {
e.Counts[k] += v
}
}
if len(fckup)+len(keys) == 0 {
log.Debugf(c, "Nothing to do")
return nil
}
log.Infof(c, "Updating %v FoundControllers and %v DailyCounts",
len(fckup), len(keys))
if len(keys) > 0 {
if _, err := datastore.PutMulti(c, keys, counts); err != nil {
return err
}
}
if len(fckup) > 0 {
if _, err := datastore.PutMulti(c, fckup, fcup); err != nil {
return err
}
}
return nil
}
func handleCountUsage(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
fckeys, err := decodeKeys(r.Body)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
grp, cc := errgroup.WithContext(c)
usageSem := make(chan bool, 3)
for len(fckeys) > 0 {
n := 10
if n > len(fckeys) {
n = len(fckeys)
}
todo := fckeys[:n]
grp.Go(func() error {
// Rate limit this to avoid excessive transaction contention
usageSem <- true
defer func() { <-usageSem }()
return datastore.RunInTransaction(cc, func(tc context.Context) error {
return countSomeUsage(tc, todo)
}, &datastore.TransactionOptions{XG: true, Attempts: 10})
})
fckeys = fckeys[n:]
}
if err := grp.Wait(); err != nil {
log.Errorf(c, "Error counting usage: %v", err)
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(204)
}
func handleClearCountFlag(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
fckeys, err := decodeKeys(r.Body)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
grp, cc := errgroup.WithContext(c)
for len(fckeys) > 0 {
n := 10
if n > len(fckeys) {
n = len(fckeys)
}
todo := fckeys[:n]
grp.Go(func() error {
return datastore.RunInTransaction(cc, func(tc context.Context) error {
fcs := make([]*FoundController, len(todo))
if err := datastore.GetMulti(c, todo, fcs); err != nil {
return err
}
for _, fc := range fcs {
fc.Counted = false
}
if _, err := datastore.PutMulti(c, todo, fcs); err != nil {
return err
}
return nil
}, &datastore.TransactionOptions{XG: true, Attempts: 10})
})
fckeys = fckeys[n:]
}
if err := grp.Wait(); err != nil {
log.Errorf(c, "Error counting usage: %v", err)
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(204)
}
func handleProcessUsage(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
keys, err := decodeKeys(r.Body)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
log.Debugf(c, "Got %v keys to process", len(keys))
stats := make([]UsageStat, len(keys))
err = datastore.GetMulti(c, keys, stats)
if err != nil {
log.Errorf(c, "Error grabbing all the stats from %v: %v", keys, err)
http.Error(w, err.Error(), 500)
return
}
grp, _ := errgroup.WithContext(c)
var tasks []*taskqueue.Task
total := 0
for _, st := range stats {
err = st.uncompress()
if err != nil {
log.Warningf(c, "Failed to decompress record: %v", err)
continue
}
rm := json.RawMessage(st.Data)
data := &asyncUsageData{
IP: st.Addr,
Country: st.Country,
Region: st.Region,
City: st.City,
Lat: st.Lat,
Lon: st.Lon,
Timestamp: st.Timestamp,
RawData: &rm,
}
j, err := json.Marshal(data)
if err != nil {
log.Infof(c, "Error marshaling input: %v", err)
continue
}
g, err := gz(j)
if err != nil {
log.Infof(c, "Error compressing input: %v", err)
continue
}
tasks = append(tasks, &taskqueue.Task{
Path: "/batch/asyncRollup",
Payload: g,
})
if len(tasks) == 100 {
todo := tasks
grp.Go(func() error {
_, err := taskqueue.AddMulti(c, todo, "asyncRollupBE")
return err
})
tasks = nil
log.Debugf(c, "Added a batch of 100")
}
total++
}
if tasks != nil {
grp.Go(func() error {
_, err := taskqueue.AddMulti(c, tasks, "asyncRollupBE")
return err
})
log.Debugf(c, "Added a batch of %v", len(tasks))
}
if err := grp.Wait(); err != nil {
log.Errorf(c, "Error queueing stuff: %v", err)
http.Error(w, "error queueing", 500)
return
}
log.Debugf(c, "Queued %v entries for batch processing", total)
w.WriteHeader(202)
}