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.

153 lines
4.2 KiB

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