guozhangwang commented on code in PR #12393:
URL: https://github.com/apache/kafka/pull/12393#discussion_r939822059
##########
streams/src/main/java/org/apache/kafka/streams/kstream/internals/KStreamImplJoin.java:
##########
@@ -309,9 +344,17 @@ private <K, V1, V2>
StoreBuilder<KeyValueStore<TimestampedKeyAndJoinSide<K>, Lef
final TimestampedKeyAndJoinSideSerde<K> timestampedKeyAndJoinSideSerde
= new TimestampedKeyAndJoinSideSerde<>(streamJoinedInternal.keySerde());
final LeftOrRightValueSerde<V1, V2> leftOrRightValueSerde = new
LeftOrRightValueSerde<>(streamJoinedInternal.valueSerde(),
streamJoinedInternal.otherValueSerde());
+ final KeyValueBytesStoreSupplier storeSupplier;
Review Comment:
This is a meta comment regarding "2. To determine if the persistent kv store
in KStreamImplJoin should be
transactional" in the KIP discussion: it seems that we are passing the
transactional flag from `materialized` -> "storeSupplier" -> "KStreamImplJoin
(not sure why we'd need this one though, see my other comment)", and then
depend on the flag we create the corresponding stores here.
What I'm thinking though, is that instead of setting this flag on a
per-store basis, we just set it on a global config, and this global config is
only for compatibility purposes: i.e. in the long run we would consider all
built-in stores are transactional by default, unless users say otherwise. So
for now this global config is default to "not transactional" while in some
future versions we just set default to "transaction".
Then we would not need to pass this flag all along this hierarchy, and as we
discussed on the KIP. When doing commit / recover we can have a unified logic
that do not need to check the store's transactional flag any more. What do you
think?
##########
streams/src/main/java/org/apache/kafka/streams/kstream/StreamJoined.java:
##########
@@ -62,7 +64,8 @@ private StreamJoined(final Serde<K> keySerde,
final String name,
final String storeName,
final boolean loggingEnabled,
- final Map<String, String> topicConfig) {
+ final Map<String, String> topicConfig,
+ final boolean transactional) {
Review Comment:
It seems here that, we enforce both sides of the joining to be
transactional, or none. If only one of the store is defined as txnal via
materialized while the other is not, then we just enforce both as non-txnal.
What's the rationale behind this?
##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/ProcessorStateManager.java:
##########
@@ -608,6 +618,11 @@ public void checkpoint() {
// checkpoint those stores that are only logged and persistent to the
checkpoint file
final Map<TopicPartition, Long> checkpointingOffsets = new HashMap<>();
for (final StateStoreMetadata storeMetadata : stores.values()) {
+ // do not checkpont non-transactional state stores under EOS
+ if (eosEnabled && !storeMetadata.stateStore.transactional()) {
Review Comment:
As we discussed in the KIP, I think we can just remove this branched logic
as well and always checkpoint.
##########
streams/src/main/java/org/apache/kafka/streams/state/internals/AbstractTransactionalStore.java:
##########
@@ -0,0 +1,221 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+import java.util.List;
+import java.util.Map;
+import org.apache.kafka.common.utils.Bytes;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.processor.ProcessorContext;
+import org.apache.kafka.streams.processor.StateStore;
+import org.apache.kafka.streams.processor.StateStoreContext;
+import org.apache.kafka.streams.state.KeyValueIterator;
+import org.apache.kafka.streams.state.KeyValueStore;
+
+public abstract class AbstractTransactionalStore implements
KeyValueStore<Bytes, byte[]> {
+ static final String PREFIX = "transactional-";
+ private final ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
+
+ private Path commitMarker;
+
+ final KeyValueSegment tmpStore;
+
+ Map<String, Object> configs;
+ File stateDir;
+
+ abstract KeyValueStore<Bytes, byte[]> mainStore();
+
+ AbstractTransactionalStore(final KeyValueSegment tmpStore) {
+ this.tmpStore = tmpStore;
+ }
+
+ @Deprecated
+ @Override
+ public void init(final ProcessorContext context, final StateStore root) {
+ if (context instanceof StateStoreContext) {
+ init((StateStoreContext) context, root);
+ } else {
+ throw new UnsupportedOperationException(
+ "Use TransactionalKeyValueStore#init(StateStoreContext,
StateStore) instead."
+ );
+ }
+ }
+
+ @Override
+ public void init(final StateStoreContext context, final StateStore root) {
+ doInit(context.appConfigs(), context.stateDir());
+ mainStore().init(context, root);
+ }
+
+ void doInit(final Map<String, Object> configs, final File stateDir) {
+ this.configs = configs;
+ this.stateDir = stateDir;
+ commitMarker = stateDir.toPath().resolve(PREFIX + name() + ".commit");
+ tmpStore.openDB(configs, stateDir);
+ }
+
+ @Override
+ public void close() {
+ tmpStore.close();
+ mainStore().close();
+ }
+
+ @Override
+ public void commit(final Long changelogOffset) {
+ tmpStore.commit(changelogOffset);
+ createCommitMarker(changelogOffset);
+ doCommit();
+ }
+
+ @Override
+ public Long recover(final Long changelogOffset) {
+ if (Files.exists(commitMarker)) {
Review Comment:
If the marker file does exist but is corrupted and hence we cannot read out
the long value, do we really want to return `null` and let the caller to wipe
out the whole store?
If this happens, should we be certain that we fail at " 2. Create a commit
marker with a changelog offset corresponding to the state we are committing."
and hence we know nothing from the tmp store is written, so we can just safely
return the changelog offset?
##########
streams/src/main/java/org/apache/kafka/streams/state/internals/AbstractTransactionalStore.java:
##########
@@ -0,0 +1,221 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+import java.util.List;
+import java.util.Map;
+import org.apache.kafka.common.utils.Bytes;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.processor.ProcessorContext;
+import org.apache.kafka.streams.processor.StateStore;
+import org.apache.kafka.streams.processor.StateStoreContext;
+import org.apache.kafka.streams.state.KeyValueIterator;
+import org.apache.kafka.streams.state.KeyValueStore;
+
+public abstract class AbstractTransactionalStore implements
KeyValueStore<Bytes, byte[]> {
+ static final String PREFIX = "transactional-";
+ private final ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
+
+ private Path commitMarker;
+
+ final KeyValueSegment tmpStore;
+
+ Map<String, Object> configs;
+ File stateDir;
+
+ abstract KeyValueStore<Bytes, byte[]> mainStore();
+
+ AbstractTransactionalStore(final KeyValueSegment tmpStore) {
+ this.tmpStore = tmpStore;
+ }
+
+ @Deprecated
+ @Override
+ public void init(final ProcessorContext context, final StateStore root) {
+ if (context instanceof StateStoreContext) {
+ init((StateStoreContext) context, root);
+ } else {
+ throw new UnsupportedOperationException(
+ "Use TransactionalKeyValueStore#init(StateStoreContext,
StateStore) instead."
+ );
+ }
+ }
+
+ @Override
+ public void init(final StateStoreContext context, final StateStore root) {
+ doInit(context.appConfigs(), context.stateDir());
+ mainStore().init(context, root);
+ }
+
+ void doInit(final Map<String, Object> configs, final File stateDir) {
+ this.configs = configs;
+ this.stateDir = stateDir;
+ commitMarker = stateDir.toPath().resolve(PREFIX + name() + ".commit");
+ tmpStore.openDB(configs, stateDir);
+ }
+
+ @Override
+ public void close() {
+ tmpStore.close();
+ mainStore().close();
+ }
+
+ @Override
+ public void commit(final Long changelogOffset) {
+ tmpStore.commit(changelogOffset);
+ createCommitMarker(changelogOffset);
+ doCommit();
+ }
+
+ @Override
+ public Long recover(final Long changelogOffset) {
+ if (Files.exists(commitMarker)) {
Review Comment:
This is another comment: it seems that in the current proposal, we do not
necessarily guarantee that the returned offset is always strictly reflecting
the store's current snapshot, and lays on the hope that since restoration is
idempotent, all we need to make sure is that non committed data are not written
to the store.
In that case, I'm actually wondering if the txn marker, plus the `recover()`
function is necessary: since we do this tmp -> actual store copying after the
whole EOS commit transaction completes, then upon failures we know that our
actual store's image is definitely no more than the already-committed txn's
changelog offset, so we can always just blindly start restoring from the
checkpointed offset to the log-end offset (in read committed mode of course).
Right?
##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/ProcessorStateManager.java:
##########
@@ -247,8 +247,22 @@ void initializeStoreOffsetsFromCheckpoint(final boolean
storeDirIsEmpty) {
store.changelogPartition,
store.stateStore.name());
} else if (store.offset() == null) {
if
(loadedCheckpoints.containsKey(store.changelogPartition)) {
- final Long offset =
changelogOffsetFromCheckpointedOffset(loadedCheckpoints.remove(store.changelogPartition));
- store.setOffset(offset);
+ final Long changelogOffset =
changelogOffsetFromCheckpointedOffset(loadedCheckpoints.remove(store.changelogPartition));
+ // StateStore#recover might roll back or forward a
partially committed transaction
+ final Long recoveredOffset;
+ if (store.stateStore.transactional()) {
Review Comment:
Pleas see my other comment: it seems since this recover function is "best
effort" anyways, we can just always restore from the checkpointed
`changelogOffset`, and hence we do not need this branched logic?
--
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]