protocol.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package kafka
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "strconv"
  6. )
  7. type ApiVersion struct {
  8. ApiKey int16
  9. MinVersion int16
  10. MaxVersion int16
  11. }
  12. func (v ApiVersion) Format(w fmt.State, r rune) {
  13. switch r {
  14. case 's':
  15. fmt.Fprint(w, apiKey(v.ApiKey))
  16. case 'd':
  17. switch {
  18. case w.Flag('-'):
  19. fmt.Fprint(w, v.MinVersion)
  20. case w.Flag('+'):
  21. fmt.Fprint(w, v.MaxVersion)
  22. default:
  23. fmt.Fprint(w, v.ApiKey)
  24. }
  25. case 'v':
  26. switch {
  27. case w.Flag('-'):
  28. fmt.Fprintf(w, "v%d", v.MinVersion)
  29. case w.Flag('+'):
  30. fmt.Fprintf(w, "v%d", v.MaxVersion)
  31. case w.Flag('#'):
  32. fmt.Fprintf(w, "kafka.ApiVersion{ApiKey:%d MinVersion:%d MaxVersion:%d}", v.ApiKey, v.MinVersion, v.MaxVersion)
  33. default:
  34. fmt.Fprintf(w, "%s[v%d:v%d]", apiKey(v.ApiKey), v.MinVersion, v.MaxVersion)
  35. }
  36. }
  37. }
  38. type apiKey int16
  39. const (
  40. produce apiKey = 0
  41. fetch apiKey = 1
  42. listOffsets apiKey = 2
  43. metadata apiKey = 3
  44. leaderAndIsr apiKey = 4
  45. stopReplica apiKey = 5
  46. updateMetadata apiKey = 6
  47. controlledShutdown apiKey = 7
  48. offsetCommit apiKey = 8
  49. offsetFetch apiKey = 9
  50. findCoordinator apiKey = 10
  51. joinGroup apiKey = 11
  52. heartbeat apiKey = 12
  53. leaveGroup apiKey = 13
  54. syncGroup apiKey = 14
  55. describeGroups apiKey = 15
  56. listGroups apiKey = 16
  57. saslHandshake apiKey = 17
  58. apiVersions apiKey = 18
  59. createTopics apiKey = 19
  60. deleteTopics apiKey = 20
  61. deleteRecords apiKey = 21
  62. initProducerId apiKey = 22
  63. offsetForLeaderEpoch apiKey = 23
  64. addPartitionsToTxn apiKey = 24
  65. addOffsetsToTxn apiKey = 25
  66. endTxn apiKey = 26
  67. writeTxnMarkers apiKey = 27
  68. txnOffsetCommit apiKey = 28
  69. describeAcls apiKey = 29
  70. createAcls apiKey = 30
  71. deleteAcls apiKey = 31
  72. describeConfigs apiKey = 32
  73. alterConfigs apiKey = 33
  74. alterReplicaLogDirs apiKey = 34
  75. describeLogDirs apiKey = 35
  76. saslAuthenticate apiKey = 36
  77. createPartitions apiKey = 37
  78. createDelegationToken apiKey = 38
  79. renewDelegationToken apiKey = 39
  80. expireDelegationToken apiKey = 40
  81. describeDelegationToken apiKey = 41
  82. deleteGroups apiKey = 42
  83. electLeaders apiKey = 43
  84. incrementalAlterConfigs apiKey = 44
  85. alterPartitionReassignments apiKey = 45
  86. listPartitionReassignments apiKey = 46
  87. offsetDelete apiKey = 47
  88. )
  89. func (k apiKey) String() string {
  90. if i := int(k); i >= 0 && i < len(apiKeyStrings) {
  91. return apiKeyStrings[i]
  92. }
  93. return strconv.Itoa(int(k))
  94. }
  95. type apiVersion int16
  96. const (
  97. v0 = 0
  98. v1 = 1
  99. v2 = 2
  100. v3 = 3
  101. v4 = 4
  102. v5 = 5
  103. v6 = 6
  104. v7 = 7
  105. v8 = 8
  106. v9 = 9
  107. v10 = 10
  108. )
  109. var apiKeyStrings = [...]string{
  110. produce: "Produce",
  111. fetch: "Fetch",
  112. listOffsets: "ListOffsets",
  113. metadata: "Metadata",
  114. leaderAndIsr: "LeaderAndIsr",
  115. stopReplica: "StopReplica",
  116. updateMetadata: "UpdateMetadata",
  117. controlledShutdown: "ControlledShutdown",
  118. offsetCommit: "OffsetCommit",
  119. offsetFetch: "OffsetFetch",
  120. findCoordinator: "FindCoordinator",
  121. joinGroup: "JoinGroup",
  122. heartbeat: "Heartbeat",
  123. leaveGroup: "LeaveGroup",
  124. syncGroup: "SyncGroup",
  125. describeGroups: "DescribeGroups",
  126. listGroups: "ListGroups",
  127. saslHandshake: "SaslHandshake",
  128. apiVersions: "ApiVersions",
  129. createTopics: "CreateTopics",
  130. deleteTopics: "DeleteTopics",
  131. deleteRecords: "DeleteRecords",
  132. initProducerId: "InitProducerId",
  133. offsetForLeaderEpoch: "OffsetForLeaderEpoch",
  134. addPartitionsToTxn: "AddPartitionsToTxn",
  135. addOffsetsToTxn: "AddOffsetsToTxn",
  136. endTxn: "EndTxn",
  137. writeTxnMarkers: "WriteTxnMarkers",
  138. txnOffsetCommit: "TxnOffsetCommit",
  139. describeAcls: "DescribeAcls",
  140. createAcls: "CreateAcls",
  141. deleteAcls: "DeleteAcls",
  142. describeConfigs: "DescribeConfigs",
  143. alterConfigs: "AlterConfigs",
  144. alterReplicaLogDirs: "AlterReplicaLogDirs",
  145. describeLogDirs: "DescribeLogDirs",
  146. saslAuthenticate: "SaslAuthenticate",
  147. createPartitions: "CreatePartitions",
  148. createDelegationToken: "CreateDelegationToken",
  149. renewDelegationToken: "RenewDelegationToken",
  150. expireDelegationToken: "ExpireDelegationToken",
  151. describeDelegationToken: "DescribeDelegationToken",
  152. deleteGroups: "DeleteGroups",
  153. electLeaders: "ElectLeaders",
  154. incrementalAlterConfigs: "IncrementalAlfterConfigs",
  155. alterPartitionReassignments: "AlterPartitionReassignments",
  156. listPartitionReassignments: "ListPartitionReassignments",
  157. offsetDelete: "OffsetDelete",
  158. }
  159. type requestHeader struct {
  160. Size int32
  161. ApiKey int16
  162. ApiVersion int16
  163. CorrelationID int32
  164. ClientID string
  165. }
  166. func (h requestHeader) size() int32 {
  167. return 4 + 2 + 2 + 4 + sizeofString(h.ClientID)
  168. }
  169. func (h requestHeader) writeTo(wb *writeBuffer) {
  170. wb.writeInt32(h.Size)
  171. wb.writeInt16(h.ApiKey)
  172. wb.writeInt16(h.ApiVersion)
  173. wb.writeInt32(h.CorrelationID)
  174. wb.writeString(h.ClientID)
  175. }
  176. type request interface {
  177. size() int32
  178. writable
  179. }
  180. func makeInt8(b []byte) int8 {
  181. return int8(b[0])
  182. }
  183. func makeInt16(b []byte) int16 {
  184. return int16(binary.BigEndian.Uint16(b))
  185. }
  186. func makeInt32(b []byte) int32 {
  187. return int32(binary.BigEndian.Uint32(b))
  188. }
  189. func makeInt64(b []byte) int64 {
  190. return int64(binary.BigEndian.Uint64(b))
  191. }
  192. func expectZeroSize(sz int, err error) error {
  193. if err == nil && sz != 0 {
  194. err = fmt.Errorf("reading a response left %d unread bytes", sz)
  195. }
  196. return err
  197. }