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.

118 lines
2.9 KiB

10 years ago
10 years ago
12 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "sync"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. )
  10. type Volume struct {
  11. Id VolumeId
  12. dir string
  13. Collection string
  14. dataFile *os.File
  15. nm NeedleMapper
  16. needleMapKind NeedleMapType
  17. readOnly bool
  18. SuperBlock
  19. dataFileAccessLock sync.Mutex
  20. lastModifiedTime uint64 //unix time in seconds
  21. }
  22. func NewVolume(dirname string, collection string, id VolumeId, needleMapKind NeedleMapType, replicaPlacement *ReplicaPlacement, ttl *TTL) (v *Volume, e error) {
  23. v = &Volume{dir: dirname, Collection: collection, Id: id}
  24. v.SuperBlock = SuperBlock{ReplicaPlacement: replicaPlacement, Ttl: ttl}
  25. v.needleMapKind = needleMapKind
  26. e = v.load(true, true, needleMapKind)
  27. return
  28. }
  29. func (v *Volume) String() string {
  30. return fmt.Sprintf("Id:%v, dir:%s, Collection:%s, dataFile:%v, nm:%v, readOnly:%v", v.Id, v.dir, v.Collection, v.dataFile, v.nm, v.readOnly)
  31. }
  32. func (v *Volume) FileName() (fileName string) {
  33. if v.Collection == "" {
  34. fileName = path.Join(v.dir, v.Id.String())
  35. } else {
  36. fileName = path.Join(v.dir, v.Collection+"_"+v.Id.String())
  37. }
  38. return
  39. }
  40. func (v *Volume) DataFile() *os.File {
  41. return v.dataFile
  42. }
  43. func (v *Volume) Version() Version {
  44. return v.SuperBlock.Version()
  45. }
  46. func (v *Volume) Size() int64 {
  47. stat, e := v.dataFile.Stat()
  48. if e == nil {
  49. return stat.Size()
  50. }
  51. glog.V(0).Infof("Failed to read file size %s %v", v.dataFile.Name(), e)
  52. return -1
  53. }
  54. // Close cleanly shuts down this volume
  55. func (v *Volume) Close() {
  56. v.dataFileAccessLock.Lock()
  57. defer v.dataFileAccessLock.Unlock()
  58. v.nm.Close()
  59. _ = v.dataFile.Close()
  60. }
  61. func (v *Volume) NeedToReplicate() bool {
  62. return v.ReplicaPlacement.GetCopyCount() > 1
  63. }
  64. func (v *Volume) ContentSize() uint64 {
  65. return v.nm.ContentSize()
  66. }
  67. // volume is expired if modified time + volume ttl < now
  68. // except when volume is empty
  69. // or when the volume does not have a ttl
  70. // or when volumeSizeLimit is 0 when server just starts
  71. func (v *Volume) expired(volumeSizeLimit uint64) bool {
  72. if volumeSizeLimit == 0 {
  73. //skip if we don't know size limit
  74. return false
  75. }
  76. if v.ContentSize() == 0 {
  77. return false
  78. }
  79. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  80. return false
  81. }
  82. glog.V(0).Infof("now:%v lastModified:%v", time.Now().Unix(), v.lastModifiedTime)
  83. livedMinutes := (time.Now().Unix() - int64(v.lastModifiedTime)) / 60
  84. glog.V(0).Infof("ttl:%v lived:%v", v.Ttl, livedMinutes)
  85. if int64(v.Ttl.Minutes()) < livedMinutes {
  86. return true
  87. }
  88. return false
  89. }
  90. // wait either maxDelayMinutes or 10% of ttl minutes
  91. func (v *Volume) exiredLongEnough(maxDelayMinutes uint32) bool {
  92. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  93. return false
  94. }
  95. removalDelay := v.Ttl.Minutes() / 10
  96. if removalDelay > maxDelayMinutes {
  97. removalDelay = maxDelayMinutes
  98. }
  99. if uint64(v.Ttl.Minutes()+removalDelay)*60+v.lastModifiedTime < uint64(time.Now().Unix()) {
  100. return true
  101. }
  102. return false
  103. }