ec_volume_info.go 2.7 KB

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