/
ceph.go
581 lines (499 loc) · 14.4 KB
/
ceph.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
package storage
import (
"bytes"
"container/list"
"errors"
"io"
"sync"
"fmt"
"github.com/journeymidnight/radoshttpd/rados"
log "github.com/sirupsen/logrus"
"time"
)
const (
MON_TIMEOUT = "10"
OSD_TIMEOUT = "10"
STRIPE_UNIT = 512 << 10 /* 512K */
STRIPE_COUNT = 2
OBJECT_SIZE = 8 << 20 /* 8M */
BUFFER_SIZE = 1 << 20 /* 1M */
MIN_CHUNK_SIZE = 512 << 10 /* 512K */
MAX_CHUNK_SIZE = 8 * BUFFER_SIZE /* 8M */
SMALL_FILE_POOLNAME = "rabbit"
BIG_FILE_POOLNAME = "tiger"
BIG_FILE_THRESHOLD = 128 << 10 /* 128K */
AIO_CONCURRENT = 4
)
type CephStorage struct {
Name string
Conn *rados.Conn
InstanceId uint64
CountMutex *sync.Mutex
Counter uint64
BufPool *sync.Pool
BigBufPool *sync.Pool
}
func NewCephStorage(configFile string) *CephStorage {
log.Infof("Loading Ceph file %s\n", configFile)
Rados, err := rados.NewConn("admin")
Rados.SetConfigOption("rados_mon_op_timeout", MON_TIMEOUT)
Rados.SetConfigOption("rados_osd_op_timeout", OSD_TIMEOUT)
err = Rados.ReadConfigFile(configFile)
if err != nil {
log.Errorf("Failed to open ceph.conf: %s\n", configFile)
return nil
}
err = Rados.Connect()
if err != nil {
log.Errorf("Failed to connect to remote cluster: %s\n", configFile)
return nil
}
name, err := Rados.GetFSID()
if err != nil {
log.Errorf("Failed to get FSID: %s\n", configFile)
Rados.Shutdown()
return nil
}
id := Rados.GetInstanceID()
cluster := CephStorage{
Conn: Rados,
Name: name,
InstanceId: id,
CountMutex: new(sync.Mutex),
BufPool: &sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, BIG_FILE_THRESHOLD))
},
},
BigBufPool: &sync.Pool{
New: func() interface{} {
return make([]byte, MAX_CHUNK_SIZE)
},
},
}
log.Infof("Ceph Cluster %s is ready, InstanceId is %d\n", name, id)
return &cluster
}
func setStripeLayout(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 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 (cluster *CephStorage) GetUniqUploadName() string {
cluster.CountMutex.Lock()
defer cluster.CountMutex.Unlock()
cluster.Counter += 1
oid := fmt.Sprintf("%d:%d", cluster.InstanceId, cluster.Counter)
return oid
}
func (c *CephStorage) Shutdown() {
c.Conn.Shutdown()
}
func (cluster *CephStorage) doSmallPut(poolname string, oid string, data io.Reader) (size int64, err error) {
tstart := time.Now()
pool, err := cluster.Conn.OpenPool(poolname)
if err != nil {
return 0, errors.New("Bad poolname")
}
defer pool.Destroy()
tpool := time.Now()
dur := tpool.Sub(tstart).Nanoseconds() / 1000000
if dur >= 10 {
log.Warnf("slow log: doSmallPut OpenPool(%s, %s) spent %d", poolname, oid, dur)
}
buffer := cluster.BufPool.Get().(*bytes.Buffer)
buffer.Reset()
defer cluster.BufPool.Put(buffer)
written, err := buffer.ReadFrom(data)
if err != nil {
log.Errorf("failed to read data for pool %s, oid %s, err: %v", poolname, oid, err)
return 0, err
}
size = written
tread := time.Now()
dur = tread.Sub(tpool).Nanoseconds() / 1000000
if dur >= 10 {
log.Warnf("slow log: doSmallPut read body(%s, %s) spent %d", poolname, oid, dur)
}
err = pool.WriteSmallObject(oid, buffer.Bytes())
if err != nil {
return 0, err
}
twrite := time.Now()
dur = twrite.Sub(tread).Nanoseconds() / 1000000
if dur >= 50 {
log.Warnf("slow log: doSmallPut ceph write(%s, %s) spent %d", poolname, oid, dur)
}
dur = twrite.Sub(tstart).Nanoseconds() / 1000000
if dur >= 100 {
log.Warnf("slow log: doSmallPut fin(%s, %s) spent %d", poolname, oid, dur)
}
return size, nil
}
type RadosSmallDownloader struct {
oid string
offset int64
remaining int64
pool *rados.Pool
}
func (rd *RadosSmallDownloader) Read(p []byte) (n int, err error) {
if rd.remaining <= 0 {
return 0, io.EOF
}
if int64(len(p)) > rd.remaining {
p = p[:rd.remaining]
}
count, err := rd.pool.Read(rd.oid, p, uint64(rd.offset))
if count == 0 {
return 0, io.EOF
}
rd.offset += int64(count)
rd.remaining -= int64(count)
return count, err
}
func (rd *RadosSmallDownloader) Seek(offset int64, whence int) (int64, error) {
switch whence {
case 0:
rd.offset = offset
case 1:
rd.offset += offset
case 2:
panic("Not implemented")
}
return rd.offset, nil
}
func (rd *RadosSmallDownloader) Close() error {
rd.pool.Destroy()
return nil
}
func (cluster *CephStorage) Put(poolname string, oid string, data io.Reader) (size int64, err error) {
if poolname == SMALL_FILE_POOLNAME {
return cluster.doSmallPut(poolname, oid, data)
}
pool, err := cluster.Conn.OpenPool(poolname)
if err != nil {
return 0, fmt.Errorf("Bad poolname %s", poolname)
}
defer pool.Destroy()
striper, err := pool.CreateStriper()
if err != nil {
return 0, fmt.Errorf("Bad ioctx of pool %s", poolname)
}
defer striper.Destroy()
setStripeLayout(&striper)
/* if the data len in pending_data is bigger than current_upload_window, I will flush the data to ceph */
/* current_upload_window could not dynamically increase or shrink */
var c *rados.AioCompletion
pending := list.New()
var current_upload_window = MIN_CHUNK_SIZE /* initial window size as MIN_CHUNK_SIZE, max size is MAX_CHUNK_SIZE */
var pending_data = cluster.BigBufPool.Get().([]byte)
defer func() {
cluster.BigBufPool.Put(pending_data)
}()
var slice_offset = 0
var slow_count = 0
// slice is the buffer size of reader, the size is equal to remain size of pending_data
var slice = pending_data[0:current_upload_window]
var offset uint64 = 0
for {
start := time.Now()
count, err := data.Read(slice)
if err != nil && err != io.EOF {
drain_pending(pending)
return 0, fmt.Errorf("Read from client failed. pool:%s oid:%s", poolname, oid)
}
if count == 0 {
break
}
// it's used to calculate next upload window
elapsed_time := time.Since(start)
slice_offset += count
slice = pending_data[slice_offset:]
//is pending_data full?
if slice_offset < len(pending_data) {
continue
}
/* pending data is full now */
c = new(rados.AioCompletion)
c.Create()
_, err = striper.WriteAIO(c, oid, pending_data, offset)
if err != nil {
c.Release()
drain_pending(pending)
return 0, fmt.Errorf("Bad io. pool:%s oid:%s", poolname, oid)
}
pending.PushBack(c)
for pending_has_completed(pending) {
if ret := wait_pending_front(pending); ret < 0 {
drain_pending(pending)
return 0, fmt.Errorf("Error drain_pending in pending_has_completed(%d). pool:%s oid:%s", ret, poolname, oid)
}
}
if pending.Len() > AIO_CONCURRENT {
if ret := wait_pending_front(pending); ret < 0 {
drain_pending(pending)
return 0, fmt.Errorf("Error wait_pending_front(%d). pool:%s oid:%s", ret, poolname, oid)
}
}
offset += uint64(len(pending_data))
/* Resize current upload window */
expected_time := count * 1000 * 1000 * 1000 / current_upload_window /* 1000 * 1000 * 1000 means use Nanoseconds */
// If the upload speed is less than half of the current upload window, reduce the upload window by half.
// If upload speed is larger than current window size per second, used the larger window and twice
if elapsed_time.Nanoseconds() > 2*int64(expected_time) {
if slow_count > 2 && current_upload_window > MIN_CHUNK_SIZE {
current_upload_window = current_upload_window >> 1
slow_count = 0
}
slow_count += 1
} else if int64(expected_time) > elapsed_time.Nanoseconds() {
/* if upload speed is fast enough, enlarge the current_upload_window a bit */
current_upload_window = current_upload_window << 1
if current_upload_window > MAX_CHUNK_SIZE {
current_upload_window = MAX_CHUNK_SIZE
}
slow_count = 0
}
/* allocate a new pending data */
slice_offset = 0
slice = pending_data[0:current_upload_window]
}
size = int64(uint64(slice_offset) + offset)
//write all remaining data
if slice_offset > 0 {
c = new(rados.AioCompletion)
c.Create()
striper.WriteAIO(c, oid, pending_data[:slice_offset], offset)
pending.PushBack(c)
}
//drain_pending
if ret := drain_pending(pending); ret < 0 {
return 0, fmt.Errorf("Error wait_pending_front(%d). pool:%s oid:%s", ret, poolname, oid)
}
return size, nil
}
func (cluster *CephStorage) Append(poolname string, oid string, data io.Reader, offset uint64, isExist bool) (size int64, err error) {
if poolname != BIG_FILE_POOLNAME {
return 0, errors.New("specified pool must be used for storing big file.")
}
pool, err := cluster.Conn.OpenPool(poolname)
if err != nil {
return 0, fmt.Errorf("Bad poolname %s", poolname)
}
defer pool.Destroy()
striper, err := pool.CreateStriper()
if err != nil {
return 0, fmt.Errorf("Bad ioctx of pool %s", poolname)
}
defer striper.Destroy()
setStripeLayout(&striper)
var current_upload_window = MIN_CHUNK_SIZE /* initial window size as MIN_CHUNK_SIZE, max size is MAX_CHUNK_SIZE */
var pending_data = make([]byte, current_upload_window)
var origin_offset = offset
var slice_offset = 0
var slow_count = 0
// slice is the buffer size of reader, the size is equal to remain size of pending_data
var slice = pending_data[0:current_upload_window]
for {
start := time.Now()
count, err := data.Read(slice)
if count == 0 {
break
}
// it's used to calculate next upload window
elapsed_time := time.Since(start)
slice_offset += count
slice = pending_data[slice_offset:]
//is pending_data full?
if slice_offset < len(pending_data) {
continue
}
/* pending data is full now */
_, err = striper.Write(oid, pending_data, offset)
if err != nil {
return 0, fmt.Errorf("Bad io. pool:%s oid:%s", poolname, oid)
}
offset += uint64(len(pending_data))
/* Resize current upload window */
expected_time := count * 1000 * 1000 * 1000 / current_upload_window /* 1000 * 1000 * 1000 means use Nanoseconds */
// If the upload speed is less than half of the current upload window, reduce the upload window by half.
// If upload speed is larger than current window size per second, used the larger window and twice
if elapsed_time.Nanoseconds() > 2*int64(expected_time) {
if slow_count > 2 && current_upload_window > MIN_CHUNK_SIZE {
current_upload_window = current_upload_window >> 1
slow_count = 0
}
slow_count += 1
} else if int64(expected_time) > elapsed_time.Nanoseconds() {
/* if upload speed is fast enough, enlarge the current_upload_window a bit */
current_upload_window = current_upload_window << 1
if current_upload_window > MAX_CHUNK_SIZE {
current_upload_window = MAX_CHUNK_SIZE
}
slow_count = 0
}
/* allocate a new pending data */
pending_data = make([]byte, current_upload_window)
slice_offset = 0
slice = pending_data[0:current_upload_window]
}
size = int64(uint64(slice_offset) + offset - origin_offset)
//write all remaining data
if slice_offset > 0 {
_, err = striper.Write(oid, pending_data, offset)
if err != nil {
return 0, fmt.Errorf("Bad io. pool:%s oid:%s", poolname, oid)
}
}
return size, nil
}
type RadosDownloader struct {
striper *rados.StriperPool
oid string
offset int64
remaining int64
pool *rados.Pool
}
func (rd *RadosDownloader) Read(p []byte) (n int, err error) {
if rd.remaining <= 0 {
return 0, io.EOF
}
if int64(len(p)) > rd.remaining {
p = p[:rd.remaining]
}
count, err := rd.striper.Read(rd.oid, p, uint64(rd.offset))
if count == 0 {
return 0, io.EOF
}
rd.offset += int64(count)
rd.remaining -= int64(count)
return count, err
}
func (rd *RadosDownloader) Seek(offset int64, whence int) (int64, error) {
switch whence {
case 0:
rd.offset = offset
case 1:
rd.offset += offset
case 2:
panic("Not implemented")
}
return rd.offset, nil
}
func (rd *RadosDownloader) Close() error {
rd.striper.Destroy()
rd.pool.Destroy()
return nil
}
func (cluster *CephStorage) getReader(poolName string, oid string, startOffset int64,
length int64) (reader io.ReadCloser, err error) {
if poolName == SMALL_FILE_POOLNAME {
pool, e := cluster.Conn.OpenPool(poolName)
if e != nil {
err = errors.New("bad poolname")
return
}
radosSmallReader := &RadosSmallDownloader{
oid: oid,
offset: startOffset,
pool: pool,
remaining: length,
}
return radosSmallReader, nil
}
pool, err := cluster.Conn.OpenPool(poolName)
if err != nil {
err = errors.New("bad poolname")
return
}
striper, err := pool.CreateStriper()
if err != nil {
err = errors.New("bad ioctx")
return
}
radosReader := &RadosDownloader{
striper: &striper,
oid: oid,
offset: startOffset,
pool: pool,
remaining: length,
}
return radosReader, nil
}
// Works together with `wrapAlignedEncryptionReader`, see comments there.
func (cluster *CephStorage) getAlignedReader(poolName string, oid string, startOffset int64,
length int64) (reader io.ReadCloser, err error) {
alignedOffset := startOffset / AES_BLOCK_SIZE * AES_BLOCK_SIZE
length += startOffset - alignedOffset
return cluster.getReader(poolName, oid, alignedOffset, length)
}
func (cluster *CephStorage) doSmallRemove(poolname string, oid string) error {
pool, err := cluster.Conn.OpenPool(poolname)
if err != nil {
return errors.New("Bad poolname")
}
defer pool.Destroy()
return pool.Delete(oid)
}
func (cluster *CephStorage) Remove(poolname string, oid string) error {
if poolname == SMALL_FILE_POOLNAME {
return cluster.doSmallRemove(poolname, oid)
}
pool, err := cluster.Conn.OpenPool(poolname)
if err != nil {
return errors.New("Bad poolname")
}
defer pool.Destroy()
striper, err := pool.CreateStriper()
if err != nil {
return errors.New("Bad ioctx")
}
defer striper.Destroy()
return striper.Delete(oid)
}
func (cluster *CephStorage) GetUsedSpacePercent() (pct int, err error) {
stat, err := cluster.Conn.GetClusterStats()
if err != nil {
return 0, errors.New("Stat error")
}
pct = int(stat.Kb_used * uint64(100) / stat.Kb)
return
}