Skip to content
Merged
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
75 changes: 44 additions & 31 deletions core/src/main/scala/kafka/server/checkpoints/CheckpointFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,48 @@ trait CheckpointFileFormatter[T]{
def fromLine(line: String): Option[T]
}

class CheckpointReadBuffer[T](location: String,
reader: BufferedReader,
version: Int,
formatter: CheckpointFileFormatter[T]) extends Logging {
def read(): Seq[T] = {
def malformedLineException(line: String) =
new IOException(s"Malformed line in checkpoint file ($location): $line'")

var line: String = null
try {
line = reader.readLine()
if (line == null)
return Seq.empty
line.toInt match {
case fileVersion if fileVersion == version =>
line = reader.readLine()
if (line == null)
return Seq.empty
val expectedSize = line.toInt
val entries = mutable.Buffer[T]()
line = reader.readLine()
while (line != null) {
val entry = formatter.fromLine(line)
entry match {
case Some(e) =>
entries += e
line = reader.readLine()
case _ => throw malformedLineException(line)
}
}
if (entries.size != expectedSize)
throw new IOException(s"Expected $expectedSize entries in checkpoint file ($location), but found only ${entries.size}")
entries
case _ =>
throw new IOException(s"Unrecognized version of the checkpoint file ($location): " + version)
}
} catch {
case _: NumberFormatException => throw malformedLineException(line)
}
}
}

class CheckpointFile[T](val file: File,
version: Int,
formatter: CheckpointFileFormatter[T],
Expand Down Expand Up @@ -80,41 +122,12 @@ class CheckpointFile[T](val file: File,
}

def read(): Seq[T] = {
def malformedLineException(line: String) =
new IOException(s"Malformed line in checkpoint file (${file.getAbsolutePath}): $line'")
lock synchronized {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove malformedLineException() in this method, it doesn't seem to be used anymore.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've removed it.

try {
val reader = Files.newBufferedReader(path)
var line: String = null
try {
line = reader.readLine()
if (line == null)
return Seq.empty
line.toInt match {
case fileVersion if fileVersion == version =>
line = reader.readLine()
if (line == null)
return Seq.empty
val expectedSize = line.toInt
val entries = mutable.Buffer[T]()
line = reader.readLine()
while (line != null) {
val entry = formatter.fromLine(line)
entry match {
case Some(e) =>
entries += e
line = reader.readLine()
case _ => throw malformedLineException(line)
}
}
if (entries.size != expectedSize)
throw new IOException(s"Expected $expectedSize entries in checkpoint file (${file.getAbsolutePath}), but found only ${entries.size}")
entries
case _ =>
throw new IOException(s"Unrecognized version of the checkpoint file (${file.getAbsolutePath}): " + version)
}
} catch {
case _: NumberFormatException => throw malformedLineException(line)
val checkpointBuffer = new CheckpointReadBuffer[T](file.getAbsolutePath, reader, version, formatter)
checkpointBuffer.read()
} finally {
reader.close()
}
Expand Down