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.

254 lines
7.0 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/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. lowDiskSpace bool
  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. }
  39. func (v *Volume) SetLowDiskSpace(lowDiskSpace bool) {
  40. glog.V(0).Infof("SetLowDiskSpace id %d value %t", v.Id, lowDiskSpace)
  41. v.lowDiskSpace = lowDiskSpace
  42. }
  43. 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) {
  44. // if replicaPlacement is nil, the superblock will be loaded from disk
  45. v = &Volume{dir: dirname, Collection: collection, Id: id, MemoryMapMaxSizeMb: memoryMapMaxSizeMb,
  46. asyncRequestsChan: make(chan *needle.AsyncRequest, 128)}
  47. v.SuperBlock = super_block.SuperBlock{ReplicaPlacement: replicaPlacement, Ttl: ttl}
  48. v.needleMapKind = needleMapKind
  49. e = v.load(true, true, needleMapKind, preallocate)
  50. v.startWorker()
  51. return
  52. }
  53. func (v *Volume) String() string {
  54. 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)
  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) FileName() (fileName string) {
  66. return VolumeFileName(v.dir, v.Collection, int(v.Id))
  67. }
  68. func (v *Volume) Version() needle.Version {
  69. if v.volumeInfo.Version != 0 {
  70. v.SuperBlock.Version = needle.Version(v.volumeInfo.Version)
  71. }
  72. return v.SuperBlock.Version
  73. }
  74. func (v *Volume) FileStat() (datSize uint64, idxSize uint64, modTime time.Time) {
  75. v.dataFileAccessLock.RLock()
  76. defer v.dataFileAccessLock.RUnlock()
  77. if v.DataBackend == nil {
  78. return
  79. }
  80. datFileSize, modTime, e := v.DataBackend.GetStat()
  81. if e == nil {
  82. return uint64(datFileSize), v.nm.IndexFileSize(), modTime
  83. }
  84. glog.V(0).Infof("Failed to read file size %s %v", v.DataBackend.Name(), e)
  85. return // -1 causes integer overflow and the volume to become unwritable.
  86. }
  87. func (v *Volume) ContentSize() uint64 {
  88. v.dataFileAccessLock.RLock()
  89. defer v.dataFileAccessLock.RUnlock()
  90. if v.nm == nil {
  91. return 0
  92. }
  93. return v.nm.ContentSize()
  94. }
  95. func (v *Volume) DeletedSize() uint64 {
  96. v.dataFileAccessLock.RLock()
  97. defer v.dataFileAccessLock.RUnlock()
  98. if v.nm == nil {
  99. return 0
  100. }
  101. return v.nm.DeletedSize()
  102. }
  103. func (v *Volume) FileCount() uint64 {
  104. v.dataFileAccessLock.RLock()
  105. defer v.dataFileAccessLock.RUnlock()
  106. if v.nm == nil {
  107. return 0
  108. }
  109. return uint64(v.nm.FileCount())
  110. }
  111. func (v *Volume) DeletedCount() uint64 {
  112. v.dataFileAccessLock.RLock()
  113. defer v.dataFileAccessLock.RUnlock()
  114. if v.nm == nil {
  115. return 0
  116. }
  117. return uint64(v.nm.DeletedCount())
  118. }
  119. func (v *Volume) MaxFileKey() types.NeedleId {
  120. v.dataFileAccessLock.RLock()
  121. defer v.dataFileAccessLock.RUnlock()
  122. if v.nm == nil {
  123. return 0
  124. }
  125. return v.nm.MaxFileKey()
  126. }
  127. func (v *Volume) IndexFileSize() uint64 {
  128. v.dataFileAccessLock.RLock()
  129. defer v.dataFileAccessLock.RUnlock()
  130. if v.nm == nil {
  131. return 0
  132. }
  133. return v.nm.IndexFileSize()
  134. }
  135. // Close cleanly shuts down this volume
  136. func (v *Volume) Close() {
  137. v.dataFileAccessLock.Lock()
  138. defer v.dataFileAccessLock.Unlock()
  139. if v.nm != nil {
  140. v.nm.Close()
  141. v.nm = nil
  142. }
  143. if v.DataBackend != nil {
  144. _ = v.DataBackend.Close()
  145. v.DataBackend = nil
  146. stats.VolumeServerVolumeCounter.WithLabelValues(v.Collection, "volume").Dec()
  147. }
  148. }
  149. func (v *Volume) NeedToReplicate() bool {
  150. return v.ReplicaPlacement.GetCopyCount() > 1
  151. }
  152. // volume is expired if modified time + volume ttl < now
  153. // except when volume is empty
  154. // or when the volume does not have a ttl
  155. // or when volumeSizeLimit is 0 when server just starts
  156. func (v *Volume) expired(volumeSizeLimit uint64) bool {
  157. if volumeSizeLimit == 0 {
  158. //skip if we don't know size limit
  159. return false
  160. }
  161. if v.ContentSize() == 0 {
  162. return false
  163. }
  164. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  165. return false
  166. }
  167. glog.V(2).Infof("now:%v lastModified:%v", time.Now().Unix(), v.lastModifiedTsSeconds)
  168. livedMinutes := (time.Now().Unix() - int64(v.lastModifiedTsSeconds)) / 60
  169. glog.V(2).Infof("ttl:%v lived:%v", v.Ttl, livedMinutes)
  170. if int64(v.Ttl.Minutes()) < livedMinutes {
  171. return true
  172. }
  173. return false
  174. }
  175. // wait either maxDelayMinutes or 10% of ttl minutes
  176. func (v *Volume) expiredLongEnough(maxDelayMinutes uint32) bool {
  177. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  178. return false
  179. }
  180. removalDelay := v.Ttl.Minutes() / 10
  181. if removalDelay > maxDelayMinutes {
  182. removalDelay = maxDelayMinutes
  183. }
  184. if uint64(v.Ttl.Minutes()+removalDelay)*60+v.lastModifiedTsSeconds < uint64(time.Now().Unix()) {
  185. return true
  186. }
  187. return false
  188. }
  189. func (v *Volume) ToVolumeInformationMessage() *master_pb.VolumeInformationMessage {
  190. size, _, modTime := v.FileStat()
  191. volumInfo := &master_pb.VolumeInformationMessage{
  192. Id: uint32(v.Id),
  193. Size: size,
  194. Collection: v.Collection,
  195. FileCount: v.FileCount(),
  196. DeleteCount: v.DeletedCount(),
  197. DeletedByteCount: v.DeletedSize(),
  198. ReadOnly: v.IsReadOnly(),
  199. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  200. Version: uint32(v.Version()),
  201. Ttl: v.Ttl.ToUint32(),
  202. CompactRevision: uint32(v.SuperBlock.CompactionRevision),
  203. ModifiedAtSecond: modTime.Unix(),
  204. }
  205. volumInfo.RemoteStorageName, volumInfo.RemoteStorageKey = v.RemoteStorageNameKey()
  206. return volumInfo
  207. }
  208. func (v *Volume) RemoteStorageNameKey() (storageName, storageKey string) {
  209. if v.volumeInfo == nil {
  210. return
  211. }
  212. if len(v.volumeInfo.GetFiles()) == 0 {
  213. return
  214. }
  215. return v.volumeInfo.GetFiles()[0].BackendName(), v.volumeInfo.GetFiles()[0].GetKey()
  216. }
  217. func (v *Volume) IsReadOnly() bool {
  218. return v.noWriteOrDelete || v.noWriteCanDelete || v.lowDiskSpace
  219. }