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

Function newLevelsController

levels.go:75–204  ·  view source on GitHub ↗
(db *DB, mf *Manifest)

Source from the content-addressed store, hash-verified

73}
74
75func newLevelsController(db *DB, mf *Manifest) (*levelsController, error) {
76 y.AssertTrue(db.opt.NumLevelZeroTablesStall > db.opt.NumLevelZeroTables)
77 s := &levelsController{
78 kv: db,
79 levels: make([]*levelHandler, db.opt.MaxLevels),
80 }
81 s.cstatus.levels = make([]*levelCompactStatus, db.opt.MaxLevels)
82
83 for i := 0; i < db.opt.MaxLevels; i++ {
84 s.levels[i] = newLevelHandler(db, i)
85 switch i {
86 case 0:
87 // Do nothing.
88 case 1:
89 // Level 1 probably shouldn't be too much bigger than level 0.
90 s.levels[i].maxTotalSize = db.opt.LevelOneSize
91 default:
92 s.levels[i].maxTotalSize = s.levels[i-1].maxTotalSize * int64(db.opt.LevelSizeMultiplier)
93 }
94 s.cstatus.levels[i] = new(levelCompactStatus)
95 }
96
97 if db.opt.InMemory {
98 return s, nil
99 }
100 // Compare manifest against directory, check for existent/non-existent files, and remove.
101 if err := revertToManifest(db, mf, getIDMap(db.opt.Dir)); err != nil {
102 return nil, err
103 }
104
105 // Some files may be deleted. Let's reload.
106 var flags uint32 = y.Sync
107 if db.opt.ReadOnly {
108 flags |= y.ReadOnly
109 }
110
111 var mu sync.Mutex
112 tables := make([][]*table.Table, db.opt.MaxLevels)
113 var maxFileID uint64
114
115 // We found that using 3 goroutines allows disk throughput to be utilized to its max.
116 // Disk utilization is the main thing we should focus on, while trying to read the data. That's
117 // the one factor that remains constant between HDD and SSD.
118 throttle := y.NewThrottle(3)
119
120 start := time.Now()
121 var numOpened int32
122 tick := time.NewTicker(3 * time.Second)
123 defer tick.Stop()
124
125 for fileID, tf := range mf.Tables {
126 fname := table.NewFilename(fileID, db.opt.Dir)
127 select {
128 case <-tick.C:
129 db.opt.Infof("%d tables out of %d opened in %s\n", atomic.LoadInt32(&numOpened),
130 len(mf.Tables), time.Since(start).Round(time.Millisecond))
131 default:
132 }

Callers 2

OpenFunction · 0.85

Calls 15

DoMethod · 0.95
DoneMethod · 0.95
FinishMethod · 0.95
validateMethod · 0.95
cleanupLevelsMethod · 0.95
closeMethod · 0.95
AssertTrueFunction · 0.92
NewThrottleFunction · 0.92
NewFilenameFunction · 0.92
OpenExistingFileFunction · 0.92
OpenTableFunction · 0.92
newLevelHandlerFunction · 0.85

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…