pvary commented on code in PR #13277:
URL: https://github.com/apache/iceberg/pull/13277#discussion_r2137223023


##########
flink/v2.0/flink/src/main/java/org/apache/iceberg/flink/sink/dynamic/HashKeyGenerator.java:
##########
@@ -0,0 +1,371 @@
+/*
+ * 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.iceberg.flink.sink.dynamic;
+
+import static org.apache.iceberg.TableProperties.WRITE_DISTRIBUTION_MODE;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+import javax.annotation.Nullable;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.java.functions.KeySelector;
+import org.apache.flink.runtime.state.KeyGroupRangeAssignment;
+import org.apache.flink.table.data.RowData;
+import org.apache.iceberg.DistributionMode;
+import org.apache.iceberg.PartitionField;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.flink.FlinkSchemaUtil;
+import org.apache.iceberg.flink.sink.EqualityFieldKeySelector;
+import org.apache.iceberg.flink.sink.NonThrowingKeySelector;
+import org.apache.iceberg.flink.sink.PartitionKeySelector;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The HashKeyGenerator is responsible for creating the appropriate hash key 
for Flink's keyBy
+ * operation. The hash key is generated depending on the user-provided 
DynamicRecord and the table
+ * metadata. Under the hood, we maintain a set of Flink {@link KeySelector}s 
which implement the
+ * appropriate Iceberg {@link DistributionMode}. For every table, we randomly 
select a consistent
+ * subset of writer subtasks which receive data via their associated keys, 
depending on the chosen
+ * DistributionMode.
+ *
+ * <p>Caching ensures that a new key selector is also created when the table 
metadata (e.g. schema,
+ * spec) or the user-provided metadata changes (e.g. distribution mode, write 
parallelism).
+ */
+class HashKeyGenerator {
+  private static final Logger LOG = 
LoggerFactory.getLogger(HashKeyGenerator.class);
+
+  private final int maxWriteParallelism;
+  private final Cache<SelectorKey, NonThrowingKeySelector<RowData, Integer>> 
keySelectorCache;
+
+  HashKeyGenerator(int maxCacheSize, int maxWriteParallelism) {
+    this.maxWriteParallelism = maxWriteParallelism;
+    this.keySelectorCache = 
Caffeine.newBuilder().maximumSize(maxCacheSize).build();
+  }
+
+  Integer generateKey(DynamicRecord dynamicRecord) {
+    return generateKey(dynamicRecord, null, null, null);
+  }
+
+  Integer generateKey(
+      DynamicRecord dynamicRecord,
+      @Nullable Schema tableSchema,
+      @Nullable PartitionSpec tableSpec,
+      @Nullable RowData overrideRowData) {
+    String tableIdent = dynamicRecord.tableIdentifier().toString();
+    SelectorKey cacheKey =
+        new SelectorKey(
+            tableIdent,
+            dynamicRecord.branch(),
+            tableSchema != null ? tableSchema.schemaId() : null,
+            tableSpec != null ? tableSpec.specId() : null,
+            dynamicRecord.schema(),
+            dynamicRecord.spec(),
+            dynamicRecord.equalityFields());
+    return keySelectorCache
+        .get(
+            cacheKey,
+            k ->
+                getKeySelector(
+                    tableIdent,
+                    MoreObjects.firstNonNull(tableSchema, 
dynamicRecord.schema()),
+                    MoreObjects.firstNonNull(tableSpec, dynamicRecord.spec()),
+                    MoreObjects.firstNonNull(
+                        dynamicRecord.distributionMode(), 
DistributionMode.NONE),
+                    MoreObjects.firstNonNull(
+                        dynamicRecord.equalityFields(), 
Collections.emptyList()),
+                    dynamicRecord.writeParallelism()))
+        .getKey(overrideRowData != null ? overrideRowData : 
dynamicRecord.rowData());
+  }
+
+  private NonThrowingKeySelector<RowData, Integer> getKeySelector(
+      String tableName,
+      Schema schema,
+      PartitionSpec spec,
+      DistributionMode mode,
+      List<String> equalityFields,
+      int writeParallelism) {
+    LOG.info("Write distribution mode is '{}'", mode.modeName());

Review Comment:
   This log line is not too informative when we have many tables we are writing 
to.
   Maybe add more context to the log



##########
flink/v2.0/flink/src/main/java/org/apache/iceberg/flink/sink/dynamic/HashKeyGenerator.java:
##########
@@ -0,0 +1,371 @@
+/*
+ * 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.iceberg.flink.sink.dynamic;
+
+import static org.apache.iceberg.TableProperties.WRITE_DISTRIBUTION_MODE;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+import javax.annotation.Nullable;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.java.functions.KeySelector;
+import org.apache.flink.runtime.state.KeyGroupRangeAssignment;
+import org.apache.flink.table.data.RowData;
+import org.apache.iceberg.DistributionMode;
+import org.apache.iceberg.PartitionField;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.flink.FlinkSchemaUtil;
+import org.apache.iceberg.flink.sink.EqualityFieldKeySelector;
+import org.apache.iceberg.flink.sink.NonThrowingKeySelector;
+import org.apache.iceberg.flink.sink.PartitionKeySelector;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The HashKeyGenerator is responsible for creating the appropriate hash key 
for Flink's keyBy
+ * operation. The hash key is generated depending on the user-provided 
DynamicRecord and the table
+ * metadata. Under the hood, we maintain a set of Flink {@link KeySelector}s 
which implement the
+ * appropriate Iceberg {@link DistributionMode}. For every table, we randomly 
select a consistent
+ * subset of writer subtasks which receive data via their associated keys, 
depending on the chosen
+ * DistributionMode.
+ *
+ * <p>Caching ensures that a new key selector is also created when the table 
metadata (e.g. schema,
+ * spec) or the user-provided metadata changes (e.g. distribution mode, write 
parallelism).
+ */
+class HashKeyGenerator {
+  private static final Logger LOG = 
LoggerFactory.getLogger(HashKeyGenerator.class);
+
+  private final int maxWriteParallelism;
+  private final Cache<SelectorKey, NonThrowingKeySelector<RowData, Integer>> 
keySelectorCache;
+
+  HashKeyGenerator(int maxCacheSize, int maxWriteParallelism) {
+    this.maxWriteParallelism = maxWriteParallelism;
+    this.keySelectorCache = 
Caffeine.newBuilder().maximumSize(maxCacheSize).build();
+  }
+
+  Integer generateKey(DynamicRecord dynamicRecord) {
+    return generateKey(dynamicRecord, null, null, null);
+  }
+
+  Integer generateKey(
+      DynamicRecord dynamicRecord,
+      @Nullable Schema tableSchema,
+      @Nullable PartitionSpec tableSpec,
+      @Nullable RowData overrideRowData) {
+    String tableIdent = dynamicRecord.tableIdentifier().toString();
+    SelectorKey cacheKey =
+        new SelectorKey(
+            tableIdent,
+            dynamicRecord.branch(),
+            tableSchema != null ? tableSchema.schemaId() : null,
+            tableSpec != null ? tableSpec.specId() : null,
+            dynamicRecord.schema(),
+            dynamicRecord.spec(),
+            dynamicRecord.equalityFields());
+    return keySelectorCache
+        .get(
+            cacheKey,
+            k ->
+                getKeySelector(
+                    tableIdent,
+                    MoreObjects.firstNonNull(tableSchema, 
dynamicRecord.schema()),
+                    MoreObjects.firstNonNull(tableSpec, dynamicRecord.spec()),
+                    MoreObjects.firstNonNull(
+                        dynamicRecord.distributionMode(), 
DistributionMode.NONE),
+                    MoreObjects.firstNonNull(
+                        dynamicRecord.equalityFields(), 
Collections.emptyList()),
+                    dynamicRecord.writeParallelism()))
+        .getKey(overrideRowData != null ? overrideRowData : 
dynamicRecord.rowData());
+  }
+
+  private NonThrowingKeySelector<RowData, Integer> getKeySelector(
+      String tableName,
+      Schema schema,
+      PartitionSpec spec,
+      DistributionMode mode,
+      List<String> equalityFields,
+      int writeParallelism) {
+    LOG.info("Write distribution mode is '{}'", mode.modeName());
+    switch (mode) {
+      case NONE:
+        if (equalityFields.isEmpty()) {
+          return tableKeySelector(tableName, writeParallelism, 
maxWriteParallelism);
+        } else {
+          LOG.info("Distribute rows by equality fields, because there are 
equality fields set");

Review Comment:
   More context to the logs please



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to