|
| 1 | +package net.snowflake.ingest.streaming.internal; |
| 2 | + |
| 3 | +import net.snowflake.ingest.utils.Logging; |
| 4 | +import net.snowflake.ingest.utils.ParameterProvider; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collection; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Objects; |
| 11 | + |
| 12 | +import static net.snowflake.ingest.utils.Constants.MAX_BLOB_SIZE_IN_BYTES; |
| 13 | + |
| 14 | +/** |
| 15 | + * Responsible for accepting data from channels and collating into collections that will be used to build the actual blobs |
| 16 | + * <p> |
| 17 | + * A chunk is represented as a list of channel data from a single table |
| 18 | + * A blob is represented as a list of chunks that must share the same schema (but not necessarily the same table) |
| 19 | + * <p> |
| 20 | + * This class returns a list of blobs |
| 21 | + */ |
| 22 | +class BlobDataBuilder<T> { |
| 23 | + private static final Logging logger = new Logging(BlobDataBuilder.class); |
| 24 | + private final List<List<List<ChannelData<T>>>> allBlobs; |
| 25 | + private final ParameterProvider parameterProvider; |
| 26 | + private final String clientName; |
| 27 | + private List<List<ChannelData<T>>> currentBlob; |
| 28 | + private ChannelData<T> prevChannelData = null; |
| 29 | + private float totalCurrentBlobSizeInBytes = 0F; |
| 30 | + private float totalBufferSizeInBytes = 0F; |
| 31 | + |
| 32 | + public BlobDataBuilder(String clientName, ParameterProvider parameterProvider) { |
| 33 | + this.clientName = clientName; |
| 34 | + this.parameterProvider = parameterProvider; |
| 35 | + this.currentBlob = new ArrayList<>(); |
| 36 | + this.allBlobs = new ArrayList<>(); |
| 37 | + } |
| 38 | + |
| 39 | + public List<List<List<ChannelData<T>>>> getAllBlobData() { |
| 40 | + addCurrentBlob(); |
| 41 | + return allBlobs; |
| 42 | + } |
| 43 | + |
| 44 | + public void appendDataForTable(Collection<? extends SnowflakeStreamingIngestChannelFlushable<T>> tableChannels) { |
| 45 | + List<ChannelData<T>> chunk = getChunkForTable(tableChannels); |
| 46 | + appendChunk(chunk); |
| 47 | + } |
| 48 | + |
| 49 | + private List<ChannelData<T>> getChunkForTable(Collection<? extends SnowflakeStreamingIngestChannelFlushable<T>> tableChannels) { |
| 50 | + List<ChannelData<T>> channelsDataPerTable = Collections.synchronizedList(new ArrayList<>()); |
| 51 | + // Use parallel stream since getData could be the performance bottleneck when we have a |
| 52 | + // high number of channels |
| 53 | + tableChannels.parallelStream() |
| 54 | + .forEach( |
| 55 | + channel -> { |
| 56 | + if (channel.isValid()) { |
| 57 | + ChannelData<T> data = channel.getData(); |
| 58 | + if (data != null) { |
| 59 | + channelsDataPerTable.add(data); |
| 60 | + } |
| 61 | + } |
| 62 | + }); |
| 63 | + return channelsDataPerTable; |
| 64 | + } |
| 65 | + |
| 66 | + private void appendChunk(List<ChannelData<T>> chunkData) { |
| 67 | + if (chunkData.isEmpty()) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (currentBlob.size() >= parameterProvider.getMaxChunksInBlob()) { |
| 72 | + // Create a new blob if the current one already contains max allowed number of chunks |
| 73 | + logger.logInfo( |
| 74 | + "Max allowed number of chunks in the current blob reached. chunkCount={}" |
| 75 | + + " maxChunkCount={}", |
| 76 | + currentBlob.size(), |
| 77 | + parameterProvider.getMaxChunksInBlob()); |
| 78 | + |
| 79 | + addCurrentBlob(); |
| 80 | + } |
| 81 | + |
| 82 | + int i, start = 0; |
| 83 | + for (i = 0; i < chunkData.size(); i++) { |
| 84 | + ChannelData<T> channelData = chunkData.get(i); |
| 85 | + if (prevChannelData != null && shouldStopProcessing( |
| 86 | + totalCurrentBlobSizeInBytes, |
| 87 | + totalBufferSizeInBytes, |
| 88 | + channelData, |
| 89 | + prevChannelData)) { |
| 90 | + logger.logInfo( |
| 91 | + "Creation of another blob is needed because of blob/chunk size limit or" |
| 92 | + + " different encryption ids or different schema, client={}, table={}," |
| 93 | + + " blobSize={}, chunkSize={}, nextChannelSize={}, encryptionId1={}," |
| 94 | + + " encryptionId2={}, schema1={}, schema2={}", |
| 95 | + clientName, |
| 96 | + channelData.getChannelContext().getTableName(), |
| 97 | + totalCurrentBlobSizeInBytes, |
| 98 | + totalBufferSizeInBytes, |
| 99 | + channelData.getBufferSize(), |
| 100 | + channelData.getChannelContext().getEncryptionKeyId(), |
| 101 | + prevChannelData.getChannelContext().getEncryptionKeyId(), |
| 102 | + channelData.getColumnEps().keySet(), |
| 103 | + prevChannelData.getColumnEps().keySet()); |
| 104 | + |
| 105 | + if (i != start) { |
| 106 | + currentBlob.add(chunkData.subList(start, i)); |
| 107 | + start = i; |
| 108 | + } |
| 109 | + |
| 110 | + addCurrentBlob(); |
| 111 | + } |
| 112 | + |
| 113 | + totalCurrentBlobSizeInBytes += channelData.getBufferSize(); |
| 114 | + totalBufferSizeInBytes += channelData.getBufferSize(); |
| 115 | + prevChannelData = channelData; |
| 116 | + } |
| 117 | + |
| 118 | + if (i != start) { |
| 119 | + currentBlob.add(chunkData.subList(start, i)); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private void addCurrentBlob() { |
| 124 | + if (!currentBlob.isEmpty()) { |
| 125 | + allBlobs.add(currentBlob); |
| 126 | + currentBlob = new ArrayList<>(); |
| 127 | + } |
| 128 | + totalBufferSizeInBytes = 0; |
| 129 | + totalCurrentBlobSizeInBytes = 0; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Check whether we should stop merging more channels into the same chunk, we need to stop in a |
| 134 | + * few cases: |
| 135 | + * |
| 136 | + * <p>When the blob size is larger than a certain threshold |
| 137 | + * |
| 138 | + * <p>When the chunk size is larger than a certain threshold |
| 139 | + * |
| 140 | + * <p>When the schemas are not the same |
| 141 | + */ |
| 142 | + private boolean shouldStopProcessing( |
| 143 | + float totalBufferSizeInBytes, |
| 144 | + float totalBufferSizePerTableInBytes, |
| 145 | + ChannelData<T> current, |
| 146 | + ChannelData<T> prev) { |
| 147 | + return totalBufferSizeInBytes + current.getBufferSize() > MAX_BLOB_SIZE_IN_BYTES |
| 148 | + || totalBufferSizePerTableInBytes + current.getBufferSize() |
| 149 | + > parameterProvider.getMaxChunkSizeInBytes() |
| 150 | + || !Objects.equals( |
| 151 | + current.getChannelContext().getEncryptionKeyId(), |
| 152 | + prev.getChannelContext().getEncryptionKeyId()) |
| 153 | + || !current.getColumnEps().keySet().equals(prev.getColumnEps().keySet()); |
| 154 | + } |
| 155 | +} |
0 commit comments