You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.6 KiB

  1. package storage
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. _ "github.com/chrislusf/seaweedfs/weed/storage/backend/s3_backend"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  9. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  10. "github.com/golang/protobuf/jsonpb"
  11. )
  12. func (v *Volume) GetVolumeTierInfo() *volume_server_pb.VolumeTierInfo {
  13. return v.volumeTierInfo
  14. }
  15. func (v *Volume) maybeLoadVolumeTierInfo() bool {
  16. v.volumeTierInfo = &volume_server_pb.VolumeTierInfo{}
  17. tierFileName := v.FileName() + ".tier"
  18. if exists, canRead, _, _, _ := checkFile(tierFileName); !exists || !canRead {
  19. if !exists {
  20. return false
  21. }
  22. if !canRead {
  23. glog.Warningf("can not read %s", tierFileName)
  24. }
  25. return false
  26. }
  27. glog.V(0).Infof("maybeLoadVolumeTierInfo loading volume %d check file", v.Id)
  28. tierData, readErr := ioutil.ReadFile(tierFileName)
  29. if readErr != nil {
  30. glog.Warningf("fail to read %s : %v", tierFileName, readErr)
  31. return false
  32. }
  33. glog.V(0).Infof("maybeLoadVolumeTierInfo loading volume %d ReadFile", v.Id)
  34. if err := jsonpb.Unmarshal(bytes.NewReader(tierData), v.volumeTierInfo); err != nil {
  35. glog.Warningf("unmarshal error: %v", err)
  36. return false
  37. }
  38. glog.V(0).Infof("maybeLoadVolumeTierInfo loading volume %d Unmarshal tierInfo %v", v.Id, v.volumeTierInfo)
  39. if len(v.volumeTierInfo.GetFiles()) == 0 {
  40. return false
  41. }
  42. glog.V(0).Infof("volume %d is tiered to %s as %s and read only", v.Id,
  43. v.volumeTierInfo.Files[0].BackendName(), v.volumeTierInfo.Files[0].Key)
  44. v.noWriteCanDelete = true
  45. v.noWriteOrDelete = false
  46. glog.V(0).Infof("loading volume %d from remote %v", v.Id, v.volumeTierInfo.Files)
  47. v.LoadRemoteFile()
  48. return true
  49. }
  50. func (v *Volume) LoadRemoteFile() error {
  51. tierFile := v.volumeTierInfo.GetFiles()[0]
  52. backendStorage := backend.BackendStorages[tierFile.BackendName()]
  53. if v.DataBackend != nil {
  54. v.DataBackend.Close()
  55. }
  56. v.DataBackend = backendStorage.NewStorageFile(tierFile.Key, v.volumeTierInfo)
  57. return nil
  58. }
  59. func (v *Volume) SaveVolumeTierInfo() error {
  60. tierFileName := v.FileName() + ".tier"
  61. if exists, _, canWrite, _, _ := checkFile(tierFileName); exists && !canWrite {
  62. return fmt.Errorf("%s not writable", tierFileName)
  63. }
  64. m := jsonpb.Marshaler{
  65. EmitDefaults: true,
  66. Indent: " ",
  67. }
  68. text, marshalErr := m.MarshalToString(v.GetVolumeTierInfo())
  69. if marshalErr != nil {
  70. return fmt.Errorf("marshal volume %d tier info: %v", v.Id, marshalErr)
  71. }
  72. writeErr := ioutil.WriteFile(tierFileName, []byte(text), 0755)
  73. if writeErr != nil {
  74. return fmt.Errorf("fail to write %s : %v", tierFileName, writeErr)
  75. }
  76. return nil
  77. }