seaweedfs/weed/storage/volume_info/volume_info.go

104 lines
3.1 KiB
Go
Raw Normal View History

2021-08-27 06:18:34 +08:00
package volume_info
import (
"fmt"
2022-08-18 03:42:03 +08:00
jsonpb "google.golang.org/protobuf/encoding/protojson"
2024-10-29 10:44:30 +08:00
"os"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
_ "github.com/seaweedfs/seaweedfs/weed/storage/backend/rclone_backend"
_ "github.com/seaweedfs/seaweedfs/weed/storage/backend/s3_backend"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// MaybeLoadVolumeInfo load the file data as *volume_server_pb.VolumeInfo, the returned volumeInfo will not be nil
func MaybeLoadVolumeInfo(fileName string) (volumeInfo *volume_server_pb.VolumeInfo, hasRemoteFile bool, hasVolumeInfoFile bool, err error) {
volumeInfo = &volume_server_pb.VolumeInfo{}
glog.V(1).Infof("maybeLoadVolumeInfo checks %s", fileName)
if exists, canRead, _, _, _ := util.CheckFile(fileName); !exists || !canRead {
if !exists {
return
}
hasVolumeInfoFile = true
if !canRead {
glog.Warningf("can not read %s", fileName)
err = fmt.Errorf("can not read %s", fileName)
return
}
return
}
hasVolumeInfoFile = true
glog.V(1).Infof("maybeLoadVolumeInfo reads %s", fileName)
2024-10-29 10:44:30 +08:00
fileData, readErr := os.ReadFile(fileName)
if readErr != nil {
glog.Warningf("fail to read %s : %v", fileName, readErr)
err = fmt.Errorf("fail to read %s : %v", fileName, readErr)
return
}
glog.V(1).Infof("maybeLoadVolumeInfo Unmarshal volume info %v", fileName)
2024-10-29 10:44:30 +08:00
if err = jsonpb.Unmarshal(fileData, volumeInfo); err != nil {
if oldVersionErr := tryOldVersionVolumeInfo(fileData, volumeInfo); oldVersionErr != nil {
glog.Warningf("unmarshal error: %v oldFormat: %v", err, oldVersionErr)
err = fmt.Errorf("unmarshal error: %v oldFormat: %v", err, oldVersionErr)
return
} else {
err = nil
}
}
if len(volumeInfo.GetFiles()) == 0 {
return
}
hasRemoteFile = true
return
}
func SaveVolumeInfo(fileName string, volumeInfo *volume_server_pb.VolumeInfo) error {
2019-12-29 13:40:12 +08:00
if exists, _, canWrite, _, _ := util.CheckFile(fileName); exists && !canWrite {
return fmt.Errorf("failed to check %s not writable", fileName)
}
2022-08-18 03:42:03 +08:00
m := jsonpb.MarshalOptions{
AllowPartial: true,
2022-08-18 03:42:03 +08:00
EmitUnpopulated: true,
Indent: " ",
}
2022-08-18 03:42:03 +08:00
text, marshalErr := m.Marshal(volumeInfo)
if marshalErr != nil {
return fmt.Errorf("failed to marshal %s: %v", fileName, marshalErr)
}
if err := util.WriteFile(fileName, text, 0644); err != nil {
return fmt.Errorf("failed to write %s: %v", fileName, err)
}
return nil
}
2024-10-29 10:44:30 +08:00
func tryOldVersionVolumeInfo(data []byte, volumeInfo *volume_server_pb.VolumeInfo) error {
oldVersionVolumeInfo := &volume_server_pb.OldVersionVolumeInfo{}
if err := jsonpb.Unmarshal(data, oldVersionVolumeInfo); err != nil {
return fmt.Errorf("failed to unmarshal old version volume info: %v", err)
}
volumeInfo.Files = oldVersionVolumeInfo.Files
volumeInfo.Version = oldVersionVolumeInfo.Version
volumeInfo.Replication = oldVersionVolumeInfo.Replication
volumeInfo.BytesOffset = oldVersionVolumeInfo.BytesOffset
volumeInfo.DatFileSize = oldVersionVolumeInfo.DatFileSize
volumeInfo.ExpireAtSec = oldVersionVolumeInfo.DestroyTime
volumeInfo.ReadOnly = oldVersionVolumeInfo.ReadOnly
return nil
}