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.

120 lines
2.8 KiB

  1. package erasure_coding
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  4. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  5. )
  6. // data structure used in master
  7. type EcVolumeInfo struct {
  8. VolumeId needle.VolumeId
  9. Collection string
  10. ShardBits ShardBits
  11. DiskType string
  12. DestroyTime uint64 //ec volume destroy time, calculated from the ec volume was created
  13. }
  14. func NewEcVolumeInfo(diskType string, collection string, vid needle.VolumeId, shardBits ShardBits, destroyTime uint64) *EcVolumeInfo {
  15. return &EcVolumeInfo{
  16. Collection: collection,
  17. VolumeId: vid,
  18. ShardBits: shardBits,
  19. DiskType: diskType,
  20. DestroyTime: destroyTime,
  21. }
  22. }
  23. func (ecInfo *EcVolumeInfo) AddShardId(id ShardId) {
  24. ecInfo.ShardBits = ecInfo.ShardBits.AddShardId(id)
  25. }
  26. func (ecInfo *EcVolumeInfo) RemoveShardId(id ShardId) {
  27. ecInfo.ShardBits = ecInfo.ShardBits.RemoveShardId(id)
  28. }
  29. func (ecInfo *EcVolumeInfo) HasShardId(id ShardId) bool {
  30. return ecInfo.ShardBits.HasShardId(id)
  31. }
  32. func (ecInfo *EcVolumeInfo) ShardIds() (ret []ShardId) {
  33. return ecInfo.ShardBits.ShardIds()
  34. }
  35. func (ecInfo *EcVolumeInfo) ShardIdCount() (count int) {
  36. return ecInfo.ShardBits.ShardIdCount()
  37. }
  38. func (ecInfo *EcVolumeInfo) Minus(other *EcVolumeInfo) *EcVolumeInfo {
  39. ret := &EcVolumeInfo{
  40. VolumeId: ecInfo.VolumeId,
  41. Collection: ecInfo.Collection,
  42. ShardBits: ecInfo.ShardBits.Minus(other.ShardBits),
  43. DiskType: ecInfo.DiskType,
  44. }
  45. return ret
  46. }
  47. func (ecInfo *EcVolumeInfo) ToVolumeEcShardInformationMessage() (ret *master_pb.VolumeEcShardInformationMessage) {
  48. return &master_pb.VolumeEcShardInformationMessage{
  49. Id: uint32(ecInfo.VolumeId),
  50. EcIndexBits: uint32(ecInfo.ShardBits),
  51. Collection: ecInfo.Collection,
  52. DiskType: ecInfo.DiskType,
  53. DestroyTime: ecInfo.DestroyTime,
  54. }
  55. }
  56. type ShardBits uint32 // use bits to indicate the shard id, use 32 bits just for possible future extension
  57. func (b ShardBits) AddShardId(id ShardId) ShardBits {
  58. return b | (1 << id)
  59. }
  60. func (b ShardBits) RemoveShardId(id ShardId) ShardBits {
  61. return b &^ (1 << id)
  62. }
  63. func (b ShardBits) HasShardId(id ShardId) bool {
  64. return b&(1<<id) > 0
  65. }
  66. func (b ShardBits) ShardIds() (ret []ShardId) {
  67. for i := ShardId(0); i < TotalShardsCount; i++ {
  68. if b.HasShardId(i) {
  69. ret = append(ret, i)
  70. }
  71. }
  72. return
  73. }
  74. func (b ShardBits) ToUint32Slice() (ret []uint32) {
  75. for i := uint32(0); i < TotalShardsCount; i++ {
  76. if b.HasShardId(ShardId(i)) {
  77. ret = append(ret, i)
  78. }
  79. }
  80. return
  81. }
  82. func (b ShardBits) ShardIdCount() (count int) {
  83. for count = 0; b > 0; count++ {
  84. b &= b - 1
  85. }
  86. return
  87. }
  88. func (b ShardBits) Minus(other ShardBits) ShardBits {
  89. return b &^ other
  90. }
  91. func (b ShardBits) Plus(other ShardBits) ShardBits {
  92. return b | other
  93. }
  94. func (b ShardBits) MinusParityShards() ShardBits {
  95. for i := DataShardsCount; i < TotalShardsCount; i++ {
  96. b = b.RemoveShardId(ShardId(i))
  97. }
  98. return b
  99. }