claudevdm commented on code in PR #39933: URL: https://github.com/apache/beam/pull/39933#discussion_r3915944360
########## sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/ReadFooterSchema.java: ########## @@ -0,0 +1,181 @@ +/* + * 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.sdk.io.iceberg; + +import static org.apache.beam.sdk.metrics.Metrics.counter; +import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull; + +import java.util.Collections; +import java.util.concurrent.Callable; +import org.apache.beam.sdk.metrics.Counter; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.windowing.BoundedWindow; +import org.apache.beam.sdk.transforms.windowing.PaneInfo; +import org.apache.iceberg.FileFormat; +import org.apache.parquet.hadoop.metadata.ParquetMetadata; +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.joda.time.Instant; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Emits the canonical schema (see {@link FileSchemas}) of every readable Parquet file as JSON. + * Unreadable or non-Parquet files contribute nothing. + */ +class ReadFooterSchema extends DoFn<String, String> { + private static final Logger LOG = LoggerFactory.getLogger(ReadFooterSchema.class); + + static final int DEFAULT_THREAD_POOL_SIZE = 10; + static final int DEFAULT_MAX_IN_FLIGHT_TASKS = 100; + static final String FILES_READ_COUNTER = "numFilesRead"; + static final String SCHEMAS_EMITTED_COUNTER = "numSchemasEmitted"; + static final String FOOTER_READ_ERRORS_COUNTER = "numFooterReadErrors"; + private static final Counter numFilesRead = counter(ReadFooterSchema.class, FILES_READ_COUNTER); + private static final Counter numSchemasEmitted = + counter(ReadFooterSchema.class, SCHEMAS_EMITTED_COUNTER); + private static final Counter numFooterReadErrors = + counter(ReadFooterSchema.class, FOOTER_READ_ERRORS_COUNTER); + + private final int threadPoolSize; + private final int maxInFlightTasks; + private transient @MonotonicNonNull BoundedAsyncTasks<ReadResult> tasks; + + ReadFooterSchema() { + this(DEFAULT_THREAD_POOL_SIZE, DEFAULT_MAX_IN_FLIGHT_TASKS); + } + + ReadFooterSchema(int threadPoolSize, int maxInFlightTasks) { + this.threadPoolSize = threadPoolSize; + this.maxInFlightTasks = maxInFlightTasks; + } + + /** + * {@code schemaJson} is null when the file contributes no schema. Counters are updated when the + * result is delivered, on the processing thread: metrics touched from the executor are lost. + */ + private static class ReadResult { + final @Nullable String schemaJson; + final boolean footerError; + final Instant timestamp; + final BoundedWindow window; + final PaneInfo paneInfo; + + ReadResult( + @Nullable String schemaJson, + boolean footerError, + Instant timestamp, + BoundedWindow window, + PaneInfo paneInfo) { + this.schemaJson = schemaJson; + this.footerError = footerError; + this.timestamp = timestamp; + this.window = window; + this.paneInfo = paneInfo; + } + } + + @Setup + public void setup() { + tasks = new BoundedAsyncTasks<>(threadPoolSize, maxInFlightTasks); + } + + /** Clears anything left behind if the runner reuses this instance after a failed bundle. */ + @StartBundle + public void startBundle() { + checkStateNotNull(tasks).cancelAll(); + } + + @Teardown + public void teardown() { + if (tasks != null) { + tasks.shutdown(); + } + } + + @ProcessElement + public void process( + @Element String filePath, + @Timestamp Instant timestamp, + BoundedWindow window, + PaneInfo paneInfo, + OutputReceiver<String> output) + throws Exception { + numFilesRead.inc(); Review Comment: Not thread safe. -- 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]
