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.

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