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 deal making trace #336

Merged
merged 10 commits into from
Oct 18, 2024
Merged
32 changes: 32 additions & 0 deletions pkg/data/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,48 @@ func GetJobOfferID(offer JobOffer) (string, error) {
return CalculateCID(offer)
}

func GetJobOfferContainerIDs(jobOffers []JobOfferContainer) []string {
var ids []string
for _, offer := range jobOffers {
ids = append(ids, offer.ID)
}
return ids
}

func GetResourceOfferID(offer ResourceOffer) (string, error) {
offer.ID = ""
return CalculateCID(offer)
}

func GetResourceOfferIDs(resourceOffers []ResourceOffer) []string {
var ids []string
for _, offer := range resourceOffers {
ids = append(ids, offer.ID)
}
return ids
}
Comment on lines +49 to +55
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be confusing that that GetResourceOfferIDs has a different behavior than GetResourceOfferID. Open to naming suggestions.


func GetResourceOfferContainerIDs(resourceOffers []ResourceOfferContainer) []string {
var ids []string
for _, offer := range resourceOffers {
ids = append(ids, offer.ID)
}
return ids
}

func GetDealID(deal Deal) (string, error) {
deal.ID = ""
return CalculateCID(deal)
}

func GetDealIDs(deals []Deal) []string {
var ids []string
for _, deal := range deals {
ids = append(ids, deal.ID)
}
return ids
}

func GetModuleID(module ModuleConfig) (string, error) {
return CalculateCID(module)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/options/resource-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func GetDefaultResourceProviderOfferOptions() resourceprovider.ResourceProviderO
// this can be populated by a config file
Specs: []data.MachineSpec{},
// if an RP wants to only run certain modules they list them here
// XXX SECURITY: enforce that they are specified with specific git hashes!
// XXX SECURITY: enforce that they are specified by CID
Modules: GetDefaultServeOptionStringArray("OFFER_MODULES", []string{}),
// this is the default pricing mode for an RP
Mode: GetDefaultPricingMode(data.FixedPrice),
Expand Down
50 changes: 45 additions & 5 deletions pkg/solver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/lilypad-tech/lilypad/pkg/web3/bindings/mediation"
"github.com/lilypad-tech/lilypad/pkg/web3/bindings/storage"
"github.com/rs/zerolog/log"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -107,7 +109,7 @@ func (controller *SolverController) Start(ctx context.Context, cm *system.Cleanu
ctx,
CONTROL_LOOP_INTERVAL,
func() error {
err := controller.solve()
err := controller.solve(ctx)
if err != nil {
errorChan <- err
}
Expand Down Expand Up @@ -272,20 +274,32 @@ func (controller *SolverController) registerAsSolver() error {
*
*/

func (controller *SolverController) solve() error {
func (controller *SolverController) solve(ctx context.Context) error {
ctx, span := controller.tracer.Start(ctx, "solve")
defer span.End()

// find out which deals we can make from matching the offers
deals, err := matcher.GetMatchingDeals(controller.store, controller.updateJobOfferState)
deals, err := matcher.GetMatchingDeals(ctx, controller.store, controller.updateJobOfferState, controller.tracer)
if err != nil {
span.SetStatus(codes.Error, "get matching deals failed")
span.RecordError(err)
return err
}
span.SetAttributes(attribute.KeyValue{
Key: "deal_ids",
Value: attribute.StringSliceValue(data.GetDealIDs(deals)),
})

// loop over each of the deals add add them to the store and emit events
span.AddEvent("add_deals.start")
for _, deal := range deals {
_, err := controller.addDeal(deal)
_, err := controller.addDeal(ctx, deal)
if err != nil {
return err
}
}
span.AddEvent("add_deals.done")

return nil
}

Expand Down Expand Up @@ -391,31 +405,57 @@ func (controller *SolverController) removeResourceOfferByResourceProvider(ID str
return nil
}

func (controller *SolverController) addDeal(deal data.Deal) (*data.DealContainer, error) {
func (controller *SolverController) addDeal(ctx context.Context, deal data.Deal) (*data.DealContainer, error) {
ctx, span := controller.tracer.Start(ctx, "add_deal")
defer span.End()

span.AddEvent("data.get_deal_id.start")
id, err := data.GetDealID(deal)
if err != nil {
span.SetStatus(codes.Error, "get deal ID failed")
span.RecordError(err)
return nil, err
}
deal.ID = id
span.SetAttributes(attribute.String("deal.id", deal.ID))
span.AddEvent("data.get_deal_id.done")

controller.log.Info("add deal", deal)

span.AddEvent("store.add_deal.start")
ret, err := controller.store.AddDeal(data.GetDealContainer(deal))
if err != nil {
span.SetStatus(codes.Error, "add deal to store failed")
span.RecordError(err)
return nil, err
}
span.AddEvent("store.add_deal.done")

span.AddEvent("write_event.start")
controller.writeEvent(SolverEvent{
EventType: DealAdded,
Deal: ret,
})
span.AddEvent("write_event.done")

span.AddEvent("update_job_offer_state.start")
_, err = controller.updateJobOfferState(ret.JobOffer, ret.ID, ret.State)
if err != nil {
span.SetStatus(codes.Error, "updated job offer state failed")
span.RecordError(err)
return nil, err
}
span.AddEvent("update_job_offer_state.done")

span.AddEvent("update_resource_offer_state.start")
_, err = controller.updateResourceOfferState(ret.ResourceOffer, ret.ID, ret.State)
if err != nil {
span.SetStatus(codes.Error, "updated resource offer state failed")
span.RecordError(err)
return nil, err
}
span.AddEvent("update_resource_offer_state.done")

return ret, nil
}

Expand Down
Loading
Loading