bbejeck commented on code in PR #22626: URL: https://github.com/apache/kafka/pull/22626#discussion_r3444567603
########## streams/src/main/java/org/apache/kafka/streams/state/internals/InMemoryTransactionBuffer.java: ########## @@ -0,0 +1,151 @@ +/* + * 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.utils.Bytes; +import org.apache.kafka.streams.KeyValue; + +import java.util.Map; +import java.util.NavigableMap; +import java.util.NoSuchElementException; +import java.util.Optional; + +/** + * A {@link TransactionBuffer} implementation for {@link InMemoryKeyValueStore}. + * <p> + * Uses Option A from the design: accepts that non-owner scan iterators read values + * live from the base map (no snapshot isolation on values). This avoids the O(N) + * commit cost and double memory usage of the committed snapshot approach (Option D). + * Staged-key reads are always correct (staging takes precedence). The gap only affects + * base-store values that are concurrently being committed during iteration — a narrow + * window in practice. + */ +class InMemoryTransactionBuffer extends AbstractTransactionBuffer<Bytes> { + + private final NavigableMap<Bytes, byte[]> baseMap; + + InMemoryTransactionBuffer(final NavigableMap<Bytes, byte[]> baseMap) { + this.baseMap = baseMap; + } + + @Override + int estimateKeySize(final Bytes key) { + return key.get().length; + } + + @Override + void stageToBackend(final Bytes key, final byte[] value) { + // no-op — staging map is sufficient; no write-batch concept for in-memory + } + + @Override + ManagedKeyValueIterator<Bytes, byte[]> newBaseIterator(final Bytes from, final Bytes to) { + return newBaseIterator(from, to, true, true); + } + + @Override + ManagedKeyValueIterator<Bytes, byte[]> newBaseIterator(final Bytes from, final Bytes to, + final boolean forward, final boolean toInclusive) { + final NavigableMap<Bytes, byte[]> view; + if (from != null && to != null) { + view = baseMap.subMap(from, true, to, toInclusive); + } else if (from != null) { + view = baseMap.tailMap(from, true); + } else if (to != null) { + view = baseMap.headMap(to, toInclusive); + } else { + view = baseMap; + } + return new SnapshotMapIterator(forward ? view : view.descendingMap()); Review Comment: This can result in a `ConcurrentModificationException` - `view` is a live view over `baseMap` solved by https://github.com/apache/kafka/pull/22626/changes#diff-dd735c4d6edacf3ee7378bfdcc2d5e89499e6846913fbbd9ee147b54235fc50bR57 ########## streams/src/main/java/org/apache/kafka/streams/state/internals/InMemoryKeyValueStore.java: ########## @@ -85,7 +86,12 @@ public void init(final StateStoreContext stateStoreContext, (RecordBatchingStateRestoreCallback) records -> { synchronized (position) { for (final ConsumerRecord<byte[], byte[]> record : records) { - put(Bytes.wrap(record.key()), record.value()); + final Bytes key = Bytes.wrap(record.key()); Review Comment: this inlines `map.put/get` vs calling `put` -> `putInternal` which in turn called ` StoreQueryUtils.updatePosition(position, context)` so `position` is now dropped, so maybe go back to `put`? ########## streams/src/main/java/org/apache/kafka/streams/state/internals/InMemoryKeyValueStore.java: ########## Review Comment: I think this should get updated to `ConcurrentSkipListMap` I'll explain in an another comment. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
