Skip to content

Commit

Permalink
fix(comp): prevent divide-by-zero error when calculating array size f…
Browse files Browse the repository at this point in the history
…or sharded targets (#6326)
  • Loading branch information
hainenber authored Feb 8, 2024
1 parent 21324d2 commit 1a035ee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ Main (unreleased)
- Fix an issue with static integrations-next marshaling where non singletons
would cause `/-/config` to fail to marshal. (@erikbaranowski)

- Fix divide-by-zero issue when sharding targets. (@hainenber)

### Other changes

- Removed support for Windows 2012 in line with Microsoft end of life. (@mattdurham)
Expand Down
10 changes: 8 additions & 2 deletions component/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ func (t *DistributedTargets) Get() []Target {
return t.targets
}

res := make([]Target, 0, (len(t.targets)+1)/len(t.cluster.Peers()))
peerCount := len(t.cluster.Peers())
resCap := (len(t.targets) + 1)
if peerCount != 0 {
resCap = (len(t.targets) + 1) / peerCount
}

res := make([]Target, 0, resCap)

for _, tgt := range t.targets {
peers, err := t.cluster.Lookup(shard.StringKey(tgt.NonMetaLabels().String()), 1, shard.OpReadWrite)
Expand All @@ -53,7 +59,7 @@ func (t *DistributedTargets) Get() []Target {
// back to owning the target ourselves.
res = append(res, tgt)
}
if peers[0].Self {
if len(peers) == 0 || peers[0].Self {
res = append(res, tgt)
}
}
Expand Down
10 changes: 8 additions & 2 deletions component/loki/source/podlogs/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ func distributeTargets(c cluster.Cluster, targets []*kubetail.Target) []*kubetai
return targets
}

res := make([]*kubetail.Target, 0, (len(targets)+1)/len(c.Peers()))
peerCount := len(c.Peers())
resCap := len(targets) + 1
if peerCount != 0 {
resCap = (len(targets) + 1) / peerCount
}

res := make([]*kubetail.Target, 0, resCap)

for _, target := range targets {
peers, err := c.Lookup(shard.StringKey(target.Labels().String()), 1, shard.OpReadWrite)
Expand All @@ -141,7 +147,7 @@ func distributeTargets(c cluster.Cluster, targets []*kubetail.Target) []*kubetai
// back to owning the target ourselves.
res = append(res, target)
}
if peers[0].Self {
if len(peers) == 0 || peers[0].Self {
res = append(res, target)
}
}
Expand Down

0 comments on commit 1a035ee

Please sign in to comment.