|
| 1 | +/*! |
| 2 | + * Copyright 2022 Google LLC. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as common from '@google-cloud/common'; |
| 18 | +import * as extend from 'extend'; |
| 19 | +import * as uuid from 'uuid'; |
| 20 | +import {RequestCallback, Table, InsertStreamOptions} from '.'; |
| 21 | +import {GoogleErrorBody} from '@google-cloud/common/build/src/util'; |
| 22 | +import bigquery from './types'; |
| 23 | +import {BATCH_LIMITS, RowBatch} from './rowBatch'; |
| 24 | +import {Stream} from 'stream'; |
| 25 | +import {RowBatchOptions, InsertRowsOptions, RowMetadata} from './table'; |
| 26 | + |
| 27 | +export interface MaxInsertOptions { |
| 28 | + maxOutstandingRows: number; |
| 29 | + maxOutstandingBytes: number; |
| 30 | + maxDelayMillis: number; |
| 31 | +} |
| 32 | + |
| 33 | +export const defaultOptions: MaxInsertOptions = { |
| 34 | + // The maximum number of rows we'll batch up for insert(). |
| 35 | + maxOutstandingRows: 300, |
| 36 | + |
| 37 | + // The maximum size of the total batched up rows for insert(). |
| 38 | + maxOutstandingBytes: 9 * 1024 * 1024, |
| 39 | + |
| 40 | + // The maximum time we'll wait to send batched rows, in milliseconds. |
| 41 | + maxDelayMillis: 10000, |
| 42 | +}; |
| 43 | + |
| 44 | +export type InsertRowsStreamResponse = bigquery.ITableDataInsertAllResponse; |
| 45 | + |
| 46 | +export type InsertRowsCallback = RequestCallback< |
| 47 | + bigquery.ITableDataInsertAllResponse | bigquery.ITable |
| 48 | +>; |
| 49 | +export interface InsertRow { |
| 50 | + insertId?: string; |
| 51 | + json?: bigquery.IJsonObject; |
| 52 | +} |
| 53 | + |
| 54 | +export type TableRow = bigquery.ITableRow; |
| 55 | +export interface PartialInsertFailure { |
| 56 | + message: string; |
| 57 | + reason: string; |
| 58 | + row: RowMetadata; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Standard row queue used for inserting rows. |
| 63 | + * |
| 64 | + * |
| 65 | + * @param {Table} table The table. |
| 66 | + * @param {Duplex} dup Row stream. |
| 67 | + * @param {InsertStreamOptions} options Insert and batch options. |
| 68 | + */ |
| 69 | +export class RowQueue { |
| 70 | + table: Table; |
| 71 | + stream: Stream; |
| 72 | + insertRowsOptions: InsertRowsOptions = {}; |
| 73 | + batch: RowBatch; |
| 74 | + batchOptions?: RowBatchOptions; |
| 75 | + inFlight: boolean; |
| 76 | + pending?: ReturnType<typeof setTimeout>; |
| 77 | + constructor(table: Table, dup: Stream, options?: InsertStreamOptions) { |
| 78 | + this.table = table; |
| 79 | + this.stream = dup; |
| 80 | + this.inFlight = false; |
| 81 | + |
| 82 | + const opts = typeof options === 'object' ? options : {}; |
| 83 | + |
| 84 | + if (opts.insertRowsOptions) { |
| 85 | + this.insertRowsOptions = opts.insertRowsOptions; |
| 86 | + } else { |
| 87 | + this.insertRowsOptions = {}; |
| 88 | + } |
| 89 | + if (opts.batchOptions) { |
| 90 | + this.setOptions(opts.batchOptions); |
| 91 | + } else { |
| 92 | + this.setOptions(); |
| 93 | + } |
| 94 | + |
| 95 | + this.batch = new RowBatch(this.batchOptions!); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Adds a row to the queue. |
| 100 | + * |
| 101 | + * @param {RowMetadata} row The row to insert. |
| 102 | + * @param {InsertRowsCallback} callback The insert callback. |
| 103 | + */ |
| 104 | + add(row: RowMetadata, callback: InsertRowsCallback): void { |
| 105 | + if (!this.insertRowsOptions.raw) { |
| 106 | + row = { |
| 107 | + json: Table.encodeValue_(row)!, |
| 108 | + }; |
| 109 | + |
| 110 | + if (this.insertRowsOptions.createInsertId !== false) { |
| 111 | + row.insertId = uuid.v4(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (!this.batch.canFit(row)) { |
| 116 | + this.insert(); |
| 117 | + } |
| 118 | + this.batch.add(row, callback); |
| 119 | + |
| 120 | + if (this.batch.isFull()) { |
| 121 | + this.insert(); |
| 122 | + } else if (!this.pending) { |
| 123 | + const {maxMilliseconds} = this.batchOptions!; |
| 124 | + this.pending = setTimeout(() => { |
| 125 | + this.insert(); |
| 126 | + }, maxMilliseconds); |
| 127 | + } |
| 128 | + } |
| 129 | + /** |
| 130 | + * Cancels any pending inserts and calls _insert immediately. |
| 131 | + */ |
| 132 | + insert(callback?: InsertRowsCallback): void { |
| 133 | + const {rows, callbacks} = this.batch; |
| 134 | + |
| 135 | + this.batch = new RowBatch(this.batchOptions!); |
| 136 | + |
| 137 | + if (this.pending) { |
| 138 | + clearTimeout(this.pending); |
| 139 | + delete this.pending; |
| 140 | + } |
| 141 | + if (rows.length > 0) { |
| 142 | + this._insert(rows, callbacks, callback); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Accepts a batch of rows and inserts them into table. |
| 148 | + * |
| 149 | + * @param {object[]} rows The rows to insert. |
| 150 | + * @param {InsertCallback[]} callbacks The corresponding callback functions. |
| 151 | + * @param {function} [callback] Callback to be fired when insert is done. |
| 152 | + */ |
| 153 | + _insert( |
| 154 | + rows: RowMetadata | RowMetadata[], |
| 155 | + callbacks: InsertRowsCallback[], |
| 156 | + cb?: InsertRowsCallback |
| 157 | + ): void { |
| 158 | + const json = extend(true, {}, this.insertRowsOptions, {rows}); |
| 159 | + |
| 160 | + delete json.createInsertId; |
| 161 | + delete json.partialRetries; |
| 162 | + delete json.raw; |
| 163 | + |
| 164 | + this.table.request( |
| 165 | + { |
| 166 | + method: 'POST', |
| 167 | + uri: '/insertAll', |
| 168 | + json, |
| 169 | + }, |
| 170 | + (err, resp) => { |
| 171 | + const partialFailures = (resp.insertErrors || []).map( |
| 172 | + (insertError: GoogleErrorBody) => { |
| 173 | + return { |
| 174 | + errors: insertError.errors!.map(error => { |
| 175 | + return { |
| 176 | + message: error.message, |
| 177 | + reason: error.reason, |
| 178 | + }; |
| 179 | + }), |
| 180 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 181 | + row: rows[(insertError as any).index], |
| 182 | + }; |
| 183 | + } |
| 184 | + ); |
| 185 | + |
| 186 | + if (partialFailures.length > 0) { |
| 187 | + err = new common.util.PartialFailureError({ |
| 188 | + errors: partialFailures, |
| 189 | + response: resp, |
| 190 | + } as GoogleErrorBody); |
| 191 | + |
| 192 | + callbacks.forEach(callback => callback!(err, resp)); |
| 193 | + this.stream.emit('error', err); |
| 194 | + } else { |
| 195 | + callbacks.forEach(callback => callback!(err, resp)); |
| 196 | + this.stream.emit('response', resp); |
| 197 | + cb!(err, resp); |
| 198 | + } |
| 199 | + cb!(err, resp); |
| 200 | + } |
| 201 | + ); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Sets the batching options. |
| 206 | + * |
| 207 | + * |
| 208 | + * @param {RowBatchOptions} [options] The batching options. |
| 209 | + */ |
| 210 | + setOptions(options = {} as RowBatchOptions): void { |
| 211 | + const defaults = this.getOptionDefaults(); |
| 212 | + |
| 213 | + const {maxBytes, maxRows, maxMilliseconds} = extend( |
| 214 | + true, |
| 215 | + defaults, |
| 216 | + options |
| 217 | + ); |
| 218 | + |
| 219 | + this.batchOptions = { |
| 220 | + maxBytes: Math.min(maxBytes, BATCH_LIMITS.maxBytes), |
| 221 | + maxRows: Math.min(maxRows!, BATCH_LIMITS.maxRows), |
| 222 | + maxMilliseconds: maxMilliseconds, |
| 223 | + }; |
| 224 | + } |
| 225 | + |
| 226 | + getOptionDefaults(): RowBatchOptions { |
| 227 | + // Return a unique copy to avoid shenanigans. |
| 228 | + const defaults: RowBatchOptions = { |
| 229 | + maxBytes: defaultOptions.maxOutstandingBytes, |
| 230 | + maxRows: defaultOptions.maxOutstandingRows, |
| 231 | + maxMilliseconds: defaultOptions.maxDelayMillis, |
| 232 | + }; |
| 233 | + return defaults; |
| 234 | + } |
| 235 | +} |
0 commit comments