Debian Codegraph @

etcd/scheduler/rrstrategy.go

raw symbols SCIP
1 // Mgmt
2 // Copyright (C) 2013-2024+ James Shubin and the project contributors
3 // Written by James Shubin <[email protected]> and the project contributors
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <https://www.gnu.org/licenses/>.
17 //
18 // Additional permission under GNU GPL version 3 section 7
19 //
20 // If you modify this program, or any covered work, by linking or combining it
21 // with embedded mcl code and modules (and that the embedded mcl code and
22 // modules which link with this program, contain a copy of their source code in
23 // the authoritative form) containing parts covered by the terms of any other
24 // license, the licensors of this program grant you additional permission to
25 // convey the resulting work. Furthermore, the licensors of this program grant
26 // the original author, James Shubin, additional permission to update this
27 // additional permission if he deems it necessary to achieve the goals of this
28 // additional permission.
29
30 package schedulerscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/ // TODO: i'd like this to be a separate package, but cycles!
31
32 import (
33 "fmtscip-go gomod github.com/golang/go/src go1.21 fmt/"
34 "sortscip-go gomod github.com/golang/go/src go1.21 sort/"
35
36 "github.com/purpleidea/mgmt/utilscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util`/"
37 )
38
39 func initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/init().() {
40 Registerscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/Register().("rr", func() Strategyscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/Strategy# { return &rrStrategyscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#{} }) // must register the func and name
41 }
42
43 type rrStrategyscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy# struct {
44 // some stored state
45 hostsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#hosts. []string
46 }
47
48 // Schedule returns hosts in round robin style from the available hostnames.
49 func (obj *rrStrategyscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#) Schedulescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#Schedule().(hostnames map[string]string, opts *schedulerOptionsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/schedulerOptions#) ([]string, error) {
50 if len(hostnames) <= 0 {
51 return nil, fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("strategy: cannot schedule from zero hosts")
52 }
53 if opts.maxCountscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/schedulerOptions#maxCount. <= 0 {
54 return nil, fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("strategy: cannot schedule with a max of zero")
55 }
56
57 // always get a deterministic list of current hosts first...
58 sortedHosts := []string{}
59 for key := range hostnames {
60 sortedHosts = append(sortedHosts, key)
61 }
62 sortscip-go gomod github.com/golang/go/src go1.21 sort/.Stringsscip-go gomod github.com/golang/go/src go1.21 sort/Strings().(sortedHosts)
63
64 if obj.hostsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#hosts. == nil {
65 obj.hostsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#hosts. = []string{} // initialize if needed
66 }
67
68 // add any new hosts we learned about, to the end of the list
69 for _, x := range sortedHosts {
70 if !utilscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util`/.StrInListscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util`/StrInList().(x, obj.hostsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#hosts.) {
71 obj.hostsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#hosts. = append(obj.hostsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#hosts., x)
72 }
73 }
74
75 // remove any hosts we previouly knew about from the list
76 for ix := len(obj.hostsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#hosts.) - 1; ix >= 0; ix-- {
77 if !utilscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util`/.StrInListscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util`/StrInList().(obj.hostsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#hosts.[ix], sortedHosts) {
78 // delete entry at this index
79 obj.hostsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#hosts. = append(obj.hostsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#hosts.[:ix], obj.hostsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#hosts.[ix+1:]...)
80 }
81 }
82
83 // get the maximum number of hosts to return
84 max := len(obj.hostsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#hosts.) // can't return more than we have
85 if opts.maxCountscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/schedulerOptions#maxCount. < max { // found a smaller limit
86 max = opts.maxCountscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/schedulerOptions#maxCount.
87 }
88
89 result := []string{}
90 // now return the number of needed hosts from the list
91 for i := 0; i < max; i++ {
92 result = append(result, obj.hostsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/rrStrategy#hosts.[i])
93 }
94
95 return result, nil
96 }