junaiddshaukat commented on code in PR #39494:
URL: https://github.com/apache/beam/pull/39494#discussion_r3656513582


##########
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/translation/KafkaStreamsStateInternals.java:
##########
@@ -0,0 +1,393 @@
+/*
+ * 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.beam.runners.kafka.streams.translation;
+
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.beam.runners.core.StateInternals;
+import org.apache.beam.runners.core.StateNamespace;
+import org.apache.beam.runners.core.StateTag;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.CoderException;
+import org.apache.beam.sdk.coders.InstantCoder;
+import org.apache.beam.sdk.coders.ListCoder;
+import org.apache.beam.sdk.state.BagState;
+import org.apache.beam.sdk.state.CombiningState;
+import org.apache.beam.sdk.state.MapState;
+import org.apache.beam.sdk.state.MultimapState;
+import org.apache.beam.sdk.state.OrderedListState;
+import org.apache.beam.sdk.state.ReadableState;
+import org.apache.beam.sdk.state.SetState;
+import org.apache.beam.sdk.state.State;
+import org.apache.beam.sdk.state.StateBinder;
+import org.apache.beam.sdk.state.StateContext;
+import org.apache.beam.sdk.state.StateSpec;
+import org.apache.beam.sdk.state.ValueState;
+import org.apache.beam.sdk.state.WatermarkHoldState;
+import org.apache.beam.sdk.transforms.Combine.CombineFn;
+import org.apache.beam.sdk.transforms.CombineWithContext;
+import org.apache.beam.sdk.transforms.windowing.TimestampCombiner;
+import org.apache.beam.sdk.util.CoderUtils;
+import org.apache.beam.sdk.util.CombineFnUtil;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Instant;
+
+/**
+ * A {@link StateInternals} for one key, backed by a Kafka Streams {@link 
KeyValueStore}.
+ *
+ * <p>Beam addresses a state cell by {@code (key, StateNamespace, StateTag)}; 
a windowed pipeline
+ * puts each window's state in its own namespace. Every cell is stored as one 
entry in the shared
+ * per-transform store under a composite byte key {@code len(key)|key | 
len(ns)|ns | len(tag)|tag},
+ * so all cells for one Beam key share a prefix and a whole key's state can be 
range-scanned. The
+ * value is the cell's contents encoded with its Beam {@link Coder}. Writing 
straight to the store
+ * (rather than buffering and flushing) keeps this restart-safe for free: the 
store is changelogged
+ * and, under exactly-once, its writes commit atomically with the input 
offsets.
+ *
+ * <p>Modeled on the Spark runner's {@code SparkStateInternals}; the 
difference is that each cell
+ * reads and writes its own store entry instead of an in-memory table, so 
there is no separate
+ * persist step.
+ */
+class KafkaStreamsStateInternals<K> implements StateInternals {
+
+  private final @NonNull K key;
+  private final byte[] encodedKey;
+  private final KeyValueStore<byte[], byte[]> store;
+
+  KafkaStreamsStateInternals(
+      @NonNull K key, byte[] encodedKey, KeyValueStore<byte[], byte[]> store) {
+    this.key = key;
+    this.encodedKey = encodedKey;
+    this.store = store;
+  }
+
+  @Override
+  public Object getKey() {
+    return key;
+  }
+
+  @Override
+  public <T extends State> T state(
+      StateNamespace namespace, StateTag<T> address, StateContext<?> c) {
+    return address.getSpec().bind(address.getId(), new 
KafkaStreamsStateBinder(namespace, c));
+  }
+
+  /** The composite store key for one cell: {@code len|key len|namespace 
len|tagId}. */
+  private byte[] compositeKey(StateNamespace namespace, String id) {

Review Comment:
   Done, it now builds into one exactly-sized array with System.arraycopy, and 
caches the key | namespace prefix, which is reused across the several tags one 
ReduceFnRunner turn touches. That removes the stream growth and the final copy.
   
   I kept the length prefixes rather than a separator, though: an encoded key 
is arbitrary coder output and a namespace stringKey can contain any byte too, 
so key="a/b", ns="c" and key="a", ns="b/c" would produce the same bytes with 
any separator. The prefixes are also what lets the timer scan read the encoded 
key back out (your comment below). I moved the byte helpers into StoreKeys and 
wrote the reasoning there.



-- 
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]

Reply via email to