2011-12-16 14:51:26 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2012-11-20 09:45:36 +00:00
|
|
|
"fmt"
|
2019-04-19 04:43:36 +00:00
|
|
|
|
2019-09-02 10:28:40 +00:00
|
|
|
"github.com/joeslay/seaweedfs/weed/pb/master_pb"
|
|
|
|
"github.com/joeslay/seaweedfs/weed/stats"
|
|
|
|
"github.com/joeslay/seaweedfs/weed/storage/needle"
|
|
|
|
"github.com/joeslay/seaweedfs/weed/storage/types"
|
2019-04-19 04:43:36 +00:00
|
|
|
|
2011-12-16 14:51:26 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2019-03-23 18:33:34 +00:00
|
|
|
"strconv"
|
2011-12-24 08:40:56 +00:00
|
|
|
"sync"
|
2013-08-12 23:53:32 +00:00
|
|
|
"time"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2019-09-02 10:28:40 +00:00
|
|
|
"github.com/joeslay/seaweedfs/weed/glog"
|
2011-12-16 14:51:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Volume struct {
|
2019-09-05 10:56:48 +00:00
|
|
|
Id needle.VolumeId
|
|
|
|
dir string
|
|
|
|
Collection string
|
|
|
|
dataFile *os.File
|
|
|
|
nm NeedleMapper
|
|
|
|
needleMapKind NeedleMapType
|
|
|
|
readOnly bool
|
|
|
|
MemoryMapMaxSizeMB uint32
|
2011-12-16 14:51:26 +00:00
|
|
|
|
2013-01-21 03:44:23 +00:00
|
|
|
SuperBlock
|
2012-11-20 09:45:36 +00:00
|
|
|
|
2019-04-19 07:39:34 +00:00
|
|
|
dataFileAccessLock sync.Mutex
|
|
|
|
lastModifiedTsSeconds uint64 //unix time in seconds
|
|
|
|
lastAppendAtNs uint64 //unix time in nanoseconds
|
2016-09-29 05:57:23 +00:00
|
|
|
|
2016-10-07 08:22:24 +00:00
|
|
|
lastCompactIndexOffset uint64
|
|
|
|
lastCompactRevision uint16
|
2019-08-12 07:53:50 +00:00
|
|
|
|
|
|
|
isCompacting bool
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 10:56:48 +00:00
|
|
|
func NewVolume(dirname string, collection string, id needle.VolumeId, needleMapKind NeedleMapType, replicaPlacement *ReplicaPlacement, ttl *needle.TTL, preallocate int64, memoryMapMaxSizeMB uint32) (v *Volume, e error) {
|
2018-06-23 23:48:19 +00:00
|
|
|
// if replicaPlacement is nil, the superblock will be loaded from disk
|
2019-09-05 10:56:48 +00:00
|
|
|
v = &Volume{dir: dirname, Collection: collection, Id: id, MemoryMapMaxSizeMB: memoryMapMaxSizeMB}
|
2014-09-20 19:38:59 +00:00
|
|
|
v.SuperBlock = SuperBlock{ReplicaPlacement: replicaPlacement, Ttl: ttl}
|
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-29 18:04:32 +00:00
|
|
|
v.needleMapKind = needleMapKind
|
2019-09-03 16:00:59 +00:00
|
|
|
e = v.load(true, true, needleMapKind, preallocate)
|
2013-01-21 03:44:23 +00:00
|
|
|
return
|
|
|
|
}
|
2014-12-26 07:36:33 +00:00
|
|
|
func (v *Volume) String() string {
|
|
|
|
return fmt.Sprintf("Id:%v, dir:%s, Collection:%s, dataFile:%v, nm:%v, readOnly:%v", v.Id, v.dir, v.Collection, v.dataFile, v.nm, v.readOnly)
|
|
|
|
}
|
|
|
|
|
2019-06-03 09:26:31 +00:00
|
|
|
func VolumeFileName(dir string, collection string, id int) (fileName string) {
|
2019-03-23 18:33:34 +00:00
|
|
|
idString := strconv.Itoa(id)
|
|
|
|
if collection == "" {
|
|
|
|
fileName = path.Join(dir, idString)
|
2014-02-07 01:32:06 +00:00
|
|
|
} else {
|
2019-03-23 18:33:34 +00:00
|
|
|
fileName = path.Join(dir, collection+"_"+idString)
|
2014-02-07 01:32:06 +00:00
|
|
|
}
|
|
|
|
return
|
2014-01-22 04:51:46 +00:00
|
|
|
}
|
2019-03-23 18:33:34 +00:00
|
|
|
func (v *Volume) FileName() (fileName string) {
|
2019-06-03 09:26:31 +00:00
|
|
|
return VolumeFileName(v.dir, v.Collection, int(v.Id))
|
2019-03-23 18:33:34 +00:00
|
|
|
}
|
2015-05-26 07:58:41 +00:00
|
|
|
func (v *Volume) DataFile() *os.File {
|
|
|
|
return v.dataFile
|
|
|
|
}
|
2014-02-07 01:32:06 +00:00
|
|
|
|
2019-04-19 04:43:36 +00:00
|
|
|
func (v *Volume) Version() needle.Version {
|
2014-08-25 18:37:00 +00:00
|
|
|
return v.SuperBlock.Version()
|
2012-12-18 01:51:39 +00:00
|
|
|
}
|
2016-07-03 06:56:49 +00:00
|
|
|
|
2019-04-19 07:39:34 +00:00
|
|
|
func (v *Volume) FileStat() (datSize uint64, idxSize uint64, modTime time.Time) {
|
2018-11-04 07:28:24 +00:00
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
|
|
|
|
|
|
|
if v.dataFile == nil {
|
2019-04-19 07:39:34 +00:00
|
|
|
return
|
2018-11-04 07:28:24 +00:00
|
|
|
}
|
|
|
|
|
2011-12-22 04:04:47 +00:00
|
|
|
stat, e := v.dataFile.Stat()
|
|
|
|
if e == nil {
|
2019-04-19 07:39:34 +00:00
|
|
|
return uint64(stat.Size()), v.nm.IndexFileSize(), stat.ModTime()
|
2011-12-22 04:04:47 +00:00
|
|
|
}
|
2014-10-21 08:27:06 +00:00
|
|
|
glog.V(0).Infof("Failed to read file size %s %v", v.dataFile.Name(), e)
|
2019-04-19 07:39:34 +00:00
|
|
|
return // -1 causes integer overflow and the volume to become unwritable.
|
2011-12-17 06:47:23 +00:00
|
|
|
}
|
2015-03-09 08:09:15 +00:00
|
|
|
|
2019-08-14 08:08:01 +00:00
|
|
|
func (v *Volume) ContentSize() uint64 {
|
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
|
|
|
return v.nm.ContentSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) DeletedSize() uint64 {
|
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
|
|
|
return v.nm.DeletedSize()
|
2019-04-10 11:41:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 07:19:18 +00:00
|
|
|
func (v *Volume) FileCount() uint64 {
|
2019-08-14 08:08:01 +00:00
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
2019-04-11 06:39:53 +00:00
|
|
|
return uint64(v.nm.FileCount())
|
2019-04-10 11:41:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 08:08:01 +00:00
|
|
|
func (v *Volume) DeletedCount() uint64 {
|
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
|
|
|
return uint64(v.nm.DeletedCount())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) MaxFileKey() types.NeedleId {
|
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
|
|
|
return v.nm.MaxFileKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) IndexFileSize() uint64 {
|
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
|
|
|
return v.nm.IndexFileSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) IndexFileContent() ([]byte, error) {
|
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
|
|
|
return v.nm.IndexFileContent()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) IndexFileName() string {
|
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
|
|
|
return v.nm.IndexFileName()
|
|
|
|
}
|
|
|
|
|
2015-03-09 08:09:15 +00:00
|
|
|
// Close cleanly shuts down this volume
|
2011-12-16 14:51:26 +00:00
|
|
|
func (v *Volume) Close() {
|
2015-05-23 17:16:01 +00:00
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
2018-11-04 07:28:24 +00:00
|
|
|
if v.nm != nil {
|
|
|
|
v.nm.Close()
|
|
|
|
v.nm = nil
|
|
|
|
}
|
|
|
|
if v.dataFile != nil {
|
|
|
|
_ = v.dataFile.Close()
|
|
|
|
v.dataFile = nil
|
2019-06-18 04:02:50 +00:00
|
|
|
stats.VolumeServerVolumeCounter.WithLabelValues(v.Collection, "volume").Dec()
|
2018-11-04 07:28:24 +00:00
|
|
|
}
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2015-03-09 08:09:15 +00:00
|
|
|
|
2012-11-20 08:54:37 +00:00
|
|
|
func (v *Volume) NeedToReplicate() bool {
|
2014-03-03 06:16:54 +00:00
|
|
|
return v.ReplicaPlacement.GetCopyCount() > 1
|
2012-11-12 09:26:18 +00:00
|
|
|
}
|
2011-12-16 14:51:26 +00:00
|
|
|
|
2014-09-20 19:38:59 +00:00
|
|
|
// volume is expired if modified time + volume ttl < now
|
|
|
|
// except when volume is empty
|
|
|
|
// or when the volume does not have a ttl
|
|
|
|
// or when volumeSizeLimit is 0 when server just starts
|
|
|
|
func (v *Volume) expired(volumeSizeLimit uint64) bool {
|
|
|
|
if volumeSizeLimit == 0 {
|
|
|
|
//skip if we don't know size limit
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if v.ContentSize() == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if v.Ttl == nil || v.Ttl.Minutes() == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2019-04-19 07:39:34 +00:00
|
|
|
glog.V(1).Infof("now:%v lastModified:%v", time.Now().Unix(), v.lastModifiedTsSeconds)
|
|
|
|
livedMinutes := (time.Now().Unix() - int64(v.lastModifiedTsSeconds)) / 60
|
2016-11-13 22:07:51 +00:00
|
|
|
glog.V(1).Infof("ttl:%v lived:%v", v.Ttl, livedMinutes)
|
2014-09-20 19:38:59 +00:00
|
|
|
if int64(v.Ttl.Minutes()) < livedMinutes {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait either maxDelayMinutes or 10% of ttl minutes
|
2019-01-17 01:17:19 +00:00
|
|
|
func (v *Volume) expiredLongEnough(maxDelayMinutes uint32) bool {
|
2014-09-20 19:38:59 +00:00
|
|
|
if v.Ttl == nil || v.Ttl.Minutes() == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
removalDelay := v.Ttl.Minutes() / 10
|
|
|
|
if removalDelay > maxDelayMinutes {
|
|
|
|
removalDelay = maxDelayMinutes
|
|
|
|
}
|
|
|
|
|
2019-04-19 07:39:34 +00:00
|
|
|
if uint64(v.Ttl.Minutes()+removalDelay)*60+v.lastModifiedTsSeconds < uint64(time.Now().Unix()) {
|
2014-09-20 19:38:59 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-03-18 03:27:08 +00:00
|
|
|
|
|
|
|
func (v *Volume) ToVolumeInformationMessage() *master_pb.VolumeInformationMessage {
|
2019-06-01 06:41:17 +00:00
|
|
|
size, _, modTime := v.FileStat()
|
2019-08-14 08:08:01 +00:00
|
|
|
|
2019-03-18 03:27:08 +00:00
|
|
|
return &master_pb.VolumeInformationMessage{
|
|
|
|
Id: uint32(v.Id),
|
2019-04-19 07:39:34 +00:00
|
|
|
Size: size,
|
2019-03-18 03:27:08 +00:00
|
|
|
Collection: v.Collection,
|
2019-08-14 08:08:01 +00:00
|
|
|
FileCount: uint64(v.FileCount()),
|
|
|
|
DeleteCount: uint64(v.DeletedCount()),
|
|
|
|
DeletedByteCount: v.DeletedSize(),
|
2019-03-18 03:27:08 +00:00
|
|
|
ReadOnly: v.readOnly,
|
|
|
|
ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
|
|
|
|
Version: uint32(v.Version()),
|
|
|
|
Ttl: v.Ttl.ToUint32(),
|
2019-04-19 19:29:49 +00:00
|
|
|
CompactRevision: uint32(v.SuperBlock.CompactionRevision),
|
2019-06-01 06:41:17 +00:00
|
|
|
ModifiedAtSecond: modTime.Unix(),
|
2019-03-18 03:27:08 +00:00
|
|
|
}
|
|
|
|
}
|