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.

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