2019-12-03 07:08:28 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
_ "github.com/chrislusf/seaweedfs/weed/storage/backend/s3_backend"
|
|
|
|
|
2019-12-29 03:35:27 +08:00
|
|
|
"github.com/golang/protobuf/jsonpb"
|
|
|
|
|
2019-12-03 07:08:28 +08:00
|
|
|
"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-03 07:08:28 +08:00
|
|
|
}
|
|
|
|
|
2019-12-29 03:21:49 +08:00
|
|
|
func (v *Volume) maybeLoadVolumeInfo() bool {
|
2019-12-03 07:08:28 +08:00
|
|
|
|
2019-12-29 03:21:49 +08:00
|
|
|
v.volumeInfo = &volume_server_pb.VolumeInfo{}
|
2019-12-03 07:08:28 +08:00
|
|
|
|
2019-12-29 03:17:39 +08:00
|
|
|
tierFileName := v.FileName() + ".vif"
|
2019-12-03 07:08:28 +08:00
|
|
|
|
|
|
|
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)
|
2019-12-03 07:08:28 +08:00
|
|
|
|
|
|
|
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-03 07:08:28 +08:00
|
|
|
|
2019-12-29 03:21:49 +08:00
|
|
|
if err := jsonpb.Unmarshal(bytes.NewReader(tierData), v.volumeInfo); err != nil {
|
2019-12-03 07:08:28 +08:00
|
|
|
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-03 07:08:28 +08:00
|
|
|
|
2019-12-29 03:21:49 +08:00
|
|
|
if len(v.volumeInfo.GetFiles()) == 0 {
|
2019-12-03 07:08:28 +08:00
|
|
|
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)
|
2019-12-03 07:08:28 +08:00
|
|
|
|
2019-12-29 03:35:27 +08:00
|
|
|
v.hasRemoteFile = true
|
2019-12-03 07:08:28 +08:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-12-26 08:17:58 +08:00
|
|
|
func (v *Volume) HasRemoteFile() bool {
|
2019-12-29 03:35:27 +08:00
|
|
|
return v.hasRemoteFile
|
2019-12-26 08:17:58 +08:00
|
|
|
}
|
|
|
|
|
2019-12-03 07:08:28 +08:00
|
|
|
func (v *Volume) LoadRemoteFile() error {
|
2019-12-29 03:21:49 +08:00
|
|
|
tierFile := v.volumeInfo.GetFiles()[0]
|
2019-12-03 07:08:28 +08:00
|
|
|
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)
|
2019-12-03 07:08:28 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-29 03:21:49 +08:00
|
|
|
func (v *Volume) SaveVolumeInfo() error {
|
2019-12-03 07:08:28 +08:00
|
|
|
|
2019-12-29 03:17:39 +08:00
|
|
|
tierFileName := v.FileName() + ".vif"
|
2019-12-03 07:08:28 +08:00
|
|
|
|
|
|
|
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())
|
2019-12-03 07:08:28 +08:00
|
|
|
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
|
|
|
|
}
|