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.

171 lines
4.5 KiB

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