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.

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