volume.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. lastCompactIndexOffset uint64
  22. lastCompactRevision uint16
  23. }
  24. func NewVolume(dirname string, collection string, id VolumeId, needleMapKind NeedleMapType, replicaPlacement *ReplicaPlacement, ttl *TTL, preallocate int64) (v *Volume, e error) {
  25. v = &Volume{dir: dirname, Collection: collection, Id: id}
  26. v.SuperBlock = SuperBlock{ReplicaPlacement: replicaPlacement, Ttl: ttl}
  27. v.needleMapKind = needleMapKind
  28. e = v.load(true, true, needleMapKind, preallocate)
  29. return
  30. }
  31. func (v *Volume) String() string {
  32. 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)
  33. }
  34. func (v *Volume) FileName() (fileName string) {
  35. if v.Collection == "" {
  36. fileName = path.Join(v.dir, v.Id.String())
  37. } else {
  38. fileName = path.Join(v.dir, v.Collection+"_"+v.Id.String())
  39. }
  40. return
  41. }
  42. func (v *Volume) DataFile() *os.File {
  43. return v.dataFile
  44. }
  45. func (v *Volume) Version() Version {
  46. return v.SuperBlock.Version()
  47. }
  48. func (v *Volume) Size() int64 {
  49. stat, e := v.dataFile.Stat()
  50. if e == nil {
  51. return stat.Size()
  52. }
  53. glog.V(0).Infof("Failed to read file size %s %v", v.dataFile.Name(), e)
  54. return 0 // -1 causes integer overflow and the volume to become unwritable.
  55. }
  56. // Close cleanly shuts down this volume
  57. func (v *Volume) Close() {
  58. v.dataFileAccessLock.Lock()
  59. defer v.dataFileAccessLock.Unlock()
  60. v.nm.Close()
  61. _ = v.dataFile.Close()
  62. }
  63. func (v *Volume) NeedToReplicate() bool {
  64. return v.ReplicaPlacement.GetCopyCount() > 1
  65. }
  66. func (v *Volume) ContentSize() uint64 {
  67. return v.nm.ContentSize()
  68. }
  69. // volume is expired if modified time + volume ttl < now
  70. // except when volume is empty
  71. // or when the volume does not have a ttl
  72. // or when volumeSizeLimit is 0 when server just starts
  73. func (v *Volume) expired(volumeSizeLimit uint64) bool {
  74. if volumeSizeLimit == 0 {
  75. //skip if we don't know size limit
  76. return false
  77. }
  78. if v.ContentSize() == 0 {
  79. return false
  80. }
  81. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  82. return false
  83. }
  84. glog.V(1).Infof("now:%v lastModified:%v", time.Now().Unix(), v.lastModifiedTime)
  85. livedMinutes := (time.Now().Unix() - int64(v.lastModifiedTime)) / 60
  86. glog.V(1).Infof("ttl:%v lived:%v", v.Ttl, livedMinutes)
  87. if int64(v.Ttl.Minutes()) < livedMinutes {
  88. return true
  89. }
  90. return false
  91. }
  92. // wait either maxDelayMinutes or 10% of ttl minutes
  93. func (v *Volume) exiredLongEnough(maxDelayMinutes uint32) bool {
  94. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  95. return false
  96. }
  97. removalDelay := v.Ttl.Minutes() / 10
  98. if removalDelay > maxDelayMinutes {
  99. removalDelay = maxDelayMinutes
  100. }
  101. if uint64(v.Ttl.Minutes()+removalDelay)*60+v.lastModifiedTime < uint64(time.Now().Unix()) {
  102. return true
  103. }
  104. return false
  105. }