Skip to content

Commit 65dcf3b

Browse files
committed
add metric logging for cloud fetch
1 parent d886a41 commit 65dcf3b

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

lib/DBSQLClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient, I
9090

9191
useCloudFetch: true, // enabling cloud fetch by default.
9292
cloudFetchConcurrentDownloads: 10,
93+
cloudFetchSpeedThresholdMBps: 0.1,
9394

9495
useLZ4Compression: true,
9596
};

lib/contracts/IClientContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface ClientConfig {
1818

1919
useCloudFetch: boolean;
2020
cloudFetchConcurrentDownloads: number;
21+
cloudFetchSpeedThresholdMBps: number;
2122

2223
useLZ4Compression: boolean;
2324
}

lib/result/CloudFetchResultHandler.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import IClientContext from '../contracts/IClientContext';
55
import IResultsProvider, { ResultsProviderFetchNextOptions } from './IResultsProvider';
66
import { ArrowBatch } from './utils';
77
import { LZ4 } from '../utils';
8+
import { LogLevel } from '../contracts/IDBSQLLogger';
89

910
export default class CloudFetchResultHandler implements IResultsProvider<ArrowBatch> {
1011
private readonly context: IClientContext;
@@ -68,17 +69,40 @@ export default class CloudFetchResultHandler implements IResultsProvider<ArrowBa
6869
return batch;
6970
}
7071

72+
private logDownloadMetrics(url: string, fileSizeBytes: number, downloadTimeMs: number): void {
73+
const speedMBps = (fileSizeBytes / (1024 * 1024)) / (downloadTimeMs / 1000);
74+
const cleanUrl = url.split('?')[0];
75+
76+
this.context.getLogger().log(
77+
LogLevel.info,
78+
`Result File Download speed from cloud storage ${cleanUrl}: ${speedMBps.toFixed(4)} MB/s`
79+
);
80+
81+
const speedThresholdMBps = this.context.getConfig().cloudFetchSpeedThresholdMBps;
82+
if (speedMBps < speedThresholdMBps) {
83+
this.context.getLogger().log(
84+
LogLevel.warn,
85+
`Results download is slower than threshold speed of ${speedThresholdMBps.toFixed(4)} MB/s: ${speedMBps.toFixed(4)} MB/s`
86+
);
87+
}
88+
}
89+
7190
private async downloadLink(link: TSparkArrowResultLink): Promise<ArrowBatch> {
7291
if (Date.now() >= link.expiryTime.toNumber()) {
7392
throw new Error('CloudFetch link has expired');
7493
}
7594

95+
const startTime = Date.now();
7696
const response = await this.fetch(link.fileLink, { headers: link.httpHeaders });
7797
if (!response.ok) {
7898
throw new Error(`CloudFetch HTTP error ${response.status} ${response.statusText}`);
7999
}
80100

81101
const result = await response.arrayBuffer();
102+
const downloadTimeMs = Date.now() - startTime;
103+
104+
this.logDownloadMetrics(link.fileLink, result.byteLength, downloadTimeMs);
105+
82106
return {
83107
batches: [Buffer.from(result)],
84108
rowCount: link.rowCount.toNumber(true),

0 commit comments

Comments
 (0)