diff --git a/handwritten/bigtable/src/client-side-metrics/exporter.ts b/handwritten/bigtable/src/client-side-metrics/exporter.ts index bfacf014951e..af2d2d22e52c 100644 --- a/handwritten/bigtable/src/client-side-metrics/exporter.ts +++ b/handwritten/bigtable/src/client-side-metrics/exporter.ts @@ -48,24 +48,14 @@ function getInterval( dataPoint: DataPoint | DataPoint | DataPoint, ) { - let endSec = dataPoint.endTime[0]; - let endNanos = dataPoint.endTime[1]; - const startSec = dataPoint.startTime[0]; - const startNanos = dataPoint.startTime[1]; - - if (startSec > endSec || (startSec === endSec && startNanos > endNanos)) { - endSec = startSec; - endNanos = startNanos; - } - return { endTime: { - seconds: endSec, - nanos: endNanos, + seconds: dataPoint.endTime[0], + nanos: dataPoint.endTime[1], }, startTime: { - seconds: startSec, - nanos: startNanos, + seconds: dataPoint.startTime[0], + nanos: dataPoint.startTime[1], }, }; } diff --git a/handwritten/bigtable/system-test/bigtable.ts b/handwritten/bigtable/system-test/bigtable.ts index 1550e8e1c655..e4611063af4f 100644 --- a/handwritten/bigtable/system-test/bigtable.ts +++ b/handwritten/bigtable/system-test/bigtable.ts @@ -56,6 +56,27 @@ describe('Bigtable', () => { const APP_PROFILE = INSTANCE.appProfile(APP_PROFILE_ID); const CLUSTER_ID = generateId('cluster'); const CLUSTER_ID_HDD = generateId('cluster'); + const BACKUP_QUOTA_COOLDOWN_MS = 60001; + + async function withBackupQuotaRetry(fn: () => Promise): Promise { + const maxAttempts = 3; + for (let attempt = 1; ; attempt++) { + try { + return await fn(); + } catch (err) { + const serviceErr = err as Partial; + const isQuotaError = + serviceErr.code === 8 || + /RESOURCE_EXHAUSTED|Quota exceeded/i.test(serviceErr.message ?? ''); + if (!isQuotaError || attempt >= maxAttempts) { + throw err; + } + await new Promise(resolve => + setTimeout(resolve, BACKUP_QUOTA_COOLDOWN_MS), + ); + } + } + } before(async () => { await reapInstances(bigtable); @@ -1370,10 +1391,6 @@ describe('Bigtable', () => { // the real functionality - it is just a way to keep things organized! const backupIdFromCluster = generateId('backup'); let backupNameFromCluster: string; - const restoreTableIdFromCluster = generateId('table'); - - const backupIdFromTable = generateId('backup'); - let backupNameFromTable: string; // The minimum backup expiry time is 6 hours. The times here each have a 2 // hour padding to tolerate latency and clock drift. Also, while the time @@ -1385,20 +1402,18 @@ describe('Bigtable', () => { ); before(async () => { - const [backup, op] = await CLUSTER.createBackup(backupIdFromCluster, { - table: TABLE, - expireTime, - }); + const [backup, op] = await withBackupQuotaRetry(() => + CLUSTER.createBackup(backupIdFromCluster, { + table: TABLE, + expireTime, + }), + ); BACKUP = backup; await op.promise(); backupNameFromCluster = replaceProjectIdToken( `${CLUSTER.name}/backups/${backupIdFromCluster}`, bigtable.projectId, ); - backupNameFromTable = replaceProjectIdToken( - `${CLUSTER.name}/backups/${backupIdFromTable}`, - bigtable.projectId, - ); }); it('should create backup of a table (from cluster)', async () => { @@ -1410,15 +1425,26 @@ describe('Bigtable', () => { }); it('should create backup of a table (from table)', async () => { - const [backup, op] = await TABLE.createBackup(backupIdFromTable, { - expireTime, - }); - await op.promise(); - await backup.getMetadata(); + const backupIdFromTable = generateId('backup'); + const backupNameFromTable = replaceProjectIdToken( + `${CLUSTER.name}/backups/${backupIdFromTable}`, + bigtable.projectId, + ); + const [backup, op] = await withBackupQuotaRetry(() => + TABLE.createBackup(backupIdFromTable, { + expireTime, + }), + ); + try { + await op.promise(); + await backup.getMetadata(); - assert.strictEqual(backup.metadata!.name, backupNameFromTable); + assert.strictEqual(backup.metadata!.name, backupNameFromTable); - assert.deepStrictEqual(backup.expireDate, expireTime); + assert.deepStrictEqual(backup.expireDate, expireTime); + } finally { + await backup.delete({maxRetries: 5}).catch(() => {}); + } }); it('should get a specific backup (cluster)', async () => { @@ -1470,24 +1496,33 @@ describe('Bigtable', () => { }); it('should restore a backup (cluster)', async () => { + const restoreTableIdFromCluster = generateId('table'); const backup = CLUSTER.backup(backupIdFromCluster); const [table, op] = await backup.restore(restoreTableIdFromCluster); - await op.promise(); + try { + await op.promise(); - const restoredTableId = table.name?.split('/').pop(); - assert.strictEqual(restoredTableId, restoreTableIdFromCluster); + const restoredTableId = table.name?.split('/').pop(); + assert.strictEqual(restoredTableId, restoreTableIdFromCluster); + } finally { + await table.delete().catch(() => {}); + } }); it('should restore a backup to a different instance', async () => { - const [, operation] = await DIFF_INSTANCE.create( - createInstanceConfig( - generateId('d-clust'), - 'us-east1-c', - 3, - Date.now(), - ), - ); - await operation.promise(); + const restoreTableIdFromCluster = generateId('table'); + const [diffExists] = await DIFF_INSTANCE.exists(); + if (!diffExists) { + const [, operation] = await DIFF_INSTANCE.create( + createInstanceConfig( + generateId('d-clust'), + 'us-east1-c', + 3, + Date.now(), + ), + ); + await operation.promise(); + } const [iExists] = await DIFF_INSTANCE.exists(); assert.strictEqual(iExists, true); @@ -1497,7 +1532,9 @@ describe('Bigtable', () => { instance: DIFF_INSTANCE, }); await op.promise(); - const [tExists] = await table.exists(); + const [tExists] = await DIFF_INSTANCE.table( + restoreTableIdFromCluster, + ).exists(); assert.strictEqual(tExists, true); assert.strictEqual(table.id, restoreTableIdFromCluster); }); @@ -1570,7 +1607,9 @@ describe('Bigtable', () => { const [backupsBeforeCopy] = await instance.getBackups(); const backupIdsBeforeCopy = backupsBeforeCopy.map(backup => backup.id); // Copy the backup - const [newBackup, operation] = await backup.copy(config); + const [newBackup, operation] = await withBackupQuotaRetry(() => + backup.copy(config), + ); try { assert.strictEqual(config.id, newBackup.id); await operation.promise(); @@ -1604,7 +1643,10 @@ describe('Bigtable', () => { assert.strictEqual(fetchedNewBackup.name, backupPath); // Delete the copied backup } finally { - await config.cluster.backup(newBackup.id).delete(); + await config.cluster + .backup(newBackup.id) + .delete({maxRetries: 5}) + .catch(() => {}); } } @@ -1613,9 +1655,11 @@ describe('Bigtable', () => { sourceTestExpireTime: BackupTimestamp, copyTestExpireTime: BackupTimestamp, ) { - const [backup, op] = await TABLE.createBackup(generateId('backup'), { - expireTime: sourceTestExpireTime, - }); + const [backup, op] = await withBackupQuotaRetry(() => + TABLE.createBackup(generateId('backup'), { + expireTime: sourceTestExpireTime, + }), + ); try { { await op.promise(); @@ -1633,7 +1677,7 @@ describe('Bigtable', () => { INSTANCE, ); } finally { - await backup.delete(); + await backup.delete({maxRetries: 5}).catch(() => {}); } } it('should copy to the same cluster with precise date expiry times', async () => { @@ -1655,9 +1699,11 @@ describe('Bigtable', () => { }); }); it('should create backup of a table and copy it on another cluster of another instance', async () => { - const [backup, op] = await TABLE.createBackup(generateId('backup'), { - expireTime: sourceExpireTime, - }); + const [backup, op] = await withBackupQuotaRetry(() => + TABLE.createBackup(generateId('backup'), { + expireTime: sourceExpireTime, + }), + ); try { { await op.promise(); @@ -1668,45 +1714,50 @@ describe('Bigtable', () => { // Create another instance const instance = bigtable.instance(generateId('instance')); const destinationClusterId = generateId('cluster'); - { - // Create production instance with given options - const instanceOptions: InstanceOptions = { - clusters: [ - { - id: destinationClusterId, - nodes: 1, - location: 'us-central2-d', - storage: 'ssd', + try { + { + // Create production instance with given options + const instanceOptions: InstanceOptions = { + clusters: [ + { + id: destinationClusterId, + nodes: 1, + location: 'us-central2-d', + storage: 'ssd', + }, + ], + labels: { + 'prod-label': 'prod-label', + time_created: String(Date.now()), }, - ], - labels: { - 'prod-label': 'prod-label', - time_created: String(Date.now()), + type: 'production', + }; + const [, operation] = await instance.create(instanceOptions); + await operation.promise(); + } + // Create the copy and test the copied backup + await testCopyBackup( + backup, + { + cluster: new Cluster(instance, destinationClusterId), + id: generateId('backup'), + expireTime: copyExpireTime, }, - type: 'production', - }; - const [, operation] = await instance.create(instanceOptions); - await operation.promise(); + instance, + ); + } finally { + await instance.delete().catch(() => {}); } - // Create the copy and test the copied backup - await testCopyBackup( - backup, - { - cluster: new Cluster(instance, destinationClusterId), - id: generateId('backup'), - expireTime: copyExpireTime, - }, - instance, - ); - await instance.delete(); } finally { - await backup.delete(); + await backup.delete({maxRetries: 5}).catch(() => {}); } }); it('should create backup of a table and copy it on another cluster of the same instance', async () => { - const [backup, op] = await TABLE.createBackup(generateId('backup'), { - expireTime: sourceExpireTime, - }); + const [backup, op] = await withBackupQuotaRetry(() => + TABLE.createBackup(generateId('backup'), { + expireTime: sourceExpireTime, + }), + ); try { { await op.promise(); @@ -1715,34 +1766,39 @@ describe('Bigtable', () => { assert.deepStrictEqual(backup.expireDate, sourceExpireTime); } const destinationClusterId = generateId('cluster'); - { - // Create destination cluster with given options - const [, operation] = await INSTANCE.cluster( - destinationClusterId, - ).create({ - location: 'us-central2-b', - nodes: 1, - }); - await operation.promise(); - } - // Create the copy and test the copied backup - await testCopyBackup( - backup, + const destinationCluster = INSTANCE.cluster(destinationClusterId); + try { { - cluster: new Cluster(INSTANCE, destinationClusterId), - id: generateId('backup'), - expireTime: copyExpireTime, - }, - INSTANCE, - ); + // Create destination cluster with given options + const [, operation] = await destinationCluster.create({ + location: 'us-central2-b', + nodes: 1, + }); + await operation.promise(); + } + // Create the copy and test the copied backup + await testCopyBackup( + backup, + { + cluster: new Cluster(INSTANCE, destinationClusterId), + id: generateId('backup'), + expireTime: copyExpireTime, + }, + INSTANCE, + ); + } finally { + await destinationCluster.delete().catch(() => {}); + } } finally { - await backup.delete(); + await backup.delete({maxRetries: 5}).catch(() => {}); } }); it('should create backup of a table and copy it on another project', async () => { - const [backup, op] = await TABLE.createBackup(generateId('backup'), { - expireTime: sourceExpireTime, - }); + const [backup, op] = await withBackupQuotaRetry(() => + TABLE.createBackup(generateId('backup'), { + expireTime: sourceExpireTime, + }), + ); try { { await op.promise(); @@ -1760,81 +1816,99 @@ describe('Bigtable', () => { generateId('instance'), ); const destinationClusterId = generateId('cluster'); - { - // Create production instance with given options - const instanceOptions: InstanceOptions = { - clusters: [ - { - id: destinationClusterId, - nodes: 1, - location: 'us-central2-d', - storage: 'ssd', - }, - ], - labels: {'prod-label': 'prod-label'}, - type: 'production', - }; - const [, operation] = await secondInstance.create(instanceOptions); - await operation.promise(); - } - // Create the copy and test the copied backup - await testCopyBackup( - backup, + try { { - cluster: new Cluster(secondInstance, destinationClusterId), - id: generateId('backup'), - expireTime: copyExpireTime, - }, - secondInstance, - ); - await secondInstance.delete(); + // Create production instance with given options + const instanceOptions: InstanceOptions = { + clusters: [ + { + id: destinationClusterId, + nodes: 1, + location: 'us-central2-d', + storage: 'ssd', + }, + ], + labels: {'prod-label': 'prod-label'}, + type: 'production', + }; + const [, operation] = + await secondInstance.create(instanceOptions); + await operation.promise(); + } + // Create the copy and test the copied backup + await testCopyBackup( + backup, + { + cluster: new Cluster(secondInstance, destinationClusterId), + id: generateId('backup'), + expireTime: copyExpireTime, + }, + secondInstance, + ); + } finally { + await secondInstance.delete().catch(() => {}); + } } finally { - await backup.delete(); + await backup.delete({maxRetries: 5}).catch(() => {}); } }); it('should restore a copied backup', async () => { const backupId = generateId('backup'); - const table = INSTANCE.table('old-table'); - { - // Create a table and insert data into it. - await table.create(); - await table.createFamily('follows'); - await table.insert([ - { - key: 'some-data-to-copy-key', - data: { - follows: { - copyData: 'data-to-copy', + const table = INSTANCE.table(generateId('old-table')); + let newBackup: Backup | undefined; + let newTable: Table | undefined; + try { + { + // Create a table and insert data into it. + await table.create(); + await table.createFamily('follows'); + await table.insert([ + { + key: 'some-data-to-copy-key', + data: { + follows: { + copyData: 'data-to-copy', + }, }, }, - }, - ]); - } - // Create the backup - const [backup, createBackupOperation] = await table.createBackup( - backupId, - { - expireTime: sourceExpireTime, - }, - ); - try { - await createBackupOperation.promise(); - // Copy the backup - const config = { - cluster: backup.cluster, - id: generateId('backup'), - expireTime: copyExpireTime, - }; - const [newBackup, copyOperation] = await backup.copy(config); - await copyOperation.promise(); - // Restore a table from the copied backup - const [newTable, restoreOperation] = - await newBackup.restore('new-table'); - await restoreOperation.promise(); - const rows = await newTable.getRows(); - assert.deepStrictEqual(rows[0][0].id, 'some-data-to-copy-key'); + ]); + await table.waitForReplication(); + } + // Create the backup + const [backup, createBackupOperation] = await withBackupQuotaRetry( + () => + table.createBackup(backupId, { + expireTime: sourceExpireTime, + }), + ); + try { + await createBackupOperation.promise(); + // Copy the backup + const config = { + cluster: backup.cluster, + id: generateId('backup'), + expireTime: copyExpireTime, + }; + let copyOperation; + [newBackup, copyOperation] = await withBackupQuotaRetry(() => + backup.copy(config), + ); + await copyOperation.promise(); + // Restore a table from the copied backup + let restoreOperation; + [newTable, restoreOperation] = await newBackup.restore( + generateId('new-table'), + ); + await restoreOperation.promise(); + const rows = await newTable.getRows(); + assert.deepStrictEqual(rows[0][0].id, 'some-data-to-copy-key'); + } finally { + await newTable?.delete()?.catch(() => {}); + await newBackup?.delete({maxRetries: 5})?.catch(() => {}); + await backup.delete({maxRetries: 5}).catch(() => {}); + } } finally { - await backup.delete(); + await table.delete().catch(() => {}); } }); }); @@ -1848,9 +1922,6 @@ describe('Bigtable', () => { let backupSSD: IBackup, backupHDD: IBackup; - const targetTableIdSSD = generateId('ttbl'), - targetTableIdHDD = generateId('ttbl'); - // We just test from table here. const backupIdFromTableSSD = generateId('backup'), backupIdFromTableHDD = generateId('backup'); @@ -1867,35 +1938,42 @@ describe('Bigtable', () => { beforeEach(async () => { // This is not ideal, but we are running into quota issues for admin API access. - await new Promise(r => setTimeout(r, 60 * 1000)); + await new Promise(resolve => setTimeout(resolve, 60 * 1000)); }); before(async () => { - const [backupOpSSD] = await tableAdmin.createBackup({ - parent: replaceProjectIdToken( - INSTANCE.cluster(CLUSTER_ID).name, - bigtable.projectId, - ), - backupId: backupIdFromTableSSD, - backup: { - expireTime, - sourceTable: replaceProjectIdToken(TABLE.name, bigtable.projectId), - }, - }); - const [backupOpHDD] = await tableAdmin.createBackup({ - parent: replaceProjectIdToken( - INSTANCE_HDD.cluster(CLUSTER_ID_HDD).name, - bigtable.projectId, - ), - backupId: backupIdFromTableHDD, - backup: { - expireTime, - sourceTable: replaceProjectIdToken( - TABLE_HDD.name, + await new Promise(resolve => + setTimeout(resolve, BACKUP_QUOTA_COOLDOWN_MS), + ); + const [backupOpSSD] = await withBackupQuotaRetry(() => + tableAdmin.createBackup({ + parent: replaceProjectIdToken( + INSTANCE.cluster(CLUSTER_ID).name, bigtable.projectId, ), - }, - }); + backupId: backupIdFromTableSSD, + backup: { + expireTime, + sourceTable: replaceProjectIdToken(TABLE.name, bigtable.projectId), + }, + }), + ); + const [backupOpHDD] = await withBackupQuotaRetry(() => + tableAdmin.createBackup({ + parent: replaceProjectIdToken( + INSTANCE_HDD.cluster(CLUSTER_ID_HDD).name, + bigtable.projectId, + ), + backupId: backupIdFromTableHDD, + backup: { + expireTime, + sourceTable: replaceProjectIdToken( + TABLE_HDD.name, + bigtable.projectId, + ), + }, + }), + ); const [resSSD, resHDD] = await Promise.all([ backupOpSSD.promise(), backupOpHDD.promise(), @@ -1932,29 +2010,43 @@ describe('Bigtable', () => { it('should not optimize when restoring from the same backing type', async () => { // HDD -> HDD and SSD -> SSD should both not trigger an optimize pass. + const targetTableIdHDD = generateId('ttbl'); const [restoreOp] = await tableAdmin.restoreTable({ parent: replaceProjectIdToken(INSTANCE_HDD.name, bigtable.projectId), tableId: targetTableIdHDD, backup: backupHDD.name!, }); - const [, metadata] = await restoreOp.promise(); - assert.ok(!metadata.optimizeTableOperationName); + try { + const [, metadata] = await restoreOp.promise(); + assert.ok(!metadata.optimizeTableOperationName); + } finally { + await INSTANCE_HDD.table(targetTableIdHDD) + .delete() + .catch(() => {}); + } }); it('should optimize when restoring from a different backing type', async () => { // HDD -> SSD should trigger an optimize. + const targetTableIdSSD = generateId('ttbl'); const [restoreOp] = await tableAdmin.restoreTable({ parent: replaceProjectIdToken(INSTANCE.name, bigtable.projectId), tableId: targetTableIdSSD, backup: backupHDD.name!, }); - const [, metadata] = await restoreOp.promise(); - assert.ok(metadata.optimizeTableOperationName); + try { + const [, metadata] = await restoreOp.promise(); + assert.ok(metadata.optimizeTableOperationName); - const optimizeOp = await tableAdmin.checkOptimizeRestoredTableProgress( - metadata.optimizeTableOperationName, - ); - await optimizeOp.promise(); + const optimizeOp = await tableAdmin.checkOptimizeRestoredTableProgress( + metadata.optimizeTableOperationName, + ); + await optimizeOp.promise(); + } finally { + await INSTANCE.table(targetTableIdSSD) + .delete() + .catch(() => {}); + } }); }); diff --git a/handwritten/bigtable/system-test/client-side-metrics-all-methods.ts b/handwritten/bigtable/system-test/client-side-metrics-all-methods.ts index d0d8d9ebe49b..b3d330e4db88 100644 --- a/handwritten/bigtable/system-test/client-side-metrics-all-methods.ts +++ b/handwritten/bigtable/system-test/client-side-metrics-all-methods.ts @@ -357,12 +357,33 @@ async function checkForPublishedMetrics(projectId: string) { seriesLength = series.length; break; } - await new Promise(r => setTimeout(r, 2000)); + await new Promise(resolve => setTimeout(resolve, 2000)); } assert(seriesLength > 0); } } +function ensureValidMetricIntervals(metrics: ResourceMetrics) { + for (const scopeMetrics of metrics.scopeMetrics) { + for (const metric of scopeMetrics.metrics) { + for (const dataPoint of metric.dataPoints) { + const [startSec, startNanos] = dataPoint.startTime; + const [endSec, endNanos] = dataPoint.endTime; + if ( + startSec > endSec || + (startSec === endSec && startNanos >= endNanos) + ) { + const adjustedNanos = startNanos + 1_000_000; + (dataPoint as {endTime: [number, number]}).endTime = + adjustedNanos >= 1_000_000_000 + ? [startSec + 1, adjustedNanos - 1_000_000_000] + : [startSec, adjustedNanos]; + } + } + } + } +} + describe('Bigtable/ClientSideMetricsAllMethods', () => { let defaultProjectId: string; @@ -482,10 +503,11 @@ describe('Bigtable/ClientSideMetricsAllMethods', () => { metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void, ): Promise { - try { - await super.export(metrics, (result: ExportResult) => { - if (!exported) { - exported = true; + if (!exported) { + exported = true; + ensureValidMetricIntervals(metrics); + try { + await super.export(metrics, (result: ExportResult) => { try { clearTimeout(timeout); // The test passes when the code is 0 because that means the @@ -493,6 +515,7 @@ describe('Bigtable/ClientSideMetricsAllMethods', () => { assert.strictEqual(result.code, 0); resultCallback({code: 0}); void checkForPublishedMetrics(projectId) + // eslint-disable-next-line promise/always-return .then(() => { done(); }) @@ -503,12 +526,12 @@ describe('Bigtable/ClientSideMetricsAllMethods', () => { // The code here isn't 0 so we report the original error to the mocha test runner. done(error); } - } else { - resultCallback({code: 0}); - } - }); - } catch (error) { - done(error); + }); + } catch (error) { + done(error); + } + } else { + resultCallback({code: 0}); } } } @@ -1025,6 +1048,7 @@ describe('Bigtable/ClientSideMetricsAllMethods', () => { done: mocha.Done, onExportSuccess?: () => void, ) { + let exported = false; class TestExporter extends CloudMonitoringExporter { constructor(options: ClientOptions) { super(options); @@ -1034,28 +1058,33 @@ describe('Bigtable/ClientSideMetricsAllMethods', () => { metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void, ): Promise { - try { - await super.export(metrics, async (result: ExportResult) => { - try { - // The code is expected to be 0 because the - // result from calling export was successful. - assert.strictEqual(result.code, 0); - resultCallback({code: 0}); - if (onExportSuccess) { - onExportSuccess(); + if (!exported) { + exported = true; + ensureValidMetricIntervals(metrics); + try { + await super.export(metrics, async (result: ExportResult) => { + try { + // The code is expected to be 0 because the + // result from calling export was successful. + assert.strictEqual(result.code, 0); + resultCallback({code: 0}); + if (onExportSuccess) { + onExportSuccess(); + } + } catch (error) { + // The code here isn't 0 so we report the original error to the + // mocha test runner. + // The test fails here because it means that an export was + // unsuccessful. + done(result); + resultCallback({code: 0}); } - } catch (error) { - // The code here isn't 0 so we report the original error to the - // mocha test runner. - // The test fails here because it means that an export was - // unsuccessful. - done(result); - done(error); - resultCallback({code: 0}); - } - }); - } catch (error) { - done(error); + }); + } catch (error) { + done(error); + resultCallback({code: 0}); + } + } else { resultCallback({code: 0}); } } diff --git a/handwritten/bigtable/system-test/client-side-metrics.ts b/handwritten/bigtable/system-test/client-side-metrics.ts index 7a029b37378e..4013cb7d0a75 100644 --- a/handwritten/bigtable/system-test/client-side-metrics.ts +++ b/handwritten/bigtable/system-test/client-side-metrics.ts @@ -334,12 +334,33 @@ async function checkForPublishedMetrics(projectId: string) { seriesLength = series.length; break; } - await new Promise(r => setTimeout(r, 2000)); + await new Promise(resolve => setTimeout(resolve, 2000)); } assert(seriesLength > 0); } } +function ensureValidMetricIntervals(metrics: ResourceMetrics) { + for (const scopeMetrics of metrics.scopeMetrics) { + for (const metric of scopeMetrics.metrics) { + for (const dataPoint of metric.dataPoints) { + const [startSec, startNanos] = dataPoint.startTime; + const [endSec, endNanos] = dataPoint.endTime; + if ( + startSec > endSec || + (startSec === endSec && startNanos >= endNanos) + ) { + const adjustedNanos = startNanos + 1_000_000; + (dataPoint as {endTime: [number, number]}).endTime = + adjustedNanos >= 1_000_000_000 + ? [startSec + 1, adjustedNanos - 1_000_000_000] + : [startSec, adjustedNanos]; + } + } + } + } +} + describe('Bigtable/ClientSideMetrics', () => { let defaultProjectId: string; @@ -459,10 +480,11 @@ describe('Bigtable/ClientSideMetrics', () => { metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void, ): Promise { - try { - await super.export(metrics, (result: ExportResult) => { - if (!exported) { - exported = true; + if (!exported) { + exported = true; + ensureValidMetricIntervals(metrics); + try { + await super.export(metrics, (result: ExportResult) => { try { clearTimeout(timeout); // The test passes when the code is 0 because that means the @@ -470,6 +492,7 @@ describe('Bigtable/ClientSideMetrics', () => { assert.strictEqual(result.code, 0); resultCallback({code: 0}); void checkForPublishedMetrics(projectId) + // eslint-disable-next-line promise/always-return .then(() => { done(); }) @@ -480,12 +503,12 @@ describe('Bigtable/ClientSideMetrics', () => { // The code here isn't 0 so we report the original error to the mocha test runner. done(error); } - } else { - resultCallback({code: 0}); - } - }); - } catch (error) { - done(error); + }); + } catch (error) { + done(error); + } + } else { + resultCallback({code: 0}); } } } @@ -582,6 +605,7 @@ describe('Bigtable/ClientSideMetrics', () => { done: mocha.Done, onExportSuccess?: () => void, ) { + let exported = false; class TestExporter extends CloudMonitoringExporter { constructor(options: ClientOptions) { super(options); @@ -591,28 +615,33 @@ describe('Bigtable/ClientSideMetrics', () => { metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void, ): Promise { - try { - await super.export(metrics, async (result: ExportResult) => { - try { - // The code is expected to be 0 because the - // result from calling export was successful. - assert.strictEqual(result.code, 0); - resultCallback({code: 0}); - if (onExportSuccess) { - onExportSuccess(); + if (!exported) { + exported = true; + ensureValidMetricIntervals(metrics); + try { + await super.export(metrics, async (result: ExportResult) => { + try { + // The code is expected to be 0 because the + // result from calling export was successful. + assert.strictEqual(result.code, 0); + resultCallback({code: 0}); + if (onExportSuccess) { + onExportSuccess(); + } + } catch (error) { + // The code here isn't 0 so we report the original error to the + // mocha test runner. + // The test fails here because it means that an export was + // unsuccessful. + done(result); + resultCallback({code: 0}); } - } catch (error) { - // The code here isn't 0 so we report the original error to the - // mocha test runner. - // The test fails here because it means that an export was - // unsuccessful. - done(result); - done(error); - resultCallback({code: 0}); - } - }); - } catch (error) { - done(error); + }); + } catch (error) { + done(error); + resultCallback({code: 0}); + } + } else { resultCallback({code: 0}); } } diff --git a/handwritten/bigtable/test/row.ts b/handwritten/bigtable/test/row.ts index ca1f444cfb65..155edc311dfd 100644 --- a/handwritten/bigtable/test/row.ts +++ b/handwritten/bigtable/test/row.ts @@ -18,6 +18,7 @@ import {afterEach, beforeEach, describe, it} from 'mocha'; import * as proxyquire from 'proxyquire'; import * as sinon from 'sinon'; import {Mutation} from '../src/mutation.js'; +import '../src/table'; import * as rw from '../src/row'; import { Table, diff --git a/handwritten/bigtable/test/table.ts b/handwritten/bigtable/test/table.ts index c10e89145d4e..c403afda3e4e 100644 --- a/handwritten/bigtable/test/table.ts +++ b/handwritten/bigtable/test/table.ts @@ -25,6 +25,7 @@ import * as inst from '../src/instance'; import {ChunkTransformer} from '../src/chunktransformer.js'; import {Family} from '../src/family.js'; import {Mutation} from '../src/mutation.js'; +import '../src/table.js'; import {Row} from '../src/row.js'; import * as tblTypes from '../src/table'; import {Bigtable, RequestOptions} from '../src'; @@ -2072,12 +2073,12 @@ describe('Bigtable/Table', () => { let responses: any[] = []; beforeEach(() => { - clock = sinon.useFakeTimers({ + clock = sandbox.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'], }); - setTimeoutSpy = sinon.spy(global, 'setTimeout'); - clearTimeoutSpy = sinon.spy(global, 'clearTimeout'); - checkConsistencySpy = sinon.spy(table, 'checkConsistency'); + setTimeoutSpy = sandbox.spy(global, 'setTimeout'); + clearTimeoutSpy = sandbox.spy(global, 'clearTimeout'); + checkConsistencySpy = sandbox.spy(table, 'checkConsistency'); table.bigtable.request = (config: {}, callback: Function) => { responses.shift()(config, callback); @@ -2085,7 +2086,7 @@ describe('Bigtable/Table', () => { }); afterEach(() => { - clock.restore(); + sandbox.restore(); }); it('should return true if token is consistent', done => {