/
quota.go
172 lines (135 loc) · 4.58 KB
/
quota.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
// Copyright 2022-2023 Tigris Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package quota
import (
"context"
"github.com/rs/zerolog/log"
"github.com/tigrisdata/tigris/errors"
"github.com/tigrisdata/tigris/server/config"
"github.com/tigrisdata/tigris/server/metadata"
"github.com/tigrisdata/tigris/server/metrics"
)
var (
ErrReadUnitsExceeded = errors.ResourceExhausted("request read rate exceeded")
ErrWriteUnitsExceeded = errors.ResourceExhausted("request write rate exceeded")
ErrStorageSizeExceeded = errors.ResourceExhausted("data size limit exceeded")
ErrMaxRequestSizeExceeded = errors.ResourceExhausted("maximum request size limit exceeded")
)
type Quota interface {
Allow(ctx context.Context, namespace string, size int, isWrite bool) error
Wait(ctx context.Context, namespace string, size int, isWrite bool) error
Cleanup()
}
type State struct {
Read Limiter
Write Limiter
}
func (s *State) Allow(size int, isWrite bool) error {
if isWrite {
return s.Write.Allow(size)
}
return s.Read.Allow(size)
}
func (s *State) Wait(ctx context.Context, size int, isWrite bool) error {
if isWrite {
return s.Write.Wait(ctx, size)
}
return s.Read.Wait(ctx, size)
}
type Manager struct {
quota []Quota
}
var mgr Manager
// this is extracted from Init for tests.
func initManager(tm *metadata.TenantManager, cfg *config.Config) *Manager {
var q []Quota
if cfg.Quota.ReadUnitSize != 0 {
config.ReadUnitSize = cfg.Quota.ReadUnitSize
}
if cfg.Quota.WriteUnitSize != 0 {
config.WriteUnitSize = cfg.Quota.WriteUnitSize
}
log.Debug().Int("read_unit_size", config.ReadUnitSize).Int("write_unit_size", config.WriteUnitSize).Msg("Initializing quota manager")
// metrics calculation is piggybacked to storage quota, so initialize
// storage quota manager even when quota is disabled, but metrics are enabled
if cfg.Quota.Storage.Enabled || cfg.Metrics.Size.Enabled {
q = append(q, initStorage(tm, &cfg.Quota))
}
if cfg.Quota.Node.Enabled {
q = append(q, initNode(&cfg.Quota))
}
if cfg.Quota.Namespace.Enabled {
if cfg.Observability.Provider == "datadog" {
if cfg.Quota.Namespace.RefreshInterval <= 0 {
log.Fatal().Msg("refresh interval should be non-empty")
}
q = append(q, initNamespace(tm, &cfg.Quota, initDatadogMetrics(cfg)))
} else {
q = append(q, initNamespace(tm, &cfg.Quota, initNoopMetrics(cfg)))
}
}
return &Manager{quota: q}
}
func Init(tm *metadata.TenantManager, cfg *config.Config) error {
mgr = *initManager(tm, cfg)
return nil
}
func (*Manager) cleanup() {
for _, q := range mgr.quota {
q.Cleanup()
}
log.Debug().Msg("Cleaned up quota manager")
}
func Cleanup() {
mgr.cleanup()
}
func unitSize(isWrite bool) int64 {
if isWrite {
return int64(config.WriteUnitSize)
}
return int64(config.ReadUnitSize)
}
func toUnits(size int, isWrite bool) int {
us := unitSize(isWrite)
// + (unitSize - 1) guarantees that fractional part is counted as one unit
return int((int64(size) + us - 1) / us)
}
func reportError(namespace string, size int, isWrite bool, err error) error {
if errors.Is(err, ErrReadUnitsExceeded) || errors.Is(err, ErrWriteUnitsExceeded) {
metrics.UpdateQuotaRateThrottled(namespace, toUnits(size, isWrite), isWrite)
} else if errors.Is(err, ErrStorageSizeExceeded) {
metrics.UpdateQuotaStorageThrottled(namespace, size)
}
return err
}
// Allow checks read, write rates and storage size limits for the namespace
// and returns error if at least one of them is exceeded.
func Allow(ctx context.Context, namespace string, size int, isWrite bool) error {
for _, q := range mgr.quota {
if err := q.Allow(ctx, namespace, size, isWrite); err != nil {
return reportError(namespace, size, isWrite, err)
}
}
metrics.UpdateQuotaUsage(namespace, toUnits(size, isWrite), isWrite)
return nil
}
func Wait(ctx context.Context, namespace string, size int, isWrite bool) error {
for _, q := range mgr.quota {
if err := q.Wait(ctx, namespace, size, isWrite); err != nil {
return reportError(namespace, size, isWrite, err)
}
}
metrics.UpdateQuotaUsage(namespace, toUnits(size, isWrite), isWrite)
return nil
}