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.

220 lines
5.6 KiB

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