Merge pull request #243 from prometheus/feature/size-exposition

Simplify compaction and expose database sizes.
This commit is contained in:
Matt T. Proud 2013-05-13 04:19:28 -07:00
commit dad20c15e7
4 changed files with 83 additions and 43 deletions

View file

@ -877,34 +877,57 @@ func (l *LevelDBMetricPersistence) ForEachSample(builder IteratorsForFingerprint
panic("not implemented") panic("not implemented")
} }
// CompactKeyspace compacts each database's keyspace serially. An error may // CompactKeyspace compacts each database's keyspace serially.
// be returned if there are difficulties with the underlying database or if
// it's empty.
// //
// Beware that it would probably be imprudent to run this on a live user-facing // Beware that it would probably be imprudent to run this on a live user-facing
// server due to latency implications. // server due to latency implications.
func (l *LevelDBMetricPersistence) CompactKeyspaces() error { func (l *LevelDBMetricPersistence) CompactKeyspaces() {
if err := l.CurationRemarks.CompactKeyspace(); err != nil { l.CurationRemarks.CompactKeyspace()
return fmt.Errorf("Could not compact curation remarks: %s", err) l.fingerprintToMetrics.CompactKeyspace()
} l.labelNameToFingerprints.CompactKeyspace()
if err := l.fingerprintToMetrics.CompactKeyspace(); err != nil { l.labelSetToFingerprints.CompactKeyspace()
return fmt.Errorf("Could not compact fingerprint to metric index: %s", err) l.MetricHighWatermarks.CompactKeyspace()
} l.metricMembershipIndex.CompactKeyspace()
if err := l.labelNameToFingerprints.CompactKeyspace(); err != nil { l.MetricSamples.CompactKeyspace()
return fmt.Errorf("Could not compact label name to fingerprint index: %s", err) }
}
if err := l.labelSetToFingerprints.CompactKeyspace(); err != nil { func (l *LevelDBMetricPersistence) ApproximateSizes() (total uint64, err error) {
return fmt.Errorf("Could not compact label pair to fingerprint index: %s", err) size := uint64(0)
}
if err := l.MetricHighWatermarks.CompactKeyspace(); err != nil { if size, err = l.CurationRemarks.ApproximateSize(); err != nil {
return fmt.Errorf("Could not compact metric high watermarks: %s", err) return 0, err
} }
if err := l.metricMembershipIndex.CompactKeyspace(); err != nil { total += size
return fmt.Errorf("Could not compact metric membership index: %s", err)
} if size, err = l.fingerprintToMetrics.ApproximateSize(); err != nil {
if err := l.MetricSamples.CompactKeyspace(); err != nil { return 0, err
return fmt.Errorf("Could not compact metric samples database: %s", err) }
} total += size
return nil if size, err = l.labelNameToFingerprints.ApproximateSize(); err != nil {
return 0, err
}
total += size
if size, err = l.labelSetToFingerprints.ApproximateSize(); err != nil {
return 0, err
}
total += size
if size, err = l.MetricHighWatermarks.ApproximateSize(); err != nil {
return 0, err
}
total += size
if size, err = l.metricMembershipIndex.ApproximateSize(); err != nil {
return 0, err
}
total += size
if size, err = l.MetricSamples.ApproximateSize(); err != nil {
return 0, err
}
total += size
return total, nil
} }

View file

@ -62,12 +62,14 @@ func (l *LevelDBMembershipIndex) Commit(batch raw.Batch) error {
return l.persistence.Commit(batch) return l.persistence.Commit(batch)
} }
// CompactKeyspace compacts the entire database's keyspace. An error may be // CompactKeyspace compacts the entire database's keyspace.
// returned if there are difficulties with the underlying database or if it's
// empty.
// //
// Beware that it would probably be imprudent to run this on a live user-facing // Beware that it would probably be imprudent to run this on a live user-facing
// server due to latency implications. // server due to latency implications.
func (l *LevelDBMembershipIndex) CompactKeyspace() error { func (l *LevelDBMembershipIndex) CompactKeyspace() {
return l.persistence.CompactKeyspace() l.persistence.CompactKeyspace()
}
func (l *LevelDBMembershipIndex) ApproximateSize() (uint64, error) {
return l.persistence.ApproximateSize()
} }

View file

@ -308,18 +308,27 @@ func (l *LevelDBPersistence) Commit(b raw.Batch) (err error) {
return l.storage.Write(l.writeOptions, batch.batch) return l.storage.Write(l.writeOptions, batch.batch)
} }
// CompactKeyspace compacts the entire database's keyspace. An error may be // CompactKeyspace compacts the entire database's keyspace.
// returned if there are difficulties with the underlying database or if it's
// empty.
// //
// Beware that it would probably be imprudent to run this on a live user-facing // Beware that it would probably be imprudent to run this on a live user-facing
// server due to latency implications. // server due to latency implications.
func (l *LevelDBPersistence) CompactKeyspace() error { func (l *LevelDBPersistence) CompactKeyspace() {
// Magic values per https://code.google.com/p/leveldb/source/browse/include/leveldb/db.h#131.
keyspace := levigo.Range{
Start: nil,
Limit: nil,
}
l.storage.CompactRange(keyspace)
}
func (l *LevelDBPersistence) ApproximateSize() (uint64, error) {
iterator := l.NewIterator(false) iterator := l.NewIterator(false)
defer iterator.Close() defer iterator.Close()
if !iterator.SeekToFirst() { if !iterator.SeekToFirst() {
return fmt.Errorf("could not seek to first key") return 0, fmt.Errorf("could not seek to first key")
} }
keyspace := levigo.Range{} keyspace := levigo.Range{}
@ -327,14 +336,18 @@ func (l *LevelDBPersistence) CompactKeyspace() error {
keyspace.Start = iterator.Key() keyspace.Start = iterator.Key()
if !iterator.SeekToLast() { if !iterator.SeekToLast() {
return fmt.Errorf("could not seek to last key") return 0, fmt.Errorf("could not seek to last key")
} }
keyspace.Limit = iterator.Key() keyspace.Limit = iterator.Key()
l.storage.CompactRange(keyspace) sizes := l.storage.GetApproximateSizes([]levigo.Range{keyspace})
total := uint64(0)
for _, size := range sizes {
total += size
}
return nil return total, nil
} }
// NewIterator creates a new levigoIterator, which follows the Iterator // NewIterator creates a new levigoIterator, which follows the Iterator

View file

@ -42,8 +42,10 @@ func main() {
start := time.Now() start := time.Now()
log.Printf("Starting compaction...") log.Printf("Starting compaction...")
if err := persistences.CompactKeyspaces(); err != nil { size, _ := persistences.ApproximateSizes()
log.Fatalf("Abording after %s", time.Since(start)) log.Printf("Original Size: %d", size)
} persistences.CompactKeyspaces()
log.Printf("Finished in %s", time.Since(start)) log.Printf("Finished in %s", time.Since(start))
size, _ = persistences.ApproximateSizes()
log.Printf("New Size: %d", size)
} }