Skip to content

Commit d56f83c

Browse files
committed
o/ifacestate: add some comments and rename some functions to hopefully help reading
1 parent d0bd8c6 commit d56f83c

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

overlord/ifacestate/handlers.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ func (d delayedEffectsForSnaps) EnqueueFor(snapName affectedSnap, backend interf
337337
d[snapName][backend] = append(d[snapName][backend], item)
338338
}
339339

340+
// refreshAppSetConnections refreshes repository connections for appSet and, on
341+
// the setup-profiles do path, records undo data for persisted connection state
342+
// that reloadConnections changed or dropped.
340343
func (m *InterfaceManager) refreshAppSetConnections(task *state.Task, appSet *interfaces.SnapAppSet) ([]string, []string, error) {
341344
snapInfo := appSet.Info()
342345
snapName := appSet.InstanceName()
@@ -369,16 +372,16 @@ func (m *InterfaceManager) refreshAppSetConnections(task *state.Task, appSet *in
369372
task.Logf("%s", snap.BadInterfacesSummary(snapInfo))
370373
}
371374

372-
reloadedConns, changedConns, err := m.reloadConnections(snapName)
375+
reloadedConns, changedOrDroppedConns, err := m.reloadConnections(snapName)
373376
if err != nil {
374377
return nil, nil, err
375378
}
376379

377-
// if this task modified any connection states, we take a snapshot of the
378-
// original connections so that we can restore them on the undo path, if
380+
// if this task modified any connection states, take a snapshot of the
381+
// original connections so that setup-profiles' undo can restore them, if
379382
// needed
380383
if task.Status() != state.UndoingStatus {
381-
if err := snapshotChangedConnectionsForUndo(task, snapName, changedConns); err != nil {
384+
if err := snapshotChangedConnectionsForUndo(task, snapName, changedOrDroppedConns); err != nil {
382385
return nil, nil, err
383386
}
384387
}
@@ -706,6 +709,10 @@ func (m *InterfaceManager) undoSetupProfiles(task *state.Task, tomb *tomb.Tomb)
706709
if err := snapstateFinishRestart(task, snapsup, finishOpts); err != nil {
707710
return err
708711
}
712+
713+
// restore any connection state snapshot saved by refreshAppSetConnections on
714+
// the original setup-profiles do path before rebuilding profiles for the old
715+
// revision
709716
if err := restoreConnectionsForSetupProfiles(task); err != nil {
710717
return err
711718
}

overlord/ifacestate/helpers.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,9 @@ func cloneConnState(connState *schema.ConnState) *schema.ConnState {
444444
return &clone
445445
}
446446

447-
// snapshotChangedConnectionsForUndo records original states for connections
448-
// changed by setup-profiles so undo can restore them, if needed.
447+
// snapshotChangedConnectionsForUndo records original states for persisted
448+
// connections that setup-profiles changed or dropped so undo can restore them,
449+
// if needed.
449450
func snapshotChangedConnectionsForUndo(task *state.Task, instanceName string, changedConns map[string]*schema.ConnState) error {
450451
if len(changedConns) == 0 {
451452
return nil
@@ -458,7 +459,7 @@ func snapshotChangedConnectionsForUndo(task *state.Task, instanceName string, ch
458459
}
459460

460461
var connectionSnapshot map[string]*schema.ConnState
461-
err := task.Get("changed-connection-snapshot", &connectionSnapshot)
462+
err := task.Get("changed-or-dropped-connection-snapshot", &connectionSnapshot)
462463
if err != nil && !errors.Is(err, state.ErrNoState) {
463464
return err
464465
}
@@ -475,16 +476,16 @@ func snapshotChangedConnectionsForUndo(task *state.Task, instanceName string, ch
475476
connectionSnapshot[connID] = connState
476477
}
477478

478-
task.Set("changed-connection-snapshot", connectionSnapshot)
479+
task.Set("changed-or-dropped-connection-snapshot", connectionSnapshot)
479480

480481
return nil
481482
}
482483

483-
// restoreConnectionsForSetupProfiles restores connection states saved on a
484-
// setup-profiles task.
484+
// restoreConnectionsForSetupProfiles restores connection states saved by
485+
// snapshotChangedConnectionsForUndo on a setup-profiles task.
485486
func restoreConnectionsForSetupProfiles(task *state.Task) error {
486487
var connectionSnapshot map[string]*schema.ConnState
487-
err := task.Get("changed-connection-snapshot", &connectionSnapshot)
488+
err := task.Get("changed-or-dropped-connection-snapshot", &connectionSnapshot)
488489
if errors.Is(err, state.ErrNoState) {
489490
return nil
490491
}
@@ -512,8 +513,12 @@ func restoreConnectionsForSetupProfiles(task *state.Task) error {
512513
// affecting a given snap.
513514
//
514515
// The return value is the list of reloaded connection IDs, plus the original
515-
// connection states whose persisted state was changed.
516-
func (m *InterfaceManager) reloadConnections(snapName string) (reloadedConnectionIDs []string, changedConns map[string]*schema.ConnState, err error) {
516+
// connection states whose persisted state was changed or dropped.
517+
func (m *InterfaceManager) reloadConnections(snapName string) (
518+
reloadedConnectionIDs []string,
519+
changedOrDroppedConns map[string]*schema.ConnState,
520+
err error,
521+
) {
517522
conns, err := getConns(m.state)
518523
if err != nil {
519524
return nil, nil, err
@@ -542,7 +547,7 @@ func (m *InterfaceManager) reloadConnections(snapName string) (reloadedConnectio
542547
}
543548

544549
connStateChanged := false
545-
changedConns = make(map[string]*schema.ConnState)
550+
changedOrDroppedConns = make(map[string]*schema.ConnState)
546551

547552
var reloadedConnections []string
548553
ConnsLoop:
@@ -587,7 +592,7 @@ ConnsLoop:
587592
continue ConnsLoop
588593
}
589594
}
590-
changedConns[connId] = cloneConnState(connState)
595+
changedOrDroppedConns[connId] = cloneConnState(connState)
591596
delete(conns, connId)
592597
connStateChanged = true
593598
}
@@ -652,7 +657,7 @@ ConnsLoop:
652657
reloadedConnections = append(reloadedConnections, connId)
653658

654659
if updateStaticAttrs {
655-
changedConns[connId] = cloneConnState(connState)
660+
changedOrDroppedConns[connId] = cloneConnState(connState)
656661
connState.StaticPlugAttrs = staticPlugAttrs
657662
connState.StaticSlotAttrs = staticSlotAttrs
658663
connStateChanged = true
@@ -663,7 +668,7 @@ ConnsLoop:
663668
setConns(m.state, conns)
664669
}
665670

666-
return reloadedConnections, changedConns, nil
671+
return reloadedConnections, changedOrDroppedConns, nil
667672
}
668673

669674
// removeConnections disconnects all connections of the snap in the repo. It should only be used if the snap

0 commit comments

Comments
 (0)