1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
package protocol
import (
"errors"
"io"
"time"
)
// RecordReader is an interface representing a sequence of records. Record sets
// are used in both produce and fetch requests to represent the sequence of
// records that are sent to or receive from kafka brokers.
//
// RecordSet values are not safe to use concurrently from multiple goroutines.
type RecordReader interface {
// Returns the next record in the set, or io.EOF if the end of the sequence
// has been reached.
//
// The returned Record is guaranteed to be valid until the next call to
// ReadRecord. If the program needs to retain the Record value it must make
// a copy.
ReadRecord() (*Record, error)
}
// NewRecordReader constructs a reader exposing the records passed as arguments.
func NewRecordReader(records ...Record) RecordReader {
switch len(records) {
case 0:
return emptyRecordReader{}
default:
r := &recordReader{records: make([]Record, len(records))}
copy(r.records, records)
return r
}
}
// MultiRecordReader merges multiple record batches into one.
func MultiRecordReader(batches ...RecordReader) RecordReader {
switch len(batches) {
case 0:
return emptyRecordReader{}
case 1:
return batches[0]
default:
m := &multiRecordReader{batches: make([]RecordReader, len(batches))}
copy(m.batches, batches)
return m
}
}
func forEachRecord(r RecordReader, f func(int, *Record) error) error {
for i := 0; ; i++ {
rec, err := r.ReadRecord()
if err != nil {
if errors.Is(err, io.EOF) {
err = nil
}
return err
}
if err := handleRecord(i, rec, f); err != nil {
return err
}
}
}
func handleRecord(i int, r *Record, f func(int, *Record) error) error {
if r.Key != nil {
defer r.Key.Close()
}
if r.Value != nil {
defer r.Value.Close()
}
return f(i, r)
}
type recordReader struct {
records []Record
index int
}
func (r *recordReader) ReadRecord() (*Record, error) {
if i := r.index; i >= 0 && i < len(r.records) {
r.index++
return &r.records[i], nil
}
return nil, io.EOF
}
type multiRecordReader struct {
batches []RecordReader
index int
}
func (m *multiRecordReader) ReadRecord() (*Record, error) {
for {
if m.index == len(m.batches) {
return nil, io.EOF
}
r, err := m.batches[m.index].ReadRecord()
if err == nil {
return r, nil
}
if !errors.Is(err, io.EOF) {
return nil, err
}
m.index++
}
}
// optimizedRecordReader is an implementation of a RecordReader which exposes a
// sequence.
type optimizedRecordReader struct {
records []optimizedRecord
index int
buffer Record
headers [][]Header
}
func (r *optimizedRecordReader) ReadRecord() (*Record, error) {
if i := r.index; i >= 0 && i < len(r.records) {
rec := &r.records[i]
r.index++
r.buffer = Record{
Offset: rec.offset,
Time: rec.time(),
Key: rec.key(),
Value: rec.value(),
}
if i < len(r.headers) {
r.buffer.Headers = r.headers[i]
}
return &r.buffer, nil
}
return nil, io.EOF
}
type optimizedRecord struct {
offset int64
timestamp int64
keyRef *pageRef
valueRef *pageRef
}
func (r *optimizedRecord) time() time.Time {
return makeTime(r.timestamp)
}
func (r *optimizedRecord) key() Bytes {
return makeBytes(r.keyRef)
}
func (r *optimizedRecord) value() Bytes {
return makeBytes(r.valueRef)
}
func makeBytes(ref *pageRef) Bytes {
if ref == nil {
return nil
}
return ref
}
type emptyRecordReader struct{}
func (emptyRecordReader) ReadRecord() (*Record, error) { return nil, io.EOF }
// ControlRecord represents a record read from a control batch.
type ControlRecord struct {
Offset int64
Time time.Time
Version int16
Type int16
Data []byte
Headers []Header
}
func ReadControlRecord(r *Record) (*ControlRecord, error) {
if r.Key != nil {
defer r.Key.Close()
}
if r.Value != nil {
defer r.Value.Close()
}
k, err := ReadAll(r.Key)
if err != nil {
return nil, err
}
if k == nil {
return nil, Error("invalid control record with nil key")
}
if len(k) != 4 {
return nil, Errorf("invalid control record with key of size %d", len(k))
}
v, err := ReadAll(r.Value)
if err != nil {
return nil, err
}
c := &ControlRecord{
Offset: r.Offset,
Time: r.Time,
Version: readInt16(k[:2]),
Type: readInt16(k[2:]),
Data: v,
Headers: r.Headers,
}
return c, nil
}
func (cr *ControlRecord) Key() Bytes {
k := make([]byte, 4)
writeInt16(k[:2], cr.Version)
writeInt16(k[2:], cr.Type)
return NewBytes(k)
}
func (cr *ControlRecord) Value() Bytes {
return NewBytes(cr.Data)
}
func (cr *ControlRecord) Record() Record {
return Record{
Offset: cr.Offset,
Time: cr.Time,
Key: cr.Key(),
Value: cr.Value(),
Headers: cr.Headers,
}
}
// ControlBatch is an implementation of the RecordReader interface representing
// control batches returned by kafka brokers.
type ControlBatch struct {
Attributes Attributes
PartitionLeaderEpoch int32
BaseOffset int64
ProducerID int64
ProducerEpoch int16
BaseSequence int32
Records RecordReader
}
// NewControlBatch constructs a control batch from the list of records passed as
// arguments.
func NewControlBatch(records ...ControlRecord) *ControlBatch {
rawRecords := make([]Record, len(records))
for i, cr := range records {
rawRecords[i] = cr.Record()
}
return &ControlBatch{
Records: NewRecordReader(rawRecords...),
}
}
func (c *ControlBatch) ReadRecord() (*Record, error) {
return c.Records.ReadRecord()
}
func (c *ControlBatch) ReadControlRecord() (*ControlRecord, error) {
r, err := c.ReadRecord()
if err != nil {
return nil, err
}
if r.Key != nil {
defer r.Key.Close()
}
if r.Value != nil {
defer r.Value.Close()
}
return ReadControlRecord(r)
}
func (c *ControlBatch) Offset() int64 {
return c.BaseOffset
}
func (c *ControlBatch) Version() int {
return 2
}
// RecordBatch is an implementation of the RecordReader interface representing
// regular record batches (v2).
type RecordBatch struct {
Attributes Attributes
PartitionLeaderEpoch int32
BaseOffset int64
ProducerID int64
ProducerEpoch int16
BaseSequence int32
Records RecordReader
}
func (r *RecordBatch) ReadRecord() (*Record, error) {
return r.Records.ReadRecord()
}
func (r *RecordBatch) Offset() int64 {
return r.BaseOffset
}
func (r *RecordBatch) Version() int {
return 2
}
// MessageSet is an implementation of the RecordReader interface representing
// regular message sets (v1).
type MessageSet struct {
Attributes Attributes
BaseOffset int64
Records RecordReader
}
func (m *MessageSet) ReadRecord() (*Record, error) {
return m.Records.ReadRecord()
}
func (m *MessageSet) Offset() int64 {
return m.BaseOffset
}
func (m *MessageSet) Version() int {
return 1
}
// RecordStream is an implementation of the RecordReader interface which
// combines multiple underlying RecordReader and only expose records that
// are not from control batches.
type RecordStream struct {
Records []RecordReader
index int
}
func (s *RecordStream) ReadRecord() (*Record, error) {
for {
if s.index < 0 || s.index >= len(s.Records) {
return nil, io.EOF
}
if _, isControl := s.Records[s.index].(*ControlBatch); isControl {
s.index++
continue
}
r, err := s.Records[s.index].ReadRecord()
if err != nil {
if errors.Is(err, io.EOF) {
s.index++
continue
}
}
return r, err
}
}
|