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.

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