MCPcopy
hub / github.com/dgraph-io/badger / doRunGC

Method doRunGC

value.go:1688–1822  ·  view source on GitHub ↗
(lf *logFile, discardRatio float64, tr trace.Trace)

Source from the content-addressed store, hash-verified

1686}
1687
1688func (vlog *valueLog) doRunGC(lf *logFile, discardRatio float64, tr trace.Trace) (err error) {
1689 // Update stats before exiting
1690 defer func() {
1691 if err == nil {
1692 vlog.lfDiscardStats.Lock()
1693 delete(vlog.lfDiscardStats.m, lf.fid)
1694 vlog.lfDiscardStats.Unlock()
1695 }
1696 }()
1697
1698 type reason struct {
1699 total float64
1700 discard float64
1701 count int
1702 }
1703
1704 fi, err := lf.fd.Stat()
1705 if err != nil {
1706 tr.LazyPrintf("Error while finding file size: %v", err)
1707 tr.SetError()
1708 return err
1709 }
1710
1711 // Set up the sampling window sizes.
1712 sizeWindow := float64(fi.Size()) * 0.1 // 10% of the file as window.
1713 sizeWindowM := sizeWindow / (1 << 20) // in MBs.
1714 countWindow := int(float64(vlog.opt.ValueLogMaxEntries) * 0.01) // 1% of num entries.
1715 tr.LazyPrintf("Size window: %5.2f. Count window: %d.", sizeWindow, countWindow)
1716
1717 // Pick a random start point for the log.
1718 skipFirstM := float64(rand.Int63n(fi.Size())) // Pick a random starting location.
1719 skipFirstM -= sizeWindow // Avoid hitting EOF by moving back by window.
1720 skipFirstM /= float64(mi) // Convert to MBs.
1721 tr.LazyPrintf("Skip first %5.2f MB of file of size: %d MB", skipFirstM, fi.Size()/mi)
1722 var skipped float64
1723
1724 var r reason
1725 start := time.Now()
1726 y.AssertTrue(vlog.db != nil)
1727 s := new(y.Slice)
1728 var numIterations int
1729 _, err = vlog.iterate(lf, 0, func(e Entry, vp valuePointer) error {
1730 numIterations++
1731 esz := float64(vp.Len) / (1 << 20) // in MBs.
1732 if skipped < skipFirstM {
1733 skipped += esz
1734 return nil
1735 }
1736
1737 // Sample until we reach the window sizes or exceed 10 seconds.
1738 if r.count > countWindow {
1739 tr.LazyPrintf("Stopping sampling after %d entries.", countWindow)
1740 return errStop
1741 }
1742 if r.total > sizeWindowM {
1743 tr.LazyPrintf("Stopping sampling after reaching window size.")
1744 return errStop
1745 }

Callers 1

runGCMethod · 0.95

Calls 14

iterateMethod · 0.95
readValueBytesMethod · 0.95
getUnlockCallbackMethod · 0.95
rewriteMethod · 0.95
AssertTrueFunction · 0.92
discardEntryFunction · 0.85
runCallbackFunction · 0.85
decodeEntryMethod · 0.80
printMethod · 0.80
DebugfMethod · 0.65
ErrorfMethod · 0.65
SizeMethod · 0.45

Tested by

no test coverage detected