Skip to content
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

KAFKA-3682 ArrayIndexOutOfBoundsException thrown by #1352

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/src/main/scala/kafka/log/OffsetMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ class SkimpyOffsetMap(val memory: Int, val hashAlgorithm: String = "MD5") extend
// search for the hash of this key by repeated probing until we find the hash we are looking for or we find an empty slot
var attempt = 0
var pos = 0
//we need to guard against attempt integer overflow if the map is full
//limit attempt to number of slots once positionOf(..) enters linear search mode
val maxAttempts = slots + hashSize - 4
do {
if(attempt >= maxAttempts)
return -1L
pos = positionOf(hash1, attempt)
bytes.position(pos)
if(isEmpty(pos))
Expand Down
14 changes: 13 additions & 1 deletion core/src/test/scala/unit/kafka/log/OffsetMapTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,19 @@ class OffsetMapTest extends JUnitSuite {
assertEquals(map.get(key(i)), -1L)
}

def key(key: Int) = ByteBuffer.wrap(key.toString.getBytes)
@Test
def testGetWhenFull() {
val map = new SkimpyOffsetMap(4096)
var i = 37L //any value would do
while (map.size < map.slots) {
map.put(key(i), i)
i = i + 1L
}
assertEquals(map.get(key(i)), -1L)
assertEquals(map.get(key(i-1L)), i-1L)
}

def key(key: Long) = ByteBuffer.wrap(key.toString.getBytes)

def validateMap(items: Int, loadFactor: Double = 0.5): SkimpyOffsetMap = {
val map = new SkimpyOffsetMap((items/loadFactor * 24).toInt)
Expand Down