volume_super_block.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  7. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "github.com/golang/protobuf/proto"
  11. )
  12. const (
  13. _SuperBlockSize = 8
  14. )
  15. /*
  16. * Super block currently has 8 bytes allocated for each volume.
  17. * Byte 0: version, 1 or 2
  18. * Byte 1: Replica Placement strategy, 000, 001, 002, 010, etc
  19. * Byte 2 and byte 3: Time to live. See TTL for definition
  20. * Byte 4 and byte 5: The number of times the volume has been compacted.
  21. * Rest bytes: Reserved
  22. */
  23. type SuperBlock struct {
  24. version needle.Version
  25. ReplicaPlacement *ReplicaPlacement
  26. Ttl *needle.TTL
  27. CompactionRevision uint16
  28. Extra *master_pb.SuperBlockExtra
  29. extraSize uint16
  30. }
  31. func (s *SuperBlock) BlockSize() int {
  32. switch s.version {
  33. case needle.Version2, needle.Version3:
  34. return _SuperBlockSize + int(s.extraSize)
  35. }
  36. return _SuperBlockSize
  37. }
  38. func (s *SuperBlock) Version() needle.Version {
  39. return s.version
  40. }
  41. func (s *SuperBlock) Bytes() []byte {
  42. header := make([]byte, _SuperBlockSize)
  43. header[0] = byte(s.version)
  44. header[1] = s.ReplicaPlacement.Byte()
  45. s.Ttl.ToBytes(header[2:4])
  46. util.Uint16toBytes(header[4:6], s.CompactionRevision)
  47. if s.Extra != nil {
  48. extraData, err := proto.Marshal(s.Extra)
  49. if err != nil {
  50. glog.Fatalf("cannot marshal super block extra %+v: %v", s.Extra, err)
  51. }
  52. extraSize := len(extraData)
  53. if extraSize > 256*256-2 {
  54. // reserve a couple of bits for future extension
  55. glog.Fatalf("super block extra size is %d bigger than %d", extraSize, 256*256-2)
  56. }
  57. s.extraSize = uint16(extraSize)
  58. util.Uint16toBytes(header[6:8], s.extraSize)
  59. header = append(header, extraData...)
  60. }
  61. return header
  62. }
  63. func (s *SuperBlock) Initialized() bool {
  64. return s.ReplicaPlacement != nil && s.Ttl != nil
  65. }
  66. func (v *Volume) maybeWriteSuperBlock() error {
  67. datSize, _, e := v.DataBackend.GetStat()
  68. if e != nil {
  69. glog.V(0).Infof("failed to stat datafile %s: %v", v.DataBackend.String(), e)
  70. return e
  71. }
  72. if datSize == 0 {
  73. v.SuperBlock.version = needle.CurrentVersion
  74. _, e = v.DataBackend.WriteAt(v.SuperBlock.Bytes(), 0)
  75. if e != nil && os.IsPermission(e) {
  76. //read-only, but zero length - recreate it!
  77. var dataFile *os.File
  78. if dataFile, e = os.Create(v.DataBackend.String()); e == nil {
  79. v.DataBackend = backend.NewDiskFile(dataFile)
  80. if _, e = v.DataBackend.WriteAt(v.SuperBlock.Bytes(), 0); e == nil {
  81. v.readOnly = false
  82. }
  83. }
  84. }
  85. }
  86. return e
  87. }
  88. func (v *Volume) readSuperBlock() (err error) {
  89. v.SuperBlock, err = ReadSuperBlock(v.DataBackend)
  90. return err
  91. }
  92. // ReadSuperBlock reads from data file and load it into volume's super block
  93. func ReadSuperBlock(datBackend backend.DataStorageBackend) (superBlock SuperBlock, err error) {
  94. header := make([]byte, _SuperBlockSize)
  95. if _, e := datBackend.ReadAt(header, 0); e != nil {
  96. err = fmt.Errorf("cannot read volume %s super block: %v", datBackend.String(), e)
  97. return
  98. }
  99. superBlock.version = needle.Version(header[0])
  100. if superBlock.ReplicaPlacement, err = NewReplicaPlacementFromByte(header[1]); err != nil {
  101. err = fmt.Errorf("cannot read replica type: %s", err.Error())
  102. return
  103. }
  104. superBlock.Ttl = needle.LoadTTLFromBytes(header[2:4])
  105. superBlock.CompactionRevision = util.BytesToUint16(header[4:6])
  106. superBlock.extraSize = util.BytesToUint16(header[6:8])
  107. if superBlock.extraSize > 0 {
  108. // read more
  109. extraData := make([]byte, int(superBlock.extraSize))
  110. superBlock.Extra = &master_pb.SuperBlockExtra{}
  111. err = proto.Unmarshal(extraData, superBlock.Extra)
  112. if err != nil {
  113. err = fmt.Errorf("cannot read volume %s super block extra: %v", datBackend.String(), err)
  114. return
  115. }
  116. }
  117. return
  118. }