mirror of https://github.com/apache/kafka.git
KAFKA-171 Do a single write for request sends.
git-svn-id: https://svn.apache.org/repos/asf/incubator/kafka/trunk@1205313 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1280b71a35
commit
cb4486656e
|
@ -20,7 +20,7 @@ package kafka.api
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
import kafka.utils.{nonthreadsafe, Utils}
|
import kafka.utils.{nonthreadsafe, Utils}
|
||||||
import kafka.network.{Send, Request}
|
import kafka.network.{Send, Request}
|
||||||
import java.nio.channels.WritableByteChannel
|
import java.nio.channels.GatheringByteChannel
|
||||||
import kafka.common.ErrorMapping
|
import kafka.common.ErrorMapping
|
||||||
|
|
||||||
object OffsetRequest {
|
object OffsetRequest {
|
||||||
|
@ -85,7 +85,7 @@ private[kafka] class OffsetArraySend(offsets: Array[Long]) extends Send {
|
||||||
|
|
||||||
var complete: Boolean = false
|
var complete: Boolean = false
|
||||||
|
|
||||||
def writeTo(channel: WritableByteChannel): Int = {
|
def writeTo(channel: GatheringByteChannel): Int = {
|
||||||
expectIncomplete()
|
expectIncomplete()
|
||||||
var written = 0
|
var written = 0
|
||||||
if(header.hasRemaining)
|
if(header.hasRemaining)
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
|
|
||||||
package kafka.javaapi.message
|
package kafka.javaapi.message
|
||||||
|
|
||||||
import java.nio.channels.WritableByteChannel
|
import java.nio.channels._
|
||||||
import kafka.message.{MessageAndOffset, InvalidMessageException, Message}
|
import kafka.message.{MessageAndOffset, InvalidMessageException, Message}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -21,7 +21,7 @@ import scala.collection.mutable
|
||||||
import kafka.utils.Logging
|
import kafka.utils.Logging
|
||||||
import kafka.common.{InvalidMessageSizeException, ErrorMapping}
|
import kafka.common.{InvalidMessageSizeException, ErrorMapping}
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
import java.nio.channels.WritableByteChannel
|
import java.nio.channels._
|
||||||
import kafka.utils.IteratorTemplate
|
import kafka.utils.IteratorTemplate
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -71,7 +71,7 @@ class ByteBufferMessageSet(private val buffer: ByteBuffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Write the messages in this set to the given channel */
|
/** Write the messages in this set to the given channel */
|
||||||
def writeTo(channel: WritableByteChannel, offset: Long, size: Long): Long =
|
def writeTo(channel: GatheringByteChannel, offset: Long, size: Long): Long =
|
||||||
channel.write(buffer.duplicate)
|
channel.write(buffer.duplicate)
|
||||||
|
|
||||||
override def iterator: Iterator[MessageAndOffset] = deepIterator
|
override def iterator: Iterator[MessageAndOffset] = deepIterator
|
||||||
|
|
|
@ -100,7 +100,7 @@ class FileMessageSet private[kafka](private[message] val channel: FileChannel,
|
||||||
/**
|
/**
|
||||||
* Write some of this set to the given channel, return the ammount written
|
* Write some of this set to the given channel, return the ammount written
|
||||||
*/
|
*/
|
||||||
def writeTo(destChannel: WritableByteChannel, writeOffset: Long, size: Long): Long =
|
def writeTo(destChannel: GatheringByteChannel, writeOffset: Long, size: Long): Long =
|
||||||
channel.transferTo(offset + writeOffset, scala.math.min(size, sizeInBytes), destChannel)
|
channel.transferTo(offset + writeOffset, scala.math.min(size, sizeInBytes), destChannel)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -89,7 +89,7 @@ abstract class MessageSet extends Iterable[MessageAndOffset] {
|
||||||
/** Write the messages in this set to the given channel starting at the given offset byte.
|
/** Write the messages in this set to the given channel starting at the given offset byte.
|
||||||
* Less than the complete amount may be written, but no more than maxSize can be. The number
|
* Less than the complete amount may be written, but no more than maxSize can be. The number
|
||||||
* of bytes written is returned */
|
* of bytes written is returned */
|
||||||
def writeTo(channel: WritableByteChannel, offset: Long, maxSize: Long): Long
|
def writeTo(channel: GatheringByteChannel, offset: Long, maxSize: Long): Long
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides an iterator over the messages in this set
|
* Provides an iterator over the messages in this set
|
||||||
|
|
|
@ -26,6 +26,10 @@ private[kafka] class BoundedByteBufferSend(val buffer: ByteBuffer) extends Send
|
||||||
|
|
||||||
private var sizeBuffer = ByteBuffer.allocate(4)
|
private var sizeBuffer = ByteBuffer.allocate(4)
|
||||||
|
|
||||||
|
// Avoid possibility of overflow for 2GB-4 byte buffer
|
||||||
|
if(buffer.remaining > Int.MaxValue - sizeBuffer.limit)
|
||||||
|
throw new IllegalArgumentException("Attempt to create a bounded buffer of " + buffer.remaining + " bytes, but the maximum " +
|
||||||
|
"allowable size for a bounded buffer is " + (Int.MaxValue - sizeBuffer.limit) + ".")
|
||||||
sizeBuffer.putInt(buffer.limit)
|
sizeBuffer.putInt(buffer.limit)
|
||||||
sizeBuffer.rewind()
|
sizeBuffer.rewind()
|
||||||
|
|
||||||
|
@ -40,20 +44,13 @@ private[kafka] class BoundedByteBufferSend(val buffer: ByteBuffer) extends Send
|
||||||
buffer.rewind()
|
buffer.rewind()
|
||||||
}
|
}
|
||||||
|
|
||||||
def writeTo(channel: WritableByteChannel): Int = {
|
def writeTo(channel: GatheringByteChannel): Int = {
|
||||||
expectIncomplete()
|
expectIncomplete()
|
||||||
var written = 0
|
var written = channel.write(Array(sizeBuffer, buffer))
|
||||||
// try to write the size if we haven't already
|
|
||||||
if(sizeBuffer.hasRemaining)
|
|
||||||
written += channel.write(sizeBuffer)
|
|
||||||
// try to write the actual buffer itself
|
|
||||||
if(!sizeBuffer.hasRemaining && buffer.hasRemaining)
|
|
||||||
written += channel.write(buffer)
|
|
||||||
// if we are done, mark it off
|
// if we are done, mark it off
|
||||||
if(!buffer.hasRemaining)
|
if(!buffer.hasRemaining)
|
||||||
complete = true
|
complete = true
|
||||||
|
written.asInstanceOf[Int]
|
||||||
written
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ private[kafka] class ByteBufferSend(val buffer: ByteBuffer) extends Send {
|
||||||
|
|
||||||
def this(size: Int) = this(ByteBuffer.allocate(size))
|
def this(size: Int) = this(ByteBuffer.allocate(size))
|
||||||
|
|
||||||
def writeTo(channel: WritableByteChannel): Int = {
|
def writeTo(channel: GatheringByteChannel): Int = {
|
||||||
expectIncomplete()
|
expectIncomplete()
|
||||||
var written = 0
|
var written = 0
|
||||||
written += channel.write(buffer)
|
written += channel.write(buffer)
|
||||||
|
|
|
@ -65,9 +65,9 @@ private[kafka] trait Receive extends Transmission {
|
||||||
*/
|
*/
|
||||||
private[kafka] trait Send extends Transmission {
|
private[kafka] trait Send extends Transmission {
|
||||||
|
|
||||||
def writeTo(channel: WritableByteChannel): Int
|
def writeTo(channel: GatheringByteChannel): Int
|
||||||
|
|
||||||
def writeCompletely(channel: WritableByteChannel): Int = {
|
def writeCompletely(channel: GatheringByteChannel): Int = {
|
||||||
var written = 0
|
var written = 0
|
||||||
while(!complete) {
|
while(!complete) {
|
||||||
written = writeTo(channel)
|
written = writeTo(channel)
|
||||||
|
@ -86,7 +86,7 @@ abstract class MultiSend[S <: Send](val sends: List[S]) extends Send {
|
||||||
private var current = sends
|
private var current = sends
|
||||||
var totalWritten = 0
|
var totalWritten = 0
|
||||||
|
|
||||||
def writeTo(channel: WritableByteChannel): Int = {
|
def writeTo(channel: GatheringByteChannel): Int = {
|
||||||
expectIncomplete
|
expectIncomplete
|
||||||
val written = current.head.writeTo(channel)
|
val written = current.head.writeTo(channel)
|
||||||
totalWritten += written
|
totalWritten += written
|
||||||
|
|
|
@ -44,7 +44,7 @@ private[server] class MessageSetSend(val messages: MessageSet, val errorCode: In
|
||||||
|
|
||||||
def this() = this(MessageSet.Empty)
|
def this() = this(MessageSet.Empty)
|
||||||
|
|
||||||
def writeTo(channel: WritableByteChannel): Int = {
|
def writeTo(channel: GatheringByteChannel): Int = {
|
||||||
expectIncomplete()
|
expectIncomplete()
|
||||||
var written = 0
|
var written = 0
|
||||||
if(header.hasRemaining)
|
if(header.hasRemaining)
|
||||||
|
|
Loading…
Reference in New Issue