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.

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