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.

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