-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-9629 Use generated protocol for Fetch API #9008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
56e4156
04538af
6094e4e
41d03d1
5c98083
8ca460c
3808a96
efdadc5
538ed00
155621b
89de508
7878289
701619d
82a0d46
38b2ebf
efa5450
2514f5a
e198797
cf3bf33
78cd012
507eb04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * 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.protocol; | ||
|
|
||
| import org.apache.kafka.common.record.BaseRecords; | ||
| import org.apache.kafka.common.record.MemoryRecords; | ||
| import org.apache.kafka.common.utils.ByteUtils; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
|
|
||
| /** | ||
| * Implementation of Readable which reads from a byte buffer and can read records as {@link MemoryRecords} | ||
| * | ||
| * @see org.apache.kafka.common.requests.FetchResponse | ||
| */ | ||
| public class RecordsReadable implements Readable { | ||
| private final ByteBuffer buf; | ||
|
|
||
| public RecordsReadable(ByteBuffer buf) { | ||
| this.buf = buf; | ||
| } | ||
|
|
||
| @Override | ||
| public byte readByte() { | ||
| return buf.get(); | ||
| } | ||
|
|
||
| @Override | ||
| public short readShort() { | ||
| return buf.getShort(); | ||
| } | ||
|
|
||
| @Override | ||
| public int readInt() { | ||
| return buf.getInt(); | ||
| } | ||
|
|
||
| @Override | ||
| public long readLong() { | ||
| return buf.getLong(); | ||
| } | ||
|
|
||
| @Override | ||
| public double readDouble() { | ||
| return ByteUtils.readDouble(buf); | ||
| } | ||
|
|
||
| @Override | ||
| public void readArray(byte[] arr) { | ||
| buf.get(arr); | ||
| } | ||
|
|
||
| @Override | ||
| public int readUnsignedVarint() { | ||
| return ByteUtils.readUnsignedVarint(buf); | ||
| } | ||
|
|
||
| @Override | ||
| public ByteBuffer readByteBuffer(int length) { | ||
| ByteBuffer res = buf.slice(); | ||
| res.limit(length); | ||
|
|
||
| buf.position(buf.position() + length); | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| public BaseRecords readRecords(int length) { | ||
| if (length < 0) { | ||
| // no records | ||
| return null; | ||
| } else { | ||
| ByteBuffer recordsBuffer = readByteBuffer(length); | ||
| return MemoryRecords.readableRecords(recordsBuffer); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* | ||
| * 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.protocol; | ||
|
|
||
| import org.apache.kafka.common.network.ByteBufferSend; | ||
| import org.apache.kafka.common.network.Send; | ||
| import org.apache.kafka.common.record.BaseRecords; | ||
| import org.apache.kafka.common.utils.ByteUtils; | ||
|
|
||
| import java.io.DataOutput; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.function.Consumer; | ||
|
|
||
| /** | ||
| * Implementation of Writable which produces a sequence of {@link Send} objects. This allows for deferring the transfer | ||
| * of data from a record-set's file channel to the eventual socket channel. | ||
| * | ||
| * Excepting {@link #writeRecords(BaseRecords)}, calls to the write methods on this class will append to a byte array | ||
| * according to the format specified in {@link DataOutput}. When a call is made to writeRecords, any previously written | ||
| * bytes will be flushed as a new {@link ByteBufferSend} to the given Send consumer. After flushing the pending bytes, | ||
| * another Send is passed to the consumer which wraps the underlying record-set's transfer logic. | ||
| * | ||
| * For example, | ||
| * | ||
| * <pre> | ||
| * recordsWritable.writeInt(10); | ||
| * recordsWritable.writeRecords(records1); | ||
| * recordsWritable.writeInt(20); | ||
| * recordsWritable.writeRecords(records2); | ||
| * recordsWritable.writeInt(30); | ||
| * recordsWritable.flush(); | ||
| * </pre> | ||
| * | ||
| * Will pass 5 Send objects to the consumer given in the constructor. Care must be taken by callers to flush any | ||
| * pending bytes at the end of the writing sequence to ensure everything is flushed to the consumer. This class is | ||
| * intended to be used with {@link org.apache.kafka.common.record.MultiRecordsSend}. | ||
| * | ||
| * @see org.apache.kafka.common.requests.FetchResponse | ||
| */ | ||
| public class RecordsWritable implements Writable { | ||
| private final String dest; | ||
| private final Consumer<Send> sendConsumer; | ||
| private final ByteBuffer buffer; | ||
| private int mark; | ||
|
|
||
| public RecordsWritable(String dest, int messageSizeExcludingRecords, Consumer<Send> sendConsumer) { | ||
| this.dest = dest; | ||
| this.sendConsumer = sendConsumer; | ||
| this.buffer = ByteBuffer.allocate(messageSizeExcludingRecords); | ||
| this.mark = 0; | ||
| } | ||
|
|
||
| @Override | ||
| public void writeByte(byte val) { | ||
| buffer.put(val); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeShort(short val) { | ||
| buffer.putShort(val); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeInt(int val) { | ||
| buffer.putInt(val); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeLong(long val) { | ||
| buffer.putLong(val); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeDouble(double val) { | ||
| ByteUtils.writeDouble(val, buffer); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeByteArray(byte[] arr) { | ||
| buffer.put(arr); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeUnsignedVarint(int i) { | ||
| ByteUtils.writeUnsignedVarint(i, buffer); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeByteBuffer(ByteBuffer src) { | ||
| buffer.put(src); | ||
| } | ||
|
|
||
| public void writeRecords(BaseRecords records) { | ||
| flush(); | ||
| sendConsumer.accept(records.toSend(dest)); | ||
| } | ||
|
|
||
| /** | ||
| * Flush any pending bytes as a ByteBufferSend | ||
| */ | ||
| public void flush() { | ||
| int end = buffer.position(); | ||
| int len = end - mark; | ||
|
|
||
| if (len > 0) { | ||
| int limit = buffer.limit(); | ||
|
|
||
| // Set the desired absolute position and limit before slicing | ||
| buffer.position(mark); | ||
| buffer.limit(end); | ||
| ByteBuffer slice = buffer.slice(); | ||
|
|
||
| // Restore absolute position and limit on original buffer | ||
| buffer.limit(limit); | ||
| buffer.position(end); | ||
|
|
||
| // Update the mark to the end of slice we just took | ||
| mark = end; | ||
|
|
||
| ByteBufferSend send = new ByteBufferSend(dest, slice); | ||
| sendConsumer.accept(send); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| package org.apache.kafka.common.requests; | ||
|
|
||
| import org.apache.kafka.common.errors.UnsupportedVersionException; | ||
| import org.apache.kafka.common.message.FetchRequestData; | ||
| import org.apache.kafka.common.network.NetworkSend; | ||
| import org.apache.kafka.common.network.Send; | ||
| import org.apache.kafka.common.protocol.ApiKeys; | ||
|
|
@@ -146,7 +147,7 @@ public static AbstractRequest parseRequest(ApiKeys apiKey, short apiVersion, Str | |
| case PRODUCE: | ||
| return new ProduceRequest(struct, apiVersion); | ||
| case FETCH: | ||
| return new FetchRequest(struct, apiVersion); | ||
| return new FetchRequest(new FetchRequestData(struct, apiVersion), apiVersion); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: any reason not to stick with the same constructor convention as the other requests?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just wanted to remove the Struct constructor of FetchRequest completely. Eventually, |
||
| case LIST_OFFSETS: | ||
| return new ListOffsetRequest(struct, apiVersion); | ||
| case METADATA: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,4 +17,5 @@ | |
| package org.apache.kafka.common.requests; | ||
|
|
||
| public interface AbstractRequestResponse { | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: could probably change this to use
ifPresent