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.

231 lines
6.1 KiB

6 years ago
6 years ago
6 years ago
5 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/pb/volume_server_pb"
  6. "github.com/chrislusf/seaweedfs/weed/stats"
  7. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. "github.com/chrislusf/seaweedfs/weed/storage/types"
  10. "path"
  11. "strconv"
  12. "sync"
  13. "time"
  14. "github.com/chrislusf/seaweedfs/weed/glog"
  15. )
  16. type Volume struct {
  17. Id needle.VolumeId
  18. dir string
  19. Collection string
  20. DataBackend backend.BackendStorageFile
  21. nm NeedleMapper
  22. needleMapKind NeedleMapType
  23. readOnly bool
  24. MemoryMapMaxSizeMb uint32
  25. SuperBlock
  26. dataFileAccessLock sync.RWMutex
  27. lastModifiedTsSeconds uint64 //unix time in seconds
  28. lastAppendAtNs uint64 //unix time in nanoseconds
  29. lastCompactIndexOffset uint64
  30. lastCompactRevision uint16
  31. isCompacting bool
  32. volumeTierInfo *volume_server_pb.VolumeTierInfo
  33. }
  34. func NewVolume(dirname string, collection string, id needle.VolumeId, needleMapKind NeedleMapType, replicaPlacement *ReplicaPlacement, ttl *needle.TTL, preallocate int64, memoryMapMaxSizeMb uint32) (v *Volume, e error) {
  35. // if replicaPlacement is nil, the superblock will be loaded from disk
  36. v = &Volume{dir: dirname, Collection: collection, Id: id, MemoryMapMaxSizeMb: memoryMapMaxSizeMb}
  37. v.SuperBlock = SuperBlock{ReplicaPlacement: replicaPlacement, Ttl: ttl}
  38. v.needleMapKind = needleMapKind
  39. e = v.load(true, true, needleMapKind, preallocate)
  40. return
  41. }
  42. func (v *Volume) String() string {
  43. return fmt.Sprintf("Id:%v, dir:%s, Collection:%s, dataFile:%v, nm:%v, readOnly:%v", v.Id, v.dir, v.Collection, v.DataBackend, v.nm, v.readOnly)
  44. }
  45. func VolumeFileName(dir string, collection string, id int) (fileName string) {
  46. idString := strconv.Itoa(id)
  47. if collection == "" {
  48. fileName = path.Join(dir, idString)
  49. } else {
  50. fileName = path.Join(dir, collection+"_"+idString)
  51. }
  52. return
  53. }
  54. func (v *Volume) FileName() (fileName string) {
  55. return VolumeFileName(v.dir, v.Collection, int(v.Id))
  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.RLock()
  62. defer v.dataFileAccessLock.RUnlock()
  63. if v.DataBackend == nil {
  64. return
  65. }
  66. datFileSize, modTime, e := v.DataBackend.GetStat()
  67. if e == nil {
  68. return uint64(datFileSize), v.nm.IndexFileSize(), modTime
  69. }
  70. glog.V(0).Infof("Failed to read file size %s %v", v.DataBackend.Name(), e)
  71. return // -1 causes integer overflow and the volume to become unwritable.
  72. }
  73. func (v *Volume) ContentSize() uint64 {
  74. v.dataFileAccessLock.RLock()
  75. defer v.dataFileAccessLock.RUnlock()
  76. if v.nm == nil {
  77. return 0
  78. }
  79. return v.nm.ContentSize()
  80. }
  81. func (v *Volume) DeletedSize() uint64 {
  82. v.dataFileAccessLock.RLock()
  83. defer v.dataFileAccessLock.RUnlock()
  84. if v.nm == nil {
  85. return 0
  86. }
  87. return v.nm.DeletedSize()
  88. }
  89. func (v *Volume) FileCount() uint64 {
  90. v.dataFileAccessLock.RLock()
  91. defer v.dataFileAccessLock.RUnlock()
  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.RLock()
  99. defer v.dataFileAccessLock.RUnlock()
  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.RLock()
  107. defer v.dataFileAccessLock.RUnlock()
  108. if v.nm == nil {
  109. return 0
  110. }
  111. return v.nm.MaxFileKey()
  112. }
  113. func (v *Volume) IndexFileSize() uint64 {
  114. v.dataFileAccessLock.RLock()
  115. defer v.dataFileAccessLock.RUnlock()
  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.DataBackend != nil {
  130. _ = v.DataBackend.Close()
  131. v.DataBackend = 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. volumInfo := &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. volumInfo.RemoteStorageName, volumInfo.RemoteStorageKey = v.RemoteStorageNameKey()
  192. return volumInfo
  193. }
  194. func (v *Volume) RemoteStorageNameKey() (storageName, storageKey string) {
  195. if len(v.volumeTierInfo.GetFiles()) == 0 {
  196. return
  197. }
  198. return v.volumeTierInfo.GetFiles()[0].BackendName(), v.volumeTierInfo.GetFiles()[0].GetKey()
  199. }