/
cache.go
282 lines (239 loc) · 12.5 KB
/
cache.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
282
package prometheus
import (
"sync"
"time"
"github.com/prometheus/common/model"
kialiConfig "github.com/kiali/kiali/config"
"github.com/kiali/kiali/log"
)
type (
timeInResult struct {
queryTime time.Time
inResult model.Vector
}
timeInOutResult struct {
queryTime time.Time
inResult model.Vector
outResult model.Vector
}
PromCache interface {
GetAllRequestRates(namespace, cluster string, ratesInterval string, queryTime time.Time) (bool, model.Vector)
GetAppRequestRates(namespace, cluster, app, ratesInterval string, queryTime time.Time) (bool, model.Vector, model.Vector)
GetNamespaceServicesRequestRates(namespace, cluster string, ratesInterval string, queryTime time.Time) (bool, model.Vector)
GetServiceRequestRates(namespace, cluster, service, ratesInterval string, queryTime time.Time) (bool, model.Vector)
GetWorkloadRequestRates(namespace, cluster, workload, ratesInterval string, queryTime time.Time) (bool, model.Vector, model.Vector)
SetAllRequestRates(namespace, cluster string, ratesInterval string, queryTime time.Time, inResult model.Vector)
SetAppRequestRates(namespace, cluster, app, ratesInterval string, queryTime time.Time, inResult model.Vector, outResult model.Vector)
SetNamespaceServicesRequestRates(namespace, cluster string, ratesInterval string, queryTime time.Time, inResult model.Vector)
SetServiceRequestRates(namespace, cluster, service, ratesInterval string, queryTime time.Time, inResult model.Vector)
SetWorkloadRequestRates(namespace, cluster, workload, ratesInterval string, queryTime time.Time, inResult model.Vector, outResult model.Vector)
}
promCacheImpl struct {
cacheDuration time.Duration
cacheExpiration time.Duration
// Cached by namespace, cluster, app, ratesInterval
cacheSvcRequestRates map[string]map[string]map[string]map[string]timeInResult
cacheWkRequestRates map[string]map[string]map[string]map[string]timeInOutResult
cacheAppRequestRates map[string]map[string]map[string]map[string]timeInOutResult
// Cached by namespace, cluster, ratesInterval
cacheAllRequestRates map[string]map[string]map[string]timeInResult
cacheNsSvcRequestRates map[string]map[string]map[string]timeInResult
allRequestRatesLock sync.RWMutex
appRequestRatesLock sync.RWMutex
nsSvcRequestRatesLock sync.RWMutex
svcRequestRatesLock sync.RWMutex
wkRequestRatesLock sync.RWMutex
}
)
func NewPromCache() PromCache {
kConfig := kialiConfig.Get()
cacheDuration := time.Duration(kConfig.ExternalServices.Prometheus.CacheDuration) * time.Second
cacheExpiration := time.Duration(kConfig.ExternalServices.Prometheus.CacheExpiration) * time.Second
promCacheImpl := promCacheImpl{
cacheDuration: cacheDuration,
cacheExpiration: cacheExpiration,
cacheAllRequestRates: make(map[string]map[string]map[string]timeInResult),
cacheAppRequestRates: make(map[string]map[string]map[string]map[string]timeInOutResult),
cacheNsSvcRequestRates: make(map[string]map[string]map[string]timeInResult),
cacheSvcRequestRates: make(map[string]map[string]map[string]map[string]timeInResult),
cacheWkRequestRates: make(map[string]map[string]map[string]map[string]timeInOutResult),
}
go promCacheImpl.watchExpiration()
return &promCacheImpl
}
func (c *promCacheImpl) GetAllRequestRates(namespace, cluster string, ratesInterval string, queryTime time.Time) (bool, model.Vector) {
defer c.allRequestRatesLock.RUnlock()
c.allRequestRatesLock.RLock()
if nsRates, okNs := c.cacheAllRequestRates[namespace][cluster]; okNs {
if rtInterval, okRt := nsRates[ratesInterval]; okRt {
if !queryTime.Before(rtInterval.queryTime) && queryTime.Sub(rtInterval.queryTime) < c.cacheDuration {
log.Tracef("[Prom Cache] GetAllRequestRates [namespace: %s] [ratesInterval: %s] [queryTime: %s]", namespace, ratesInterval, queryTime.String())
return true, rtInterval.inResult
}
}
}
return false, nil
}
func (c *promCacheImpl) SetAllRequestRates(namespace, cluster string, ratesInterval string, queryTime time.Time, inResult model.Vector) {
defer c.allRequestRatesLock.Unlock()
c.allRequestRatesLock.Lock()
if _, okNs := c.cacheAllRequestRates[namespace]; !okNs {
c.cacheAllRequestRates[namespace] = make(map[string]map[string]timeInResult)
}
if _, okCluster := c.cacheAllRequestRates[namespace][cluster]; !okCluster {
c.cacheAllRequestRates[namespace][cluster] = make(map[string]timeInResult)
}
c.cacheAllRequestRates[namespace][cluster][ratesInterval] = timeInResult{
queryTime: queryTime,
inResult: inResult,
}
log.Tracef("[Prom Cache] SetAllRequestRates [namespace: %s] [cluster: %s] [ratesInterval: %s] [queryTime: %s]", namespace, cluster, ratesInterval, queryTime.String())
}
func (c *promCacheImpl) GetAppRequestRates(namespace, cluster, app, ratesInterval string, queryTime time.Time) (bool, model.Vector, model.Vector) {
defer c.appRequestRatesLock.RUnlock()
c.appRequestRatesLock.RLock()
if nsRates, okNs := c.cacheAppRequestRates[namespace]; okNs {
if appInterval, okApp := nsRates[app][cluster]; okApp {
if rtInterval, okRt := appInterval[ratesInterval]; okRt {
if !queryTime.Before(rtInterval.queryTime) && queryTime.Sub(rtInterval.queryTime) < c.cacheDuration {
log.Tracef("[Prom Cache] GetAppRequestRates [namespace: %s] [app: %s] [ratesInterval: %s] [queryTime: %s]", namespace, app, ratesInterval, queryTime.String())
return true, rtInterval.inResult, rtInterval.outResult
}
}
}
}
return false, nil, nil
}
func (c *promCacheImpl) SetAppRequestRates(namespace, cluster, app, ratesInterval string, queryTime time.Time, inResult model.Vector, outResult model.Vector) {
defer c.appRequestRatesLock.Unlock()
c.appRequestRatesLock.Lock()
if _, okNs := c.cacheAppRequestRates[namespace]; !okNs {
c.cacheAppRequestRates[namespace] = make(map[string]map[string]map[string]timeInOutResult)
}
if _, okCluster := c.cacheAppRequestRates[namespace][cluster]; !okCluster {
c.cacheAppRequestRates[namespace][cluster] = make(map[string]map[string]timeInOutResult)
}
if _, okApp := c.cacheAppRequestRates[namespace][app]; !okApp {
c.cacheAppRequestRates[namespace][cluster][app] = make(map[string]timeInOutResult)
}
c.cacheAppRequestRates[namespace][cluster][app][ratesInterval] = timeInOutResult{
queryTime: queryTime,
inResult: inResult,
outResult: outResult,
}
log.Tracef("[Prom Cache] SetAppRequestRates [namespace: %s] [cluster: %s] [app: %s] [ratesInterval: %s] [queryTime: %s]", namespace, cluster, app, ratesInterval, queryTime.String())
}
func (c *promCacheImpl) GetNamespaceServicesRequestRates(namespace, cluster string, ratesInterval string, queryTime time.Time) (bool, model.Vector) {
defer c.nsSvcRequestRatesLock.RUnlock()
c.nsSvcRequestRatesLock.RLock()
if nsRates, okNs := c.cacheNsSvcRequestRates[namespace][cluster]; okNs {
if rtInterval, okRt := nsRates[ratesInterval]; okRt {
if !queryTime.Before(rtInterval.queryTime) && queryTime.Sub(rtInterval.queryTime) < c.cacheDuration {
log.Tracef("[Prom Cache] GetNamespaceServicesRequestRates [namespace: %s] [ratesInterval: %s] [queryTime: %s]", namespace, ratesInterval, queryTime.String())
return true, rtInterval.inResult
}
}
}
return false, nil
}
func (c *promCacheImpl) SetNamespaceServicesRequestRates(namespace, cluster string, ratesInterval string, queryTime time.Time, inResult model.Vector) {
defer c.nsSvcRequestRatesLock.Unlock()
c.nsSvcRequestRatesLock.Lock()
if _, okNs := c.cacheNsSvcRequestRates[namespace]; !okNs {
c.cacheNsSvcRequestRates[namespace] = make(map[string]map[string]timeInResult)
}
if _, okCluster := c.cacheNsSvcRequestRates[namespace][cluster]; !okCluster {
c.cacheNsSvcRequestRates[namespace][cluster] = make(map[string]timeInResult)
}
c.cacheNsSvcRequestRates[namespace][cluster][ratesInterval] = timeInResult{
queryTime: queryTime,
inResult: inResult,
}
log.Tracef("[Prom Cache] SetNamespaceServicesRequestRates [namespace: %s] [ratesInterval: %s] [queryTime: %s]", namespace, ratesInterval, queryTime.String())
}
func (c *promCacheImpl) GetServiceRequestRates(namespace, cluster, service, ratesInterval string, queryTime time.Time) (bool, model.Vector) {
defer c.svcRequestRatesLock.RUnlock()
c.svcRequestRatesLock.RLock()
if rtInterval, okRt := c.cacheSvcRequestRates[namespace][cluster][service][ratesInterval]; okRt {
if !queryTime.Before(rtInterval.queryTime) && queryTime.Sub(rtInterval.queryTime) < c.cacheDuration {
log.Tracef("[Prom Cache] GetServiceRequestRates [namespace: %s] [service: %s] [ratesInterval: %s] [queryTime: %s]", namespace, service, ratesInterval, queryTime.String())
return true, rtInterval.inResult
}
}
return false, nil
}
func (c *promCacheImpl) SetServiceRequestRates(namespace, cluster, service, ratesInterval string, queryTime time.Time, inResult model.Vector) {
defer c.svcRequestRatesLock.Unlock()
c.svcRequestRatesLock.Lock()
if _, okNs := c.cacheSvcRequestRates[namespace]; !okNs {
c.cacheSvcRequestRates[namespace] = make(map[string]map[string]map[string]timeInResult)
}
if _, okCluster := c.cacheSvcRequestRates[namespace][cluster]; !okCluster {
c.cacheSvcRequestRates[namespace][cluster] = make(map[string]map[string]timeInResult)
}
if _, okSvc := c.cacheSvcRequestRates[namespace][cluster][service]; !okSvc {
c.cacheSvcRequestRates[namespace][cluster][service] = make(map[string]timeInResult)
}
c.cacheSvcRequestRates[namespace][cluster][service][ratesInterval] = timeInResult{
queryTime: queryTime,
inResult: inResult,
}
log.Tracef("[Prom Cache] SetServiceRequestRates [namespace: %s] [cluster: %s] [service: %s] [ratesInterval: %s] [queryTime: %s]", namespace, cluster, service, ratesInterval, queryTime.String())
}
func (c *promCacheImpl) GetWorkloadRequestRates(namespace, cluster, workload, ratesInterval string, queryTime time.Time) (bool, model.Vector, model.Vector) {
defer c.wkRequestRatesLock.RUnlock()
c.wkRequestRatesLock.RLock()
if nsRates, okNs := c.cacheWkRequestRates[namespace][cluster]; okNs {
if wkInterval, okWk := nsRates[workload]; okWk {
if rtInterval, okRt := wkInterval[ratesInterval]; okRt {
if !queryTime.Before(rtInterval.queryTime) && queryTime.Sub(rtInterval.queryTime) < c.cacheDuration {
log.Tracef("[Prom Cache] GetWorkloadRequestRates [namespace: %s] [cluster :%s] [workload: %s] [ratesInterval: %s] [queryTime: %s]", namespace, cluster, workload, ratesInterval, queryTime.String())
return true, rtInterval.inResult, rtInterval.outResult
}
}
}
}
return false, nil, nil
}
func (c *promCacheImpl) SetWorkloadRequestRates(namespace, cluster, workload, ratesInterval string, queryTime time.Time, inResult model.Vector, outResult model.Vector) {
defer c.wkRequestRatesLock.Unlock()
c.wkRequestRatesLock.Lock()
if _, okNs := c.cacheWkRequestRates[namespace]; !okNs {
c.cacheWkRequestRates[namespace] = make(map[string]map[string]map[string]timeInOutResult)
}
if _, clusterNs := c.cacheWkRequestRates[namespace][cluster]; !clusterNs {
c.cacheWkRequestRates[namespace][cluster] = make(map[string]map[string]timeInOutResult)
}
if _, okApp := c.cacheWkRequestRates[namespace][workload]; !okApp {
c.cacheWkRequestRates[namespace][cluster][workload] = make(map[string]timeInOutResult)
}
c.cacheWkRequestRates[namespace][cluster][workload][ratesInterval] = timeInOutResult{
queryTime: queryTime,
inResult: inResult,
outResult: outResult,
}
log.Tracef("[Prom Cache] SetAppRequestRates [namespace: %s] [cluster: %s] [workload: %s] [ratesInterval: %s] [queryTime: %s]", namespace, cluster, workload, ratesInterval, queryTime.String())
}
// Expiration is done globally, this cache is designed as short term, so in the worst case it would populated the queries
// Doing an expiration check per item is costly and it's not necessary in this particular context
func (c *promCacheImpl) watchExpiration() {
for {
time.Sleep(c.cacheExpiration)
c.allRequestRatesLock.Lock()
c.cacheAllRequestRates = make(map[string]map[string]map[string]timeInResult)
c.allRequestRatesLock.Unlock()
c.appRequestRatesLock.Lock()
c.cacheAppRequestRates = make(map[string]map[string]map[string]map[string]timeInOutResult)
c.appRequestRatesLock.Unlock()
c.nsSvcRequestRatesLock.Lock()
c.cacheNsSvcRequestRates = make(map[string]map[string]map[string]timeInResult)
c.nsSvcRequestRatesLock.Unlock()
c.svcRequestRatesLock.Lock()
c.cacheSvcRequestRates = make(map[string]map[string]map[string]map[string]timeInResult)
c.svcRequestRatesLock.Unlock()
c.wkRequestRatesLock.Lock()
c.cacheWkRequestRates = make(map[string]map[string]map[string]map[string]timeInOutResult)
c.wkRequestRatesLock.Unlock()
log.Tracef("[Prom Cache] Expired")
}
}