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.

157 lines
4.3 KiB

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/memory_map"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  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. mMap, exists := memory_map.FileMemoryMap[v.dataFile.Name()]
  68. if exists {
  69. if mMap.End_of_file == -1 {
  70. v.SuperBlock.version = needle.CurrentVersion
  71. mMap.WriteMemory(0, uint64(len(v.SuperBlock.Bytes())), v.SuperBlock.Bytes())
  72. }
  73. return nil
  74. } else {
  75. stat, e := v.dataFile.Stat()
  76. if e != nil {
  77. glog.V(0).Infof("failed to stat datafile %s: %v", v.dataFile.Name(), e)
  78. return e
  79. }
  80. if stat.Size() == 0 {
  81. v.SuperBlock.version = needle.CurrentVersion
  82. _, e = v.dataFile.Write(v.SuperBlock.Bytes())
  83. if e != nil && os.IsPermission(e) {
  84. //read-only, but zero length - recreate it!
  85. if v.dataFile, e = os.Create(v.dataFile.Name()); e == nil {
  86. if _, e = v.dataFile.Write(v.SuperBlock.Bytes()); e == nil {
  87. v.readOnly = false
  88. }
  89. }
  90. }
  91. }
  92. return e
  93. }
  94. }
  95. func (v *Volume) readSuperBlock() (err error) {
  96. v.SuperBlock, err = ReadSuperBlock(v.dataFile)
  97. return err
  98. }
  99. // ReadSuperBlock reads from data file and load it into volume's super block
  100. func ReadSuperBlock(dataFile *os.File) (superBlock SuperBlock, err error) {
  101. header := make([]byte, _SuperBlockSize)
  102. mMap, exists := memory_map.FileMemoryMap[dataFile.Name()]
  103. if exists {
  104. header, err = mMap.ReadMemory(0, _SuperBlockSize)
  105. if err != nil {
  106. err = fmt.Errorf("cannot read volume %s super block: %v", dataFile.Name(), err)
  107. return
  108. }
  109. } else {
  110. if _, err = dataFile.Seek(0, 0); err != nil {
  111. err = fmt.Errorf("cannot seek to the beginning of %s: %v", dataFile.Name(), err)
  112. return
  113. }
  114. if _, e := dataFile.Read(header); e != nil {
  115. err = fmt.Errorf("cannot read volume %s super block: %v", dataFile.Name(), e)
  116. return
  117. }
  118. }
  119. superBlock.version = needle.Version(header[0])
  120. if superBlock.ReplicaPlacement, err = NewReplicaPlacementFromByte(header[1]); err != nil {
  121. err = fmt.Errorf("cannot read replica type: %s", err.Error())
  122. return
  123. }
  124. superBlock.Ttl = needle.LoadTTLFromBytes(header[2:4])
  125. superBlock.CompactionRevision = util.BytesToUint16(header[4:6])
  126. superBlock.extraSize = util.BytesToUint16(header[6:8])
  127. if superBlock.extraSize > 0 {
  128. // read more
  129. extraData := make([]byte, int(superBlock.extraSize))
  130. superBlock.Extra = &master_pb.SuperBlockExtra{}
  131. err = proto.Unmarshal(extraData, superBlock.Extra)
  132. if err != nil {
  133. err = fmt.Errorf("cannot read volume %s super block extra: %v", dataFile.Name(), err)
  134. return
  135. }
  136. }
  137. return
  138. }