Skip to content

Agent Mesh

controlplane: the node GC sweeps at a tenth of the retention

Antonio Ojeaee896767da17
diff --git a/internal/controlplane/node_gc.go b/internal/controlplane/node_gc.go
index c7a5237a..91529108 100644
--- a/internal/controlplane/node_gc.go
+++ b/internal/controlplane/node_gc.go
@@ -28,11 +28,20 @@ const (
// and is what --node-retention defaults to.
DefaultNodeRetention = 30 * 24 * time.Hour
- // nodeGCInterval is how often the sweep runs. Rows lapse on a scale of
- // days, so nothing is gained by sweeping more often than hourly.
- nodeGCInterval = time.Hour
+ // maxNodeGCInterval bounds how often the sweep runs. Rows lapse on a
+ // scale of days, so nothing is gained by sweeping more often than hourly;
+ // a retention far shorter than that, as a test sets, sweeps at a tenth
+ // of it so a lapsed row does not outlive the retention by much.
+ maxNodeGCInterval = time.Hour
+ minNodeGCInterval = time.Second
)
+// nodeGCInterval is the sweep period for a retention: a tenth of it, within
+// [minNodeGCInterval, maxNodeGCInterval].
+func nodeGCInterval(retention time.Duration) time.Duration {
+ return min(maxNodeGCInterval, max(minNodeGCInterval, retention/10))
+}
+
var nodesDeletedTotal = promauto.NewCounter(prometheus.CounterOpts{
Name: "agentmesh_control_plane_nodes_deleted_total",
Help: "Enrolled node records removed after outliving their session by the retention period",
@@ -45,7 +54,7 @@ func (s *Server) runNodeGCLoop() {
defer s.wg.Done()
s.gcExpiredNodes(time.Now())
- ticker := time.NewTicker(nodeGCInterval)
+ ticker := time.NewTicker(nodeGCInterval(s.config.NodeRetention))
defer ticker.Stop()
for {
select {
diff --git a/internal/controlplane/node_gc_test.go b/internal/controlplane/node_gc_test.go
index f15ca172..58b62e7b 100644
--- a/internal/controlplane/node_gc_test.go
+++ b/internal/controlplane/node_gc_test.go
@@ -91,6 +91,23 @@ func TestNodeRetentionMustBePositive(t *testing.T) {
}
}
+// The sweep runs at a tenth of the retention, no more often than every
+// second and no less often than hourly: the default retention sweeps hourly,
+// a test's retention of seconds sweeps every second.
+func TestNodeGCIntervalFollowsRetention(t *testing.T) {
+ for retention, want := range map[time.Duration]time.Duration{
+ DefaultNodeRetention: time.Hour,
+ 24 * time.Hour: time.Hour,
+ 5 * time.Hour: 30 * time.Minute,
+ time.Second: time.Second,
+ 3 * time.Second: time.Second,
+ } {
+ if got := nodeGCInterval(retention); got != want {
+ t.Errorf("nodeGCInterval(%s) = %s, want %s", retention, got, want)
+ }
+ }
+}
+
// A caller that leaves NodeRetention unset, such as agentmesh-one, still
// reclaims rows whose session lapsed more than DefaultNodeRetention ago.
func TestGCExpiredNodesDefaultsRetention(t *testing.T) {