Skip to content

Commit 6b165ca

Browse files
authored
add metric logging for cloud fetch (#305)
1 parent d886a41 commit 6b165ca

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-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: 27 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,43 @@ 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
77+
.getLogger()
78+
.log(LogLevel.info, `Result File Download speed from cloud storage ${cleanUrl}: ${speedMBps.toFixed(4)} MB/s`);
79+
80+
const speedThresholdMBps = this.context.getConfig().cloudFetchSpeedThresholdMBps;
81+
if (speedMBps < speedThresholdMBps) {
82+
this.context
83+
.getLogger()
84+
.log(
85+
LogLevel.warn,
86+
`Results download is slower than threshold speed of ${speedThresholdMBps.toFixed(
87+
4,
88+
)} MB/s: ${speedMBps.toFixed(4)} MB/s`,
89+
);
90+
}
91+
}
92+
7193
private async downloadLink(link: TSparkArrowResultLink): Promise<ArrowBatch> {
7294
if (Date.now() >= link.expiryTime.toNumber()) {
7395
throw new Error('CloudFetch link has expired');
7496
}
7597

98+
const startTime = Date.now();
7699
const response = await this.fetch(link.fileLink, { headers: link.httpHeaders });
77100
if (!response.ok) {
78101
throw new Error(`CloudFetch HTTP error ${response.status} ${response.statusText}`);
79102
}
80103

81104
const result = await response.arrayBuffer();
105+
const downloadTimeMs = Date.now() - startTime;
106+
107+
this.logDownloadMetrics(link.fileLink, result.byteLength, downloadTimeMs);
108+
82109
return {
83110
batches: [Buffer.from(result)],
84111
rowCount: link.rowCount.toNumber(true),

0 commit comments

Comments
 (0)