-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-7223: add tests in preparation for suppression #5687
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
3b74c63
70d74df
f7b8ea3
2a78450
189c83b
4c2a4d7
7c000b2
b40354c
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,128 @@ | ||
| /* | ||
| * 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.kstream.internals; | ||
|
|
||
| import org.apache.kafka.common.serialization.ByteBufferDeserializer; | ||
| import org.apache.kafka.common.serialization.ByteBufferSerializer; | ||
| import org.apache.kafka.common.serialization.Deserializer; | ||
| import org.apache.kafka.common.serialization.Serde; | ||
| import org.apache.kafka.common.serialization.Serializer; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.Map; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class FullChangeSerde<T> implements Serde<Change<T>> { | ||
| private final Serde<T> inner; | ||
|
|
||
| public FullChangeSerde(final Serde<T> inner) { | ||
|
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. Can we get a unit test for
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. Oh! good catch.
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. Thanks for this. I added one, and it turns out I caught a couple of bugs! |
||
| this.inner = requireNonNull(inner); | ||
| } | ||
|
|
||
| @Override | ||
| public void configure(final Map<String, ?> configs, final boolean isKey) { | ||
| inner.configure(configs, isKey); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| inner.close(); | ||
| } | ||
|
|
||
| @Override | ||
| public Serializer<Change<T>> serializer() { | ||
| final Serializer<T> innerSerializer = inner.serializer(); | ||
| final ByteBufferSerializer byteBufferSerializer = new ByteBufferSerializer(); | ||
|
|
||
| return new Serializer<Change<T>>() { | ||
| @Override | ||
| public void configure(final Map<String, ?> configs, final boolean isKey) { | ||
| innerSerializer.configure(configs, isKey); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] serialize(final String topic, final Change<T> data) { | ||
| if (data == null) { | ||
| return null; | ||
| } | ||
| final byte[] oldBytes = data.oldValue == null ? null : innerSerializer.serialize(topic, data.oldValue); | ||
| final int oldSize = oldBytes == null ? -1 : oldBytes.length; | ||
| final byte[] newBytes = data.newValue == null ? null : innerSerializer.serialize(topic, data.newValue); | ||
| final int newSize = newBytes == null ? -1 : newBytes.length; | ||
|
|
||
| final ByteBuffer buffer = ByteBuffer.allocate( | ||
| 4 + (oldSize == -1 ? 0 : oldSize) + 4 + (newSize == -1 ? 0 : newSize) | ||
| ); | ||
| buffer.putInt(oldSize); | ||
| if (oldBytes != null) { | ||
| buffer.put(oldBytes); | ||
| } | ||
| buffer.putInt(newSize); | ||
| if (newBytes != null) { | ||
| buffer.put(newBytes); | ||
| } | ||
| return byteBufferSerializer.serialize(null, buffer); | ||
|
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. @guozhangwang , rather than copy/paste the non-trivial transformation logic, what do you think about just delegating to the actual ByteBufferSerializer? The deserializer is trivial, but I went ahead and used it in my deserializer, too, for symmetry. I'm thinking that as long as the ByteBufferSerializer is stable, and it doesn't do anything weird in the future, this should be fine. And that should be the case, since future versions of the code are going to have to be compatible with data written by older versions.
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. Sounds good. |
||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| innerSerializer.close(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public Deserializer<Change<T>> deserializer() { | ||
| final Deserializer<T> innerDeserializer = inner.deserializer(); | ||
| final ByteBufferDeserializer byteBufferDeserializer = new ByteBufferDeserializer(); | ||
| return new Deserializer<Change<T>>() { | ||
| @Override | ||
| public void configure(final Map<String, ?> configs, final boolean isKey) { | ||
| innerDeserializer.configure(configs, isKey); | ||
| } | ||
|
|
||
| @Override | ||
| public Change<T> deserialize(final String topic, final byte[] data) { | ||
| if (data == null) { | ||
| return null; | ||
| } | ||
| final ByteBuffer buffer = byteBufferDeserializer.deserialize(null, data); | ||
|
|
||
| final int oldSize = buffer.getInt(); | ||
| final byte[] oldBytes = oldSize == -1 ? null : new byte[oldSize]; | ||
| if (oldBytes != null) { | ||
| buffer.get(oldBytes); | ||
| } | ||
| final T oldValue = oldBytes == null ? null : innerDeserializer.deserialize(topic, oldBytes); | ||
|
|
||
| final int newSize = buffer.getInt(); | ||
| final byte[] newBytes = newSize == -1 ? null : new byte[newSize]; | ||
| if (newBytes != null) { | ||
| buffer.get(newBytes); | ||
| } | ||
| final T newValue = newBytes == null ? null : innerDeserializer.deserialize(topic, newBytes); | ||
| return new Change<>(newValue, oldValue); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| innerDeserializer.close(); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -364,8 +364,13 @@ public <K1> KStream<K1, V> toStream(final KeyValueMapper<? super K, ? super V, ? | |
| public KTable<K, V> suppress(final Suppressed<K> suppressed) { | ||
| final String name = builder.newProcessorName(SUPPRESS_NAME); | ||
|
|
||
| // TODO: follow-up pr to forward the k/v serdes | ||
|
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. This will be fixed in part 3 |
||
| final ProcessorSupplier<K, Change<V>> suppressionSupplier = | ||
| () -> new KTableSuppressProcessor<>(buildSuppress(suppressed)); | ||
| () -> new KTableSuppressProcessor<>( | ||
| buildSuppress(suppressed), | ||
| null, | ||
| null | ||
| ); | ||
|
|
||
| final ProcessorParameters<K, Change<V>> processorParameters = new ProcessorParameters<>( | ||
| suppressionSupplier, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,19 +16,29 @@ | |
| */ | ||
| package org.apache.kafka.streams.kstream.internals.suppress; | ||
|
|
||
| import org.apache.kafka.common.serialization.Serde; | ||
| import org.apache.kafka.streams.kstream.internals.Change; | ||
| import org.apache.kafka.streams.processor.Processor; | ||
| import org.apache.kafka.streams.processor.ProcessorContext; | ||
| import org.apache.kafka.streams.processor.internals.InternalProcessorContext; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class KTableSuppressProcessor<K, V> implements Processor<K, Change<V>> { | ||
| private final SuppressedImpl<K> suppress; | ||
| private InternalProcessorContext internalProcessorContext; | ||
|
|
||
| public KTableSuppressProcessor(final SuppressedImpl<K> suppress) { | ||
| this.suppress = suppress; | ||
| private final Serde<K> keySerde; | ||
| private final Serde<Change<V>> valueSerde; | ||
|
|
||
| public KTableSuppressProcessor(final SuppressedImpl<K> suppress, | ||
| final Serde<K> keySerde, | ||
| final Serde<Change<V>> valueSerde) { | ||
| this.suppress = requireNonNull(suppress); | ||
| this.keySerde = keySerde; | ||
|
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. Where are these two serdes used in this PR?
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. They aren't. They will be in part 3, so the tests need to call this constructor. I added this constructor and the dummy fields just so the (ignored) tests can compile. It's a bit awkward, I know.
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. Ack. |
||
| this.valueSerde = valueSerde; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -39,12 +49,18 @@ public void init(final ProcessorContext context) { | |
| @Override | ||
| public void process(final K key, final Change<V> value) { | ||
| if (suppress.getTimeToWaitForMoreEvents() == Duration.ZERO && definedRecordTime(key) <= internalProcessorContext.streamTime()) { | ||
| internalProcessorContext.forward(key, value); | ||
| if (shouldForward(value)) { | ||
| internalProcessorContext.forward(key, value); | ||
| } // else skip | ||
| } else { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
|
|
||
| private boolean shouldForward(final Change<V> value) { | ||
| return !(value.newValue == null && suppress.suppressTombstones()); | ||
| } | ||
|
|
||
| private long definedRecordTime(final K key) { | ||
| return suppress.getTimeDefinition().time(internalProcessorContext, key); | ||
| } | ||
|
|
@@ -55,10 +71,14 @@ public void close() { | |
|
|
||
| @Override | ||
| public String toString() { | ||
| return "KTableSuppressProcessor{suppress=" + suppress + '}'; | ||
| return "KTableSuppressProcessor{" + | ||
| "suppress=" + suppress + | ||
| ", keySerde=" + keySerde + | ||
| ", valueSerde=" + valueSerde + | ||
| '}'; | ||
| } | ||
|
|
||
| static class NotImplementedException extends RuntimeException { | ||
| public static class NotImplementedException extends RuntimeException { | ||
|
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. Just so we can assert that it happens in the scenario tests (which are in a different package) |
||
| NotImplementedException() { | ||
| super(); | ||
| } | ||
|
|
||
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.
Unlike the source/sink nodes, the
suppressprocessor needs to be able to handleChanges in which bothnewandoldare non-null.