Skip to content

Commit e40c7ed

Browse files
compute-domain-daemon: refine SIGUSR1 logic and add unit tests
Signed-off-by: AkshatDudeja77 <akshat.dudeja77@gmail.com>
1 parent a7ddddf commit e40c7ed

File tree

2 files changed

+103
-14
lines changed

2 files changed

+103
-14
lines changed

cmd/compute-domain-daemon/main.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,20 @@ func IMEXDaemonUpdateLoopWithIPs(ctx context.Context, controller *Controller, cl
359359
}
360360
}
361361

362+
func shouldSendSIGUSR1(oldIPs, newIPs map[string]struct{}, updated, fresh bool) bool {
363+
if !updated || fresh {
364+
return false
365+
}
366+
367+
for ip := range newIPs {
368+
if _, existed := oldIPs[ip]; !existed {
369+
return true
370+
}
371+
}
372+
373+
return false
374+
}
375+
362376
// IMEXDaemonUpdateLoopWithDNSNames reacts to ComputeDomain status changes by
363377
// updating the /etc/hosts file with IP to DNS name mappings. This relies on
364378
// the IMEX daemon to pick up these changes automatically (and quickly) --
@@ -395,20 +409,7 @@ func IMEXDaemonUpdateLoopWithDNSNames(ctx context.Context, controller *Controlle
395409

396410
dnsNameManager.LogDNSNameMappings()
397411

398-
onlyRemovals := true
399-
for ip := range dnsNameManager.ipToDNSName {
400-
if _, existed := oldIPs[ip]; !existed {
401-
onlyRemovals = false
402-
break
403-
}
404-
}
405-
406-
// Skip sending SIGUSR1 when the process is fresh (has newly been
407-
// created) or when this was a noop update. TODO: review skipping
408-
// this also if the new set of IP addresses only strictly removes
409-
// addresses compared to the old set (then we don't need to force
410-
// the daemon to re-resolve & re-connect).
411-
if !updated || fresh || onlyRemovals {
412+
if !shouldSendSIGUSR1(oldIPs, dnsNameManager.ipToDNSName, updated, fresh) {
412413
break
413414
}
414415

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package main
2+
3+
import "testing"
4+
5+
func set(items ...string) map[string]struct{} {
6+
m := make(map[string]struct{}, len(items))
7+
for _, i := range items {
8+
m[i] = struct{}{}
9+
}
10+
return m
11+
}
12+
13+
func TestShouldSendSIGUSR1(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
old map[string]struct{}
17+
new map[string]struct{}
18+
updated bool
19+
fresh bool
20+
want bool
21+
}{
22+
{
23+
name: "no change, not updated",
24+
old: set("A", "B", "C"),
25+
new: set("A", "B", "C"),
26+
updated: false,
27+
fresh: false,
28+
want: false,
29+
},
30+
{
31+
name: "no change, updated",
32+
old: set("A", "B", "C"),
33+
new: set("A", "B", "C"),
34+
updated: true,
35+
fresh: false,
36+
want: false,
37+
},
38+
{
39+
name: "pure removal",
40+
old: set("A", "B", "C"),
41+
new: set("A", "B"),
42+
updated: true,
43+
fresh: false,
44+
want: false,
45+
},
46+
{
47+
name: "addition",
48+
old: set("A", "B"),
49+
new: set("A", "B", "C"),
50+
updated: true,
51+
fresh: false,
52+
want: true,
53+
},
54+
{
55+
name: "replacement same size",
56+
old: set("A", "B", "C"),
57+
new: set("A", "B", "D"),
58+
updated: true,
59+
fresh: false,
60+
want: true,
61+
},
62+
{
63+
name: "remove and add",
64+
old: set("A", "B", "C"),
65+
new: set("A", "D"),
66+
updated: true,
67+
fresh: false,
68+
want: true,
69+
},
70+
{
71+
name: "fresh process",
72+
old: set("A", "B"),
73+
new: set("A", "B", "C"),
74+
updated: true,
75+
fresh: true,
76+
want: false,
77+
},
78+
}
79+
80+
for _, tt := range tests {
81+
t.Run(tt.name, func(t *testing.T) {
82+
got := shouldSendSIGUSR1(tt.old, tt.new, tt.updated, tt.fresh)
83+
if got != tt.want {
84+
t.Fatalf("got %v, want %v", got, tt.want)
85+
}
86+
})
87+
}
88+
}

0 commit comments

Comments
 (0)