|
@@ -1,6 +1,7 @@
|
|
|
package sarama
|
|
|
|
|
|
import (
|
|
|
+ "sort"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -185,6 +186,17 @@ func (b *FetchResponseBlock) encode(pe packetEncoder, version int16) (err error)
|
|
|
return pe.pop()
|
|
|
}
|
|
|
|
|
|
+func (b *FetchResponseBlock) getAbortedTransactions() []*AbortedTransaction {
|
|
|
+
|
|
|
+
|
|
|
+ at := b.AbortedTransactions
|
|
|
+ sort.Slice(
|
|
|
+ at,
|
|
|
+ func(i, j int) bool { return at[i].FirstOffset < at[j].FirstOffset },
|
|
|
+ )
|
|
|
+ return at
|
|
|
+}
|
|
|
+
|
|
|
type FetchResponse struct {
|
|
|
Blocks map[string]map[int32]*FetchResponseBlock
|
|
|
ThrottleTime time.Duration
|
|
@@ -385,6 +397,65 @@ func (r *FetchResponse) AddRecordWithTimestamp(topic string, partition int32, ke
|
|
|
batch.addRecord(rec)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (r *FetchResponse) AddRecordBatchWithTimestamp(topic string, partition int32, key, value Encoder, offset int64, producerID int64, isTransactional bool, timestamp time.Time) {
|
|
|
+ frb := r.getOrCreateBlock(topic, partition)
|
|
|
+ kb, vb := encodeKV(key, value)
|
|
|
+
|
|
|
+ records := newDefaultRecords(&RecordBatch{Version: 2, LogAppendTime: r.LogAppendTime, FirstTimestamp: timestamp, MaxTimestamp: r.Timestamp})
|
|
|
+ batch := &RecordBatch{
|
|
|
+ Version: 2,
|
|
|
+ LogAppendTime: r.LogAppendTime,
|
|
|
+ FirstTimestamp: timestamp,
|
|
|
+ MaxTimestamp: r.Timestamp,
|
|
|
+ FirstOffset: offset,
|
|
|
+ LastOffsetDelta: 0,
|
|
|
+ ProducerID: producerID,
|
|
|
+ IsTransactional: isTransactional,
|
|
|
+ }
|
|
|
+ rec := &Record{Key: kb, Value: vb, OffsetDelta: 0, TimestampDelta: timestamp.Sub(batch.FirstTimestamp)}
|
|
|
+ batch.addRecord(rec)
|
|
|
+ records.RecordBatch = batch
|
|
|
+
|
|
|
+ frb.RecordsSet = append(frb.RecordsSet, &records)
|
|
|
+}
|
|
|
+
|
|
|
+func (r *FetchResponse) AddControlRecordWithTimestamp(topic string, partition int32, offset int64, producerID int64, recordType ControlRecordType, timestamp time.Time) {
|
|
|
+ frb := r.getOrCreateBlock(topic, partition)
|
|
|
+
|
|
|
+
|
|
|
+ batch := &RecordBatch{
|
|
|
+ Version: 2,
|
|
|
+ LogAppendTime: r.LogAppendTime,
|
|
|
+ FirstTimestamp: timestamp,
|
|
|
+ MaxTimestamp: r.Timestamp,
|
|
|
+ FirstOffset: offset,
|
|
|
+ LastOffsetDelta: 0,
|
|
|
+ ProducerID: producerID,
|
|
|
+ IsTransactional: true,
|
|
|
+ Control: true,
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ records := newDefaultRecords(nil)
|
|
|
+ records.RecordBatch = batch
|
|
|
+
|
|
|
+
|
|
|
+ crAbort := ControlRecord{
|
|
|
+ Version: 0,
|
|
|
+ Type: recordType,
|
|
|
+ }
|
|
|
+ crKey := &realEncoder{raw: make([]byte, 4)}
|
|
|
+ crValue := &realEncoder{raw: make([]byte, 6)}
|
|
|
+ crAbort.encode(crKey, crValue)
|
|
|
+ rec := &Record{Key: ByteEncoder(crKey.raw), Value: ByteEncoder(crValue.raw), OffsetDelta: 0, TimestampDelta: timestamp.Sub(batch.FirstTimestamp)}
|
|
|
+ batch.addRecord(rec)
|
|
|
+
|
|
|
+ frb.RecordsSet = append(frb.RecordsSet, &records)
|
|
|
+}
|
|
|
+
|
|
|
func (r *FetchResponse) AddMessage(topic string, partition int32, key, value Encoder, offset int64) {
|
|
|
r.AddMessageWithTimestamp(topic, partition, key, value, offset, time.Time{}, 0)
|
|
|
}
|
|
@@ -393,6 +464,15 @@ func (r *FetchResponse) AddRecord(topic string, partition int32, key, value Enco
|
|
|
r.AddRecordWithTimestamp(topic, partition, key, value, offset, time.Time{})
|
|
|
}
|
|
|
|
|
|
+func (r *FetchResponse) AddRecordBatch(topic string, partition int32, key, value Encoder, offset int64, producerID int64, isTransactional bool) {
|
|
|
+ r.AddRecordBatchWithTimestamp(topic, partition, key, value, offset, producerID, isTransactional, time.Time{})
|
|
|
+}
|
|
|
+
|
|
|
+func (r *FetchResponse) AddControlRecord(topic string, partition int32, offset int64, producerID int64, recordType ControlRecordType) {
|
|
|
+
|
|
|
+ r.AddControlRecordWithTimestamp(topic, partition, offset, producerID, recordType, time.Time{})
|
|
|
+}
|
|
|
+
|
|
|
func (r *FetchResponse) SetLastOffsetDelta(topic string, partition int32, offset int32) {
|
|
|
frb := r.getOrCreateBlock(topic, partition)
|
|
|
if len(frb.RecordsSet) == 0 {
|