This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
/
syncUserHandler.go
1233 lines (1056 loc) · 32.1 KB
/
syncUserHandler.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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package web
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"path"
"strconv"
"strings"
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/pkg/errors"
"github.com/gorilla/mux"
"github.com/mozilla-services/go-syncstorage/syncstorage"
)
type SyncUserHandlerConfig struct {
// API Limits
MaxRequestBytes int
MaxPOSTRecords int
MaxPOSTBytes int
MaxTotalRecords int
MaxTotalBytes int
MaxBatchTTL int
MaxRecordPayloadBytes int // largest BSO payload
}
func NewDefaultSyncUserHandlerConfig() *SyncUserHandlerConfig {
return &SyncUserHandlerConfig{
// API Limits
MaxRequestBytes: 2 * 1024 * 1024,
MaxPOSTRecords: 100,
MaxPOSTBytes: 2 * 1024 * 1024,
MaxTotalRecords: 10000,
MaxTotalBytes: 100 * 1024 * 1024,
MaxRecordPayloadBytes: 1024 * 1024 * 2,
// batches older than this are likely to be purged
MaxBatchTTL: 2 * 60 * 60 * 1000, // 2 hours in milliseconds
}
}
// SyncUserHandler provides all the sync 1.5 API routes for a single user.
// It implements http.Handler. It's design is kept simple on purpose
// to make it easy to wrap it in other http.Handler.
type SyncUserHandler struct {
StoppableHandler
requestLock sync.Mutex
router *mux.Router
uid string
db *syncstorage.DB
// Sync 1.5 tracks changes based on timestamps.
// The X-Last-Modified has an accuracy of 10's of milliseconds.
// Changes (POST, PUT, DELETE) require a unique timestamp so the handler
// must sleep for a bit to ensure all timestamps are unique. Also all changes
// need to be synchronized
lastChange time.Time
config *SyncUserHandlerConfig
}
func NewSyncUserHandler(uid string, db *syncstorage.DB, config *SyncUserHandlerConfig) *SyncUserHandler {
// https://docs.services.mozilla.com/storage/apis-1.5.html
r := mux.NewRouter()
if config == nil {
config = NewDefaultSyncUserHandlerConfig()
}
server := &SyncUserHandler{
uid: uid,
router: r,
db: db,
config: config,
}
// top level deletions for the user and their storage
// Note: not part of the sub-routers since since they don't end with a `/`
r.HandleFunc("/1.5/"+uid, server.hDeleteEverything).Methods("DELETE")
r.HandleFunc("/1.5/"+uid+"/storage", server.hDeleteEverything).Methods("DELETE")
v := r.PathPrefix("/1.5/" + uid + "/").Subrouter()
info := v.PathPrefix("/info/").Subrouter()
info.HandleFunc("/collections", server.hInfoCollections).Methods("GET")
info.HandleFunc("/collection_usage", server.hInfoCollectionUsage).Methods("GET")
info.HandleFunc("/collection_counts", server.hInfoCollectionCounts).Methods("GET")
info.HandleFunc("/configuration", server.hInfoConfiguration).Methods("GET")
info.HandleFunc("/quota", server.hInfoQuota).Methods("GET")
storage := v.PathPrefix("/storage/").Subrouter()
storage.HandleFunc("/{collection}", server.hCollectionGET).Methods("GET")
storage.HandleFunc("/{collection}", catchBadCrypto(server.hCollectionPOST)).Methods("POST")
storage.HandleFunc("/{collection}", server.hCollectionDELETE).Methods("DELETE")
storage.HandleFunc("/{collection}/{bsoId}", server.hBsoGET).Methods("GET")
storage.HandleFunc("/{collection}/{bsoId}", catchBadCrypto(server.hBsoPUT)).Methods("PUT")
storage.HandleFunc("/{collection}/{bsoId}", server.hBsoDELETE).Methods("DELETE")
return server
}
// TidyUp will purge expired BSOs and Batches. When the database has exceeded
// vacuumKB (in kilobytes) it will be optimized. This could
// potentially be a long operation as the database vacuumed needs to rewrite
// the entire database file
func (s *SyncUserHandler) TidyUp(minPurge, maxPurge time.Duration, vacuumKB int) (skipped bool, took time.Duration, err error) {
// Purge Expired BSOs
start := time.Now()
nextStr, err := s.db.GetKey("NEXT_PURGE")
if err != nil {
log.WithFields(log.Fields{
"uid": s.uid,
"err": err.Error(),
}).Error("SyncUserHandler - Error Fetching next purge time")
return true, time.Since(start), err
}
if nextStr != "" {
nextPurge, err := time.Parse(time.RFC3339Nano, nextStr)
if err != nil {
log.WithFields(log.Fields{
"uid": s.uid,
"err": err.Error(),
}).Error("SyncUserHandler - Error parsing NEXT_PURGE value")
// try to fix it for next time
s.db.SetKey("NEXT_PURGE", time.Now().Format(time.RFC3339Nano))
return true, time.Since(start), nil
}
if time.Now().Before(nextPurge) {
return true, took, nil
}
} else {
// never been purged, skip it and set it to the maxpurge time in the future
nextPurge := time.Now().Add(maxPurge)
err = s.db.SetKey("NEXT_PURGE", nextPurge.Format(time.RFC3339Nano))
return true, time.Since(start), err
}
logFields := log.Fields{
"uid": s.uid,
"db": path.Base(s.db.Path),
}
var freeKB int
var usage *syncstorage.DBPageStats
{ // purge bsos and batches
purgeStart := time.Now()
numBSOPurged, err := s.db.PurgeExpired()
if err != nil {
log.WithFields(log.Fields{
"uid": s.uid,
"err": err.Error(),
}).Error("SyncUserHandler - Error purging expired BSOs")
return true, time.Since(start), err
}
numBatchesPurged, err := s.db.BatchPurge(s.config.MaxBatchTTL)
if err != nil {
log.WithFields(log.Fields{
"uid": s.uid,
"err": err.Error(),
}).Error("SyncUserHandler - Error purging expired Batches")
return true, time.Since(start), err
}
usage, err = s.db.Usage()
if err != nil {
log.WithFields(log.Fields{
"uid": s.uid,
"err": err.Error(),
}).Error("SyncUserHandler - Error retrieving usage")
return true, time.Since(start), err
}
logFields["purge_bso"] = numBSOPurged
logFields["purge_batch"] = numBatchesPurged
logFields["purge_t"] = time.Since(purgeStart).Nanoseconds() / 1000 / 1000
freeKB = (usage.Free * usage.Size / 1024)
logFields["free_pages_kb"] = freeKB
}
{ // vacuum the db if there are too many free blocks
vacStart := time.Now()
if vacuumKB > 0 && freeKB >= vacuumKB {
if err = s.db.Vacuum(); err != nil {
log.WithFields(log.Fields{
"uid": s.uid,
"err": err.Error(),
}).Error("SyncUserHandler - Error Vacuuming DB")
return true, time.Since(start), err
}
after, err := s.db.Usage()
if err != nil {
log.WithFields(log.Fields{
"uid": s.uid,
"err": err.Error(),
}).Error("SyncUserHandler - Error retrieving usage after vacuum")
return true, time.Since(start), err
}
vacBeforeKB := usage.Total * usage.Size / 1024
vacAfterKB := after.Total * after.Size / 1024
logFields["vac"] = "yes"
logFields["vac_before_kb"] = vacBeforeKB
logFields["vac_after_kb"] = vacAfterKB
logFields["vac_delta_kb"] = vacBeforeKB - vacAfterKB
logFields["vac_t"] = time.Since(vacStart).Nanoseconds() / 1000 / 1000
}
}
{ // Schedule the next purge
deltaTime := minPurge
if maxPurge.Nanoseconds() > 0 {
deltaTime = time.Duration(rand.Int63n(maxPurge.Nanoseconds()))
}
if deltaTime < minPurge {
deltaTime = minPurge
}
nextPurge := time.Now().Add(deltaTime)
err = s.db.SetKey("NEXT_PURGE", nextPurge.Format(time.RFC3339Nano))
if err != nil {
log.WithFields(log.Fields{
"uid": s.uid,
"err": err.Error(),
}).Error("SyncUserHandler - Error Setting Next Purge Key")
return true, time.Since(start), err
}
}
took = time.Since(start)
logFields["t"] = took.Nanoseconds() / 1000 / 1000
log.WithFields(logFields).Info("SyncUserHandler - TidyUp")
return
}
func (s *SyncUserHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
s.requestLock.Lock()
defer s.requestLock.Unlock()
if s.IsStopped() {
s.StoppableHandler.ServeHTTP(w, req)
return
}
switch req.Method {
case "POST", "PUT", "DELETE":
// make sure all X-Last-Modified values are unique we sleep for a bit
var toSleep time.Duration
if s.lastChange.IsZero() {
// edge case race where db is closed by cleanup and
// reopened (new request) < 10ms later results in the same
// modified timestmap. Sleep just to be safe for all new changes
toSleep = 10 * time.Millisecond
} else {
toSleep = 11*time.Millisecond - time.Now().Sub(s.lastChange)
}
if toSleep > 0 {
if log.GetLevel() == log.DebugLevel {
log.WithFields(log.Fields{
"t_ms": toSleep,
"uid": s.uid,
"method": req.Method,
"p": req.RequestURI,
}).Debug("write-delay")
}
time.Sleep(toSleep)
}
s.router.ServeHTTP(w, req)
s.lastChange = time.Now()
default:
s.router.ServeHTTP(w, req)
}
}
// Stop immediately prevents handling web requests then purges
// expired BSOs before closing the DB.
func (s *SyncUserHandler) StopHTTP() {
s.requestLock.Lock()
defer s.requestLock.Unlock()
if s.IsStopped() {
return
}
s.StoppableHandler.StopHTTP()
s.db.Close()
if log.GetLevel() == log.DebugLevel {
log.WithFields(log.Fields{
"uid": s.uid,
}).Debug("syncUserHandler stopped")
}
}
// getcid looks up a collection by name and returns its id. If it doesn't
// exist it will create it if automake is true
func (s *SyncUserHandler) getcid(r *http.Request, automake bool) (cId int, err error) {
collection := mux.Vars(r)["collection"]
if !syncstorage.CollectionNameOk(collection) {
err = syncstorage.ErrInvalidCollectionName
return
}
cId, err = s.db.GetCollectionId(collection)
if err == nil {
return
}
if err == syncstorage.ErrNotFound && automake {
cId, err = s.db.CreateCollection(collection)
}
return
}
// hInfoQuota calculates the total disk space used by the user by calculating
// it based on the number of DB pages used * size of each page.
// TODO actually implement quotas in the system.
func (s *SyncUserHandler) hInfoQuota(w http.ResponseWriter, r *http.Request) {
results, err := s.db.InfoCollectionUsage()
if err != nil {
InternalError(w, r, err)
return
}
modified, err := s.db.LastModified()
if err != nil {
InternalError(w, r, err)
return
}
if sentNotModified(w, r, modified) {
return
}
used := 0
for _, bytes := range results {
used += bytes
}
m := syncstorage.ModifiedToString(modified)
w.Header().Set("X-Last-Modified", m)
w.Header().Set("Content-Type", "application/json")
// 8 decimals of precision cause python's test_quota functional test
w.Write([]byte(fmt.Sprintf("[%0.8f,null]", float64(used)/1024)))
}
func (s *SyncUserHandler) hInfoCollections(w http.ResponseWriter, r *http.Request) {
if !AcceptHeaderOk(w, r) {
return
}
info, err := s.db.InfoCollections()
if err != nil {
InternalError(w, r, err)
return
}
modified, err := s.db.LastModified()
if err != nil {
InternalError(w, r, err)
return
}
if sentNotModified(w, r, modified) {
return
}
m := syncstorage.ModifiedToString(modified)
w.Header().Set("X-Last-Modified", m)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, "{")
num := len(info)
for name, modified := range info {
fmt.Fprintf(w, `"%s":%s`, name, syncstorage.ModifiedToString(modified))
num--
if num != 0 {
fmt.Fprint(w, ",")
}
}
fmt.Fprint(w, "}")
}
func (s *SyncUserHandler) hInfoCollectionUsage(w http.ResponseWriter, r *http.Request) {
if !AcceptHeaderOk(w, r) {
return
}
modified, err := s.db.LastModified()
if err != nil {
InternalError(w, r, err)
return
}
if sentNotModified(w, r, modified) {
return
}
if results, err := s.db.InfoCollectionUsage(); err != nil {
InternalError(w, r, err)
return
} else {
// the sync 1.5 api says data should be in KB
resultsKB := make(map[string]float64)
for name, bytes := range results {
resultsKB[name] = float64(bytes) / 1024
}
m := syncstorage.ModifiedToString(modified)
w.Header().Set("X-Last-Modified", m)
JsonNewline(w, r, resultsKB)
}
}
func (s *SyncUserHandler) hInfoCollectionCounts(w http.ResponseWriter, r *http.Request) {
if !AcceptHeaderOk(w, r) {
return
}
results, err := s.db.InfoCollectionCounts()
if err != nil {
InternalError(w, r, err)
return
}
modified, err := s.db.LastModified()
if err != nil {
InternalError(w, r, err)
return
}
if sentNotModified(w, r, modified) {
return
}
m := syncstorage.ModifiedToString(modified)
w.Header().Set("X-Last-Modified", m)
JsonNewline(w, r, results)
}
func (s *SyncUserHandler) hInfoConfiguration(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{
"max_post_records":%d,
"max_post_bytes":%d,
"max_total_records":%d,
"max_total_bytes":%d,
"max_request_bytes":%d,
"max_record_payload_bytes":%d}`,
s.config.MaxPOSTRecords,
s.config.MaxPOSTBytes,
s.config.MaxTotalRecords,
s.config.MaxTotalBytes,
s.config.MaxRequestBytes,
s.config.MaxRecordPayloadBytes,
)
}
func (s *SyncUserHandler) hCollectionGET(w http.ResponseWriter, r *http.Request) {
if !AcceptHeaderOk(w, r) {
return
}
// query params that control searching
var (
err error
ids []string
newer int
older int
full bool
limit int
offset int
sort = syncstorage.SORT_NEWEST
)
cId, err := s.getcid(r, false)
if err != nil {
if err == syncstorage.ErrNotFound {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("[]"))
return
} else {
InternalError(w, r, err)
return
}
}
if err = r.ParseForm(); err != nil {
sendRequestProblem(w, r, http.StatusBadRequest, errors.Wrap(err, "Bad query parameters"))
return
}
if v := r.Form.Get("ids"); v != "" {
ids = strings.Split(v, ",")
if len(ids) > 100 {
sendRequestProblem(w, r, http.StatusBadRequest, errors.New("Too many ids provided"))
return
}
for i, id := range ids {
id = strings.TrimSpace(id)
if syncstorage.BSOIdOk(id) {
ids[i] = id
} else {
sendRequestProblem(w, r, http.StatusBadRequest, errors.Errorf("Invalid bso id %s", id))
return
}
}
}
// we expect to get sync's two decimal timestamps, these need
// to be converted to milliseconds
if v := r.Form.Get("older"); v != "" {
floatNew, err := strconv.ParseFloat(v, 64)
if err != nil {
sendRequestProblem(w, r, http.StatusBadRequest, errors.Wrap(err, "Invalid older param format"))
return
}
older = int(floatNew * 1000)
if !syncstorage.NewerOk(newer) {
sendRequestProblem(w, r, http.StatusBadRequest, errors.New("Invalid older value"))
return
}
} else {
older = syncstorage.MaxTimestamp
}
if v := r.Form.Get("newer"); v != "" {
floatNew, err := strconv.ParseFloat(v, 64)
if err != nil {
sendRequestProblem(w, r, http.StatusBadRequest, errors.Wrap(err, "Invalid newer param format"))
return
}
newer = int(floatNew * 1000)
if !syncstorage.NewerOk(newer) {
sendRequestProblem(w, r, http.StatusBadRequest, errors.New("Invalid newer value"))
return
}
}
if v := r.Form.Get("full"); v != "" {
full = true
}
if v := r.Form.Get("limit"); v != "" {
limit, err = strconv.Atoi(v)
if err != nil || limit < 0 {
errMessage := "Invalid limit value"
if err != nil {
err = errors.Wrap(err, errMessage)
} else {
err = errors.New(errMessage)
}
sendRequestProblem(w, r, http.StatusBadRequest, err)
return
}
} else {
// in sqlite a negative value for LIMIT results in
// no upper bound, ref: http://sqlite.org/lang_select.html#limitoffset
limit = -1
}
if v := r.Form.Get("offset"); v != "" {
offset, err = strconv.Atoi(v)
if err != nil || !syncstorage.OffsetOk(offset) {
errMessage := "Invalid offset value"
if err != nil {
err = errors.Wrap(err, errMessage)
} else {
err = errors.New(errMessage)
}
sendRequestProblem(w, r, http.StatusBadRequest, err)
return
}
}
if v := r.Form.Get("sort"); v != "" {
switch v {
case "newest":
sort = syncstorage.SORT_NEWEST
case "oldest":
sort = syncstorage.SORT_OLDEST
case "index":
sort = syncstorage.SORT_INDEX
default:
sendRequestProblem(w, r, http.StatusBadRequest, errors.New("Invalid sort value"))
return
}
}
// this is way down here since IO is more expensive
// than parsing if the GET params are valid
cmodified, err := s.db.GetCollectionModified(cId)
if err != nil {
InternalError(w, r, err)
return
} else if sentNotModified(w, r, cmodified) {
return
}
results, err := s.db.GetBSOs(cId, ids, older, newer, sort, limit, offset)
if err != nil {
InternalError(w, r, err)
return
}
m := syncstorage.ModifiedToString(cmodified)
w.Header().Set("X-Last-Modified", m)
w.Header().Set("X-Weave-Records", strconv.Itoa(len(results.BSOs)))
if results.More {
w.Header().Set("X-Weave-Next-Offset", strconv.Itoa(results.Offset))
}
if full {
JsonNewline(w, r, results.BSOs)
} else {
bsoIds := make([]string, len(results.BSOs))
for i, b := range results.BSOs {
bsoIds[i] = b.Id
}
JsonNewline(w, r, bsoIds)
}
}
func (s *SyncUserHandler) hCollectionPOST(w http.ResponseWriter, r *http.Request) {
// accept text/plain from old (broken) clients
ct := getMediaType(r.Header.Get("Content-Type"))
if ct != "application/json" && ct != "text/plain" && ct != "application/newlines" {
sendRequestProblem(w, r, http.StatusUnsupportedMediaType, errors.Errorf("Not acceptable Content-Type: %s", ct))
return
}
cId, err := s.getcid(r, true) // automake the collection if it doesn't exit
if err != nil {
if err == syncstorage.ErrInvalidCollectionName {
sendRequestProblem(w, r, http.StatusBadRequest, errors.Wrap(err, "Invalid collection name"))
} else {
InternalError(w, r, err)
}
return
}
// handle X-If-Unmodified-Since and X-If-Modified-Since
cmodified, err := s.db.GetCollectionModified(cId)
if err != nil {
InternalError(w, r, err)
return
} else if sentNotModified(w, r, cmodified) {
return
}
batchFound, batchId, batchCommit := GetBatchIdAndCommit(r)
if batchCommit && !batchFound {
sendRequestProblem(w, r, http.StatusBadRequest, errors.New("Batch ID expected with commit"))
return
} else if batchId != "" || (batchId == "true" && batchCommit == false) {
s.hCollectionPOSTBatch(cId, w, r)
} else {
s.hCollectionPOSTClassic(cId, w, r)
}
}
// hCollectionPOSTClassic is the historical POST handling logic prior to
// the addition of atomic commits from multiple POST requests
func (s *SyncUserHandler) hCollectionPOSTClassic(collectionId int, w http.ResponseWriter, r *http.Request) {
bsoToBeProcessed, results, err := RequestToPostBSOInput(r, s.config.MaxRecordPayloadBytes)
if err != nil {
WeaveInvalidWBOError(w, r, errors.Wrap(err, "Failed turning POST body into BSO work list"))
return
}
if len(bsoToBeProcessed) > s.config.MaxPOSTRecords {
sendRequestProblem(w, r, http.StatusRequestEntityTooLarge,
errors.Errorf("Exceed %d BSO per request", s.config.MaxPOSTRecords))
return
}
// Send the changes to the database and merge
// with `results` above
postResults, err := s.db.PostBSOs(collectionId, bsoToBeProcessed)
if err != nil {
InternalError(w, r, err)
} else {
for bsoId, failMessage := range postResults.Failed {
results.Failed[bsoId] = failMessage
}
w.Header().Set("X-Last-Modified", syncstorage.ModifiedToString(postResults.Modified))
JsonNewline(w, r, &PostResults{
Modified: postResults.Modified,
Success: postResults.Success,
Failed: results.Failed,
})
}
}
// hCollectionPOSTBatch handles batch=? requests. It is called internally by hCollectionPOST
// to handle batch request logic
func (s *SyncUserHandler) hCollectionPOSTBatch(collectionId int, w http.ResponseWriter, r *http.Request) {
// CHECK client provided headers to quickly determine if batch exceeds limits
// this is meant to be a cheap(er) check without actually having to parse the
// data provided by the user
for _, headerName := range []string{"X-Weave-Total-Records", "X-Weave-Total-Bytes", "X-Weave-Records", "X-Weave-Bytes"} {
if strVal := r.Header.Get(headerName); strVal != "" {
if intVal, err := strconv.Atoi(strVal); err == nil {
max := 0
switch headerName {
case "X-Weave-Total-Records":
max = s.config.MaxTotalRecords
case "X-Weave-Total-Bytes":
max = s.config.MaxTotalBytes
case "X-Weave-Bytes":
max = s.config.MaxPOSTBytes
case "X-Weave-Records":
max = s.config.MaxPOSTRecords
}
if intVal > max {
WeaveSizeLimitExceeded(w, r,
errors.Errorf("Limit %s exceed. %d/%d", headerName, intVal, max))
return
}
} else {
// header value is invalid (not an int)
sendRequestProblem(w, r, http.StatusBadRequest, errors.Errorf("Invalid integer value for %s", headerName))
return
}
}
}
// CHECK the POST size, if possible from client supplied data
// hopefully shortcut a fail if this exceeds limits
if r.ContentLength > 0 && r.ContentLength > int64(s.config.MaxPOSTBytes) {
WeaveSizeLimitExceeded(w, r,
errors.Errorf("MaxPOSTBytes exceeded in request.ContentLength(%d) > %d",
r.ContentLength, s.config.MaxPOSTBytes))
return
}
// EXTRACT actual data to check
bsoToBeProcessed, results, err := RequestToPostBSOInput(r, s.config.MaxRecordPayloadBytes)
if err != nil {
WeaveInvalidWBOError(w, r, errors.Wrap(err, "Failed turning POST body into BSO work list"))
return
}
// CHECK actual BSOs sent to see if they exceed limits
if len(bsoToBeProcessed) > s.config.MaxPOSTRecords {
sendRequestProblem(w, r, http.StatusRequestEntityTooLarge,
errors.Errorf("Exceeded %d BSO per request", s.config.MaxPOSTRecords))
return
}
// CHECK BSO decoding validation errors. Don't even start a Batch if there are.
if len(results.Failed) > 0 {
modified, err := s.db.LastModified()
if err != nil {
InternalError(w, r, err)
return
}
w.Header().Set("X-Last-Modified", syncstorage.ModifiedToString(modified))
JsonNewline(w, r, &PostResults{
Modified: modified,
Success: nil,
Failed: results.Failed,
})
return
}
// Get batch id, commit command and internal collection Id
_, batchId, batchCommit := GetBatchIdAndCommit(r)
// CHECK batch id is valid for appends. Do this before loading and decoding
// the body to be more efficient.
if batchId != "true" {
id, err := batchIdInt(batchId)
if err != nil {
sendRequestProblem(w, r, http.StatusBadRequest, errors.Wrap(err, "Invalid Batch ID Format"))
return
}
if found, err := s.db.BatchExists(id, collectionId); err != nil {
InternalError(w, r, err)
} else if !found {
sendRequestProblem(w, r, http.StatusBadRequest,
errors.Errorf("Batch id: %s does not exist", batchId))
}
}
filteredBSOs := make([]*syncstorage.PutBSOInput, 0, len(bsoToBeProcessed))
failures := make(map[string][]string)
for _, putInput := range bsoToBeProcessed {
var failId string
var failReason string
if !syncstorage.BSOIdOk(putInput.Id) {
failId = "na"
failReason = fmt.Sprintf("Invalid BSO id %s", putInput.Id)
} else {
if putInput.SortIndex != nil && !syncstorage.SortIndexOk(*putInput.SortIndex) {
failId = putInput.Id
failReason = fmt.Sprintf("Invalid sort index for: %s", putInput.Id)
}
if putInput.TTL != nil && !syncstorage.TTLOk(*putInput.TTL) {
failId = putInput.Id
failReason = fmt.Sprintf("Invalid TTL for: %s", putInput.Id)
}
}
if failReason != "" {
if failures[failId] == nil {
failures[failId] = []string{failReason}
} else {
failures[failId] = append(failures[failId], failReason)
}
continue
}
filteredBSOs = append(filteredBSOs, putInput)
}
// JSON Serialize the data for storage in the DB
buf := new(bytes.Buffer)
if len(filteredBSOs) > 0 {
encoder := json.NewEncoder(buf)
for _, bso := range filteredBSOs {
if err := encoder.Encode(bso); err != nil { // Note: this writes a newline after each record
// whoa... presumably should never happen
InternalError(w, r, errors.Wrap(err, "Failed encoding BSO for payload"))
return
}
}
}
// Save either as a new batch or append to an existing batch
var dbBatchId int
// - batchIdInt used to track the internal batchId number in the database after
// - the create || append
appendedOkIds := make([]string, 0, len(filteredBSOs))
if batchId == "true" {
newBatchId, err := s.db.BatchCreate(collectionId, buf.String())
if err != nil {
InternalError(w, r, errors.Wrap(err, "Failed creating batch"))
return
}
dbBatchId = newBatchId
} else {
id, err := batchIdInt(batchId)
if err != nil {
sendRequestProblem(w, r, http.StatusBadRequest, errors.Wrap(err, "Invalid Batch ID Format"))
return
}
if len(filteredBSOs) > 0 { // append only if something to do
if err := s.db.BatchAppend(id, collectionId, buf.String()); err != nil {
InternalError(w, r, errors.Wrap(err, fmt.Sprintf("Failed append to batch id:%d", dbBatchId)))
return
}
}
dbBatchId = id
}
// The success list only includes the BSO Ids in the
// POST request. These need to kept to be sent back in
// the response. Matches the python server's behaviour / passes
// the tests
for _, bso := range filteredBSOs {
appendedOkIds = append(appendedOkIds, bso.Id)
}
if batchCommit {
batchRecord, err := s.db.BatchLoad(dbBatchId, collectionId)
if err != nil {
InternalError(w, r, errors.Wrap(err, "Failed Loading Batch to commit"))
return
}
rawJSON := ReadNewlineJSON(bytes.NewBufferString(batchRecord.BSOS))
// CHECK final data before committing it to the database
numInBatch := len(rawJSON)
if numInBatch > s.config.MaxTotalRecords {
s.db.BatchRemove(dbBatchId)
WeaveSizeLimitExceeded(w, r,
errors.Errorf("Too many BSOs (%d) in Batch(%d)", numInBatch, dbBatchId))
return
}
postData := make(syncstorage.PostBSOInput, len(rawJSON), len(rawJSON))
for i, bsoJSON := range rawJSON {
var bso syncstorage.PutBSOInput
if parseErr := parseIntoBSO(bsoJSON, &bso); parseErr != nil {
// well there is definitely a bug somewhere if this happens
InternalError(w, r, errors.Wrap(parseErr, "Could not decode batch data"))
return
} else {
postData[i] = &bso
}
}
// CHECK that actual Batch data size
sum := 0
for _, bso := range postData {
if bso.Payload == nil {
continue
}
sum := sum + len(*bso.Payload)
if sum > s.config.MaxTotalBytes {
s.db.BatchRemove(dbBatchId)
WeaveSizeLimitExceeded(w, r,
errors.Errorf("Batch size(%d) exceeded MaxTotalBytes limit(%d)",
sum, s.config.MaxTotalBytes))
return
}
}
postResults, err := s.db.PostBSOs(collectionId, postData)
if err != nil {
InternalError(w, r, err)
return
}
// merge failures
for key, reasons := range postResults.Failed {
if failures[key] == nil {
failures[key] = reasons
} else {
failures[key] = append(failures[key], reasons...)
}
}
// DELETE the batch from the DB
s.db.BatchRemove(dbBatchId)
w.Header().Set("X-Last-Modified", syncstorage.ModifiedToString(postResults.Modified))
JsonNewline(w, r, &PostResults{
Modified: postResults.Modified,
Success: appendedOkIds,
Failed: failures,
})
} else {
// https://bugzilla.mozilla.org/show_bug.cgi?id=1324600#c11
// batch create/append are not considered real writes until they are
// committed. In this case, send the collection's last modified timestamp
// for X-Last-Modified
modified, err := s.db.GetCollectionModified(collectionId)
if err != nil {
InternalError(w, r, errors.Wrap(err, "Failed getting modified ts for batch create/append"))
return
}
w.Header().Set("X-Last-Modified", syncstorage.ModifiedToString(modified))
JsonNewlineStatus(w, r, http.StatusAccepted, &PostResults{
Batch: batchIdString(dbBatchId),
Modified: modified,
Success: appendedOkIds,
Failed: failures,
})
}