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.

113 lines
2.7 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) HasRemoteFile() bool {
  51. if v.DataBackend == nil {
  52. return false
  53. }
  54. _, ok := v.DataBackend.(*backend.DiskFile)
  55. return !ok
  56. }
  57. func (v *Volume) LoadRemoteFile() error {
  58. tierFile := v.volumeTierInfo.GetFiles()[0]
  59. backendStorage := backend.BackendStorages[tierFile.BackendName()]
  60. if v.DataBackend != nil {
  61. v.DataBackend.Close()
  62. }
  63. v.DataBackend = backendStorage.NewStorageFile(tierFile.Key, v.volumeTierInfo)
  64. return nil
  65. }
  66. func (v *Volume) SaveVolumeTierInfo() error {
  67. tierFileName := v.FileName() + ".tier"
  68. if exists, _, canWrite, _, _ := checkFile(tierFileName); exists && !canWrite {
  69. return fmt.Errorf("%s not writable", tierFileName)
  70. }
  71. m := jsonpb.Marshaler{
  72. EmitDefaults: true,
  73. Indent: " ",
  74. }
  75. text, marshalErr := m.MarshalToString(v.GetVolumeTierInfo())
  76. if marshalErr != nil {
  77. return fmt.Errorf("marshal volume %d tier info: %v", v.Id, marshalErr)
  78. }
  79. writeErr := ioutil.WriteFile(tierFileName, []byte(text), 0755)
  80. if writeErr != nil {
  81. return fmt.Errorf("fail to write %s : %v", tierFileName, writeErr)
  82. }
  83. return nil
  84. }