diff --git a/changelog.d/fixed-16002-purge-activity-on-hive-delete.md b/changelog.d/fixed-16002-purge-activity-on-hive-delete.md new file mode 100644 index 0000000000..69ad3d6576 --- /dev/null +++ b/changelog.d/fixed-16002-purge-activity-on-hive-delete.md @@ -0,0 +1 @@ +- Removing a hive from the hub registry now also drops its in-memory activity digest instead of keeping it for up to 24 hours (#16002). diff --git a/src/pkg/hub/activity.go b/src/pkg/hub/activity.go index c5826b4867..abe58319e5 100644 --- a/src/pkg/hub/activity.go +++ b/src/pkg/hub/activity.go @@ -115,6 +115,17 @@ func (st *activityStore) record(hiveID string, block *hubactivity.Digest, now ti return clean.ETag } +// forget drops a hive's held digest, e.g. when the hive leaves the registry. +// Unknown ids and a nil store are no-ops. +func (st *activityStore) forget(hiveID string) { + if st == nil { + return + } + st.mu.Lock() + defer st.mu.Unlock() + delete(st.byHive, hiveID) +} + func (st *activityStore) forgetOldLocked(now time.Time) { for id, a := range st.byHive { if now.Sub(a.receivedAt) > activityForgetAfter { diff --git a/src/pkg/hub/activity_test.go b/src/pkg/hub/activity_test.go index f5a95f05d6..d7de543655 100644 --- a/src/pkg/hub/activity_test.go +++ b/src/pkg/hub/activity_test.go @@ -501,3 +501,21 @@ func TestSpokeActivityWireCompat(t *testing.T) { t.Fatalf("spoke did not decode the ack: %+v err=%v", ack, err) } } + +func TestActivityStoreForget(t *testing.T) { + var st activityStore + st.record("hive-a", activityDigest("o/r", 1), activityT0) + st.record("hive-b", activityDigest("o/r", 2), activityT0) + + st.forget("hive-a") + if got := st.snapshot([]string{"hive-a"}); len(got) != 0 { + t.Errorf("forgotten hive still held: %v", got) + } + if got := st.snapshot([]string{"hive-b"}); len(got) != 1 { + t.Errorf("forget dropped an unrelated hive: %v", got) + } + + st.forget("unknown") + var nilStore *activityStore + nilStore.forget("hive-a") +} diff --git a/src/pkg/hub/delete_hive_registry_test.go b/src/pkg/hub/delete_hive_registry_test.go index 71c09103fd..f0b4fa36ac 100644 --- a/src/pkg/hub/delete_hive_registry_test.go +++ b/src/pkg/hub/delete_hive_registry_test.go @@ -423,8 +423,18 @@ func TestRemoveRegistryEntryPersistsSynchronously(t *testing.T) { t.Fatalf("seed registry: %v", err) } + srv.activity.record(dropID, activityDigest("o/r", 1), time.Now().UTC()) + srv.activity.record(keepID, activityDigest("o/r", 2), time.Now().UTC()) + srv.removeRegistryEntry(dropID, "tester") + if got := srv.activity.snapshot([]string{dropID}); len(got) != 0 { + t.Error("removeRegistryEntry left the removed hive's activity digest in memory") + } + if got := srv.activity.snapshot([]string{keepID}); len(got) != 1 { + t.Error("removeRegistryEntry purged an unrelated hive's activity digest") + } + // No sleep: the write must already have happened. if helperPersistedRegistryHasHive(t, dropID) { t.Error("removeRegistryEntry did not flush the removal to disk before returning") diff --git a/src/pkg/hub/hub_coverage_boost_test.go b/src/pkg/hub/hub_coverage_boost_test.go index 15fb09e930..1a1823aa03 100644 --- a/src/pkg/hub/hub_coverage_boost_test.go +++ b/src/pkg/hub/hub_coverage_boost_test.go @@ -911,8 +911,11 @@ func TestHandleRegistryDeleteAsAdmin(t *testing.T) { srv.mu.Lock() srv.registry.Hives = []RegistryEntry{ {ID: "delete-me", Online: true}, + {ID: "keep-me", Online: true}, } srv.mu.Unlock() + srv.activity.record("delete-me", activityDigest("o/r", 1), time.Now().UTC()) + srv.activity.record("keep-me", activityDigest("o/r", 2), time.Now().UTC()) mux := http.NewServeMux() mux.HandleFunc("DELETE /api/hub/registry/{id}", srv.handleRegistryDelete) @@ -927,10 +930,18 @@ func TestHandleRegistryDeleteAsAdmin(t *testing.T) { } srv.mu.RLock() - if len(srv.registry.Hives) != 0 { - t.Error("hive should have been removed") + if len(srv.registry.Hives) != 1 || srv.registry.Hives[0].ID != "keep-me" { + t.Errorf("expected only keep-me to remain, got %+v", srv.registry.Hives) } srv.mu.RUnlock() + + // The admin path must purge the activity digest too (#16002). + if got := srv.activity.snapshot([]string{"delete-me"}); len(got) != 0 { + t.Error("handleRegistryDelete left the removed hive's activity digest in memory") + } + if got := srv.activity.snapshot([]string{"keep-me"}); len(got) != 1 { + t.Error("handleRegistryDelete purged an unrelated hive's activity digest") + } } func TestHandleRegistryDeleteNotFound(t *testing.T) { diff --git a/src/pkg/hub/server.go b/src/pkg/hub/server.go index 41afe151ce..0d2686b9ed 100644 --- a/src/pkg/hub/server.go +++ b/src/pkg/hub/server.go @@ -4280,6 +4280,8 @@ func (s *HubServer) removeRegistryEntry(id, by string) { // with it. An uncollectible hive is by definition never armed, so this is // the ONLY path that can ever retire its entry (see forgetUncollectibleUpgrade). s.forgetUncollectibleUpgrade(id) + // Memory-only activity digests must not outlive the hive (#16002). + s.activity.forget(id) s.mu.Lock() removed := false for i, h := range s.registry.Hives { @@ -4344,6 +4346,8 @@ func (s *HubServer) handleRegistryDelete(w http.ResponseWriter, r *http.Request) } id := r.PathValue("id") s.forgetUncollectibleUpgrade(id) + // Memory-only activity digests must not outlive the hive (#16002). + s.activity.forget(id) s.mu.Lock() removed := false for i, h := range s.registry.Hives {