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.

284 lines
8.0 KiB

6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
10 years ago
6 years ago
12 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 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/super_block"
  14. "github.com/chrislusf/seaweedfs/weed/storage/types"
  15. "github.com/chrislusf/seaweedfs/weed/glog"
  16. )
  17. type Volume struct {
  18. Id needle.VolumeId
  19. dir string
  20. dirIdx string
  21. Collection string
  22. DataBackend backend.BackendStorageFile
  23. nm NeedleMapper
  24. needleMapKind NeedleMapType
  25. noWriteOrDelete bool // if readonly, either noWriteOrDelete or noWriteCanDelete
  26. noWriteCanDelete bool // if readonly, either noWriteOrDelete or noWriteCanDelete
  27. noWriteLock sync.RWMutex
  28. hasRemoteFile bool // if the volume has a remote file
  29. MemoryMapMaxSizeMb uint32
  30. super_block.SuperBlock
  31. dataFileAccessLock sync.RWMutex
  32. asyncRequestsChan chan *needle.AsyncRequest
  33. lastModifiedTsSeconds uint64 // unix time in seconds
  34. lastAppendAtNs uint64 // unix time in nanoseconds
  35. lastCompactIndexOffset uint64
  36. lastCompactRevision uint16
  37. isCompacting bool
  38. volumeInfo *volume_server_pb.VolumeInfo
  39. location *DiskLocation
  40. }
  41. func NewVolume(dirname string, dirIdx string, collection string, id needle.VolumeId, needleMapKind NeedleMapType, replicaPlacement *super_block.ReplicaPlacement, ttl *needle.TTL, preallocate int64, memoryMapMaxSizeMb uint32) (v *Volume, e error) {
  42. // if replicaPlacement is nil, the superblock will be loaded from disk
  43. v = &Volume{dir: dirname, dirIdx: dirIdx, Collection: collection, Id: id, MemoryMapMaxSizeMb: memoryMapMaxSizeMb,
  44. asyncRequestsChan: make(chan *needle.AsyncRequest, 128)}
  45. v.SuperBlock = super_block.SuperBlock{ReplicaPlacement: replicaPlacement, Ttl: ttl}
  46. v.needleMapKind = needleMapKind
  47. e = v.load(true, true, needleMapKind, preallocate)
  48. v.startWorker()
  49. return
  50. }
  51. func (v *Volume) String() string {
  52. v.noWriteLock.RLock()
  53. defer v.noWriteLock.RUnlock()
  54. return fmt.Sprintf("Id:%v dir:%s dirIdx:%s Collection:%s dataFile:%v nm:%v noWrite:%v canDelete:%v", v.Id, v.dir, v.dirIdx, v.Collection, v.DataBackend, v.nm, v.noWriteOrDelete || v.noWriteCanDelete, v.noWriteCanDelete)
  55. }
  56. func VolumeFileName(dir string, collection string, id int) (fileName string) {
  57. idString := strconv.Itoa(id)
  58. if collection == "" {
  59. fileName = path.Join(dir, idString)
  60. } else {
  61. fileName = path.Join(dir, collection+"_"+idString)
  62. }
  63. return
  64. }
  65. func (v *Volume) DataFileName() (fileName string) {
  66. return VolumeFileName(v.dir, v.Collection, int(v.Id))
  67. }
  68. func (v *Volume) IndexFileName() (fileName string) {
  69. return VolumeFileName(v.dirIdx, v.Collection, int(v.Id))
  70. }
  71. func (v *Volume) FileName(ext string) (fileName string) {
  72. switch ext {
  73. case ".idx", ".cpx", ".ldb":
  74. return VolumeFileName(v.dirIdx, v.Collection, int(v.Id))+ext
  75. }
  76. // .dat, .cpd, .vif
  77. return VolumeFileName(v.dir, v.Collection, int(v.Id))+ext
  78. }
  79. func (v *Volume) Version() needle.Version {
  80. if v.volumeInfo.Version != 0 {
  81. v.SuperBlock.Version = needle.Version(v.volumeInfo.Version)
  82. }
  83. return v.SuperBlock.Version
  84. }
  85. func (v *Volume) FileStat() (datSize uint64, idxSize uint64, modTime time.Time) {
  86. v.dataFileAccessLock.RLock()
  87. defer v.dataFileAccessLock.RUnlock()
  88. if v.DataBackend == nil {
  89. return
  90. }
  91. datFileSize, modTime, e := v.DataBackend.GetStat()
  92. if e == nil {
  93. return uint64(datFileSize), v.nm.IndexFileSize(), modTime
  94. }
  95. glog.V(0).Infof("Failed to read file size %s %v", v.DataBackend.Name(), e)
  96. return // -1 causes integer overflow and the volume to become unwritable.
  97. }
  98. func (v *Volume) ContentSize() uint64 {
  99. v.dataFileAccessLock.RLock()
  100. defer v.dataFileAccessLock.RUnlock()
  101. if v.nm == nil {
  102. return 0
  103. }
  104. return v.nm.ContentSize()
  105. }
  106. func (v *Volume) DeletedSize() uint64 {
  107. v.dataFileAccessLock.RLock()
  108. defer v.dataFileAccessLock.RUnlock()
  109. if v.nm == nil {
  110. return 0
  111. }
  112. return v.nm.DeletedSize()
  113. }
  114. func (v *Volume) FileCount() uint64 {
  115. v.dataFileAccessLock.RLock()
  116. defer v.dataFileAccessLock.RUnlock()
  117. if v.nm == nil {
  118. return 0
  119. }
  120. return uint64(v.nm.FileCount())
  121. }
  122. func (v *Volume) DeletedCount() uint64 {
  123. v.dataFileAccessLock.RLock()
  124. defer v.dataFileAccessLock.RUnlock()
  125. if v.nm == nil {
  126. return 0
  127. }
  128. return uint64(v.nm.DeletedCount())
  129. }
  130. func (v *Volume) MaxFileKey() types.NeedleId {
  131. v.dataFileAccessLock.RLock()
  132. defer v.dataFileAccessLock.RUnlock()
  133. if v.nm == nil {
  134. return 0
  135. }
  136. return v.nm.MaxFileKey()
  137. }
  138. func (v *Volume) IndexFileSize() uint64 {
  139. v.dataFileAccessLock.RLock()
  140. defer v.dataFileAccessLock.RUnlock()
  141. if v.nm == nil {
  142. return 0
  143. }
  144. return v.nm.IndexFileSize()
  145. }
  146. // Close cleanly shuts down this volume
  147. func (v *Volume) Close() {
  148. v.dataFileAccessLock.Lock()
  149. defer v.dataFileAccessLock.Unlock()
  150. if v.nm != nil {
  151. v.nm.Close()
  152. v.nm = nil
  153. }
  154. if v.DataBackend != nil {
  155. _ = v.DataBackend.Close()
  156. v.DataBackend = nil
  157. stats.VolumeServerVolumeCounter.WithLabelValues(v.Collection, "volume").Dec()
  158. }
  159. }
  160. func (v *Volume) NeedToReplicate() bool {
  161. return v.ReplicaPlacement.GetCopyCount() > 1
  162. }
  163. // volume is expired if modified time + volume ttl < now
  164. // except when volume is empty
  165. // or when the volume does not have a ttl
  166. // or when volumeSizeLimit is 0 when server just starts
  167. func (v *Volume) expired(contentSize uint64, volumeSizeLimit uint64) bool {
  168. if volumeSizeLimit == 0 {
  169. // skip if we don't know size limit
  170. return false
  171. }
  172. if contentSize <= super_block.SuperBlockSize {
  173. return false
  174. }
  175. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  176. return false
  177. }
  178. glog.V(2).Infof("now:%v lastModified:%v", time.Now().Unix(), v.lastModifiedTsSeconds)
  179. livedMinutes := (time.Now().Unix() - int64(v.lastModifiedTsSeconds)) / 60
  180. glog.V(2).Infof("ttl:%v lived:%v", v.Ttl, livedMinutes)
  181. if int64(v.Ttl.Minutes()) < livedMinutes {
  182. return true
  183. }
  184. return false
  185. }
  186. // wait either maxDelayMinutes or 10% of ttl minutes
  187. func (v *Volume) expiredLongEnough(maxDelayMinutes uint32) bool {
  188. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  189. return false
  190. }
  191. removalDelay := v.Ttl.Minutes() / 10
  192. if removalDelay > maxDelayMinutes {
  193. removalDelay = maxDelayMinutes
  194. }
  195. if uint64(v.Ttl.Minutes()+removalDelay)*60+v.lastModifiedTsSeconds < uint64(time.Now().Unix()) {
  196. return true
  197. }
  198. return false
  199. }
  200. func (v *Volume) CollectStatus() (maxFileKey types.NeedleId, datFileSize int64, modTime time.Time, fileCount, deletedCount, deletedSize uint64) {
  201. v.dataFileAccessLock.RLock()
  202. defer v.dataFileAccessLock.RUnlock()
  203. glog.V(3).Infof("CollectStatus volume %d", v.Id)
  204. maxFileKey = v.nm.MaxFileKey()
  205. datFileSize, modTime, _ = v.DataBackend.GetStat()
  206. fileCount = uint64(v.nm.FileCount())
  207. deletedCount = uint64(v.nm.DeletedCount())
  208. deletedSize = v.nm.DeletedSize()
  209. fileCount = uint64(v.nm.FileCount())
  210. return
  211. }
  212. func (v *Volume) ToVolumeInformationMessage() (types.NeedleId, *master_pb.VolumeInformationMessage) {
  213. maxFileKey, volumeSize, modTime, fileCount, deletedCount, deletedSize := v.CollectStatus()
  214. volumeInfo := &master_pb.VolumeInformationMessage{
  215. Id: uint32(v.Id),
  216. Size: uint64(volumeSize),
  217. Collection: v.Collection,
  218. FileCount: fileCount,
  219. DeleteCount: deletedCount,
  220. DeletedByteCount: deletedSize,
  221. ReadOnly: v.IsReadOnly(),
  222. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  223. Version: uint32(v.Version()),
  224. Ttl: v.Ttl.ToUint32(),
  225. CompactRevision: uint32(v.SuperBlock.CompactionRevision),
  226. ModifiedAtSecond: modTime.Unix(),
  227. }
  228. volumeInfo.RemoteStorageName, volumeInfo.RemoteStorageKey = v.RemoteStorageNameKey()
  229. return maxFileKey, volumeInfo
  230. }
  231. func (v *Volume) RemoteStorageNameKey() (storageName, storageKey string) {
  232. if v.volumeInfo == nil {
  233. return
  234. }
  235. if len(v.volumeInfo.GetFiles()) == 0 {
  236. return
  237. }
  238. return v.volumeInfo.GetFiles()[0].BackendName(), v.volumeInfo.GetFiles()[0].GetKey()
  239. }
  240. func (v *Volume) IsReadOnly() bool {
  241. v.noWriteLock.RLock()
  242. defer v.noWriteLock.RUnlock()
  243. return v.noWriteOrDelete || v.noWriteCanDelete || v.location.isDiskSpaceLow
  244. }