volume.go 6.8 KB

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