This page is meant as a template for writing a KIP. To create a KIP choose Tools->Copy on this page and modify with your content and replace the heading with the next KIP number and a description of your issue. Replace anything in italics with your own description.
Status
Current state:Under Discussion
Discussion thread: here
JIRA: SensorAccess.getOrCreate should be more efficient
Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).
Motivation
.Improve Kafka performance in request handling and make the source code cleaner.
Public Interfaces
There's no public interfaces to be defined. The change proposed here is to remove a read lock which is completely unnecessary. The read lock is called for each request (at least) from producer, which is really expensive.
Proposed Changes
The getOrCreate of SensorAccess class under this folder: core\src\main\scala\kafka\server is using a read like this:
lock.readLock().lock()
The lock is used to prevent race condition: when a new sensor is created by a writer thread, and the "sensor" variable is not null more, but the initialization of the new sensor object is not finished, if at this moment another thread may gets the sensor object and use it before the sensor initialization is finished.
But this read lock can be avoided by firstly assigning the new sensor to a temporary variable say "tmp", and then initialized tmp, finally assign temp to the "sensor" variable. By doing this, we can get rid of the read lock.
Below code and screen shows the code change:
Compatibility, Deprecation, and Migration Plan
N/A
Rejected Alternatives
If there are alternative ways of accomplishing the same thing, what were they? The purpose of this section is to motivate why the design is the way it is and not some other way.