Skip to content

Commit

Permalink
Slot schedule: print also recoverable roots extremes
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Sep 25, 2023
1 parent 14cc86f commit 7f2d9d2
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
92 changes: 92 additions & 0 deletions pkg/blockstore/meta_rocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ package blockstore

import (
"fmt"
"math"

"github.com/linxGnu/grocksdb"
"go.firedancer.io/radiance/pkg/shred"
)

// MaxRoot returns the last known root slot.
Expand All @@ -26,6 +28,96 @@ func (d *DB) MaxRoot() (uint64, error) {
return slot, nil
}

// MinRoot returns the first known root slot.
func (d *DB) MinRoot() (uint64, error) {
opts := grocksdb.NewDefaultReadOptions()
opts.SetVerifyChecksums(false)
opts.SetFillCache(false)
iter := d.DB.NewIteratorCF(opts, d.CfRoot)
defer iter.Close()
iter.SeekToFirst()
if !iter.Valid() {
return 0, ErrNotFound
}
slot, ok := ParseSlotKey(iter.Key().Data())
if !ok {
return 0, fmt.Errorf("invalid key in root cf")
}
return slot, nil
}

// MaxMaybeRootedValidSlot returns the last valid slot, either rooted or having meta and entries.
func (d *DB) MaxMaybeRootedValidSlot() (uint64, error) {
opts := grocksdb.NewDefaultReadOptions()
opts.SetVerifyChecksums(false)
opts.SetFillCache(false)
iter := d.DB.NewIteratorCF(opts, d.CfMeta)
defer iter.Close()
iter.SeekToLast()
for {
if !iter.Valid() {
return 0, ErrNotFound
}
slot, can := canRecover(d, iter)
if can {
return slot, nil
}
iter.Prev()
}
return 0, ErrNotFound
}

// MinMaybeRootedValidSlot returns the first valid slot, either rooted or having meta and entries.
func (d *DB) MinMaybeRootedValidSlot() (uint64, error) {
opts := grocksdb.NewDefaultReadOptions()
opts.SetVerifyChecksums(false)
opts.SetFillCache(false)
iter := d.DB.NewIteratorCF(opts, d.CfMeta)
defer iter.Close()
iter.SeekToFirst()
for {
if !iter.Valid() {
return 0, ErrNotFound
}
slot, can := canRecover(d, iter)
if can {
return slot, nil
}
iter.Next()
}
return 0, ErrNotFound
}

func canRecover(db *DB, iter *grocksdb.Iterator) (uint64, bool) {
slot, ok := ParseSlotKey(iter.Key().Data())
if !ok {
return 0, false
}
meta, err := db.GetSlotMeta(slot)
if err != nil {
return 0, false
}
if meta.ParentSlot == math.MaxUint64 {
return 0, false
}
// check if has entries
_, err = getEntriesAnyVersion(db, meta)
if err != nil {
return 0, false
}
return slot, true
}

func getEntriesAnyVersion(db *DB, meta *SlotMeta) ([]Entries, error) {
for _, revision := range []int{shred.RevisionV1, shred.RevisionV2} {
entries, err := db.GetEntries(meta, revision)
if err == nil {
return entries, nil
}
}
return nil, fmt.Errorf("no entries found for slot %d", meta.Slot)
}

// GetSlotMeta returns the shredding metadata of a given slot.
func (d *DB) GetSlotMeta(slot uint64) (*SlotMeta, error) {
key := MakeSlotKey(slot)
Expand Down
39 changes: 39 additions & 0 deletions pkg/blockstore/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,45 @@ func (schedule *TraversalSchedule) init(
copy(reversedHandles, handles)
reverse(reversedHandles)

{
for _, h := range reversedHandles {
msg := fmt.Sprintf("db %q:", h.DB.DB.Name())
{
slot, err := h.DB.MinRoot()
if err != nil {
msg += fmt.Sprintf(" MinRoot=error(%s)", err)
} else {
msg += fmt.Sprintf(" MinRoot=%d", slot)
}
}
{
slot, err := h.DB.MinMaybeRootedValidSlot()
if err != nil {
msg += fmt.Sprintf(" MinMaybeRootedValidSlot=error(%s)", err)
} else {
msg += fmt.Sprintf(" MinMaybeRootedValidSlot=%d", slot)
}
}
{
slot, err := h.DB.MaxRoot()
if err != nil {
msg += fmt.Sprintf(" MaxRoot=error(%s)", err)
} else {
msg += fmt.Sprintf(" MaxRoot=%d", slot)
}
}
{
slot, err := h.DB.MaxMaybeRootedValidSlot()
if err != nil {
msg += fmt.Sprintf(" MaxMaybeRootedValidSlot=error(%s)", err)
} else {
msg += fmt.Sprintf(" MaxMaybeRootedValidSlot=%d", slot)
}
}
klog.Info(msg)
}
}

recoveries := make(map[uint64]struct{})
defer func() {
if len(recoveries) > 0 {
Expand Down

0 comments on commit 7f2d9d2

Please sign in to comment.