forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
contention.go
281 lines (246 loc) · 8.38 KB
/
contention.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package controller
import (
"sync"
"time"
"github.com/golang/glog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
routeapi "github.com/openshift/origin/pkg/route/apis/route"
)
// ContentionTracker records modifications to a particular entry to prevent endless
// loops when multiple routers are configured with conflicting info. A given router
// process tracks whether the ingress status is change from a correct value to any
// other value (by invoking IsContended when the state has diverged).
type ContentionTracker interface {
// IsContended should be invoked when the state of the object in storage differs
// from the desired state. It will return true if the provided id was recently
// reset from the correct state to an incorrect state. The current ingress is the
// expected state of the object at this time and may be used by the tracker to
// determine if the most recent update was a contention. This method does not
// update the state of the tracker.
IsChangeContended(id string, now time.Time, current *routeapi.RouteIngress) bool
// Clear informs the tracker that the provided ingress state was confirmed to
// match the current state of this process. If a subsequent call to IsChangeContended
// is made within the expiration window, the object will be considered as contended.
Clear(id string, current *routeapi.RouteIngress)
}
type elementState int
const (
stateCandidate elementState = iota
stateContended
)
type trackerElement struct {
at time.Time
state elementState
last *routeapi.RouteIngress
}
// SimpleContentionTracker tracks whether a given identifier is changed from a correct
// state (set by Clear) to an incorrect state (inferred by calling IsContended).
type SimpleContentionTracker struct {
informer cache.SharedInformer
routerName string
expires time.Duration
// maxContentions is the number of contentions detected before the entire
maxContentions int
message string
lock sync.Mutex
contentions int
ids map[string]trackerElement
}
// NewSimpleContentionTracker creates a ContentionTracker that will prevent writing
// to the same route more often than once per interval. A background process will
// periodically flush old entries (at twice interval) in order to prevent the list
// growing unbounded if routes are created and deleted frequently. The informer
// detects changes to ingress records for routerName and will advance the tracker
// state from candidate to contended if the host, wildcardPolicy, or canonical host
// name fields are repeatedly updated.
func NewSimpleContentionTracker(informer cache.SharedInformer, routerName string, interval time.Duration) *SimpleContentionTracker {
return &SimpleContentionTracker{
informer: informer,
routerName: routerName,
expires: interval,
maxContentions: 5,
ids: make(map[string]trackerElement),
}
}
// SetConflictMessage will print message whenever contention with another writer
// is detected.
func (t *SimpleContentionTracker) SetConflictMessage(message string) {
t.lock.Lock()
defer t.lock.Unlock()
t.message = message
}
// Run starts the background cleanup process for expired items.
func (t *SimpleContentionTracker) Run(stopCh <-chan struct{}) {
// Watch the informer for changes to the route ingress we care about (identified
// by router name) and if we see it change remember it. This loop can process
// changes to routes faster than the router, which means it has more up-to-date
// contention info and can detect contention while the main controller is still
// syncing.
t.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(oldObj, obj interface{}) {
oldRoute, ok := oldObj.(*routeapi.Route)
if !ok {
return
}
route, ok := obj.(*routeapi.Route)
if !ok {
return
}
if ingress := ingressChanged(oldRoute, route, t.routerName); ingress != nil {
t.Changed(string(route.UID), ingress)
}
},
})
// periodically clean up expired changes
ticker := time.NewTicker(t.expires * 2)
defer ticker.Stop()
for {
select {
case <-stopCh:
return
case <-ticker.C:
t.flush()
}
}
}
func (t *SimpleContentionTracker) flush() {
t.lock.Lock()
defer t.lock.Unlock()
// reset conflicts every expiration interval, but remove tracking info less often
contentionExpiration := nowFn().Add(-t.expires)
trackerExpiration := contentionExpiration.Add(-2 * t.expires)
removed := 0
contentions := 0
for id, last := range t.ids {
switch last.state {
case stateContended:
if last.at.Before(contentionExpiration) {
delete(t.ids, id)
removed++
continue
}
contentions++
default:
if last.at.Before(trackerExpiration) {
delete(t.ids, id)
removed++
continue
}
}
}
if t.contentions > 0 && len(t.message) > 0 {
glog.Warning(t.message)
}
glog.V(5).Infof("Flushed contention tracker (%s): %d out of %d removed, %d total contentions", t.expires*2, removed, removed+len(t.ids), t.contentions)
t.contentions = contentions
}
// Changed records that a change to an ingress value was detected. This is called from
// a separate goroutine and may have seen newer events than the current route controller
// plugins, so we don't do direct time comparisons. Instead we count edge transitions on
// a given id.
func (t *SimpleContentionTracker) Changed(id string, current *routeapi.RouteIngress) {
t.lock.Lock()
defer t.lock.Unlock()
// we have detected a sufficient number of conflicts to skip all updates for this interval
if t.contentions > t.maxContentions {
glog.V(4).Infof("Reached max contentions, stop tracking changes")
return
}
// if we have never recorded this object
last, ok := t.ids[id]
if !ok {
t.ids[id] = trackerElement{
at: nowFn().Time,
state: stateCandidate,
last: current,
}
glog.V(4).Infof("Object %s is a candidate for contention", id)
return
}
// the previous state matches the current state, nothing to do
if ingressEqual(last.last, current) {
glog.V(4).Infof("Object %s is unchanged", id)
return
}
if last.state == stateContended {
t.contentions++
glog.V(4).Infof("Object %s is contended and has been modified by another writer", id)
return
}
// if it appears that the state is being changed by another party, mark it as contended
if last.state == stateCandidate {
t.ids[id] = trackerElement{
at: nowFn().Time,
state: stateContended,
last: current,
}
t.contentions++
glog.V(4).Infof("Object %s has been modified by another writer", id)
return
}
}
func (t *SimpleContentionTracker) IsChangeContended(id string, now time.Time, current *routeapi.RouteIngress) bool {
t.lock.Lock()
defer t.lock.Unlock()
// we have detected a sufficient number of conflicts to skip all updates for this interval
if t.contentions > t.maxContentions {
glog.V(4).Infof("Reached max contentions, rejecting all update attempts until the next interval")
return true
}
// if we have expired or never recorded this object
last, ok := t.ids[id]
if !ok || last.at.Add(t.expires).Before(now) {
return false
}
// if the object is contended, exit early
if last.state == stateContended {
glog.V(4).Infof("Object %s is being contended by another writer", id)
return true
}
return false
}
func (t *SimpleContentionTracker) Clear(id string, current *routeapi.RouteIngress) {
t.lock.Lock()
defer t.lock.Unlock()
last, ok := t.ids[id]
if !ok {
return
}
last.last = current
last.state = stateCandidate
t.ids[id] = last
}
func ingressEqual(a, b *routeapi.RouteIngress) bool {
return a.Host == b.Host && a.RouterCanonicalHostname == b.RouterCanonicalHostname && a.WildcardPolicy == b.WildcardPolicy && a.RouterName == b.RouterName
}
func ingressConditionTouched(ingress *routeapi.RouteIngress) *metav1.Time {
var lastTouch *metav1.Time
for _, condition := range ingress.Conditions {
if t := condition.LastTransitionTime; t != nil {
switch {
case lastTouch == nil, t.After(lastTouch.Time):
lastTouch = t
}
}
}
return lastTouch
}
func ingressChanged(oldRoute, route *routeapi.Route, routerName string) *routeapi.RouteIngress {
var ingress *routeapi.RouteIngress
for i := range route.Status.Ingress {
if route.Status.Ingress[i].RouterName == routerName {
ingress = &route.Status.Ingress[i]
for _, old := range oldRoute.Status.Ingress {
if old.RouterName == routerName {
if !ingressEqual(ingress, &old) {
return ingress
}
return nil
}
}
return nil
}
}
return nil
}