You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.6 KiB

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