-
Notifications
You must be signed in to change notification settings - Fork 394
/
cache.go
318 lines (262 loc) · 8.15 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package nodestats
import (
"context"
"math/rand"
"sync"
"time"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"storj.io/common/storj"
"storj.io/common/sync2"
"storj.io/storj/private/date"
"storj.io/storj/storagenode/payouts"
"storj.io/storj/storagenode/pricing"
"storj.io/storj/storagenode/reputation"
"storj.io/storj/storagenode/storageusage"
"storj.io/storj/storagenode/trust"
)
// Config defines nodestats cache configuration.
type Config struct {
MaxSleep time.Duration `help:"maximum duration to wait before requesting data" releaseDefault:"300s" devDefault:"1s"`
ReputationSync time.Duration `help:"how often to sync reputation" releaseDefault:"4h" devDefault:"1m"`
StorageSync time.Duration `help:"how often to sync storage" releaseDefault:"12h" devDefault:"2m"`
}
// CacheStorage encapsulates cache DBs.
type CacheStorage struct {
Reputation reputation.DB
StorageUsage storageusage.DB
Payout payouts.DB
Pricing pricing.DB
}
// Cache runs cache loop and stores reputation stats and storage usage into db.
//
// architecture: Chore
type Cache struct {
log *zap.Logger
db CacheStorage
service *Service
payoutEndpoint *payouts.Endpoint
reputationService *reputation.Service
trust *trust.Pool
maxSleep time.Duration
Reputation *sync2.Cycle
Storage *sync2.Cycle
UsageStat *UsageStat
}
// NewCache creates new caching service instance.
func NewCache(log *zap.Logger, config Config, db CacheStorage, service *Service,
payoutEndpoint *payouts.Endpoint, reputationService *reputation.Service, trust *trust.Pool) *Cache {
cache := &Cache{
log: log,
db: db,
service: service,
payoutEndpoint: payoutEndpoint,
reputationService: reputationService,
trust: trust,
maxSleep: config.MaxSleep,
Reputation: sync2.NewCycle(config.ReputationSync),
Storage: sync2.NewCycle(config.StorageSync),
UsageStat: NewUsageStat(),
}
mon.Chain(cache.UsageStat)
return cache
}
// Run runs loop.
func (cache *Cache) Run(ctx context.Context) error {
var group errgroup.Group
err := cache.satelliteLoop(ctx, func(satelliteID storj.NodeID) error {
stubHistory, err := cache.payoutEndpoint.GetAllPaystubs(ctx, satelliteID)
if err != nil {
return err
}
for i := 0; i < len(stubHistory); i++ {
err := cache.db.Payout.StorePayStub(ctx, stubHistory[i])
if err != nil {
return err
}
}
paymentHistory, err := cache.payoutEndpoint.GetAllPayments(ctx, satelliteID)
if err != nil {
return err
}
for j := 0; j < len(paymentHistory); j++ {
err := cache.db.Payout.StorePayment(ctx, paymentHistory[j])
if err != nil {
return err
}
}
pricingModel, err := cache.service.GetPricingModel(ctx, satelliteID)
if err != nil {
return err
}
return cache.db.Pricing.Store(ctx, *pricingModel)
})
if err != nil {
cache.log.Error("Get pricing-model/join date failed", zap.Error(err))
}
cache.Reputation.Start(ctx, &group, func(ctx context.Context) error {
if err := cache.sleep(ctx); err != nil {
return err
}
err := cache.CacheReputationStats(ctx)
if err != nil {
cache.log.Error("Get stats query failed", zap.Error(err))
}
return nil
})
cache.Storage.Start(ctx, &group, func(ctx context.Context) error {
if err := cache.sleep(ctx); err != nil {
return err
}
err := cache.CacheSpaceUsage(ctx)
if err != nil {
cache.log.Error("Get disk space usage query failed", zap.Error(err))
}
err = cache.CacheHeldAmount(ctx)
if err != nil {
cache.log.Error("Get held amount query failed", zap.Error(err))
}
return nil
})
return group.Wait()
}
// CacheReputationStats queries node stats from all the satellites
// known to the storagenode and stores information into db.
func (cache *Cache) CacheReputationStats(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
return cache.satelliteLoop(ctx, func(satellite storj.NodeID) error {
stats, err := cache.service.GetReputationStats(ctx, satellite)
if err != nil {
return err
}
if err = cache.reputationService.Store(ctx, *stats, satellite); err != nil {
cache.log.Error("failed to store reputation", zap.Error(err))
return err
}
return nil
})
}
// UsageStat caches last space usage value for each satellite, to make it available for monkit.
type UsageStat struct {
mu sync.Mutex
usedBytes map[storj.NodeID]float64
}
// NewUsageStat initializes a UsageState.
func NewUsageStat() *UsageStat {
return &UsageStat{
usedBytes: make(map[storj.NodeID]float64),
}
}
// Stats implements monkit.StatSource.
func (u *UsageStat) Stats(cb func(key monkit.SeriesKey, field string, val float64)) {
u.mu.Lock()
defer u.mu.Unlock()
for satellite, space := range u.usedBytes {
cb(monkit.NewSeriesKey("satellite_usage").WithTag("satellite", satellite.String()), "used_bytes", space)
}
}
// Update updates the cached value.
func (u *UsageStat) Update(satellite storj.NodeID, usedSpace float64) {
u.mu.Lock()
defer u.mu.Unlock()
u.usedBytes[satellite] = usedSpace
}
var _ monkit.StatSource = &UsageStat{}
// CacheSpaceUsage queries disk space usage from all the satellites
// known to the storagenode and stores information into db.
func (cache *Cache) CacheSpaceUsage(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
// get current month edges
startDate, endDate := date.MonthBoundary(time.Now().UTC())
// start from last day of previous month
startDate = startDate.AddDate(0, 0, -1)
return cache.satelliteLoop(ctx, func(satellite storj.NodeID) error {
spaceUsages, err := cache.service.GetDailyStorageUsage(ctx, satellite, startDate, endDate)
if err != nil {
return err
}
// update monkit cache
if len(spaceUsages) > 1 {
lastRec := spaceUsages[len(spaceUsages)-1]
cache.UsageStat.Update(satellite, lastRec.AtRestTotal/lastRec.IntervalEndTime.Sub(spaceUsages[len(spaceUsages)-2].IntervalEndTime).Hours())
}
err = cache.db.StorageUsage.Store(ctx, spaceUsages)
if err != nil {
return err
}
return nil
})
}
// CacheHeldAmount queries held amount stats and payments from
// all the satellites known to the storagenode and stores info into db.
func (cache *Cache) CacheHeldAmount(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
return cache.satelliteLoop(ctx, func(satellite storj.NodeID) error {
now := time.Now().String()
yearAndMonth, err := date.PeriodToTime(now)
if err != nil {
return err
}
previousMonth := yearAndMonth.AddDate(0, -1, 0).String()
payStub, err := cache.payoutEndpoint.GetPaystub(ctx, satellite, previousMonth)
if err != nil {
if payouts.ErrNoPayStubForPeriod.Has(err) {
return nil
}
cache.log.Error("payouts err", zap.String("satellite", satellite.String()))
return err
}
if payStub != nil {
if err = cache.db.Payout.StorePayStub(ctx, *payStub); err != nil {
return err
}
}
payment, err := cache.payoutEndpoint.GetPayment(ctx, satellite, previousMonth)
if err != nil {
return err
}
if payment != nil {
if err = cache.db.Payout.StorePayment(ctx, *payment); err != nil {
return err
}
}
return nil
})
}
// sleep for random interval in [0;maxSleep)
// returns error if context was cancelled.
func (cache *Cache) sleep(ctx context.Context) error {
if cache.maxSleep <= 0 {
return nil
}
jitter := time.Duration(rand.Int63n(int64(cache.maxSleep)))
if !sync2.Sleep(ctx, jitter) {
return ctx.Err()
}
return nil
}
// satelliteLoop loops over all satellites from trust pool executing provided fn, caching errors if occurred,
// on each step checks if context has been cancelled.
func (cache *Cache) satelliteLoop(ctx context.Context, fn func(id storj.NodeID) error) error {
var groupErr errs.Group
for _, satellite := range cache.trust.GetSatellites(ctx) {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
groupErr.Add(fn(satellite))
}
return groupErr.Err()
}
// Close closes underlying cycles.
func (cache *Cache) Close() error {
defer mon.Task()(nil)(nil)
cache.Reputation.Close()
cache.Storage.Close()
return nil
}