Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.beam.sdk.coders.RowCoder;
import org.apache.beam.sdk.schemas.Schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import java.util.function.BiConsumer;
import java.util.function.Function;
import org.apache.beam.sdk.values.TimestampedValue;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Iterators;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Lists;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.PeekingIterator;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.joda.time.Instant;

Expand Down Expand Up @@ -85,7 +87,8 @@ public SplittingIterable(
@Override
public Iterator<Value> iterator() {
return new Iterator<Value>() {
final Iterator<StorageApiWritePayload> underlyingIterator = underlying.iterator();
final PeekingIterator<StorageApiWritePayload> underlyingIterator =
Iterators.peekingIterator(underlying.iterator());

@Override
public boolean hasNext() {
Expand All @@ -103,6 +106,13 @@ public Value next() {
ProtoRows.Builder inserts = ProtoRows.newBuilder();
long bytesSize = 0;
while (underlyingIterator.hasNext()) {
// Make sure that we don't exceed the split-size length over multiple elements. A single
// element can exceed
// the split threshold, but in that case it should be the only element returned.
if ((bytesSize + underlyingIterator.peek().getPayload().length > splitSize)
&& inserts.getSerializedRowsCount() > 0) {
break;
}
StorageApiWritePayload payload = underlyingIterator.next();
ByteString byteString = ByteString.copyFrom(payload.getPayload());
@Nullable TableRow failsafeTableRow = null;
Expand Down Expand Up @@ -157,9 +167,6 @@ public Value next() {
timestamps.add(timestamp);
failsafeRows.add(failsafeTableRow);
bytesSize += byteString.size();
if (bytesSize > splitSize) {
break;
}
}
return new AutoValue_SplittingIterable_Value(inserts.build(), timestamps, failsafeRows);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1008,15 +1008,18 @@ void postFlush() {
this.bigLakeConfiguration = bigLakeConfiguration;
}

boolean shouldFlush() {
return numPendingRecords > flushThresholdCount || numPendingRecordBytes > flushThresholdBytes;
boolean shouldFlush(int recordBytes) {
return numPendingRecords > flushThresholdCount
|| (((numPendingRecordBytes + recordBytes) > flushThresholdBytes)
&& numPendingRecords > 0);
}

void flushIfNecessary(
OutputReceiver<BigQueryStorageApiInsertError> failedRowsReceiver,
@Nullable OutputReceiver<TableRow> successfulRowsReceiver)
@Nullable OutputReceiver<TableRow> successfulRowsReceiver,
int recordBytes)
throws Exception {
if (shouldFlush()) {
if (shouldFlush(recordBytes)) {
forcedFlushes.inc();
// Too much memory being used. Flush the state and wait for it to drain out.
// TODO(reuvenlax): Consider waiting for memory usage to drop instead of waiting for all the
Expand Down Expand Up @@ -1172,10 +1175,12 @@ public void process(
@Nullable
OutputReceiver<TableRow> successfulRowsReceiver =
(successfulRowsTag != null) ? o.get(successfulRowsTag) : null;
flushIfNecessary(failedRowsReceiver, successfulRowsReceiver);

int recordBytes = element.getValue().getPayload().length;
flushIfNecessary(failedRowsReceiver, successfulRowsReceiver, recordBytes);
state.addMessage(element.getValue(), elementTs, failedRowsReceiver);
++numPendingRecords;
numPendingRecordBytes += element.getValue().getPayload().length;
numPendingRecordBytes += recordBytes;
}

private OutputReceiver<TableRow> makeSuccessfulRowsreceiver(
Expand Down
Loading