types.go 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Package types provides access to the types and constants that the Kafka protocol uses,
  3. since they are needed by all levels of the saramago stack.
  4. */
  5. package types
  6. // KError is the type of error that can be returned directly by the Kafka broker.
  7. // See https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol#AGuideToTheKafkaProtocol-ErrorCodes
  8. type KError int16
  9. const (
  10. NO_ERROR KError = 0
  11. UNKNOWN KError = -1
  12. OFFSET_OUT_OF_RANGE KError = 1
  13. INVALID_MESSAGE KError = 2
  14. UNKNOWN_TOPIC_OR_PARTITION KError = 3
  15. INVALID_MESSAGE_SIZE KError = 4
  16. LEADER_NOT_AVAILABLE KError = 5
  17. NOT_LEADER_FOR_PARTITION KError = 6
  18. REQUEST_TIMED_OUT KError = 7
  19. BROKER_NOT_AVAILABLE KError = 8
  20. REPLICA_NOT_AVAILABLE KError = 9
  21. MESSAGE_SIZE_TOO_LARGE KError = 10
  22. STALE_CONTROLLER_EPOCH_CODE KError = 11
  23. OFFSET_METADATA_TOO_LARGE KError = 12
  24. )
  25. func (err KError) Error() string {
  26. // Error messages stolen/adapted from
  27. // https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol
  28. switch err {
  29. case NO_ERROR:
  30. return "kafka server: Not an error, why are you printing me?"
  31. case UNKNOWN:
  32. return "kafka server: Unexpected (unknown?) server error."
  33. case OFFSET_OUT_OF_RANGE:
  34. return "kafka server: The requested offset is outside the range of offsets maintained by the server for the given topic/partition."
  35. case INVALID_MESSAGE:
  36. return "kafka server: Message contents does not match its CRC."
  37. case UNKNOWN_TOPIC_OR_PARTITION:
  38. return "kafka server: Request was for a topic or partition that does not exist on this broker."
  39. case INVALID_MESSAGE_SIZE:
  40. return "kafka server: The message has a negative size."
  41. case LEADER_NOT_AVAILABLE:
  42. return "kafka server: In the middle of a leadership election, there is currently no leader for this partition and hence it is unavailable for writes."
  43. case NOT_LEADER_FOR_PARTITION:
  44. return "kafka server: Tried to send a message to a replica that is not the leader for some partition. Your metadata is out of date."
  45. case REQUEST_TIMED_OUT:
  46. return "kafka server: Request exceeded the user-specified time limit in the request."
  47. case BROKER_NOT_AVAILABLE:
  48. return "kafka server: Broker not available. Not a client facing error, we should never receive this!!!"
  49. case REPLICA_NOT_AVAILABLE:
  50. return "kafka server: Replica not available. What is the difference between this and LeaderNotAvailable?"
  51. case MESSAGE_SIZE_TOO_LARGE:
  52. return "kafka server: Message was too large, server rejected it to avoid allocation error."
  53. case STALE_CONTROLLER_EPOCH_CODE:
  54. return "kafka server: Stale controller epoch code. ???"
  55. case OFFSET_METADATA_TOO_LARGE:
  56. return "kafka server: Specified a string larger than the configured maximum for offset metadata."
  57. default:
  58. return "Unknown error, how did this happen?"
  59. }
  60. }
  61. // CompressionCodec represents the various compression codecs recognized by Kafka in messages.
  62. type CompressionCodec int8
  63. const (
  64. COMPRESSION_NONE CompressionCodec = 0
  65. COMPRESSION_GZIP CompressionCodec = 1
  66. COMPRESSION_SNAPPY CompressionCodec = 2
  67. )
  68. // RequiredAcks is used in Produce Requests to tell the broker how many replica acknowledgements
  69. // it must see before responding. Any positive int16 value is valid, or the constants defined here.
  70. type RequiredAcks int16
  71. const (
  72. NO_RESPONSE RequiredAcks = 0 // Don't send any response, the TCP ACK is all you get.
  73. WAIT_FOR_LOCAL RequiredAcks = 1 // Wait for only the local commit to succeed before responding.
  74. WAIT_FOR_ALL RequiredAcks = -1 // Wait for all replicas to commit before responding.
  75. )
  76. // OffsetTime is used in Offset Requests to ask for all messages before a certain time. Any positive int64
  77. // value will be interpreted as milliseconds, or use the special constants defined here.
  78. type OffsetTime int64
  79. const (
  80. // Ask for the latest offsets.
  81. LATEST_OFFSETS OffsetTime = -1
  82. // Ask for the earliest available offset. Note that because offsets are pulled in descending order,
  83. // asking for the earliest offset will always return you a single element.
  84. EARLIEST_OFFSET OffsetTime = -2
  85. )