volume.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. ldbTimeout int64
  40. isCompacting bool
  41. isCommitCompacting bool
  42. volumeInfoRWLock sync.RWMutex
  43. volumeInfo *volume_server_pb.VolumeInfo
  44. location *DiskLocation
  45. diskId uint32 // ID of this volume's disk in Store.Locations array
  46. lastIoError error
  47. }
  48. func NewVolume(dirname string, dirIdx string, collection string, id needle.VolumeId, needleMapKind NeedleMapKind, replicaPlacement *super_block.ReplicaPlacement, ttl *needle.TTL, preallocate int64, ver needle.Version, memoryMapMaxSizeMb uint32, ldbTimeout int64) (v *Volume, e error) {
  49. // if replicaPlacement is nil, the superblock will be loaded from disk
  50. v = &Volume{dir: dirname, dirIdx: dirIdx, Collection: collection, Id: id, MemoryMapMaxSizeMb: memoryMapMaxSizeMb,
  51. asyncRequestsChan: make(chan *needle.AsyncRequest, 128)}
  52. v.SuperBlock = super_block.SuperBlock{ReplicaPlacement: replicaPlacement, Ttl: ttl}
  53. v.needleMapKind = needleMapKind
  54. v.ldbTimeout = ldbTimeout
  55. e = v.load(true, true, needleMapKind, preallocate, ver)
  56. v.startWorker()
  57. return
  58. }
  59. func (v *Volume) String() string {
  60. v.noWriteLock.RLock()
  61. defer v.noWriteLock.RUnlock()
  62. 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)
  63. }
  64. func VolumeFileName(dir string, collection string, id int) (fileName string) {
  65. idString := strconv.Itoa(id)
  66. if collection == "" {
  67. fileName = path.Join(dir, idString)
  68. } else {
  69. fileName = path.Join(dir, collection+"_"+idString)
  70. }
  71. return
  72. }
  73. func (v *Volume) DataFileName() (fileName string) {
  74. return VolumeFileName(v.dir, v.Collection, int(v.Id))
  75. }
  76. func (v *Volume) IndexFileName() (fileName string) {
  77. return VolumeFileName(v.dirIdx, v.Collection, int(v.Id))
  78. }
  79. func (v *Volume) FileName(ext string) (fileName string) {
  80. switch ext {
  81. case ".idx", ".cpx", ".ldb", ".cpldb":
  82. return VolumeFileName(v.dirIdx, v.Collection, int(v.Id)) + ext
  83. }
  84. // .dat, .cpd, .vif
  85. return VolumeFileName(v.dir, v.Collection, int(v.Id)) + ext
  86. }
  87. func (v *Volume) Version() needle.Version {
  88. v.superBlockAccessLock.Lock()
  89. defer v.superBlockAccessLock.Unlock()
  90. if v.volumeInfo.Version != 0 {
  91. v.SuperBlock.Version = needle.Version(v.volumeInfo.Version)
  92. }
  93. return v.SuperBlock.Version
  94. }
  95. func (v *Volume) FileStat() (datSize uint64, idxSize uint64, modTime time.Time) {
  96. v.dataFileAccessLock.RLock()
  97. defer v.dataFileAccessLock.RUnlock()
  98. if v.DataBackend == nil {
  99. return
  100. }
  101. datFileSize, modTime, e := v.DataBackend.GetStat()
  102. if e == nil {
  103. return uint64(datFileSize), v.nm.IndexFileSize(), modTime
  104. }
  105. glog.V(0).Infof("Failed to read file size %s %v", v.DataBackend.Name(), e)
  106. return // -1 causes integer overflow and the volume to become unwritable.
  107. }
  108. func (v *Volume) ContentSize() uint64 {
  109. v.dataFileAccessLock.RLock()
  110. defer v.dataFileAccessLock.RUnlock()
  111. if v.nm == nil {
  112. return 0
  113. }
  114. return v.nm.ContentSize()
  115. }
  116. func (v *Volume) doIsEmpty() (bool, error) {
  117. // check v.DataBackend.GetStat()
  118. if v.DataBackend == nil {
  119. return false, fmt.Errorf("v.DataBackend is nil")
  120. } else {
  121. datFileSize, _, e := v.DataBackend.GetStat()
  122. if e != nil {
  123. glog.V(0).Infof("Failed to read file size %s %v", v.DataBackend.Name(), e)
  124. return false, fmt.Errorf("v.DataBackend.GetStat(): %v", e)
  125. }
  126. if datFileSize > super_block.SuperBlockSize {
  127. return false, nil
  128. }
  129. }
  130. // check v.nm.ContentSize()
  131. if v.nm != nil {
  132. if v.nm.ContentSize() > 0 {
  133. return false, nil
  134. }
  135. }
  136. return true, nil
  137. }
  138. func (v *Volume) DeletedSize() uint64 {
  139. v.dataFileAccessLock.RLock()
  140. defer v.dataFileAccessLock.RUnlock()
  141. if v.nm == nil {
  142. return 0
  143. }
  144. return v.nm.DeletedSize()
  145. }
  146. func (v *Volume) FileCount() uint64 {
  147. v.dataFileAccessLock.RLock()
  148. defer v.dataFileAccessLock.RUnlock()
  149. if v.nm == nil {
  150. return 0
  151. }
  152. return uint64(v.nm.FileCount())
  153. }
  154. func (v *Volume) DeletedCount() uint64 {
  155. v.dataFileAccessLock.RLock()
  156. defer v.dataFileAccessLock.RUnlock()
  157. if v.nm == nil {
  158. return 0
  159. }
  160. return uint64(v.nm.DeletedCount())
  161. }
  162. func (v *Volume) MaxFileKey() types.NeedleId {
  163. v.dataFileAccessLock.RLock()
  164. defer v.dataFileAccessLock.RUnlock()
  165. if v.nm == nil {
  166. return 0
  167. }
  168. return v.nm.MaxFileKey()
  169. }
  170. func (v *Volume) IndexFileSize() uint64 {
  171. v.dataFileAccessLock.RLock()
  172. defer v.dataFileAccessLock.RUnlock()
  173. if v.nm == nil {
  174. return 0
  175. }
  176. return v.nm.IndexFileSize()
  177. }
  178. func (v *Volume) DiskType() types.DiskType {
  179. return v.location.DiskType
  180. }
  181. func (v *Volume) SyncToDisk() {
  182. v.dataFileAccessLock.Lock()
  183. defer v.dataFileAccessLock.Unlock()
  184. if v.nm != nil {
  185. if err := v.nm.Sync(); err != nil {
  186. glog.Warningf("Volume Close fail to sync volume idx %d", v.Id)
  187. }
  188. }
  189. if v.DataBackend != nil {
  190. if err := v.DataBackend.Sync(); err != nil {
  191. glog.Warningf("Volume Close fail to sync volume %d", v.Id)
  192. }
  193. }
  194. }
  195. // Close cleanly shuts down this volume
  196. func (v *Volume) Close() {
  197. v.dataFileAccessLock.Lock()
  198. defer v.dataFileAccessLock.Unlock()
  199. v.doClose()
  200. }
  201. func (v *Volume) doClose() {
  202. for v.isCommitCompacting {
  203. time.Sleep(521 * time.Millisecond)
  204. glog.Warningf("Volume Close wait for compaction %d", v.Id)
  205. }
  206. if v.nm != nil {
  207. if err := v.nm.Sync(); err != nil {
  208. glog.Warningf("Volume Close fail to sync volume idx %d", v.Id)
  209. }
  210. v.nm.Close()
  211. v.nm = nil
  212. }
  213. if v.DataBackend != nil {
  214. if err := v.DataBackend.Close(); err != nil {
  215. glog.Warningf("Volume Close fail to sync volume %d", v.Id)
  216. }
  217. v.DataBackend = nil
  218. stats.VolumeServerVolumeGauge.WithLabelValues(v.Collection, "volume").Dec()
  219. }
  220. }
  221. func (v *Volume) NeedToReplicate() bool {
  222. return v.ReplicaPlacement.GetCopyCount() > 1
  223. }
  224. // volume is expired if modified time + volume ttl < now
  225. // except when volume is empty
  226. // or when the volume does not have a ttl
  227. // or when volumeSizeLimit is 0 when server just starts
  228. func (v *Volume) expired(contentSize uint64, volumeSizeLimit uint64) bool {
  229. if volumeSizeLimit == 0 {
  230. // skip if we don't know size limit
  231. return false
  232. }
  233. if contentSize <= super_block.SuperBlockSize {
  234. return false
  235. }
  236. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  237. return false
  238. }
  239. glog.V(2).Infof("volume %d now:%v lastModified:%v", v.Id, time.Now().Unix(), v.lastModifiedTsSeconds)
  240. livedMinutes := (time.Now().Unix() - int64(v.lastModifiedTsSeconds)) / 60
  241. glog.V(2).Infof("volume %d ttl:%v lived:%v", v.Id, v.Ttl, livedMinutes)
  242. if int64(v.Ttl.Minutes()) < livedMinutes {
  243. return true
  244. }
  245. return false
  246. }
  247. // wait either maxDelayMinutes or 10% of ttl minutes
  248. func (v *Volume) expiredLongEnough(maxDelayMinutes uint32) bool {
  249. if v.Ttl == nil || v.Ttl.Minutes() == 0 {
  250. return false
  251. }
  252. removalDelay := v.Ttl.Minutes() / 10
  253. if removalDelay > maxDelayMinutes {
  254. removalDelay = maxDelayMinutes
  255. }
  256. if uint64(v.Ttl.Minutes()+removalDelay)*60+v.lastModifiedTsSeconds < uint64(time.Now().Unix()) {
  257. return true
  258. }
  259. return false
  260. }
  261. func (v *Volume) collectStatus() (maxFileKey types.NeedleId, datFileSize int64, modTime time.Time, fileCount, deletedCount, deletedSize uint64, ok bool) {
  262. v.dataFileAccessLock.RLock()
  263. defer v.dataFileAccessLock.RUnlock()
  264. glog.V(4).Infof("collectStatus volume %d", v.Id)
  265. if v.nm == nil || v.DataBackend == nil {
  266. return
  267. }
  268. ok = true
  269. maxFileKey = v.nm.MaxFileKey()
  270. datFileSize, modTime, _ = v.DataBackend.GetStat()
  271. fileCount = uint64(v.nm.FileCount())
  272. deletedCount = uint64(v.nm.DeletedCount())
  273. deletedSize = v.nm.DeletedSize()
  274. return
  275. }
  276. func (v *Volume) ToVolumeInformationMessage() (types.NeedleId, *master_pb.VolumeInformationMessage) {
  277. maxFileKey, volumeSize, modTime, fileCount, deletedCount, deletedSize, ok := v.collectStatus()
  278. if !ok {
  279. return 0, nil
  280. }
  281. volumeInfo := &master_pb.VolumeInformationMessage{
  282. Id: uint32(v.Id),
  283. Size: uint64(volumeSize),
  284. Collection: v.Collection,
  285. FileCount: fileCount,
  286. DeleteCount: deletedCount,
  287. DeletedByteCount: deletedSize,
  288. ReadOnly: v.IsReadOnly(),
  289. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  290. Version: uint32(v.Version()),
  291. Ttl: v.Ttl.ToUint32(),
  292. CompactRevision: uint32(v.SuperBlock.CompactionRevision),
  293. ModifiedAtSecond: modTime.Unix(),
  294. DiskType: string(v.location.DiskType),
  295. DiskId: v.diskId,
  296. }
  297. volumeInfo.RemoteStorageName, volumeInfo.RemoteStorageKey = v.RemoteStorageNameKey()
  298. return maxFileKey, volumeInfo
  299. }
  300. func (v *Volume) RemoteStorageNameKey() (storageName, storageKey string) {
  301. if v.volumeInfo == nil {
  302. return
  303. }
  304. if len(v.volumeInfo.GetFiles()) == 0 {
  305. return
  306. }
  307. return v.volumeInfo.GetFiles()[0].BackendName(), v.volumeInfo.GetFiles()[0].GetKey()
  308. }
  309. func (v *Volume) IsReadOnly() bool {
  310. v.noWriteLock.RLock()
  311. defer v.noWriteLock.RUnlock()
  312. return v.noWriteOrDelete || v.noWriteCanDelete || v.location.isDiskSpaceLow
  313. }
  314. func (v *Volume) PersistReadOnly(readOnly bool) {
  315. v.volumeInfoRWLock.RLock()
  316. defer v.volumeInfoRWLock.RUnlock()
  317. v.volumeInfo.ReadOnly = readOnly
  318. v.SaveVolumeInfo()
  319. }