2014-08-26 02:37:00 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2019-09-12 21:18:21 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
2019-11-09 16:10:59 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/backend"
|
2019-09-12 21:18:21 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2019-10-22 14:03:48 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2014-08-26 02:37:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-06-25 02:37:08 +08:00
|
|
|
_SuperBlockSize = 8
|
2014-08-26 02:37:00 +08:00
|
|
|
)
|
|
|
|
|
2014-09-01 14:25:54 +08:00
|
|
|
/*
|
|
|
|
* Super block currently has 8 bytes allocated for each volume.
|
|
|
|
* Byte 0: version, 1 or 2
|
|
|
|
* Byte 1: Replica Placement strategy, 000, 001, 002, 010, etc
|
2014-09-21 03:38:59 +08:00
|
|
|
* Byte 2 and byte 3: Time to live. See TTL for definition
|
2015-05-09 14:34:14 +08:00
|
|
|
* Byte 4 and byte 5: The number of times the volume has been compacted.
|
2014-09-21 03:38:59 +08:00
|
|
|
* Rest bytes: Reserved
|
2014-09-01 14:25:54 +08:00
|
|
|
*/
|
2014-08-26 02:37:00 +08:00
|
|
|
type SuperBlock struct {
|
2019-04-20 03:29:49 +08:00
|
|
|
version needle.Version
|
|
|
|
ReplicaPlacement *ReplicaPlacement
|
|
|
|
Ttl *needle.TTL
|
|
|
|
CompactionRevision uint16
|
|
|
|
Extra *master_pb.SuperBlockExtra
|
|
|
|
extraSize uint16
|
2014-08-26 02:37:00 +08:00
|
|
|
}
|
|
|
|
|
2018-06-25 02:37:08 +08:00
|
|
|
func (s *SuperBlock) BlockSize() int {
|
2018-06-25 06:37:54 +08:00
|
|
|
switch s.version {
|
2019-04-19 12:43:36 +08:00
|
|
|
case needle.Version2, needle.Version3:
|
2018-06-25 06:19:57 +08:00
|
|
|
return _SuperBlockSize + int(s.extraSize)
|
|
|
|
}
|
2018-06-25 02:37:08 +08:00
|
|
|
return _SuperBlockSize
|
|
|
|
}
|
|
|
|
|
2019-04-19 12:43:36 +08:00
|
|
|
func (s *SuperBlock) Version() needle.Version {
|
2014-08-26 02:37:00 +08:00
|
|
|
return s.version
|
|
|
|
}
|
|
|
|
func (s *SuperBlock) Bytes() []byte {
|
2018-06-25 02:37:08 +08:00
|
|
|
header := make([]byte, _SuperBlockSize)
|
2014-08-26 02:37:00 +08:00
|
|
|
header[0] = byte(s.version)
|
|
|
|
header[1] = s.ReplicaPlacement.Byte()
|
2014-09-21 03:38:59 +08:00
|
|
|
s.Ttl.ToBytes(header[2:4])
|
2019-04-20 03:29:49 +08:00
|
|
|
util.Uint16toBytes(header[4:6], s.CompactionRevision)
|
2018-06-25 06:19:57 +08:00
|
|
|
|
|
|
|
if s.Extra != nil {
|
|
|
|
extraData, err := proto.Marshal(s.Extra)
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("cannot marshal super block extra %+v: %v", s.Extra, err)
|
|
|
|
}
|
|
|
|
extraSize := len(extraData)
|
2018-06-25 06:23:03 +08:00
|
|
|
if extraSize > 256*256-2 {
|
|
|
|
// reserve a couple of bits for future extension
|
2018-06-25 07:12:41 +08:00
|
|
|
glog.Fatalf("super block extra size is %d bigger than %d", extraSize, 256*256-2)
|
2018-06-25 06:19:57 +08:00
|
|
|
}
|
|
|
|
s.extraSize = uint16(extraSize)
|
|
|
|
util.Uint16toBytes(header[6:8], s.extraSize)
|
|
|
|
|
|
|
|
header = append(header, extraData...)
|
|
|
|
}
|
|
|
|
|
2014-08-26 02:37:00 +08:00
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
2019-10-09 15:02:50 +08:00
|
|
|
func (s *SuperBlock) Initialized() bool {
|
2019-10-09 15:07:18 +08:00
|
|
|
return s.ReplicaPlacement != nil && s.Ttl != nil
|
2019-10-09 15:02:50 +08:00
|
|
|
}
|
|
|
|
|
2014-08-26 02:37:00 +08:00
|
|
|
func (v *Volume) maybeWriteSuperBlock() error {
|
2019-08-28 21:48:06 +08:00
|
|
|
|
2019-11-09 16:10:59 +08:00
|
|
|
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)
|
2019-11-09 16:10:59 +08:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
if datSize == 0 {
|
|
|
|
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 {
|
2019-11-09 16:10:59 +08:00
|
|
|
v.DataBackend = backend.NewDiskFile(dataFile)
|
|
|
|
if _, e = v.DataBackend.WriteAt(v.SuperBlock.Bytes(), 0); e == nil {
|
2019-12-19 16:42:46 +08:00
|
|
|
v.noWriteOrDelete = false
|
|
|
|
v.noWriteCanDelete = false
|
2014-08-26 02:37:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 16:10:59 +08:00
|
|
|
return e
|
2014-08-26 02:37:00 +08:00
|
|
|
}
|
2018-06-25 02:37:08 +08:00
|
|
|
|
2014-08-26 02:37:00 +08:00
|
|
|
func (v *Volume) readSuperBlock() (err error) {
|
2019-10-29 15:35:16 +08:00
|
|
|
v.SuperBlock, err = ReadSuperBlock(v.DataBackend)
|
2014-08-26 02:37:00 +08:00
|
|
|
return err
|
|
|
|
}
|
2018-06-25 02:37:08 +08:00
|
|
|
|
|
|
|
// ReadSuperBlock reads from data file and load it into volume's super block
|
2019-11-29 10:33:18 +08:00
|
|
|
func ReadSuperBlock(datBackend backend.BackendStorageFile) (superBlock SuperBlock, err error) {
|
2019-08-28 21:48:06 +08:00
|
|
|
|
2018-06-25 02:37:08 +08:00
|
|
|
header := make([]byte, _SuperBlockSize)
|
2019-11-09 16:10:59 +08:00
|
|
|
if _, e := datBackend.ReadAt(header, 0); e != nil {
|
2019-12-09 11:44:16 +08:00
|
|
|
err = fmt.Errorf("cannot read volume %s super block: %v", datBackend.Name(), e)
|
2019-11-09 16:10:59 +08:00
|
|
|
return
|
2018-06-25 02:37:08 +08:00
|
|
|
}
|
2019-08-28 21:48:06 +08:00
|
|
|
|
2019-04-19 12:43:36 +08:00
|
|
|
superBlock.version = needle.Version(header[0])
|
2018-06-25 06:19:57 +08:00
|
|
|
if superBlock.ReplicaPlacement, err = NewReplicaPlacementFromByte(header[1]); err != nil {
|
|
|
|
err = fmt.Errorf("cannot read replica type: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2019-04-19 12:43:36 +08:00
|
|
|
superBlock.Ttl = needle.LoadTTLFromBytes(header[2:4])
|
2019-04-20 03:29:49 +08:00
|
|
|
superBlock.CompactionRevision = util.BytesToUint16(header[4:6])
|
2018-06-25 06:19:57 +08:00
|
|
|
superBlock.extraSize = util.BytesToUint16(header[6:8])
|
|
|
|
|
|
|
|
if superBlock.extraSize > 0 {
|
|
|
|
// read more
|
|
|
|
extraData := make([]byte, int(superBlock.extraSize))
|
|
|
|
superBlock.Extra = &master_pb.SuperBlockExtra{}
|
|
|
|
err = proto.Unmarshal(extraData, superBlock.Extra)
|
|
|
|
if err != nil {
|
2019-12-09 11:44:16 +08:00
|
|
|
err = fmt.Errorf("cannot read volume %s super block extra: %v", datBackend.Name(), err)
|
2018-06-25 06:19:57 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2018-06-25 02:37:08 +08:00
|
|
|
}
|