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.

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