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.

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