| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 1 | package sarama |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | ) |
| 6 | |
| 7 | // ApiVersionsResponseKey contains the APIs supported by the broker. |
| 8 | type ApiVersionsResponseKey struct { |
| 9 | // Version defines the protocol version to use for encode and decode |
| 10 | Version int16 |
| 11 | // ApiKey contains the API index. |
| 12 | ApiKey int16 |
| 13 | // MinVersion contains the minimum supported version, inclusive. |
| 14 | MinVersion int16 |
| 15 | // MaxVersion contains the maximum supported version, inclusive. |
| 16 | MaxVersion int16 |
| 17 | } |
| 18 | |
| 19 | func (a *ApiVersionsResponseKey) encode(pe packetEncoder, version int16) (err error) { |
| 20 | a.Version = version |
| 21 | pe.putInt16(a.ApiKey) |
| 22 | |
| 23 | pe.putInt16(a.MinVersion) |
| 24 | |
| 25 | pe.putInt16(a.MaxVersion) |
| 26 | |
| 27 | if version >= 3 { |
| 28 | pe.putEmptyTaggedFieldArray() |
| 29 | } |
| 30 | |
| 31 | return nil |
| 32 | } |
| 33 | |
| 34 | func (a *ApiVersionsResponseKey) decode(pd packetDecoder, version int16) (err error) { |
| 35 | a.Version = version |
| 36 | if a.ApiKey, err = pd.getInt16(); err != nil { |
| 37 | return err |
| 38 | } |
| 39 | |
| 40 | if a.MinVersion, err = pd.getInt16(); err != nil { |
| 41 | return err |
| 42 | } |
| 43 | |
| 44 | if a.MaxVersion, err = pd.getInt16(); err != nil { |
| 45 | return err |
| 46 | } |
| 47 | |
| 48 | _, err = pd.getEmptyTaggedFieldArray() |
| 49 | return err |
| 50 | } |
| 51 | |
| 52 | type ApiVersionsResponse struct { |
| 53 | // Version defines the protocol version to use for encode and decode |
| 54 | Version int16 |
| 55 | // ErrorCode contains the top-level error code. |
| 56 | ErrorCode int16 |
| 57 | // ApiKeys contains the APIs supported by the broker. |
| 58 | ApiKeys []ApiVersionsResponseKey |
| 59 | // ThrottleTimeMs contains the duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota. |
| 60 | ThrottleTimeMs int32 |
| 61 | } |
| 62 | |
| 63 | func (r *ApiVersionsResponse) setVersion(v int16) { |
| 64 | r.Version = v |
| 65 | } |
| 66 | |
| 67 | func (r *ApiVersionsResponse) encode(pe packetEncoder) (err error) { |
| 68 | pe.putInt16(r.ErrorCode) |
| 69 | |
| 70 | if err := pe.putArrayLength(len(r.ApiKeys)); err != nil { |
| 71 | return err |
| 72 | } |
| 73 | for _, block := range r.ApiKeys { |
| 74 | if err := block.encode(pe, r.Version); err != nil { |
| 75 | return err |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if r.Version >= 1 { |
| 80 | pe.putInt32(r.ThrottleTimeMs) |
| 81 | } |
| 82 | |
| 83 | if r.Version >= 3 { |
| 84 | pe.putEmptyTaggedFieldArray() |
| 85 | } |
| 86 | |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | func (r *ApiVersionsResponse) decode(pd packetDecoder, version int16) (err error) { |
| 91 | r.Version = version |
| 92 | if r.ErrorCode, err = pd.getInt16(); err != nil { |
| 93 | return err |
| 94 | } |
| 95 | |
| 96 | // KIP-511: if broker didn't understand the ApiVersionsRequest version then |
| 97 | // it replies with a V0 non-flexible ApiVersionResponse where its supported |
| 98 | // ApiVersionsRequest version is available in ApiKeys |
| 99 | if r.ErrorCode == int16(ErrUnsupportedVersion) { |
| 100 | // drop version to 0 and to revert packageDecoder to non-flexible for remaining decoding |
| 101 | r.Version = 0 |
| 102 | pd = downgradeFlexibleDecoder(pd) |
| 103 | } |
| 104 | |
| 105 | numApiKeys, err := pd.getArrayLength() |
| 106 | if err != nil { |
| 107 | return err |
| 108 | } |
| 109 | r.ApiKeys = make([]ApiVersionsResponseKey, numApiKeys) |
| 110 | for i := 0; i < numApiKeys; i++ { |
| 111 | var block ApiVersionsResponseKey |
| 112 | if err = block.decode(pd, r.Version); err != nil { |
| 113 | return err |
| 114 | } |
| 115 | r.ApiKeys[i] = block |
| 116 | } |
| 117 | |
| 118 | if r.Version >= 1 { |
| 119 | if r.ThrottleTimeMs, err = pd.getInt32(); err != nil { |
| 120 | return err |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | _, err = pd.getEmptyTaggedFieldArray() |
| 125 | return err |
| 126 | } |
| 127 | |
| 128 | func (r *ApiVersionsResponse) key() int16 { |
| 129 | return apiKeyApiVersions |
| 130 | } |
| 131 | |
| 132 | func (r *ApiVersionsResponse) version() int16 { |
| 133 | return r.Version |
| 134 | } |
| 135 | |
| 136 | func (r *ApiVersionsResponse) headerVersion() int16 { |
| 137 | // ApiVersionsResponse always includes a v0 header. |
| 138 | // See KIP-511 for details |
| 139 | return 0 |
| 140 | } |
| 141 | |
| 142 | func (r *ApiVersionsResponse) isValidVersion() bool { |
| 143 | return r.Version >= 0 && r.Version <= 3 |
| 144 | } |
| 145 | |
| 146 | func (r *ApiVersionsResponse) isFlexible() bool { |
| 147 | return r.isFlexibleVersion(r.Version) |
| 148 | } |
| 149 | |
| 150 | func (r *ApiVersionsResponse) isFlexibleVersion(version int16) bool { |
| 151 | return version >= 3 |
| 152 | } |
| 153 | |
| 154 | func (r *ApiVersionsResponse) requiredVersion() KafkaVersion { |
| 155 | switch r.Version { |
| 156 | case 3: |
| 157 | return V2_4_0_0 |
| 158 | case 2: |
| 159 | return V2_0_0_0 |
| 160 | case 1: |
| 161 | return V0_11_0_0 |
| 162 | case 0: |
| 163 | return V0_10_0_0 |
| 164 | default: |
| 165 | return V2_4_0_0 |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | func (r *ApiVersionsResponse) throttleTime() time.Duration { |
| 170 | return time.Duration(r.ThrottleTimeMs) * time.Millisecond |
| 171 | } |