-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
tx_limiter.go
179 lines (151 loc) · 5.47 KB
/
tx_limiter.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
/*
Copyright 2019 The Vitess Authors.
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 txlimiter
import (
"strings"
"sync"
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/vt/callerid"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
querypb "vitess.io/vitess/go/vt/proto/query"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)
const unknown string = "unknown"
// TxLimiter is the transaction limiter interface.
type TxLimiter interface {
Get(immediate *querypb.VTGateCallerID, effective *vtrpcpb.CallerID) bool
Release(immediate *querypb.VTGateCallerID, effective *vtrpcpb.CallerID)
}
// New creates a new TxLimiter.
// slotCount: total slot count in transaction pool
// maxPerUser: fraction of the pool that may be taken by single user
// enabled: should the feature be enabled. If false, will return
// "allow-all" limiter
// dryRun: if true, does no limiting, but records stats of the decisions made
// byXXX: whether given field from immediate/effective caller id should be taken
// into account when deciding "user" identity for purposes of transaction
// limiting.
func New(env tabletenv.Env) TxLimiter {
config := env.Config()
if !config.EnableTransactionLimit && !config.EnableTransactionLimitDryRun {
return &TxAllowAll{}
}
return &Impl{
maxPerUser: int64(float64(config.TxPool.Size) * config.TransactionLimitPerUser),
dryRun: config.EnableTransactionLimitDryRun,
byUsername: config.TransactionLimitByUsername,
byPrincipal: config.TransactionLimitByPrincipal,
byComponent: config.TransactionLimitByComponent,
bySubcomponent: config.TransactionLimitBySubcomponent,
byEffectiveUser: config.TransactionLimitByPrincipal || config.TransactionLimitByComponent || config.TransactionLimitBySubcomponent,
usageMap: make(map[string]int64),
rejections: env.Exporter().NewCountersWithSingleLabel("TxLimiterRejections", "rejections from TxLimiter", "user"),
rejectionsDryRun: env.Exporter().NewCountersWithSingleLabel("TxLimiterRejectionsDryRun", "rejections from TxLimiter in dry run", "user"),
}
}
// TxAllowAll is a TxLimiter that allows all Get requests and does no tracking.
// Implements Txlimiter.
type TxAllowAll struct{}
// Get always returns true (allows all requests).
// Implements TxLimiter.Get
func (txa *TxAllowAll) Get(immediate *querypb.VTGateCallerID, effective *vtrpcpb.CallerID) bool {
return true
}
// Release is noop, because TxAllowAll does no tracking.
// Implements TxLimiter.Release
func (txa *TxAllowAll) Release(immediate *querypb.VTGateCallerID, effective *vtrpcpb.CallerID) {
// NOOP
}
// Impl limits the total number of transactions a single user may use
// concurrently.
// Implements TxLimiter.
type Impl struct {
maxPerUser int64
usageMap map[string]int64
mu sync.Mutex
dryRun bool
byUsername bool
byPrincipal bool
byComponent bool
bySubcomponent bool
byEffectiveUser bool
rejections, rejectionsDryRun *stats.CountersWithSingleLabel
}
// Get tells whether given user (identified by context.Context) is allowed
// to use another transaction slot. If this method returns true, it's
// necessary to call Release once transaction is returned to the pool.
// Implements TxLimiter.Get
func (txl *Impl) Get(immediate *querypb.VTGateCallerID, effective *vtrpcpb.CallerID) bool {
key := txl.extractKey(immediate, effective)
txl.mu.Lock()
defer txl.mu.Unlock()
usage := txl.usageMap[key]
if usage < txl.maxPerUser {
txl.usageMap[key] = usage + 1
return true
}
if txl.dryRun {
log.Infof("TxLimiter: DRY RUN: user over limit: %s", key)
txl.rejectionsDryRun.Add(key, 1)
return true
}
log.Infof("TxLimiter: Over limit, rejecting transaction request for user: %s", key)
txl.rejections.Add(key, 1)
return false
}
// Release marks that given user (identified by caller ID) is no longer using
// a transaction slot.
// Implements TxLimiter.Release
func (txl *Impl) Release(immediate *querypb.VTGateCallerID, effective *vtrpcpb.CallerID) {
key := txl.extractKey(immediate, effective)
txl.mu.Lock()
defer txl.mu.Unlock()
usage, ok := txl.usageMap[key]
if !ok {
return
}
if usage == 1 {
delete(txl.usageMap, key)
return
}
txl.usageMap[key] = usage - 1
}
// extractKey builds a string key used to differentiate users, based
// on fields specified in configuration and their values from caller ID.
func (txl *Impl) extractKey(immediate *querypb.VTGateCallerID, effective *vtrpcpb.CallerID) string {
var parts []string
if txl.byUsername {
if immediate != nil {
parts = append(parts, callerid.GetUsername(immediate))
} else {
parts = append(parts, unknown)
}
}
if txl.byEffectiveUser {
if effective != nil {
if txl.byPrincipal {
parts = append(parts, callerid.GetPrincipal(effective))
}
if txl.byComponent {
parts = append(parts, callerid.GetComponent(effective))
}
if txl.bySubcomponent {
parts = append(parts, callerid.GetSubcomponent(effective))
}
} else {
parts = append(parts, unknown)
}
}
return strings.Join(parts, "/")
}