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.

153 lines
4.0 KiB

10 years ago
10 years ago
12 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  5. "os"
  6. "path"
  7. "sync"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. )
  11. type Volume struct {
  12. Id VolumeId
  13. dir string
  14. Collection string
  15. dataFile *os.File
  16. nm NeedleMapper
  17. compactingWg sync.WaitGroup
  18. needleMapKind NeedleMapType
  19. readOnly bool
  20. SuperBlock
  21. dataFileAccessLock sync.Mutex
  22. lastModifiedTime uint64 //unix time in seconds
  23. lastCompactIndexOffset uint64
  24. lastCompactRevision uint16
  25. }
  26. func NewVolume(dirname string, collection string, id VolumeId, needleMapKind NeedleMapType, replicaPlacement *ReplicaPlacement, ttl *TTL, preallocate int64) (v *Volume, e error) {
  27. // if replicaPlacement is nil, the superblock will be loaded from disk
  28. v = &Volume{dir: dirname, Collection: collection, Id: id}
  29. v.SuperBlock = SuperBlock{ReplicaPlacement: replicaPlacement, Ttl: ttl}
  30. v.needleMapKind = needleMapKind
  31. e = v.load(true, true, needleMapKind, preallocate)
  32. return
  33. }
  34. func (v *Volume) String() string {
  35. 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)
  36. }
  37. func (v *Volume) FileName() (fileName string) {
  38. if v.Collection == "" {
  39. fileName = path.Join(v.dir, v.Id.String())
  40. } else {
  41. fileName = path.Join(v.dir, v.Collection+"_"+v.Id.String())
  42. }
  43. return
  44. }
  45. func (v *Volume) DataFile() *os.File {
  46. return v.dataFile
  47. }
  48. func (v *Volume) Version() Version {
  49. return v.SuperBlock.Version()
  50. }
  51. func (v *Volume) Size() int64 {
  52. v.dataFileAccessLock.Lock()
  53. defer v.dataFileAccessLock.Unlock()
  54. if v.dataFile == nil {
  55. return 0
  56. }
  57. stat, e := v.dataFile.Stat()
  58. if e == nil {
  59. return stat.Size()
  60. }
  61. glog.V(0).Infof("Failed to read file size %s %v", v.dataFile.Name(), e)
  62. return 0 // -1 causes integer overflow and the volume to become unwritable.
  63. }
  64. // Close cleanly shuts down this volume
  65. func (v *Volume) Close() {
  66. v.dataFileAccessLock.Lock()
  67. defer v.dataFileAccessLock.Unlock()
  68. if v.nm != nil {
  69. v.nm.Close()
  70. v.nm = nil
  71. }
  72. if v.dataFile != nil {
  73. _ = v.dataFile.Close()
  74. v.dataFile = nil
  75. }
  76. }
  77. func (v *Volume) NeedToReplicate() bool {
  78. return v.ReplicaPlacement.GetCopyCount() > 1
  79. }
  80. func (v *Volume) ContentSize() uint64 {
  81. return v.nm.ContentSize()
  82. }
  83. // volume is expired if modified time + volume ttl < now
  84. // except when volume is empty
  85. // or when the volume does not have a ttl
  86. // or when volumeSizeLimit is 0 when server just starts
  87. func (v *Volume) expired(volumeSizeLimit uint64) bool {
  88. if volumeSizeLimit == 0 {
  89. //skip if we don't know size limit
  90. return false
  91. }
  92. if v.ContentSize() == 0 {
  93. return false
  94. }
  95. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  96. return false
  97. }
  98. glog.V(1).Infof("now:%v lastModified:%v", time.Now().Unix(), v.lastModifiedTime)
  99. livedMinutes := (time.Now().Unix() - int64(v.lastModifiedTime)) / 60
  100. glog.V(1).Infof("ttl:%v lived:%v", v.Ttl, livedMinutes)
  101. if int64(v.Ttl.Minutes()) < livedMinutes {
  102. return true
  103. }
  104. return false
  105. }
  106. // wait either maxDelayMinutes or 10% of ttl minutes
  107. func (v *Volume) expiredLongEnough(maxDelayMinutes uint32) bool {
  108. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  109. return false
  110. }
  111. removalDelay := v.Ttl.Minutes() / 10
  112. if removalDelay > maxDelayMinutes {
  113. removalDelay = maxDelayMinutes
  114. }
  115. if uint64(v.Ttl.Minutes()+removalDelay)*60+v.lastModifiedTime < uint64(time.Now().Unix()) {
  116. return true
  117. }
  118. return false
  119. }
  120. func (v *Volume) ToVolumeInformationMessage() *master_pb.VolumeInformationMessage {
  121. return &master_pb.VolumeInformationMessage{
  122. Id: uint32(v.Id),
  123. Size: uint64(v.Size()),
  124. Collection: v.Collection,
  125. FileCount: uint64(v.nm.FileCount()),
  126. DeleteCount: uint64(v.nm.DeletedCount()),
  127. DeletedByteCount: v.nm.DeletedSize(),
  128. ReadOnly: v.readOnly,
  129. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  130. Version: uint32(v.Version()),
  131. Ttl: v.Ttl.ToUint32(),
  132. CompactRevision: uint32(v.SuperBlock.CompactRevision),
  133. }
  134. }