2014-08-25 18:37:00 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-06-24 22:19:57 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-06-24 22:19:57 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2014-08-25 18:37:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-06-24 18:37:08 +00:00
|
|
|
_SuperBlockSize = 8
|
2014-08-25 18:37:00 +00:00
|
|
|
)
|
|
|
|
|
2014-09-01 06:25:54 +00: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-20 19:38:59 +00:00
|
|
|
* Byte 2 and byte 3: Time to live. See TTL for definition
|
2015-05-09 06:34:14 +00:00
|
|
|
* Byte 4 and byte 5: The number of times the volume has been compacted.
|
2014-09-20 19:38:59 +00:00
|
|
|
* Rest bytes: Reserved
|
2014-09-01 06:25:54 +00:00
|
|
|
*/
|
2014-08-25 18:37:00 +00:00
|
|
|
type SuperBlock struct {
|
|
|
|
version Version
|
|
|
|
ReplicaPlacement *ReplicaPlacement
|
2014-09-20 19:38:59 +00:00
|
|
|
Ttl *TTL
|
2015-05-09 06:34:14 +00:00
|
|
|
CompactRevision uint16
|
2018-06-24 22:19:57 +00:00
|
|
|
Extra *master_pb.SuperBlockExtra
|
|
|
|
extraSize uint16
|
2014-08-25 18:37:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 18:37:08 +00:00
|
|
|
func (s *SuperBlock) BlockSize() int {
|
2018-06-24 22:37:54 +00:00
|
|
|
switch s.version {
|
2018-07-24 08:36:04 +00:00
|
|
|
case Version2, Version3:
|
2018-06-24 22:19:57 +00:00
|
|
|
return _SuperBlockSize + int(s.extraSize)
|
|
|
|
}
|
2018-06-24 18:37:08 +00:00
|
|
|
return _SuperBlockSize
|
|
|
|
}
|
|
|
|
|
2014-08-25 18:37:00 +00:00
|
|
|
func (s *SuperBlock) Version() Version {
|
|
|
|
return s.version
|
|
|
|
}
|
|
|
|
func (s *SuperBlock) Bytes() []byte {
|
2018-06-24 18:37:08 +00:00
|
|
|
header := make([]byte, _SuperBlockSize)
|
2014-08-25 18:37:00 +00:00
|
|
|
header[0] = byte(s.version)
|
|
|
|
header[1] = s.ReplicaPlacement.Byte()
|
2014-09-20 19:38:59 +00:00
|
|
|
s.Ttl.ToBytes(header[2:4])
|
2016-04-10 07:24:22 +00:00
|
|
|
util.Uint16toBytes(header[4:6], s.CompactRevision)
|
2018-06-24 22:19:57 +00: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-24 22:23:03 +00:00
|
|
|
if extraSize > 256*256-2 {
|
|
|
|
// reserve a couple of bits for future extension
|
2018-06-24 23:12:41 +00:00
|
|
|
glog.Fatalf("super block extra size is %d bigger than %d", extraSize, 256*256-2)
|
2018-06-24 22:19:57 +00:00
|
|
|
}
|
|
|
|
s.extraSize = uint16(extraSize)
|
|
|
|
util.Uint16toBytes(header[6:8], s.extraSize)
|
|
|
|
|
|
|
|
header = append(header, extraData...)
|
|
|
|
}
|
|
|
|
|
2014-08-25 18:37:00 +00:00
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) maybeWriteSuperBlock() error {
|
|
|
|
stat, e := v.dataFile.Stat()
|
|
|
|
if e != nil {
|
2018-06-12 06:39:42 +00:00
|
|
|
glog.V(0).Infof("failed to stat datafile %s: %v", v.dataFile.Name(), e)
|
2014-08-25 18:37:00 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
if stat.Size() == 0 {
|
|
|
|
v.SuperBlock.version = CurrentVersion
|
|
|
|
_, e = v.dataFile.Write(v.SuperBlock.Bytes())
|
|
|
|
if e != nil && os.IsPermission(e) {
|
|
|
|
//read-only, but zero length - recreate it!
|
|
|
|
if v.dataFile, e = os.Create(v.dataFile.Name()); e == nil {
|
|
|
|
if _, e = v.dataFile.Write(v.SuperBlock.Bytes()); e == nil {
|
|
|
|
v.readOnly = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
2018-06-24 18:37:08 +00:00
|
|
|
|
2014-08-25 18:37:00 +00:00
|
|
|
func (v *Volume) readSuperBlock() (err error) {
|
2018-06-24 18:37:08 +00:00
|
|
|
v.SuperBlock, err = ReadSuperBlock(v.dataFile)
|
2014-08-25 18:37:00 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-06-24 18:37:08 +00:00
|
|
|
|
|
|
|
// ReadSuperBlock reads from data file and load it into volume's super block
|
|
|
|
func ReadSuperBlock(dataFile *os.File) (superBlock SuperBlock, err error) {
|
|
|
|
if _, err = dataFile.Seek(0, 0); err != nil {
|
|
|
|
err = fmt.Errorf("cannot seek to the beginning of %s: %v", dataFile.Name(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
header := make([]byte, _SuperBlockSize)
|
|
|
|
if _, e := dataFile.Read(header); e != nil {
|
|
|
|
err = fmt.Errorf("cannot read volume %s super block: %v", dataFile.Name(), e)
|
|
|
|
return
|
|
|
|
}
|
2018-06-24 22:19:57 +00:00
|
|
|
superBlock.version = Version(header[0])
|
|
|
|
if superBlock.ReplicaPlacement, err = NewReplicaPlacementFromByte(header[1]); err != nil {
|
|
|
|
err = fmt.Errorf("cannot read replica type: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
superBlock.Ttl = LoadTTLFromBytes(header[2:4])
|
|
|
|
superBlock.CompactRevision = util.BytesToUint16(header[4:6])
|
|
|
|
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 {
|
2018-06-24 22:37:54 +00:00
|
|
|
err = fmt.Errorf("cannot read volume %s super block extra: %v", dataFile.Name(), err)
|
2018-06-24 22:19:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2018-06-24 18:37:08 +00:00
|
|
|
}
|