Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(comp): prevent divide-by-zero error when calculating array size for sharded targets #6326

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,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
Loading