From f46a10f9bda76bff0d3064395e4af78af3833b8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= <2000michal@wp.pl> Date: Mon, 11 Mar 2024 00:44:53 +0100 Subject: [PATCH] refactor(repair): move masterSelector to a separate file This change increases readability. --- pkg/service/repair/generator.go | 41 ----------------------- pkg/service/repair/master_selector.go | 48 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 41 deletions(-) create mode 100644 pkg/service/repair/master_selector.go diff --git a/pkg/service/repair/generator.go b/pkg/service/repair/generator.go index dd21813cc4..e41056cdd9 100644 --- a/pkg/service/repair/generator.go +++ b/pkg/service/repair/generator.go @@ -4,8 +4,6 @@ package repair import ( "context" - "math" - "sort" "strings" "sync/atomic" @@ -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 diff --git a/pkg/service/repair/master_selector.go b/pkg/service/repair/master_selector.go new file mode 100644 index 0000000000..266aab0120 --- /dev/null +++ b/pkg/service/repair/master_selector.go @@ -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 +}