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

Use builtin min function #556

Merged
merged 1 commit into from
Aug 7, 2024
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
5 changes: 2 additions & 3 deletions concurrency/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"go.uber.org/atomic"
"golang.org/x/sync/errgroup"

"github.com/grafana/dskit/internal/math"
"github.com/grafana/dskit/multierror"
)

Expand All @@ -31,7 +30,7 @@ func ForEachUser(ctx context.Context, userIDs []string, concurrency int, userFun
errsMx := sync.Mutex{}

wg := sync.WaitGroup{}
for ix := 0; ix < math.Min(concurrency, len(userIDs)); ix++ {
for ix := 0; ix < min(concurrency, len(userIDs)); ix++ {
wg.Add(1)
go func() {
defer wg.Done()
Expand Down Expand Up @@ -108,7 +107,7 @@ func ForEachJob(ctx context.Context, jobs int, concurrency int, jobFunc func(ctx

// Start workers to process jobs.
g, ctx := errgroup.WithContext(ctx)
for ix := 0; ix < math.Min(concurrency, jobs); ix++ {
for ix := 0; ix < min(concurrency, jobs); ix++ {
g.Go(func() error {
for ctx.Err() == nil {
idx := int(indexes.Inc())
Expand Down
9 changes: 0 additions & 9 deletions internal/math/math.go

This file was deleted.

5 changes: 2 additions & 3 deletions ring/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto"

"github.com/grafana/dskit/flagext"
dsmath "github.com/grafana/dskit/internal/math"
"github.com/grafana/dskit/internal/slices"
"github.com/grafana/dskit/kv"
shardUtil "github.com/grafana/dskit/ring/shard"
Expand Down Expand Up @@ -423,7 +422,7 @@ func (r *Ring) findInstancesForKey(key uint32, op Operation, bufDescs []Instance
distinctHosts = bufHosts[:0]
distinctZones = bufZones[:0]
)
for i := start; len(distinctHosts) < dsmath.Min(maxInstances, n) && len(distinctZones) < maxZones && iterations < len(r.ringTokens); i++ {
for i := start; len(distinctHosts) < min(maxInstances, n) && len(distinctZones) < maxZones && iterations < len(r.ringTokens); i++ {
iterations++
// Wrap i around in the ring.
i %= len(r.ringTokens)
Expand Down Expand Up @@ -528,7 +527,7 @@ func (r *Ring) GetReplicationSetForOperation(op Operation) (ReplicationSet, erro
// Given data is replicated to RF different zones, we can tolerate a number of
// RF/2 failing zones. However, we need to protect from the case the ring currently
// contains instances in a number of zones < RF.
numReplicatedZones := dsmath.Min(len(r.ringZones), r.cfg.ReplicationFactor)
numReplicatedZones := min(len(r.ringZones), r.cfg.ReplicationFactor)
minSuccessZones := (numReplicatedZones / 2) + 1
maxUnavailableZones = minSuccessZones - 1

Expand Down
5 changes: 2 additions & 3 deletions ring/ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/grafana/dskit/concurrency"
"github.com/grafana/dskit/flagext"
"github.com/grafana/dskit/httpgrpc"
dsmath "github.com/grafana/dskit/internal/math"
"github.com/grafana/dskit/internal/slices"
"github.com/grafana/dskit/kv"
"github.com/grafana/dskit/kv/consul"
Expand Down Expand Up @@ -2953,9 +2952,9 @@ func BenchmarkRing_Get(b *testing.B) {
buf, bufHosts, bufZones := MakeBuffersForGet()
r := rand.New(rand.NewSource(time.Now().UnixNano()))

expectedInstances := dsmath.Min(benchCase.numInstances, benchCase.replicationFactor)
expectedInstances := min(benchCase.numInstances, benchCase.replicationFactor)
if ring.cfg.ZoneAwarenessEnabled {
expectedInstances = dsmath.Min(benchCase.numZones, benchCase.replicationFactor)
expectedInstances = min(benchCase.numZones, benchCase.replicationFactor)
}

b.Run(benchName, func(b *testing.B) {
Expand Down