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.

214 lines
5.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
10 years ago
6 years ago
12 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "github.com/joeslay/seaweedfs/weed/pb/master_pb"
  5. "github.com/joeslay/seaweedfs/weed/stats"
  6. "github.com/joeslay/seaweedfs/weed/storage/needle"
  7. "github.com/joeslay/seaweedfs/weed/storage/types"
  8. "os"
  9. "path"
  10. "strconv"
  11. "sync"
  12. "time"
  13. "github.com/joeslay/seaweedfs/weed/glog"
  14. )
  15. type Volume struct {
  16. Id needle.VolumeId
  17. dir string
  18. Collection string
  19. dataFile *os.File
  20. nm NeedleMapper
  21. needleMapKind NeedleMapType
  22. readOnly bool
  23. MemoryMapMaxSizeMB uint32
  24. SuperBlock
  25. dataFileAccessLock sync.Mutex
  26. lastModifiedTsSeconds uint64 //unix time in seconds
  27. lastAppendAtNs uint64 //unix time in nanoseconds
  28. lastCompactIndexOffset uint64
  29. lastCompactRevision uint16
  30. isCompacting bool
  31. }
  32. func NewVolume(dirname string, collection string, id needle.VolumeId, needleMapKind NeedleMapType, replicaPlacement *ReplicaPlacement, ttl *needle.TTL, preallocate int64, memoryMapMaxSizeMB uint32) (v *Volume, e error) {
  33. // if replicaPlacement is nil, the superblock will be loaded from disk
  34. v = &Volume{dir: dirname, Collection: collection, Id: id, MemoryMapMaxSizeMB: memoryMapMaxSizeMB}
  35. v.SuperBlock = SuperBlock{ReplicaPlacement: replicaPlacement, Ttl: ttl}
  36. v.needleMapKind = needleMapKind
  37. e = v.load(true, true, needleMapKind, preallocate)
  38. return
  39. }
  40. func (v *Volume) String() string {
  41. return fmt.Sprintf("Id:%v, dir:%s, Collection:%s, dataFile:%v, nm:%v, readOnly:%v", v.Id, v.dir, v.Collection, v.dataFile, v.nm, v.readOnly)
  42. }
  43. func VolumeFileName(dir string, collection string, id int) (fileName string) {
  44. idString := strconv.Itoa(id)
  45. if collection == "" {
  46. fileName = path.Join(dir, idString)
  47. } else {
  48. fileName = path.Join(dir, collection+"_"+idString)
  49. }
  50. return
  51. }
  52. func (v *Volume) FileName() (fileName string) {
  53. return VolumeFileName(v.dir, v.Collection, int(v.Id))
  54. }
  55. func (v *Volume) DataFile() *os.File {
  56. return v.dataFile
  57. }
  58. func (v *Volume) Version() needle.Version {
  59. return v.SuperBlock.Version()
  60. }
  61. func (v *Volume) FileStat() (datSize uint64, idxSize uint64, modTime time.Time) {
  62. v.dataFileAccessLock.Lock()
  63. defer v.dataFileAccessLock.Unlock()
  64. if v.dataFile == nil {
  65. return
  66. }
  67. stat, e := v.dataFile.Stat()
  68. if e == nil {
  69. return uint64(stat.Size()), v.nm.IndexFileSize(), stat.ModTime()
  70. }
  71. glog.V(0).Infof("Failed to read file size %s %v", v.dataFile.Name(), e)
  72. return // -1 causes integer overflow and the volume to become unwritable.
  73. }
  74. func (v *Volume) ContentSize() uint64 {
  75. v.dataFileAccessLock.Lock()
  76. defer v.dataFileAccessLock.Unlock()
  77. return v.nm.ContentSize()
  78. }
  79. func (v *Volume) DeletedSize() uint64 {
  80. v.dataFileAccessLock.Lock()
  81. defer v.dataFileAccessLock.Unlock()
  82. return v.nm.DeletedSize()
  83. }
  84. func (v *Volume) FileCount() uint64 {
  85. v.dataFileAccessLock.Lock()
  86. defer v.dataFileAccessLock.Unlock()
  87. return uint64(v.nm.FileCount())
  88. }
  89. func (v *Volume) DeletedCount() uint64 {
  90. v.dataFileAccessLock.Lock()
  91. defer v.dataFileAccessLock.Unlock()
  92. return uint64(v.nm.DeletedCount())
  93. }
  94. func (v *Volume) MaxFileKey() types.NeedleId {
  95. v.dataFileAccessLock.Lock()
  96. defer v.dataFileAccessLock.Unlock()
  97. return v.nm.MaxFileKey()
  98. }
  99. func (v *Volume) IndexFileSize() uint64 {
  100. v.dataFileAccessLock.Lock()
  101. defer v.dataFileAccessLock.Unlock()
  102. return v.nm.IndexFileSize()
  103. }
  104. func (v *Volume) IndexFileContent() ([]byte, error) {
  105. v.dataFileAccessLock.Lock()
  106. defer v.dataFileAccessLock.Unlock()
  107. return v.nm.IndexFileContent()
  108. }
  109. func (v *Volume) IndexFileName() string {
  110. v.dataFileAccessLock.Lock()
  111. defer v.dataFileAccessLock.Unlock()
  112. return v.nm.IndexFileName()
  113. }
  114. // Close cleanly shuts down this volume
  115. func (v *Volume) Close() {
  116. v.dataFileAccessLock.Lock()
  117. defer v.dataFileAccessLock.Unlock()
  118. if v.nm != nil {
  119. v.nm.Close()
  120. v.nm = nil
  121. }
  122. if v.dataFile != nil {
  123. _ = v.dataFile.Close()
  124. v.dataFile = nil
  125. stats.VolumeServerVolumeCounter.WithLabelValues(v.Collection, "volume").Dec()
  126. }
  127. }
  128. func (v *Volume) NeedToReplicate() bool {
  129. return v.ReplicaPlacement.GetCopyCount() > 1
  130. }
  131. // volume is expired if modified time + volume ttl < now
  132. // except when volume is empty
  133. // or when the volume does not have a ttl
  134. // or when volumeSizeLimit is 0 when server just starts
  135. func (v *Volume) expired(volumeSizeLimit uint64) bool {
  136. if volumeSizeLimit == 0 {
  137. //skip if we don't know size limit
  138. return false
  139. }
  140. if v.ContentSize() == 0 {
  141. return false
  142. }
  143. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  144. return false
  145. }
  146. glog.V(1).Infof("now:%v lastModified:%v", time.Now().Unix(), v.lastModifiedTsSeconds)
  147. livedMinutes := (time.Now().Unix() - int64(v.lastModifiedTsSeconds)) / 60
  148. glog.V(1).Infof("ttl:%v lived:%v", v.Ttl, livedMinutes)
  149. if int64(v.Ttl.Minutes()) < livedMinutes {
  150. return true
  151. }
  152. return false
  153. }
  154. // wait either maxDelayMinutes or 10% of ttl minutes
  155. func (v *Volume) expiredLongEnough(maxDelayMinutes uint32) bool {
  156. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  157. return false
  158. }
  159. removalDelay := v.Ttl.Minutes() / 10
  160. if removalDelay > maxDelayMinutes {
  161. removalDelay = maxDelayMinutes
  162. }
  163. if uint64(v.Ttl.Minutes()+removalDelay)*60+v.lastModifiedTsSeconds < uint64(time.Now().Unix()) {
  164. return true
  165. }
  166. return false
  167. }
  168. func (v *Volume) ToVolumeInformationMessage() *master_pb.VolumeInformationMessage {
  169. size, _, modTime := v.FileStat()
  170. return &master_pb.VolumeInformationMessage{
  171. Id: uint32(v.Id),
  172. Size: size,
  173. Collection: v.Collection,
  174. FileCount: uint64(v.FileCount()),
  175. DeleteCount: uint64(v.DeletedCount()),
  176. DeletedByteCount: v.DeletedSize(),
  177. ReadOnly: v.readOnly,
  178. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  179. Version: uint32(v.Version()),
  180. Ttl: v.Ttl.ToUint32(),
  181. CompactRevision: uint32(v.SuperBlock.CompactionRevision),
  182. ModifiedAtSecond: modTime.Unix(),
  183. }
  184. }