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.

332 lines
9.2 KiB

6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
10 years ago
6 years ago
12 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "path"
  5. "strconv"
  6. "sync"
  7. "time"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  10. "github.com/seaweedfs/seaweedfs/weed/stats"
  11. "github.com/seaweedfs/seaweedfs/weed/storage/backend"
  12. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  13. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  14. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  15. "github.com/seaweedfs/seaweedfs/weed/glog"
  16. )
  17. type Volume struct {
  18. Id needle.VolumeId
  19. dir string
  20. dirIdx string
  21. Collection string
  22. DataBackend backend.BackendStorageFile
  23. nm NeedleMapper
  24. tmpNm TempNeedleMapper
  25. needleMapKind NeedleMapKind
  26. noWriteOrDelete bool // if readonly, either noWriteOrDelete or noWriteCanDelete
  27. noWriteCanDelete bool // if readonly, either noWriteOrDelete or noWriteCanDelete
  28. noWriteLock sync.RWMutex
  29. hasRemoteFile bool // if the volume has a remote file
  30. MemoryMapMaxSizeMb uint32
  31. super_block.SuperBlock
  32. dataFileAccessLock sync.RWMutex
  33. superBlockAccessLock sync.Mutex
  34. asyncRequestsChan chan *needle.AsyncRequest
  35. lastModifiedTsSeconds uint64 // unix time in seconds
  36. lastAppendAtNs uint64 // unix time in nanoseconds
  37. lastCompactIndexOffset uint64
  38. lastCompactRevision uint16
  39. isCompacting bool
  40. isCommitCompacting bool
  41. volumeInfo *volume_server_pb.VolumeInfo
  42. location *DiskLocation
  43. lastIoError error
  44. }
  45. func NewVolume(dirname string, dirIdx string, collection string, id needle.VolumeId, needleMapKind NeedleMapKind, replicaPlacement *super_block.ReplicaPlacement, ttl *needle.TTL, preallocate int64, memoryMapMaxSizeMb uint32) (v *Volume, e error) {
  46. // if replicaPlacement is nil, the superblock will be loaded from disk
  47. v = &Volume{dir: dirname, dirIdx: dirIdx, Collection: collection, Id: id, MemoryMapMaxSizeMb: memoryMapMaxSizeMb,
  48. asyncRequestsChan: make(chan *needle.AsyncRequest, 128)}
  49. v.SuperBlock = super_block.SuperBlock{ReplicaPlacement: replicaPlacement, Ttl: ttl}
  50. v.needleMapKind = needleMapKind
  51. e = v.load(true, true, needleMapKind, preallocate)
  52. v.startWorker()
  53. return
  54. }
  55. func (v *Volume) String() string {
  56. v.noWriteLock.RLock()
  57. defer v.noWriteLock.RUnlock()
  58. return fmt.Sprintf("Id:%v dir:%s dirIdx:%s Collection:%s dataFile:%v nm:%v noWrite:%v canDelete:%v", v.Id, v.dir, v.dirIdx, v.Collection, v.DataBackend, v.nm, v.noWriteOrDelete || v.noWriteCanDelete, v.noWriteCanDelete)
  59. }
  60. func VolumeFileName(dir string, collection string, id int) (fileName string) {
  61. idString := strconv.Itoa(id)
  62. if collection == "" {
  63. fileName = path.Join(dir, idString)
  64. } else {
  65. fileName = path.Join(dir, collection+"_"+idString)
  66. }
  67. return
  68. }
  69. func (v *Volume) DataFileName() (fileName string) {
  70. return VolumeFileName(v.dir, v.Collection, int(v.Id))
  71. }
  72. func (v *Volume) IndexFileName() (fileName string) {
  73. return VolumeFileName(v.dirIdx, v.Collection, int(v.Id))
  74. }
  75. func (v *Volume) FileName(ext string) (fileName string) {
  76. switch ext {
  77. case ".idx", ".cpx", ".ldb":
  78. return VolumeFileName(v.dirIdx, v.Collection, int(v.Id)) + ext
  79. }
  80. // .dat, .cpd, .vif
  81. return VolumeFileName(v.dir, v.Collection, int(v.Id)) + ext
  82. }
  83. func (v *Volume) Version() needle.Version {
  84. v.superBlockAccessLock.Lock()
  85. defer v.superBlockAccessLock.Unlock()
  86. if v.volumeInfo.Version != 0 {
  87. v.SuperBlock.Version = needle.Version(v.volumeInfo.Version)
  88. }
  89. return v.SuperBlock.Version
  90. }
  91. func (v *Volume) FileStat() (datSize uint64, idxSize uint64, modTime time.Time) {
  92. v.dataFileAccessLock.RLock()
  93. defer v.dataFileAccessLock.RUnlock()
  94. if v.DataBackend == nil {
  95. return
  96. }
  97. datFileSize, modTime, e := v.DataBackend.GetStat()
  98. if e == nil {
  99. return uint64(datFileSize), v.nm.IndexFileSize(), modTime
  100. }
  101. glog.V(0).Infof("Failed to read file size %s %v", v.DataBackend.Name(), e)
  102. return // -1 causes integer overflow and the volume to become unwritable.
  103. }
  104. func (v *Volume) ContentSize() uint64 {
  105. v.dataFileAccessLock.RLock()
  106. defer v.dataFileAccessLock.RUnlock()
  107. if v.nm == nil {
  108. return 0
  109. }
  110. return v.nm.ContentSize()
  111. }
  112. func (v *Volume) DeletedSize() uint64 {
  113. v.dataFileAccessLock.RLock()
  114. defer v.dataFileAccessLock.RUnlock()
  115. if v.nm == nil {
  116. return 0
  117. }
  118. return v.nm.DeletedSize()
  119. }
  120. func (v *Volume) FileCount() uint64 {
  121. v.dataFileAccessLock.RLock()
  122. defer v.dataFileAccessLock.RUnlock()
  123. if v.nm == nil {
  124. return 0
  125. }
  126. return uint64(v.nm.FileCount())
  127. }
  128. func (v *Volume) DeletedCount() uint64 {
  129. v.dataFileAccessLock.RLock()
  130. defer v.dataFileAccessLock.RUnlock()
  131. if v.nm == nil {
  132. return 0
  133. }
  134. return uint64(v.nm.DeletedCount())
  135. }
  136. func (v *Volume) MaxFileKey() types.NeedleId {
  137. v.dataFileAccessLock.RLock()
  138. defer v.dataFileAccessLock.RUnlock()
  139. if v.nm == nil {
  140. return 0
  141. }
  142. return v.nm.MaxFileKey()
  143. }
  144. func (v *Volume) IndexFileSize() uint64 {
  145. v.dataFileAccessLock.RLock()
  146. defer v.dataFileAccessLock.RUnlock()
  147. if v.nm == nil {
  148. return 0
  149. }
  150. return v.nm.IndexFileSize()
  151. }
  152. func (v *Volume) DiskType() types.DiskType {
  153. return v.location.DiskType
  154. }
  155. func (v *Volume) SyncToDisk() {
  156. v.dataFileAccessLock.Lock()
  157. defer v.dataFileAccessLock.Unlock()
  158. if v.nm != nil {
  159. if err := v.nm.Sync(); err != nil {
  160. glog.Warningf("Volume Close fail to sync volume idx %d", v.Id)
  161. }
  162. }
  163. if v.DataBackend != nil {
  164. if err := v.DataBackend.Sync(); err != nil {
  165. glog.Warningf("Volume Close fail to sync volume %d", v.Id)
  166. }
  167. }
  168. }
  169. // Close cleanly shuts down this volume
  170. func (v *Volume) Close() {
  171. v.dataFileAccessLock.Lock()
  172. defer v.dataFileAccessLock.Unlock()
  173. for v.isCommitCompacting {
  174. time.Sleep(521 * time.Millisecond)
  175. glog.Warningf("Volume Close wait for compaction %d", v.Id)
  176. }
  177. if v.nm != nil {
  178. if err := v.nm.Sync(); err != nil {
  179. glog.Warningf("Volume Close fail to sync volume idx %d", v.Id)
  180. }
  181. v.nm.Close()
  182. v.nm = nil
  183. }
  184. if v.DataBackend != nil {
  185. if err := v.DataBackend.Close(); err != nil {
  186. glog.Warningf("Volume Close fail to sync volume %d", v.Id)
  187. }
  188. v.DataBackend = nil
  189. stats.VolumeServerVolumeCounter.WithLabelValues(v.Collection, "volume").Dec()
  190. }
  191. }
  192. func (v *Volume) NeedToReplicate() bool {
  193. return v.ReplicaPlacement.GetCopyCount() > 1
  194. }
  195. // volume is expired if modified time + volume ttl < now
  196. // except when volume is empty
  197. // or when the volume does not have a ttl
  198. // or when volumeSizeLimit is 0 when server just starts
  199. func (v *Volume) expired(contentSize uint64, volumeSizeLimit uint64) bool {
  200. if volumeSizeLimit == 0 {
  201. // skip if we don't know size limit
  202. return false
  203. }
  204. if contentSize <= super_block.SuperBlockSize {
  205. return false
  206. }
  207. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  208. return false
  209. }
  210. glog.V(2).Infof("volume %d now:%v lastModified:%v", v.Id, time.Now().Unix(), v.lastModifiedTsSeconds)
  211. livedMinutes := (time.Now().Unix() - int64(v.lastModifiedTsSeconds)) / 60
  212. glog.V(2).Infof("volume %d ttl:%v lived:%v", v.Id, v.Ttl, livedMinutes)
  213. if int64(v.Ttl.Minutes()) < livedMinutes {
  214. return true
  215. }
  216. return false
  217. }
  218. // wait either maxDelayMinutes or 10% of ttl minutes
  219. func (v *Volume) expiredLongEnough(maxDelayMinutes uint32) bool {
  220. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  221. return false
  222. }
  223. removalDelay := v.Ttl.Minutes() / 10
  224. if removalDelay > maxDelayMinutes {
  225. removalDelay = maxDelayMinutes
  226. }
  227. if uint64(v.Ttl.Minutes()+removalDelay)*60+v.lastModifiedTsSeconds < uint64(time.Now().Unix()) {
  228. return true
  229. }
  230. return false
  231. }
  232. func (v *Volume) collectStatus() (maxFileKey types.NeedleId, datFileSize int64, modTime time.Time, fileCount, deletedCount, deletedSize uint64, ok bool) {
  233. v.dataFileAccessLock.RLock()
  234. defer v.dataFileAccessLock.RUnlock()
  235. glog.V(4).Infof("collectStatus volume %d", v.Id)
  236. if v.nm == nil || v.DataBackend == nil {
  237. return
  238. }
  239. ok = true
  240. maxFileKey = v.nm.MaxFileKey()
  241. datFileSize, modTime, _ = v.DataBackend.GetStat()
  242. fileCount = uint64(v.nm.FileCount())
  243. deletedCount = uint64(v.nm.DeletedCount())
  244. deletedSize = v.nm.DeletedSize()
  245. fileCount = uint64(v.nm.FileCount())
  246. return
  247. }
  248. func (v *Volume) ToVolumeInformationMessage() (types.NeedleId, *master_pb.VolumeInformationMessage) {
  249. maxFileKey, volumeSize, modTime, fileCount, deletedCount, deletedSize, ok := v.collectStatus()
  250. if !ok {
  251. return 0, nil
  252. }
  253. volumeInfo := &master_pb.VolumeInformationMessage{
  254. Id: uint32(v.Id),
  255. Size: uint64(volumeSize),
  256. Collection: v.Collection,
  257. FileCount: fileCount,
  258. DeleteCount: deletedCount,
  259. DeletedByteCount: deletedSize,
  260. ReadOnly: v.IsReadOnly(),
  261. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  262. Version: uint32(v.Version()),
  263. Ttl: v.Ttl.ToUint32(),
  264. CompactRevision: uint32(v.SuperBlock.CompactionRevision),
  265. ModifiedAtSecond: modTime.Unix(),
  266. DiskType: string(v.location.DiskType),
  267. }
  268. volumeInfo.RemoteStorageName, volumeInfo.RemoteStorageKey = v.RemoteStorageNameKey()
  269. return maxFileKey, volumeInfo
  270. }
  271. func (v *Volume) RemoteStorageNameKey() (storageName, storageKey string) {
  272. if v.volumeInfo == nil {
  273. return
  274. }
  275. if len(v.volumeInfo.GetFiles()) == 0 {
  276. return
  277. }
  278. return v.volumeInfo.GetFiles()[0].BackendName(), v.volumeInfo.GetFiles()[0].GetKey()
  279. }
  280. func (v *Volume) IsReadOnly() bool {
  281. v.noWriteLock.RLock()
  282. defer v.noWriteLock.RUnlock()
  283. return v.noWriteOrDelete || v.noWriteCanDelete || v.location.isDiskSpaceLow
  284. }