mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
refactor for reading super block
This commit is contained in:
parent
ebe7af1833
commit
79a49ada39
|
@ -1,10 +1,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"pkg/storage"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"pkg/storage"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
|
@ -44,10 +44,15 @@ func runFix(cmd *Command, args []string) bool {
|
|||
}
|
||||
defer indexFile.Close()
|
||||
|
||||
//skip the volume super block
|
||||
dataFile.Seek(storage.SuperBlockSize, 0)
|
||||
dataFile.Seek(0, 0)
|
||||
header := make([]byte, storage.SuperBlockSize)
|
||||
if _, e := dataFile.Read(header); e != nil {
|
||||
log.Fatalf("cannot read superblock: %s", e)
|
||||
}
|
||||
|
||||
n, rest := storage.ReadNeedle(dataFile)
|
||||
ver, _, _ := storage.ParseSuperBlock(header)
|
||||
|
||||
n, rest := storage.ReadNeedle(dataFile, ver)
|
||||
dataFile.Seek(int64(rest), 1)
|
||||
nm := storage.NewNeedleMap(indexFile)
|
||||
offset := uint32(storage.SuperBlockSize)
|
||||
|
@ -57,8 +62,8 @@ func runFix(cmd *Command, args []string) bool {
|
|||
count, pe := nm.Put(n.Id, offset/8, n.Size)
|
||||
debug("saved", count, "with error", pe)
|
||||
}
|
||||
offset += rest+16
|
||||
n, rest = storage.ReadNeedle(dataFile)
|
||||
offset += rest + 16
|
||||
n, rest = storage.ReadNeedle(dataFile, ver)
|
||||
dataFile.Seek(int64(rest), 1)
|
||||
}
|
||||
return true
|
||||
|
|
|
@ -15,14 +15,14 @@ func (n *Needle) Append(w io.Writer) uint32 {
|
|||
util.Uint32toBytes(header[12:16], n.Size)
|
||||
w.Write(header)
|
||||
w.Write(n.Data)
|
||||
rest := 8 - ((n.Size + 16 + 4) % 8)
|
||||
rest := 8 - ((16 + n.Size + 4) % 8)
|
||||
util.Uint32toBytes(header[0:4], n.Checksum.Value())
|
||||
w.Write(header[0 : rest+4])
|
||||
w.Write(header[0 : 4+rest])
|
||||
return n.Size
|
||||
}
|
||||
func (n *Needle) Read(r io.Reader, size uint32, version Version) (int, error) {
|
||||
if version == Version1 {
|
||||
bytes := make([]byte, size+16+4)
|
||||
bytes := make([]byte, 16+size+4)
|
||||
ret, e := r.Read(bytes)
|
||||
n.Cookie = util.BytesToUint32(bytes[0:4])
|
||||
n.Id = util.BytesToUint64(bytes[4:12])
|
||||
|
@ -33,12 +33,13 @@ func (n *Needle) Read(r io.Reader, size uint32, version Version) (int, error) {
|
|||
return 0, errors.New("CRC error! Data On Disk Corrupted!")
|
||||
}
|
||||
return ret, e
|
||||
}else if version == Version2 {
|
||||
} else if version == Version2 {
|
||||
}
|
||||
return 0, errors.New("Unsupported Version!")
|
||||
}
|
||||
func ReadNeedle(r *os.File) (*Needle, uint32) {
|
||||
n := new(Needle)
|
||||
func ReadNeedle(r *os.File, version Version) (n *Needle, bytesTillNextFile uint32) {
|
||||
n = new(Needle)
|
||||
if version == Version1 {
|
||||
bytes := make([]byte, 16)
|
||||
count, e := r.Read(bytes)
|
||||
if count <= 0 || e != nil {
|
||||
|
@ -48,5 +49,8 @@ func ReadNeedle(r *os.File) (*Needle, uint32) {
|
|||
n.Id = util.BytesToUint64(bytes[4:12])
|
||||
n.Size = util.BytesToUint32(bytes[12:16])
|
||||
rest := 8 - ((n.Size + 16 + 4) % 8)
|
||||
return n, n.Size + 4 + rest
|
||||
bytesTillNextFile = n.Size + 4 + rest
|
||||
} else if version == Version2 {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -18,8 +18,8 @@ type Volume struct {
|
|||
dataFile *os.File
|
||||
nm *NeedleMap
|
||||
|
||||
replicaType ReplicationType
|
||||
version Version
|
||||
replicaType ReplicationType
|
||||
|
||||
accessLock sync.Mutex
|
||||
}
|
||||
|
@ -85,12 +85,17 @@ func (v *Volume) readSuperBlock() error {
|
|||
if _, e := v.dataFile.Read(header); e != nil {
|
||||
return fmt.Errorf("cannot read superblock: %s", e)
|
||||
}
|
||||
v.version = Version(header[0])
|
||||
var err error
|
||||
if v.replicaType, err = NewReplicationTypeFromByte(header[1]); err != nil {
|
||||
return fmt.Errorf("cannot read replica type: %s", err)
|
||||
v.version, v.replicaType, err = ParseSuperBlock(header)
|
||||
return err
|
||||
}
|
||||
func ParseSuperBlock(header []byte) (version Version, replicaType ReplicationType, e error) {
|
||||
version = Version(header[0])
|
||||
var err error
|
||||
if replicaType, err = NewReplicationTypeFromByte(header[1]); err != nil {
|
||||
e = fmt.Errorf("cannot read replica type: %s", err)
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
func (v *Volume) NeedToReplicate() bool {
|
||||
return v.replicaType.GetCopyCount() > 1
|
||||
|
@ -133,7 +138,7 @@ func (v *Volume) read(n *Needle) (int, error) {
|
|||
}
|
||||
|
||||
func (v *Volume) garbageLevel() float64 {
|
||||
return float64(v.nm.deletionByteCounter)/float64(v.ContentSize())
|
||||
return float64(v.nm.deletionByteCounter) / float64(v.ContentSize())
|
||||
}
|
||||
|
||||
func (v *Volume) compact() error {
|
||||
|
@ -143,7 +148,7 @@ func (v *Volume) compact() error {
|
|||
filePath := path.Join(v.dir, v.Id.String())
|
||||
return v.copyDataAndGenerateIndexFile(filePath+".dat", filePath+".cpd", filePath+".cpx")
|
||||
}
|
||||
func (v *Volume) commitCompact() (error) {
|
||||
func (v *Volume) commitCompact() error {
|
||||
v.accessLock.Lock()
|
||||
defer v.accessLock.Unlock()
|
||||
v.dataFile.Close()
|
||||
|
@ -185,7 +190,9 @@ func (v *Volume) copyDataAndGenerateIndexFile(srcName, dstName, idxName string)
|
|||
dst.Write(header)
|
||||
}
|
||||
|
||||
n, rest := ReadNeedle(src)
|
||||
version, _, _ := ParseSuperBlock(header)
|
||||
|
||||
n, rest := ReadNeedle(src, version)
|
||||
nm := NewNeedleMap(idx)
|
||||
old_offset := uint32(SuperBlockSize)
|
||||
new_offset := uint32(SuperBlockSize)
|
||||
|
@ -208,11 +215,11 @@ func (v *Volume) copyDataAndGenerateIndexFile(srcName, dstName, idxName string)
|
|||
src.Seek(int64(rest-n.Size-4), 1)
|
||||
}
|
||||
old_offset += rest + 16
|
||||
n, rest = ReadNeedle(src)
|
||||
n, rest = ReadNeedle(src, version)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
func (v *Volume) ContentSize() uint64{
|
||||
func (v *Volume) ContentSize() uint64 {
|
||||
return v.nm.fileByteCounter
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue