2011-12-16 22:51:26 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2013-07-14 04:51:47 +08:00
|
|
|
"bytes"
|
2012-11-07 17:51:43 +08:00
|
|
|
"errors"
|
2012-11-20 17:45:36 +08:00
|
|
|
"fmt"
|
2013-01-21 11:44:23 +08:00
|
|
|
"io"
|
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
|
|
|
|
2015-04-17 03:18:06 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/go/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
|
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
|
|
|
func NewVolume(dirname string, collection string, id VolumeId, needleMapKind NeedleMapType, replicaPlacement *ReplicaPlacement, ttl *TTL) (v *Volume, e error) {
|
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
|
|
|
|
e = v.load(true, true, needleMapKind)
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func loadVolumeWithoutIndex(dirname string, collection string, id VolumeId, needleMapKind NeedleMapType) (v *Volume, e error) {
|
2013-11-12 18:21:22 +08:00
|
|
|
v = &Volume{dir: dirname, Collection: collection, Id: id}
|
2014-03-03 14:16:54 +08:00
|
|
|
v.SuperBlock = SuperBlock{}
|
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
|
|
|
|
e = v.load(false, false, needleMapKind)
|
2012-11-07 17:51:43 +08:00
|
|
|
return
|
|
|
|
}
|
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
|
|
|
|
}
|
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
|
|
|
func (v *Volume) load(alsoLoadIndex bool, createDatIfMissing bool, needleMapKind NeedleMapType) error {
|
2012-11-07 17:51:43 +08:00
|
|
|
var e error
|
2014-01-22 12:51:46 +08:00
|
|
|
fileName := v.FileName()
|
2014-02-07 09:32:06 +08:00
|
|
|
|
2014-09-21 03:38:59 +08:00
|
|
|
if exists, canRead, canWrite, modifiedTime := checkFile(fileName + ".dat"); exists {
|
2014-02-07 09:32:06 +08:00
|
|
|
if !canRead {
|
|
|
|
return fmt.Errorf("cannot read Volume Data file %s.dat", fileName)
|
|
|
|
}
|
|
|
|
if canWrite {
|
|
|
|
v.dataFile, e = os.OpenFile(fileName+".dat", os.O_RDWR|os.O_CREATE, 0644)
|
2014-09-21 03:38:59 +08:00
|
|
|
v.lastModifiedTime = uint64(modifiedTime.Unix())
|
2014-02-07 09:32:06 +08:00
|
|
|
} else {
|
|
|
|
glog.V(0).Infoln("opening " + fileName + ".dat in READONLY mode")
|
|
|
|
v.dataFile, e = os.Open(fileName + ".dat")
|
|
|
|
v.readOnly = true
|
|
|
|
}
|
2013-08-13 14:48:10 +08:00
|
|
|
} else {
|
2014-02-07 09:32:06 +08:00
|
|
|
if createDatIfMissing {
|
|
|
|
v.dataFile, e = os.OpenFile(fileName+".dat", os.O_RDWR|os.O_CREATE, 0644)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Volume Data file %s.dat does not exist.", fileName)
|
|
|
|
}
|
2013-08-13 07:53:32 +08:00
|
|
|
}
|
2014-02-07 09:32:06 +08:00
|
|
|
|
2011-12-16 22:51:26 +08:00
|
|
|
if e != nil {
|
2013-04-15 10:30:26 +08:00
|
|
|
if !os.IsPermission(e) {
|
2014-10-21 16:27:06 +08:00
|
|
|
return fmt.Errorf("cannot load Volume Data %s.dat: %v", fileName, e)
|
2013-04-15 10:30:26 +08:00
|
|
|
}
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2013-08-13 07:53:32 +08:00
|
|
|
|
2014-03-03 14:16:54 +08:00
|
|
|
if v.ReplicaPlacement == nil {
|
2013-02-11 06:00:06 +08:00
|
|
|
e = v.readSuperBlock()
|
2012-09-13 16:33:47 +08:00
|
|
|
} else {
|
2013-02-11 06:00:06 +08:00
|
|
|
e = v.maybeWriteSuperBlock()
|
2012-09-13 16:33:47 +08:00
|
|
|
}
|
2013-02-11 06:00:06 +08:00
|
|
|
if e == nil && alsoLoadIndex {
|
2013-08-13 07:53:32 +08:00
|
|
|
var indexFile *os.File
|
2013-08-12 02:38:55 +08:00
|
|
|
if v.readOnly {
|
|
|
|
glog.V(1).Infoln("open to read file", fileName+".idx")
|
2013-08-13 07:53:32 +08:00
|
|
|
if indexFile, e = os.OpenFile(fileName+".idx", os.O_RDONLY, 0644); e != nil {
|
2014-10-21 16:27:06 +08:00
|
|
|
return fmt.Errorf("cannot read Volume Index %s.idx: %v", fileName, e)
|
2013-08-12 02:38:55 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
glog.V(1).Infoln("open to write file", fileName+".idx")
|
2013-08-13 07:53:32 +08:00
|
|
|
if indexFile, e = os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644); e != nil {
|
2014-10-21 16:27:06 +08:00
|
|
|
return fmt.Errorf("cannot write Volume Index %s.idx: %v", fileName, e)
|
2013-08-12 02:38:55 +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
|
|
|
switch needleMapKind {
|
|
|
|
case NeedleMapInMemory:
|
2015-03-28 07:34:58 +08:00
|
|
|
glog.V(0).Infoln("loading index file", fileName+".idx", "readonly", v.readOnly)
|
|
|
|
if v.nm, e = LoadNeedleMap(indexFile); e != nil {
|
|
|
|
glog.V(0).Infof("loading index %s error: %v", fileName+".idx", e)
|
|
|
|
}
|
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
|
|
|
case NeedleMapLevelDb:
|
2015-03-28 07:34:58 +08:00
|
|
|
glog.V(0).Infoln("loading leveldb file", fileName+".ldb")
|
|
|
|
if v.nm, e = NewLevelDbNeedleMap(fileName+".ldb", indexFile); e != nil {
|
|
|
|
glog.V(0).Infof("loading leveldb %s error: %v", fileName+".ldb", e)
|
|
|
|
}
|
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
|
|
|
case NeedleMapBoltDb:
|
|
|
|
glog.V(0).Infoln("loading boltdb file", fileName+".bdb")
|
|
|
|
if v.nm, e = NewBoltDbNeedleMap(fileName+".bdb", indexFile); e != nil {
|
|
|
|
glog.V(0).Infof("loading boltdb %s error: %v", fileName+".bdb", e)
|
|
|
|
}
|
2013-08-09 14:57:22 +08:00
|
|
|
}
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2013-02-11 06:00:06 +08:00
|
|
|
return e
|
2011-12-16 22:51:26 +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
|
|
|
}
|
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)
|
2011-12-22 12:04:47 +08:00
|
|
|
return -1
|
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
|
|
|
|
2015-03-09 16:09:15 +08:00
|
|
|
// isFileUnchanged checks whether this needle to write is same as last one.
|
|
|
|
// It requires serialized access in the same volume.
|
2013-07-12 13:44:59 +08:00
|
|
|
func (v *Volume) isFileUnchanged(n *Needle) bool {
|
2015-07-12 03:20:39 +08:00
|
|
|
if v.Ttl.String() != "" {
|
2015-07-11 00:43:49 +08:00
|
|
|
return true
|
|
|
|
}
|
2013-07-12 13:44:59 +08:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
|
|
|
if ok && nv.Offset > 0 {
|
|
|
|
oldNeedle := new(Needle)
|
2015-05-26 15:58:41 +08:00
|
|
|
err := oldNeedle.ReadData(v.dataFile, int64(nv.Offset)*NeedlePaddingSize, nv.Size, v.Version())
|
2015-04-15 14:05:33 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("Failed to check updated file %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2013-07-14 04:51:47 +08:00
|
|
|
if oldNeedle.Checksum == n.Checksum && bytes.Equal(oldNeedle.Data, n.Data) {
|
2014-12-26 15:36:33 +08:00
|
|
|
n.DataSize = oldNeedle.DataSize
|
2013-07-12 13:44:59 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2014-03-11 02:43:54 +08:00
|
|
|
|
2015-03-09 16:09:15 +08:00
|
|
|
// Destroy removes everything related to this volume
|
2014-03-11 02:43:54 +08:00
|
|
|
func (v *Volume) Destroy() (err error) {
|
|
|
|
if v.readOnly {
|
2014-04-17 15:16:44 +08:00
|
|
|
err = fmt.Errorf("%s is read-only", v.dataFile.Name())
|
2014-03-11 02:43:54 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
v.Close()
|
|
|
|
err = os.Remove(v.dataFile.Name())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = v.nm.Destroy()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-26 15:58:41 +08:00
|
|
|
// AppendBlob append a blob to end of the data file, used in replication
|
|
|
|
func (v *Volume) AppendBlob(b []byte) (offset int64, err error) {
|
|
|
|
if v.readOnly {
|
|
|
|
err = fmt.Errorf("%s is read-only", v.dataFile.Name())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
|
|
|
if offset, err = v.dataFile.Seek(0, 2); err != nil {
|
|
|
|
glog.V(0).Infof("failed to seek the end of file: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//ensure file writing starting from aligned positions
|
|
|
|
if offset%NeedlePaddingSize != 0 {
|
|
|
|
offset = offset + (NeedlePaddingSize - offset%NeedlePaddingSize)
|
|
|
|
if offset, err = v.dataFile.Seek(offset, 0); err != nil {
|
|
|
|
glog.V(0).Infof("failed to align in datafile %s: %v", v.dataFile.Name(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v.dataFile.Write(b)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-01-20 19:40:04 +08:00
|
|
|
func (v *Volume) write(n *Needle) (size uint32, err error) {
|
2014-03-24 12:57:10 +08:00
|
|
|
glog.V(4).Infof("writing needle %s", NewFileIdFromNeedle(v.Id, n).String())
|
2013-04-15 10:30:26 +08:00
|
|
|
if v.readOnly {
|
2014-04-17 15:16:44 +08:00
|
|
|
err = fmt.Errorf("%s is read-only", v.dataFile.Name())
|
2013-04-15 10:30:26 +08:00
|
|
|
return
|
|
|
|
}
|
2015-05-24 01:16:01 +08:00
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
2013-07-12 13:44:59 +08:00
|
|
|
if v.isFileUnchanged(n) {
|
2014-12-26 15:36:33 +08:00
|
|
|
size = n.DataSize
|
2013-11-01 03:55:19 +08:00
|
|
|
glog.V(4).Infof("needle is unchanged!")
|
2013-07-12 13:44:59 +08:00
|
|
|
return
|
|
|
|
}
|
2013-01-20 19:40:04 +08:00
|
|
|
var offset int64
|
|
|
|
if offset, err = v.dataFile.Seek(0, 2); err != nil {
|
2015-01-13 18:46:56 +08:00
|
|
|
glog.V(0).Infof("failed to seek the end of file: %v", err)
|
2013-01-20 19:40:04 +08:00
|
|
|
return
|
|
|
|
}
|
2013-07-06 07:05:35 +08:00
|
|
|
|
|
|
|
//ensure file writing starting from aligned positions
|
|
|
|
if offset%NeedlePaddingSize != 0 {
|
|
|
|
offset = offset + (NeedlePaddingSize - offset%NeedlePaddingSize)
|
|
|
|
if offset, err = v.dataFile.Seek(offset, 0); err != nil {
|
2014-10-21 16:27:06 +08:00
|
|
|
glog.V(0).Infof("failed to align in datafile %s: %v", v.dataFile.Name(), err)
|
2013-07-06 07:05:35 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-21 11:44:23 +08:00
|
|
|
if size, err = n.Append(v.dataFile, v.Version()); err != nil {
|
2013-02-27 14:54:22 +08:00
|
|
|
if e := v.dataFile.Truncate(offset); e != nil {
|
2014-10-21 16:27:06 +08:00
|
|
|
err = fmt.Errorf("%s\ncannot truncate %s: %v", err, v.dataFile.Name(), e)
|
2013-02-27 14:54:22 +08:00
|
|
|
}
|
2013-01-20 19:40:04 +08:00
|
|
|
return
|
|
|
|
}
|
2012-08-24 14:06:15 +08:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2013-01-20 19:40:04 +08:00
|
|
|
if !ok || int64(nv.Offset)*NeedlePaddingSize < offset {
|
2015-03-28 07:34:58 +08:00
|
|
|
if err = v.nm.Put(n.Id, uint32(offset/NeedlePaddingSize), n.Size); err != nil {
|
2014-10-21 16:27:06 +08:00
|
|
|
glog.V(4).Infof("failed to save in needle map %d: %v", n.Id, err)
|
2013-11-01 03:55:19 +08:00
|
|
|
}
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2014-09-21 03:38:59 +08:00
|
|
|
if v.lastModifiedTime < n.LastModified {
|
|
|
|
v.lastModifiedTime = n.LastModified
|
|
|
|
}
|
2013-01-20 19:40:04 +08:00
|
|
|
return
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2013-07-06 07:05:35 +08:00
|
|
|
|
2013-01-20 19:40:04 +08:00
|
|
|
func (v *Volume) delete(n *Needle) (uint32, error) {
|
2014-03-24 12:57:10 +08:00
|
|
|
glog.V(4).Infof("delete needle %s", NewFileIdFromNeedle(v.Id, n).String())
|
2013-04-15 10:30:26 +08:00
|
|
|
if v.readOnly {
|
2014-04-17 15:16:44 +08:00
|
|
|
return 0, fmt.Errorf("%s is read-only", v.dataFile.Name())
|
2013-04-15 10:30:26 +08:00
|
|
|
}
|
2015-05-24 01:16:01 +08:00
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
2012-08-24 14:06:15 +08:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2013-01-03 07:39:55 +08:00
|
|
|
//fmt.Println("key", n.Id, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
|
2012-01-19 08:49:41 +08:00
|
|
|
if ok {
|
2013-07-29 13:49:17 +08:00
|
|
|
size := nv.Size
|
2013-08-09 14:57:22 +08:00
|
|
|
if err := v.nm.Delete(n.Id); err != nil {
|
2013-07-29 13:49:17 +08:00
|
|
|
return size, err
|
2013-02-27 14:54:22 +08:00
|
|
|
}
|
2013-08-09 14:57:22 +08:00
|
|
|
if _, err := v.dataFile.Seek(0, 2); err != nil {
|
2013-07-29 13:49:17 +08:00
|
|
|
return size, err
|
2013-02-27 14:54:22 +08:00
|
|
|
}
|
2015-03-09 16:09:15 +08:00
|
|
|
n.Data = nil
|
2013-07-29 13:53:25 +08:00
|
|
|
_, err := n.Append(v.dataFile, v.Version())
|
2013-07-29 13:49:17 +08:00
|
|
|
return size, err
|
2012-01-19 08:49:41 +08:00
|
|
|
}
|
2013-01-20 19:40:04 +08:00
|
|
|
return 0, nil
|
2012-01-19 08:49:41 +08:00
|
|
|
}
|
2012-11-24 09:03:27 +08:00
|
|
|
|
2015-05-26 15:58:41 +08:00
|
|
|
// read fills in Needle content by looking up n.Id from NeedleMapper
|
|
|
|
func (v *Volume) readNeedle(n *Needle) (int, error) {
|
2012-08-24 14:06:15 +08:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2014-09-21 03:38:59 +08:00
|
|
|
if !ok || nv.Offset == 0 {
|
|
|
|
return -1, errors.New("Not Found")
|
|
|
|
}
|
2015-05-26 15:58:41 +08:00
|
|
|
err := n.ReadData(v.dataFile, int64(nv.Offset)*NeedlePaddingSize, nv.Size, v.Version())
|
2014-09-21 03:38:59 +08:00
|
|
|
if err != nil {
|
2015-05-26 15:58:41 +08:00
|
|
|
return 0, err
|
2014-09-21 03:38:59 +08:00
|
|
|
}
|
2015-05-26 15:58:41 +08:00
|
|
|
bytesRead := len(n.Data)
|
2014-09-21 03:38:59 +08:00
|
|
|
if !n.HasTtl() {
|
2015-05-26 15:58:41 +08:00
|
|
|
return bytesRead, nil
|
2014-09-21 03:38:59 +08:00
|
|
|
}
|
|
|
|
ttlMinutes := n.Ttl.Minutes()
|
|
|
|
if ttlMinutes == 0 {
|
|
|
|
return bytesRead, nil
|
|
|
|
}
|
|
|
|
if !n.HasLastModifiedDate() {
|
|
|
|
return bytesRead, nil
|
|
|
|
}
|
|
|
|
if uint64(time.Now().Unix()) < n.LastModified+uint64(ttlMinutes*60) {
|
|
|
|
return bytesRead, nil
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2012-09-27 11:30:05 +08:00
|
|
|
return -1, errors.New("Not Found")
|
2011-12-24 16:40:56 +08:00
|
|
|
}
|
2012-11-07 17:51:43 +08:00
|
|
|
|
2013-11-12 18:21:22 +08:00
|
|
|
func ScanVolumeFile(dirname string, collection string, id VolumeId,
|
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
|
|
|
needleMapKind NeedleMapType,
|
2013-01-21 11:44:23 +08:00
|
|
|
visitSuperBlock func(SuperBlock) error,
|
2014-06-01 08:10:51 +08:00
|
|
|
readNeedleBody bool,
|
2013-09-20 02:06:14 +08:00
|
|
|
visitNeedle func(n *Needle, offset int64) error) (err error) {
|
2013-01-21 11:44:23 +08:00
|
|
|
var v *Volume
|
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
|
|
|
if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
|
2015-01-14 09:04:41 +08:00
|
|
|
return fmt.Errorf("Failed to load volume %d: %v", id, err)
|
2012-11-07 17:51:43 +08:00
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
if err = visitSuperBlock(v.SuperBlock); err != nil {
|
2015-05-09 14:44:11 +08:00
|
|
|
return fmt.Errorf("Failed to process volume %d super block: %v", id, err)
|
2012-11-07 17:51:43 +08:00
|
|
|
}
|
|
|
|
|
2013-01-21 11:44:23 +08:00
|
|
|
version := v.Version()
|
2012-11-07 17:51:43 +08:00
|
|
|
|
2013-09-20 02:06:14 +08:00
|
|
|
offset := int64(SuperBlockSize)
|
2014-03-19 14:48:01 +08:00
|
|
|
n, rest, e := ReadNeedleHeader(v.dataFile, version, offset)
|
2013-01-21 11:44:23 +08:00
|
|
|
if e != nil {
|
2014-10-21 16:27:06 +08:00
|
|
|
err = fmt.Errorf("cannot read needle header: %v", e)
|
2013-01-21 11:44:23 +08:00
|
|
|
return
|
2013-01-20 19:40:04 +08:00
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
for n != nil {
|
2014-06-01 08:10:51 +08:00
|
|
|
if readNeedleBody {
|
|
|
|
if err = n.ReadNeedleBody(v.dataFile, version, offset+int64(NeedleHeaderSize), rest); err != nil {
|
2015-03-09 16:09:15 +08:00
|
|
|
glog.V(0).Infof("cannot read needle body: %v", err)
|
|
|
|
//err = fmt.Errorf("cannot read needle body: %v", err)
|
|
|
|
//return
|
|
|
|
}
|
|
|
|
if n.DataSize >= n.Size {
|
|
|
|
// this should come from a bug reported on #87 and #93
|
|
|
|
// fixed in v0.69
|
|
|
|
// remove this whole "if" clause later, long after 0.69
|
|
|
|
oldRest, oldSize := rest, n.Size
|
|
|
|
padding := NeedlePaddingSize - ((n.Size + NeedleHeaderSize + NeedleChecksumSize) % NeedlePaddingSize)
|
|
|
|
n.Size = 0
|
|
|
|
rest = n.Size + NeedleChecksumSize + padding
|
|
|
|
if rest%NeedlePaddingSize != 0 {
|
|
|
|
rest += (NeedlePaddingSize - rest%NeedlePaddingSize)
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("Adjusting n.Size %d=>0 rest:%d=>%d %+v", oldSize, oldRest, rest, n)
|
2014-06-01 08:10:51 +08:00
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
}
|
|
|
|
if err = visitNeedle(n, offset); err != nil {
|
2015-03-09 16:09:15 +08:00
|
|
|
glog.V(0).Infof("visit needle error: %v", err)
|
2013-01-21 11:44:23 +08:00
|
|
|
}
|
2014-03-19 19:48:13 +08:00
|
|
|
offset += int64(NeedleHeaderSize) + int64(rest)
|
2015-03-09 16:09:15 +08:00
|
|
|
glog.V(4).Infof("==> new entry offset %d", offset)
|
2014-03-19 14:48:01 +08:00
|
|
|
if n, rest, err = ReadNeedleHeader(v.dataFile, version, offset); err != nil {
|
2013-01-21 11:44:23 +08:00
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
2014-10-21 16:27:06 +08:00
|
|
|
return fmt.Errorf("cannot read needle header: %v", err)
|
2013-01-21 11:44:23 +08:00
|
|
|
}
|
2015-03-09 16:09:15 +08:00
|
|
|
glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
|
2012-11-07 17:51:43 +08:00
|
|
|
}
|
|
|
|
|
2013-01-21 11:44:23 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
func checkFile(filename string) (exists, canRead, canWrite bool, modTime time.Time) {
|
|
|
|
exists = true
|
|
|
|
fi, err := os.Stat(filename)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
exists = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if fi.Mode()&0400 != 0 {
|
|
|
|
canRead = true
|
|
|
|
}
|
|
|
|
if fi.Mode()&0200 != 0 {
|
|
|
|
canWrite = true
|
|
|
|
}
|
|
|
|
modTime = fi.ModTime()
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
glog.V(0).Infof("now:%v lastModified:%v", time.Now().Unix(), v.lastModifiedTime)
|
|
|
|
livedMinutes := (time.Now().Unix() - int64(v.lastModifiedTime)) / 60
|
|
|
|
glog.V(0).Infof("ttl:%v lived:%v", v.Ttl, livedMinutes)
|
|
|
|
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
|
|
|
|
}
|