protocol.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. v5 = 5
  102. v6 = 6
  103. v7 = 7
  104. v10 = 10
  105. // Unused protocol versions: v4, v8, v9.
  106. )
  107. var apiKeyStrings = [...]string{
  108. produce: "Produce",
  109. fetch: "Fetch",
  110. listOffsets: "ListOffsets",
  111. metadata: "Metadata",
  112. leaderAndIsr: "LeaderAndIsr",
  113. stopReplica: "StopReplica",
  114. updateMetadata: "UpdateMetadata",
  115. controlledShutdown: "ControlledShutdown",
  116. offsetCommit: "OffsetCommit",
  117. offsetFetch: "OffsetFetch",
  118. findCoordinator: "FindCoordinator",
  119. joinGroup: "JoinGroup",
  120. heartbeat: "Heartbeat",
  121. leaveGroup: "LeaveGroup",
  122. syncGroup: "SyncGroup",
  123. describeGroups: "DescribeGroups",
  124. listGroups: "ListGroups",
  125. saslHandshake: "SaslHandshake",
  126. apiVersions: "ApiVersions",
  127. createTopics: "CreateTopics",
  128. deleteTopics: "DeleteTopics",
  129. deleteRecords: "DeleteRecords",
  130. initProducerId: "InitProducerId",
  131. offsetForLeaderEpoch: "OffsetForLeaderEpoch",
  132. addPartitionsToTxn: "AddPartitionsToTxn",
  133. addOffsetsToTxn: "AddOffsetsToTxn",
  134. endTxn: "EndTxn",
  135. writeTxnMarkers: "WriteTxnMarkers",
  136. txnOffsetCommit: "TxnOffsetCommit",
  137. describeAcls: "DescribeAcls",
  138. createAcls: "CreateAcls",
  139. deleteAcls: "DeleteAcls",
  140. describeConfigs: "DescribeConfigs",
  141. alterConfigs: "AlterConfigs",
  142. alterReplicaLogDirs: "AlterReplicaLogDirs",
  143. describeLogDirs: "DescribeLogDirs",
  144. saslAuthenticate: "SaslAuthenticate",
  145. createPartitions: "CreatePartitions",
  146. createDelegationToken: "CreateDelegationToken",
  147. renewDelegationToken: "RenewDelegationToken",
  148. expireDelegationToken: "ExpireDelegationToken",
  149. describeDelegationToken: "DescribeDelegationToken",
  150. deleteGroups: "DeleteGroups",
  151. electLeaders: "ElectLeaders",
  152. incrementalAlterConfigs: "IncrementalAlfterConfigs",
  153. alterPartitionReassignments: "AlterPartitionReassignments",
  154. listPartitionReassignments: "ListPartitionReassignments",
  155. offsetDelete: "OffsetDelete",
  156. }
  157. type requestHeader struct {
  158. Size int32
  159. ApiKey int16
  160. ApiVersion int16
  161. CorrelationID int32
  162. ClientID string
  163. }
  164. func (h requestHeader) size() int32 {
  165. return 4 + 2 + 2 + 4 + sizeofString(h.ClientID)
  166. }
  167. func (h requestHeader) writeTo(wb *writeBuffer) {
  168. wb.writeInt32(h.Size)
  169. wb.writeInt16(h.ApiKey)
  170. wb.writeInt16(h.ApiVersion)
  171. wb.writeInt32(h.CorrelationID)
  172. wb.writeString(h.ClientID)
  173. }
  174. type request interface {
  175. size() int32
  176. writable
  177. }
  178. func makeInt8(b []byte) int8 {
  179. return int8(b[0])
  180. }
  181. func makeInt16(b []byte) int16 {
  182. return int16(binary.BigEndian.Uint16(b))
  183. }
  184. func makeInt32(b []byte) int32 {
  185. return int32(binary.BigEndian.Uint32(b))
  186. }
  187. func makeInt64(b []byte) int64 {
  188. return int64(binary.BigEndian.Uint64(b))
  189. }
  190. func expectZeroSize(sz int, err error) error {
  191. if err == nil && sz != 0 {
  192. err = fmt.Errorf("reading a response left %d unread bytes", sz)
  193. }
  194. return err
  195. }