-
Notifications
You must be signed in to change notification settings - Fork 15k
KAFKA-20132: Implement TimestampedKeyValueStoreWithHeaders (4/N) #21454
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 3 commits
61d0419
b94eea0
973d7c1
4a05553
d81dddb
72497d6
3b6ee2d
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,99 @@ | ||
| /* | ||
| * 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.streams.state.internals; | ||
|
|
||
| import org.apache.kafka.common.header.Headers; | ||
| import org.apache.kafka.common.utils.Bytes; | ||
| import org.apache.kafka.streams.KeyValue; | ||
| import org.apache.kafka.streams.state.KeyValueStore; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.apache.kafka.streams.state.internals.ValueTimestampHeadersDeserializer.headers; | ||
| import static org.apache.kafka.streams.state.internals.ValueTimestampHeadersDeserializer.rawValue; | ||
| import static org.apache.kafka.streams.state.internals.ValueTimestampHeadersDeserializer.timestamp; | ||
|
|
||
| /** | ||
| * Change-logging wrapper for a timestamped key-value bytes store whose values also carry headers. | ||
| * <p> | ||
| * the header-aware serialized value format produced by {@link ValueTimestampHeadersSerializer}. | ||
| * <p> | ||
| * Semantics: | ||
| * - The inner store value format is: | ||
| * [ varint header_length ][ header_bytes ][ 8-byte timestamp ][ value_bytes ] | ||
| * - The changelog record value logged via {@code log(...)} remains just {@code value_bytes} | ||
| * (no timestamp, no headers), and the timestamp is logged separately. | ||
| */ | ||
| public class ChangeLoggingTimestampedKeyValueBytesStoreWithHeaders | ||
| extends ChangeLoggingKeyValueBytesStore { | ||
|
|
||
| ChangeLoggingTimestampedKeyValueBytesStoreWithHeaders(final KeyValueStore<Bytes, byte[]> inner) { | ||
| super(inner); | ||
| } | ||
|
|
||
| @Override | ||
| public void put(final Bytes key, | ||
| final byte[] valueTimestampHeaders) { | ||
| wrapped().put(key, valueTimestampHeaders); | ||
| log( | ||
| key, | ||
| rawValue(valueTimestampHeaders), | ||
| valueTimestampHeaders == null | ||
| ? internalContext.recordContext().timestamp() | ||
| : timestamp(valueTimestampHeaders), | ||
| headers(valueTimestampHeaders) | ||
|
||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] putIfAbsent(final Bytes key, | ||
| final byte[] valueTimestampHeaders) { | ||
| final byte[] previous = wrapped().putIfAbsent(key, valueTimestampHeaders); | ||
| if (previous == null) { | ||
| // then it was absent | ||
| log( | ||
| key, | ||
| rawValue(valueTimestampHeaders), | ||
| valueTimestampHeaders == null | ||
| ? internalContext.recordContext().timestamp() | ||
| : timestamp(valueTimestampHeaders), | ||
| headers(valueTimestampHeaders) | ||
| ); | ||
| } | ||
| return previous; | ||
| } | ||
|
|
||
| @Override | ||
| public void putAll(final List<KeyValue<Bytes, byte[]>> entries) { | ||
| wrapped().putAll(entries); | ||
| for (final KeyValue<Bytes, byte[]> entry : entries) { | ||
| final byte[] valueTimestampHeaders = entry.value; | ||
| log( | ||
| entry.key, | ||
| rawValue(valueTimestampHeaders), | ||
| valueTimestampHeaders == null | ||
| ? internalContext.recordContext().timestamp() | ||
| : timestamp(valueTimestampHeaders), | ||
| headers(valueTimestampHeaders) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| void log(final Bytes key, final byte[] value, final long timestamp, final Headers headers) { | ||
|
||
| internalContext.logChange(name(), key, value, timestamp, headers, wrapped().getPosition()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -164,4 +164,17 @@ static Headers headers(final byte[] rawValueTimestampHeaders) { | |
| final byte[] rawHeaders = readBytes(buffer, headersSize); | ||
| return HEADERS_DESERIALIZER.deserialize("", rawHeaders); | ||
| } | ||
| /** | ||
| * Extract raw value from serialized ValueTimestampHeaders. | ||
| */ | ||
| static byte[] rawValue(final byte[] rawValueTimestampHeaders) { | ||
|
Member
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. We have
Contributor
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. that method returns the deserialized value while
Member
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. Yes, I am not questioning that we add But it's somewhat unrelated to this PR, and it's only internal code here. So we could remove
Contributor
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. We can remove it at some point if not needed: https://issues.apache.org/jira/browse/KAFKA-20193 |
||
| if (rawValueTimestampHeaders == null) { | ||
| return null; | ||
| } | ||
|
|
||
| final ByteBuffer buffer = ByteBuffer.wrap(rawValueTimestampHeaders); | ||
| final int headersSize = ByteUtils.readVarint(buffer); | ||
| buffer.position(buffer.position() + headersSize + Long.BYTES); | ||
| return readBytes(buffer, buffer.remaining()); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.