@@ -64,6 +64,7 @@ type diskQueue struct {
64
64
writeFileNum int64
65
65
readMessages int64
66
66
writeMessages int64
67
+ writeBytes int64
67
68
depth int64
68
69
69
70
sync.RWMutex
@@ -107,6 +108,9 @@ type diskQueue struct {
107
108
108
109
// disk limit implementation flag
109
110
diskLimitFeatIsOn bool
111
+
112
+ // the size of the
113
+ readMsgSize int32
110
114
}
111
115
112
116
// New instantiates an instance of diskQueue, retrieving metadata
@@ -300,6 +304,7 @@ func (d *diskQueue) skipToNextRWFile() error {
300
304
d .depth = 0
301
305
d .readMessages = 0
302
306
d .writeMessages = 0
307
+ d .writeBytes = 0
303
308
304
309
return err
305
310
}
@@ -308,7 +313,6 @@ func (d *diskQueue) skipToNextRWFile() error {
308
313
// while advancing read positions and rolling files, if necessary
309
314
func (d * diskQueue ) readOne () ([]byte , error ) {
310
315
var err error
311
- var msgSize int32
312
316
313
317
if d .readFile == nil {
314
318
curFileName := d .fileName (d .readFileNum )
@@ -345,30 +349,30 @@ func (d *diskQueue) readOne() ([]byte, error) {
345
349
d .reader = bufio .NewReader (d .readFile )
346
350
}
347
351
348
- err = binary .Read (d .reader , binary .BigEndian , & msgSize )
352
+ err = binary .Read (d .reader , binary .BigEndian , & d . readMsgSize )
349
353
if err != nil {
350
354
d .readFile .Close ()
351
355
d .readFile = nil
352
356
return nil , err
353
357
}
354
358
355
- if msgSize < d .minMsgSize || msgSize > d .maxMsgSize {
359
+ if d . readMsgSize < d .minMsgSize || d . readMsgSize > d .maxMsgSize {
356
360
// this file is corrupt and we have no reasonable guarantee on
357
361
// where a new message should begin
358
362
d .readFile .Close ()
359
363
d .readFile = nil
360
- return nil , fmt .Errorf ("invalid message read size (%d)" , msgSize )
364
+ return nil , fmt .Errorf ("invalid message read size (%d)" , d . readMsgSize )
361
365
}
362
366
363
- readBuf := make ([]byte , msgSize )
367
+ readBuf := make ([]byte , d . readMsgSize )
364
368
_ , err = io .ReadFull (d .reader , readBuf )
365
369
if err != nil {
366
370
d .readFile .Close ()
367
371
d .readFile = nil
368
372
return nil , err
369
373
}
370
374
371
- totalBytes := int64 (4 + msgSize )
375
+ totalBytes := int64 (4 + d . readMsgSize )
372
376
373
377
// we only advance next* because we have not yet sent this to consumers
374
378
// (where readFileNum, readPos will actually be advanced)
@@ -461,6 +465,7 @@ func (d *diskQueue) writeOne(data []byte) error {
461
465
if d .diskLimitFeatIsOn {
462
466
// save space for the number of messages in this file
463
467
fileSize += 8
468
+ d .writeBytes += totalBytes
464
469
d .writeMessages += 1
465
470
}
466
471
@@ -471,7 +476,12 @@ func (d *diskQueue) writeOne(data []byte) error {
471
476
472
477
d .writeFileNum ++
473
478
d .writePos = 0
474
- d .writeMessages = 0
479
+
480
+ if d .diskLimitFeatIsOn {
481
+ // add bytes for the number of messages in the file
482
+ d .writeBytes += 8
483
+ d .writeMessages = 0
484
+ }
475
485
476
486
// sync every time we start writing to a new file
477
487
err = d .sync ()
@@ -522,10 +532,10 @@ func (d *diskQueue) retrieveMetaData() error {
522
532
523
533
// if user is using disk space limit feature
524
534
if d .diskLimitFeatIsOn {
525
- _ , err = fmt .Fscanf (f , "%d\n %d,%d,%d\n %d,%d,%d\n " ,
535
+ _ , err = fmt .Fscanf (f , "%d\n %d,%d,%d\n %d,%d,%d,%d \n " ,
526
536
& d .depth ,
527
537
& d .readFileNum , & d .readMessages , & d .readPos ,
528
- & d .writeFileNum , & d .writeMessages , & d .writePos )
538
+ & d .writeBytes , & d . writeFileNum , & d .writeMessages , & d .writePos )
529
539
} else {
530
540
_ , err = fmt .Fscanf (f , "%d\n %d,%d\n %d,%d\n " ,
531
541
& d .depth ,
@@ -559,10 +569,10 @@ func (d *diskQueue) persistMetaData() error {
559
569
560
570
// if user is using disk space limit feature
561
571
if d .diskLimitFeatIsOn {
562
- _ , err = fmt .Fprintf (f , "%d\n %d,%d,%d\n %d,%d,%d\n " ,
572
+ _ , err = fmt .Fprintf (f , "%d\n %d,%d,%d\n %d,%d,%d,%d \n " ,
563
573
d .depth ,
564
574
d .readFileNum , d .readMessages , d .readPos ,
565
- d .writeFileNum , d .writeMessages , d .writePos )
575
+ d .writeBytes , d . writeFileNum , d .writeMessages , d .writePos )
566
576
} else {
567
577
_ , err = fmt .Fprintf (f , "%d\n %d,%d\n %d,%d\n " ,
568
578
d .depth ,
@@ -629,6 +639,8 @@ func (d *diskQueue) checkTailCorruption(depth int64) {
629
639
}
630
640
631
641
func (d * diskQueue ) moveForward () {
642
+ // add bytes for the number of messages and the size of the message
643
+ readFileLen := int64 (d .readMsgSize ) + d .readPos + 12
632
644
oldReadFileNum := d .readFileNum
633
645
d .readFileNum = d .nextReadFileNum
634
646
d .readPos = d .nextReadPos
@@ -647,6 +659,8 @@ func (d *diskQueue) moveForward() {
647
659
if err != nil {
648
660
d .logf (ERROR , "DISKQUEUE(%s) failed to Remove(%s) - %s" , d .name , fn , err )
649
661
}
662
+
663
+ d .writeBytes -= readFileLen
650
664
}
651
665
652
666
d .checkTailCorruption (d .depth )
0 commit comments