Skip to content

Commit

Permalink
feat: Harvest should not simultaneously publish quota metrics from Qt… (
Browse files Browse the repository at this point in the history
#3067)

* feat: Harvest should not simultaneously publish quota metrics from Qtree and Quota templates

* feat: Harvest should not simultaneously publish quota metrics from Qtree and Quota templates
  • Loading branch information
cgrinds authored Jul 26, 2024
1 parent 1c5f18e commit 3e6e0bb
Show file tree
Hide file tree
Showing 9 changed files with 868 additions and 12 deletions.
59 changes: 55 additions & 4 deletions cmd/poller/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import (
"os/signal"
"regexp"
"runtime"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -301,10 +302,7 @@ func (p *Poller) Init() error {
}

// for each object, only allow one of config & perf collectors to start
uniqueOCs := make([]objectCollector, 0, len(objectsToCollectors))
for _, collectors := range objectsToCollectors {
uniqueOCs = append(uniqueOCs, nonOverlappingCollectors(collectors)...)
}
uniqueOCs := uniquifyObjectCollectors(objectsToCollectors)

// start the uniqueified collectors
err = p.loadCollectorObject(uniqueOCs)
Expand Down Expand Up @@ -392,6 +390,59 @@ func (p *Poller) Init() error {

}

func uniquifyObjectCollectors(objectsToCollectors map[string][]objectCollector) []objectCollector {
uniqueOCs := make([]objectCollector, 0, len(objectsToCollectors))

specialCaseQtree(objectsToCollectors)

for _, collectors := range objectsToCollectors {
uniqueOCs = append(uniqueOCs, nonOverlappingCollectors(collectors)...)
}

return uniqueOCs
}

func specialCaseQtree(objectsToCollectors map[string][]objectCollector) {
// ZAPI Qtree also publishes quota metrics. This means that if ZAPI Qtree
// appears earlier in the slice than REST Qtree, REST Quota should be
// disabled to prevent both ZAPI Qtree and REST Quota from publishing
// quota metrics simultaneously.

qtreeCollectors := objectsToCollectors["Qtree"]
quotaCollectors := objectsToCollectors["Quota"]
zapiQtreeWillRun := false

if len(quotaCollectors) == 0 {
return
}

qtreeNoOverlaps := nonOverlappingCollectors(qtreeCollectors)
for _, oc := range qtreeNoOverlaps {
if oc.class == "Zapi" {
zapiQtreeWillRun = true
break
}
}

if !zapiQtreeWillRun {
return
}

// Disable REST Quota, if it is enabled
quotaNoOverlaps := nonOverlappingCollectors(quotaCollectors)
deleteIndex := -1
for i, oc := range quotaNoOverlaps {
if oc.class == "Rest" {
deleteIndex = i
break
}
}
if deleteIndex != -1 {
quotaNoOverlaps = slices.Delete(quotaNoOverlaps, deleteIndex, deleteIndex+1)
objectsToCollectors["Quota"] = quotaNoOverlaps
}
}

func (p *Poller) firstAutoSupport() {
if p.collectors == nil {
return
Expand Down
49 changes: 49 additions & 0 deletions cmd/poller/poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"errors"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/tree/node"
"os"
"strings"
"testing"
)

Expand Down Expand Up @@ -210,3 +212,50 @@ func ocs(names ...string) []objectCollector {
}
return collectors
}

func Test_uniquifyObjectCollectors(t *testing.T) {
tests := []struct {
name string
args map[string][]objectCollector
want []objectCollector
}{
{name: "empty", args: make(map[string][]objectCollector), want: []objectCollector{}},
{name: "volume-rest", args: objectCollectorMap("Volume: Rest, Zapi"), want: []objectCollector{{class: "Rest", object: "Volume"}}},
{name: "qtree-rest", args: objectCollectorMap("Qtree: Rest, Zapi"), want: []objectCollector{{class: "Rest", object: "Qtree"}}},
{name: "qtree-zapi", args: objectCollectorMap("Qtree: Zapi, Rest"), want: []objectCollector{{class: "Zapi", object: "Qtree"}}},
{name: "qtree-rest-quota", args: objectCollectorMap("Qtree: Rest, Zapi", "Quota: Rest"),
want: []objectCollector{{class: "Rest", object: "Qtree"}, {class: "Rest", object: "Quota"}}},
{name: "qtree-zapi-disable-quota", args: objectCollectorMap("Qtree: Zapi, Rest", "Quota: Rest"),
want: []objectCollector{{class: "Zapi", object: "Qtree"}}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := uniquifyObjectCollectors(tt.args)

diff := cmp.Diff(got, tt.want, cmp.AllowUnexported(objectCollector{}), cmpopts.SortSlices(func(a, b objectCollector) bool {
return a.class+a.object < b.class+b.object
}))

if diff != "" {
t.Errorf("Mismatch (-got +want):\n%s", diff)
}
})
}
}

func objectCollectorMap(constructors ...string) map[string][]objectCollector {
objectsToCollectors := make(map[string][]objectCollector)

for _, template := range constructors {
before, after, _ := strings.Cut(template, ":")
object := before
classes := strings.Split(after, ",")
for _, class := range classes {
class := strings.TrimSpace(class)
objectsToCollectors[object] = append(objectsToCollectors[object], objectCollector{class: class, object: object})
}
}

return objectsToCollectors
}
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
github.com/zekroTJA/timedmap/v2 v2.0.0 h1:Bo9oq8AExd0GuDFbcPXm3xoidUAtrnNsZN1d1Hc3PvY=
github.com/zekroTJA/timedmap/v2 v2.0.0/go.mod h1:xHDLg687zASqLBJqoysF+WORHxL/kYNphVD36CRJxhM=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
golang.org/x/exp v0.0.0-20240707233637-46b078467d37 h1:uLDX+AfeFCct3a2C7uIWBKMJIR3CJMhcgfrUAqjRK6w=
golang.org/x/exp v0.0.0-20240707233637-46b078467d37/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -88,12 +84,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
Expand Down
185 changes: 185 additions & 0 deletions vendor/github.com/google/go-cmp/cmp/cmpopts/equate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3e6e0bb

Please sign in to comment.