Kafka 源代码分析之MessageSet

这里分析MessageSet类

MessageSet是一个抽象类,定义了一条log的一些接口和常量,FileMessageSet就是MessageSet类的实现类.一条日志中存储的log完整格式如下

offset+message_size+message

object MessageSet {  //消息的常量

  val MessageSizeLength = 4  //消息大小的长度
val OffsetLength = 8 //offset的长度
val LogOverhead = MessageSizeLength + OffsetLength //消息头部的构成
val Empty = new ByteBufferMessageSet(ByteBuffer.allocate(0)) //空消息. /**
* The size of a message set containing the given messages
*/
def messageSetSize(messages: Iterable[Message]): Int =
messages.foldLeft(0)(_ + entrySize(_)) //在最左侧添加消息长度. /**
* The size of a list of messages
*/
def messageSetSize(messages: java.util.List[Message]): Int = {
var size = 0
val iter = messages.iterator
while(iter.hasNext) {
val message = iter.next.asInstanceOf[Message] //设置消息尺寸
size += entrySize(message)
}
size
} /**
* The size of a size-delimited entry in a message set
*/
def entrySize(message: Message): Int = LogOverhead + message.size //计算总长度,是头部长度加消息本身长度. } /**
* A set of messages with offsets. A message set has a fixed serialized form, though the container
* for the bytes could be either in-memory or on disk. The format of each message is
* as follows:
* 8 byte message offset number
* 4 byte size containing an integer N
* N message bytes as described in the Message class
*/
abstract class MessageSet extends Iterable[MessageAndOffset] { //这个抽象类定义了几个消息接口. /** 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
* of bytes written is returned */
def writeTo(channel: GatheringByteChannel, offset: Long, maxSize: Int): Int //写入消息 /**
* Provides an iterator over the message/offset pairs in this set
*/
def iterator: Iterator[MessageAndOffset] //遍历转换消息 /**
* Gives the total size of this message set in bytes
*/
def sizeInBytes: Int //字节大小. /**
* Print this message set's contents. If the message set has more than 100 messages, just
* print the first 100.
*/
override def toString: String = {
val builder = new StringBuilder()
builder.append(getClass.getSimpleName + "(")
val iter = this.iterator
var i = 0
while(iter.hasNext && i < 100) {
val message = iter.next
builder.append(message)
if(iter.hasNext)
builder.append(", ")
i += 1
}
if(iter.hasNext)
builder.append("...")
builder.append(")")
builder.toString
} }

  MessageSet定义log的一般接口的格式.

上一篇:Java并发编程原理与实战三十九:JDK8新增锁StampedLock详解


下一篇:Oracle RAC 环境下的 v$log v$logfile