Skip to content

Commit

Permalink
Fixed the snapshots by traget
Browse files Browse the repository at this point in the history
  • Loading branch information
linuskendall authored Dec 19, 2024
1 parent 6c419cb commit d6d04b3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
28 changes: 9 additions & 19 deletions internal/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ func (d *DB) GetSnapshotsByTarget(group string, target string) (entries []*Snaps
return
}

// GetAllSnapshots returns a list of all snapshots.
func (d *DB) GetAllSnapshots() (entries []*SnapshotEntry) {
iter, err := d.DB.Txn(false).LowerBound(tableSnapshotEntry, "id", "", uint64(0))
func (d *DB) GetAllSnapshotsByGroup(group string) (entries []*SnapshotEntry) {
iter, err := d.DB.Txn(false).LowerBound(tableSnapshotEntry, "id", "", "", uint64(0))
if err != nil {
panic("getting best snapshots failed: " + err.Error())
}
Expand All @@ -78,33 +77,24 @@ func (d *DB) GetAllSnapshots() (entries []*SnapshotEntry) {
if el == nil {
break
}
if group != "" && el.(*SnapshotEntry).Group != group {
continue
}
entries = append(entries, el.(*SnapshotEntry))
}
return
}

func (d *DB) GetAllSnapshotsByGroup(group string) (entries []*SnapshotEntry) {
iter, err := d.DB.Txn(false).LowerBound(tableSnapshotEntry, "id_prefix", group, "", uint64(0))
if err != nil {
panic("getting best snapshots failed: " + err.Error())
}
for {
el := iter.Next()
if el == nil {
break
}
entries = append(entries, el.(*SnapshotEntry))
}
return
// GetAllSnapshots returns a list of all snapshots.
func (d *DB) GetAllSnapshots() (entries []*SnapshotEntry) {
return d.GetAllSnapshotsByGroup("")
}

// GetBestSnapshots returns newest-to-oldest snapshots.
// The `max` argument controls the max number of snapshots to return.
// If max is negative, it returns all snapshots.
func (d *DB) GetBestSnapshotsByGroup(group string, max int) (entries []*SnapshotEntry) {
var res memdb.ResultIterator
var err error
res, err = d.DB.Txn(false).Get(tableSnapshotEntry, "slot")
res, err := d.DB.Txn(false).Get(tableSnapshotEntry, "slot")

if err != nil {
panic("getting best snapshots failed: " + err.Error())
Expand Down
13 changes: 13 additions & 0 deletions internal/index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,23 @@ func TestDB(t *testing.T) {
},
db.GetBestSnapshotsByGroup("devnet", -1))

assert.Equal(t,
[]*SnapshotEntry{
snapshotEntry4,
},
db.GetAllSnapshotsByGroup("devnet"))

assert.Equal(t,
[]*SnapshotEntry{
snapshotEntry1,
snapshotEntry3,
},
db.GetBestSnapshotsByGroup("mainnet", -1))

assert.Equal(t,
[]*SnapshotEntry{
snapshotEntry1,
snapshotEntry3,
},
db.GetAllSnapshotsByGroup("mainnet"))
}
4 changes: 4 additions & 0 deletions internal/integrationtest/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func TestTracker(t *testing.T) {
},
TotalSize: 1,
},
Group: "test",
},
{
SnapshotInfo: types.SnapshotInfo{
Expand All @@ -136,6 +137,7 @@ func TestTracker(t *testing.T) {
},
TotalSize: 1,
},
Group: "test",
},
{
SnapshotInfo: types.SnapshotInfo{
Expand All @@ -153,6 +155,7 @@ func TestTracker(t *testing.T) {
},
TotalSize: 1,
},
Group: "test",
},
{
SnapshotInfo: types.SnapshotInfo{
Expand All @@ -170,6 +173,7 @@ func TestTracker(t *testing.T) {
},
TotalSize: 1,
},
Group: "test",
},
},
snaps)
Expand Down
3 changes: 2 additions & 1 deletion internal/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (h *Handler) createJson(c *gin.Context, entries []*index.SnapshotEntry) {
sources[i] = types.SnapshotSource{
SnapshotInfo: *entry.Info,
Target: entry.Target,
Group: entry.Group,
UpdatedAt: entry.UpdatedAt,
}
}
Expand Down Expand Up @@ -110,7 +111,7 @@ func (h *Handler) Health(c *gin.Context) {
return
}
query.Max = 1
entries := h.DB.GetBestSnapshots(query.Max)
entries := h.DB.GetBestSnapshotsByGroup(query.Group, query.Max)

var health struct {
MaxSnapshot uint64
Expand Down
1 change: 1 addition & 0 deletions types/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
type SnapshotSource struct {
SnapshotInfo
Target string `json:"target"`
Group string `json:"group"`
UpdatedAt time.Time `json:"updated_at"`
}

Expand Down

0 comments on commit d6d04b3

Please sign in to comment.