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.

129 lines
3.5 KiB

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