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

Move More Generics to a Library #19

Merged
merged 1 commit into from
Nov 29, 2024
Merged
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require (
github.com/google/uuid v1.6.0
github.com/oapi-codegen/runtime v1.1.1
github.com/spf13/pflag v1.0.5
github.com/spjmurray/go-sat v0.4.0
github.com/spjmurray/go-util v0.1.2
github.com/spjmurray/go-sat v0.4.1
github.com/spjmurray/go-util v0.1.3
github.com/stretchr/testify v1.10.0
github.com/unikorn-cloud/core v0.1.86
github.com/unikorn-cloud/identity v0.2.45
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spjmurray/go-sat v0.4.0 h1:BFD20Ot3wKN4Rf26vVOjfosda2MJW/uJ6GrDruKiTdU=
github.com/spjmurray/go-sat v0.4.0/go.mod h1:DCU2g/lAREdvbec/Szz+Sqcg4NiO1RxzAP+nJHExoq8=
github.com/spjmurray/go-util v0.1.2 h1:dZ9ZGus+P95kcFMDY+aR/0SAj+c3WMC8ZAT/CHLa9UM=
github.com/spjmurray/go-util v0.1.2/go.mod h1:fARcBeaHio/6h9H7Ht+egZPBZMNxEwxmHC1vyjBtPbs=
github.com/spjmurray/go-sat v0.4.1 h1:V5yCggtyOJJA8j9tMhjNXTKi+p8aRsUdiu4Jt52GM9w=
github.com/spjmurray/go-sat v0.4.1/go.mod h1:KnOUTnJXZ5TmUjjFyDtGSKI/BlCMMIkWtSrX4ADAKlI=
github.com/spjmurray/go-util v0.1.3 h1:EJxYEH6FnKJwgRWw8sSEyOreqU07lI2V8+KZnCSsLoE=
github.com/spjmurray/go-util v0.1.3/go.mod h1:fARcBeaHio/6h9H7Ht+egZPBZMNxEwxmHC1vyjBtPbs=
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
7 changes: 4 additions & 3 deletions pkg/provisioners/managers/application/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"

"github.com/spf13/pflag"
"github.com/spjmurray/go-util/pkg/graph"
"github.com/spjmurray/go-util/pkg/set"

unikornv1 "github.com/unikorn-cloud/application/pkg/apis/unikorn/v1alpha1"
Expand Down Expand Up @@ -189,10 +190,10 @@ func Schedule(ctx context.Context, applications solver.ApplicationIndex, solutio

// Then we need to walk the graph from the roots to the leaves, but only
// processing nodes once all their dependencies are satisfied.
graph := solver.NewGraphWalker[solver.AppVersion]()
graph := graph.NewWalker[solver.AppVersion]()

for _, root := range roots {
graph.Enqueue(root)
graph.Push(root)
}

visitor := &schedulerVistor{
Expand All @@ -202,7 +203,7 @@ func Schedule(ctx context.Context, applications solver.ApplicationIndex, solutio
seen: set.Set[string]{},
}

if err := graph.Walk(visitor); err != nil {
if err := graph.Visit(visitor); err != nil {
return nil, err
}

Expand Down
52 changes: 4 additions & 48 deletions pkg/solver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"fmt"

"github.com/spjmurray/go-sat/pkg/cdcl"
"github.com/spjmurray/go-util/pkg/queue"
"github.com/spjmurray/go-util/pkg/graph"
"github.com/spjmurray/go-util/pkg/set"

unikornv1 "github.com/unikorn-cloud/application/pkg/apis/unikorn/v1alpha1"
Expand All @@ -40,50 +40,6 @@ var (
ErrConstraint = errors.New("constraint error")
)

// GraphVisitor is used to visit a node in the graph.
type GraphVisitor[T comparable] interface {
// Visit is called when a new node is encountered, it accepts
// the node itself and an enqueue function.
Visit(node T, enqueue func(T)) error
}

type GraphWalker[T comparable] struct {
queue *queue.Queue[T]
seen set.Set[T]
}

func NewGraphWalker[T comparable]() *GraphWalker[T] {
return &GraphWalker[T]{
queue: queue.New[T](),
seen: set.New[T](),
}
}

func (g *GraphWalker[T]) Enqueue(t T) {
g.queue.Push(t)
}

func (g *GraphWalker[T]) Walk(visitor GraphVisitor[T]) error {
for !g.queue.Empty() {
t, err := g.queue.Pop()
if err != nil {
return err
}

if g.seen.Contains(t) {
continue
}

g.seen.Add(t)

if err := visitor.Visit(t, g.Enqueue); err != nil {
return err
}
}

return nil
}

// AppVersion wraps up applicationID and version tuples in a comparable
// and easy to use form when interacting with the SAT solver.
type AppVersion struct {
Expand Down Expand Up @@ -207,7 +163,7 @@ func (v *solverVisitor) Visit(name string, enqueue func(string)) error {
func SolveApplicationSet(ctx context.Context, applications ApplicationIndex, applicationset *unikornv1.ApplicationSet) (set.Set[AppVersion], error) {
// We're going to do an exhaustive walk of the dependency graph gathering
// all application/version tuples as variables, and also create any clauses along the way.
graph := NewGraphWalker[string]()
graph := graph.NewWalker[string]()

model := cdcl.NewModel[AppVersion]()

Expand All @@ -219,7 +175,7 @@ func SolveApplicationSet(ctx context.Context, applications ApplicationIndex, app
return nil, err
}

graph.Enqueue(ref.Name)
graph.Push(ref.Name)

// Add a unit clause if an application version is specified.
if ref.Version != nil {
Expand Down Expand Up @@ -248,7 +204,7 @@ func SolveApplicationSet(ctx context.Context, applications ApplicationIndex, app
model: model,
}

if err := graph.Walk(visitor); err != nil {
if err := graph.Visit(visitor); err != nil {
return nil, err
}

Expand Down