in cmd/amazon-cloudwatch-agent-target-allocator/allocation/consistent_hashing.go [166:221]
func (c *consistentHashingAllocator) SetTargets(targets map[string]*target.Item) {
timer := prometheus.NewTimer(TimeToAssign.WithLabelValues("SetTargets", consistentHashingStrategyName))
defer timer.ObserveDuration()
if c.filter != nil {
targets = c.filter.Apply(targets)
}
RecordTargetsKept(targets)
c.m.Lock()
defer c.m.Unlock()
if len(c.collectors) == 0 {
c.log.Info("No collector instances present, saving targets to allocate to collector(s)")
// If there were no targets discovered previously, assign this as the new set of target items
if len(c.targetItems) == 0 {
c.log.Info("Not discovered any targets previously, saving targets found to the targetItems set")
for k, item := range targets {
c.targetItems[k] = item
}
} else {
// If there were previously discovered targets, add or remove accordingly
targetsDiffEmptyCollectorSet := diff.Maps(c.targetItems, targets)
// Check for additions
if len(targetsDiffEmptyCollectorSet.Additions()) > 0 {
c.log.Info("New targets discovered, adding new targets to the targetItems set")
for k, item := range targetsDiffEmptyCollectorSet.Additions() {
// Do nothing if the item is already there
if _, ok := c.targetItems[k]; ok {
continue
} else {
// Add item to item pool
c.targetItems[k] = item
}
}
}
// Check for deletions
if len(targetsDiffEmptyCollectorSet.Removals()) > 0 {
c.log.Info("Targets removed, Removing targets from the targetItems set")
for k := range targetsDiffEmptyCollectorSet.Removals() {
// Delete item from target items
delete(c.targetItems, k)
}
}
}
return
}
// Check for target changes
targetsDiff := diff.Maps(c.targetItems, targets)
// If there are any additions or removals
if len(targetsDiff.Additions()) != 0 || len(targetsDiff.Removals()) != 0 {
c.handleTargets(targetsDiff)
}
}