mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-11-25 03:29:10 +08:00
adding ttl field to volume super block
This commit is contained in:
parent
57a4549d86
commit
69343c5951
@ -2,6 +2,7 @@ package storage
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"code.google.com/p/weed-fs/go/glog"
|
"code.google.com/p/weed-fs/go/glog"
|
||||||
|
"code.google.com/p/weed-fs/go/util"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@ -10,9 +11,16 @@ const (
|
|||||||
SuperBlockSize = 8
|
SuperBlockSize = 8
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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
|
||||||
|
* Byte 2 and byte 3: Time to live in minutes
|
||||||
|
*/
|
||||||
type SuperBlock struct {
|
type SuperBlock struct {
|
||||||
version Version
|
version Version
|
||||||
ReplicaPlacement *ReplicaPlacement
|
ReplicaPlacement *ReplicaPlacement
|
||||||
|
Ttl uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SuperBlock) Version() Version {
|
func (s *SuperBlock) Version() Version {
|
||||||
@ -22,6 +30,7 @@ func (s *SuperBlock) Bytes() []byte {
|
|||||||
header := make([]byte, SuperBlockSize)
|
header := make([]byte, SuperBlockSize)
|
||||||
header[0] = byte(s.version)
|
header[0] = byte(s.version)
|
||||||
header[1] = s.ReplicaPlacement.Byte()
|
header[1] = s.ReplicaPlacement.Byte()
|
||||||
|
util.Uint16toBytes(header[2:4], s.Ttl)
|
||||||
return header
|
return header
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,5 +70,6 @@ func ParseSuperBlock(header []byte) (superBlock SuperBlock, err error) {
|
|||||||
if superBlock.ReplicaPlacement, err = NewReplicaPlacementFromByte(header[1]); err != nil {
|
if superBlock.ReplicaPlacement, err = NewReplicaPlacementFromByte(header[1]); err != nil {
|
||||||
err = fmt.Errorf("cannot read replica type: %s", err.Error())
|
err = fmt.Errorf("cannot read replica type: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
superBlock.Ttl = util.BytesToUint16(header[2:4])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
22
go/storage/volume_super_block_test.go
Normal file
22
go/storage/volume_super_block_test.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSuperBlockReadWrite(t *testing.T) {
|
||||||
|
rp, _ := NewReplicaPlacementFromByte(byte(001))
|
||||||
|
s := &SuperBlock{
|
||||||
|
version: CurrentVersion,
|
||||||
|
ReplicaPlacement: rp,
|
||||||
|
Ttl: uint16(35),
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes := s.Bytes()
|
||||||
|
|
||||||
|
if !(bytes[2] == 0 && bytes[3] == 35) {
|
||||||
|
println("byte[2]:", bytes[2], "byte[3]:", bytes[3])
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
|
// big endian
|
||||||
|
|
||||||
func BytesToUint64(b []byte) (v uint64) {
|
func BytesToUint64(b []byte) (v uint64) {
|
||||||
length := uint(len(b))
|
length := uint(len(b))
|
||||||
for i := uint(0); i < length-1; i++ {
|
for i := uint(0); i < length-1; i++ {
|
||||||
@ -18,6 +20,12 @@ func BytesToUint32(b []byte) (v uint32) {
|
|||||||
v += uint32(b[length-1])
|
v += uint32(b[length-1])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
func BytesToUint16(b []byte) (v uint16) {
|
||||||
|
v += uint16(b[0])
|
||||||
|
v <<= 8
|
||||||
|
v += uint16(b[1])
|
||||||
|
return
|
||||||
|
}
|
||||||
func Uint64toBytes(b []byte, v uint64) {
|
func Uint64toBytes(b []byte, v uint64) {
|
||||||
for i := uint(0); i < 8; i++ {
|
for i := uint(0); i < 8; i++ {
|
||||||
b[7-i] = byte(v >> (i * 8))
|
b[7-i] = byte(v >> (i * 8))
|
||||||
@ -28,6 +36,10 @@ func Uint32toBytes(b []byte, v uint32) {
|
|||||||
b[3-i] = byte(v >> (i * 8))
|
b[3-i] = byte(v >> (i * 8))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func Uint16toBytes(b []byte, v uint16) {
|
||||||
|
b[0] = byte(v >> 8)
|
||||||
|
b[1] = byte(v)
|
||||||
|
}
|
||||||
func Uint8toBytes(b []byte, v uint8) {
|
func Uint8toBytes(b []byte, v uint8) {
|
||||||
b[0] = byte(v)
|
b[0] = byte(v)
|
||||||
}
|
}
|
||||||
|
@ -3,5 +3,5 @@ package util
|
|||||||
import ()
|
import ()
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION = "0.63 beta"
|
VERSION = "0.63"
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user