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: Solver Persistency #391

Closed
wants to merge 6 commits into from
Closed
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
1 change: 1 addition & 0 deletions .clabot
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"alvin-reyes",
"hunjixin",
"sigmundxyz",
"apquinit",
"lilypad-releases[bot]"
],
"message": "Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have the users {{usersWithoutCLA}} on file. In order for us to review and merge your code, please open a new pull request and sign the Contributor License Agreement following the instructions in our [contributing guide](https://github.com/Lilypad-Tech/lilypad/blob/main/CONTRIBUTING.md#contributor-license-agreement). Once the pull request is merged, ping a maintainer who will summon me again."
Expand Down
5 changes: 5 additions & 0 deletions NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ Licensee’s name: Robert Marinescu
Repository system identifier: sigmundxyz

---------------------------------------------------------------------------------
Licensee’s name: Alexander Paul Quinit

Repository system identifier: apquinit

---------------------------------------------------------------------------------
183 changes: 17 additions & 166 deletions go.mod

Large diffs are not rendered by default.

345 changes: 20 additions & 325 deletions go.sum

Large diffs are not rendered by default.

76 changes: 76 additions & 0 deletions pkg/solver/store/sqlite/bindings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package store

import (
"github.com/lilypad-tech/lilypad/pkg/data"
)

// Adding all pkg/data types to pkg/store/sqlite/models converters

func ToJobOfferData(jobOffer data.JobOfferContainer) JobOfferData {
jobOfferData := JobOfferData{}
jobOfferData.CID = jobOffer.ID
jobOfferData.DealID = jobOffer.DealID
jobOfferData.JobCreator = jobOffer.JobCreator
jobOfferData.State = jobOffer.State
jobOfferData.JobOffer = jobOffer.JobOffer

return jobOfferData
}

func ToResourceOfferData(resourceOffer data.ResourceOfferContainer) ResourceOfferData {
resourceOfferData := ResourceOfferData{}
resourceOfferData.CID = resourceOffer.ID
resourceOfferData.DealID = resourceOffer.DealID
resourceOfferData.ResourceProvider = resourceOffer.ResourceProvider
resourceOfferData.State = resourceOffer.State
resourceOfferData.ResourceOffer = resourceOffer.ResourceOffer

return resourceOfferData
}

func ToDealData(deal data.DealContainer) DealData {
dealData := DealData{}
dealData.CID = deal.ID
dealData.JobCreator = deal.JobCreator
dealData.ResourceProvider = deal.ResourceProvider
dealData.JobOffer = deal.JobOffer
dealData.ResourceOffer = deal.ResourceOffer
dealData.State = deal.State
dealData.Deal = deal.Deal
dealData.Transactions = deal.Transactions
dealData.Mediator = deal.Mediator

return dealData
}

func ToResultData(result data.Result) ResultData {
resultData := ResultData{}
resultData.CID = result.ID
resultData.DealID = result.DealID
resultData.DataID = result.DataID
resultData.Error = result.Error
resultData.InstructionCount = result.InstructionCount

return resultData
}

func ToMatchDecisionData(matchDecision data.MatchDecision) MatchDecisionData {
matchDecisionData := MatchDecisionData{}
matchDecisionData.JobOffer = matchDecision.JobOffer
matchDecisionData.ResourceOffer = matchDecision.ResourceOffer
matchDecisionData.ResourceOffer = matchDecision.ResourceOffer
return matchDecisionData
}

// }
//
// // represents a solver decision
// // the solver keeps track of "no" decisions to avoid trying to repeatedly match
// // things it's already decided it can't match
// type MatchDecisionData struct {
// gorm.Model
// JobOffer string `json:"job_offer"`
// ResourceOffer string `json:"resource_offer"`
// Deal string `json:"deal"`
// Result bool `json:"result"`
// }
63 changes: 63 additions & 0 deletions pkg/solver/store/sqlite/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package store

import (
"gorm.io/gorm"
"github.com/lilypad-tech/lilypad/pkg/data"
)

// this is what the solver keeps track of so we can know
// what the current state of the deal is
type JobOfferData struct {
gorm.Model
CID string `json:"cid"`
DealID string `json:"deal_id"`
JobCreator string `json:"job_creator"`
State uint8 `json:"state"`
JobOffer data.JobOffer `json:"job_offer"`
}

// this is what the solver keeps track of so we can know
// what the current state of the deal is
type ResourceOfferData struct {
gorm.Model
CID string `json:"cid"`
DealID string `json:"deal_id"`
ResourceProvider string `json:"resource_provider"`
State uint8 `json:"state"`
ResourceOffer data.ResourceOffer `json:"job_offer"`
}

type DealData struct {
gorm.Model
CID string `json:"cid"`
JobCreator string `json:"job_creator"`
ResourceProvider string `json:"resource_provider"`
JobOffer string `json:"job_offer"`
ResourceOffer string `json:"resource_offer"`
State uint8 `json:"state"`
Deal data.Deal `json:"deal"`
Transactions data.DealTransactions `json:"transactions"`
Mediator string `json:"mediator"`
}

type ResultData struct {
gorm.Model
// this is the cid of the result where ID is set to empty string
CID string `json:"cid"`
DealID string `json:"deal_id"`
// the CID of the actual results
DataID string `json:"results_id"`
Error string `json:"error"`
InstructionCount uint64 `json:"instruction_count"`
}

// represents a solver decision
// the solver keeps track of "no" decisions to avoid trying to repeatedly match
// things it's already decided it can't match
type MatchDecisionData struct {
gorm.Model
JobOffer string `json:"job_offer"`
ResourceOffer string `json:"resource_offer"`
Deal string `json:"deal"`
Result bool `json:"result"`
}
93 changes: 93 additions & 0 deletions pkg/solver/store/sqlite/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package store

import (
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/driver/sqlite"
"fmt"
// "strings"
"sync"

"github.com/lilypad-tech/lilypad/pkg/data"
// "github.com/lilypad-tech/lilypad/pkg/jsonl"
// "github.com/lilypad-tech/lilypad/pkg/solver/store"
)

type SolverStoreSqlite struct {
DB *gorm.DB
mutex sync.RWMutex
}

func getMatchID(resourceOffer string, jobOffer string) string {
return fmt.Sprintf("%s-%s", resourceOffer, jobOffer)
}

func NewSolverStoreSqlite(dbPath string) (*SolverStoreSqlite, error) {
var database *gorm.DB
var err error

database, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
Logger: logger.Default.LogMode(logger.Silent),
})

if err != nil {
return nil, err
}

// create tables if they haven't been created yet
err = database.AutoMigrate()
if err != nil {
fmt.Println("Failed to auto migrate:", err)
return nil, err
}

return &SolverStoreSqlite{DB: database}, err
}

func (s *SolverStoreSqlite) AddJobOffer(jobOffer data.JobOfferContainer) (*data.JobOfferContainer, error) {
s.mutex.Lock()
defer s.mutex.Unlock()

jobOfferData := ToJobOfferData(jobOffer)
result := s.DB.Create(&jobOfferData)
if result.Error != nil {
return nil, result.Error
}

return &jobOffer, nil
}

// func (s *SolverStoreSqlite) AddResourceOffer(resourceOffer data.ResourceOfferContainer) (*data.ResourceOfferContainer, error) {
// s.mutex.Lock()
// defer s.mutex.Unlock()
//
// dealData := ToRe(deal)
// result := s.DB.Create(&dealData)
// if result.Error != nil {
// return nil, result.Error
// }
//
// return &resourceOffer, nil
// }

func (s *SolverStoreSqlite) AddDeal(deal data.DealContainer) (*data.DealContainer, error) {
s.mutex.Lock()
defer s.mutex.Unlock()

dealData := ToDealData(deal)
result := s.DB.Create(&dealData)
if result.Error != nil {
return nil, result.Error
}

return &deal, nil
}

func (s *SolverStoreSqlite) AddResult(result data.Result) (*data.Result, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
return &result, nil
}

// Removed everything else needs redo
1 change: 1 addition & 0 deletions pkg/solver/store/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package store