mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-07 20:47:46 +01:00
EcVolume creation properly refactored
This commit is contained in:
1 parent
ef5f9f629a
commit
8c31d5e331
5 files changed
+70
-20
No files matched your search
@@ -25,11 +25,11 @@ type EcVolumeShard struct {
|
||||
DiskType types.DiskType
|
||||
}
|
||||
|
||||
func NewEcVolumeShard(diskType types.DiskType, dirname string, collection string, id needle.VolumeId, shardId ShardId) (v *EcVolumeShard, e error) {
|
||||
func NewEcVolumeShard(diskType types.DiskType, dirname string, collection string, id needle.VolumeId, shardId ShardId, generation uint32) (v *EcVolumeShard, e error) {
|
||||
|
||||
v = &EcVolumeShard{dir: dirname, Collection: collection, VolumeId: id, ShardId: shardId, DiskType: diskType}
|
||||
|
||||
baseFileName := v.FileName()
|
||||
baseFileName := v.FileNameWithGeneration(generation)
|
||||
|
||||
// open ecd file
|
||||
if v.ecdFile, e = os.OpenFile(baseFileName+ToExt(int(shardId)), os.O_RDONLY, 0644); e != nil {
|
||||
@@ -151,3 +151,24 @@ func (shard *EcVolumeShard) ReadAt(buf []byte, offset int64) (int, error) {
|
||||
return n, err
|
||||
|
||||
}
|
||||
|
||||
// ParseGenerationFromFileName extracts generation from EC volume filename
|
||||
// Returns 0 for files without generation suffix (backward compatibility)
|
||||
func ParseGenerationFromFileName(fileName string) uint32 {
|
||||
// Remove extension first
|
||||
baseName := fileName
|
||||
if lastDot := strings.LastIndex(fileName, "."); lastDot >= 0 {
|
||||
baseName = fileName[:lastDot]
|
||||
}
|
||||
|
||||
// Look for _g{N} pattern at the end
|
||||
if gIndex := strings.LastIndex(baseName, "_g"); gIndex >= 0 {
|
||||
generationStr := baseName[gIndex+2:]
|
||||
if generation, err := strconv.ParseUint(generationStr, 10, 32); err == nil {
|
||||
return uint32(generation)
|
||||
}
|
||||
}
|
||||
|
||||
// No generation suffix found, return 0 for backward compatibility
|
||||
return 0
|
||||
}
|
||||
@@ -27,6 +27,7 @@ var (
|
||||
type EcVolume struct {
|
||||
VolumeId needle.VolumeId
|
||||
Collection string
|
||||
Generation uint32 // generation of this EC volume, defaults to 0 for backward compatibility
|
||||
dir string
|
||||
dirIdx string
|
||||
ecxFile *os.File
|
||||
@@ -44,11 +45,19 @@ type EcVolume struct {
|
||||
ExpireAtSec uint64 //ec volume destroy time, calculated from the ec volume was created
|
||||
}
|
||||
|
||||
func NewEcVolume(diskType types.DiskType, dir string, dirIdx string, collection string, vid needle.VolumeId) (ev *EcVolume, err error) {
|
||||
ev = &EcVolume{dir: dir, dirIdx: dirIdx, Collection: collection, VolumeId: vid, diskType: diskType}
|
||||
func NewEcVolume(diskType types.DiskType, dir string, dirIdx string, collection string, vid needle.VolumeId, generation uint32) (ev *EcVolume, err error) {
|
||||
ev = &EcVolume{
|
||||
dir: dir,
|
||||
dirIdx: dirIdx,
|
||||
Collection: collection,
|
||||
VolumeId: vid,
|
||||
Generation: generation,
|
||||
diskType: diskType,
|
||||
}
|
||||
|
||||
dataBaseFileName := EcShardFileName(collection, dir, int(vid))
|
||||
indexBaseFileName := EcShardFileName(collection, dirIdx, int(vid))
|
||||
// Use generation-aware filenames
|
||||
dataBaseFileName := EcShardFileNameWithGeneration(collection, dir, int(vid), generation)
|
||||
indexBaseFileName := EcShardFileNameWithGeneration(collection, dirIdx, int(vid), generation)
|
||||
|
||||
// open ecx file
|
||||
if ev.ecxFile, err = os.OpenFile(indexBaseFileName+".ecx", os.O_RDWR, 0644); err != nil {
|
||||
@@ -74,7 +83,7 @@ func NewEcVolume(diskType types.DiskType, dir string, dirIdx string, collection
|
||||
ev.datFileSize = volumeInfo.DatFileSize
|
||||
ev.ExpireAtSec = volumeInfo.ExpireAtSec
|
||||
} else {
|
||||
glog.Warningf("vif file not found,volumeId:%d, filename:%s", vid, dataBaseFileName)
|
||||
glog.V(1).Infof("vif file not found for volume %d generation %d, creating new one: %s", vid, generation, dataBaseFileName)
|
||||
volume_info.SaveVolumeInfo(dataBaseFileName+".vif", &volume_server_pb.VolumeInfo{Version: uint32(ev.Version)})
|
||||
}
|
||||
|
||||
@@ -155,7 +164,7 @@ func (ev *EcVolume) Destroy() {
|
||||
}
|
||||
|
||||
func (ev *EcVolume) FileName(ext string) string {
|
||||
return ev.FileNameWithGeneration(ext, 0)
|
||||
return ev.FileNameWithGeneration(ext, ev.Generation)
|
||||
}
|
||||
|
||||
func (ev *EcVolume) FileNameWithGeneration(ext string, generation uint32) string {
|
||||
@@ -168,7 +177,7 @@ func (ev *EcVolume) FileNameWithGeneration(ext string, generation uint32) string
|
||||
}
|
||||
|
||||
func (ev *EcVolume) DataBaseFileName() string {
|
||||
return EcShardFileName(ev.Collection, ev.dir, int(ev.VolumeId))
|
||||
return EcShardFileNameWithGeneration(ev.Collection, ev.dir, int(ev.VolumeId), ev.Generation)
|
||||
}
|
||||
|
||||
func (ev *EcVolume) DataBaseFileNameWithGeneration(generation uint32) string {
|
||||
@@ -176,7 +185,7 @@ func (ev *EcVolume) DataBaseFileNameWithGeneration(generation uint32) string {
|
||||
}
|
||||
|
||||
func (ev *EcVolume) IndexBaseFileName() string {
|
||||
return EcShardFileName(ev.Collection, ev.dirIdx, int(ev.VolumeId))
|
||||
return EcShardFileNameWithGeneration(ev.Collection, ev.dirIdx, int(ev.VolumeId), ev.Generation)
|
||||
}
|
||||
|
||||
func (ev *EcVolume) IndexBaseFileNameWithGeneration(generation uint32) string {
|
||||
@@ -190,6 +199,20 @@ func (ev *EcVolume) ShardSize() uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// String returns a string representation of the EC volume including generation
|
||||
func (ev *EcVolume) String() string {
|
||||
return fmt.Sprintf("EcVolume{Id:%d, Collection:%s, Generation:%d, Shards:%d}",
|
||||
ev.VolumeId, ev.Collection, ev.Generation, len(ev.Shards))
|
||||
}
|
||||
|
||||
// Key returns a unique key for this EC volume including generation
|
||||
func (ev *EcVolume) Key() string {
|
||||
if ev.Collection == "" {
|
||||
return fmt.Sprintf("%d_g%d", ev.VolumeId, ev.Generation)
|
||||
}
|
||||
return fmt.Sprintf("%s_%d_g%d", ev.Collection, ev.VolumeId, ev.Generation)
|
||||
}
|
||||
|
||||
func (ev *EcVolume) Size() (size uint64) {
|
||||
for _, shard := range ev.Shards {
|
||||
if shardSize := shard.Size(); shardSize > 0 {
|
||||
@@ -239,6 +262,7 @@ func (ev *EcVolume) ToVolumeEcShardInformationMessage(diskId uint32) (messages [
|
||||
DiskType: string(ev.diskType),
|
||||
ExpireAtSec: ev.ExpireAtSec,
|
||||
DiskId: diskId,
|
||||
Generation: ev.Generation, // include generation in heartbeat message
|
||||
}
|
||||
messages = append(messages, m)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user