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.

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