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.

135 lines
3.3 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. 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. // if replicaPlacement is nil, the superblock will be loaded from disk
  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. v.dataFileAccessLock.Lock()
  51. defer v.dataFileAccessLock.Unlock()
  52. if v.dataFile == nil {
  53. return 0
  54. }
  55. stat, e := v.dataFile.Stat()
  56. if e == nil {
  57. return stat.Size()
  58. }
  59. glog.V(0).Infof("Failed to read file size %s %v", v.dataFile.Name(), e)
  60. return 0 // -1 causes integer overflow and the volume to become unwritable.
  61. }
  62. // Close cleanly shuts down this volume
  63. func (v *Volume) Close() {
  64. v.dataFileAccessLock.Lock()
  65. defer v.dataFileAccessLock.Unlock()
  66. if v.nm != nil {
  67. v.nm.Close()
  68. v.nm = nil
  69. }
  70. if v.dataFile != nil {
  71. _ = v.dataFile.Close()
  72. v.dataFile = nil
  73. }
  74. }
  75. func (v *Volume) NeedToReplicate() bool {
  76. return v.ReplicaPlacement.GetCopyCount() > 1
  77. }
  78. func (v *Volume) ContentSize() uint64 {
  79. return v.nm.ContentSize()
  80. }
  81. // volume is expired if modified time + volume ttl < now
  82. // except when volume is empty
  83. // or when the volume does not have a ttl
  84. // or when volumeSizeLimit is 0 when server just starts
  85. func (v *Volume) expired(volumeSizeLimit uint64) bool {
  86. if volumeSizeLimit == 0 {
  87. //skip if we don't know size limit
  88. return false
  89. }
  90. if v.ContentSize() == 0 {
  91. return false
  92. }
  93. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  94. return false
  95. }
  96. glog.V(1).Infof("now:%v lastModified:%v", time.Now().Unix(), v.lastModifiedTime)
  97. livedMinutes := (time.Now().Unix() - int64(v.lastModifiedTime)) / 60
  98. glog.V(1).Infof("ttl:%v lived:%v", v.Ttl, livedMinutes)
  99. if int64(v.Ttl.Minutes()) < livedMinutes {
  100. return true
  101. }
  102. return false
  103. }
  104. // wait either maxDelayMinutes or 10% of ttl minutes
  105. func (v *Volume) exiredLongEnough(maxDelayMinutes uint32) bool {
  106. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  107. return false
  108. }
  109. removalDelay := v.Ttl.Minutes() / 10
  110. if removalDelay > maxDelayMinutes {
  111. removalDelay = maxDelayMinutes
  112. }
  113. if uint64(v.Ttl.Minutes()+removalDelay)*60+v.lastModifiedTime < uint64(time.Now().Unix()) {
  114. return true
  115. }
  116. return false
  117. }