|
17 | 17 | package org.apache.kafka.coordinator.group;
|
18 | 18 |
|
19 | 19 | import org.apache.kafka.common.protocol.ApiMessage;
|
20 |
| -import org.apache.kafka.common.protocol.ByteBufferAccessor; |
21 |
| -import org.apache.kafka.common.protocol.MessageUtil; |
22 | 20 | import org.apache.kafka.coordinator.common.runtime.CoordinatorLoader;
|
23 |
| -import org.apache.kafka.coordinator.common.runtime.CoordinatorRecord; |
24 |
| -import org.apache.kafka.coordinator.common.runtime.Deserializer; |
25 |
| -import org.apache.kafka.coordinator.common.runtime.Serializer; |
| 21 | +import org.apache.kafka.coordinator.common.runtime.CoordinatorRecordSerde; |
26 | 22 | import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentKey;
|
27 | 23 | import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentValue;
|
28 | 24 | import org.apache.kafka.coordinator.group.generated.ConsumerGroupMemberMetadataKey;
|
|
53 | 49 | import org.apache.kafka.coordinator.group.generated.ShareGroupTargetAssignmentMemberValue;
|
54 | 50 | import org.apache.kafka.coordinator.group.generated.ShareGroupTargetAssignmentMetadataKey;
|
55 | 51 | import org.apache.kafka.coordinator.group.generated.ShareGroupTargetAssignmentMetadataValue;
|
56 |
| -import org.apache.kafka.server.common.ApiMessageAndVersion; |
57 |
| - |
58 |
| -import java.nio.BufferUnderflowException; |
59 |
| -import java.nio.ByteBuffer; |
60 |
| - |
61 |
| -/** |
62 |
| - * Serializer/Deserializer for {@link CoordinatorRecord}. The format is defined below: |
63 |
| - * <pre> |
64 |
| - * record_key = [record_type key_message] |
65 |
| - * record_value = [value_version value_message] |
66 |
| - * |
67 |
| - * record_type : The record type is currently define as the version of the key |
68 |
| - * {@link ApiMessageAndVersion} object. |
69 |
| - * key_message : The serialized message of the key {@link ApiMessageAndVersion} object. |
70 |
| - * value_version : The value version is currently define as the version of the value |
71 |
| - * {@link ApiMessageAndVersion} object. |
72 |
| - * value_message : The serialized message of the value {@link ApiMessageAndVersion} object. |
73 |
| - * </pre> |
74 |
| - */ |
75 |
| -@SuppressWarnings({ "ClassDataAbstractionCoupling", "CyclomaticComplexity" }) |
76 |
| -public class GroupCoordinatorRecordSerde implements Serializer<CoordinatorRecord>, Deserializer<CoordinatorRecord> { |
77 |
| - @Override |
78 |
| - public byte[] serializeKey(CoordinatorRecord record) { |
79 |
| - // Record does not accept a null key. |
80 |
| - return MessageUtil.toVersionPrefixedBytes( |
81 |
| - record.key().version(), |
82 |
| - record.key().message() |
83 |
| - ); |
84 |
| - } |
85 | 52 |
|
| 53 | +public class GroupCoordinatorRecordSerde extends CoordinatorRecordSerde { |
86 | 54 | @Override
|
87 |
| - public byte[] serializeValue(CoordinatorRecord record) { |
88 |
| - // Tombstone is represented with a null value. |
89 |
| - if (record.value() == null) { |
90 |
| - return null; |
91 |
| - } else { |
92 |
| - return MessageUtil.toVersionPrefixedBytes( |
93 |
| - record.value().version(), |
94 |
| - record.value().message() |
95 |
| - ); |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - @Override |
100 |
| - public CoordinatorRecord deserialize( |
101 |
| - ByteBuffer keyBuffer, |
102 |
| - ByteBuffer valueBuffer |
103 |
| - ) throws RuntimeException { |
104 |
| - final short recordType = readVersion(keyBuffer, "key"); |
105 |
| - final ApiMessage keyMessage = apiMessageKeyFor(recordType); |
106 |
| - readMessage(keyMessage, keyBuffer, recordType, "key"); |
107 |
| - |
108 |
| - if (valueBuffer == null) { |
109 |
| - return new CoordinatorRecord(new ApiMessageAndVersion(keyMessage, recordType), null); |
110 |
| - } |
111 |
| - |
112 |
| - final ApiMessage valueMessage = apiMessageValueFor(recordType); |
113 |
| - final short valueVersion = readVersion(valueBuffer, "value"); |
114 |
| - readMessage(valueMessage, valueBuffer, valueVersion, "value"); |
115 |
| - |
116 |
| - return new CoordinatorRecord( |
117 |
| - new ApiMessageAndVersion(keyMessage, recordType), |
118 |
| - new ApiMessageAndVersion(valueMessage, valueVersion) |
119 |
| - ); |
120 |
| - } |
121 |
| - |
122 |
| - private short readVersion(ByteBuffer buffer, String name) throws RuntimeException { |
123 |
| - try { |
124 |
| - return buffer.getShort(); |
125 |
| - } catch (BufferUnderflowException ex) { |
126 |
| - throw new RuntimeException(String.format("Could not read version from %s's buffer.", name)); |
127 |
| - } |
128 |
| - } |
129 |
| - |
130 |
| - private void readMessage(ApiMessage message, ByteBuffer buffer, short version, String name) throws RuntimeException { |
131 |
| - try { |
132 |
| - message.read(new ByteBufferAccessor(buffer), version); |
133 |
| - } catch (RuntimeException ex) { |
134 |
| - throw new RuntimeException(String.format("Could not read record with version %d from %s's buffer due to: %s.", |
135 |
| - version, name, ex.getMessage()), ex); |
136 |
| - } |
137 |
| - } |
138 |
| - |
139 |
| - private ApiMessage apiMessageKeyFor(short recordType) { |
140 |
| - switch (recordType) { |
| 55 | + protected ApiMessage apiMessageKeyFor(short recordVersion) { |
| 56 | + switch (recordVersion) { |
141 | 57 | case 0:
|
142 | 58 | case 1:
|
143 | 59 | return new OffsetCommitKey();
|
@@ -170,12 +86,13 @@ private ApiMessage apiMessageKeyFor(short recordType) {
|
170 | 86 | case 15:
|
171 | 87 | return new ShareGroupStatePartitionMetadataKey();
|
172 | 88 | default:
|
173 |
| - throw new CoordinatorLoader.UnknownRecordTypeException(recordType); |
| 89 | + throw new CoordinatorLoader.UnknownRecordTypeException(recordVersion); |
174 | 90 | }
|
175 | 91 | }
|
176 | 92 |
|
177 |
| - private ApiMessage apiMessageValueFor(short recordType) { |
178 |
| - switch (recordType) { |
| 93 | + @Override |
| 94 | + protected ApiMessage apiMessageValueFor(short recordVersion) { |
| 95 | + switch (recordVersion) { |
179 | 96 | case 0:
|
180 | 97 | case 1:
|
181 | 98 | return new OffsetCommitValue();
|
@@ -208,7 +125,7 @@ private ApiMessage apiMessageValueFor(short recordType) {
|
208 | 125 | case 15:
|
209 | 126 | return new ShareGroupStatePartitionMetadataValue();
|
210 | 127 | default:
|
211 |
| - throw new CoordinatorLoader.UnknownRecordTypeException(recordType); |
| 128 | + throw new CoordinatorLoader.UnknownRecordTypeException(recordVersion); |
212 | 129 | }
|
213 | 130 | }
|
214 | 131 | }
|
0 commit comments