{"action":"created","comment":{"url":"https://api.github.com/repos/apache/kafka/pulls/comments/78685369","id":78685369,"diff_hunk":"@@ -0,0 +1,208 @@\n+/**\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ **/\n+\n+package kafka.api\n+\n+import java.util.Properties\n+\n+import kafka.consumer.SimpleConsumer\n+import kafka.server.{ClientQuotaManager, QuotaConfigOverride, KafkaConfig, KafkaServer, QuotaId}\n+import kafka.utils.TestUtils\n+import org.apache.kafka.clients.consumer.{ConsumerConfig, KafkaConsumer}\n+import org.apache.kafka.clients.producer._\n+import org.apache.kafka.clients.producer.internals.ErrorLoggingCallback\n+import org.apache.kafka.common.MetricName\n+import org.apache.kafka.common.metrics.{Quota, KafkaMetric}\n+import org.apache.kafka.common.protocol.ApiKeys\n+import org.junit.Assert._\n+import org.junit.{After, Before, Test}\n+\n+import scala.collection.JavaConversions._\n+import scala.collection.JavaConverters._\n+import scala.collection.Map\n+import scala.collection.mutable\n+\n+abstract class BaseQuotaTest extends IntegrationTestHarness {\n+\n+  def userPrincipal : String\n+  def producerQuotaId : QuotaId\n+  def consumerQuotaId : QuotaId\n+  def overrideQuotas(producerQuota: Long, consumerQuota: Long)\n+  def removeQuotaOverrides()\n+\n+  override val serverCount = 2\n+  val producerCount = 1\n+  val consumerCount = 1\n+\n+  private val producerBufferSize = 300000\n+  protected val producerClientId = \"QuotasTestProducer-1\"\n+  protected val consumerClientId = \"QuotasTestConsumer-1\"\n+\n+  this.serverConfig.setProperty(KafkaConfig.ControlledShutdownEnableProp, \"false\")\n+  this.serverConfig.setProperty(KafkaConfig.OffsetsTopicReplicationFactorProp, \"2\")\n+  this.serverConfig.setProperty(KafkaConfig.OffsetsTopicPartitionsProp, \"1\")\n+  this.serverConfig.setProperty(KafkaConfig.GroupMinSessionTimeoutMsProp, \"100\")\n+  this.serverConfig.setProperty(KafkaConfig.GroupMaxSessionTimeoutMsProp, \"30000\")\n+  this.producerConfig.setProperty(ProducerConfig.ACKS_CONFIG, \"0\")\n+  this.producerConfig.setProperty(ProducerConfig.BUFFER_MEMORY_CONFIG, producerBufferSize.toString)\n+  this.producerConfig.setProperty(ProducerConfig.CLIENT_ID_CONFIG, producerClientId)\n+  this.consumerConfig.setProperty(ConsumerConfig.GROUP_ID_CONFIG, \"QuotasTest\")\n+  this.consumerConfig.setProperty(ConsumerConfig.MAX_PARTITION_FETCH_BYTES_CONFIG, 4096.toString)\n+  this.consumerConfig.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, \"earliest\")\n+  this.consumerConfig.setProperty(ConsumerConfig.CLIENT_ID_CONFIG, consumerClientId)\n+\n+  // Low enough quota that a producer sending a small payload in a tight loop should get throttled\n+  val defaultProducerQuota = 8000\n+  val defaultConsumerQuota = 2500\n+\n+  var replicaConsumers = mutable.Buffer[SimpleConsumer]()\n+\n+  var leaderNode: KafkaServer = null\n+  var followerNode: KafkaServer = null\n+  private val topic1 = \"topic-1\"\n+\n+  @Before\n+  override def setUp() {\n+    super.setUp()\n+\n+    val numPartitions = 1\n+    val leaders = TestUtils.createTopic(zkUtils, topic1, numPartitions, serverCount, servers)\n+    leaderNode = if (leaders(0).get == servers.head.config.brokerId) servers.head else servers(1)\n+    followerNode = if (leaders(0).get != servers.head.config.brokerId) servers.head else servers(1)\n+    assertTrue(\"Leader of all partitions of the topic should exist\", leaders.values.forall(leader => leader.isDefined))\n+\n+    // Create replica consumers with the same clientId as the high level consumer. These requests should never be throttled\n+    replicaConsumers += new SimpleConsumer(\"localhost\", leaderNode.boundPort(), 1000000, 64*1024, consumerClientId)\n+  }\n+\n+  @After\n+  override def tearDown() {\n+    replicaConsumers.foreach( _.close )\n+    super.tearDown()\n+  }\n+\n+  @Test\n+  def testThrottledProducerConsumer() {\n+    val allMetrics: mutable.Map[MetricName, KafkaMetric] = leaderNode.metrics.metrics().asScala\n+\n+    val numRecords = 1000\n+    produce(producers.head, numRecords)\n+\n+    val producerMetricName = throttleMetricName(ApiKeys.PRODUCE, producerQuotaId)\n+    assertTrue(\"Should have been throttled\", allMetrics(producerMetricName).value() > 0)\n+\n+    // Consumer should read in a bursty manner and get throttled immediately\n+    consume(consumers.head, numRecords)\n+    // The replica consumer should not be throttled also. Create a fetch request which will exceed the quota immediately\n+    val request = new FetchRequestBuilder().addFetch(topic1, 0, 0, 1024*1024).replicaId(followerNode.config.brokerId).build()\n+    replicaConsumers.head.fetch(request)\n+    val consumerMetricName = throttleMetricName(ApiKeys.FETCH, consumerQuotaId)\n+    assertTrue(\"Should have been throttled\", allMetrics(consumerMetricName).value() > 0)\n+  }\n+\n+  @Test\n+  def testProducerConsumerOverrideUnthrottled() {\n+    // Give effectively unlimited quota for producer and consumer\n+    val props = new Properties()\n+    props.put(QuotaConfigOverride.ProducerOverride, Long.MaxValue.toString)\n+    props.put(QuotaConfigOverride.ConsumerOverride, Long.MaxValue.toString)\n+\n+    overrideQuotas(Long.MaxValue, Long.MaxValue)\n+    waitForQuotaUpdate(Long.MaxValue, Long.MaxValue)\n+\n+    val allMetrics: mutable.Map[MetricName, KafkaMetric] = leaderNode.metrics.metrics().asScala\n+    val numRecords = 1000\n+    produce(producers.head, numRecords)\n+    val producerMetricName = throttleMetricName(ApiKeys.PRODUCE, producerQuotaId)\n+    assertEquals(\"Should not have been throttled\", 0.0, allMetrics(producerMetricName).value(), 0.0)\n+\n+    // The \"client\" consumer does not get throttled.\n+    consume(consumers.head, numRecords)\n+    // The replica consumer should not be throttled also. Create a fetch request which will exceed the quota immediately\n+    val request = new FetchRequestBuilder().addFetch(topic1, 0, 0, 1024*1024).replicaId(followerNode.config.brokerId).build()\n+    replicaConsumers.head.fetch(request)","path":"core/src/test/scala/integration/kafka/api/BaseQuotaTest.scala","position":134,"original_position":134,"commit_id":"2dd374ff2c92ad6a50ddabd202e5681e98fca45d","original_commit_id":"2dd374ff2c92ad6a50ddabd202e5681e98fca45d","user":{"login":"junrao","id":132330,"avatar_url":"https://avatars.githubusercontent.com/u/132330?v=3","gravatar_id":"","url":"https://api.github.com/users/junrao","html_url":"https://github.com/junrao","followers_url":"https://api.github.com/users/junrao/followers","following_url":"https://api.github.com/users/junrao/following{/other_user}","gists_url":"https://api.github.com/users/junrao/gists{/gist_id}","starred_url":"https://api.github.com/users/junrao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/junrao/subscriptions","organizations_url":"https://api.github.com/users/junrao/orgs","repos_url":"https://api.github.com/users/junrao/repos","events_url":"https://api.github.com/users/junrao/events{/privacy}","received_events_url":"https://api.github.com/users/junrao/received_events","type":"User","site_admin":false},"body":"Ditto as the above.","created_at":"2016-09-14T04:41:23Z","updated_at":"2016-09-14T04:41:23Z","html_url":"https://github.com/apache/kafka/pull/1753#discussion_r78685369","pull_request_url":"https://api.github.com/repos/apache/kafka/pulls/1753","_links":{"self":{"href":"https://api.github.com/repos/apache/kafka/pulls/comments/78685369"},"html":{"href":"https://github.com/apache/kafka/pull/1753#discussion_r78685369"},"pull_request":{"href":"https://api.github.com/repos/apache/kafka/pulls/1753"}}},"pull_request":{"url":"https://api.github.com/repos/apache/kafka/pulls/1753","id":81635693,"html_url":"https://github.com/apache/kafka/pull/1753","diff_url":"https://github.com/apache/kafka/pull/1753.diff","patch_url":"https://github.com/apache/kafka/pull/1753.patch","issue_url":"https://api.github.com/repos/apache/kafka/issues/1753","number":1753,"state":"open","locked":false,"title":"KAFKA-3492: Secure quotas for authenticated users","user":{"login":"rajinisivaram","id":13164074,"avatar_url":"https://avatars.githubusercontent.com/u/13164074?v=3","gravatar_id":"","url":"https://api.github.com/users/rajinisivaram","html_url":"https://github.com/rajinisivaram","followers_url":"https://api.github.com/users/rajinisivaram/followers","following_url":"https://api.github.com/users/rajinisivaram/following{/other_user}","gists_url":"https://api.github.com/users/rajinisivaram/gists{/gist_id}","starred_url":"https://api.github.com/users/rajinisivaram/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rajinisivaram/subscriptions","organizations_url":"https://api.github.com/users/rajinisivaram/orgs","repos_url":"https://api.github.com/users/rajinisivaram/repos","events_url":"https://api.github.com/users/rajinisivaram/events{/privacy}","received_events_url":"https://api.github.com/users/rajinisivaram/received_events","type":"User","site_admin":false},"body":"Implementation and tests for secure quotas at <user> and <user, client-id> levels as described in KIP-55. Also adds dynamic default quotas for <client-id>, <user> and <user-client-id>. For each client connection, the most specific quota matching the connection is used, with user quota taking precedence over client-id quota.","created_at":"2016-08-17T12:43:49Z","updated_at":"2016-09-14T04:41:23Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8a4d16f3efe484484d8807a8ce5d3d68f40951dc","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/apache/kafka/pulls/1753/commits","review_comments_url":"https://api.github.com/repos/apache/kafka/pulls/1753/comments","review_comment_url":"https://api.github.com/repos/apache/kafka/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/kafka/issues/1753/comments","statuses_url":"https://api.github.com/repos/apache/kafka/statuses/2dd374ff2c92ad6a50ddabd202e5681e98fca45d","head":{"label":"rajinisivaram:KAFKA-3492","ref":"KAFKA-3492","sha":"2dd374ff2c92ad6a50ddabd202e5681e98fca45d","user":{"login":"rajinisivaram","id":13164074,"avatar_url":"https://avatars.githubusercontent.com/u/13164074?v=3","gravatar_id":"","url":"https://api.github.com/users/rajinisivaram","html_url":"https://github.com/rajinisivaram","followers_url":"https://api.github.com/users/rajinisivaram/followers","following_url":"https://api.github.com/users/rajinisivaram/following{/other_user}","gists_url":"https://api.github.com/users/rajinisivaram/gists{/gist_id}","starred_url":"https://api.github.com/users/rajinisivaram/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rajinisivaram/subscriptions","organizations_url":"https://api.github.com/users/rajinisivaram/orgs","repos_url":"https://api.github.com/users/rajinisivaram/repos","events_url":"https://api.github.com/users/rajinisivaram/events{/privacy}","received_events_url":"https://api.github.com/users/rajinisivaram/received_events","type":"User","site_admin":false},"repo":{"id":42296329,"name":"kafka","full_name":"rajinisivaram/kafka","owner":{"login":"rajinisivaram","id":13164074,"avatar_url":"https://avatars.githubusercontent.com/u/13164074?v=3","gravatar_id":"","url":"https://api.github.com/users/rajinisivaram","html_url":"https://github.com/rajinisivaram","followers_url":"https://api.github.com/users/rajinisivaram/followers","following_url":"https://api.github.com/users/rajinisivaram/following{/other_user}","gists_url":"https://api.github.com/users/rajinisivaram/gists{/gist_id}","starred_url":"https://api.github.com/users/rajinisivaram/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rajinisivaram/subscriptions","organizations_url":"https://api.github.com/users/rajinisivaram/orgs","repos_url":"https://api.github.com/users/rajinisivaram/repos","events_url":"https://api.github.com/users/rajinisivaram/events{/privacy}","received_events_url":"https://api.github.com/users/rajinisivaram/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/rajinisivaram/kafka","description":"Mirror of Apache Kafka","fork":true,"url":"https://api.github.com/repos/rajinisivaram/kafka","forks_url":"https://api.github.com/repos/rajinisivaram/kafka/forks","keys_url":"https://api.github.com/repos/rajinisivaram/kafka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rajinisivaram/kafka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rajinisivaram/kafka/teams","hooks_url":"https://api.github.com/repos/rajinisivaram/kafka/hooks","issue_events_url":"https://api.github.com/repos/rajinisivaram/kafka/issues/events{/number}","events_url":"https://api.github.com/repos/rajinisivaram/kafka/events","assignees_url":"https://api.github.com/repos/rajinisivaram/kafka/assignees{/user}","branches_url":"https://api.github.com/repos/rajinisivaram/kafka/branches{/branch}","tags_url":"https://api.github.com/repos/rajinisivaram/kafka/tags","blobs_url":"https://api.github.com/repos/rajinisivaram/kafka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rajinisivaram/kafka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rajinisivaram/kafka/git/refs{/sha}","trees_url":"https://api.github.com/repos/rajinisivaram/kafka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rajinisivaram/kafka/statuses/{sha}","languages_url":"https://api.github.com/repos/rajinisivaram/kafka/languages","stargazers_url":"https://api.github.com/repos/rajinisivaram/kafka/stargazers","contributors_url":"https://api.github.com/repos/rajinisivaram/kafka/contributors","subscribers_url":"https://api.github.com/repos/rajinisivaram/kafka/subscribers","subscription_url":"https://api.github.com/repos/rajinisivaram/kafka/subscription","commits_url":"https://api.github.com/repos/rajinisivaram/kafka/commits{/sha}","git_commits_url":"https://api.github.com/repos/rajinisivaram/kafka/git/commits{/sha}","comments_url":"https://api.github.com/repos/rajinisivaram/kafka/comments{/number}","issue_comment_url":"https://api.github.com/repos/rajinisivaram/kafka/issues/comments{/number}","contents_url":"https://api.github.com/repos/rajinisivaram/kafka/contents/{+path}","compare_url":"https://api.github.com/repos/rajinisivaram/kafka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rajinisivaram/kafka/merges","archive_url":"https://api.github.com/repos/rajinisivaram/kafka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rajinisivaram/kafka/downloads","issues_url":"https://api.github.com/repos/rajinisivaram/kafka/issues{/number}","pulls_url":"https://api.github.com/repos/rajinisivaram/kafka/pulls{/number}","milestones_url":"https://api.github.com/repos/rajinisivaram/kafka/milestones{/number}","notifications_url":"https://api.github.com/repos/rajinisivaram/kafka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rajinisivaram/kafka/labels{/name}","releases_url":"https://api.github.com/repos/rajinisivaram/kafka/releases{/id}","deployments_url":"https://api.github.com/repos/rajinisivaram/kafka/deployments","created_at":"2015-09-11T08:16:17Z","updated_at":"2016-01-29T12:19:37Z","pushed_at":"2016-09-13T20:12:40Z","git_url":"git://github.com/rajinisivaram/kafka.git","ssh_url":"git@github.com:rajinisivaram/kafka.git","clone_url":"https://github.com/rajinisivaram/kafka.git","svn_url":"https://github.com/rajinisivaram/kafka","homepage":null,"size":32572,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"trunk"}},"base":{"label":"apache:trunk","ref":"trunk","sha":"a7ab9cb83d2c47d980b0b9f6b702e01bd1cf4139","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":2211243,"name":"kafka","full_name":"apache/kafka","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/kafka","description":"Mirror of Apache Kafka","fork":false,"url":"https://api.github.com/repos/apache/kafka","forks_url":"https://api.github.com/repos/apache/kafka/forks","keys_url":"https://api.github.com/repos/apache/kafka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/kafka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/kafka/teams","hooks_url":"https://api.github.com/repos/apache/kafka/hooks","issue_events_url":"https://api.github.com/repos/apache/kafka/issues/events{/number}","events_url":"https://api.github.com/repos/apache/kafka/events","assignees_url":"https://api.github.com/repos/apache/kafka/assignees{/user}","branches_url":"https://api.github.com/repos/apache/kafka/branches{/branch}","tags_url":"https://api.github.com/repos/apache/kafka/tags","blobs_url":"https://api.github.com/repos/apache/kafka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/kafka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/kafka/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/kafka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/kafka/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/kafka/languages","stargazers_url":"https://api.github.com/repos/apache/kafka/stargazers","contributors_url":"https://api.github.com/repos/apache/kafka/contributors","subscribers_url":"https://api.github.com/repos/apache/kafka/subscribers","subscription_url":"https://api.github.com/repos/apache/kafka/subscription","commits_url":"https://api.github.com/repos/apache/kafka/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/kafka/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/kafka/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/kafka/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/kafka/contents/{+path}","compare_url":"https://api.github.com/repos/apache/kafka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/kafka/merges","archive_url":"https://api.github.com/repos/apache/kafka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/kafka/downloads","issues_url":"https://api.github.com/repos/apache/kafka/issues{/number}","pulls_url":"https://api.github.com/repos/apache/kafka/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/kafka/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/kafka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/kafka/labels{/name}","releases_url":"https://api.github.com/repos/apache/kafka/releases{/id}","deployments_url":"https://api.github.com/repos/apache/kafka/deployments","created_at":"2011-08-15T18:06:16Z","updated_at":"2016-09-14T03:24:07Z","pushed_at":"2016-09-14T01:18:40Z","git_url":"git://github.com/apache/kafka.git","ssh_url":"git@github.com:apache/kafka.git","clone_url":"https://github.com/apache/kafka.git","svn_url":"https://github.com/apache/kafka","homepage":null,"size":48240,"stargazers_count":3755,"watchers_count":3755,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":2332,"mirror_url":"git://git.apache.org/kafka.git","open_issues_count":250,"forks":2332,"open_issues":250,"watchers":3755,"default_branch":"trunk"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/kafka/pulls/1753"},"html":{"href":"https://github.com/apache/kafka/pull/1753"},"issue":{"href":"https://api.github.com/repos/apache/kafka/issues/1753"},"comments":{"href":"https://api.github.com/repos/apache/kafka/issues/1753/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/kafka/pulls/1753/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/kafka/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/kafka/pulls/1753/commits"},"statuses":{"href":"https://api.github.com/repos/apache/kafka/statuses/2dd374ff2c92ad6a50ddabd202e5681e98fca45d"}}}}
{"actor":{"display_login":"junrao"}}