chamikaramj commented on code in PR #30797: URL: https://github.com/apache/beam/pull/30797#discussion_r1544853193
########## sdks/java/io/iceberg/src/main/java/org/apache/beam/io/iceberg/DynamicDestinations.java: ########## @@ -0,0 +1,163 @@ +/* + * 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.io.iceberg; + +import static org.apache.beam.sdk.values.TypeDescriptors.extractFromTypeParameters; +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkState; + +import java.io.Serializable; +import java.util.List; +import org.apache.beam.sdk.coders.CannotProvideCoderException; +import org.apache.beam.sdk.coders.Coder; +import org.apache.beam.sdk.coders.CoderRegistry; +import org.apache.beam.sdk.options.PipelineOptions; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.values.PCollectionView; +import org.apache.beam.sdk.values.TypeDescriptor; +import org.apache.beam.sdk.values.TypeDescriptors; +import org.apache.beam.sdk.values.ValueInSingleWindow; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Lists; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.Table; +import org.checkerframework.checker.nullness.qual.Nullable; + +public abstract class DynamicDestinations<T, DestinationT> implements Serializable { Review Comment: Seems like this has a lot of copied over logic from BQ dynamic destinations which probably we can simplify/change if we went with the new DLQ framework. https://github.com/apache/beam/blob/master/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/DynamicDestinations.java ########## sdks/java/io/iceberg/src/main/java/org/apache/beam/io/iceberg/WriteToDestinations.java: ########## @@ -0,0 +1,242 @@ +/* + * 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.io.iceberg; + +import java.util.Collections; +import java.util.UUID; +import org.apache.beam.io.iceberg.WriteBundlesToFiles.Result; +import org.apache.beam.sdk.Pipeline; +import org.apache.beam.sdk.coders.Coder; +import org.apache.beam.sdk.coders.IterableCoder; +import org.apache.beam.sdk.coders.KvCoder; +import org.apache.beam.sdk.coders.SerializableCoder; +import org.apache.beam.sdk.coders.ShardedKeyCoder; +import org.apache.beam.sdk.coders.StringUtf8Coder; +import org.apache.beam.sdk.transforms.Create; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.Flatten; +import org.apache.beam.sdk.transforms.GroupByKey; +import org.apache.beam.sdk.transforms.MapElements; +import org.apache.beam.sdk.transforms.PTransform; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.transforms.SimpleFunction; +import org.apache.beam.sdk.transforms.View; +import org.apache.beam.sdk.transforms.windowing.BoundedWindow; +import org.apache.beam.sdk.values.KV; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.PCollectionList; +import org.apache.beam.sdk.values.PCollectionTuple; +import org.apache.beam.sdk.values.PCollectionView; +import org.apache.beam.sdk.values.ShardedKey; +import org.apache.beam.sdk.values.TupleTag; +import org.apache.beam.sdk.values.TupleTagList; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList; +import org.apache.iceberg.AppendFiles; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.Snapshot; +import org.apache.iceberg.Table; +import org.apache.iceberg.catalog.TableIdentifier; + +class WriteToDestinations<DestinationT, ElementT> + extends PTransform< + PCollection<KV<DestinationT, ElementT>>, IcebergWriteResult<DestinationT, ElementT>> { + + @VisibleForTesting static final int DEFAULT_MAX_WRITERS_PER_BUNDLE = 20; + @VisibleForTesting static final int DEFAULT_MAX_FILES_PER_PARTITION = 10_000; + @VisibleForTesting static final long DEFAULT_MAX_BYTES_PER_PARTITION = 10L * (1L << 40); // 10TB + static final long DEFAULT_MAX_BYTES_PER_FILE = (1L << 40); // 1TB + static final int DEFAULT_NUM_FILE_SHARDS = 0; + static final int FILE_TRIGGERING_RECORD_COUNT = 50_000; + + private final Coder<DestinationT> destinationCoder; + + private final RecordWriterFactory<ElementT, DestinationT> recordWriterFactory; + private final TableFactory<String> tableFactory; + + WriteToDestinations( + Coder<DestinationT> destinationCoder, + RecordWriterFactory<ElementT, DestinationT> recordWriterFactory, + TableFactory<String> tableFactory) { + this.destinationCoder = destinationCoder; + this.recordWriterFactory = recordWriterFactory; + this.tableFactory = tableFactory; + } + + private PCollectionView<String> createJobIdPrefixView(Pipeline p) { + + final String jobName = p.getOptions().getJobName(); + + return p.apply("JobIdCreationRoot_", Create.of((Void) null)) + .apply( + "CreateJobId", + ParDo.of( + new DoFn<Void, String>() { + @ProcessElement + public void process(ProcessContext c) { + c.output(jobName + "-" + UUID.randomUUID().toString()); + } + })) + .apply("JobIdSideInput", View.asSingleton()); + } + + @Override + public IcebergWriteResult<DestinationT, ElementT> expand( + PCollection<KV<DestinationT, ElementT>> input) { + + final PCollectionView<String> fileView = createJobIdPrefixView(input.getPipeline()); + // We always do the equivalent of a dynamically sharded file creation + TupleTag<Result<DestinationT>> writtenFilesTag = new TupleTag<>("writtenFiles"); + TupleTag<KV<ShardedKey<DestinationT>, ElementT>> successfulWritesTag = + new TupleTag<>("successfulWrites"); + TupleTag<KV<ShardedKey<DestinationT>, ElementT>> failedWritesTag = + new TupleTag<>("failedWrites"); + TupleTag<KV<TableIdentifier, Snapshot>> snapshotsTag = new TupleTag<>("snapshots"); + + final Coder<ElementT> elementCoder = + ((KvCoder<DestinationT, ElementT>) input.getCoder()).getValueCoder(); + + // Write everything to files + PCollectionTuple writeBundlesToFiles = + input.apply( + "Write Bundles To Files", + ParDo.of( + new WriteBundlesToFiles<>( + fileView, + successfulWritesTag, + failedWritesTag, + DEFAULT_MAX_WRITERS_PER_BUNDLE, + DEFAULT_MAX_BYTES_PER_FILE, + recordWriterFactory)) + .withSideInputs(fileView) + .withOutputTags( + writtenFilesTag, + TupleTagList.of(ImmutableList.of(successfulWritesTag, failedWritesTag)))); + + PCollection<KV<ShardedKey<DestinationT>, ElementT>> successfulWrites = + writeBundlesToFiles + .get(successfulWritesTag) + .setCoder(KvCoder.of(ShardedKeyCoder.of(destinationCoder), elementCoder)); + + PCollection<KV<ShardedKey<DestinationT>, ElementT>> failedWrites = + writeBundlesToFiles + .get(failedWritesTag) + .setCoder(KvCoder.of(ShardedKeyCoder.of(destinationCoder), elementCoder)); + + PCollection<Result<DestinationT>> writtenFilesGrouped = + failedWrites + .apply("Group By Destination", GroupByKey.create()) + .apply( + "Strip Shard ID", + MapElements.via( + new SimpleFunction< + KV<ShardedKey<DestinationT>, Iterable<ElementT>>, + KV<DestinationT, Iterable<ElementT>>>() { + @Override + public KV<DestinationT, Iterable<ElementT>> apply( + KV<ShardedKey<DestinationT>, Iterable<ElementT>> input11) { + return KV.of(input11.getKey().getKey(), input11.getValue()); + } + })) + .setCoder(KvCoder.of(destinationCoder, IterableCoder.of(elementCoder))) + .apply( + "Write Grouped Records", + ParDo.of( + new WriteBundlesToFiles.WriteGroupedRecordsToFiles<>( + fileView, DEFAULT_MAX_BYTES_PER_FILE, recordWriterFactory))) + .setCoder(WriteBundlesToFiles.ResultCoder.of(destinationCoder)); + + PCollection<Result<DestinationT>> catalogUpdates = + PCollectionList.of( + writeBundlesToFiles + .get(writtenFilesTag) + .setCoder(WriteBundlesToFiles.ResultCoder.of(destinationCoder))) + .and(writtenFilesGrouped) + .apply("Flatten Files", Flatten.pCollections()) + .setCoder(WriteBundlesToFiles.ResultCoder.of(destinationCoder)); + + // Apply any sharded writes and flatten everything for catalog updates + PCollection<KV<String, Snapshot>> snapshots = + catalogUpdates + .apply( + "Extract Data File", + ParDo.of( + new DoFn<Result<DestinationT>, KV<String, MetadataUpdate>>() { + @ProcessElement + public void processElement( + ProcessContext c, @Element Result<DestinationT> element) { + c.output( + KV.of( + element.tableId, + new MetadataUpdate( + element.partitionSpec.partitionType(), + element.update.getDataFiles(), + Collections.emptyList()))); + } + })) + .setCoder(KvCoder.of(StringUtf8Coder.of(), MetadataUpdate.coder())) + .apply(GroupByKey.create()) + .apply("Write Metadata Updates", ParDo.of(new MetadataUpdates<>(tableFactory))) Review Comment: Probably this should be followed up by another GBK and a cleanup step that deletes temp files (of this step and any failed work items). ########## sdks/java/io/iceberg/src/main/java/org/apache/beam/io/iceberg/IcebergCatalog.java: ########## @@ -0,0 +1,237 @@ +/* + * 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.io.iceberg; + +import com.google.auto.value.AutoValue; +import java.io.Serializable; +import javax.annotation.Nullable; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap; +import org.apache.hadoop.conf.Configuration; +import org.apache.iceberg.CatalogProperties; +import org.apache.iceberg.CatalogUtil; +import org.checkerframework.dataflow.qual.Pure; + +@AutoValue +public abstract class IcebergCatalog implements Serializable { + + @Pure + public abstract String getName(); + + /* Core Properties */ + @Pure + public abstract @Nullable String getIcebergCatalogType(); + + @Pure + public abstract @Nullable String getCatalogImplementation(); + + @Pure + public abstract @Nullable String getFileIOImplementation(); + + @Pure + public abstract @Nullable String getWarehouseLocation(); + + @Pure + public abstract @Nullable String getMetricsReporterImplementation(); + + /* Caching */ + @Pure + public abstract boolean getCacheEnabled(); + + @Pure + public abstract boolean getCacheCaseSensitive(); + + @Pure + public abstract long getCacheExpirationIntervalMillis(); + + @Pure + public abstract boolean getIOManifestCacheEnabled(); + + @Pure + public abstract long getIOManifestCacheExpirationIntervalMillis(); + + @Pure + public abstract long getIOManifestCacheMaxTotalBytes(); + + @Pure + public abstract long getIOManifestCacheMaxContentLength(); + + @Pure + public abstract @Nullable String getUri(); + + @Pure + public abstract int getClientPoolSize(); + + @Pure + public abstract long getClientPoolEvictionIntervalMs(); + + @Pure + public abstract @Nullable String getClientPoolCacheKeys(); + + @Pure + public abstract @Nullable String getLockImplementation(); + + @Pure + public abstract long getLockHeartbeatIntervalMillis(); + + @Pure + public abstract long getLockHeartbeatTimeoutMillis(); + + @Pure + public abstract int getLockHeartbeatThreads(); + + @Pure + public abstract long getLockAcquireIntervalMillis(); + + @Pure + public abstract long getLockAcquireTimeoutMillis(); + + @Pure + public abstract @Nullable String getAppIdentifier(); + + @Pure + public abstract @Nullable String getUser(); + + @Pure + public abstract long getAuthSessionTimeoutMillis(); + + @Pure + public abstract @Nullable Configuration getConfiguration(); Review Comment: Seems like `org.apache.hadoop.conf.Configuration` is a set of string key value pairs. https://hadoop.apache.org/docs/current/api/org/apache/hadoop/conf/Configuration.html May be we should just accept a `org.apache.hadoop.conf.Configuration` and build the Hadoop Configuration to make this more portability friendly. ########## sdks/java/io/iceberg/src/main/java/org/apache/beam/io/iceberg/WriteToDestinations.java: ########## @@ -0,0 +1,242 @@ +/* + * 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.io.iceberg; + +import java.util.Collections; +import java.util.UUID; +import org.apache.beam.io.iceberg.WriteBundlesToFiles.Result; +import org.apache.beam.sdk.Pipeline; +import org.apache.beam.sdk.coders.Coder; +import org.apache.beam.sdk.coders.IterableCoder; +import org.apache.beam.sdk.coders.KvCoder; +import org.apache.beam.sdk.coders.SerializableCoder; +import org.apache.beam.sdk.coders.ShardedKeyCoder; +import org.apache.beam.sdk.coders.StringUtf8Coder; +import org.apache.beam.sdk.transforms.Create; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.Flatten; +import org.apache.beam.sdk.transforms.GroupByKey; +import org.apache.beam.sdk.transforms.MapElements; +import org.apache.beam.sdk.transforms.PTransform; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.transforms.SimpleFunction; +import org.apache.beam.sdk.transforms.View; +import org.apache.beam.sdk.transforms.windowing.BoundedWindow; +import org.apache.beam.sdk.values.KV; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.PCollectionList; +import org.apache.beam.sdk.values.PCollectionTuple; +import org.apache.beam.sdk.values.PCollectionView; +import org.apache.beam.sdk.values.ShardedKey; +import org.apache.beam.sdk.values.TupleTag; +import org.apache.beam.sdk.values.TupleTagList; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList; +import org.apache.iceberg.AppendFiles; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.Snapshot; +import org.apache.iceberg.Table; +import org.apache.iceberg.catalog.TableIdentifier; + +class WriteToDestinations<DestinationT, ElementT> + extends PTransform< + PCollection<KV<DestinationT, ElementT>>, IcebergWriteResult<DestinationT, ElementT>> { + + @VisibleForTesting static final int DEFAULT_MAX_WRITERS_PER_BUNDLE = 20; + @VisibleForTesting static final int DEFAULT_MAX_FILES_PER_PARTITION = 10_000; + @VisibleForTesting static final long DEFAULT_MAX_BYTES_PER_PARTITION = 10L * (1L << 40); // 10TB + static final long DEFAULT_MAX_BYTES_PER_FILE = (1L << 40); // 1TB + static final int DEFAULT_NUM_FILE_SHARDS = 0; + static final int FILE_TRIGGERING_RECORD_COUNT = 50_000; + + private final Coder<DestinationT> destinationCoder; + + private final RecordWriterFactory<ElementT, DestinationT> recordWriterFactory; + private final TableFactory<String> tableFactory; + + WriteToDestinations( + Coder<DestinationT> destinationCoder, + RecordWriterFactory<ElementT, DestinationT> recordWriterFactory, + TableFactory<String> tableFactory) { + this.destinationCoder = destinationCoder; + this.recordWriterFactory = recordWriterFactory; + this.tableFactory = tableFactory; + } + + private PCollectionView<String> createJobIdPrefixView(Pipeline p) { + + final String jobName = p.getOptions().getJobName(); + + return p.apply("JobIdCreationRoot_", Create.of((Void) null)) + .apply( + "CreateJobId", + ParDo.of( + new DoFn<Void, String>() { + @ProcessElement + public void process(ProcessContext c) { + c.output(jobName + "-" + UUID.randomUUID().toString()); + } + })) + .apply("JobIdSideInput", View.asSingleton()); + } + + @Override + public IcebergWriteResult<DestinationT, ElementT> expand( + PCollection<KV<DestinationT, ElementT>> input) { + + final PCollectionView<String> fileView = createJobIdPrefixView(input.getPipeline()); + // We always do the equivalent of a dynamically sharded file creation + TupleTag<Result<DestinationT>> writtenFilesTag = new TupleTag<>("writtenFiles"); + TupleTag<KV<ShardedKey<DestinationT>, ElementT>> successfulWritesTag = + new TupleTag<>("successfulWrites"); + TupleTag<KV<ShardedKey<DestinationT>, ElementT>> failedWritesTag = + new TupleTag<>("failedWrites"); + TupleTag<KV<TableIdentifier, Snapshot>> snapshotsTag = new TupleTag<>("snapshots"); + + final Coder<ElementT> elementCoder = + ((KvCoder<DestinationT, ElementT>) input.getCoder()).getValueCoder(); + + // Write everything to files + PCollectionTuple writeBundlesToFiles = + input.apply( + "Write Bundles To Files", + ParDo.of( + new WriteBundlesToFiles<>( + fileView, + successfulWritesTag, + failedWritesTag, + DEFAULT_MAX_WRITERS_PER_BUNDLE, + DEFAULT_MAX_BYTES_PER_FILE, + recordWriterFactory)) + .withSideInputs(fileView) + .withOutputTags( + writtenFilesTag, + TupleTagList.of(ImmutableList.of(successfulWritesTag, failedWritesTag)))); + + PCollection<KV<ShardedKey<DestinationT>, ElementT>> successfulWrites = + writeBundlesToFiles + .get(successfulWritesTag) + .setCoder(KvCoder.of(ShardedKeyCoder.of(destinationCoder), elementCoder)); + + PCollection<KV<ShardedKey<DestinationT>, ElementT>> failedWrites = + writeBundlesToFiles + .get(failedWritesTag) + .setCoder(KvCoder.of(ShardedKeyCoder.of(destinationCoder), elementCoder)); + + PCollection<Result<DestinationT>> writtenFilesGrouped = + failedWrites Review Comment: Re-reading the code, seems like failedWrites here are actually due to previous WriteBundlesToFiles exceeding any of the limits provided to the transform (DEFAULT_MAX_WRITERS_PER_BUNDLE, DEFAULT_MAX_BYTES_PER_FILE). We group known set of spilled over records and write in the subsequent transform which makes sense. We should probably change 'failedWrites' to 'spilledOverWrites'. -- 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]
