offset_request.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package sarama
  2. type offsetRequestBlock struct {
  3. time int64
  4. maxOffsets int32 // Only used in version 0
  5. }
  6. func (b *offsetRequestBlock) encode(pe packetEncoder, version int16) error {
  7. pe.putInt64(b.time)
  8. if version == 0 {
  9. pe.putInt32(b.maxOffsets)
  10. }
  11. return nil
  12. }
  13. func (b *offsetRequestBlock) decode(pd packetDecoder, version int16) (err error) {
  14. if b.time, err = pd.getInt64(); err != nil {
  15. return err
  16. }
  17. if version == 0 {
  18. if b.maxOffsets, err = pd.getInt32(); err != nil {
  19. return err
  20. }
  21. }
  22. return nil
  23. }
  24. type OffsetRequest struct {
  25. Version int16
  26. IsolationLevel IsolationLevel
  27. replicaID int32
  28. isReplicaIDSet bool
  29. blocks map[string]map[int32]*offsetRequestBlock
  30. }
  31. func (r *OffsetRequest) encode(pe packetEncoder) error {
  32. if r.isReplicaIDSet {
  33. pe.putInt32(r.replicaID)
  34. } else {
  35. // default replica ID is always -1 for clients
  36. pe.putInt32(-1)
  37. }
  38. if r.Version >= 2 {
  39. pe.putBool(r.IsolationLevel == ReadCommitted)
  40. }
  41. err := pe.putArrayLength(len(r.blocks))
  42. if err != nil {
  43. return err
  44. }
  45. for topic, partitions := range r.blocks {
  46. err = pe.putString(topic)
  47. if err != nil {
  48. return err
  49. }
  50. err = pe.putArrayLength(len(partitions))
  51. if err != nil {
  52. return err
  53. }
  54. for partition, block := range partitions {
  55. pe.putInt32(partition)
  56. if err = block.encode(pe, r.Version); err != nil {
  57. return err
  58. }
  59. }
  60. }
  61. return nil
  62. }
  63. func (r *OffsetRequest) decode(pd packetDecoder, version int16) error {
  64. r.Version = version
  65. replicaID, err := pd.getInt32()
  66. if err != nil {
  67. return err
  68. }
  69. if replicaID >= 0 {
  70. r.SetReplicaID(replicaID)
  71. }
  72. if r.Version >= 2 {
  73. tmp, err := pd.getBool()
  74. if err != nil {
  75. return err
  76. }
  77. r.IsolationLevel = ReadUncommitted
  78. if tmp {
  79. r.IsolationLevel = ReadCommitted
  80. }
  81. }
  82. blockCount, err := pd.getArrayLength()
  83. if err != nil {
  84. return err
  85. }
  86. if blockCount == 0 {
  87. return nil
  88. }
  89. r.blocks = make(map[string]map[int32]*offsetRequestBlock)
  90. for i := 0; i < blockCount; i++ {
  91. topic, err := pd.getString()
  92. if err != nil {
  93. return err
  94. }
  95. partitionCount, err := pd.getArrayLength()
  96. if err != nil {
  97. return err
  98. }
  99. r.blocks[topic] = make(map[int32]*offsetRequestBlock)
  100. for j := 0; j < partitionCount; j++ {
  101. partition, err := pd.getInt32()
  102. if err != nil {
  103. return err
  104. }
  105. block := &offsetRequestBlock{}
  106. if err := block.decode(pd, version); err != nil {
  107. return err
  108. }
  109. r.blocks[topic][partition] = block
  110. }
  111. }
  112. return nil
  113. }
  114. func (r *OffsetRequest) key() int16 {
  115. return 2
  116. }
  117. func (r *OffsetRequest) version() int16 {
  118. return r.Version
  119. }
  120. func (r *OffsetRequest) headerVersion() int16 {
  121. return 1
  122. }
  123. func (r *OffsetRequest) requiredVersion() KafkaVersion {
  124. switch r.Version {
  125. case 1:
  126. return V0_10_1_0
  127. case 2:
  128. return V0_11_0_0
  129. default:
  130. return MinVersion
  131. }
  132. }
  133. func (r *OffsetRequest) SetReplicaID(id int32) {
  134. r.replicaID = id
  135. r.isReplicaIDSet = true
  136. }
  137. func (r *OffsetRequest) ReplicaID() int32 {
  138. if r.isReplicaIDSet {
  139. return r.replicaID
  140. }
  141. return -1
  142. }
  143. func (r *OffsetRequest) AddBlock(topic string, partitionID int32, time int64, maxOffsets int32) {
  144. if r.blocks == nil {
  145. r.blocks = make(map[string]map[int32]*offsetRequestBlock)
  146. }
  147. if r.blocks[topic] == nil {
  148. r.blocks[topic] = make(map[int32]*offsetRequestBlock)
  149. }
  150. tmp := new(offsetRequestBlock)
  151. tmp.time = time
  152. if r.Version == 0 {
  153. tmp.maxOffsets = maxOffsets
  154. }
  155. r.blocks[topic][partitionID] = tmp
  156. }