Skip to content

Commit dc2d326

Browse files
committed
db: add counts for local tables
This patch adds counts for local [live/zombie/obsolete] tables.
1 parent ae4d7e2 commit dc2d326

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2049,7 +2049,7 @@ func (d *DB) Metrics() *Metrics {
20492049
}
20502050
metrics.Table.ZombieCount = int64(d.mu.versions.zombieTables.Count())
20512051
metrics.Table.ZombieSize = d.mu.versions.zombieTables.TotalSize()
2052-
metrics.Table.Local.ZombieSize = d.mu.versions.zombieTables.LocalSize()
2052+
metrics.Table.Local.ZombieCount, metrics.Table.Local.ZombieSize = d.mu.versions.zombieTables.LocalStats()
20532053
metrics.private.optionsFileSize = d.optionsFileSize
20542054

20552055
// TODO(jackson): Consider making these metrics optional.

metrics.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,16 @@ type Metrics struct {
299299
Local struct {
300300
// LiveSize is the number of bytes in live tables.
301301
LiveSize uint64
302+
// LiveCount is the number of live tables.
303+
LiveCount uint64
302304
// ObsoleteSize is the number of bytes in obsolete tables.
303305
ObsoleteSize uint64
306+
// ObsoleteCount is the number of obsolete tables.
307+
ObsoleteCount uint64
304308
// ZombieSize is the number of bytes in zombie tables.
305309
ZombieSize uint64
310+
// ZombieCount is the number of zombie tables.
311+
ZombieCount uint64
306312
}
307313
}
308314

obsolete_files.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ func (d *DB) onObsoleteTableDelete(fileSize uint64, isLocal bool) {
332332
d.mu.versions.metrics.Table.ObsoleteSize -= fileSize
333333
if isLocal {
334334
d.mu.versions.metrics.Table.Local.ObsoleteSize -= fileSize
335+
d.mu.versions.metrics.Table.Local.ObsoleteCount--
335336
}
336337
d.mu.Unlock()
337338
}
@@ -620,9 +621,10 @@ func makeZombieObjects() zombieObjects {
620621
// iterator. Such objects are 'dead,' but cannot be deleted until iterators that
621622
// may access them are closed.
622623
type zombieObjects struct {
623-
objs map[base.DiskFileNum]objectInfo
624-
totalSize uint64
625-
localSize uint64
624+
objs map[base.DiskFileNum]objectInfo
625+
totalSize uint64
626+
localSize uint64
627+
localCount uint64
626628
}
627629

628630
// Add adds an object to the set of zombie objects.
@@ -634,6 +636,7 @@ func (z *zombieObjects) Add(obj objectInfo) {
634636
z.totalSize += obj.FileSize
635637
if obj.isLocal {
636638
z.localSize += obj.FileSize
639+
z.localCount++
637640
}
638641
}
639642

@@ -674,6 +677,7 @@ func (z *zombieObjects) Extract(fileNum base.DiskFileNum) objectInfo {
674677
z.totalSize -= obj.FileSize
675678
if obj.isLocal {
676679
z.localSize -= obj.FileSize
680+
z.localCount--
677681
}
678682
return obj
679683
}
@@ -683,9 +687,9 @@ func (z *zombieObjects) TotalSize() uint64 {
683687
return z.totalSize
684688
}
685689

686-
// LocalSize returns the size of all local objects in the set.
687-
func (z *zombieObjects) LocalSize() uint64 {
688-
return z.localSize
690+
// LocalStats returns the count and size of all local objects in the set.
691+
func (z *zombieObjects) LocalStats() (count uint64, size uint64) {
692+
return z.localCount, z.localSize
689693
}
690694

691695
func mergeObjectInfos(a, b []objectInfo) []objectInfo {

version_set.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,20 @@ func (vs *versionSet) load(
353353
for _, l := range newVersion.Levels {
354354
for f := range l.All() {
355355
if !f.Virtual {
356-
_, localSize := sizeIfLocal(f.FileBacking, vs.provider)
356+
isLocal, localSize := sizeIfLocal(f.FileBacking, vs.provider)
357357
vs.metrics.Table.Local.LiveSize = uint64(int64(vs.metrics.Table.Local.LiveSize) + localSize)
358+
if isLocal {
359+
vs.metrics.Table.Local.LiveCount++
360+
}
358361
}
359362
}
360363
}
361364
vs.virtualBackings.ForEach(func(backing *fileBacking) {
362-
_, localSize := sizeIfLocal(backing, vs.provider)
365+
isLocal, localSize := sizeIfLocal(backing, vs.provider)
363366
vs.metrics.Table.Local.LiveSize = uint64(int64(vs.metrics.Table.Local.LiveSize) + localSize)
367+
if isLocal {
368+
vs.metrics.Table.Local.LiveCount++
369+
}
364370
})
365371

366372
vs.setCompactionPicker(
@@ -569,7 +575,7 @@ func (vs *versionSet) UpdateVersionLocked(updateFn func() (versionUpdate, error)
569575
nextFileNum := vs.nextFileNum.Load()
570576

571577
// Note: this call populates ve.RemovedBackingTables.
572-
zombieBackings, removedVirtualBackings, localLiveSizeDelta :=
578+
zombieBackings, removedVirtualBackings, localLiveSizeDelta, localLiveCountDelta :=
573579
getZombiesAndUpdateVirtualBackings(ve, &vs.virtualBackings, vs.provider)
574580

575581
var l0Update manifest.L0PreparedUpdate
@@ -734,6 +740,7 @@ func (vs *versionSet) UpdateVersionLocked(updateFn func() (versionUpdate, error)
734740
}
735741
vs.metrics.Levels[0].Sublevels = int32(len(newVersion.L0SublevelFiles))
736742
vs.metrics.Table.Local.LiveSize = uint64(int64(vs.metrics.Table.Local.LiveSize) + localLiveSizeDelta)
743+
vs.metrics.Table.Local.LiveCount = uint64(int64(vs.metrics.Table.Local.LiveCount) + localLiveCountDelta)
737744

738745
vs.setCompactionPicker(
739746
newCompactionPickerByScore(newVersion, vs.l0Organizer, &vs.virtualBackings, vs.opts, inProgress))
@@ -764,7 +771,10 @@ type fileBackingInfo struct {
764771
// - localLiveSizeDelta: the delta in local live bytes.
765772
func getZombiesAndUpdateVirtualBackings(
766773
ve *versionEdit, virtualBackings *manifest.VirtualBackings, provider objstorage.Provider,
767-
) (zombieBackings, removedVirtualBackings []fileBackingInfo, localLiveSizeDelta int64) {
774+
) (
775+
zombieBackings, removedVirtualBackings []fileBackingInfo,
776+
localLiveSizeDelta, localLiveCountDelta int64,
777+
) {
768778
// First, deal with the physical tables.
769779
//
770780
// A physical backing has become unused if it is in DeletedFiles but not in
@@ -776,8 +786,11 @@ func getZombiesAndUpdateVirtualBackings(
776786
for _, nf := range ve.NewTables {
777787
if !nf.Meta.Virtual {
778788
stillUsed[nf.Meta.FileBacking.DiskFileNum] = struct{}{}
779-
_, localFileDelta := sizeIfLocal(nf.Meta.FileBacking, provider)
789+
isLocal, localFileDelta := sizeIfLocal(nf.Meta.FileBacking, provider)
780790
localLiveSizeDelta += localFileDelta
791+
if isLocal {
792+
localLiveCountDelta++
793+
}
781794
}
782795
}
783796
for _, b := range ve.CreatedBackingTables {
@@ -792,6 +805,9 @@ func getZombiesAndUpdateVirtualBackings(
792805
// for the addition.
793806
isLocal, localFileDelta := sizeIfLocal(m.FileBacking, provider)
794807
localLiveSizeDelta -= localFileDelta
808+
if isLocal {
809+
localLiveCountDelta--
810+
}
795811
if _, ok := stillUsed[m.FileBacking.DiskFileNum]; !ok {
796812
zombieBackings = append(zombieBackings, fileBackingInfo{
797813
backing: m.FileBacking,
@@ -807,8 +823,11 @@ func getZombiesAndUpdateVirtualBackings(
807823
// which works out.
808824
for _, b := range ve.CreatedBackingTables {
809825
virtualBackings.AddAndRef(b)
810-
_, localFileDelta := sizeIfLocal(b, provider)
826+
isLocal, localFileDelta := sizeIfLocal(b, provider)
811827
localLiveSizeDelta += localFileDelta
828+
if isLocal {
829+
localLiveCountDelta++
830+
}
812831
}
813832
for _, nf := range ve.NewTables {
814833
if nf.Meta.Virtual {
@@ -828,6 +847,9 @@ func getZombiesAndUpdateVirtualBackings(
828847
for i, b := range unused {
829848
isLocal, localFileDelta := sizeIfLocal(b, provider)
830849
localLiveSizeDelta -= localFileDelta
850+
if isLocal {
851+
localLiveCountDelta--
852+
}
831853
ve.RemovedBackingTables[i] = b.DiskFileNum
832854
zombieBackings = append(zombieBackings, fileBackingInfo{
833855
backing: b,
@@ -837,7 +859,7 @@ func getZombiesAndUpdateVirtualBackings(
837859
}
838860
removedVirtualBackings = zombieBackings[len(zombieBackings)-len(unused):]
839861
}
840-
return zombieBackings, removedVirtualBackings, localLiveSizeDelta
862+
return zombieBackings, removedVirtualBackings, localLiveSizeDelta, localLiveCountDelta
841863
}
842864

843865
// sizeIfLocal returns backing.Size if the backing is a local file, else 0.
@@ -1090,10 +1112,12 @@ func (vs *versionSet) updateObsoleteTableMetricsLocked() {
10901112
vs.metrics.Table.ObsoleteCount = int64(len(vs.obsoleteTables))
10911113
vs.metrics.Table.ObsoleteSize = 0
10921114
vs.metrics.Table.Local.ObsoleteSize = 0
1115+
vs.metrics.Table.Local.ObsoleteCount = 0
10931116
for _, fi := range vs.obsoleteTables {
10941117
vs.metrics.Table.ObsoleteSize += fi.FileSize
10951118
if fi.isLocal {
10961119
vs.metrics.Table.Local.ObsoleteSize += fi.FileSize
1120+
vs.metrics.Table.Local.ObsoleteCount++
10971121
}
10981122
}
10991123
}

0 commit comments

Comments
 (0)