2018-07-07 07:51:17 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-07-08 09:28:04 +00:00
|
|
|
. "github.com/chrislusf/seaweedfs/weed/storage/types"
|
2018-07-22 00:39:10 +00:00
|
|
|
"github.com/willf/bloom"
|
|
|
|
"os"
|
2018-07-07 07:51:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mapMetric struct {
|
2018-07-08 09:28:04 +00:00
|
|
|
DeletionCounter int `json:"DeletionCounter"`
|
|
|
|
FileCounter int `json:"FileCounter"`
|
|
|
|
DeletionByteCounter uint64 `json:"DeletionByteCounter"`
|
|
|
|
FileByteCounter uint64 `json:"FileByteCounter"`
|
|
|
|
MaximumFileKey NeedleId `json:"MaxFileKey"`
|
2018-07-07 07:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *mapMetric) logDelete(deletedByteCount uint32) {
|
|
|
|
mm.DeletionByteCounter = mm.DeletionByteCounter + uint64(deletedByteCount)
|
|
|
|
mm.DeletionCounter++
|
|
|
|
}
|
|
|
|
|
2018-07-08 09:28:04 +00:00
|
|
|
func (mm *mapMetric) logPut(key NeedleId, oldSize uint32, newSize uint32) {
|
2018-07-07 07:51:17 +00:00
|
|
|
if key > mm.MaximumFileKey {
|
|
|
|
mm.MaximumFileKey = key
|
|
|
|
}
|
|
|
|
mm.FileCounter++
|
|
|
|
mm.FileByteCounter = mm.FileByteCounter + uint64(newSize)
|
|
|
|
if oldSize > 0 {
|
|
|
|
mm.DeletionCounter++
|
|
|
|
mm.DeletionByteCounter = mm.DeletionByteCounter + uint64(oldSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm mapMetric) ContentSize() uint64 {
|
|
|
|
return mm.FileByteCounter
|
|
|
|
}
|
|
|
|
func (mm mapMetric) DeletedSize() uint64 {
|
|
|
|
return mm.DeletionByteCounter
|
|
|
|
}
|
|
|
|
func (mm mapMetric) FileCount() int {
|
|
|
|
return mm.FileCounter
|
|
|
|
}
|
|
|
|
func (mm mapMetric) DeletedCount() int {
|
|
|
|
return mm.DeletionCounter
|
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
func (mm mapMetric) MaxFileKey() NeedleId {
|
2018-07-07 07:51:17 +00:00
|
|
|
return mm.MaximumFileKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNeedleMapMetricFromIndexFile(r *os.File) (mm *mapMetric, err error) {
|
|
|
|
mm = &mapMetric{}
|
|
|
|
var bf *bloom.BloomFilter
|
2018-07-08 09:28:04 +00:00
|
|
|
buf := make([]byte, NeedleIdSize)
|
2018-07-07 07:51:17 +00:00
|
|
|
err = reverseWalkIndexFile(r, func(entryCount int64) {
|
|
|
|
bf = bloom.NewWithEstimates(uint(entryCount), 0.001)
|
2018-07-08 09:28:04 +00:00
|
|
|
}, func(key NeedleId, offset Offset, size uint32) error {
|
2018-07-07 07:51:17 +00:00
|
|
|
|
|
|
|
if key > mm.MaximumFileKey {
|
|
|
|
mm.MaximumFileKey = key
|
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
NeedleIdToBytes(buf, key)
|
2018-07-07 07:51:17 +00:00
|
|
|
if size != TombstoneFileSize {
|
|
|
|
mm.FileByteCounter += uint64(size)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bf.Test(buf) {
|
|
|
|
mm.FileCounter++
|
|
|
|
bf.Add(buf)
|
|
|
|
} else {
|
|
|
|
// deleted file
|
|
|
|
mm.DeletionCounter++
|
|
|
|
if size != TombstoneFileSize {
|
|
|
|
// previously already deleted file
|
|
|
|
mm.DeletionByteCounter += uint64(size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-08 09:28:04 +00:00
|
|
|
func reverseWalkIndexFile(r *os.File, initFn func(entryCount int64), fn func(key NeedleId, offset Offset, size uint32) error) error {
|
2018-07-07 07:51:17 +00:00
|
|
|
fi, err := r.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("file %s stat error: %v", r.Name(), err)
|
|
|
|
}
|
|
|
|
fileSize := fi.Size()
|
2018-07-08 09:28:04 +00:00
|
|
|
if fileSize%NeedleEntrySize != 0 {
|
2018-07-07 07:51:17 +00:00
|
|
|
return fmt.Errorf("unexpected file %s size: %d", r.Name(), fileSize)
|
|
|
|
}
|
|
|
|
|
2018-07-08 09:28:04 +00:00
|
|
|
initFn(fileSize / NeedleEntrySize)
|
2018-07-07 07:51:17 +00:00
|
|
|
|
2018-07-08 09:28:04 +00:00
|
|
|
bytes := make([]byte, NeedleEntrySize)
|
|
|
|
for readerOffset := fileSize - NeedleEntrySize; readerOffset >= 0; readerOffset -= NeedleEntrySize {
|
2018-07-07 07:51:17 +00:00
|
|
|
count, e := r.ReadAt(bytes, readerOffset)
|
|
|
|
glog.V(3).Infoln("file", r.Name(), "readerOffset", readerOffset, "count", count, "e", e)
|
2018-07-08 09:28:04 +00:00
|
|
|
key, offset, size := IdxFileEntry(bytes)
|
2018-07-07 07:51:17 +00:00
|
|
|
if e = fn(key, offset, size); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|