2011-12-16 22:51:26 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2012-11-20 17:45:36 +08:00
|
|
|
"fmt"
|
2011-12-16 22:51:26 +08:00
|
|
|
"os"
|
|
|
|
"path"
|
2011-12-24 16:40:56 +08:00
|
|
|
"sync"
|
2013-08-13 07:53:32 +08:00
|
|
|
"time"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2011-12-16 22:51:26 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Volume struct {
|
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
|
|
|
Id VolumeId
|
|
|
|
dir string
|
|
|
|
Collection string
|
|
|
|
dataFile *os.File
|
|
|
|
nm NeedleMapper
|
|
|
|
needleMapKind NeedleMapType
|
|
|
|
readOnly bool
|
2011-12-16 22:51:26 +08:00
|
|
|
|
2013-01-21 11:44:23 +08:00
|
|
|
SuperBlock
|
2012-11-20 17:45:36 +08:00
|
|
|
|
2015-05-24 01:16:01 +08:00
|
|
|
dataFileAccessLock sync.Mutex
|
|
|
|
lastModifiedTime uint64 //unix time in seconds
|
2016-09-29 13:57:23 +08:00
|
|
|
|
2016-10-07 16:22:24 +08:00
|
|
|
lastCompactIndexOffset uint64
|
|
|
|
lastCompactRevision uint16
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
|
|
|
|
2017-01-09 03:01:46 +08:00
|
|
|
func NewVolume(dirname string, collection string, id VolumeId, needleMapKind NeedleMapType, replicaPlacement *ReplicaPlacement, ttl *TTL, preallocate int64) (v *Volume, e error) {
|
2018-06-24 07:48:19 +08:00
|
|
|
// if replicaPlacement is nil, the superblock will be loaded from disk
|
2013-11-12 18:21:22 +08:00
|
|
|
v = &Volume{dir: dirname, Collection: collection, Id: id}
|
2014-09-21 03:38:59 +08: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-30 02:04:32 +08:00
|
|
|
v.needleMapKind = needleMapKind
|
2017-01-09 03:01:46 +08:00
|
|
|
e = v.load(true, true, needleMapKind, preallocate)
|
2013-01-21 11:44:23 +08:00
|
|
|
return
|
|
|
|
}
|
2014-12-26 15:36:33 +08: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)
|
|
|
|
}
|
|
|
|
|
2014-01-22 12:51:46 +08:00
|
|
|
func (v *Volume) FileName() (fileName string) {
|
2014-02-07 09:32:06 +08:00
|
|
|
if v.Collection == "" {
|
|
|
|
fileName = path.Join(v.dir, v.Id.String())
|
|
|
|
} else {
|
|
|
|
fileName = path.Join(v.dir, v.Collection+"_"+v.Id.String())
|
|
|
|
}
|
|
|
|
return
|
2014-01-22 12:51:46 +08:00
|
|
|
}
|
2015-05-26 15:58:41 +08:00
|
|
|
func (v *Volume) DataFile() *os.File {
|
|
|
|
return v.dataFile
|
|
|
|
}
|
2014-02-07 09:32:06 +08:00
|
|
|
|
2012-12-18 09:51:39 +08:00
|
|
|
func (v *Volume) Version() Version {
|
2014-08-26 02:37:00 +08:00
|
|
|
return v.SuperBlock.Version()
|
2012-12-18 09:51:39 +08:00
|
|
|
}
|
2016-07-03 14:56:49 +08:00
|
|
|
|
2011-12-18 15:22:04 +08:00
|
|
|
func (v *Volume) Size() int64 {
|
2011-12-22 12:04:47 +08:00
|
|
|
stat, e := v.dataFile.Stat()
|
|
|
|
if e == nil {
|
2012-06-29 15:53:47 +08:00
|
|
|
return stat.Size()
|
2011-12-22 12:04:47 +08:00
|
|
|
}
|
2014-10-21 16:27:06 +08:00
|
|
|
glog.V(0).Infof("Failed to read file size %s %v", v.dataFile.Name(), e)
|
2018-04-18 03:26:48 +08:00
|
|
|
return 0 // -1 causes integer overflow and the volume to become unwritable.
|
2011-12-17 14:47:23 +08:00
|
|
|
}
|
2015-03-09 16:09:15 +08:00
|
|
|
|
|
|
|
// Close cleanly shuts down this volume
|
2011-12-16 22:51:26 +08:00
|
|
|
func (v *Volume) Close() {
|
2015-05-24 01:16:01 +08:00
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
2011-12-22 12:04:47 +08:00
|
|
|
v.nm.Close()
|
2013-02-27 14:54:22 +08:00
|
|
|
_ = v.dataFile.Close()
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2015-03-09 16:09:15 +08:00
|
|
|
|
2012-11-20 16:54:37 +08:00
|
|
|
func (v *Volume) NeedToReplicate() bool {
|
2014-03-03 14:16:54 +08:00
|
|
|
return v.ReplicaPlacement.GetCopyCount() > 1
|
2012-11-12 17:26:18 +08:00
|
|
|
}
|
2011-12-16 22:51:26 +08:00
|
|
|
|
2012-12-21 14:32:21 +08:00
|
|
|
func (v *Volume) ContentSize() uint64 {
|
2013-04-18 15:23:14 +08:00
|
|
|
return v.nm.ContentSize()
|
2012-12-04 14:54:08 +08:00
|
|
|
}
|
2013-08-13 07:53:32 +08:00
|
|
|
|
2014-09-21 03:38:59 +08: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
|
|
|
|
}
|
2016-11-14 06:07:51 +08:00
|
|
|
glog.V(1).Infof("now:%v lastModified:%v", time.Now().Unix(), v.lastModifiedTime)
|
2014-09-21 03:38:59 +08:00
|
|
|
livedMinutes := (time.Now().Unix() - int64(v.lastModifiedTime)) / 60
|
2016-11-14 06:07:51 +08:00
|
|
|
glog.V(1).Infof("ttl:%v lived:%v", v.Ttl, livedMinutes)
|
2014-09-21 03:38:59 +08:00
|
|
|
if int64(v.Ttl.Minutes()) < livedMinutes {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait either maxDelayMinutes or 10% of ttl minutes
|
|
|
|
func (v *Volume) exiredLongEnough(maxDelayMinutes uint32) bool {
|
|
|
|
if v.Ttl == nil || v.Ttl.Minutes() == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
removalDelay := v.Ttl.Minutes() / 10
|
|
|
|
if removalDelay > maxDelayMinutes {
|
|
|
|
removalDelay = maxDelayMinutes
|
|
|
|
}
|
|
|
|
|
|
|
|
if uint64(v.Ttl.Minutes()+removalDelay)*60+v.lastModifiedTime < uint64(time.Now().Unix()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|