-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bbolt abstraction for statistic tracking #1659
Draft
zackattack01
wants to merge
4
commits into
main
Choose a base branch
from
zack/bbolt_cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8419858
wire types.StorageStatTracker into knapsack, update direct bboltdb uses
zackattack01 67a7dc0
regenerate knapsack mocks
zackattack01 8551be7
clean up remaining bbolt refs, fix up tests
zackattack01 9558911
remove unused dbdump from agent
zackattack01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package agentbbolt | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log/slog" | ||
|
||
"go.etcd.io/bbolt" | ||
) | ||
|
||
type bucketStatsHolder struct { | ||
Stats bbolt.BucketStats | ||
FillPercent float64 | ||
NumberOfKeys int | ||
Size int | ||
} | ||
|
||
type dbStatsHolder struct { | ||
Stats bbolt.TxStats | ||
Size int64 | ||
} | ||
|
||
type Stats struct { | ||
DB dbStatsHolder | ||
Buckets map[string]bucketStatsHolder | ||
} | ||
|
||
type bboltStatStore struct { | ||
slogger *slog.Logger | ||
db *bbolt.DB | ||
} | ||
|
||
// NewStatStore provides a wrapper around a bbolt.DB connection. This is done at the global (above bucket) level | ||
// and should be used for any operations regarding the collection of storage statistics | ||
func NewStatStore(slogger *slog.Logger, db *bbolt.DB) (*bboltStatStore, error) { | ||
if db == nil { | ||
return nil, NoDbError{} | ||
} | ||
|
||
s := &bboltStatStore{ | ||
slogger: slogger, | ||
db: db, | ||
} | ||
|
||
return s, nil | ||
} | ||
|
||
func (s *bboltStatStore) SizeBytes() (int64, error) { | ||
if s == nil || s.db == nil { | ||
return 0, NoDbError{} | ||
} | ||
|
||
var size int64 | ||
|
||
if err := s.db.View(func(tx *bbolt.Tx) error { | ||
size = tx.Size() | ||
return nil | ||
}); err != nil { | ||
return 0, fmt.Errorf("creating view tx to check size stat: %w", err) | ||
} | ||
|
||
return size, nil | ||
} | ||
|
||
// GetStats returns a json blob containing both global and bucket-level | ||
// statistics. Note that the bucketName set does not impact the output, all buckets | ||
// will be traversed for stats regardless | ||
func (s *bboltStatStore) GetStats() ([]byte, error) { | ||
if s == nil || s.db == nil { | ||
return nil, NoDbError{} | ||
} | ||
|
||
stats := &Stats{ | ||
Buckets: make(map[string]bucketStatsHolder), | ||
} | ||
|
||
if err := s.db.View(func(tx *bbolt.Tx) error { | ||
stats.DB.Stats = tx.Stats() | ||
stats.DB.Size = tx.Size() | ||
|
||
if err := tx.ForEach(bucketStatsFunc(stats)); err != nil { | ||
return fmt.Errorf("dumping bucket: %w", err) | ||
} | ||
return nil | ||
}); err != nil { | ||
return nil, fmt.Errorf("creating view tx: %w", err) | ||
} | ||
|
||
statsJson, err := json.Marshal(stats) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return statsJson, nil | ||
} | ||
|
||
func bucketStatsFunc(stats *Stats) func([]byte, *bbolt.Bucket) error { | ||
return func(name []byte, b *bbolt.Bucket) error { | ||
bstats := b.Stats() | ||
|
||
// KeyN is the number of keys | ||
// LeafAlloc is pretty close the number of bytes used | ||
stats.Buckets[string(name)] = bucketStatsHolder{ | ||
Stats: bstats, | ||
FillPercent: b.FillPercent, | ||
NumberOfKeys: bstats.KeyN, | ||
Size: bstats.LeafAlloc, | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think? Or maybe even make it anonymous in the
GetStats
method?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i'll get that cleaned up, ty!