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.

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