forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add entry formatter to do conversions between MemoryRecords and ByteB…
…uf (apache#280) Currently, KoP has some significant performance overhead in the conversion between Kafka's records and Pulsar's messages. For pure Kafka users, i.e. no Pulsar client is involved, the conversion is unnecessary. So this PR provides a config `entry.format` to specify if the entry's format can be accepted by Pulsar clients. And a `EntryFormatter` is used to do the format conversion instead of static methods of `MessageRecordUtils`. In future, we'll implement a new `EntryFormatter` to avoid this conversion so that the performance can be improved a lot. After that, users can choose whether to support Pulsar clients for KoP.
- Loading branch information
1 parent
4d572fc
commit e675b17
Showing
13 changed files
with
374 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
kafka-impl/src/main/java/io/streamnative/pulsar/handlers/kop/format/EntryFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Licensed 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 io.streamnative.pulsar.handlers.kop.format; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import java.util.List; | ||
|
||
import org.apache.bookkeeper.mledger.Entry; | ||
import org.apache.kafka.common.record.MemoryRecords; | ||
import org.apache.kafka.common.record.MutableRecordBatch; | ||
|
||
|
||
/** | ||
* The formatter for conversion between Kafka records and Bookie entries. | ||
*/ | ||
public interface EntryFormatter { | ||
|
||
/** | ||
* Encode Kafka records to a ByteBuf. | ||
* | ||
* @param records messages with Kafka's format | ||
* @param numMessages the number of messages | ||
* @return the ByteBuf of an entry that is to be written to Bookie | ||
*/ | ||
ByteBuf encode(final MemoryRecords records, final int numMessages); | ||
|
||
/** | ||
* Decode a stream of entries to Kafka records. | ||
* | ||
* @param entries the list of entries | ||
* @param magic the Kafka record batch's magic value | ||
* @return the Kafka records | ||
*/ | ||
MemoryRecords decode(final List<Entry> entries, final byte magic); | ||
|
||
/** | ||
* Get the number of messages from MemoryRecords. | ||
* Since MemoryRecords doesn't provide a way to get the number of messages. We need to iterate over the whole | ||
* MemoryRecords object. So we use a helper method to get the number of messages that can be passed to | ||
* {@link EntryFormatter#encode(MemoryRecords, int)} and metrics related methods as well. | ||
* | ||
* @param records messages with Kafka's format | ||
* @return the number of messages | ||
*/ | ||
static int parseNumMessages(final MemoryRecords records) { | ||
int numMessages = 0; | ||
for (MutableRecordBatch batch : records.batches()) { | ||
numMessages += (batch.lastOffset() - batch.baseOffset() + 1); | ||
} | ||
return numMessages; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...-impl/src/main/java/io/streamnative/pulsar/handlers/kop/format/EntryFormatterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Licensed 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 io.streamnative.pulsar.handlers.kop.format; | ||
|
||
/** | ||
* Factory of EntryFormatter. | ||
* | ||
* @see EntryFormatter | ||
*/ | ||
public class EntryFormatterFactory { | ||
|
||
enum EntryFormat { | ||
PULSAR | ||
} | ||
|
||
public static EntryFormatter create(final String format) { | ||
try { | ||
EntryFormat entryFormat = Enum.valueOf(EntryFormat.class, format.toUpperCase()); | ||
switch (entryFormat) { | ||
case PULSAR: | ||
return new PulsarEntryFormatter(); | ||
default: | ||
throw new Exception("No EntryFormatter for " + entryFormat); | ||
} | ||
} catch (Exception e) { | ||
throw new IllegalArgumentException("Unsupported entry.format '" + format + "': " + e.getMessage()); | ||
} | ||
} | ||
} |
Oops, something went wrong.