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.

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