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.

69 lines
1.8 KiB

  1. package super_block
  2. import (
  3. "github.com/golang/protobuf/proto"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  6. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. )
  9. const (
  10. SuperBlockSize = 8
  11. )
  12. /*
  13. * Super block currently has 8 bytes allocated for each volume.
  14. * Byte 0: version, 1 or 2
  15. * Byte 1: Replica Placement strategy, 000, 001, 002, 010, etc
  16. * Byte 2 and byte 3: Time to live. See TTL for definition
  17. * Byte 4 and byte 5: The number of times the volume has been compacted.
  18. * Rest bytes: Reserved
  19. */
  20. type SuperBlock struct {
  21. Version needle.Version
  22. ReplicaPlacement *ReplicaPlacement
  23. Ttl *needle.TTL
  24. CompactionRevision uint16
  25. Extra *master_pb.SuperBlockExtra
  26. ExtraSize uint16
  27. }
  28. func (s *SuperBlock) BlockSize() int {
  29. switch s.Version {
  30. case needle.Version2, needle.Version3:
  31. return SuperBlockSize + int(s.ExtraSize)
  32. }
  33. return SuperBlockSize
  34. }
  35. func (s *SuperBlock) Bytes() []byte {
  36. header := make([]byte, SuperBlockSize)
  37. header[0] = byte(s.Version)
  38. header[1] = s.ReplicaPlacement.Byte()
  39. s.Ttl.ToBytes(header[2:4])
  40. util.Uint16toBytes(header[4:6], s.CompactionRevision)
  41. if s.Extra != nil {
  42. extraData, err := proto.Marshal(s.Extra)
  43. if err != nil {
  44. glog.Fatalf("cannot marshal super block extra %+v: %v", s.Extra, err)
  45. }
  46. extraSize := len(extraData)
  47. if extraSize > 256*256-2 {
  48. // reserve a couple of bits for future extension
  49. glog.Fatalf("super block extra size is %d bigger than %d", extraSize, 256*256-2)
  50. }
  51. s.ExtraSize = uint16(extraSize)
  52. util.Uint16toBytes(header[6:8], s.ExtraSize)
  53. header = append(header, extraData...)
  54. }
  55. return header
  56. }
  57. func (s *SuperBlock) Initialized() bool {
  58. return s.ReplicaPlacement != nil && s.Ttl != nil
  59. }