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.

156 lines
4.4 KiB

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