Skip to content

Commit

Permalink
refactor(repair): move masterSelector to a separate file
Browse files Browse the repository at this point in the history
This change increases readability.
  • Loading branch information
Michal-Leszczynski committed Mar 13, 2024
1 parent 15f4226 commit f46a10f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 41 deletions.
41 changes: 0 additions & 41 deletions pkg/service/repair/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package repair

import (
"context"
"math"
"sort"
"strings"
"sync/atomic"

Expand All @@ -14,47 +12,8 @@ import (
"github.com/scylladb/go-set/strset"
"github.com/scylladb/scylla-manager/v3/pkg/dht"
"github.com/scylladb/scylla-manager/v3/pkg/scyllaclient"
"github.com/scylladb/scylla-manager/v3/pkg/util/slice"
)

// masterSelector describes each host priority for being repair master.
// Repair master is first chosen by smallest shard count,
// then by smallest dc RTT from SM.
type masterSelector map[string]int

func newMasterSelector(shards map[string]uint, hostDC map[string]string, closestDC []string) masterSelector {
hosts := make([]string, 0, len(shards))
for h := range shards {
hosts = append(hosts, h)
}

sort.Slice(hosts, func(i, j int) bool {
if shards[hosts[i]] != shards[hosts[j]] {
return shards[hosts[i]] < shards[hosts[j]]
}
return slice.Index(closestDC, hostDC[hosts[i]]) < slice.Index(closestDC, hostDC[hosts[j]])
})

ms := make(masterSelector)
for i, h := range hosts {
ms[h] = i
}
return ms
}

// Select returns repair master from replica set.
func (ms masterSelector) Select(replicas []string) string {
var master string
p := math.MaxInt64
for _, r := range replicas {
if ms[r] < p {
p = ms[r]
master = r
}
}
return master
}

type submitter[T, R any] interface {
Submit(task T)
Results() chan R
Expand Down
48 changes: 48 additions & 0 deletions pkg/service/repair/master_selector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (C) 2024 ScyllaDB

package repair

import (
"math"
"sort"

"github.com/scylladb/scylla-manager/v3/pkg/util/slice"
)

// masterSelector describes each host priority for being repair master.
// Repair master is first chosen by smallest shard count,
// then by smallest dc RTT from SM.
type masterSelector map[string]int

func newMasterSelector(shards map[string]uint, hostDC map[string]string, closestDC []string) masterSelector {
hosts := make([]string, 0, len(shards))
for h := range shards {
hosts = append(hosts, h)
}

sort.Slice(hosts, func(i, j int) bool {
if shards[hosts[i]] != shards[hosts[j]] {
return shards[hosts[i]] < shards[hosts[j]]
}
return slice.Index(closestDC, hostDC[hosts[i]]) < slice.Index(closestDC, hostDC[hosts[j]])
})

ms := make(masterSelector)
for i, h := range hosts {
ms[h] = i
}
return ms
}

// Select returns repair master from replica set.
func (ms masterSelector) Select(replicas []string) string {
var master string
p := math.MaxInt64
for _, r := range replicas {
if ms[r] < p {
p = ms[r]
master = r
}
}
return master
}

0 comments on commit f46a10f

Please sign in to comment.