2019-05-23 13:44:28 +08:00
|
|
|
package erasure_coding
|
|
|
|
|
|
|
|
import (
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
2019-05-23 13:44:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// data structure used in master
|
|
|
|
type EcVolumeInfo struct {
|
2024-08-16 15:20:00 +08:00
|
|
|
VolumeId needle.VolumeId
|
|
|
|
Collection string
|
|
|
|
ShardBits ShardBits
|
|
|
|
DiskType string
|
2024-10-25 12:35:11 +08:00
|
|
|
ExpireAtSec uint64 //ec volume destroy time, calculated from the ec volume was created
|
2019-05-23 13:44:28 +08:00
|
|
|
}
|
|
|
|
|
2024-10-25 12:35:11 +08:00
|
|
|
func NewEcVolumeInfo(diskType string, collection string, vid needle.VolumeId, shardBits ShardBits, expireAtSec uint64) *EcVolumeInfo {
|
2019-05-23 15:42:28 +08:00
|
|
|
return &EcVolumeInfo{
|
2024-08-16 15:20:00 +08:00
|
|
|
Collection: collection,
|
|
|
|
VolumeId: vid,
|
|
|
|
ShardBits: shardBits,
|
|
|
|
DiskType: diskType,
|
2024-10-25 12:35:11 +08:00
|
|
|
ExpireAtSec: expireAtSec,
|
2019-05-23 15:42:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 13:44:28 +08:00
|
|
|
func (ecInfo *EcVolumeInfo) AddShardId(id ShardId) {
|
2019-05-25 04:28:44 +08:00
|
|
|
ecInfo.ShardBits = ecInfo.ShardBits.AddShardId(id)
|
2019-05-23 13:44:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ecInfo *EcVolumeInfo) RemoveShardId(id ShardId) {
|
2019-05-25 04:28:44 +08:00
|
|
|
ecInfo.ShardBits = ecInfo.ShardBits.RemoveShardId(id)
|
2019-05-23 13:44:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ecInfo *EcVolumeInfo) HasShardId(id ShardId) bool {
|
2019-05-25 04:28:44 +08:00
|
|
|
return ecInfo.ShardBits.HasShardId(id)
|
2019-05-23 13:44:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ecInfo *EcVolumeInfo) ShardIds() (ret []ShardId) {
|
2019-05-25 04:28:44 +08:00
|
|
|
return ecInfo.ShardBits.ShardIds()
|
2019-05-23 13:44:28 +08:00
|
|
|
}
|
|
|
|
|
2019-05-25 17:02:44 +08:00
|
|
|
func (ecInfo *EcVolumeInfo) ShardIdCount() (count int) {
|
|
|
|
return ecInfo.ShardBits.ShardIdCount()
|
|
|
|
}
|
|
|
|
|
2019-05-29 12:29:07 +08:00
|
|
|
func (ecInfo *EcVolumeInfo) Minus(other *EcVolumeInfo) *EcVolumeInfo {
|
2019-05-24 13:51:18 +08:00
|
|
|
ret := &EcVolumeInfo{
|
|
|
|
VolumeId: ecInfo.VolumeId,
|
|
|
|
Collection: ecInfo.Collection,
|
2019-05-25 04:28:44 +08:00
|
|
|
ShardBits: ecInfo.ShardBits.Minus(other.ShardBits),
|
2021-02-16 18:47:02 +08:00
|
|
|
DiskType: ecInfo.DiskType,
|
2019-05-24 13:51:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-05-25 04:28:44 +08:00
|
|
|
func (ecInfo *EcVolumeInfo) ToVolumeEcShardInformationMessage() (ret *master_pb.VolumeEcShardInformationMessage) {
|
|
|
|
return &master_pb.VolumeEcShardInformationMessage{
|
|
|
|
Id: uint32(ecInfo.VolumeId),
|
|
|
|
EcIndexBits: uint32(ecInfo.ShardBits),
|
|
|
|
Collection: ecInfo.Collection,
|
2021-02-16 18:47:02 +08:00
|
|
|
DiskType: ecInfo.DiskType,
|
2024-10-25 12:35:11 +08:00
|
|
|
ExpireAtSec: ecInfo.ExpireAtSec,
|
2019-05-25 04:28:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ShardBits uint32 // use bits to indicate the shard id, use 32 bits just for possible future extension
|
2019-05-23 13:44:28 +08:00
|
|
|
|
2019-05-25 04:28:44 +08:00
|
|
|
func (b ShardBits) AddShardId(id ShardId) ShardBits {
|
|
|
|
return b | (1 << id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b ShardBits) RemoveShardId(id ShardId) ShardBits {
|
|
|
|
return b &^ (1 << id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b ShardBits) HasShardId(id ShardId) bool {
|
|
|
|
return b&(1<<id) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b ShardBits) ShardIds() (ret []ShardId) {
|
2019-05-25 17:02:44 +08:00
|
|
|
for i := ShardId(0); i < TotalShardsCount; i++ {
|
2019-05-25 04:28:44 +08:00
|
|
|
if b.HasShardId(i) {
|
|
|
|
ret = append(ret, i)
|
|
|
|
}
|
2019-05-23 13:44:28 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-05-25 02:52:23 +08:00
|
|
|
|
2019-12-24 04:48:20 +08:00
|
|
|
func (b ShardBits) ToUint32Slice() (ret []uint32) {
|
|
|
|
for i := uint32(0); i < TotalShardsCount; i++ {
|
|
|
|
if b.HasShardId(ShardId(i)) {
|
|
|
|
ret = append(ret, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-25 17:02:44 +08:00
|
|
|
func (b ShardBits) ShardIdCount() (count int) {
|
|
|
|
for count = 0; b > 0; count++ {
|
|
|
|
b &= b - 1
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-29 12:29:07 +08:00
|
|
|
func (b ShardBits) Minus(other ShardBits) ShardBits {
|
2019-05-25 04:28:44 +08:00
|
|
|
return b &^ other
|
2019-05-25 02:52:23 +08:00
|
|
|
}
|
2019-05-26 15:21:17 +08:00
|
|
|
|
2019-05-29 12:29:07 +08:00
|
|
|
func (b ShardBits) Plus(other ShardBits) ShardBits {
|
2019-05-26 15:21:17 +08:00
|
|
|
return b | other
|
|
|
|
}
|
2019-12-24 10:06:13 +08:00
|
|
|
|
|
|
|
func (b ShardBits) MinusParityShards() ShardBits {
|
|
|
|
for i := DataShardsCount; i < TotalShardsCount; i++ {
|
|
|
|
b = b.RemoveShardId(ShardId(i))
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|