seaweedfs/weed/storage/volume_super_block.go

51 lines
1.4 KiB
Go
Raw Normal View History

package storage
import (
"fmt"
"os"
"github.com/chrislusf/seaweedfs/weed/glog"
"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"
)
func (v *Volume) maybeWriteSuperBlock() error {
datSize, _, e := v.DataBackend.GetStat()
if e != nil {
2019-12-09 11:44:16 +08:00
glog.V(0).Infof("failed to stat datafile %s: %v", v.DataBackend.Name(), e)
return e
}
if datSize == 0 {
2019-12-24 04:48:20 +08:00
v.SuperBlock.Version = needle.CurrentVersion
_, e = v.DataBackend.WriteAt(v.SuperBlock.Bytes(), 0)
if e != nil && os.IsPermission(e) {
//read-only, but zero length - recreate it!
var dataFile *os.File
2019-12-09 11:44:16 +08:00
if dataFile, e = os.Create(v.DataBackend.Name()); e == nil {
v.DataBackend = backend.NewDiskFile(dataFile)
if _, e = v.DataBackend.WriteAt(v.SuperBlock.Bytes(), 0); e == nil {
v.noWriteLock.Lock()
v.noWriteOrDelete = false
v.noWriteCanDelete = false
v.noWriteLock.Unlock()
}
}
}
}
return e
}
2018-06-25 02:37:08 +08:00
func (v *Volume) readSuperBlock() (err error) {
2019-12-24 04:48:20 +08:00
v.SuperBlock, err = super_block.ReadSuperBlock(v.DataBackend)
2020-02-27 08:52:57 +08:00
if v.volumeInfo != nil && v.volumeInfo.Replication != "" {
if replication, err := super_block.NewReplicaPlacementFromString(v.volumeInfo.Replication); err != nil {
return fmt.Errorf("Error parse volume %d replication %s : %v", v.Id, v.volumeInfo.Replication, err)
} else {
v.SuperBlock.ReplicaPlacement = replication
}
}
return err
}