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.

362 lines
11 KiB

7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
10 years ago
6 years ago
6 years ago
6 years ago
6 years ago
8 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. func (v *Volume) garbageLevel() float64 {
  11. if v.ContentSize() == 0 {
  12. return 0
  13. }
  14. return float64(v.nm.DeletedSize()) / float64(v.ContentSize())
  15. }
  16. func (v *Volume) Compact(preallocate int64) error {
  17. glog.V(3).Infof("Compacting volume %d ...", v.Id)
  18. //no need to lock for copy on write
  19. //v.accessLock.Lock()
  20. //defer v.accessLock.Unlock()
  21. //glog.V(3).Infof("Got Compaction lock...")
  22. filePath := v.FileName()
  23. v.lastCompactIndexOffset = v.nm.IndexFileSize()
  24. v.lastCompactRevision = v.SuperBlock.CompactRevision
  25. glog.V(3).Infof("creating copies for volume %d ,last offset %d...", v.Id, v.lastCompactIndexOffset)
  26. return v.copyDataAndGenerateIndexFile(filePath+".cpd", filePath+".cpx", preallocate)
  27. }
  28. func (v *Volume) Compact2() error {
  29. glog.V(3).Infof("Compact2 volume %d ...", v.Id)
  30. filePath := v.FileName()
  31. glog.V(3).Infof("creating copies for volume %d ...", v.Id)
  32. return v.copyDataBasedOnIndexFile(filePath+".cpd", filePath+".cpx")
  33. }
  34. func (v *Volume) CommitCompact() error {
  35. glog.V(0).Infof("Committing volume %d vacuuming...", v.Id)
  36. v.dataFileAccessLock.Lock()
  37. defer v.dataFileAccessLock.Unlock()
  38. glog.V(3).Infof("Got volume %d committing lock...", v.Id)
  39. v.compactingWg.Add(1)
  40. defer v.compactingWg.Done()
  41. v.nm.Close()
  42. if err := v.dataFile.Close(); err != nil {
  43. glog.V(0).Infof("fail to close volume %d", v.Id)
  44. }
  45. v.dataFile = nil
  46. var e error
  47. if e = v.makeupDiff(v.FileName()+".cpd", v.FileName()+".cpx", v.FileName()+".dat", v.FileName()+".idx"); e != nil {
  48. glog.V(0).Infof("makeupDiff in CommitCompact volume %d failed %v", v.Id, e)
  49. e = os.Remove(v.FileName() + ".cpd")
  50. if e != nil {
  51. return e
  52. }
  53. e = os.Remove(v.FileName() + ".cpx")
  54. if e != nil {
  55. return e
  56. }
  57. } else {
  58. var e error
  59. if e = os.Rename(v.FileName()+".cpd", v.FileName()+".dat"); e != nil {
  60. return fmt.Errorf("rename %s: %v", v.FileName()+".cpd", e)
  61. }
  62. if e = os.Rename(v.FileName()+".cpx", v.FileName()+".idx"); e != nil {
  63. return fmt.Errorf("rename %s: %v", v.FileName()+".cpx", e)
  64. }
  65. }
  66. //glog.V(3).Infof("Pretending to be vacuuming...")
  67. //time.Sleep(20 * time.Second)
  68. os.RemoveAll(v.FileName() + ".ldb")
  69. os.RemoveAll(v.FileName() + ".bdb")
  70. glog.V(3).Infof("Loading volume %d commit file...", v.Id)
  71. if e = v.load(true, false, v.needleMapKind, 0); e != nil {
  72. return e
  73. }
  74. return nil
  75. }
  76. func (v *Volume) cleanupCompact() error {
  77. glog.V(0).Infof("Cleaning up volume %d vacuuming...", v.Id)
  78. e1 := os.Remove(v.FileName() + ".cpd")
  79. e2 := os.Remove(v.FileName() + ".cpx")
  80. if e1 != nil {
  81. return e1
  82. }
  83. if e2 != nil {
  84. return e2
  85. }
  86. return nil
  87. }
  88. func fetchCompactRevisionFromDatFile(file *os.File) (compactRevision uint16, err error) {
  89. superBlock, err := ReadSuperBlock(file)
  90. if err != nil {
  91. return 0, err
  92. }
  93. return superBlock.CompactRevision, nil
  94. }
  95. func (v *Volume) makeupDiff(newDatFileName, newIdxFileName, oldDatFileName, oldIdxFileName string) (err error) {
  96. var indexSize int64
  97. oldIdxFile, err := os.Open(oldIdxFileName)
  98. defer oldIdxFile.Close()
  99. oldDatFile, err := os.Open(oldDatFileName)
  100. defer oldDatFile.Close()
  101. if indexSize, err = verifyIndexFileIntegrity(oldIdxFile); err != nil {
  102. return fmt.Errorf("verifyIndexFileIntegrity %s failed: %v", oldIdxFileName, err)
  103. }
  104. if indexSize == 0 || uint64(indexSize) <= v.lastCompactIndexOffset {
  105. return nil
  106. }
  107. oldDatCompactRevision, err := fetchCompactRevisionFromDatFile(oldDatFile)
  108. if err != nil {
  109. return fmt.Errorf("fetchCompactRevisionFromDatFile src %s failed: %v", oldDatFile.Name(), err)
  110. }
  111. if oldDatCompactRevision != v.lastCompactRevision {
  112. return fmt.Errorf("current old dat file's compact revision %d is not the expected one %d", oldDatCompactRevision, v.lastCompactRevision)
  113. }
  114. type keyField struct {
  115. offset Offset
  116. size uint32
  117. }
  118. incrementedHasUpdatedIndexEntry := make(map[NeedleId]keyField)
  119. for idxOffset := indexSize - NeedleEntrySize; uint64(idxOffset) >= v.lastCompactIndexOffset; idxOffset -= NeedleEntrySize {
  120. var IdxEntry []byte
  121. if IdxEntry, err = readIndexEntryAtOffset(oldIdxFile, idxOffset); err != nil {
  122. return fmt.Errorf("readIndexEntry %s at offset %d failed: %v", oldIdxFileName, idxOffset, err)
  123. }
  124. key, offset, size := IdxFileEntry(IdxEntry)
  125. glog.V(4).Infof("key %d offset %d size %d", key, offset, size)
  126. if _, found := incrementedHasUpdatedIndexEntry[key]; !found {
  127. incrementedHasUpdatedIndexEntry[key] = keyField{
  128. offset: offset,
  129. size: size,
  130. }
  131. }
  132. }
  133. // no updates during commit step
  134. if len(incrementedHasUpdatedIndexEntry) == 0 {
  135. return nil
  136. }
  137. // deal with updates during commit step
  138. var (
  139. dst, idx *os.File
  140. )
  141. if dst, err = os.OpenFile(newDatFileName, os.O_RDWR, 0644); err != nil {
  142. return fmt.Errorf("open dat file %s failed: %v", newDatFileName, err)
  143. }
  144. defer dst.Close()
  145. if idx, err = os.OpenFile(newIdxFileName, os.O_RDWR, 0644); err != nil {
  146. return fmt.Errorf("open idx file %s failed: %v", newIdxFileName, err)
  147. }
  148. defer idx.Close()
  149. var newDatCompactRevision uint16
  150. newDatCompactRevision, err = fetchCompactRevisionFromDatFile(dst)
  151. if err != nil {
  152. return fmt.Errorf("fetchCompactRevisionFromDatFile dst %s failed: %v", dst.Name(), err)
  153. }
  154. if oldDatCompactRevision+1 != newDatCompactRevision {
  155. return fmt.Errorf("oldDatFile %s 's compact revision is %d while newDatFile %s 's compact revision is %d", oldDatFileName, oldDatCompactRevision, newDatFileName, newDatCompactRevision)
  156. }
  157. idxEntryBytes := make([]byte, NeedleIdSize+OffsetSize+SizeSize)
  158. for key, increIdxEntry := range incrementedHasUpdatedIndexEntry {
  159. NeedleIdToBytes(idxEntryBytes[0:NeedleIdSize], key)
  160. OffsetToBytes(idxEntryBytes[NeedleIdSize:NeedleIdSize+OffsetSize], increIdxEntry.offset)
  161. util.Uint32toBytes(idxEntryBytes[NeedleIdSize+OffsetSize:NeedleIdSize+OffsetSize+SizeSize], increIdxEntry.size)
  162. var offset int64
  163. if offset, err = dst.Seek(0, 2); err != nil {
  164. glog.V(0).Infof("failed to seek the end of file: %v", err)
  165. return
  166. }
  167. //ensure file writing starting from aligned positions
  168. if offset%NeedlePaddingSize != 0 {
  169. offset = offset + (NeedlePaddingSize - offset%NeedlePaddingSize)
  170. if offset, err = v.dataFile.Seek(offset, 0); err != nil {
  171. glog.V(0).Infof("failed to align in datafile %s: %v", v.dataFile.Name(), err)
  172. return
  173. }
  174. }
  175. //updated needle
  176. if !increIdxEntry.offset.IsZero() && increIdxEntry.size != 0 && increIdxEntry.size != TombstoneFileSize {
  177. //even the needle cache in memory is hit, the need_bytes is correct
  178. glog.V(4).Infof("file %d offset %d size %d", key, increIdxEntry.offset.ToAcutalOffset(), increIdxEntry.size)
  179. var needleBytes []byte
  180. needleBytes, err = ReadNeedleBlob(oldDatFile, increIdxEntry.offset.ToAcutalOffset(), increIdxEntry.size, v.Version())
  181. if err != nil {
  182. return fmt.Errorf("ReadNeedleBlob %s key %d offset %d size %d failed: %v", oldDatFile.Name(), key, increIdxEntry.offset.ToAcutalOffset(), increIdxEntry.size, err)
  183. }
  184. dst.Write(needleBytes)
  185. util.Uint32toBytes(idxEntryBytes[8:12], uint32(offset/NeedlePaddingSize))
  186. } else { //deleted needle
  187. //fakeDelNeedle 's default Data field is nil
  188. fakeDelNeedle := new(Needle)
  189. fakeDelNeedle.Id = key
  190. fakeDelNeedle.Cookie = 0x12345678
  191. fakeDelNeedle.AppendAtNs = uint64(time.Now().UnixNano())
  192. _, _, _, err = fakeDelNeedle.Append(dst, v.Version())
  193. if err != nil {
  194. return fmt.Errorf("append deleted %d failed: %v", key, err)
  195. }
  196. util.Uint32toBytes(idxEntryBytes[8:12], uint32(0))
  197. }
  198. if _, err := idx.Seek(0, 2); err != nil {
  199. return fmt.Errorf("cannot seek end of indexfile %s: %v",
  200. newIdxFileName, err)
  201. }
  202. _, err = idx.Write(idxEntryBytes)
  203. }
  204. return nil
  205. }
  206. type VolumeFileScanner4Vacuum struct {
  207. version Version
  208. v *Volume
  209. dst *os.File
  210. nm *NeedleMap
  211. newOffset int64
  212. now uint64
  213. }
  214. func (scanner *VolumeFileScanner4Vacuum) VisitSuperBlock(superBlock SuperBlock) error {
  215. scanner.version = superBlock.Version()
  216. superBlock.CompactRevision++
  217. _, err := scanner.dst.Write(superBlock.Bytes())
  218. scanner.newOffset = int64(superBlock.BlockSize())
  219. return err
  220. }
  221. func (scanner *VolumeFileScanner4Vacuum) ReadNeedleBody() bool {
  222. return true
  223. }
  224. func (scanner *VolumeFileScanner4Vacuum) VisitNeedle(n *Needle, offset int64) error {
  225. if n.HasTtl() && scanner.now >= n.LastModified+uint64(scanner.v.Ttl.Minutes()*60) {
  226. return nil
  227. }
  228. nv, ok := scanner.v.nm.Get(n.Id)
  229. glog.V(4).Infoln("needle expected offset ", offset, "ok", ok, "nv", nv)
  230. if ok && nv.Offset.ToAcutalOffset() == offset && nv.Size > 0 && nv.Size != TombstoneFileSize {
  231. if err := scanner.nm.Put(n.Id, ToOffset(scanner.newOffset), n.Size); err != nil {
  232. return fmt.Errorf("cannot put needle: %s", err)
  233. }
  234. if _, _, _, err := n.Append(scanner.dst, scanner.v.Version()); err != nil {
  235. return fmt.Errorf("cannot append needle: %s", err)
  236. }
  237. scanner.newOffset += n.DiskSize(scanner.version)
  238. glog.V(4).Infoln("saving key", n.Id, "volume offset", offset, "=>", scanner.newOffset, "data_size", n.Size)
  239. }
  240. return nil
  241. }
  242. func (v *Volume) copyDataAndGenerateIndexFile(dstName, idxName string, preallocate int64) (err error) {
  243. var (
  244. dst, idx *os.File
  245. )
  246. if dst, err = createVolumeFile(dstName, preallocate); err != nil {
  247. return
  248. }
  249. defer dst.Close()
  250. if idx, err = os.OpenFile(idxName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
  251. return
  252. }
  253. defer idx.Close()
  254. scanner := &VolumeFileScanner4Vacuum{
  255. v: v,
  256. now: uint64(time.Now().Unix()),
  257. nm: NewBtreeNeedleMap(idx),
  258. dst: dst,
  259. }
  260. err = ScanVolumeFile(v.dir, v.Collection, v.Id, v.needleMapKind, scanner)
  261. return
  262. }
  263. func (v *Volume) copyDataBasedOnIndexFile(dstName, idxName string) (err error) {
  264. var (
  265. dst, idx, oldIndexFile *os.File
  266. )
  267. if dst, err = os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
  268. return
  269. }
  270. defer dst.Close()
  271. if idx, err = os.OpenFile(idxName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
  272. return
  273. }
  274. defer idx.Close()
  275. if oldIndexFile, err = os.OpenFile(v.FileName()+".idx", os.O_RDONLY, 0644); err != nil {
  276. return
  277. }
  278. defer oldIndexFile.Close()
  279. nm := NewBtreeNeedleMap(idx)
  280. now := uint64(time.Now().Unix())
  281. v.SuperBlock.CompactRevision++
  282. dst.Write(v.SuperBlock.Bytes())
  283. newOffset := int64(v.SuperBlock.BlockSize())
  284. WalkIndexFile(oldIndexFile, func(key NeedleId, offset Offset, size uint32) error {
  285. if offset.IsZero() || size == TombstoneFileSize {
  286. return nil
  287. }
  288. nv, ok := v.nm.Get(key)
  289. if !ok {
  290. return nil
  291. }
  292. n := new(Needle)
  293. err := n.ReadData(v.dataFile, offset.ToAcutalOffset(), size, v.Version())
  294. if err != nil {
  295. return nil
  296. }
  297. if n.HasTtl() && now >= n.LastModified+uint64(v.Ttl.Minutes()*60) {
  298. return nil
  299. }
  300. glog.V(4).Infoln("needle expected offset ", offset, "ok", ok, "nv", nv)
  301. if nv.Offset == offset && nv.Size > 0 {
  302. if err = nm.Put(n.Id, ToOffset(newOffset), n.Size); err != nil {
  303. return fmt.Errorf("cannot put needle: %s", err)
  304. }
  305. if _, _, _, err = n.Append(dst, v.Version()); err != nil {
  306. return fmt.Errorf("cannot append needle: %s", err)
  307. }
  308. newOffset += n.DiskSize(v.Version())
  309. glog.V(3).Infoln("saving key", n.Id, "volume offset", offset, "=>", newOffset, "data_size", n.Size)
  310. }
  311. return nil
  312. })
  313. return
  314. }