2016-07-03 14:56:49 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2019-04-19 12:43:36 +08:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
|
2019-09-12 21:18:21 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2019-12-18 17:21:21 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/stats"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/backend"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2019-12-24 04:48:20 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/super_block"
|
2019-12-29 04:28:58 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2016-07-03 14:56:49 +08:00
|
|
|
)
|
|
|
|
|
2019-12-19 16:44:46 +08:00
|
|
|
func loadVolumeWithoutIndex(dirname string, collection string, id needle.VolumeId, needleMapKind NeedleMapType) (v *Volume, err error) {
|
2016-07-03 14:56:49 +08:00
|
|
|
v = &Volume{dir: dirname, Collection: collection, Id: id}
|
2019-12-24 04:48:20 +08:00
|
|
|
v.SuperBlock = super_block.SuperBlock{}
|
2016-07-03 14:56:49 +08:00
|
|
|
v.needleMapKind = needleMapKind
|
2019-12-19 16:44:46 +08:00
|
|
|
err = v.load(false, false, needleMapKind, 0)
|
2016-07-03 14:56:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:44:46 +08:00
|
|
|
func (v *Volume) load(alsoLoadIndex bool, createDatIfMissing bool, needleMapKind NeedleMapType, preallocate int64) (err error) {
|
2016-07-03 14:56:49 +08:00
|
|
|
fileName := v.FileName()
|
2018-09-09 17:48:58 +08:00
|
|
|
alreadyHasSuperBlock := false
|
2016-07-03 14:56:49 +08:00
|
|
|
|
2020-01-09 01:45:26 +08:00
|
|
|
hasVolumeInfoFile := v.maybeLoadVolumeInfo() && v.volumeInfo.Version != 0
|
2019-12-29 03:35:27 +08:00
|
|
|
|
|
|
|
if v.HasRemoteFile() {
|
2019-12-19 16:42:46 +08:00
|
|
|
v.noWriteCanDelete = true
|
2019-12-29 03:35:27 +08:00
|
|
|
v.noWriteOrDelete = false
|
|
|
|
glog.V(0).Infof("loading volume %d from remote %v", v.Id, v.volumeInfo.Files)
|
|
|
|
v.LoadRemoteFile()
|
2019-12-03 07:08:28 +08:00
|
|
|
alreadyHasSuperBlock = true
|
2019-12-29 04:28:58 +08:00
|
|
|
} else if exists, canRead, canWrite, modifiedTime, fileSize := util.CheckFile(fileName + ".dat"); exists {
|
2019-12-03 07:08:28 +08:00
|
|
|
// open dat file
|
2016-07-03 14:56:49 +08:00
|
|
|
if !canRead {
|
|
|
|
return fmt.Errorf("cannot read Volume Data file %s.dat", fileName)
|
|
|
|
}
|
2019-11-09 16:10:59 +08:00
|
|
|
var dataFile *os.File
|
2016-07-03 14:56:49 +08:00
|
|
|
if canWrite {
|
2019-12-19 16:44:46 +08:00
|
|
|
dataFile, err = os.OpenFile(fileName+".dat", os.O_RDWR|os.O_CREATE, 0644)
|
2016-07-03 14:56:49 +08:00
|
|
|
} else {
|
|
|
|
glog.V(0).Infoln("opening " + fileName + ".dat in READONLY mode")
|
2019-12-19 16:44:46 +08:00
|
|
|
dataFile, err = os.Open(fileName + ".dat")
|
2019-12-19 16:42:46 +08:00
|
|
|
v.noWriteOrDelete = true
|
2016-07-03 14:56:49 +08:00
|
|
|
}
|
2019-08-21 11:26:01 +08:00
|
|
|
v.lastModifiedTsSeconds = uint64(modifiedTime.Unix())
|
2019-12-24 04:48:20 +08:00
|
|
|
if fileSize >= super_block.SuperBlockSize {
|
2018-09-09 17:48:58 +08:00
|
|
|
alreadyHasSuperBlock = true
|
|
|
|
}
|
2019-11-09 16:10:59 +08:00
|
|
|
v.DataBackend = backend.NewDiskFile(dataFile)
|
2016-07-03 14:56:49 +08:00
|
|
|
} else {
|
|
|
|
if createDatIfMissing {
|
2020-04-12 05:27:25 +08:00
|
|
|
v.DataBackend, err = backend.CreateVolumeFile(fileName+".dat", preallocate, v.MemoryMapMaxSizeMb)
|
2016-07-03 14:56:49 +08:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Volume Data file %s.dat does not exist.", fileName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:44:46 +08:00
|
|
|
if err != nil {
|
|
|
|
if !os.IsPermission(err) {
|
|
|
|
return fmt.Errorf("cannot load Volume Data %s.dat: %v", fileName, err)
|
2018-07-10 14:31:25 +08:00
|
|
|
} else {
|
2019-12-19 16:44:46 +08:00
|
|
|
return fmt.Errorf("load data file %s.dat: %v", fileName, err)
|
2016-07-03 14:56:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 17:48:58 +08:00
|
|
|
if alreadyHasSuperBlock {
|
2019-12-19 16:44:46 +08:00
|
|
|
err = v.readSuperBlock()
|
2016-07-03 14:56:49 +08:00
|
|
|
} else {
|
2019-10-09 15:02:50 +08:00
|
|
|
if !v.SuperBlock.Initialized() {
|
|
|
|
return fmt.Errorf("volume %s.dat not initialized", fileName)
|
|
|
|
}
|
2019-12-19 16:44:46 +08:00
|
|
|
err = v.maybeWriteSuperBlock()
|
2016-07-03 14:56:49 +08:00
|
|
|
}
|
2019-12-19 16:44:46 +08:00
|
|
|
if err == nil && alsoLoadIndex {
|
2016-07-03 14:56:49 +08:00
|
|
|
var indexFile *os.File
|
2019-12-19 16:42:46 +08:00
|
|
|
if v.noWriteOrDelete {
|
2019-12-25 06:55:50 +08:00
|
|
|
glog.V(0).Infoln("open to read file", fileName+".idx")
|
2019-12-19 16:44:46 +08:00
|
|
|
if indexFile, err = os.OpenFile(fileName+".idx", os.O_RDONLY, 0644); err != nil {
|
|
|
|
return fmt.Errorf("cannot read Volume Index %s.idx: %v", fileName, err)
|
2016-07-03 14:56:49 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
glog.V(1).Infoln("open to write file", fileName+".idx")
|
2019-12-19 16:44:46 +08:00
|
|
|
if indexFile, err = os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644); err != nil {
|
|
|
|
return fmt.Errorf("cannot write Volume Index %s.idx: %v", fileName, err)
|
2016-07-03 14:56:49 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-19 16:44:46 +08:00
|
|
|
if v.lastAppendAtNs, err = CheckVolumeDataIntegrity(v, indexFile); err != nil {
|
2019-12-19 16:42:46 +08:00
|
|
|
v.noWriteOrDelete = true
|
2019-12-19 16:44:46 +08:00
|
|
|
glog.V(0).Infof("volumeDataIntegrityChecking failed %v", err)
|
2016-07-03 14:56:49 +08:00
|
|
|
}
|
2019-12-18 17:21:21 +08:00
|
|
|
|
2020-07-04 07:34:31 +08:00
|
|
|
if v.noWriteOrDelete || v.noWriteCanDelete {
|
2019-12-19 16:44:46 +08:00
|
|
|
if v.nm, err = NewSortedFileNeedleMap(fileName, indexFile); err != nil {
|
2019-12-25 02:19:12 +08:00
|
|
|
glog.V(0).Infof("loading sorted db %s error: %v", fileName+".sdx", err)
|
2019-04-10 00:42:06 +08:00
|
|
|
}
|
2019-12-18 17:21:21 +08:00
|
|
|
} else {
|
|
|
|
switch needleMapKind {
|
|
|
|
case NeedleMapInMemory:
|
|
|
|
glog.V(0).Infoln("loading index", fileName+".idx", "to memory")
|
2019-12-19 16:44:46 +08:00
|
|
|
if v.nm, err = LoadCompactNeedleMap(indexFile); err != nil {
|
|
|
|
glog.V(0).Infof("loading index %s to memory error: %v", fileName+".idx", err)
|
2019-12-18 17:21:21 +08:00
|
|
|
}
|
|
|
|
case NeedleMapLevelDb:
|
|
|
|
glog.V(0).Infoln("loading leveldb", fileName+".ldb")
|
|
|
|
opts := &opt.Options{
|
|
|
|
BlockCacheCapacity: 2 * 1024 * 1024, // default value is 8MiB
|
|
|
|
WriteBuffer: 1 * 1024 * 1024, // default value is 4MiB
|
|
|
|
CompactionTableSizeMultiplier: 10, // default value is 1
|
|
|
|
}
|
2019-12-19 16:44:46 +08:00
|
|
|
if v.nm, err = NewLevelDbNeedleMap(fileName+".ldb", indexFile, opts); err != nil {
|
|
|
|
glog.V(0).Infof("loading leveldb %s error: %v", fileName+".ldb", err)
|
2019-12-18 17:21:21 +08:00
|
|
|
}
|
|
|
|
case NeedleMapLevelDbMedium:
|
|
|
|
glog.V(0).Infoln("loading leveldb medium", fileName+".ldb")
|
|
|
|
opts := &opt.Options{
|
|
|
|
BlockCacheCapacity: 4 * 1024 * 1024, // default value is 8MiB
|
|
|
|
WriteBuffer: 2 * 1024 * 1024, // default value is 4MiB
|
|
|
|
CompactionTableSizeMultiplier: 10, // default value is 1
|
|
|
|
}
|
2019-12-19 16:44:46 +08:00
|
|
|
if v.nm, err = NewLevelDbNeedleMap(fileName+".ldb", indexFile, opts); err != nil {
|
|
|
|
glog.V(0).Infof("loading leveldb %s error: %v", fileName+".ldb", err)
|
2019-12-18 17:21:21 +08:00
|
|
|
}
|
|
|
|
case NeedleMapLevelDbLarge:
|
|
|
|
glog.V(0).Infoln("loading leveldb large", fileName+".ldb")
|
|
|
|
opts := &opt.Options{
|
|
|
|
BlockCacheCapacity: 8 * 1024 * 1024, // default value is 8MiB
|
|
|
|
WriteBuffer: 4 * 1024 * 1024, // default value is 4MiB
|
|
|
|
CompactionTableSizeMultiplier: 10, // default value is 1
|
|
|
|
}
|
2019-12-19 16:44:46 +08:00
|
|
|
if v.nm, err = NewLevelDbNeedleMap(fileName+".ldb", indexFile, opts); err != nil {
|
|
|
|
glog.V(0).Infof("loading leveldb %s error: %v", fileName+".ldb", err)
|
2019-12-18 17:21:21 +08:00
|
|
|
}
|
2017-05-27 13:51:25 +08:00
|
|
|
}
|
2016-07-03 14:56:49 +08:00
|
|
|
}
|
|
|
|
}
|
2017-01-07 02:22:20 +08:00
|
|
|
|
2020-01-09 01:45:26 +08:00
|
|
|
if !hasVolumeInfoFile {
|
|
|
|
v.volumeInfo.Version = uint32(v.SuperBlock.Version)
|
|
|
|
v.SaveVolumeInfo()
|
|
|
|
}
|
|
|
|
|
2019-06-18 12:02:50 +08:00
|
|
|
stats.VolumeServerVolumeCounter.WithLabelValues(v.Collection, "volume").Inc()
|
2019-06-16 17:24:15 +08:00
|
|
|
|
2019-12-19 16:44:46 +08:00
|
|
|
return err
|
2016-07-03 14:56:49 +08:00
|
|
|
}
|