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
1 change: 1 addition & 0 deletions changelog.d/fixed-16002-purge-activity-on-hive-delete.md
Original file line number Diff line number Diff line change
@@ -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).
11 changes: 11 additions & 0 deletions src/pkg/hub/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions src/pkg/hub/activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
10 changes: 10 additions & 0 deletions src/pkg/hub/delete_hive_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
15 changes: 13 additions & 2 deletions src/pkg/hub/hub_coverage_boost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/pkg/hub/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Loading