2014-08-25 18:37:00 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2015-04-16 19:18:06 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/go/glog"
|
2014-08-25 18:37:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SuperBlockSize = 8
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
* 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
|
2014-08-25 18:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SuperBlock) Version() Version {
|
|
|
|
return s.version
|
|
|
|
}
|
|
|
|
func (s *SuperBlock) Bytes() []byte {
|
|
|
|
header := make([]byte, SuperBlockSize)
|
|
|
|
header[0] = byte(s.version)
|
|
|
|
header[1] = s.ReplicaPlacement.Byte()
|
2014-09-20 19:38:59 +00:00
|
|
|
s.Ttl.ToBytes(header[2:4])
|
2014-08-25 18:37:00 +00:00
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) maybeWriteSuperBlock() error {
|
|
|
|
stat, e := v.dataFile.Stat()
|
|
|
|
if e != nil {
|
2015-01-14 01:04:41 +00:00
|
|
|
glog.V(0).Infof("failed to stat datafile %s: %v", v.dataFile, 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
|
|
|
|
}
|
|
|
|
func (v *Volume) readSuperBlock() (err error) {
|
|
|
|
if _, err = v.dataFile.Seek(0, 0); err != nil {
|
2015-01-14 01:04:41 +00:00
|
|
|
return fmt.Errorf("cannot seek to the beginning of %s: %v", v.dataFile.Name(), err)
|
2014-08-25 18:37:00 +00:00
|
|
|
}
|
|
|
|
header := make([]byte, SuperBlockSize)
|
|
|
|
if _, e := v.dataFile.Read(header); e != nil {
|
2015-01-14 01:04:41 +00:00
|
|
|
return fmt.Errorf("cannot read volume %d super block: %v", v.Id, e)
|
2014-08-25 18:37:00 +00:00
|
|
|
}
|
|
|
|
v.SuperBlock, err = ParseSuperBlock(header)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
func ParseSuperBlock(header []byte) (superBlock SuperBlock, err error) {
|
|
|
|
superBlock.version = Version(header[0])
|
|
|
|
if superBlock.ReplicaPlacement, err = NewReplicaPlacementFromByte(header[1]); err != nil {
|
|
|
|
err = fmt.Errorf("cannot read replica type: %s", err.Error())
|
|
|
|
}
|
2014-09-20 19:38:59 +00:00
|
|
|
superBlock.Ttl = LoadTTLFromBytes(header[2:4])
|
2014-08-25 18:37:00 +00:00
|
|
|
return
|
|
|
|
}
|