4
0

suppression.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package alerts
  2. import (
  3. "sync"
  4. )
  5. // SuppressionGraph models router parent-child relationships for alert
  6. // suppression: if a parent router is offline, child router alerts are marked
  7. // as suppressed instead of discarded (issue #332).
  8. type SuppressionGraph struct {
  9. mu sync.RWMutex
  10. parent map[string]string // child -> parent routerId
  11. down map[string]bool // routerId -> currently offline
  12. }
  13. func NewSuppressionGraph() *SuppressionGraph {
  14. return &SuppressionGraph{
  15. parent: map[string]string{},
  16. down: map[string]bool{},
  17. }
  18. }
  19. // SetTopology replaces the parent map with fresh topology data.
  20. // parent maps child routerId to its parent routerId.
  21. func (g *SuppressionGraph) SetTopology(parent map[string]string) {
  22. g.mu.Lock()
  23. defer g.mu.Unlock()
  24. g.parent = make(map[string]string, len(parent))
  25. for k, v := range parent {
  26. g.parent[k] = v
  27. }
  28. }
  29. // MarkDown records a router as offline.
  30. func (g *SuppressionGraph) MarkDown(routerID string) {
  31. g.mu.Lock()
  32. defer g.mu.Unlock()
  33. g.down[routerID] = true
  34. }
  35. // MarkUp clears a router's offline status.
  36. func (g *SuppressionGraph) MarkUp(routerID string) {
  37. g.mu.Lock()
  38. defer g.mu.Unlock()
  39. delete(g.down, routerID)
  40. }
  41. // IsDown reports whether a router is currently offline.
  42. func (g *SuppressionGraph) IsDown(routerID string) bool {
  43. g.mu.RLock()
  44. defer g.mu.RUnlock()
  45. return g.down[routerID]
  46. }
  47. // SuppressedBy returns the routerId suppressing alerts for child, or ""
  48. // if none. It walks up the parent chain: if any ancestor is down, that
  49. // ancestor suppresses the child.
  50. func (g *SuppressionGraph) SuppressedBy(child string) string {
  51. g.mu.RLock()
  52. defer g.mu.RUnlock()
  53. visited := map[string]bool{}
  54. cur := child
  55. for {
  56. p, ok := g.parent[cur]
  57. if !ok || p == "" || visited[p] {
  58. return ""
  59. }
  60. visited[p] = true
  61. if g.down[p] {
  62. return p
  63. }
  64. cur = p
  65. }
  66. }
  67. // DownRouters returns a copy of the current down set.
  68. func (g *SuppressionGraph) DownRouters() []string {
  69. g.mu.RLock()
  70. defer g.mu.RUnlock()
  71. out := make([]string, 0, len(g.down))
  72. for id := range g.down {
  73. out = append(out, id)
  74. }
  75. return out
  76. }