This repository has been archived by the owner on Feb 12, 2019. It is now read-only.
/
quota_usage.go
157 lines (139 loc) · 4.91 KB
/
quota_usage.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
// Copyright 2017 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"sync"
"time"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/kbfs/kbfsblock"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
// ECQUCtxTagKey is the type for unique ECQU background operation IDs.
type ECQUCtxTagKey struct{}
// ECQUID is used in EventuallyConsistentQuotaUsage for only background RPCs.
// More specifically, when we need to spawn a background goroutine for
// GetUserQuotaInfo, a new context with this tag is created and used. This is
// also used as a prefix for the logger module name in
// EventuallyConsistentQuotaUsage.
const ECQUID = "ECQU"
type cachedQuotaUsage struct {
timestamp time.Time
usageBytes int64
limitBytes int64
gitUsageBytes int64
gitLimitBytes int64
}
// EventuallyConsistentQuotaUsage keeps tracks of quota usage, in a way user of
// which can choose to accept stale data to reduce calls into block servers.
type EventuallyConsistentQuotaUsage struct {
config Config
log logger.Logger
tid keybase1.TeamID
fetcher *fetchDecider
mu sync.RWMutex
cached cachedQuotaUsage
}
// NewEventuallyConsistentQuotaUsage creates a new
// EventuallyConsistentQuotaUsage object.
func NewEventuallyConsistentQuotaUsage(
config Config, loggerSuffix string) *EventuallyConsistentQuotaUsage {
q := &EventuallyConsistentQuotaUsage{
config: config,
log: config.MakeLogger(ECQUID + "-" + loggerSuffix),
}
q.fetcher = newFetchDecider(
q.log, q.getAndCache, ECQUCtxTagKey{}, ECQUID, q.config)
return q
}
// NewEventuallyConsistentTeamQuotaUsage creates a new
// EventuallyConsistentQuotaUsage object.
func NewEventuallyConsistentTeamQuotaUsage(
config Config, tid keybase1.TeamID,
loggerSuffix string) *EventuallyConsistentQuotaUsage {
q := NewEventuallyConsistentQuotaUsage(config, loggerSuffix)
q.tid = tid
return q
}
func (q *EventuallyConsistentQuotaUsage) getAndCache(
ctx context.Context) (err error) {
defer func() {
q.log.CDebugf(ctx, "getAndCache: error=%v", err)
}()
var quotaInfo *kbfsblock.QuotaInfo
if q.tid.IsNil() {
quotaInfo, err = q.config.BlockServer().GetUserQuotaInfo(ctx)
} else {
quotaInfo, err = q.config.BlockServer().GetTeamQuotaInfo(ctx, q.tid)
}
if err != nil {
return err
}
q.mu.Lock()
defer q.mu.Unlock()
q.cached.limitBytes = quotaInfo.Limit
q.cached.gitLimitBytes = quotaInfo.GitLimit
if quotaInfo.Total != nil {
q.cached.usageBytes = quotaInfo.Total.Bytes[kbfsblock.UsageWrite]
q.cached.gitUsageBytes = quotaInfo.Total.Bytes[kbfsblock.UsageGitWrite]
} else {
q.cached.usageBytes = 0
}
q.cached.timestamp = q.config.Clock().Now()
return nil
}
func (q *EventuallyConsistentQuotaUsage) getCached() cachedQuotaUsage {
q.mu.RLock()
defer q.mu.RUnlock()
return q.cached
}
// Get returns KBFS bytes used and limit for user, for the current
// default block type. To help avoid having too frequent calls into
// bserver, caller can provide a positive tolerance, to accept stale
// LimitBytes and UsageBytes data. If tolerance is 0 or negative, this
// always makes a blocking RPC to bserver and return latest quota
// usage.
//
// 1) If the age of cached data is more than blockTolerance, a blocking RPC is
// issued and the function only returns after RPC finishes, with the newest
// data from RPC. The RPC causes cached data to be refreshed as well.
// 2) Otherwise, if the age of cached data is more than bgTolerance,
// a background RPC is spawned to refresh cached data, and the stale
// data is returned immediately.
// 3) Otherwise, the cached stale data is returned immediately.
func (q *EventuallyConsistentQuotaUsage) Get(
ctx context.Context, bgTolerance, blockTolerance time.Duration) (
timestamp time.Time, usageBytes, limitBytes int64, err error) {
c := q.getCached()
err = q.fetcher.Do(ctx, bgTolerance, blockTolerance, c.timestamp)
if err != nil {
return time.Time{}, -1, -1, err
}
c = q.getCached()
switch q.config.DefaultBlockType() {
case keybase1.BlockType_DATA:
return c.timestamp, c.usageBytes, c.limitBytes, nil
case keybase1.BlockType_GIT:
return c.timestamp, c.gitUsageBytes, c.gitLimitBytes, nil
default:
return time.Time{}, -1, -1, errors.Errorf(
"Unknown default block type: %d", q.config.DefaultBlockType())
}
}
// GetAllTypes is the same as Get, except it returns usage and limits
// for all block types.
func (q *EventuallyConsistentQuotaUsage) GetAllTypes(
ctx context.Context, bgTolerance, blockTolerance time.Duration) (
timestamp time.Time,
usageBytes, limitBytes, gitUsageBytes, getLimitBytes int64, err error) {
c := q.getCached()
err = q.fetcher.Do(ctx, bgTolerance, blockTolerance, c.timestamp)
if err != nil {
return time.Time{}, -1, -1, -1, -1, err
}
c = q.getCached()
return c.timestamp,
c.usageBytes, c.limitBytes, c.gitUsageBytes, c.gitLimitBytes, nil
}