diff options
Diffstat (limited to 'weed/storage/erasure_coding/ec_volume_info.go')
| -rw-r--r-- | weed/storage/erasure_coding/ec_volume_info.go | 162 |
1 files changed, 144 insertions, 18 deletions
diff --git a/weed/storage/erasure_coding/ec_volume_info.go b/weed/storage/erasure_coding/ec_volume_info.go index 787910b0c..53b352168 100644 --- a/weed/storage/erasure_coding/ec_volume_info.go +++ b/weed/storage/erasure_coding/ec_volume_info.go @@ -1,6 +1,8 @@ package erasure_coding import ( + "math/bits" + "github.com/seaweedfs/seaweedfs/weed/pb/master_pb" "github.com/seaweedfs/seaweedfs/weed/storage/needle" ) @@ -11,27 +13,51 @@ type EcVolumeInfo struct { Collection string ShardBits ShardBits DiskType string - DiskId uint32 // ID of the disk this EC volume is on - ExpireAtSec uint64 // ec volume destroy time, calculated from the ec volume was created -} - -func NewEcVolumeInfo(diskType string, collection string, vid needle.VolumeId, shardBits ShardBits, expireAtSec uint64, diskId uint32) *EcVolumeInfo { - return &EcVolumeInfo{ - Collection: collection, - VolumeId: vid, - ShardBits: shardBits, - DiskType: diskType, - DiskId: diskId, - ExpireAtSec: expireAtSec, - } + DiskId uint32 // ID of the disk this EC volume is on + ExpireAtSec uint64 // ec volume destroy time, calculated from the ec volume was created + ShardSizes []int64 // optimized: sizes for shards in order of set bits in ShardBits } func (ecInfo *EcVolumeInfo) AddShardId(id ShardId) { + oldBits := ecInfo.ShardBits ecInfo.ShardBits = ecInfo.ShardBits.AddShardId(id) + + // If shard was actually added, resize ShardSizes array + if oldBits != ecInfo.ShardBits { + ecInfo.resizeShardSizes(oldBits) + } } func (ecInfo *EcVolumeInfo) RemoveShardId(id ShardId) { + oldBits := ecInfo.ShardBits ecInfo.ShardBits = ecInfo.ShardBits.RemoveShardId(id) + + // If shard was actually removed, resize ShardSizes array + if oldBits != ecInfo.ShardBits { + ecInfo.resizeShardSizes(oldBits) + } +} + +func (ecInfo *EcVolumeInfo) SetShardSize(id ShardId, size int64) { + ecInfo.ensureShardSizesInitialized() + if index, found := ecInfo.ShardBits.ShardIdToIndex(id); found && index < len(ecInfo.ShardSizes) { + ecInfo.ShardSizes[index] = size + } +} + +func (ecInfo *EcVolumeInfo) GetShardSize(id ShardId) (int64, bool) { + if index, found := ecInfo.ShardBits.ShardIdToIndex(id); found && index < len(ecInfo.ShardSizes) { + return ecInfo.ShardSizes[index], true + } + return 0, false +} + +func (ecInfo *EcVolumeInfo) GetTotalSize() int64 { + var total int64 + for _, size := range ecInfo.ShardSizes { + total += size + } + return total } func (ecInfo *EcVolumeInfo) HasShardId(id ShardId) bool { @@ -48,17 +74,33 @@ func (ecInfo *EcVolumeInfo) ShardIdCount() (count int) { func (ecInfo *EcVolumeInfo) Minus(other *EcVolumeInfo) *EcVolumeInfo { ret := &EcVolumeInfo{ - VolumeId: ecInfo.VolumeId, - Collection: ecInfo.Collection, - ShardBits: ecInfo.ShardBits.Minus(other.ShardBits), - DiskType: ecInfo.DiskType, + VolumeId: ecInfo.VolumeId, + Collection: ecInfo.Collection, + ShardBits: ecInfo.ShardBits.Minus(other.ShardBits), + DiskType: ecInfo.DiskType, + DiskId: ecInfo.DiskId, + ExpireAtSec: ecInfo.ExpireAtSec, + } + + // Initialize optimized ShardSizes for the result + ret.ensureShardSizesInitialized() + + // Copy shard sizes for remaining shards + retIndex := 0 + for shardId := ShardId(0); shardId < TotalShardsCount && retIndex < len(ret.ShardSizes); shardId++ { + if ret.ShardBits.HasShardId(shardId) { + if size, exists := ecInfo.GetShardSize(shardId); exists { + ret.ShardSizes[retIndex] = size + } + retIndex++ + } } return ret } func (ecInfo *EcVolumeInfo) ToVolumeEcShardInformationMessage() (ret *master_pb.VolumeEcShardInformationMessage) { - return &master_pb.VolumeEcShardInformationMessage{ + t := &master_pb.VolumeEcShardInformationMessage{ Id: uint32(ecInfo.VolumeId), EcIndexBits: uint32(ecInfo.ShardBits), Collection: ecInfo.Collection, @@ -66,6 +108,12 @@ func (ecInfo *EcVolumeInfo) ToVolumeEcShardInformationMessage() (ret *master_pb. ExpireAtSec: ecInfo.ExpireAtSec, DiskId: ecInfo.DiskId, } + + // Directly set the optimized ShardSizes + t.ShardSizes = make([]int64, len(ecInfo.ShardSizes)) + copy(t.ShardSizes, ecInfo.ShardSizes) + + return t } type ShardBits uint32 // use bits to indicate the shard id, use 32 bits just for possible future extension @@ -121,3 +169,81 @@ func (b ShardBits) MinusParityShards() ShardBits { } return b } + +// ShardIdToIndex converts a shard ID to its index position in the ShardSizes slice +// Returns the index and true if the shard is present, -1 and false if not present +func (b ShardBits) ShardIdToIndex(shardId ShardId) (index int, found bool) { + if !b.HasShardId(shardId) { + return -1, false + } + + // Create a mask for bits before the shardId + mask := uint32((1 << shardId) - 1) + // Count set bits before the shardId using efficient bit manipulation + index = bits.OnesCount32(uint32(b) & mask) + return index, true +} + +// EachSetIndex iterates over all set shard IDs and calls the provided function for each +// This is highly efficient using bit manipulation - only iterates over actual set bits +func (b ShardBits) EachSetIndex(fn func(shardId ShardId)) { + bitsValue := uint32(b) + for bitsValue != 0 { + // Find the position of the least significant set bit + shardId := ShardId(bits.TrailingZeros32(bitsValue)) + fn(shardId) + // Clear the least significant set bit + bitsValue &= bitsValue - 1 + } +} + +// IndexToShardId converts an index position in ShardSizes slice to the corresponding shard ID +// Returns the shard ID and true if valid index, -1 and false if invalid index +func (b ShardBits) IndexToShardId(index int) (shardId ShardId, found bool) { + if index < 0 { + return 0, false + } + + currentIndex := 0 + for i := ShardId(0); i < TotalShardsCount; i++ { + if b.HasShardId(i) { + if currentIndex == index { + return i, true + } + currentIndex++ + } + } + return 0, false // index out of range +} + +// Helper methods for EcVolumeInfo to manage the optimized ShardSizes slice +func (ecInfo *EcVolumeInfo) ensureShardSizesInitialized() { + expectedLength := ecInfo.ShardBits.ShardIdCount() + if ecInfo.ShardSizes == nil { + ecInfo.ShardSizes = make([]int64, expectedLength) + } else if len(ecInfo.ShardSizes) != expectedLength { + // Resize and preserve existing data + ecInfo.resizeShardSizes(ecInfo.ShardBits) + } +} + +func (ecInfo *EcVolumeInfo) resizeShardSizes(prevShardBits ShardBits) { + expectedLength := ecInfo.ShardBits.ShardIdCount() + newSizes := make([]int64, expectedLength) + + // Copy existing sizes to new positions based on current ShardBits + if len(ecInfo.ShardSizes) > 0 { + newIndex := 0 + for shardId := ShardId(0); shardId < TotalShardsCount && newIndex < expectedLength; shardId++ { + if ecInfo.ShardBits.HasShardId(shardId) { + // Try to find the size for this shard in the old array using previous ShardBits + if oldIndex, found := prevShardBits.ShardIdToIndex(shardId); found && oldIndex < len(ecInfo.ShardSizes) { + newSizes[newIndex] = ecInfo.ShardSizes[oldIndex] + } + newIndex++ + } + } + } + + ecInfo.ShardSizes = newSizes +} |
