forked from apache/kafka
-
Notifications
You must be signed in to change notification settings - Fork 11
Topicthrottling #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wuqingjun
wants to merge
3
commits into
microsoft:azpubsub-release-2-4-1
Choose a base branch
from
wuqingjun:topicthrottling
base: azpubsub-release-2-4-1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Topicthrottling #51
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
azpubsub/src/main/java/com/microsoft/azpubsub/security/auth/ThreadCounterTimerTask.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.microsoft.azpubsub.security.auth; | ||
|
|
||
| import java.util.TimerTask; | ||
|
|
||
| public class ThreadCounterTimerTask extends TimerTask { | ||
| private String topicName = null; | ||
| private String clientId = null; | ||
| private int threadCount = 0; | ||
| private Long ioThreadId = 0L; | ||
| private int throttlingLevel = 0; | ||
| private Long intervalInMs = 300000L; | ||
| private TopicThreadCounter topicThreadCounterInstance = null; | ||
|
|
||
| public ThreadCounterTimerTask(long interval, int level) { | ||
| intervalInMs = interval; | ||
| throttlingLevel = level; | ||
| topicThreadCounterInstance = TopicThreadCounter.getInstance(this.intervalInMs, this.throttlingLevel); | ||
| } | ||
|
|
||
| public void setTopicName(String topic) { | ||
| topicName = topic; | ||
| } | ||
|
|
||
| public void setClientId(String client) { clientId = client; } | ||
|
|
||
| public void setIoThreadId(Long threadId) { | ||
| ioThreadId = threadId; | ||
| } | ||
|
|
||
| public int getThreadCount() { | ||
| return threadCount; | ||
| } | ||
|
|
||
| public void setThrottlingLevel (int level) { throttlingLevel = level; } | ||
| public int getThrottlingLevel () { return throttlingLevel; } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| if(null != topicName) { | ||
| threadCount = topicThreadCounterInstance.add(topicName, System.currentTimeMillis(), ioThreadId, clientId); | ||
| } | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
azpubsub/src/main/java/com/microsoft/azpubsub/security/auth/TopicThreadCounter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package com.microsoft.azpubsub.security.auth; | ||
|
|
||
| import java.util.*; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| public class TopicThreadCounter { | ||
| private static TopicThreadCounter instance = null; | ||
| private static Object lock = new Object(); | ||
| private Long interval = null; | ||
| private int throttlingLevel = 0; | ||
| ConcurrentHashMap<String, TreeMap<Long, Long>> topicThreadMap = new ConcurrentHashMap<>(); | ||
|
|
||
| public TopicThreadCounter(Long intvl, int level) { | ||
| interval = intvl; | ||
| throttlingLevel = level; | ||
| } | ||
|
|
||
| static TopicThreadCounter getInstance(Long interval, int level) { | ||
| synchronized (lock) { | ||
| if(null == instance) { | ||
| instance = new TopicThreadCounter(interval, level); | ||
| } | ||
| } | ||
| return instance; | ||
| } | ||
|
|
||
| public int add(String topic, Long currentTimeInMs, Long threadId, String clientId) { | ||
| String key = TopicThreadCounter.makeKey(this.throttlingLevel, topic, clientId, threadId); | ||
| if(!topicThreadMap.containsKey(key)) { | ||
| topicThreadMap.put(key, new TreeMap<>()); | ||
| } | ||
| topicThreadMap.get(key).put(currentTimeInMs, threadId); | ||
| NavigableMap<Long, Long> subMap= topicThreadMap.get(key).tailMap(currentTimeInMs - interval, false); | ||
| HashSet<Long> hs = new HashSet<>(); | ||
| for(Map.Entry element: subMap.entrySet()) { | ||
| hs.add((Long)element.getValue()); | ||
| } | ||
| topicThreadMap.put(key, new TreeMap<>(subMap)); | ||
| return hs.size(); | ||
| } | ||
|
|
||
| public static String makeKey(int throttlingLevel, String topic, String clientId, Long threaId) { | ||
| if(1 == throttlingLevel) return String.format("ClientId:%s|ThreadId:%d|Topic:%s", clientId, threaId, topic); | ||
| else if(2 == throttlingLevel) return String.format("ClientId:%s|ThreadId:%d", clientId, threaId); | ||
| else if(3 == throttlingLevel) return String.format("Topic:%s|ThreadId:%d", topic, threaId); | ||
| return topic; | ||
| } | ||
| } |
135 changes: 135 additions & 0 deletions
135
azpubsub/src/main/scala/com/microsoft/azpubsub/security/auth/AzPubSubAclAuthorizerV2.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package com.microsoft.azpubsub.security.auth | ||
|
|
||
| import java.net.InetAddress | ||
| import java.util | ||
| import java.util.Timer | ||
| import java.util.concurrent._ | ||
|
|
||
| import com.yammer.metrics.core.{Meter, MetricName} | ||
| import kafka.metrics.KafkaMetricsGroup | ||
| import kafka.security.authorizer.AclAuthorizer | ||
| import kafka.utils.Logging | ||
| import org.apache.kafka.common.security.auth.{KafkaPrincipal, SecurityProtocol} | ||
| import org.apache.kafka.server.authorizer.{Action, AuthorizableRequestContext, AuthorizationResult} | ||
|
|
||
| import scala.collection.JavaConverters.asScalaSetConverter | ||
| import scala.collection.mutable | ||
|
|
||
| /* | ||
| * AzPubSub ACL Authorizer to handle the certificate & role based principal type | ||
| */ | ||
|
|
||
| object AzPubSubAclAuthorizerV2 { | ||
| val configThrottlingLevel = "azpubsub.qps.throttling.level" | ||
| val configTopicThrottlingQps = "azpubsub.topic.max.qps" | ||
| val configClientidTopicThrottlingQps = "azpubsub.clientid.topic.max.qps" | ||
| val configMetterSuccessRatePerSec = "AuthorizerSuccessPerSec" | ||
| val configMetterFailureRatePerSec = "AuthorizerFailurePerSec" | ||
| val configTimerTaskExecutionInterval = "azpubsub.timer.task.execution.interval.in.ms" | ||
| } | ||
|
|
||
|
|
||
| class AzPubSubAclAuthorizerV2 extends AclAuthorizer with Logging with KafkaMetricsGroup { | ||
| override def metricName(name: String, metricTags: scala.collection.Map[String, String]): MetricName = { | ||
| explicitMetricName("azpubsub.security", "AuthorizerMetrics", name, metricTags) | ||
| } | ||
|
|
||
| override def configure(javaConfigs: util.Map[String, _]): Unit = { | ||
| super.configure(javaConfigs) | ||
| config = AzPubSubConfig.fromProps(javaConfigs) | ||
| throttlingLevel = config.getInt(AzPubSubAclAuthorizerV2.configThrottlingLevel) | ||
| throttlingTopicQps = config.getInt(AzPubSubAclAuthorizerV2.configTopicThrottlingQps) | ||
| throttlingClientIdTopicQps = config.getInt(AzPubSubAclAuthorizerV2.configTopicThrottlingQps) | ||
| timerTaskExecutionInterval = config.getLong(AzPubSubAclAuthorizerV2.configTimerTaskExecutionInterval) | ||
| topicThreadCounterTimerTask = new ThreadCounterTimerTask(timerTaskExecutionInterval, throttlingLevel) | ||
| trigger.scheduleAtFixedRate(topicThreadCounterTimerTask, 2, timerTaskExecutionInterval) | ||
| topicThreadCounterTimerTask.setIoThreadId(Thread.currentThread().getId) | ||
| } | ||
|
|
||
| private var config: AzPubSubConfig = null | ||
| private var throttlingLevel: Int = 0 | ||
| private var throttlingTopicQps: Int = 0 | ||
| private var throttlingClientIdTopicQps: Int = 0 | ||
| private var timerTaskExecutionInterval: Long = 0 | ||
| private val successRate: Meter = newMeter(AzPubSubAclAuthorizerV2.configMetterSuccessRatePerSec, "success", TimeUnit.SECONDS) | ||
| private val failureRate: Meter = newMeter(AzPubSubAclAuthorizerV2.configMetterFailureRatePerSec, "failure", TimeUnit.SECONDS) | ||
| private var topicThreadCounterTimerTask : ThreadCounterTimerTask = null | ||
| private val trigger: Timer = new Timer(true) | ||
|
|
||
| var ints = new mutable.HashMap[String, mutable.TreeSet[Long]] | ||
|
|
||
| override def authorize(requestContext: AuthorizableRequestContext, actions: util.List[Action]): util.List[AuthorizationResult] = { | ||
| if(throttlingLevel > 0) { | ||
| actions.forEach( a => if( a.resourcePattern().resourceType() == org.apache.kafka.common.resource.ResourceType.TOPIC) { | ||
| topicThreadCounterTimerTask.setTopicName(a.resourcePattern.name()) | ||
| topicThreadCounterTimerTask.setClientId(requestContext.clientId()) | ||
|
|
||
| val key = makeKey(a.resourcePattern().name(), requestContext.clientId()) | ||
| if(!ints.contains(key)) ints.put(key, new mutable.TreeSet[Long]()) | ||
|
|
||
| val threadCount = Math.max(topicThreadCounterTimerTask.getThreadCount, 1) | ||
|
|
||
| var count = 1 | ||
| while (throttlingLevel == 1 && ints.get(key).size * threadCount > throttlingTopicQps || throttlingLevel == 2 && ints.get(key).size * threadCount > throttlingClientIdTopicQps) { | ||
| val pivot = ints.get(key).get.minBy(x => x > System.currentTimeMillis - 1000) | ||
| val (_, after) = ints.get(key).get.partition(x => x > pivot) | ||
| ints.put(key, after) | ||
| Thread.sleep(count) | ||
| count *= 2 | ||
| } | ||
|
|
||
| ints.get(key).get += System.currentTimeMillis | ||
| } ) | ||
| } | ||
|
|
||
| var res : util.List[AuthorizationResult] = null | ||
|
|
||
| if (requestContext.principal().getClass == classOf[AzPubSubPrincipal]) { | ||
| val tmpPrincipal = requestContext.principal().asInstanceOf[AzPubSubPrincipal] | ||
| for(role <- tmpPrincipal.getRoles.asScala) { | ||
| val context : AuthorizableRequestContext = new AuthorizableRequestContext { | ||
| override def listenerName(): String = requestContext.listenerName() | ||
|
|
||
| override def securityProtocol(): SecurityProtocol = requestContext.securityProtocol() | ||
|
|
||
| override def principal(): KafkaPrincipal = { | ||
| new KafkaPrincipal(tmpPrincipal.getPrincipalType, role) | ||
| } | ||
|
|
||
| override def clientAddress(): InetAddress = requestContext.clientAddress() | ||
|
|
||
| override def requestType(): Int = requestContext.requestType() | ||
|
|
||
| override def requestVersion(): Int = requestContext.requestVersion() | ||
|
|
||
| override def clientId(): String = requestContext.clientId() | ||
|
|
||
| override def correlationId(): Int = requestContext.correlationId() | ||
| } | ||
| res = super.authorize(context, actions) | ||
| if (res.contains(AuthorizationResult.ALLOWED) ) { | ||
| successRate.mark() | ||
| res | ||
| } | ||
| } | ||
| failureRate.mark() | ||
| return res | ||
| } | ||
| res = super.authorize(requestContext, actions) | ||
| if(null != res && res.contains(AuthorizationResult.ALLOWED)) { | ||
| successRate.mark() | ||
| } | ||
| else { | ||
| failureRate.mark() | ||
| } | ||
| return res | ||
| } | ||
|
|
||
| private def makeKey(topic: String, clientId: String) : String = { | ||
| throttlingLevel match { | ||
| case 1 => String.format("ClientId:%s|Topic:%s", topic, clientId) | ||
| case 2 => String.format("ClientId:%s", clientId) | ||
| case _ => String.format("Topic:%s", topic) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be reverted.