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.

344 lines
11 KiB

7 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 idx_offset := indexSize - NeedleEntrySize; uint64(idx_offset) >= v.lastCompactIndexOffset; idx_offset -= NeedleEntrySize {
  120. var IdxEntry []byte
  121. if IdxEntry, err = readIndexEntryAtOffset(oldIdxFile, idx_offset); err != nil {
  122. return fmt.Errorf("readIndexEntry %s at offset %d failed: %v", oldIdxFileName, idx_offset, 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. idx_entry_bytes := make([]byte, NeedleIdSize+OffsetSize+SizeSize)
  158. for key, incre_idx_entry := range incrementedHasUpdatedIndexEntry {
  159. NeedleIdToBytes(idx_entry_bytes[0:NeedleIdSize], key)
  160. OffsetToBytes(idx_entry_bytes[NeedleIdSize:NeedleIdSize+OffsetSize], incre_idx_entry.offset)
  161. util.Uint32toBytes(idx_entry_bytes[NeedleIdSize+OffsetSize:NeedleIdSize+OffsetSize+SizeSize], incre_idx_entry.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 incre_idx_entry.offset != 0 && incre_idx_entry.size != 0 && incre_idx_entry.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, int64(incre_idx_entry.offset)*NeedlePaddingSize, incre_idx_entry.size)
  179. var needle_bytes []byte
  180. needle_bytes, err = ReadNeedleBlob(oldDatFile, int64(incre_idx_entry.offset)*NeedlePaddingSize, incre_idx_entry.size, v.Version())
  181. if err != nil {
  182. return fmt.Errorf("ReadNeedleBlob %s key %d offset %d size %d failed: %v", oldDatFile.Name(), key, int64(incre_idx_entry.offset)*NeedlePaddingSize, incre_idx_entry.size, err)
  183. }
  184. dst.Write(needle_bytes)
  185. util.Uint32toBytes(idx_entry_bytes[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(idx_entry_bytes[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(idx_entry_bytes)
  203. }
  204. return nil
  205. }
  206. func (v *Volume) copyDataAndGenerateIndexFile(dstName, idxName string, preallocate int64) (err error) {
  207. var (
  208. dst, idx *os.File
  209. )
  210. if dst, err = createVolumeFile(dstName, preallocate); err != nil {
  211. return
  212. }
  213. defer dst.Close()
  214. if idx, err = os.OpenFile(idxName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
  215. return
  216. }
  217. defer idx.Close()
  218. nm := NewBtreeNeedleMap(idx)
  219. new_offset := int64(0)
  220. now := uint64(time.Now().Unix())
  221. var version Version
  222. err = ScanVolumeFile(v.dir, v.Collection, v.Id, v.needleMapKind,
  223. func(superBlock SuperBlock) error {
  224. version = superBlock.Version()
  225. superBlock.CompactRevision++
  226. _, err = dst.Write(superBlock.Bytes())
  227. new_offset = int64(superBlock.BlockSize())
  228. return err
  229. }, true, func(n *Needle, offset int64) error {
  230. if n.HasTtl() && now >= n.LastModified+uint64(v.Ttl.Minutes()*60) {
  231. return nil
  232. }
  233. nv, ok := v.nm.Get(n.Id)
  234. glog.V(4).Infoln("needle expected offset ", offset, "ok", ok, "nv", nv)
  235. if ok && int64(nv.Offset)*NeedlePaddingSize == offset && nv.Size > 0 {
  236. if err = nm.Put(n.Id, Offset(new_offset/NeedlePaddingSize), n.Size); err != nil {
  237. return fmt.Errorf("cannot put needle: %s", err)
  238. }
  239. if _, _, _, err := n.Append(dst, v.Version()); err != nil {
  240. return fmt.Errorf("cannot append needle: %s", err)
  241. }
  242. new_offset += n.DiskSize(version)
  243. glog.V(4).Infoln("saving key", n.Id, "volume offset", offset, "=>", new_offset, "data_size", n.Size)
  244. }
  245. return nil
  246. })
  247. return
  248. }
  249. func (v *Volume) copyDataBasedOnIndexFile(dstName, idxName string) (err error) {
  250. var (
  251. dst, idx, oldIndexFile *os.File
  252. )
  253. if dst, err = os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
  254. return
  255. }
  256. defer dst.Close()
  257. if idx, err = os.OpenFile(idxName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
  258. return
  259. }
  260. defer idx.Close()
  261. if oldIndexFile, err = os.OpenFile(v.FileName()+".idx", os.O_RDONLY, 0644); err != nil {
  262. return
  263. }
  264. defer oldIndexFile.Close()
  265. nm := NewBtreeNeedleMap(idx)
  266. now := uint64(time.Now().Unix())
  267. v.SuperBlock.CompactRevision++
  268. dst.Write(v.SuperBlock.Bytes())
  269. new_offset := int64(v.SuperBlock.BlockSize())
  270. WalkIndexFile(oldIndexFile, func(key NeedleId, offset Offset, size uint32) error {
  271. if offset == 0 || size == TombstoneFileSize {
  272. return nil
  273. }
  274. nv, ok := v.nm.Get(key)
  275. if !ok {
  276. return nil
  277. }
  278. n := new(Needle)
  279. n.ReadData(v.dataFile, int64(offset)*NeedlePaddingSize, size, v.Version())
  280. if n.HasTtl() && now >= n.LastModified+uint64(v.Ttl.Minutes()*60) {
  281. return nil
  282. }
  283. glog.V(4).Infoln("needle expected offset ", offset, "ok", ok, "nv", nv)
  284. if nv.Offset == offset && nv.Size > 0 {
  285. if err = nm.Put(n.Id, Offset(new_offset/NeedlePaddingSize), n.Size); err != nil {
  286. return fmt.Errorf("cannot put needle: %s", err)
  287. }
  288. if _, _, _, err = n.Append(dst, v.Version()); err != nil {
  289. return fmt.Errorf("cannot append needle: %s", err)
  290. }
  291. new_offset += n.DiskSize(v.Version())
  292. glog.V(3).Infoln("saving key", n.Id, "volume offset", offset, "=>", new_offset, "data_size", n.Size)
  293. }
  294. return nil
  295. })
  296. return
  297. }