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.

337 lines
9.3 KiB

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