/
AbstractRequest.java
320 lines (292 loc) · 13.6 KB
/
AbstractRequest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kafka.common.requests;
import org.apache.kafka.common.errors.UnsupportedVersionException;
import org.apache.kafka.common.network.Send;
import org.apache.kafka.common.protocol.ApiKeys;
import org.apache.kafka.common.protocol.Errors;
import org.apache.kafka.common.protocol.MessageUtil;
import org.apache.kafka.common.protocol.ObjectSerializationCache;
import org.apache.kafka.common.protocol.SendBuilder;
import java.nio.ByteBuffer;
import java.util.Map;
public abstract class AbstractRequest implements AbstractRequestResponse {
public static abstract class Builder<T extends AbstractRequest> {
private final ApiKeys apiKey;
private final short oldestAllowedVersion;
private final short latestAllowedVersion;
/**
* Construct a new builder which allows any supported version
*/
public Builder(ApiKeys apiKey, boolean enableUnstableLastVersion) {
this(apiKey, apiKey.oldestVersion(), apiKey.latestVersion(enableUnstableLastVersion));
}
/**
* Construct a new builder which allows any supported and released version
*/
public Builder(ApiKeys apiKey) {
this(apiKey, false);
}
/**
* Construct a new builder which allows only a specific version
*/
public Builder(ApiKeys apiKey, short allowedVersion) {
this(apiKey, allowedVersion, allowedVersion);
}
/**
* Construct a new builder which allows an inclusive range of versions
*/
public Builder(ApiKeys apiKey, short oldestAllowedVersion, short latestAllowedVersion) {
this.apiKey = apiKey;
this.oldestAllowedVersion = oldestAllowedVersion;
this.latestAllowedVersion = latestAllowedVersion;
}
public ApiKeys apiKey() {
return apiKey;
}
public short oldestAllowedVersion() {
return oldestAllowedVersion;
}
public short latestAllowedVersion() {
return latestAllowedVersion;
}
public T build() {
return build(latestAllowedVersion());
}
public abstract T build(short version);
}
private final short version;
private final ApiKeys apiKey;
public AbstractRequest(ApiKeys apiKey, short version) {
if (!apiKey.isVersionSupported(version))
throw new UnsupportedVersionException("The " + apiKey + " protocol does not support version " + version);
this.version = version;
this.apiKey = apiKey;
}
/**
* Get the version of this AbstractRequest object.
*/
public short version() {
return version;
}
public ApiKeys apiKey() {
return apiKey;
}
public final Send toSend(RequestHeader header) {
return SendBuilder.buildRequestSend(header, data());
}
/**
* Serializes header and body without prefixing with size (unlike `toSend`, which does include a size prefix).
*/
public final ByteBuffer serializeWithHeader(RequestHeader header) {
if (header.apiKey() != apiKey) {
throw new IllegalArgumentException("Could not build request " + apiKey + " with header api key " + header.apiKey());
}
if (header.apiVersion() != version) {
throw new IllegalArgumentException("Could not build request version " + version + " with header version " + header.apiVersion());
}
return RequestUtils.serialize(header.data(), header.headerVersion(), data(), version);
}
// Visible for testing
public final ByteBuffer serialize() {
return MessageUtil.toByteBuffer(data(), version);
}
// Visible for testing
final int sizeInBytes() {
return data().size(new ObjectSerializationCache(), version);
}
public String toString(boolean verbose) {
return data().toString();
}
@Override
public final String toString() {
return toString(true);
}
/**
* Get an error response for a request
*/
public AbstractResponse getErrorResponse(Throwable e) {
return getErrorResponse(AbstractResponse.DEFAULT_THROTTLE_TIME, e);
}
/**
* Get an error response for a request with specified throttle time in the response if applicable
*/
public abstract AbstractResponse getErrorResponse(int throttleTimeMs, Throwable e);
/**
* Get the error counts corresponding to an error response. This is overridden for requests
* where response may be null (e.g produce with acks=0).
*/
public Map<Errors, Integer> errorCounts(Throwable e) {
AbstractResponse response = getErrorResponse(0, e);
if (response == null)
throw new IllegalStateException("Error counts could not be obtained for request " + this);
else
return response.errorCounts();
}
/**
* Factory method for getting a request object based on ApiKey ID and a version
*/
public static RequestAndSize parseRequest(ApiKeys apiKey, short apiVersion, ByteBuffer buffer) {
int bufferSize = buffer.remaining();
return new RequestAndSize(doParseRequest(apiKey, apiVersion, buffer), bufferSize);
}
private static AbstractRequest doParseRequest(ApiKeys apiKey, short apiVersion, ByteBuffer buffer) {
switch (apiKey) {
case PRODUCE:
return ProduceRequest.parse(buffer, apiVersion);
case FETCH:
return FetchRequest.parse(buffer, apiVersion);
case LIST_OFFSETS:
return ListOffsetsRequest.parse(buffer, apiVersion);
case METADATA:
return MetadataRequest.parse(buffer, apiVersion);
case OFFSET_COMMIT:
return OffsetCommitRequest.parse(buffer, apiVersion);
case OFFSET_FETCH:
return OffsetFetchRequest.parse(buffer, apiVersion);
case FIND_COORDINATOR:
return FindCoordinatorRequest.parse(buffer, apiVersion);
case JOIN_GROUP:
return JoinGroupRequest.parse(buffer, apiVersion);
case HEARTBEAT:
return HeartbeatRequest.parse(buffer, apiVersion);
case LEAVE_GROUP:
return LeaveGroupRequest.parse(buffer, apiVersion);
case SYNC_GROUP:
return SyncGroupRequest.parse(buffer, apiVersion);
case STOP_REPLICA:
return StopReplicaRequest.parse(buffer, apiVersion);
case CONTROLLED_SHUTDOWN:
return ControlledShutdownRequest.parse(buffer, apiVersion);
case UPDATE_METADATA:
return UpdateMetadataRequest.parse(buffer, apiVersion);
case LEADER_AND_ISR:
return LeaderAndIsrRequest.parse(buffer, apiVersion);
case DESCRIBE_GROUPS:
return DescribeGroupsRequest.parse(buffer, apiVersion);
case LIST_GROUPS:
return ListGroupsRequest.parse(buffer, apiVersion);
case SASL_HANDSHAKE:
return SaslHandshakeRequest.parse(buffer, apiVersion);
case API_VERSIONS:
return ApiVersionsRequest.parse(buffer, apiVersion);
case CREATE_TOPICS:
return CreateTopicsRequest.parse(buffer, apiVersion);
case DELETE_TOPICS:
return DeleteTopicsRequest.parse(buffer, apiVersion);
case DELETE_RECORDS:
return DeleteRecordsRequest.parse(buffer, apiVersion);
case INIT_PRODUCER_ID:
return InitProducerIdRequest.parse(buffer, apiVersion);
case OFFSET_FOR_LEADER_EPOCH:
return OffsetsForLeaderEpochRequest.parse(buffer, apiVersion);
case ADD_PARTITIONS_TO_TXN:
return AddPartitionsToTxnRequest.parse(buffer, apiVersion);
case ADD_OFFSETS_TO_TXN:
return AddOffsetsToTxnRequest.parse(buffer, apiVersion);
case END_TXN:
return EndTxnRequest.parse(buffer, apiVersion);
case WRITE_TXN_MARKERS:
return WriteTxnMarkersRequest.parse(buffer, apiVersion);
case TXN_OFFSET_COMMIT:
return TxnOffsetCommitRequest.parse(buffer, apiVersion);
case DESCRIBE_ACLS:
return DescribeAclsRequest.parse(buffer, apiVersion);
case CREATE_ACLS:
return CreateAclsRequest.parse(buffer, apiVersion);
case DELETE_ACLS:
return DeleteAclsRequest.parse(buffer, apiVersion);
case DESCRIBE_CONFIGS:
return DescribeConfigsRequest.parse(buffer, apiVersion);
case ALTER_CONFIGS:
return AlterConfigsRequest.parse(buffer, apiVersion);
case ALTER_REPLICA_LOG_DIRS:
return AlterReplicaLogDirsRequest.parse(buffer, apiVersion);
case DESCRIBE_LOG_DIRS:
return DescribeLogDirsRequest.parse(buffer, apiVersion);
case SASL_AUTHENTICATE:
return SaslAuthenticateRequest.parse(buffer, apiVersion);
case CREATE_PARTITIONS:
return CreatePartitionsRequest.parse(buffer, apiVersion);
case CREATE_DELEGATION_TOKEN:
return CreateDelegationTokenRequest.parse(buffer, apiVersion);
case RENEW_DELEGATION_TOKEN:
return RenewDelegationTokenRequest.parse(buffer, apiVersion);
case EXPIRE_DELEGATION_TOKEN:
return ExpireDelegationTokenRequest.parse(buffer, apiVersion);
case DESCRIBE_DELEGATION_TOKEN:
return DescribeDelegationTokenRequest.parse(buffer, apiVersion);
case DELETE_GROUPS:
return DeleteGroupsRequest.parse(buffer, apiVersion);
case ELECT_LEADERS:
return ElectLeadersRequest.parse(buffer, apiVersion);
case INCREMENTAL_ALTER_CONFIGS:
return IncrementalAlterConfigsRequest.parse(buffer, apiVersion);
case ALTER_PARTITION_REASSIGNMENTS:
return AlterPartitionReassignmentsRequest.parse(buffer, apiVersion);
case LIST_PARTITION_REASSIGNMENTS:
return ListPartitionReassignmentsRequest.parse(buffer, apiVersion);
case OFFSET_DELETE:
return OffsetDeleteRequest.parse(buffer, apiVersion);
case DESCRIBE_CLIENT_QUOTAS:
return DescribeClientQuotasRequest.parse(buffer, apiVersion);
case ALTER_CLIENT_QUOTAS:
return AlterClientQuotasRequest.parse(buffer, apiVersion);
case DESCRIBE_USER_SCRAM_CREDENTIALS:
return DescribeUserScramCredentialsRequest.parse(buffer, apiVersion);
case ALTER_USER_SCRAM_CREDENTIALS:
return AlterUserScramCredentialsRequest.parse(buffer, apiVersion);
case VOTE:
return VoteRequest.parse(buffer, apiVersion);
case BEGIN_QUORUM_EPOCH:
return BeginQuorumEpochRequest.parse(buffer, apiVersion);
case END_QUORUM_EPOCH:
return EndQuorumEpochRequest.parse(buffer, apiVersion);
case DESCRIBE_QUORUM:
return DescribeQuorumRequest.parse(buffer, apiVersion);
case ALTER_PARTITION:
return AlterPartitionRequest.parse(buffer, apiVersion);
case UPDATE_FEATURES:
return UpdateFeaturesRequest.parse(buffer, apiVersion);
case ENVELOPE:
return EnvelopeRequest.parse(buffer, apiVersion);
case FETCH_SNAPSHOT:
return FetchSnapshotRequest.parse(buffer, apiVersion);
case DESCRIBE_CLUSTER:
return DescribeClusterRequest.parse(buffer, apiVersion);
case DESCRIBE_PRODUCERS:
return DescribeProducersRequest.parse(buffer, apiVersion);
case BROKER_REGISTRATION:
return BrokerRegistrationRequest.parse(buffer, apiVersion);
case BROKER_HEARTBEAT:
return BrokerHeartbeatRequest.parse(buffer, apiVersion);
case UNREGISTER_BROKER:
return UnregisterBrokerRequest.parse(buffer, apiVersion);
case DESCRIBE_TRANSACTIONS:
return DescribeTransactionsRequest.parse(buffer, apiVersion);
case LIST_TRANSACTIONS:
return ListTransactionsRequest.parse(buffer, apiVersion);
case ALLOCATE_PRODUCER_IDS:
return AllocateProducerIdsRequest.parse(buffer, apiVersion);
case CONSUMER_GROUP_HEARTBEAT:
return ConsumerGroupHeartbeatRequest.parse(buffer, apiVersion);
default:
throw new AssertionError(String.format("ApiKey %s is not currently handled in `parseRequest`, the " +
"code should be updated to do so.", apiKey));
}
}
}