Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,6 @@ static <K extends Windowed> Suppressed<K> untilWindowCloses(final StrictBufferCo
* @return a suppression configuration
*/
static <K> Suppressed<K> untilTimeLimit(final Duration timeToWaitForMoreEvents, final BufferConfig bufferConfig) {
return new SuppressedImpl<>(timeToWaitForMoreEvents, bufferConfig, null);
return new SuppressedImpl<>(timeToWaitForMoreEvents, bufferConfig, null, false);
}
}
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>> {

Copy link
Copy Markdown
Contributor Author

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 suppress processor needs to be able to handle Changes in which both new and old are non-null.

private final Serde<T> inner;

public FullChangeSerde(final Serde<T> inner) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get a unit test for FullChangeSerde?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! good catch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
Expand Up @@ -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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public SuppressedImpl<K> buildFinalResultsSuppression(final Duration gracePeriod
return new SuppressedImpl<>(
gracePeriod,
bufferConfig,
(ProcessorContext context, K key) -> key.window().end()
(ProcessorContext context, K key) -> key.window().end(),
true
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are these two serdes used in this PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack.

this.valueSerde = valueSerde;
}

@Override
Expand All @@ -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);
}
Expand All @@ -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 {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ public class SuppressedImpl<K> implements Suppressed<K> {
private final BufferConfig bufferConfig;
private final Duration timeToWaitForMoreEvents;
private final TimeDefinition<K> timeDefinition;
private final boolean suppressTombstones;

public SuppressedImpl(final Duration suppressionTime,
final BufferConfig bufferConfig,
final TimeDefinition<K> timeDefinition) {
final TimeDefinition<K> timeDefinition,
final boolean suppressTombstones) {
this.timeToWaitForMoreEvents = suppressionTime == null ? DEFAULT_SUPPRESSION_TIME : suppressionTime;
this.timeDefinition = timeDefinition == null ? (context, anyKey) -> context.timestamp() : timeDefinition;
this.bufferConfig = bufferConfig == null ? DEFAULT_BUFFER_CONFIG : bufferConfig;
this.suppressTombstones = suppressTombstones;
}

interface TimeDefinition<K> {
Expand Down Expand Up @@ -73,4 +76,8 @@ public String toString() {
", timeDefinition=" + timeDefinition +
'}';
}

boolean suppressTombstones() {
return suppressTombstones;
}
}
Loading