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

go/runtime/registry/host: Remove obsolete RONL versions from aggregate #6028

Merged
merged 3 commits into from
Jan 31, 2025
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
Empty file added .changelog/6028.trivial.md
Empty file.
57 changes: 47 additions & 10 deletions go/runtime/host/multi/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"slices"
"sync"

"github.com/cenkalti/backoff/v4"
Expand Down Expand Up @@ -343,13 +344,27 @@ func (agg *Aggregate) Component(id component.ID) (host.Runtime, bool) {
return nil, false
}

// GetVersion retrieves the runtime host for the specified version.
func (agg *Aggregate) GetVersion(version version.Version) (host.Runtime, error) {
// Versions returns a sorted list of all versions in the aggregate.
func (agg *Aggregate) Versions() []version.Version {
agg.l.RLock()
defer agg.l.RUnlock()

host := agg.hosts[version]
if host == nil {
versions := make([]version.Version, 0, len(agg.hosts))
for v := range agg.hosts {
versions = append(versions, v)
}

slices.SortFunc(versions, version.Version.Cmp)
return versions
}

// Version retrieves the runtime host for the specified version.
func (agg *Aggregate) Version(version version.Version) (host.Runtime, error) {
agg.l.RLock()
defer agg.l.RUnlock()

host, ok := agg.hosts[version]
if !ok {
return nil, ErrNoSuchVersion
}
// Only allow fetching either the active or next versions.
Expand Down Expand Up @@ -379,9 +394,7 @@ func (agg *Aggregate) AddVersion(version version.Version, rt host.Runtime) error
agg.l.Lock()
defer agg.l.Unlock()

agg.logger.Info("adding version",
"version", version,
)
agg.logger.Info("adding version", "version", version)

if id := rt.ID(); id != agg.id {
return fmt.Errorf("runtime/host/multi: runtime mismatch: got '%s', expected '%s'", id, agg.id)
Expand All @@ -393,9 +406,7 @@ func (agg *Aggregate) AddVersion(version version.Version, rt host.Runtime) error

agg.hosts[version] = newAggregateHost(rt, version)

agg.logger.Info("version added",
"version", version,
)
agg.logger.Info("version added", "version", version)

if agg.running {
agg.startActiveLocked()
Expand All @@ -405,6 +416,32 @@ func (agg *Aggregate) AddVersion(version version.Version, rt host.Runtime) error
return nil
}

// RemoveVersion removes the specified version from the aggregate.
//
// A version cannot be removed if it is currently running, meaning it
// is marked as the active version or the next version.
func (agg *Aggregate) RemoveVersion(version version.Version) error {
agg.l.Lock()
defer agg.l.Unlock()

agg.logger.Info("removing version", "version", version)

if _, ok := agg.hosts[version]; !ok {
return ErrNoSuchVersion
}
if agg.activeVersion != nil && version == *agg.activeVersion {
return fmt.Errorf("runtime/host/multi: cannot remove active version '%s'", version)
}
if agg.nextVersion != nil && version == *agg.nextVersion {
return fmt.Errorf("runtime/host/multi: cannot remove next version '%s'", version)
}
delete(agg.hosts, version)

agg.logger.Info("version removed", "version", version)

return nil
}

// SetVersion updates the active and next runtime versions.
//
// If the aggregate is not running, this function simply updates the versions.
Expand Down
44 changes: 30 additions & 14 deletions go/runtime/registry/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/oasisprotocol/oasis-core/go/runtime/bundle/component"
"github.com/oasisprotocol/oasis-core/go/runtime/host"
"github.com/oasisprotocol/oasis-core/go/runtime/host/composite"
"github.com/oasisprotocol/oasis-core/go/runtime/host/multi"
"github.com/oasisprotocol/oasis-core/go/runtime/host/protocol"
)

Expand Down Expand Up @@ -126,7 +127,7 @@ func (n *RuntimeHostNode) GetHostedRuntimeCapabilityTEEForVersion(version versio
if !ok {
return nil, fmt.Errorf("failed to get RONL component runtime host")
}
rt, err := comp.GetVersion(version)
rt, err := comp.Version(version)
if err != nil {
return nil, err
}
Expand All @@ -138,22 +139,37 @@ func (n *RuntimeHostNode) SetHostedRuntimeVersion(active *version.Version, next
n.mu.Lock()
defer n.mu.Unlock()

pruneVersions := func(comp *multi.Aggregate, lowest version.Version) {
for _, v := range comp.Versions() {
if !v.Less(lowest) {
return
}
_ = comp.RemoveVersion(v)
}
}

for id, comp := range n.host.Components() {
latest, ok := n.rofls[id]
if !ok {
switch latest, ok := n.rofls[id]; ok {
case false:
// RONL components should honor versioning.
comp.SetVersion(active, next)
continue
}

// ROFL components should start when the RONL component starts and
// should upgrade immediately when a new version becomes available.
switch {
case active == nil && next == nil:
comp.SetVersion(nil, nil)
case active == nil && next != nil:
comp.SetVersion(nil, &latest)
default:
comp.SetVersion(&latest, nil)
if active != nil {
pruneVersions(comp, *active)
}
case true:
// ROFL components should start when the RONL component starts and
// should upgrade immediately when a new version becomes available.
switch {
case active == nil && next == nil:
comp.SetVersion(nil, nil)
case active == nil && next != nil:
comp.SetVersion(nil, &latest)
default:
comp.SetVersion(&latest, nil)
}

pruneVersions(comp, latest)
}
}
}
Expand Down
Loading