seaweedfs/weed/storage/volume_tier.go

107 lines
2.5 KiB
Go
Raw Normal View History

package storage
import (
"bytes"
"fmt"
"io/ioutil"
_ "github.com/chrislusf/seaweedfs/weed/storage/backend/s3_backend"
"github.com/golang/protobuf/jsonpb"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
"github.com/chrislusf/seaweedfs/weed/storage/backend"
)
2019-12-29 03:21:49 +08:00
func (v *Volume) GetVolumeInfo() *volume_server_pb.VolumeInfo {
return v.volumeInfo
}
2019-12-29 03:21:49 +08:00
func (v *Volume) maybeLoadVolumeInfo() bool {
2019-12-29 03:21:49 +08:00
v.volumeInfo = &volume_server_pb.VolumeInfo{}
2019-12-29 03:17:39 +08:00
tierFileName := v.FileName() + ".vif"
if exists, canRead, _, _, _ := checkFile(tierFileName); !exists || !canRead {
if !exists {
return false
}
if !canRead {
glog.Warningf("can not read %s", tierFileName)
}
return false
}
2019-12-29 03:21:49 +08:00
glog.V(0).Infof("maybeLoadVolumeInfo loading volume %d check file", v.Id)
tierData, readErr := ioutil.ReadFile(tierFileName)
if readErr != nil {
glog.Warningf("fail to read %s : %v", tierFileName, readErr)
return false
}
2019-12-29 03:21:49 +08:00
glog.V(0).Infof("maybeLoadVolumeInfo loading volume %d ReadFile", v.Id)
2019-12-29 03:21:49 +08:00
if err := jsonpb.Unmarshal(bytes.NewReader(tierData), v.volumeInfo); err != nil {
glog.Warningf("unmarshal error: %v", err)
return false
}
2019-12-29 03:21:49 +08:00
glog.V(0).Infof("maybeLoadVolumeInfo loading volume %d Unmarshal tierInfo %v", v.Id, v.volumeInfo)
2019-12-29 03:21:49 +08:00
if len(v.volumeInfo.GetFiles()) == 0 {
return false
}
glog.V(0).Infof("volume %d is tiered to %s as %s and read only", v.Id,
2019-12-29 03:21:49 +08:00
v.volumeInfo.Files[0].BackendName(), v.volumeInfo.Files[0].Key)
v.hasRemoteFile = true
return true
}
2019-12-26 08:17:58 +08:00
func (v *Volume) HasRemoteFile() bool {
return v.hasRemoteFile
2019-12-26 08:17:58 +08:00
}
func (v *Volume) LoadRemoteFile() error {
2019-12-29 03:21:49 +08:00
tierFile := v.volumeInfo.GetFiles()[0]
backendStorage := backend.BackendStorages[tierFile.BackendName()]
if v.DataBackend != nil {
v.DataBackend.Close()
}
2019-12-29 03:21:49 +08:00
v.DataBackend = backendStorage.NewStorageFile(tierFile.Key, v.volumeInfo)
return nil
}
2019-12-29 03:21:49 +08:00
func (v *Volume) SaveVolumeInfo() error {
2019-12-29 03:17:39 +08:00
tierFileName := v.FileName() + ".vif"
if exists, _, canWrite, _, _ := checkFile(tierFileName); exists && !canWrite {
return fmt.Errorf("%s not writable", tierFileName)
}
m := jsonpb.Marshaler{
EmitDefaults: true,
Indent: " ",
}
2019-12-29 03:21:49 +08:00
text, marshalErr := m.MarshalToString(v.GetVolumeInfo())
if marshalErr != nil {
return fmt.Errorf("marshal volume %d tier info: %v", v.Id, marshalErr)
}
writeErr := ioutil.WriteFile(tierFileName, []byte(text), 0755)
if writeErr != nil {
return fmt.Errorf("fail to write %s : %v", tierFileName, writeErr)
}
return nil
}