2021-08-26 22:18:34 +00:00
|
|
|
package volume_info
|
2019-12-28 20:28:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-10-14 04:27:58 +00:00
|
|
|
"os"
|
2019-12-28 20:28:58 +00:00
|
|
|
|
2022-08-17 19:42:03 +00:00
|
|
|
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
2019-12-28 20:28:58 +00:00
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
2023-04-18 21:12:12 +00:00
|
|
|
_ "github.com/seaweedfs/seaweedfs/weed/storage/backend/rclone_backend"
|
2022-07-29 07:17:28 +00:00
|
|
|
_ "github.com/seaweedfs/seaweedfs/weed/storage/backend/s3_backend"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2019-12-28 20:28:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MaybeLoadVolumeInfo load the file data as *volume_server_pb.VolumeInfo, the returned volumeInfo will not be nil
|
2021-03-09 20:09:32 +00:00
|
|
|
func MaybeLoadVolumeInfo(fileName string) (volumeInfo *volume_server_pb.VolumeInfo, hasRemoteFile bool, hasVolumeInfoFile bool, err error) {
|
2019-12-28 20:28:58 +00:00
|
|
|
|
2021-03-09 20:09:32 +00:00
|
|
|
volumeInfo = &volume_server_pb.VolumeInfo{}
|
2019-12-28 20:28:58 +00:00
|
|
|
|
|
|
|
glog.V(1).Infof("maybeLoadVolumeInfo checks %s", fileName)
|
|
|
|
if exists, canRead, _, _, _ := util.CheckFile(fileName); !exists || !canRead {
|
|
|
|
if !exists {
|
2021-03-09 20:09:32 +00:00
|
|
|
return
|
2019-12-28 20:28:58 +00:00
|
|
|
}
|
2021-03-09 20:09:32 +00:00
|
|
|
hasVolumeInfoFile = true
|
2019-12-28 20:28:58 +00:00
|
|
|
if !canRead {
|
|
|
|
glog.Warningf("can not read %s", fileName)
|
2021-03-09 20:09:32 +00:00
|
|
|
err = fmt.Errorf("can not read %s", fileName)
|
|
|
|
return
|
2019-12-28 20:28:58 +00:00
|
|
|
}
|
2021-03-09 20:09:32 +00:00
|
|
|
return
|
2019-12-28 20:28:58 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 20:09:32 +00:00
|
|
|
hasVolumeInfoFile = true
|
|
|
|
|
2019-12-28 20:28:58 +00:00
|
|
|
glog.V(1).Infof("maybeLoadVolumeInfo reads %s", fileName)
|
2021-10-14 04:27:58 +00:00
|
|
|
tierData, readErr := os.ReadFile(fileName)
|
2019-12-28 20:28:58 +00:00
|
|
|
if readErr != nil {
|
|
|
|
glog.Warningf("fail to read %s : %v", fileName, readErr)
|
2021-03-09 20:09:32 +00:00
|
|
|
err = fmt.Errorf("fail to read %s : %v", fileName, readErr)
|
|
|
|
return
|
|
|
|
|
2019-12-28 20:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("maybeLoadVolumeInfo Unmarshal volume info %v", fileName)
|
2022-08-17 19:42:03 +00:00
|
|
|
if err = jsonpb.Unmarshal(tierData, volumeInfo); err != nil {
|
2019-12-28 20:28:58 +00:00
|
|
|
glog.Warningf("unmarshal error: %v", err)
|
2021-03-09 20:09:32 +00:00
|
|
|
err = fmt.Errorf("unmarshal error: %v", err)
|
|
|
|
return
|
2019-12-28 20:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(volumeInfo.GetFiles()) == 0 {
|
2021-03-09 20:09:32 +00:00
|
|
|
return
|
2019-12-28 20:28:58 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 20:09:32 +00:00
|
|
|
hasRemoteFile = true
|
|
|
|
|
|
|
|
return
|
2019-12-28 20:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SaveVolumeInfo(fileName string, volumeInfo *volume_server_pb.VolumeInfo) error {
|
|
|
|
|
2019-12-29 05:40:12 +00:00
|
|
|
if exists, _, canWrite, _, _ := util.CheckFile(fileName); exists && !canWrite {
|
2022-10-14 07:18:09 +00:00
|
|
|
return fmt.Errorf("failed to check %s not writable", fileName)
|
2019-12-28 20:28:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 19:42:03 +00:00
|
|
|
m := jsonpb.MarshalOptions{
|
2022-10-14 07:18:09 +00:00
|
|
|
AllowPartial: true,
|
2022-08-17 19:42:03 +00:00
|
|
|
EmitUnpopulated: true,
|
|
|
|
Indent: " ",
|
2019-12-28 20:28:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 19:42:03 +00:00
|
|
|
text, marshalErr := m.Marshal(volumeInfo)
|
2019-12-28 20:28:58 +00:00
|
|
|
if marshalErr != nil {
|
2022-10-14 07:18:09 +00:00
|
|
|
return fmt.Errorf("failed to marshal %s: %v", fileName, marshalErr)
|
2019-12-28 20:28:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 19:42:03 +00:00
|
|
|
writeErr := util.WriteFile(fileName, text, 0755)
|
2019-12-28 20:28:58 +00:00
|
|
|
if writeErr != nil {
|
2022-10-14 07:18:09 +00:00
|
|
|
return fmt.Errorf("failed to write %s: %v", fileName, writeErr)
|
2019-12-28 20:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|