stankiewicz commented on code in PR #38854:
URL: https://github.com/apache/beam/pull/38854#discussion_r3441321104


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BufferMismatchedRows.java:
##########
@@ -0,0 +1,280 @@
+/*
+ * 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.gcp.bigquery;
+
+import com.google.api.services.bigquery.model.TableRow;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.stream.StreamSupport;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.metrics.Counter;
+import org.apache.beam.sdk.metrics.Metrics;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.state.BagState;
+import org.apache.beam.sdk.state.StateSpec;
+import org.apache.beam.sdk.state.StateSpecs;
+import org.apache.beam.sdk.state.TimeDomain;
+import org.apache.beam.sdk.state.Timer;
+import org.apache.beam.sdk.state.TimerSpec;
+import org.apache.beam.sdk.state.TimerSpecs;
+import org.apache.beam.sdk.state.ValueState;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.util.ShardedKey;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PCollectionTuple;
+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.collect.Iterables;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Lists;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Duration;
+import org.joda.time.Instant;
+
+class BufferMismatchedRows<DestinationT extends @NonNull Object, ElementT>
+    extends PTransform<PCollection<KV<DestinationT, MismatchedRow>>, 
PCollectionTuple> {
+  private final Coder<BigQueryStorageApiInsertError> failedRowsCoder;
+  private final Coder<TableRow> successfulRowsCoder;
+  private final Coder<DestinationT> destinationCoder;
+  private final StorageApiDynamicDestinations<ElementT, DestinationT> 
dynamicDestinations;
+  private final StorageApiWriteUnshardedRecords.WriteRecordsDoFn<DestinationT, 
ElementT> writeDoFn;
+  private final TupleTag<BigQueryStorageApiInsertError> failedRowsTag;
+  private final @Nullable TupleTag<TableRow> successfulRowsTag;
+  // This output is effectively ignored, since we only support this code path 
for
+  // StorageApiWriteRecordsInconsistent.
+  private final TupleTag<KV<String, String>> finalizeTag = new 
TupleTag<>("finalizeTag");
+  private static final int NUM_DEFAULT_SHARDS = 20;
+  private static final Duration RETRY_MISMATCHED_ROWS_PERIOD = 
Duration.standardMinutes(1);
+
+  public BufferMismatchedRows(
+      Coder<BigQueryStorageApiInsertError> failedRowsCoder,
+      Coder<TableRow> successfulRowsCoder,
+      Coder<DestinationT> destinationCoder,
+      StorageApiDynamicDestinations<ElementT, DestinationT> 
dynamicDestinations,
+      StorageApiWriteUnshardedRecords.WriteRecordsDoFn<DestinationT, ElementT> 
writeDoFn,
+      TupleTag<BigQueryStorageApiInsertError> failedRowsTag,
+      @Nullable TupleTag<TableRow> successfulRowsTag) {
+    this.failedRowsCoder = failedRowsCoder;
+    this.successfulRowsCoder = successfulRowsCoder;
+    this.destinationCoder = destinationCoder;
+    this.dynamicDestinations = dynamicDestinations;
+    this.writeDoFn = writeDoFn;
+    this.failedRowsTag = failedRowsTag;
+    this.successfulRowsTag = successfulRowsTag;
+  }
+
+  @Override
+  public PCollectionTuple expand(PCollection<KV<DestinationT, MismatchedRow>> 
input) {
+    // Append records to the Storage API streams.
+    TupleTagList tupleTagList = TupleTagList.of(failedRowsTag);
+    if (successfulRowsTag != null) {
+      tupleTagList = tupleTagList.and(successfulRowsTag);
+    }
+
+    PCollectionTuple result =
+        input
+            .apply(
+                "addShard",
+                ParDo.of(
+                    new DoFn<
+                        KV<DestinationT, MismatchedRow>,
+                        KV<ShardedKey<DestinationT>, MismatchedRow>>() {
+                      int shardNumber;
+
+                      @Setup
+                      public void setup() {
+                        shardNumber = 
ThreadLocalRandom.current().nextInt(NUM_DEFAULT_SHARDS);
+                      }
+
+                      @ProcessElement
+                      public void process(
+                          @Element KV<DestinationT, MismatchedRow> element,
+                          OutputReceiver<KV<ShardedKey<DestinationT>, 
MismatchedRow>> o) {
+                        ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
+                        buffer.putInt(++shardNumber % NUM_DEFAULT_SHARDS);
+                        o.output(
+                            KV.of(
+                                ShardedKey.of(element.getKey(), 
buffer.array()),
+                                element.getValue()));
+                      }
+                    }))
+            .setCoder(KvCoder.of(ShardedKey.Coder.of(destinationCoder), 
MismatchedRow.Coder.of()))
+            .apply(
+                "bufferMismatchedRows",
+                ParDo.of(new BufferingDoFn(writeDoFn))
+                    .withOutputTags(finalizeTag, tupleTagList)
+                    .withSideInputs(dynamicDestinations.getSideInputs()));
+
+    result.get(failedRowsTag).setCoder(failedRowsCoder);
+    if (successfulRowsTag != null) {
+      result.get(successfulRowsTag).setCoder(successfulRowsCoder);
+    }
+    return result;
+  }
+
+  class BufferingDoFn
+      extends DoFn<KV<ShardedKey<DestinationT>, MismatchedRow>, KV<String, 
String>> {
+    private final 
StorageApiWriteUnshardedRecords.WriteRecordsDoFn<DestinationT, ElementT>

Review Comment:
   maybe WriteRecordsDoFn should have service part extracted instead of 
wrapping whole doFn here.  



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