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.

177 lines
4.4 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. func (v *Volume)IndexFileSize() uint64 {
  70. return v.nm.IndexFileSize()
  71. }
  72. func (v *Volume)DataFileSize() uint64 {
  73. return uint64(v.Size())
  74. }
  75. /**
  76. unix time in seconds
  77. */
  78. func (v *Volume)LastModifiedTime() uint64 {
  79. return v.lastModifiedTime
  80. }
  81. func (v *Volume)FileCount() uint64 {
  82. return uint64(v.nm.FileCount())
  83. }
  84. // Close cleanly shuts down this volume
  85. func (v *Volume) Close() {
  86. v.dataFileAccessLock.Lock()
  87. defer v.dataFileAccessLock.Unlock()
  88. if v.nm != nil {
  89. v.nm.Close()
  90. v.nm = nil
  91. }
  92. if v.dataFile != nil {
  93. _ = v.dataFile.Close()
  94. v.dataFile = nil
  95. }
  96. }
  97. func (v *Volume) NeedToReplicate() bool {
  98. return v.ReplicaPlacement.GetCopyCount() > 1
  99. }
  100. func (v *Volume) ContentSize() uint64 {
  101. return v.nm.ContentSize()
  102. }
  103. // volume is expired if modified time + volume ttl < now
  104. // except when volume is empty
  105. // or when the volume does not have a ttl
  106. // or when volumeSizeLimit is 0 when server just starts
  107. func (v *Volume) expired(volumeSizeLimit uint64) bool {
  108. if volumeSizeLimit == 0 {
  109. //skip if we don't know size limit
  110. return false
  111. }
  112. if v.ContentSize() == 0 {
  113. return false
  114. }
  115. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  116. return false
  117. }
  118. glog.V(1).Infof("now:%v lastModified:%v", time.Now().Unix(), v.lastModifiedTime)
  119. livedMinutes := (time.Now().Unix() - int64(v.lastModifiedTime)) / 60
  120. glog.V(1).Infof("ttl:%v lived:%v", v.Ttl, livedMinutes)
  121. if int64(v.Ttl.Minutes()) < livedMinutes {
  122. return true
  123. }
  124. return false
  125. }
  126. // wait either maxDelayMinutes or 10% of ttl minutes
  127. func (v *Volume) expiredLongEnough(maxDelayMinutes uint32) bool {
  128. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  129. return false
  130. }
  131. removalDelay := v.Ttl.Minutes() / 10
  132. if removalDelay > maxDelayMinutes {
  133. removalDelay = maxDelayMinutes
  134. }
  135. if uint64(v.Ttl.Minutes()+removalDelay)*60+v.lastModifiedTime < uint64(time.Now().Unix()) {
  136. return true
  137. }
  138. return false
  139. }
  140. func (v *Volume) ToVolumeInformationMessage() *master_pb.VolumeInformationMessage {
  141. return &master_pb.VolumeInformationMessage{
  142. Id: uint32(v.Id),
  143. Size: uint64(v.Size()),
  144. Collection: v.Collection,
  145. FileCount: uint64(v.nm.FileCount()),
  146. DeleteCount: uint64(v.nm.DeletedCount()),
  147. DeletedByteCount: v.nm.DeletedSize(),
  148. ReadOnly: v.readOnly,
  149. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  150. Version: uint32(v.Version()),
  151. Ttl: v.Ttl.ToUint32(),
  152. CompactRevision: uint32(v.SuperBlock.CompactRevision),
  153. }
  154. }