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