-
Notifications
You must be signed in to change notification settings - Fork 142
/
costs.go
174 lines (144 loc) · 4.12 KB
/
costs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package xt
import (
"math"
cmap "github.com/orcaman/concurrent-map/v2"
)
const (
unknownMinCost = math.MaxUint16 * 12
failedMinCost = math.MaxUint16 * 8
defaultMinCost = math.MaxUint16 * 4
requireMinCost = 0
)
var globalCosts = &costs{
costMap: cmap.New[uint16](),
}
func GlobalCosts() Costs {
return globalCosts
}
type precedence struct {
name string
minCost uint32
maxCost uint32
}
func (p *precedence) String() string {
return p.name
}
func (p *precedence) Unbias(cost uint32) uint32 {
return cost - p.minCost
}
func (p *precedence) IsFailed() bool {
return p.minCost == failedMinCost
}
func (p *precedence) IsDefault() bool {
return p.minCost == defaultMinCost
}
func (p *precedence) IsRequired() bool {
return p.minCost == requireMinCost
}
func (p *precedence) getMinCost() uint32 {
return p.minCost
}
func (p *precedence) getMaxCost() uint32 {
return p.maxCost
}
func (p *precedence) GetBiasedCost(cost uint32) uint32 {
result := p.getMinCost() + cost
if result > p.maxCost {
return p.maxCost
}
return result
}
// Precedences define the precedence levels
var Precedences = struct {
// Required terminators should always be used in preference to non-required terminators
// Example: The current active node in an active/passive setup. If the strategy determines
// that the current active has failed, it will drop the precedence and mark some other
// terminator as Required
Required Precedence
// Default precedence is where terminators start
Default Precedence
// Failed means this terminator should only be used if all other terminators are also in a failed state
// Example: A strategy might move a terminator to Failed if three dials in a row fail
Failed Precedence
// Unknown means this terminator was likely recently removed and should not be used
unknown Precedence
}{
Required: &precedence{
name: "required",
minCost: requireMinCost,
maxCost: defaultMinCost - 1,
},
Default: &precedence{
name: "default",
minCost: defaultMinCost,
maxCost: failedMinCost - 1,
},
Failed: &precedence{
name: "failed",
minCost: failedMinCost,
maxCost: unknownMinCost - 1,
},
unknown: &precedence{
name: "unknown",
minCost: unknownMinCost,
maxCost: unknownMinCost + (math.MaxUint16 * 4) - 1,
},
}
func GetPrecedenceForName(name string) Precedence {
if Precedences.Required.String() == name {
return Precedences.Required
}
if Precedences.Failed.String() == name {
return Precedences.Failed
}
return Precedences.Default
}
type costs struct {
costMap cmap.ConcurrentMap[string, uint16]
}
func (self *costs) ClearCost(terminatorId string) {
self.costMap.Remove(terminatorId)
}
func (self *costs) SetDynamicCost(terminatorId string, cost uint16) {
self.costMap.Set(terminatorId, cost)
}
func (self *costs) UpdateDynamicCost(terminatorId string, updateF func(uint16) uint16) {
self.costMap.Upsert(terminatorId, 0, func(exist bool, valueInMap uint16, newValue uint16) uint16 {
if !exist {
return updateF(0)
}
return updateF(valueInMap)
})
}
func (self *costs) GetDynamicCost(terminatorId string) uint16 {
if cost, found := self.costMap.Get(terminatorId); found {
return cost
}
return 0
}
// In a list which is sorted by precedence, returns the terminators which have the
// same precedence as that of the first entry in the list
func GetRelatedTerminators(list []CostedTerminator) []CostedTerminator {
first := list[0]
var result = []CostedTerminator{first}
for _, t := range list[1:] {
if t.GetPrecedence() == first.GetPrecedence() {
result = append(result, t)
} else {
break
}
}
return result
}