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.

173 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. 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(dir string, collection 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.dir, v.Collection, 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. stats.VolumeServerVolumeCounter.WithLabelValues(v.Collection, "volume").Dec()
  89. }
  90. }
  91. func (v *Volume) NeedToReplicate() bool {
  92. return v.ReplicaPlacement.GetCopyCount() > 1
  93. }
  94. func (v *Volume) ContentSize() uint64 {
  95. return v.nm.ContentSize()
  96. }
  97. // volume is expired if modified time + volume ttl < now
  98. // except when volume is empty
  99. // or when the volume does not have a ttl
  100. // or when volumeSizeLimit is 0 when server just starts
  101. func (v *Volume) expired(volumeSizeLimit uint64) bool {
  102. if volumeSizeLimit == 0 {
  103. //skip if we don't know size limit
  104. return false
  105. }
  106. if v.ContentSize() == 0 {
  107. return false
  108. }
  109. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  110. return false
  111. }
  112. glog.V(1).Infof("now:%v lastModified:%v", time.Now().Unix(), v.lastModifiedTsSeconds)
  113. livedMinutes := (time.Now().Unix() - int64(v.lastModifiedTsSeconds)) / 60
  114. glog.V(1).Infof("ttl:%v lived:%v", v.Ttl, livedMinutes)
  115. if int64(v.Ttl.Minutes()) < livedMinutes {
  116. return true
  117. }
  118. return false
  119. }
  120. // wait either maxDelayMinutes or 10% of ttl minutes
  121. func (v *Volume) expiredLongEnough(maxDelayMinutes uint32) bool {
  122. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  123. return false
  124. }
  125. removalDelay := v.Ttl.Minutes() / 10
  126. if removalDelay > maxDelayMinutes {
  127. removalDelay = maxDelayMinutes
  128. }
  129. if uint64(v.Ttl.Minutes()+removalDelay)*60+v.lastModifiedTsSeconds < uint64(time.Now().Unix()) {
  130. return true
  131. }
  132. return false
  133. }
  134. func (v *Volume) ToVolumeInformationMessage() *master_pb.VolumeInformationMessage {
  135. size, _, modTime := v.FileStat()
  136. return &master_pb.VolumeInformationMessage{
  137. Id: uint32(v.Id),
  138. Size: size,
  139. Collection: v.Collection,
  140. FileCount: uint64(v.nm.FileCount()),
  141. DeleteCount: uint64(v.nm.DeletedCount()),
  142. DeletedByteCount: v.nm.DeletedSize(),
  143. ReadOnly: v.readOnly,
  144. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  145. Version: uint32(v.Version()),
  146. Ttl: v.Ttl.ToUint32(),
  147. CompactRevision: uint32(v.SuperBlock.CompactionRevision),
  148. ModifiedAtSecond: modTime.Unix(),
  149. }
  150. }