/
cachestorage.go
268 lines (233 loc) · 6.84 KB
/
cachestorage.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
package metric
import (
"encoding/json"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/Axway/agent-sdk/pkg/agent"
"github.com/Axway/agent-sdk/pkg/cache"
"github.com/Axway/agent-sdk/pkg/traceability"
"github.com/Axway/agent-sdk/pkg/util"
metrics "github.com/rcrowley/go-metrics"
)
const (
appUsagePrefix = "app_usage."
cacheFileName = "agent-usagemetric.json"
metricKeyPrefix = "metric."
metricStartTimeKey = "metric_start_time"
usageStartTimeKey = "usage_start_time"
usageCountKey = "usage_count"
volumeKey = "usage_volume"
)
type storageCache interface {
initialize()
updateUsage(usageCount int)
updateVolume(bytes int64)
updateAppUsage(usageCount int, appID string)
updateMetric(apiStatusMetric metrics.Histogram, metric *APIMetric)
removeMetric(metric *APIMetric)
save()
}
type cacheStorage struct {
cacheFilePath string
oldCacheFilePath string
collector *collector
storage cache.Cache
storageLock sync.Mutex
isInitialized bool
}
func newStorageCache(collector *collector) storageCache {
storageCache := &cacheStorage{
cacheFilePath: traceability.GetCacheDirPath() + "/" + cacheFileName,
oldCacheFilePath: traceability.GetDataDirPath() + "/" + cacheFileName,
collector: collector,
storageLock: sync.Mutex{},
storage: cache.New(),
isInitialized: false,
}
return storageCache
}
func (c *cacheStorage) moveCacheFile() {
// to remove for next major release
_, err := os.Stat(c.oldCacheFilePath)
if os.IsNotExist(err) {
return
}
// file exists, move it over
os.Rename(c.oldCacheFilePath, c.cacheFilePath)
}
func (c *cacheStorage) initialize() {
c.moveCacheFile() // to remove for next major release
storageCache := cache.Load(c.cacheFilePath)
c.loadUsage(storageCache)
c.loadMetrics(storageCache)
// Not a job as the loop requires signal processing
if !c.isInitialized && util.IsNotTest() {
go c.storeCacheJob()
}
c.storage = storageCache
c.isInitialized = true
}
func (c *cacheStorage) loadUsage(storageCache cache.Cache) {
// update the collector usage start time
usageStartTime, err := c.parseTimeFromCache(storageCache, usageStartTimeKey)
if err == nil && !agent.GetCentralConfig().GetUsageReportingConfig().IsOfflineMode() {
// do not load this start time when offline
c.collector.usageStartTime = usageStartTime
}
// update the collector metric start time
metricStartTime, err := c.parseTimeFromCache(storageCache, metricStartTimeKey)
if err == nil && !agent.GetCentralConfig().GetUsageReportingConfig().IsOfflineMode() {
// do not load this start time when offline
c.collector.metricStartTime = metricStartTime
}
// update transaction counter in registry.
usageCount, err := storageCache.Get(usageCountKey)
if err == nil {
// un-marshalling the cache defaults the serialization of numeric values to float64
c.collector.updateUsage(int64(usageCount.(float64)))
}
// update transaction volume in registry.
usageVolume, err := storageCache.Get(volumeKey)
if err == nil {
// un-marshalling the cache defaults the serialization of numeric values to float64
c.collector.updateVolume(int64(usageVolume.(float64)))
}
}
func (c *cacheStorage) updateUsage(usageCount int) {
if !c.isInitialized || !agent.GetCentralConfig().GetUsageReportingConfig().CanPublishUsage() {
return
}
c.storageLock.Lock()
defer c.storageLock.Unlock()
c.storage.Set(usageStartTimeKey, c.collector.usageStartTime)
c.storage.Set(metricStartTimeKey, c.collector.metricStartTime)
c.storage.Set(usageCountKey, usageCount)
}
func (c *cacheStorage) updateVolume(bytes int64) {
if !c.isInitialized || !agent.GetCentralConfig().IsAxwayManaged() ||
!agent.GetCentralConfig().GetUsageReportingConfig().CanPublishUsage() {
// NOT initialized or NOT axway managed or can NOT publish usage
return
}
c.storageLock.Lock()
defer c.storageLock.Unlock()
c.storage.Set(volumeKey, bytes)
}
func (c *cacheStorage) updateAppUsage(usageCount int, appID string) {
if !c.isInitialized || !agent.GetCentralConfig().GetUsageReportingConfig().CanPublishUsage() {
return
}
c.storageLock.Lock()
defer c.storageLock.Unlock()
c.storage.Set(appUsagePrefix+appID, usageCount)
}
func (c *cacheStorage) loadMetrics(storageCache cache.Cache) {
cacheKeys := storageCache.GetKeys()
for _, cacheKey := range cacheKeys {
if strings.Contains(cacheKey, metricKeyPrefix) {
cacheItem, _ := storageCache.Get(cacheKey)
buffer, _ := json.Marshal(cacheItem)
var cm cachedMetric
json.Unmarshal(buffer, &cm)
var metric *APIMetric
for _, duration := range cm.Values {
metricDetail := Detail{
APIDetails: cm.API,
AppDetails: cm.App,
StatusCode: cm.StatusCode,
Duration: duration,
}
metric = c.collector.updateMetric(metricDetail)
}
newKey := c.getKey(metric)
if newKey != cacheKey {
c.storageLock.Lock()
storageCache.Delete(cacheKey)
c.storageLock.Unlock()
}
storageCache.Set(newKey, cm)
if metric != nil {
metric.StartTime = cm.StartTime
}
}
}
}
func (c *cacheStorage) updateMetric(histogram metrics.Histogram, metric *APIMetric) {
if !c.isInitialized {
return
}
c.storageLock.Lock()
defer c.storageLock.Unlock()
cachedMetric := cachedMetric{
Subscription: metric.Subscription,
App: metric.App,
Product: metric.Product,
AssetResource: metric.AssetResource,
ProductPlan: metric.ProductPlan,
Quota: metric.Quota,
API: metric.API,
StatusCode: metric.StatusCode,
Count: histogram.Count(),
Values: histogram.Sample().Values(),
StartTime: metric.StartTime,
}
c.storage.Set(c.getKey(metric), cachedMetric)
}
func (c *cacheStorage) removeMetric(metric *APIMetric) {
if !c.isInitialized {
return
}
c.storageLock.Lock()
defer c.storageLock.Unlock()
c.storage.Delete(c.getKey(metric))
}
func (c *cacheStorage) getKey(metric *APIMetric) string {
return metricKeyPrefix +
metric.Subscription.ID + "." +
metric.App.ID + "." +
metric.API.ID + "." +
metric.StatusCode
}
func (c *cacheStorage) save() {
if !c.isInitialized {
return
}
c.storageLock.Lock()
defer c.storageLock.Unlock()
c.storage.Save(c.cacheFilePath)
}
func (c *cacheStorage) storeCacheJob() {
cachetimeTicker := time.NewTicker(5 * time.Second)
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
for {
select {
case <-cachetimeTicker.C:
c.save()
case <-signals:
c.save()
return
}
}
}
func (c *cacheStorage) parseTimeFromCache(storage cache.Cache, key string) (time.Time, error) {
resultTime := now()
item, err := storage.Get(key)
if err != nil {
return now(), err
}
cachedTimeStr, ok := item.(string)
if ok {
resultTime, _ = time.Parse(time.RFC3339, cachedTimeStr)
} else {
cachedTime, ok := item.(time.Time)
if ok {
resultTime = cachedTime
}
}
return resultTime, nil
}