|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.beam.sdk.io.gcp.bigquery; |
| 19 | + |
| 20 | +import com.google.api.services.bigquery.model.TableRow; |
| 21 | +import com.google.auto.value.AutoValue; |
| 22 | +import com.google.cloud.bigquery.storage.v1.ProtoRows; |
| 23 | +import com.google.protobuf.ByteString; |
| 24 | +import com.google.protobuf.Descriptors; |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.BitSet; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.function.Consumer; |
| 30 | +import java.util.function.Supplier; |
| 31 | +import java.util.stream.IntStream; |
| 32 | +import java.util.stream.Stream; |
| 33 | +import org.apache.beam.sdk.util.Preconditions; |
| 34 | +import org.apache.beam.sdk.util.ThrowingSupplier; |
| 35 | +import org.apache.beam.sdk.values.TimestampedValue; |
| 36 | +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Lists; |
| 37 | +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Maps; |
| 38 | +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.PeekingIterator; |
| 39 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 40 | +import org.joda.time.Instant; |
| 41 | + |
| 42 | +@AutoValue |
| 43 | +abstract class AppendRowsPacket { |
| 44 | + abstract ProtoRows getProtoRows(); |
| 45 | + |
| 46 | + abstract List<Instant> getTimestamps(); |
| 47 | + |
| 48 | + abstract List<@Nullable TableRow> getFailsafeTableRows(); |
| 49 | + |
| 50 | + abstract BitSet getSchemaMismatchedRows(); |
| 51 | + |
| 52 | + abstract Map<Integer, byte[]> getSchemaHashes(); |
| 53 | + |
| 54 | + abstract Map<Integer, TableRow> getUnknownFields(); |
| 55 | + |
| 56 | + abstract Map<Integer, byte[]> getOriginalPayloads(); |
| 57 | + |
| 58 | + private static final BitSet EMPTY_BIT_SET = new BitSet(0); |
| 59 | + |
| 60 | + static AppendRowsPacket fromStorageApiWritePayload( |
| 61 | + PeekingIterator<StorageApiWritePayload> underlyingIterator, |
| 62 | + long maxByteSize, |
| 63 | + SchemaChangeDetectorHelper schemaChangeDetectorHelper, |
| 64 | + Instant elementTimestamp, |
| 65 | + Supplier<AppendClientInfo> appendClientInfoSupplier, |
| 66 | + Consumer<TimestampedValue<BigQueryStorageApiInsertError>> failedRowsConsumer, |
| 67 | + ThrowingSupplier<byte[]> getCurrentTableSchemaHash, |
| 68 | + ThrowingSupplier<Descriptors.Descriptor> getCurrentTableSchemaDescriptor) { |
| 69 | + List<Instant> timestamps = Lists.newArrayList(); |
| 70 | + List<@Nullable TableRow> failsafeRows = Lists.newArrayList(); |
| 71 | + Map<Integer, byte[]> schemaHashes = Maps.newHashMap(); |
| 72 | + Map<Integer, TableRow> unknownFields = Maps.newHashMap(); |
| 73 | + Map<Integer, byte[]> originalPayloads = Maps.newHashMap(); |
| 74 | + ProtoRows.Builder inserts = ProtoRows.newBuilder(); |
| 75 | + long bytesSize = 0; |
| 76 | + BitSet mismatchedRows = new BitSet(); |
| 77 | + try { |
| 78 | + while (underlyingIterator.hasNext()) { |
| 79 | + // Make sure that we don't exceed the maxByteSize over multiple elements. A single |
| 80 | + // element can exceed |
| 81 | + // the split threshold, but in that case it should be the only element returned. |
| 82 | + if ((bytesSize + underlyingIterator.peek().getPayload().length > maxByteSize) |
| 83 | + && inserts.getSerializedRowsCount() > 0) { |
| 84 | + break; |
| 85 | + } |
| 86 | + StorageApiWritePayload payload = underlyingIterator.next(); |
| 87 | + |
| 88 | + @Nullable TableRow failsafeTableRow = null; |
| 89 | + try { |
| 90 | + failsafeTableRow = payload.getFailsafeTableRow(); |
| 91 | + } catch (IOException e) { |
| 92 | + // Do nothing, table row will be generated later from row bytes |
| 93 | + } |
| 94 | + |
| 95 | + // If autoUpdateSchema is set, we try to automatically merge in unknown fields. |
| 96 | + SchemaChangeDetectorHelper.MergePayloadResult mergeResult = |
| 97 | + schemaChangeDetectorHelper.getMergedPayload( |
| 98 | + payload, elementTimestamp, failsafeTableRow, appendClientInfoSupplier.get()); |
| 99 | + if (mergeResult.getKind() == SchemaChangeDetectorHelper.MergePayloadResult.Kind.FAILED) { |
| 100 | + failedRowsConsumer.accept(mergeResult.getFailed()); |
| 101 | + continue; |
| 102 | + } |
| 103 | + ByteString byteString = mergeResult.getMerged(); |
| 104 | + |
| 105 | + if (schemaChangeDetectorHelper.isPayloadSchemaOutOfDate( |
| 106 | + payload, |
| 107 | + byteString.toByteArray(), |
| 108 | + getCurrentTableSchemaHash, |
| 109 | + getCurrentTableSchemaDescriptor)) { |
| 110 | + mismatchedRows.set(inserts.getSerializedRowsCount()); |
| 111 | + } |
| 112 | + |
| 113 | + int currentIndex = inserts.getSerializedRowsCount(); |
| 114 | + inserts.addSerializedRows(byteString); |
| 115 | + Instant timestamp = payload.getTimestamp(); |
| 116 | + if (timestamp == null) { |
| 117 | + timestamp = elementTimestamp; |
| 118 | + } |
| 119 | + timestamps.add(timestamp); |
| 120 | + failsafeRows.add(failsafeTableRow); |
| 121 | + if (payload.getSchemaHash() != null) { |
| 122 | + schemaHashes.put(currentIndex, Preconditions.checkStateNotNull(payload.getSchemaHash())); |
| 123 | + } |
| 124 | + if (payload.getUnknownFields() != null) { |
| 125 | + unknownFields.put( |
| 126 | + currentIndex, Preconditions.checkStateNotNull(payload.getUnknownFields())); |
| 127 | + originalPayloads.put(currentIndex, payload.getPayload()); |
| 128 | + } |
| 129 | + bytesSize += byteString.size(); |
| 130 | + } |
| 131 | + } catch (Exception e) { |
| 132 | + throw new RuntimeException(e); |
| 133 | + } |
| 134 | + |
| 135 | + return new AutoValue_AppendRowsPacket( |
| 136 | + inserts.build(), |
| 137 | + timestamps, |
| 138 | + failsafeRows, |
| 139 | + mismatchedRows, |
| 140 | + schemaHashes, |
| 141 | + unknownFields, |
| 142 | + originalPayloads); |
| 143 | + } |
| 144 | + |
| 145 | + AppendRowsPacket getSchemaMismatchedRowsOnly() { |
| 146 | + ProtoRows.Builder inserts = ProtoRows.newBuilder(); |
| 147 | + List<Instant> timestamps = Lists.newArrayList(); |
| 148 | + List<@Nullable TableRow> failsafeTableRows = Lists.newArrayList(); |
| 149 | + Map<Integer, byte[]> schemaHashes = Maps.newHashMap(); |
| 150 | + Map<Integer, TableRow> unknownFields = Maps.newHashMap(); |
| 151 | + Map<Integer, byte[]> originalPayloads = Maps.newHashMap(); |
| 152 | + if (!getSchemaMismatchedRows().isEmpty()) { |
| 153 | + int newIndex = 0; |
| 154 | + for (int i = 0; i < getProtoRows().getSerializedRowsCount(); i++) { |
| 155 | + if (getSchemaMismatchedRows().get(i)) { |
| 156 | + inserts.addSerializedRows(getProtoRows().getSerializedRows(i)); |
| 157 | + timestamps.add(getTimestamps().get(i)); |
| 158 | + failsafeTableRows.add(getFailsafeTableRows().get(i)); |
| 159 | + if (getUnknownFields().containsKey(i)) { |
| 160 | + unknownFields.put(newIndex, Preconditions.checkStateNotNull(getUnknownFields().get(i))); |
| 161 | + } |
| 162 | + if (getOriginalPayloads().containsKey(i)) { |
| 163 | + originalPayloads.put( |
| 164 | + newIndex, Preconditions.checkStateNotNull(getOriginalPayloads().get(i))); |
| 165 | + } |
| 166 | + if (getSchemaHashes().containsKey(i)) { |
| 167 | + schemaHashes.put(newIndex, Preconditions.checkStateNotNull(getSchemaHashes().get(i))); |
| 168 | + } |
| 169 | + newIndex++; |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + BitSet allBits = new BitSet(inserts.getSerializedRowsCount()); |
| 174 | + allBits.set(0, inserts.getSerializedRowsCount()); |
| 175 | + return new AutoValue_AppendRowsPacket( |
| 176 | + inserts.build(), |
| 177 | + timestamps, |
| 178 | + failsafeTableRows, |
| 179 | + allBits, |
| 180 | + schemaHashes, |
| 181 | + unknownFields, |
| 182 | + originalPayloads); |
| 183 | + } |
| 184 | + |
| 185 | + AppendRowsPacket getSchemaMatchedRowsOnly() { |
| 186 | + if (getSchemaMismatchedRows().isEmpty()) { |
| 187 | + return this; |
| 188 | + } |
| 189 | + |
| 190 | + ProtoRows.Builder inserts = ProtoRows.newBuilder(); |
| 191 | + List<Instant> timestamps = Lists.newArrayList(); |
| 192 | + Map<Integer, byte[]> schemaHashes = Maps.newHashMap(); |
| 193 | + Map<Integer, TableRow> unknownFields = Maps.newHashMap(); |
| 194 | + Map<Integer, byte[]> originalPayloads = Maps.newHashMap(); |
| 195 | + List<@Nullable TableRow> failsafeTableRows = Lists.newArrayList(); |
| 196 | + int newIndex = 0; |
| 197 | + for (int i = 0; i < getProtoRows().getSerializedRowsCount(); i++) { |
| 198 | + if (!getSchemaMismatchedRows().get(i)) { |
| 199 | + inserts.addSerializedRows(getProtoRows().getSerializedRows(i)); |
| 200 | + timestamps.add(getTimestamps().get(i)); |
| 201 | + failsafeTableRows.add(getFailsafeTableRows().get(i)); |
| 202 | + if (getSchemaHashes().containsKey(i)) { |
| 203 | + schemaHashes.put(newIndex, Preconditions.checkStateNotNull(getSchemaHashes().get(i))); |
| 204 | + } |
| 205 | + if (getUnknownFields().containsKey(i)) { |
| 206 | + unknownFields.put(newIndex, Preconditions.checkStateNotNull(getUnknownFields().get(i))); |
| 207 | + } |
| 208 | + if (getOriginalPayloads().containsKey(i)) { |
| 209 | + originalPayloads.put( |
| 210 | + newIndex, Preconditions.checkStateNotNull(getOriginalPayloads().get(i))); |
| 211 | + } |
| 212 | + newIndex++; |
| 213 | + } |
| 214 | + } |
| 215 | + return new AutoValue_AppendRowsPacket( |
| 216 | + inserts.build(), |
| 217 | + timestamps, |
| 218 | + failsafeTableRows, |
| 219 | + EMPTY_BIT_SET, |
| 220 | + schemaHashes, |
| 221 | + unknownFields, |
| 222 | + originalPayloads); |
| 223 | + } |
| 224 | + |
| 225 | + Stream<StorageApiWritePayload> toPayloadStream() { |
| 226 | + return IntStream.range(0, getProtoRows().getSerializedRowsCount()) |
| 227 | + .mapToObj( |
| 228 | + i -> { |
| 229 | + try { |
| 230 | + byte @Nullable [] originalBytes = getOriginalPayloads().get(i); |
| 231 | + StorageApiWritePayload payload = |
| 232 | + StorageApiWritePayload.of( |
| 233 | + originalBytes != null |
| 234 | + ? originalBytes |
| 235 | + : getProtoRows().getSerializedRows(i).toByteArray(), |
| 236 | + getUnknownFields().get(i), |
| 237 | + getFailsafeTableRows().get(i)) |
| 238 | + .withTimestamp(getTimestamps().get(i)); |
| 239 | + byte @Nullable [] hash = getSchemaHashes().get(i); |
| 240 | + if (hash != null) { |
| 241 | + payload = payload.withSchemaHash(hash); |
| 242 | + } |
| 243 | + return payload; |
| 244 | + } catch (IOException e) { |
| 245 | + throw new RuntimeException(e); |
| 246 | + } |
| 247 | + }); |
| 248 | + } |
| 249 | +} |
0 commit comments