2011-12-16 22:51:26 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2013-02-11 06:00:06 +08:00
|
|
|
"fmt"
|
2015-05-26 15:58:41 +08:00
|
|
|
"io/ioutil"
|
2013-02-11 06:00:06 +08:00
|
|
|
"os"
|
2015-05-26 15:58:41 +08:00
|
|
|
"sync"
|
2016-04-10 15:24:22 +08:00
|
|
|
|
2017-05-27 13:51:25 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2018-07-08 17:28:04 +08:00
|
|
|
. "github.com/chrislusf/seaweedfs/weed/storage/types"
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2011-12-16 22:51:26 +08:00
|
|
|
)
|
|
|
|
|
Add boltdb for volume needle map
boltdb is fairly slow to write, about 6 minutes for recreating index
for 1553934 files. Boltdb loads 1,553,934 x 16 = 24,862,944bytes from
disk, and generate the boltdb as large as 134,217,728 bytes in 6
minutes.
To compare, for leveldb, it recreates index in leveldb as large as
27,188,148 bytes in 8 seconds.
For in memory version, it loads the index in
To test the memory consumption, the leveldb or boltdb index are
created. And the server is restarted. Using the benchmark tool to read
lots of files. There are 7 volumes in benchmark collection, each with
about 1553K files.
For leveldb, the memory starts at 142,884KB, and stays at 179,340KB.
For boltdb, the memory starts at 73,756KB, and stays at 144,564KB.
For in-memory, the memory starts at 368,152KB, and stays at 448,032KB.
2015-03-30 02:04:32 +08:00
|
|
|
type NeedleMapType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
NeedleMapInMemory NeedleMapType = iota
|
|
|
|
NeedleMapLevelDb
|
|
|
|
NeedleMapBoltDb
|
2017-05-27 13:51:25 +08:00
|
|
|
NeedleMapBtree
|
Add boltdb for volume needle map
boltdb is fairly slow to write, about 6 minutes for recreating index
for 1553934 files. Boltdb loads 1,553,934 x 16 = 24,862,944bytes from
disk, and generate the boltdb as large as 134,217,728 bytes in 6
minutes.
To compare, for leveldb, it recreates index in leveldb as large as
27,188,148 bytes in 8 seconds.
For in memory version, it loads the index in
To test the memory consumption, the leveldb or boltdb index are
created. And the server is restarted. Using the benchmark tool to read
lots of files. There are 7 volumes in benchmark collection, each with
about 1553K files.
For leveldb, the memory starts at 142,884KB, and stays at 179,340KB.
For boltdb, the memory starts at 73,756KB, and stays at 144,564KB.
For in-memory, the memory starts at 368,152KB, and stays at 448,032KB.
2015-03-30 02:04:32 +08:00
|
|
|
)
|
|
|
|
|
2013-04-18 15:23:14 +08:00
|
|
|
type NeedleMapper interface {
|
2018-07-08 17:28:04 +08:00
|
|
|
Put(key NeedleId, offset Offset, size uint32) error
|
|
|
|
Get(key NeedleId) (element *needle.NeedleValue, ok bool)
|
|
|
|
Delete(key NeedleId, offset Offset) error
|
2013-04-18 15:23:14 +08:00
|
|
|
Close()
|
2014-03-11 02:43:54 +08:00
|
|
|
Destroy() error
|
2013-04-18 15:23:14 +08:00
|
|
|
ContentSize() uint64
|
|
|
|
DeletedSize() uint64
|
|
|
|
FileCount() int
|
|
|
|
DeletedCount() int
|
2018-07-08 17:28:04 +08:00
|
|
|
MaxFileKey() NeedleId
|
2015-05-26 15:58:41 +08:00
|
|
|
IndexFileSize() uint64
|
|
|
|
IndexFileContent() ([]byte, error)
|
|
|
|
IndexFileName() string
|
2013-04-18 15:23:14 +08:00
|
|
|
}
|
|
|
|
|
2015-05-26 15:58:41 +08:00
|
|
|
type baseNeedleMapper struct {
|
|
|
|
indexFile *os.File
|
|
|
|
indexFileAccessLock sync.Mutex
|
|
|
|
|
|
|
|
mapMetric
|
2013-04-18 15:23:14 +08:00
|
|
|
}
|
|
|
|
|
2016-04-10 13:47:03 +08:00
|
|
|
func (nm *baseNeedleMapper) IndexFileSize() uint64 {
|
2015-05-26 15:58:41 +08:00
|
|
|
stat, err := nm.indexFile.Stat()
|
|
|
|
if err == nil {
|
|
|
|
return uint64(stat.Size())
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-04-10 13:47:03 +08:00
|
|
|
func (nm *baseNeedleMapper) IndexFileName() string {
|
2015-05-26 15:58:41 +08:00
|
|
|
return nm.indexFile.Name()
|
|
|
|
}
|
|
|
|
|
2018-07-08 17:28:04 +08:00
|
|
|
func IdxFileEntry(bytes []byte) (key NeedleId, offset Offset, size uint32) {
|
|
|
|
key = BytesToNeedleId(bytes[:NeedleIdSize])
|
2018-07-22 08:39:10 +08:00
|
|
|
offset = BytesToOffset(bytes[NeedleIdSize : NeedleIdSize+OffsetSize])
|
|
|
|
size = util.BytesToUint32(bytes[NeedleIdSize+OffsetSize : NeedleIdSize+OffsetSize+SizeSize])
|
2015-05-26 15:58:41 +08:00
|
|
|
return
|
|
|
|
}
|
2018-07-08 17:28:04 +08:00
|
|
|
func (nm *baseNeedleMapper) appendToIndexFile(key NeedleId, offset Offset, size uint32) error {
|
|
|
|
bytes := make([]byte, NeedleIdSize+OffsetSize+SizeSize)
|
|
|
|
NeedleIdToBytes(bytes[0:NeedleIdSize], key)
|
|
|
|
OffsetToBytes(bytes[NeedleIdSize:NeedleIdSize+OffsetSize], offset)
|
|
|
|
util.Uint32toBytes(bytes[NeedleIdSize+OffsetSize:NeedleIdSize+OffsetSize+SizeSize], size)
|
2015-05-26 15:58:41 +08:00
|
|
|
|
|
|
|
nm.indexFileAccessLock.Lock()
|
|
|
|
defer nm.indexFileAccessLock.Unlock()
|
|
|
|
if _, err := nm.indexFile.Seek(0, 2); err != nil {
|
2015-03-28 07:34:58 +08:00
|
|
|
return fmt.Errorf("cannot seek end of indexfile %s: %v",
|
2015-05-26 15:58:41 +08:00
|
|
|
nm.indexFile.Name(), err)
|
2014-05-30 10:19:03 +08:00
|
|
|
}
|
2015-05-26 15:58:41 +08:00
|
|
|
_, err := nm.indexFile.Write(bytes)
|
2015-03-28 07:34:58 +08:00
|
|
|
return err
|
2011-12-19 13:59:37 +08:00
|
|
|
}
|
2016-04-10 13:47:03 +08:00
|
|
|
func (nm *baseNeedleMapper) IndexFileContent() ([]byte, error) {
|
2015-05-26 15:58:41 +08:00
|
|
|
nm.indexFileAccessLock.Lock()
|
|
|
|
defer nm.indexFileAccessLock.Unlock()
|
|
|
|
return ioutil.ReadFile(nm.indexFile.Name())
|
|
|
|
}
|