title: Spark Streaming spark.streaming.backpressure.pid.minRate is per partition or total messages per batch interval?
body: <p>I have spark streaming application that reads messages from Kafka using Spark Direct Streaming (not receiver) approach and process messages per partition.</p>
  <p>In my Kafka partition, sometime we get the messages that take 20 seconds to process 2000 messages and some of the messages takes 7-9 seconds for same no. of messages.</p>
  <p>Given the fluctuation, we turned on the back pressure settings as follows.</p>
  <pre><code>spark.batch.duration=10 seconds
  spark.streaming.kafka.maxRatePerPartition=200
  
  spark.streaming.backpressure.enabled=true
  spark.streaming.backpressure.initialRate=60
  spark.streaming.kafka.maxRatePerPartition=200
  </code></pre>
  <p>and also specified <strong>RateEstimator</strong> with following parameters.
  I don't understand the mathematics of PID but tried different combination and one of them as follows.</p>
  <pre><code>spark.streaming.backpressure.rateEstimator=pid
  spark.streaming.backpressure.pid.minRate=1600
  spark.streaming.backpressure.pid.integral=1
  spark.streaming.backpressure.pid.proportional=25
  spark.streaming.backpressure.pid.derived=1
  </code></pre>
  <p>Initially, spark reads the 2000 messages for 1 partition in RDD but after some time it start reading 800 records. that i think is minRate/2. and then it stays static..
  In the logs, it always print 1600 as new rate.</p>
  <pre><code>2017-01-20 14:55:14 TRACE PIDRateEstimator:67 - New rate = 1600.0
  </code></pre>
  <p>Given my scenario, i have few questions:</p>
  <ol>
  <li>Is <code>spark.streaming.backpressure.pid.minRate</code> per partition or total no. of messages to be read by batch?</li>
  <li>Why reading 800 messages instead 1600??</li>
  <li>Any suggested parameters that reduce the input rate when processing takes long and increase back to something close to maxRatePerPartition when processing is very fast?
  In my example, input rate started with 2000 but when it took long like 20 seconds average, it reduced it to 800 but when 800 messages processed in 3-4 seconds it didn't increase it back to something 1600 or more. This results waste of time and low throughput.</li>
  </ol>
  <p><a href="https://i.stack.imgur.com/6I3N5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6I3N5.png" alt="processing after sometime" /></a>
  <a href="https://i.stack.imgur.com/AonG0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AonG0.png" alt="processing after streaming started" /></a></p>