damccorm commented on code in PR #41616:
URL: https://github.com/apache/beam/pull/41616#discussion_r4687551680
##########
runners/flink/src/main/java/org/apache/beam/runners/flink/translation/wrappers/streaming/state/FlinkStateInternals.java:
##########
@@ -1309,11 +1315,126 @@ public int hashCode() {
}
}
+ /**
+ * Whether keys of {@code coder} must be wrapped in {@link StructuralKey}
before being stored in a
+ * Flink {@link org.apache.flink.api.common.state.MapState}.
+ *
+ * <p>Heap and batch keyed state backends back {@code MapState} with a Java
{@link Map}, which
+ * compares keys by {@link Object#equals} and rejects {@code null} keys on
RocksDB. Coders that
+ * are {@link Coder#consistentWithEquals()} and cannot encode {@code null}
(i.e. not {@link
+ * NullableCoder}) can store raw keys directly, preserving savepoint
compatibility.
+ */
+ private static boolean usesStructuralKey(Coder<?> coder) {
Review Comment:
One thing I'm worried about here is upgrading an existing
`HashMapStateBackend` job from a savepoint taken before this change.
`AvroCoder` (including `SpecificRecord`/`GenericRecord`) and
`SerializableCoder` both return `consistentWithEquals() == false`, even though
those types typically implement `equals`/`hashCode` and already work on the
heap backend today.
Since `UnversionedTypeSerializerSnapshot.resolveSchemaCompatibility`
unconditionally returns `compatibleAsIs()`, restoring a pre-PR heap savepoint
will deserialize the old entries as raw `K` objects into `CopyOnWriteStateMap`
and then install `StructuralKeyCoder` without migrating them — so subsequent
lookups will silently miss the old entries (since
`StructuralKey.equals(rawKey)` is false), and the next checkpoint will throw a
`ClassCastException` when `StructuralKeyCoder.encode` gets a raw `K`. (RocksDB
is fine since the wire bytes match.)
Could we update
`UnversionedTypeSerializerSnapshot.resolveSchemaCompatibility` (in both 1.x and
2.0) to return `compatibleAfterMigration()` when transitioning between
`CoderTypeSerializer(C)` and `CoderTypeSerializer(StructuralKeyCoder(C))`, and
add a test that restores a snapshot created with the raw `keyCoder`?
##########
runners/flink/src/main/java/org/apache/beam/runners/flink/translation/wrappers/streaming/state/FlinkStateInternals.java:
##########
@@ -1309,11 +1315,126 @@ public int hashCode() {
}
}
+ /**
+ * Whether keys of {@code coder} must be wrapped in {@link StructuralKey}
before being stored in a
+ * Flink {@link org.apache.flink.api.common.state.MapState}.
+ *
+ * <p>Heap and batch keyed state backends back {@code MapState} with a Java
{@link Map}, which
+ * compares keys by {@link Object#equals} and rejects {@code null} keys on
RocksDB. Coders that
+ * are {@link Coder#consistentWithEquals()} and cannot encode {@code null}
(i.e. not {@link
+ * NullableCoder}) can store raw keys directly, preserving savepoint
compatibility.
+ */
+ private static boolean usesStructuralKey(Coder<?> coder) {
+ return !coder.consistentWithEquals() || coder instanceof NullableCoder;
+ }
+
+ private static <KeyT, ValueT> MapStateDescriptor<Object, ValueT>
createMapStateDescriptor(
+ String stateId,
+ Coder<KeyT> keyCoder,
+ TypeSerializer<ValueT> valueSerializer,
+ boolean fasterCopy) {
+ Coder<?> stateKeyCoder =
+ usesStructuralKey(keyCoder) ? new StructuralKeyCoder<>(keyCoder) :
keyCoder;
+ @SuppressWarnings("unchecked")
+ TypeSerializer<Object> keySerializer =
+ (TypeSerializer<Object>) new CoderTypeSerializer<>(stateKeyCoder,
fasterCopy);
+ return new MapStateDescriptor<>(stateId, keySerializer, valueSerializer);
+ }
+
+ /**
+ * Wraps a key together with its {@link Coder#structuralValue} so Flink's
in-memory {@code
+ * MapState} implementations compare keys by encoded equality (and accept
{@code null} keys) while
+ * still retaining the original {@code KeyT} for {@code keys()} and {@code
entries()}.
+ */
+ private static final class StructuralKey<K> {
+ private final @Nullable K key;
+ private final Object structuralValue;
+
+ StructuralKey(Coder<K> coder, @Nullable K key) {
+ this.key = key;
+ this.structuralValue = coder.structuralValue(key);
+ }
+
+ @Nullable K key() {
+ return key;
+ }
+
+ @Override
+ public boolean equals(@Nullable Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (!(o instanceof StructuralKey)) {
+ return false;
+ }
+ StructuralKey<?> that = (StructuralKey<?>) o;
+ return structuralValue.equals(that.structuralValue);
+ }
+
+ @Override
+ public int hashCode() {
+ return structuralValue.hashCode();
+ }
+ }
+
+ private static final class StructuralKeyCoder<K> extends
StructuredCoder<StructuralKey<K>> {
Review Comment:
Since `UnversionedTypeSerializerSnapshot` Java-serializes
`CoderTypeSerializer` (and therefore `StructuralKeyCoder`) into savepoints,
it's probably worth adding an explicit `private static final long
serialVersionUID = 1L;` here so future edits to the class don't accidentally
break deserialization. (Same in `2.0`.)
##########
runners/flink/src/main/java/org/apache/beam/runners/flink/translation/wrappers/streaming/state/FlinkStateInternals.java:
##########
@@ -1309,11 +1315,126 @@ public int hashCode() {
}
}
+ /**
+ * Whether keys of {@code coder} must be wrapped in {@link StructuralKey}
before being stored in a
+ * Flink {@link org.apache.flink.api.common.state.MapState}.
+ *
+ * <p>Heap and batch keyed state backends back {@code MapState} with a Java
{@link Map}, which
+ * compares keys by {@link Object#equals} and rejects {@code null} keys on
RocksDB. Coders that
+ * are {@link Coder#consistentWithEquals()} and cannot encode {@code null}
(i.e. not {@link
+ * NullableCoder}) can store raw keys directly, preserving savepoint
compatibility.
+ */
+ private static boolean usesStructuralKey(Coder<?> coder) {
+ return !coder.consistentWithEquals() || coder instanceof NullableCoder;
+ }
+
+ private static <KeyT, ValueT> MapStateDescriptor<Object, ValueT>
createMapStateDescriptor(
+ String stateId,
+ Coder<KeyT> keyCoder,
+ TypeSerializer<ValueT> valueSerializer,
+ boolean fasterCopy) {
+ Coder<?> stateKeyCoder =
+ usesStructuralKey(keyCoder) ? new StructuralKeyCoder<>(keyCoder) :
keyCoder;
+ @SuppressWarnings("unchecked")
+ TypeSerializer<Object> keySerializer =
+ (TypeSerializer<Object>) new CoderTypeSerializer<>(stateKeyCoder,
fasterCopy);
+ return new MapStateDescriptor<>(stateId, keySerializer, valueSerializer);
+ }
+
+ /**
+ * Wraps a key together with its {@link Coder#structuralValue} so Flink's
in-memory {@code
+ * MapState} implementations compare keys by encoded equality (and accept
{@code null} keys) while
+ * still retaining the original {@code KeyT} for {@code keys()} and {@code
entries()}.
+ */
+ private static final class StructuralKey<K> {
+ private final @Nullable K key;
+ private final Object structuralValue;
+
+ StructuralKey(Coder<K> coder, @Nullable K key) {
+ this.key = key;
+ this.structuralValue = coder.structuralValue(key);
+ }
+
+ @Nullable K key() {
+ return key;
+ }
+
+ @Override
+ public boolean equals(@Nullable Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (!(o instanceof StructuralKey)) {
+ return false;
+ }
+ StructuralKey<?> that = (StructuralKey<?>) o;
+ return structuralValue.equals(that.structuralValue);
+ }
+
+ @Override
+ public int hashCode() {
+ return structuralValue.hashCode();
+ }
+ }
+
+ private static final class StructuralKeyCoder<K> extends
StructuredCoder<StructuralKey<K>> {
+ private final Coder<K> keyCoder;
+
+ StructuralKeyCoder(Coder<K> keyCoder) {
+ this.keyCoder = keyCoder;
+ }
+
+ @Override
+ public void encode(StructuralKey<K> value, OutputStream outStream)
+ throws CoderException, IOException {
+ keyCoder.encode(value.key(), outStream);
+ }
+
+ @Override
+ public void encode(StructuralKey<K> value, OutputStream outStream, Context
context)
Review Comment:
`Coder#encode(..., Context)` and `decode(..., Context)` are `@Deprecated`,
and the base `Coder` already delegates them to the 2-arg versions (which are
also all `CoderTypeSerializer` calls). You can probably drop these two
overrides so they don't trigger deprecation warnings. (Same in `2.0`.)
##########
runners/flink/src/main/java/org/apache/beam/runners/flink/translation/wrappers/streaming/state/FlinkStateInternals.java:
##########
@@ -1309,11 +1315,126 @@ public int hashCode() {
}
}
+ /**
+ * Whether keys of {@code coder} must be wrapped in {@link StructuralKey}
before being stored in a
+ * Flink {@link org.apache.flink.api.common.state.MapState}.
+ *
+ * <p>Heap and batch keyed state backends back {@code MapState} with a Java
{@link Map}, which
+ * compares keys by {@link Object#equals} and rejects {@code null} keys on
RocksDB. Coders that
+ * are {@link Coder#consistentWithEquals()} and cannot encode {@code null}
(i.e. not {@link
+ * NullableCoder}) can store raw keys directly, preserving savepoint
compatibility.
+ */
+ private static boolean usesStructuralKey(Coder<?> coder) {
+ return !coder.consistentWithEquals() || coder instanceof NullableCoder;
+ }
+
+ private static <KeyT, ValueT> MapStateDescriptor<Object, ValueT>
createMapStateDescriptor(
+ String stateId,
+ Coder<KeyT> keyCoder,
+ TypeSerializer<ValueT> valueSerializer,
+ boolean fasterCopy) {
+ Coder<?> stateKeyCoder =
+ usesStructuralKey(keyCoder) ? new StructuralKeyCoder<>(keyCoder) :
keyCoder;
+ @SuppressWarnings("unchecked")
+ TypeSerializer<Object> keySerializer =
+ (TypeSerializer<Object>) new CoderTypeSerializer<>(stateKeyCoder,
fasterCopy);
+ return new MapStateDescriptor<>(stateId, keySerializer, valueSerializer);
+ }
+
+ /**
+ * Wraps a key together with its {@link Coder#structuralValue} so Flink's
in-memory {@code
+ * MapState} implementations compare keys by encoded equality (and accept
{@code null} keys) while
+ * still retaining the original {@code KeyT} for {@code keys()} and {@code
entries()}.
+ */
+ private static final class StructuralKey<K> {
+ private final @Nullable K key;
+ private final Object structuralValue;
+
+ StructuralKey(Coder<K> coder, @Nullable K key) {
+ this.key = key;
+ this.structuralValue = coder.structuralValue(key);
+ }
+
+ @Nullable K key() {
+ return key;
+ }
+
+ @Override
+ public boolean equals(@Nullable Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (!(o instanceof StructuralKey)) {
+ return false;
+ }
+ StructuralKey<?> that = (StructuralKey<?>) o;
+ return structuralValue.equals(that.structuralValue);
Review Comment:
Small defensive nit: `Objects.equals(structuralValue, that.structuralValue)`
here (and `Objects.hashCode(structuralValue)` below) would avoid an NPE if a
custom coder ever violates the `structuralValue` contract and returns `null`.
(Same in `2.0`.)
##########
runners/flink/src/main/java/org/apache/beam/runners/flink/translation/wrappers/streaming/state/FlinkStateInternals.java:
##########
@@ -1309,11 +1315,126 @@ public int hashCode() {
}
}
+ /**
+ * Whether keys of {@code coder} must be wrapped in {@link StructuralKey}
before being stored in a
+ * Flink {@link org.apache.flink.api.common.state.MapState}.
+ *
+ * <p>Heap and batch keyed state backends back {@code MapState} with a Java
{@link Map}, which
+ * compares keys by {@link Object#equals} and rejects {@code null} keys on
RocksDB. Coders that
Review Comment:
Nit: Flink's heap `CopyOnWriteStateMap` rejects `null` keys too, not just
RocksDB. (Same in the `2.0` copy.)
--
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]