forked from journeymidnight/radoshttpd
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
1058 lines (902 loc) · 25.6 KB
/
main.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
/* deanraccoon@gmail.com */
/* vim: set ts=4 shiftwidth=4 smarttab noet : */
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/codegangsta/martini"
"github.com/thesues/radoshttpd/rados"
"github.com/thesues/radoshttpd/nettimeout"
"io"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
"encoding/hex"
"container/list"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"bufio"
"bitbucket.org/wenjianhn/chunkaligned"
"bitbucket.org/wenjianhn/wugui"
)
var (
LOGPATH = "/var/log/wuzei/wuzei.log"
PIDFILE = "/var/run/wuzei/wuzei.pid"
WHITELISTPATH = "/etc/wuzei/whitelist"
QUEUETIMEOUT time.Duration = 5 /* seconds */
MONTIMEOUT = "30"
OSDTIMEOUT = "60"
BUFFERSIZE = 4 << 20 /* 4M */
AIOCONCURRENT = 4
MAX_CHUNK_SIZE = BUFFERSIZE * 2
STRIPE_UNIT = uint(512 << 10) /* 512K */
OBJECT_SIZE = uint(4 << 20) /* 4M */
STRIPE_COUNT = uint(4)
SECRET = ""
/* global variables */
slog *log.Logger
ReqQueue RequestQueue
wg sync.WaitGroup
/* IP address which could access anytime */
/* ignore any line prefixed by # */
whiteList map[string]int
/* blocked object list */
blackList *SafeMap
urlRecord *URLRecord
)
type Record struct {
last_access_time time.Time
num_of_access int
pos_in_list *list.Element
}
type URLRecord struct {
records map[string] *Record
lock * sync.Mutex
evictList *list.List
max_record_size int
}
func NewURLRecord() *URLRecord {
r := make(map[string] *Record)
l := new(sync.Mutex)
e := list.New()
size := 1000
return &URLRecord{r,l,e,size}
}
func (url_record *URLRecord) update_and_check(url string) bool {
url_record.lock.Lock()
defer url_record.lock.Unlock()
var we_are_attacked bool = false
var entry *Record
var ok bool
current_time := time.Now()
//Has record
if entry, ok = url_record.records[url]; ok {
//new request within 10s
if entry.last_access_time.Add(time.Duration(cfg.ThrottleInterval) * time.Second).After(current_time) {
entry.num_of_access += 1
if entry.num_of_access > cfg.ThrottleNums {
we_are_attacked = true
}
//new request outof 10s, re-caculate the time
} else {
entry.num_of_access = 1
entry.last_access_time = current_time
}
//update ElevictList
url_record.evictList.MoveToFront(entry.pos_in_list)
//Add new record
} else {
new_entry := &Record{current_time, 1, nil}
var pos *list.Element = url_record.evictList.PushFront(url)
//After new_entry is inserted into list, put its pointer to entry it self
new_entry.pos_in_list = pos
url_record.records[url] = new_entry
}
//evicte old map
if url_record.evictList.Len() > url_record.max_record_size {
i := 0
for i < url_record.max_record_size / 2 {
element := url_record.evictList.Back()
url = url_record.evictList.Remove(element).(string)
slog.Printf("evict old entry %s", url)
delete(url_record.records, url)
i ++
}
}
return we_are_attacked;
}
type RadosDownloader struct {
striper *rados.StriperPool
soid string
offset int64
buffer []byte
waterhighmark int
waterlowmark int
}
type SimpleRadosDownloader struct {
*RadosDownloader
}
func (rd *SimpleRadosDownloader) Read(p []byte) (n int, err error) {
count, err := rd.RadosDownloader.striper.Read(rd.RadosDownloader.soid, p, uint64(rd.RadosDownloader.offset))
if count == 0 {
return 0, io.EOF
}
rd.RadosDownloader.offset += int64(count)
return count, err
}
func (rd *SimpleRadosDownloader) Seek(offset int64, whence int) (int64, error) {
return rd.RadosDownloader.Seek(offset, whence)
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func (rd *RadosDownloader) Read(p []byte) (n int, err error) {
var count int = 0
/* local buffer is empty */
if rd.waterhighmark == rd.waterlowmark {
count, err = rd.striper.Read(rd.soid, rd.buffer, uint64(rd.offset))
if count == 0 {
return 0, io.EOF
}
/* Timeout or read error occurs */
if err != nil {
slog.Println("timeout when reading ceph object");
return count, errors.New("Timeout or Read Error")
}
rd.offset += int64(count)
rd.waterhighmark = count
rd.waterlowmark = 0
}
l := len(p)
if l <= rd.waterhighmark-rd.waterlowmark {
copy(p, rd.buffer[rd.waterlowmark:rd.waterlowmark+l])
rd.waterlowmark += l
return l, nil
} else {
copy(p, rd.buffer[rd.waterlowmark:rd.waterhighmark])
rd.waterlowmark = rd.waterhighmark
return rd.waterhighmark - rd.waterlowmark, nil
}
}
func (rd *RadosDownloader) Seek(offset int64, whence int) (int64, error) {
switch whence {
case 0:
rd.offset = offset
return offset, nil
case 1:
rd.offset += offset
return rd.offset, nil
case 2:
size, _, err := rd.striper.State(rd.soid)
if err != nil {
return 0, nil
}
rd.offset = int64(size)
return rd.offset, nil
default:
return 0, errors.New("failed to seek")
}
}
/* RequestQueue has two functions */
/* 2. slot is used to queue write/read request */
type RequestQueue struct {
slots chan bool
}
func (r *RequestQueue) Init(queueLength int) {
r.slots = make(chan bool, queueLength)
}
func (r *RequestQueue) inc() error {
select {
case <-time.After(QUEUETIMEOUT * time.Second):
return errors.New("Queue is too long, timeout")
case r.slots <- true:
/* write to channel to get a slot for writing/reading rados object */
}
return nil
}
func (r *RequestQueue) dec() {
<-r.slots
}
func (r *RequestQueue) size() int {
return len(r.slots)
}
//use HMAC
func AuthMe(key string) martini.Handler {
return func(res http.ResponseWriter, req *http.Request, c martini.Context) {
/* allow all GET */
if req.Method == "GET" || req.Method == "HEAD" {
return
}
auth := req.Header.Get("Authorization")
mac := hmac.New(sha1.New, []byte(key))
mac.Write([]byte(req.URL.Path))
expected := base64.StdEncoding.EncodeToString(mac.Sum(nil))
if !SecureCompare(auth, expected) {
slog.Println("URL:", req.URL, "expected key is %s, but received key is %s", expected, auth);
ErrorHandler(res, req, http.StatusUnauthorized)
}
}
}
func WrapBytesCounter() martini.Handler {
return func(res http.ResponseWriter, req *http.Request, c martini.Context) {
r := &BytesCounter{0}
c.Map(r)
}
}
//any object is retrived too many times, block all the request except whiteList
//return false to let request go
//return true to block request
func DDosProtect(w http.ResponseWriter, r *http.Request, conn *rados.Conn) bool{
remote_addr_parts := strings.Split(r.RemoteAddr, ":")
remote_addr := remote_addr_parts[0]
if _, ok := whiteList[remote_addr]; ok{
return false
}
url := r.RequestURI
if blackList.Check(url) {
rejectConnection(w)
return true
} else if urlRecord.update_and_check(url){
blackList.Set(url, time.Now())
rejectConnection(w)
return true
} else {
return false
}
}
func rejectConnection(w http.ResponseWriter){
hj, ok := w.(http.Hijacker)
if !ok {
slog.Println("webserver doesn't support hijacking")
return
}
conn, _, err := hj.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
conn.Close()
}
func isCacheable(size int64) bool {
// NOTE(wenjianhn): only cache small files now
// We may need to cache file whose name contains specific suffix/characters.
if size <= int64(cfg.CacheMaxObjectSizeKBytes) * 1024 {
return true
}
return false
}
func RequestLimit() martini.Handler {
return func(w http.ResponseWriter, r *http.Request, c martini.Context){
/* used for graceful stop */
wg.Add(1)
defer wg.Done()
/* limit the max request */
if err := ReqQueue.inc(); err != nil {
slog.Println("URL:", r.URL, ", request timeout")
ErrorHandler(w, r, http.StatusRequestTimeout)
return
}
defer ReqQueue.dec()
c.Next()
}
}
func GetHandler(params martini.Params, w http.ResponseWriter, r *http.Request, conn *rados.Conn, counter *BytesCounter) {
if cfg.DDos && DDosProtect(w,r,conn) {
slog.Printf("See %s, Blacklist it", r.URL)
return
}
poolname := params["pool"]
soid := params["soid"]
pool, err := conn.OpenPool(poolname)
if err != nil {
slog.Println("URL:", r.URL, "open pool failed")
ErrorHandler(w, r, http.StatusNotFound)
return
}
defer pool.Destroy()
striper, err := pool.CreateStriper()
if err != nil {
slog.Println("URL:", r.URL, "Create Striper failed")
ErrorHandler(w, r, http.StatusNotFound)
return
}
defer striper.Destroy()
filename := fmt.Sprintf("%s", soid)
size, _, err := striper.State(soid)
if err != nil {
slog.Println("URL:", r.URL, "failed to get object " + soid)
ErrorHandler(w, r, http.StatusNotFound)
return
}
if isCacheable(int64(size)) {
rr := wugui.NewRadosReaderAt(&striper, poolname, filename, int64(size))
content, err := chunkaligned.NewChunkAlignedReaderAt(&rr, cfg.CacheChunkSizeKBytes * 1024)
if err != nil {
slog.Println(err)
ErrorHandler(w, r, http.StatusInternalServerError)
return
}
readSeeker := io.NewSectionReader(content, 0, content.Size())
ServeContent(w, r, filename, readSeeker, counter)
return
}
rd := RadosDownloader{&striper, soid, 0, nil, 0, 0}
srd := &SimpleRadosDownloader{&rd}
/* set let ServerContent to detect file type */
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
w.Header().Set("Content-Length", fmt.Sprintf("%d", size))
/* set the stream */
/* here we need more data */
ServeContent(w, r, filename, srd, counter)
}
func BlockHandler(params martini.Params, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("{\"blocksize\":%d}", MAX_CHUNK_SIZE * AIOCONCURRENT)))
}
func Md5sumHandler(params martini.Params, w http.ResponseWriter, r *http.Request, conn *rados.Conn) {
poolname := params["pool"]
soid := params["soid"]
pool, err := conn.OpenPool(poolname)
if err != nil {
slog.Println("URL:", r.URL, "open pool failed")
ErrorHandler(w, r, http.StatusNotFound)
return
}
defer pool.Destroy()
striper, err := pool.CreateStriper()
if err != nil {
slog.Println("URL:", r.URL, "Create Striper failed")
ErrorHandler(w, r, http.StatusNotFound)
return
}
defer striper.Destroy()
defer striper.Flush()
var offset int64 = 0
var start, end int64 = 0, 0
var count,l int = 0, 0
/* header format: Range:bytes 0-99 */
bytesRange := r.Header.Get("Range")
if bytesRange != "" {
bytesRange = strings.Trim(bytesRange, "bytes")
bytesRange = strings.TrimSpace(bytesRange)
o := strings.Split(bytesRange, "-")
start, err = strconv.ParseInt(o[0], 10, 64)
if err != nil {
slog.Println("URL:", r.URL, "parse Content-Range failed %s", bytesRange)
ErrorHandler(w, r, http.StatusBadRequest)
return
}
end, err = strconv.ParseInt(o[1], 10, 64)
if err != nil {
slog.Println("URL:", r.URL, "parse Content-Range failed %s", bytesRange)
ErrorHandler(w, r, http.StatusBadRequest)
return
}
offset = start
}
md5_ctx,_ := MD5New()
buf := make([]byte, BUFFERSIZE)
for offset <= end || bytesRange == "" {
count, err = striper.Read(soid, buf, uint64(offset))
if err != nil {
slog.Println("URL:", r.URL, "failed to read data for md5sum")
ErrorHandler(w, r, 404)
return
}
if count == 0 {
break
}
/* Handle striper.read more data than expected when having Range Header*/
if bytesRange != "" && offset + int64(count) > end {
l = int(end - offset) + 1
} else {
l = count
}
if err = md5_ctx.Update(buf[:l]); err != nil {
slog.Println("URL:", r.URL, "calc md5sum failed")
ErrorHandler(w, r, 501)
return
}
offset += int64(count)
}
var b []byte
if b, err = md5_ctx.Final(); err != nil {
slog.Println("URL:", r.URL, "calc md5sum failed")
ErrorHandler(w, r, 501)
return
}
s := hex.EncodeToString(b)
w.Write([]byte(fmt.Sprintf("{\"md5\":\"%s\"}", s)))
}
func CephStatusHandler(params martini.Params, w http.ResponseWriter, r *http.Request, conn *rados.Conn) {
c, err := conn.Status()
if err != nil{
ErrorHandler(w, r, 504)
return
}
w.Write([]byte(c))
}
func InfoHandler(params martini.Params, w http.ResponseWriter, r *http.Request, conn *rados.Conn) {
poolname := params["pool"]
soid := params["soid"]
pool, err := conn.OpenPool(poolname)
if err != nil {
slog.Println("URL:", r.URL, "open pool failed")
ErrorHandler(w, r, http.StatusNotFound)
return
}
defer pool.Destroy()
striper, err := pool.CreateStriper()
if err != nil {
slog.Println("URL:", r.URL, "Create Striper failed")
ErrorHandler(w, r, http.StatusNotFound)
return
}
defer striper.Destroy()
size, _, err := striper.State(soid)
if err != nil {
slog.Println("URL:%s, failed to get object " + soid, r.URL)
ErrorHandler(w, r, http.StatusNotFound)
return
}
/* use json format */
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(fmt.Sprintf("{\"size\":%d}", size)))
return
}
func DeleteHandler(params martini.Params, w http.ResponseWriter, r *http.Request, conn *rados.Conn) {
poolname := params["pool"]
soid := params["soid"]
pool, err := conn.OpenPool(poolname)
if err != nil {
slog.Println("URL:", r.URL, "open pool failed")
ErrorHandler(w, r, http.StatusNotFound)
return
}
defer pool.Destroy()
striper, err := pool.CreateStriper()
if err != nil {
slog.Println("URL:", r.URL, "Create Striper failed")
ErrorHandler(w, r, http.StatusNotFound)
return
}
defer striper.Destroy()
err = striper.Delete(soid)
if err != nil {
slog.Println("URL:", r.URL, "delete object %s/%s failed\n", poolname, soid)
ErrorHandler(w, r, http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
}
/* Function name 'pending_has_completed' and 'wait_pending_front' are the same as the radosgw */
func pending_has_completed(p *list.List) bool{
if p.Len() == 0 {
return false
}
e := p.Front()
c := e.Value.(*rados.AioCompletion)
ret := c.IsComplete()
if ret == 0 {
return false
} else {
return true
}
}
func wait_pending_front(p * list.List) int{
/* remove AioCompletion from list */
e := p.Front()
p.Remove(e)
c := e.Value.(*rados.AioCompletion)
c.WaitForComplete()
ret := c.GetReturnValue()
c.Release()
return ret
}
func drain_pending(p * list.List) int {
var ret int
for p.Len() > 0 {
ret = wait_pending_front(p)
}
return ret
}
func set_stripe_layout(p * rados.StriperPool) int{
var ret int = 0
if ret = p.SetLayoutStripeUnit(STRIPE_UNIT); ret < 0 {
return ret
}
if ret = p.SetLayoutObjectSize(OBJECT_SIZE); ret < 0 {
return ret
}
if ret = p.SetLayoutStripeCount(STRIPE_COUNT); ret < 0 {
return ret
}
return ret
}
func PutHandler(params martini.Params, w http.ResponseWriter, r *http.Request, conn *rados.Conn, ) {
poolname := params["pool"]
soid := params["soid"]
pool, err := conn.OpenPool(poolname)
if err != nil {
slog.Println("URL:", r.URL, "open pool failed")
ErrorHandler(w, r, http.StatusNotFound)
return
}
defer pool.Destroy()
striper, err := pool.CreateStriper()
if err != nil {
slog.Println("URL:", r.URL, "Create Striper failed")
ErrorHandler(w, r, http.StatusNotFound)
return
}
defer striper.Destroy()
set_stripe_layout(&striper)
bytesRange := r.Header.Get("Content-Range")
var src_offset, dest_offset, start, end, size int64 = 0, 0, 0, 0, 0
if bytesRange != "" {
/* header format: Content-Range:bytes 0-99/300 */
/* remove bytes and space */
bytesRange = strings.Trim(bytesRange, "bytes")
bytesRange = strings.TrimSpace(bytesRange)
o := strings.Split(bytesRange, "/")
currentRange, s := o[0], o[1]
o = strings.Split(currentRange, "-")
/* o[0] is the start, o[1] is the end */
start, err = strconv.ParseInt(o[0], 10, 64)
if err != nil {
slog.Println("URL:", r.URL, "parse Content-Range failed %s", bytesRange)
ErrorHandler(w, r, http.StatusBadRequest)
return
}
end, err = strconv.ParseInt(o[1], 10, 64)
if err != nil {
slog.Println("URL:", r.URL, "parse Content-Range failed %s", bytesRange)
ErrorHandler(w, r, http.StatusBadRequest)
return
}
size, err = strconv.ParseInt(s, 10, 64)
if err != nil {
slog.Println("URL:", r.URL, "parse Content-Range failed %s", bytesRange)
ErrorHandler(w, r, http.StatusBadRequest)
return
}
}
if bytesRange != "" {
/* already get $start, $end */
src_offset = start
dest_offset = start
} else {
src_offset = 0
dest_offset = 0
}
buf := make([]byte, BUFFERSIZE)
/* if the data len in pending_data is bigger than MAX_CHUNK_SIZE, I will flush the data to ceph */
var pending_data []byte
var available_data_size int
var c *rados.AioCompletion
pending := list.New()
for src_offset <= end || bytesRange == "" {
count, err := r.Body.Read(buf)
if count == 0 {
break
}
if err != nil && err != io.EOF {
slog.Printf("failed to read content from client url:%s, %s",
r.RequestURI, err.Error())
drain_pending(pending)
ErrorHandler(w, r, http.StatusBadRequest)
return
}
//In case the user send more data than expected.
if bytesRange != "" {
available_data_size = min(count, int(end - src_offset + 1))
} else {
available_data_size = count
}
src_offset += int64(count)
/* add newly received buffer to pending_data */
pending_data = append(pending_data, buf[:available_data_size]...)
/* if pending_data is not big enough, continue to read more data */
if len(pending_data) < MAX_CHUNK_SIZE {
continue
}
/* will write bl to ceph */
bl := pending_data[:MAX_CHUNK_SIZE]
/* now pending_data point to remaining data */
pending_data = pending_data[MAX_CHUNK_SIZE:]
c = new(rados.AioCompletion)
c.Create()
_, err = striper.WriteAIO(c, soid, bl, uint64(dest_offset))
if err != nil {
slog.Println("URL:", r.URL, "starting to write aio failed")
c.Release()
drain_pending(pending)
ErrorHandler(w, r, 501)
return
}
pending.PushBack(c)
//Throttle data
//if the front is finished, cleanup
for pending_has_completed(pending) {
if ret := wait_pending_front(pending); ret < 0 {
slog.Println("URL:%s, write aio failed or timeout, in pending_has_completed", r.RequestURI)
drain_pending(pending)
ErrorHandler(w, r, 408)
return
}
}
if pending.Len() > AIOCONCURRENT {
slog.Println("inputstream is a bit faster, wait to finish")
if ret := wait_pending_front(pending); ret < 0 {
slog.Println("URL:%s, write aio failed or timeout, in waiting pending ", r.RequestURI)
drain_pending(pending)
ErrorHandler(w, r, 408)
return
}
}
dest_offset += int64(len(bl))
}
//write all remaining data
if len(pending_data) > 0 {
c = new(rados.AioCompletion)
c.Create()
striper.WriteAIO(c, soid, pending_data, uint64(dest_offset))
pending.PushBack(c)
}
//drain_pending
if ret := drain_pending(pending); ret < 0 {
slog.Println("URL:%s, write aio failed or timeout, in draining", r.RequestURI)
ErrorHandler(w, r, 408)
return
}
w.Header().Set("Content-Type", "application/octet-stream")
if bytesRange == "" {
w.WriteHeader(http.StatusOK)
return
} else {
/* user send too much data or right data*/
if (src_offset >= end) {
w.Header().Set("Range", fmt.Sprintf("%d-%d/%d", start, end, size))
} else {
/* user send too few data */
w.Header().Set("Range", fmt.Sprintf("%d-%d/%d", start, src_offset - 1, size))
}
w.WriteHeader(http.StatusOK)
}
}
type gcCfg struct {
Name string
CacheSizeMBytes int // max per-node memory usage
CacheChunkSizeKBytes int
CacheMaxObjectSizeKBytes int // Defines maximum size of the s3 object to be kept in cache.
MyIPAddr string
Port int
Peers []string // IP addresses of all the nodes
ListenPort int
SocketTimeout int
QueueLength int
SecretKey string
DDos bool
ThrottleInterval int
ThrottleNums int
}
var cfg gcCfg
func getGcCfg() (cfg gcCfg, err error) {
// TODO(wenjianhn): get json from etcd
f, err := os.Open("/etc/wuzei/wuzei.json")
if err != nil {
slog.Println("Parse wuzei.json failed")
return
}
defer f.Close()
err = json.NewDecoder(f).Decode(&cfg)
if err != nil {
err = errors.New("failed to parse wuzei.json: " + err.Error())
slog.Println("Parse wuzei.json failed")
return
}
found := false
for _, peer := range cfg.Peers {
if peer == cfg.MyIPAddr {
found = true
break
}
}
if !found {
cfg.Peers = append(cfg.Peers, cfg.MyIPAddr)
}
SECRET = cfg.SecretKey
if cfg.DDos {
slog.Printf("Support DDos protect, any object will be blocked when accessing %d in %d seconds", cfg.ThrottleNums, cfg.ThrottleInterval)
} else {
slog.Printf("No DDos protect")
}
return
}
type BytesCounter struct {
byteSend int64
}
func main() {
var conn *rados.Conn
/* log */
f, err := os.OpenFile(LOGPATH, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Println("failed to open log\n")
return
}
defer f.Close()
m := martini.Classic()
slog = log.New(f, "[wuzei]", log.LstdFlags)
//Redirect stdout and stderr to the log
syscall.Dup2(int(f.Fd()), 2)
syscall.Dup2(int(f.Fd()), 1)
cfg, err = getGcCfg()
if err != nil {
slog.Println(err.Error())
return
}
if cfg.DDos {
whiteList = make(map[string]int)
// initial white list
f_whitelist, err := os.OpenFile(WHITELISTPATH, os.O_RDONLY, 0666)
if err != nil {
fmt.Println("failed to open whitelist")
f_whitelist.Close()
}
scanner := bufio.NewScanner(f_whitelist)
var line string
for scanner.Scan() {
line = scanner.Text()
//put whitelist in map
slog.Printf("Put %s into whitelist", line)
whiteList[line] = 0
}
f_whitelist.Close()
blackList = NewSafeMap()
urlRecord = NewURLRecord()
//if blacklist is long, it will be slow
go func(){
//v is the inserted time
current_time := time.Now()
for {
time.Sleep(time.Minute * 60)
for k, v := range blackList.Items() {
if v.(time.Time).Add(time.Minute * 60).After(current_time) {
blackList.Delete(k)
}
}
}
}()
}
m.Use(AuthMe(SECRET))
m.Use(WrapBytesCounter())
m.Use(func(w http.ResponseWriter, r *http.Request, conn *rados.Conn, counter *BytesCounter, c martini.Context){
start := time.Now()
addr := r.Header.Get("X-Real-IP")
if addr == "" {
addr = r.Header.Get("X-Forwarded-For")
if addr == "" {
addr = r.RemoteAddr
}
c.Next()
rw := w.(martini.ResponseWriter)
slog.Printf("COMPLETE %s %s %s %v %d in %s\n", addr, r.Method, r.URL.Path, rw.Status(), counter.byteSend, time.Since(start))
}})
wugui.InitCachePool(cfg.MyIPAddr, cfg.Peers, cfg.Port)
slog.Printf("Config of group cache: %+v\n", cfg)
var cacheSize int64
cacheSize = int64(cfg.CacheSizeMBytes) * 1024 * 1024
wugui.InitRadosCache(cfg.Name, cacheSize, cfg.CacheChunkSizeKBytes * 1024)
conn, err = rados.NewConn("admin")
if err != nil {
slog.Println("failed to open keyring")
return
}
conn.SetConfigOption("rados_mon_op_timeout", MONTIMEOUT)
conn.SetConfigOption("rados_osd_op_timeout", OSDTIMEOUT)
err = conn.ReadConfigFile("/etc/ceph/ceph.conf")
if err != nil {
slog.Println("failed to open ceph.conf")
return
}
err = conn.Connect()
if err != nil {
slog.Println("failed to connect to remote cluster")
return
}
defer conn.Shutdown()
m.Map(conn)
ReqQueue.Init(cfg.QueueLength)
m.Get("/whoareyou", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("I AM WUZEI"))
})
m.Get("/cachestats", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, fmt.Sprintf("%+v\n", wugui.GetRadosCacheStats()))
})
m.Get("/threads", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, fmt.Sprintf("%d\n", ReqQueue.size()))
})
/* resume upload protocal is based on http://www.grid.net.ru/nginx/resumable_uploads.en.html */
m.Put("/(?P<pool>[A-Za-z0-9]+)/(?P<soid>[^/]+)", RequestLimit(), PutHandler)
m.Delete("/(?P<pool>[A-Za-z0-9]+)/(?P<soid>[^/]+)", RequestLimit(), DeleteHandler)
m.Get("/(?P<pool>[A-Za-z0-9]+)/(?P<soid>[^/]+)", RequestLimit(), GetHandler)
m.Get("/info/(?P<pool>[A-Za-z0-9]+)/(?P<soid>[^/]+)", RequestLimit(), InfoHandler)
m.Get("/calcmd5/(?P<pool>[A-Za-z0-9]+)/(?P<soid>[^/]+)", RequestLimit(), Md5sumHandler)
m.Get("/blocksize",BlockHandler)
m.Get("/cephstatus",CephStatusHandler)
sl, err := nettimeout.NewListener(cfg.ListenPort, time.Duration(cfg.SocketTimeout) * time.Second,
time.Duration(cfg.SocketTimeout) * time.Second);
if err != nil {
fmt.Printf("Failed to listen to %d, quiting\n", cfg.ListenPort)
os.Stdout.Sync()
slog.Printf("Failed to listen to %d, quiting", cfg.ListenPort)
return
}
server := http.Server{}
http.HandleFunc("/", m.ServeHTTP)
sigChan := make(chan os.Signal)
signal.Notify(sigChan, syscall.SIGINT,
syscall.SIGHUP,