Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/plan-rev-once.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ftw": patch
---

A replan now moves the plan number the app sees at most once, even with two phones or a phone and a watch connected. Before, two sessions reading the plan at the same moment could move it more than once for one replan.
9 changes: 6 additions & 3 deletions go/cmd/ftw/app_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,14 +552,17 @@ func (a *appPlans) Latest() *mpc.Plan {
// redelivery of the same one. Derived from the plan's own timestamp because
// mpc has no revision of its own and inventing one there would be a field two
// packages had to keep in step.
//
// The plan is read under revMu. Each session calls Rev, and a session that
// read the old plan outside the lock could count it again after another had
// counted the new one, moving the rev twice for one replan.
func (a *appPlans) Rev() uint64 {
a.revMu.Lock()
defer a.revMu.Unlock()
plan := a.Latest()
if plan == nil {
return 0
}

a.revMu.Lock()
defer a.revMu.Unlock()
if plan.GeneratedAtMs != a.lastGen {
a.lastGen = plan.GeneratedAtMs
a.rev++
Expand Down
31 changes: 31 additions & 0 deletions go/cmd/ftw/app_link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/srcfl/ftw/go/internal/config"
"github.com/srcfl/ftw/go/internal/control"
"github.com/srcfl/ftw/go/internal/loadpoint"
"github.com/srcfl/ftw/go/internal/mpc"
"github.com/srcfl/ftw/go/internal/state"
"github.com/srcfl/ftw/go/internal/telemetry"
)
Expand Down Expand Up @@ -469,3 +470,33 @@ func TestAnOfflineSiteMeterSendsNoPhases(t *testing.T) {
t.Fatalf("an offline meter sent phases %v", got)
}
}

// One replan moves the plan's rev once, however many sessions read it. A
// session that read the old plan just before another session counted the new
// one must not count the old plan again when its turn comes.
func TestPlanRevMovesOncePerReplanAcrossSessions(t *testing.T) {
svc := mpc.New(nil, nil, "SE3", mpc.Params{})
svc.InstallPlan(mpc.Plan{GeneratedAtMs: 1_760_000_000_000}, svc.Defaults, "")
plans := &appPlans{planner: svc}
if rev := plans.Rev(); rev != 1 {
t.Fatalf("first plan: rev %d, want 1", rev)
}

// The test holds the counter. A session that reads the plan before it
// takes the counter still has the old plan when it gets it.
plans.revMu.Lock()
slow := make(chan uint64)
go func() { slow <- plans.Rev() }()
time.Sleep(50 * time.Millisecond) // let the slow session reach the counter
svc.InstallPlan(mpc.Plan{GeneratedAtMs: 1_760_000_060_000}, svc.Defaults, "")
// Another session counts the replan, as its own Rev does under the lock.
plans.lastGen, plans.rev = 1_760_000_060_000, 2
plans.revMu.Unlock()

if rev := <-slow; rev != 2 {
t.Errorf("the slow session got rev %d for the replan, want 2", rev)
}
if rev := plans.Rev(); rev != 2 {
t.Errorf("the next read got rev %d with no replan since, want 2", rev)
}
}
Loading