2011-12-16 22:51:26 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2020-11-28 08:18:48 +08:00
|
|
|
"io"
|
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
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/idx"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle_map"
|
|
|
|
. "github.com/seaweedfs/seaweedfs/weed/storage/types"
|
2022-08-24 14:53:35 +08:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
2011-12-16 22:51:26 +08:00
|
|
|
)
|
|
|
|
|
2021-02-07 09:00:03 +08:00
|
|
|
type NeedleMapKind int
|
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
|
|
|
|
|
|
|
const (
|
2021-02-07 09:00:03 +08:00
|
|
|
NeedleMapInMemory NeedleMapKind = iota
|
2019-04-18 15:19:18 +08:00
|
|
|
NeedleMapLevelDb // small memory footprint, 4MB total, 1 write buffer, 3 block buffer
|
|
|
|
NeedleMapLevelDbMedium // medium memory footprint, 8MB total, 3 write buffer, 5 block buffer
|
|
|
|
NeedleMapLevelDbLarge // large memory footprint, 12MB total, 4write buffer, 8 block buffer
|
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 {
|
2020-08-19 08:04:28 +08:00
|
|
|
Put(key NeedleId, offset Offset, size Size) error
|
2019-04-19 12:43:36 +08:00
|
|
|
Get(key NeedleId) (element *needle_map.NeedleValue, ok bool)
|
2020-09-13 03:42:36 +08:00
|
|
|
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
|
2020-03-21 14:38:46 +08:00
|
|
|
Sync() error
|
2020-11-28 08:18:48 +08:00
|
|
|
ReadIndexEntry(n int64) (key NeedleId, offset Offset, size Size, err error)
|
2013-04-18 15:23:14 +08:00
|
|
|
}
|
|
|
|
|
2015-05-26 15:58:41 +08:00
|
|
|
type baseNeedleMapper struct {
|
2019-05-04 23:47:11 +08:00
|
|
|
mapMetric
|
|
|
|
|
2015-05-26 15:58:41 +08:00
|
|
|
indexFile *os.File
|
|
|
|
indexFileAccessLock sync.Mutex
|
2021-02-21 04:39:25 +08:00
|
|
|
indexFileOffset int64
|
2013-04-18 15:23:14 +08:00
|
|
|
}
|
|
|
|
|
2022-08-24 14:53:35 +08:00
|
|
|
type TempNeedleMapper interface {
|
|
|
|
NeedleMapper
|
|
|
|
DoOffsetLoading(v *Volume, indexFile *os.File, startFrom uint64) error
|
2022-11-14 16:19:27 +08:00
|
|
|
UpdateNeedleMap(v *Volume, indexFile *os.File, opts *opt.Options, ldbTimeout int64) error
|
2022-08-24 14:53:35 +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
|
|
|
|
}
|
|
|
|
|
2020-08-19 08:04:28 +08:00
|
|
|
func (nm *baseNeedleMapper) appendToIndexFile(key NeedleId, offset Offset, size Size) error {
|
2019-05-19 13:46:24 +08:00
|
|
|
bytes := needle_map.ToBytes(key, offset, size)
|
2015-05-26 15:58:41 +08:00
|
|
|
|
|
|
|
nm.indexFileAccessLock.Lock()
|
|
|
|
defer nm.indexFileAccessLock.Unlock()
|
2021-02-21 04:39:25 +08:00
|
|
|
written, err := nm.indexFile.WriteAt(bytes, nm.indexFileOffset)
|
|
|
|
if err == nil {
|
|
|
|
nm.indexFileOffset += int64(written)
|
2014-05-30 10:19:03 +08:00
|
|
|
}
|
2015-03-28 07:34:58 +08:00
|
|
|
return err
|
2011-12-19 13:59:37 +08:00
|
|
|
}
|
2020-03-21 14:38:46 +08:00
|
|
|
|
|
|
|
func (nm *baseNeedleMapper) Sync() error {
|
|
|
|
return nm.indexFile.Sync()
|
|
|
|
}
|
2020-11-28 08:18:48 +08:00
|
|
|
|
|
|
|
func (nm *baseNeedleMapper) ReadIndexEntry(n int64) (key NeedleId, offset Offset, size Size, err error) {
|
|
|
|
bytes := make([]byte, NeedleMapEntrySize)
|
|
|
|
var readCount int
|
|
|
|
if readCount, err = nm.indexFile.ReadAt(bytes, n*NeedleMapEntrySize); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
if readCount == NeedleMapEntrySize {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
key, offset, size = idx.IdxFileEntry(bytes)
|
|
|
|
return
|
|
|
|
}
|