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.

213 lines
5.6 KiB

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