Skip to content
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

feat: add ShuttleSPPreference #684

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions model/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (

type StorageMiner struct {
gorm.Model
Address util.DbAddr `gorm:"unique"`
Suspended bool
SuspendedReason string
Name string
Version string
Location string
Owner uint
Address util.DbAddr `gorm:"unique"`
Suspended bool
SuspendedReason string
Name string
Version string
Location string
Owner uint
ShuttlePreference []ShuttlePreference
}
17 changes: 9 additions & 8 deletions model/shuttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (

type Shuttle struct {
gorm.Model
Handle string `gorm:"unique"`
Token string
Host string
PeerID string
LastConnection time.Time
Private bool
Open bool
Priority int
Handle string `gorm:"unique"`
Token string
Host string
PeerID string
LastConnection time.Time
Private bool
Open bool
Priority int
ShuttlePreference []ShuttlePreference
}
10 changes: 10 additions & 0 deletions model/shuttle_preference.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package model

import "gorm.io/gorm"

type ShuttlePreference struct {
gorm.Model
StorageMinerID uint `gorm:"index:idx_pref"`
ShuttleID uint
Priority uint `gorm:"index:idx_pref, sort:asc"`
}
78 changes: 78 additions & 0 deletions shuttlepreferencemgr/shuttlepreferencemgr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package shuttlepreferencemgr

import (
"context"
"sort"

"github.com/application-research/estuary/model"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type ShuttlePreferenceManager struct {
DB *gorm.DB
MaxPriority int
}

type StorageProviderShuttlePing struct {
StorageMinerID uint
pings []ShuttlePing
}

type ShuttlePing struct {
ShuttleID uint
ping float64
}

func NewShuttlePreferenceManager(db *gorm.DB, maxPriority int) (*ShuttlePreferenceManager, error) {
spref := &ShuttlePreferenceManager{
DB: db,
MaxPriority: maxPriority,
}

return spref, nil
}

// Get SPs and priority in reference to a given shuttle
func (spm *ShuttlePreferenceManager) Query(sourceShuttle string) ([]model.ShuttlePreference, error) {
var spl []model.ShuttlePreference

if err := spm.DB.Find(&spl, "ShuttleID = ?", sourceShuttle).Error; err != nil {
return nil, err
}

return spl, nil
}

// Creates or update SP/Shuttle preference records associated with the provided SP ids
func (spm *ShuttlePreferenceManager) PostSP(ctx context.Context, pings StorageProviderShuttlePing) error {

var toCreate []model.ShuttlePreference
sorted := sortShuttles(pings.pings)

// Create entries for each SP/Shuttle Priority mapping
for pri, shuttle := range sorted {
// Only create records for this max priority #
if pri == spm.MaxPriority {
break
}
toCreate = append(toCreate, model.ShuttlePreference{
StorageMinerID: pings.StorageMinerID,
ShuttleID: shuttle.ShuttleID,
Priority: uint(pri),
})
}

// Save entry - Upsert -> Update existing entries (if SP's shuttle priority changes)
spm.DB.Create(&toCreate).Clauses(clause.OnConflict{UpdateAll: true})
return nil
}

// Sorts a list of shuttles in increasing order of their ping
func sortShuttles(m []ShuttlePing) []ShuttlePing {
sort.Slice(m, func(i, j int) bool {
return m[i].ping < m[j].ping
})

return m
}