@@ -17,7 +17,6 @@ import kafka.utils.QuotaUtils
17
17
import org .apache .kafka .common .MetricName
18
18
import org .apache .kafka .common .metrics .stats .{Avg , CumulativeSum , Rate }
19
19
import org .apache .kafka .common .metrics .{Metrics , Quota , QuotaViolationException , Sensor }
20
- import org .apache .kafka .common .requests .RequestContext
21
20
import org .apache .kafka .common .security .auth .KafkaPrincipal
22
21
import org .apache .kafka .common .utils .Time
23
22
import org .apache .kafka .network .Session
@@ -60,11 +59,15 @@ class BrokerQuotaManager(private val config: BrokerQuotaManagerConfig,
60
59
return 0
61
60
}
62
61
63
- maybeRecordAndGetThrottleTimeMs(quotaType, request.session, request.context, value, timeMs)
62
+ if (isInWhiteList(request.session.principal, request.context.clientId(), request.context.listenerName())) {
63
+ return 0
64
+ }
65
+
66
+ maybeRecordAndGetThrottleTimeMs(quotaType, value, timeMs)
64
67
}
65
68
66
69
protected def throttleTime (quotaType : QuotaType , e : QuotaViolationException , timeMs : Long ): Long = {
67
- if (quotaType == QuotaType .Request ) {
70
+ if (quotaType == QuotaType .RequestRate ) {
68
71
QuotaUtils .boundedThrottleTime(e, maxThrottleTimeMs, timeMs)
69
72
} else {
70
73
QuotaUtils .throttleTime(e, timeMs)
@@ -84,11 +87,7 @@ class BrokerQuotaManager(private val config: BrokerQuotaManagerConfig,
84
87
}
85
88
}
86
89
87
- def maybeRecordAndGetThrottleTimeMs (quotaType : QuotaType , session : Session , context : RequestContext , value : Double ,
88
- timeMs : Long ): Int = {
89
- if (isInWhiteList(session.principal, context.clientId(), context.listenerName())) {
90
- return 0
91
- }
90
+ def maybeRecordAndGetThrottleTimeMs (quotaType : QuotaType , value : Double , timeMs : Long ): Int = {
92
91
val clientSensors = getOrCreateQuotaSensors(quotaType)
93
92
try {
94
93
clientSensors.quotaSensor.record(value, timeMs, true )
@@ -112,34 +111,51 @@ class BrokerQuotaManager(private val config: BrokerQuotaManagerConfig,
112
111
whiteListCache.clear()
113
112
114
113
if (! config.quotaEnabled) {
115
- metrics.removeSensor(getQuotaSensorName(QuotaType .Request , metricsTags))
114
+ metrics.removeSensor(getQuotaSensorName(QuotaType .RequestRate , metricsTags))
116
115
metrics.removeSensor(getQuotaSensorName(QuotaType .Produce , metricsTags))
117
116
metrics.removeSensor(getQuotaSensorName(QuotaType .Fetch , metricsTags))
118
- metrics.removeSensor(getThrottleTimeSensorName(QuotaType .Request , metricsTags))
117
+ metrics.removeSensor(getThrottleTimeSensorName(QuotaType .RequestRate , metricsTags))
119
118
metrics.removeSensor(getThrottleTimeSensorName(QuotaType .Produce , metricsTags))
120
119
metrics.removeSensor(getThrottleTimeSensorName(QuotaType .Fetch , metricsTags))
121
120
return
122
121
}
123
122
124
123
val allMetrics = metrics.metrics()
125
124
126
- val requestMetrics = allMetrics.get(clientQuotaMetricName(QuotaType .Request , metricsTags))
127
- if (requestMetrics != null ) {
128
- requestMetrics .config(getQuotaMetricConfig(quotaLimit(QuotaType .Request )))
125
+ val requestRateMetric = allMetrics.get(clientQuotaMetricName(QuotaType .RequestRate , metricsTags))
126
+ if (requestRateMetric != null ) {
127
+ requestRateMetric .config(getQuotaMetricConfig(quotaLimit(QuotaType .RequestRate )))
129
128
}
130
129
131
- val produceMetrics = allMetrics.get(clientQuotaMetricName(QuotaType .Produce , metricsTags))
132
- if (produceMetrics != null ) {
133
- produceMetrics .config(getQuotaMetricConfig(quotaLimit(QuotaType .Produce )))
130
+ val produceMetric = allMetrics.get(clientQuotaMetricName(QuotaType .Produce , metricsTags))
131
+ if (produceMetric != null ) {
132
+ produceMetric .config(getQuotaMetricConfig(quotaLimit(QuotaType .Produce )))
134
133
}
135
134
136
- val fetchMetrics = allMetrics.get(clientQuotaMetricName(QuotaType .Fetch , metricsTags))
137
- if (fetchMetrics != null ) {
138
- fetchMetrics .config(getQuotaMetricConfig(quotaLimit(QuotaType .Fetch )))
135
+ val fetchMetric = allMetrics.get(clientQuotaMetricName(QuotaType .Fetch , metricsTags))
136
+ if (fetchMetric != null ) {
137
+ fetchMetric .config(getQuotaMetricConfig(quotaLimit(QuotaType .Fetch )))
139
138
}
140
139
}
141
140
}
142
141
142
+ def updateQuota (quotaType : QuotaType , quota : Double ): Unit = {
143
+ // update the quota in the config first to make sure the new quota will be used if {@link #updateQuotaMetricConfigs} is called
144
+ quotaType match {
145
+ case QuotaType .RequestRate => config.requestRateQuota(quota)
146
+ case QuotaType .Produce => config.produceQuota(quota)
147
+ case QuotaType .Fetch => config.fetchQuota(quota)
148
+ case _ => throw new IllegalArgumentException (s " Unknown quota type $quotaType" )
149
+ }
150
+
151
+ // update the metric config
152
+ val allMetrics = metrics.metrics()
153
+ val metric = allMetrics.get(clientQuotaMetricName(quotaType, metricsTags))
154
+ if (metric != null ) {
155
+ metric.config(getQuotaMetricConfig(quotaLimit(quotaType)))
156
+ }
157
+ }
158
+
143
159
def throttle (
144
160
quotaType : QuotaType ,
145
161
throttleCallback : ThrottleCallback ,
@@ -162,7 +178,7 @@ class BrokerQuotaManager(private val config: BrokerQuotaManagerConfig,
162
178
s " $quotaType- ${metricTagsToSensorSuffix(metricTags)}"
163
179
164
180
private def quotaLimit (quotaType : QuotaType ): Double = {
165
- if (quotaType == QuotaType .Request ) config.requestQuota
181
+ if (quotaType == QuotaType .RequestRate ) config.requestRateQuota
166
182
else if (quotaType == QuotaType .Produce ) config.produceQuota
167
183
else if (quotaType == QuotaType .Fetch ) config.fetchQuota
168
184
else throw new IllegalArgumentException (s " Unknown quota type $quotaType" )
0 commit comments