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(#10): collecting and sending failed links back to cloud provider via server #20

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
6 changes: 4 additions & 2 deletions examples/keyvalue-inmemory/keyvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ var (

type Provider struct {
sync.Map
sourceLinks map[string]provider.InterfaceLinkDefinition
targetLinks map[string]provider.InterfaceLinkDefinition
sourceLinks map[string]provider.InterfaceLinkDefinition
targetLinks map[string]provider.InterfaceLinkDefinition
failedSourceLinks map[string]provider.InterfaceLinkDefinition
failedTargetLinks map[string]provider.InterfaceLinkDefinition
}

func Ok[T any](v T) *wrpc.Result[T, store.Error] {
Expand Down
80 changes: 80 additions & 0 deletions examples/keyvalue-inmemory/link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"github.com/wasmCloud/provider-sdk-go"
"log"
)

func (p *Provider) establishSourceLink(link provider.InterfaceLinkDefinition) error {
if err := p.validateSourceLink(link); err != nil {
return err
}

p.sourceLinks[link.Target] = link
return nil
}

func (p *Provider) establishTargetLink(link provider.InterfaceLinkDefinition) error {
if err := p.validateTargetLink(link); err != nil {
return err
}

p.targetLinks[link.SourceID] = link
return nil
}

func (p *Provider) validateSourceLink(link provider.InterfaceLinkDefinition) error {
// TODO: Add validation checks
return nil
}

func (p *Provider) validateTargetLink(link provider.InterfaceLinkDefinition) error {
// TODO: Add validation checks
return nil
}

func (p *Provider) handleNewSourceLink(link provider.InterfaceLinkDefinition) error {
log.Println("Handling new source link", link)
err := p.establishSourceLink(link)
if err != nil {
log.Println("Failed to establish source link", link, err)
p.failedSourceLinks[link.Target] = link
return err
}
p.sourceLinks[link.Target] = link
return nil
}

func (p *Provider) handleNewTargetLink(link provider.InterfaceLinkDefinition) error {
log.Println("Handling new target link", link)
err := p.establishTargetLink(link)
if err != nil {
log.Println("Failed to establish target link", link, err)
p.failedTargetLinks[link.SourceID] = link
return err
}
p.targetLinks[link.SourceID] = link
return nil
}

func (p *Provider) handleDelSourceLink(link provider.InterfaceLinkDefinition) error {
log.Println("Handling del source link", link)
delete(p.sourceLinks, link.Target)
return nil
}

func (p *Provider) handleDelTargetLink(link provider.InterfaceLinkDefinition) error {
log.Println("Handling del target link", link)
delete(p.targetLinks, link.SourceID)
return nil
}

func (p *Provider) handleHealthCheck() string {
log.Println("Handling health check")
return "provider healthy"
}

func (p *Provider) handleShutdown() error {
log.Println("Handling shutdown")
return nil
}
42 changes: 4 additions & 38 deletions examples/keyvalue-inmemory/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:generate wit-bindgen-wrpc go --out-dir bindings --package github.com/wasmCloud/provider-sdk-go/examples/keyvalue-inmemory/bindings wit

package main

import (
Expand All @@ -20,8 +18,10 @@ func main() {

func run() error {
p := &Provider{
sourceLinks: make(map[string]provider.InterfaceLinkDefinition),
targetLinks: make(map[string]provider.InterfaceLinkDefinition),
sourceLinks: make(map[string]provider.InterfaceLinkDefinition),
targetLinks: make(map[string]provider.InterfaceLinkDefinition),
failedSourceLinks: make(map[string]provider.InterfaceLinkDefinition),
failedTargetLinks: make(map[string]provider.InterfaceLinkDefinition),
}

wasmcloudprovider, err := provider.New(
Expand Down Expand Up @@ -66,37 +66,3 @@ func run() error {

return nil
}

func (p *Provider) handleNewSourceLink(link provider.InterfaceLinkDefinition) error {
log.Println("Handling new source link", link)
p.sourceLinks[link.Target] = link
return nil
}

func (p *Provider) handleNewTargetLink(link provider.InterfaceLinkDefinition) error {
log.Println("Handling new target link", link)
p.targetLinks[link.SourceID] = link
return nil
}

func (p *Provider) handleDelSourceLink(link provider.InterfaceLinkDefinition) error {
log.Println("Handling del source link", link)
delete(p.sourceLinks, link.Target)
return nil
}

func (p *Provider) handleDelTargetLink(link provider.InterfaceLinkDefinition) error {
log.Println("Handling del target link", link)
delete(p.targetLinks, link.SourceID)
return nil
}

func (p *Provider) handleHealthCheck() string {
log.Println("Handling health check")
return "provider healthy"
}

func (p *Provider) handleShutdown() error {
log.Println("Handling shutdown")
return nil
}
34 changes: 7 additions & 27 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func New(options ...ProviderHandler) (*WasmcloudProvider, error) {
}

// partition links based on if the provider is the source or target
sourceLinks := []InterfaceLinkDefinition{}
targetLinks := []InterfaceLinkDefinition{}
var sourceLinks []InterfaceLinkDefinition
var targetLinks []InterfaceLinkDefinition

// Loop over the numbers
for _, link := range hostData.LinkDefinitions {
Expand Down Expand Up @@ -344,20 +344,11 @@ func (wp *WasmcloudProvider) putLink(l InterfaceLinkDefinition) error {

wp.lock.Lock()
defer wp.lock.Unlock()
if l.SourceID == wp.Id {
err := wp.putSourceLinkFunc(l)
if err != nil {
return err
}

wp.sourceLinks[l.Target] = l
if l.SourceID == wp.Id {
return wp.putSourceLinkFunc(l)
} else if l.Target == wp.Id {
err := wp.putTargetLinkFunc(l)
if err != nil {
return err
}

wp.targetLinks[l.SourceID] = l
Comment on lines -347 to -360
Copy link
Member

Choose a reason for hiding this comment

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

Could you comment on why you removed this specific logic? With our example provider we do keep track of links explicitly, however I think in the general case providers shouldn't have to explicitly keep track of links and should be able to store whatever data necessary. Does that make sense?

Storing the links here is also necessary for the checks in the provider SDK to prevent duplicate links

Copy link
Contributor Author

@tom-fitz tom-fitz Jun 19, 2024

Choose a reason for hiding this comment

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

I think I may not be fully understanding the business logic here, or the request to refactor the link logic outside of the provider. This logic, if I'm understanding it correctly, is further down the chain and being stored as part of these functions and bubbling the error back up to the provider:

func (p *Provider) handleNewSourceLink(link provider.InterfaceLinkDefinition) error {
	log.Println("Handling new source link", link)
	err := p.establishSourceLink(link)
	if err != nil {
		log.Println("Failed to establish source link", link, err)
		p.failedSourceLinks[link.Target] = link
		return err
	}
	p.sourceLinks[link.Target] = link
	return nil
}

func (p *Provider) handleNewTargetLink(link provider.InterfaceLinkDefinition) error {
	log.Println("Handling new target link", link)
	err := p.establishTargetLink(link)
	if err != nil {
		log.Println("Failed to establish target link", link, err)
		p.failedTargetLinks[link.SourceID] = link
		return err
	}
	p.targetLinks[link.SourceID] = link
	return nil
}

Copy link
Member

Choose a reason for hiding this comment

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

Ah to clarify, I like that the provider SDK itself is keeping track of the links in order to put/delete them from a map and that the developer writing a provider handler doesn't need to keep that map established (even though we do in our example)

return wp.putTargetLinkFunc(l)
} else {
wp.Logger.Info("received link that isn't for this provider, ignoring", "link", l)
}
Expand All @@ -369,23 +360,12 @@ func (wp *WasmcloudProvider) deleteLink(l InterfaceLinkDefinition) error {
wp.lock.Lock()
defer wp.lock.Unlock()
if l.SourceID == wp.Id {
err := wp.delSourceLinkFunc(l)
if err != nil {
return err
}

delete(wp.sourceLinks, l.Target)
return wp.delSourceLinkFunc(l)
} else if l.Target == wp.Id {
err := wp.delTargetLinkFunc(l)
if err != nil {
return err
}

delete(wp.targetLinks, l.SourceID)
return wp.delTargetLinkFunc(l)
} else {
wp.Logger.Info("received link delete that isn't for this provider, ignoring", "link", l)
}

return nil
}

Expand Down