-
Notifications
You must be signed in to change notification settings - Fork 31
365.add support of custom codecs in topics #447
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
Merged
alex268
merged 21 commits into
ydb-platform:release_v2.4.0
from
ekuvardin:365.Add-support-of-custom-codecs-in-topics
May 12, 2025
Merged
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a13c41f
Add first implementation of custom codec
ekuvardin 626931d
Add codec implementation
ekuvardin 5cfb6f3
Change contract to use custom codec
ekuvardin 556f992
Add test
ekuvardin 20dc3dc
Add checkstyle and test
ekuvardin a3be1b6
Change contract to CodecRegistry
ekuvardin eda40a1
Add more test and description
ekuvardin fad26f2
Add more test and comments
ekuvardin 40bbcc1
Add more test and comments
ekuvardin caa2966
Merge remote-tracking branch 'origin/365.Add-support-of-custom-codecs…
ekuvardin 9b47dc2
Merge remote-tracking branch 'origin/master' into 365.Add-support-of-…
ekuvardin d6ad0fa
One more test connected old constructor
ekuvardin 105ea0a
Check javadoc exception
ekuvardin 16d2b40
Changed consumer names
ekuvardin ac82f40
Divide tests
ekuvardin ce0160f
Some additional order guarantee
ekuvardin 5ffb0db
Change code due to CR
ekuvardin 2086142
Fix test after resolve bug with sync writer
ekuvardin c4cea58
Add ability rewrite predefined codec.
ekuvardin d52df35
Comment test due to bag in SyncWriter
ekuvardin a77588f
Update topic/src/main/java/tech/ydb/topic/TopicClient.java
pnv1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
32 changes: 32 additions & 0 deletions
32
topic/src/main/java/tech/ydb/topic/description/CodecRegistry.java
This file contains hidden or 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,32 @@ | ||
| package tech.ydb.topic.description; | ||
|
|
||
| /** | ||
| * Interface for register custom codec | ||
| * | ||
| * @author Evgeny Kuvardin | ||
| **/ | ||
| public interface CodecRegistry { | ||
alex268 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Register codec implementation | ||
| * @param codec codec identifier | ||
| * @param customTopicCodec codec implementation | ||
| * @return previous implementation with associated codec | ||
| */ | ||
| CustomTopicCodec registerCustomCodec(int codec, CustomTopicCodec customTopicCodec); | ||
|
|
||
| /** | ||
| * Unregister codec implementation | ||
| * @param codec codec identifier | ||
| * @return previous implementation with associated codec | ||
| */ | ||
| CustomTopicCodec unregisterCustomCodec(int codec); | ||
|
|
||
| /** | ||
| * Get codec implementation by associated id | ||
| * @param codec codec identifier | ||
| * @return codec implementation | ||
| */ | ||
| CustomTopicCodec getCustomCodec(int codec); | ||
|
|
||
| } | ||
48 changes: 48 additions & 0 deletions
48
topic/src/main/java/tech/ydb/topic/description/CodecRegistryImpl.java
This file contains hidden or 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,48 @@ | ||
| package tech.ydb.topic.description; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| /** | ||
| * Register for custom topic codec. Local to TopicClient | ||
| * | ||
| * @author Evgeny Kuvardin | ||
| **/ | ||
| public class CodecRegistryImpl implements CodecRegistry { | ||
|
|
||
| /** | ||
| * Make customCodecMap concurrent since register/unregister/read can be from different threads | ||
| */ | ||
| final Map<Integer, CustomTopicCodec> customCodecMap; | ||
|
|
||
| public CodecRegistryImpl() { | ||
| customCodecMap = new ConcurrentHashMap<>(); | ||
| } | ||
|
|
||
| @Override | ||
| public CustomTopicCodec registerCustomCodec(int codec, CustomTopicCodec customTopicCodec) { | ||
| assert customTopicCodec != null; | ||
|
|
||
| if (Codec.getInstance().isReserved(codec)) { | ||
| throw new RuntimeException( | ||
| "Create custom codec for reserved code not allowed: " + codec + " .Use code more than 10000"); | ||
| } | ||
|
|
||
| return customCodecMap.put(codec, customTopicCodec); | ||
| } | ||
|
|
||
| @Override | ||
| public CustomTopicCodec unregisterCustomCodec(int codec) { | ||
| if (Codec.getInstance().isReserved(codec)) { | ||
| throw new RuntimeException( | ||
| "Create custom codec for reserved code not allowed: " + codec + " .Use code more than 10000"); | ||
| } | ||
|
|
||
| return customCodecMap.remove(codec); | ||
| } | ||
|
|
||
| @Override | ||
| public CustomTopicCodec getCustomCodec(int codec) { | ||
| return customCodecMap.get(codec); | ||
| } | ||
| } |
This file contains hidden or 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
57 changes: 57 additions & 0 deletions
57
topic/src/main/java/tech/ydb/topic/description/CustomTopicCodec.java
This file contains hidden or 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,57 @@ | ||
| package tech.ydb.topic.description; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
|
|
||
| /** | ||
| * Interface for custom codec implementation. | ||
| * <p> | ||
| * You can use custom codec as below | ||
| * 1. Implement interface methods | ||
| * 2. Use in write data | ||
| * CustomTopicCodec customCodecImpl = .... | ||
| * Topic client = TopicClient.newClient(ydbTransport).build(); | ||
| * <p> | ||
| * client.registerCodec(10113, customCodecImpl); | ||
| * WriterSettings settings = WriterSettings.newBuilder() | ||
| * .setTopicPath(topicName) | ||
| * .setCodec(codecId) | ||
| * .build(); | ||
| * <p> | ||
| * SyncWriter writer = client.createSyncWriter(settings); | ||
| * <p> | ||
| * 3. Use in read data | ||
| * CustomTopicCodec customCodecImpl = .... | ||
| * Topic client = TopicClient.newClient(ydbTransport).build(); | ||
| * <p> | ||
| * ReaderSettings readerSettings = ReaderSettings.newBuilder() | ||
| * .addTopic(TopicReadSettings.newBuilder().setPath(topicName).build()) | ||
| * .setConsumerName(TEST_CONSUMER1) | ||
| * .build(); | ||
| * <p> | ||
| * SyncReader reader = client.createSyncReader(readerSettings); | ||
| * | ||
| */ | ||
| public interface CustomTopicCodec { | ||
|
|
||
| /** | ||
| * Decode data | ||
| * | ||
| * @param byteArrayOutputStream input stream | ||
| * @return output stream | ||
| * @throws IOException throws when error occurs | ||
| */ | ||
| InputStream decode(ByteArrayInputStream byteArrayOutputStream) throws IOException; | ||
|
|
||
| /** | ||
| * Encode data | ||
| * | ||
| * @param byteArrayInputStream input stream | ||
| * @return output stream | ||
| * @throws IOException throws when error occurs | ||
| */ | ||
| OutputStream encode(ByteArrayOutputStream byteArrayInputStream) throws IOException; | ||
| } |
This file contains hidden or 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
52 changes: 52 additions & 0 deletions
52
topic/src/main/java/tech/ydb/topic/impl/CodecRegistryImpl.java
This file contains hidden or 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,52 @@ | ||
| package tech.ydb.topic.impl; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import tech.ydb.topic.description.Codec; | ||
| import tech.ydb.topic.description.CodecRegistry; | ||
| import tech.ydb.topic.description.CustomTopicCodec; | ||
|
|
||
| /** | ||
| * Register for custom topic codec. Local to TopicClient | ||
| * | ||
| * @author Evgeny Kuvardin | ||
| **/ | ||
| public class CodecRegistryImpl implements CodecRegistry { | ||
|
|
||
| /** | ||
| * Make customCodecMap concurrent since register/unregister/read can be from different threads | ||
| */ | ||
| final Map<Integer, CustomTopicCodec> customCodecMap; | ||
|
|
||
| public CodecRegistryImpl() { | ||
| customCodecMap = new ConcurrentHashMap<>(); | ||
| } | ||
|
|
||
| @Override | ||
| public CustomTopicCodec registerCustomCodec(int codec, CustomTopicCodec customTopicCodec) { | ||
| assert customTopicCodec != null; | ||
|
|
||
| if (Codec.getInstance().isReserved(codec)) { | ||
| throw new RuntimeException( | ||
| "Create custom codec for reserved code not allowed: " + codec + " .Use code more than 10000"); | ||
| } | ||
|
|
||
| return customCodecMap.put(codec, customTopicCodec); | ||
| } | ||
|
|
||
| @Override | ||
| public CustomTopicCodec unregisterCustomCodec(int codec) { | ||
| if (Codec.getInstance().isReserved(codec)) { | ||
| throw new RuntimeException( | ||
| "Create custom codec for reserved code not allowed: " + codec + " .Use code more than 10000"); | ||
| } | ||
|
|
||
| return customCodecMap.remove(codec); | ||
| } | ||
|
|
||
| @Override | ||
| public CustomTopicCodec getCustomCodec(int codec) { | ||
| return customCodecMap.get(codec); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.