cube.js dremio driver基于补偿机制提升查询速度
默认cube.js 的dremio driver 在设计的时候,为了进行状态处理的请求使用了循环处理,同时添加了一个1s的延迟处理
原始参考代码
async query(query, values) {
const queryString = applyParams(
query,
(values || []).map(s => (typeof s === 'string' ? {
toSqlString: () => SqlString.escape(s).replace(/\\\\\\([_%])/g, '\\$1').replace(/\\'/g, '\'\'')
} : s))
);
await this.getToken();
const jobId = await this.executeQuery(queryString);
for (;;) {
const { data } = await this.getJobStatus(jobId);
console.log(data.jobState, jobId);
if (data.jobState === 'FAILED') {
throw new Error(data.errorMessage);
} else if (data.jobState === 'CANCELED') {
throw new Error(`Job ${jobId} has been canceled`);
} else if (data.jobState === 'COMPLETED') {
let rows = [];
const querys = [];
for (let i = 0; i < data.rowCount; i += dremioJobLimit) {
querys.push(this.getJobResults(jobId, dremioJobLimit, i));
}
const parts = await Promise.all(querys);
parts.forEach((e) => {
rows = rows.concat(e.data.rows);
});
return rows;
}
// 此处延迟1s执行,真是因为这个,所以会发现dremio 的查询都比较慢
await this.sleep(1000);
}
}
解决方法
基于线性补偿机制,参考了bigquery driver
async query(query, values) {
const queryString = applyParams(
query,
(values || []).map(s => (typeof s === 'string' ? {
toSqlString: () => SqlString.escape(s).replace(/\\\\([_%])/g, '\\$1').replace(/\\'/g, '\'\'')
} : s))
);
await this.getToken();
const jobId = await this.executeQuery(queryString);
// do some query like bigquery links https://github.com/cube-js/cube.js/blob/master/packages/cubejs-bigquery-driver/driver/BigQueryDriver.js#L224
const startedTime = Date.now();
for (let i = 0; Date.now() - startedTime <= this.config.pollTimeout; i++) {
const { data } = await this.getJobStatus(jobId);
console.log(data.jobState, jobId);
if (data.jobState === 'FAILED') {
throw new Error(data.errorMessage);
} else if (data.jobState === 'CANCELED') {
throw new Error(`Job ${jobId} has been canceled`);
} else if (data.jobState === 'COMPLETED') {
let rows = [];
const querys = [];
for (let j = 0; j < data.rowCount; j += dremioJobLimit) {
querys.push(this.getJobResults(jobId, dremioJobLimit, j));
}
const parts = await Promise.all(querys);
parts.forEach((e) => {
rows = rows.concat(e.data.rows);
});
return rows;
}
await this.sleep(
Math.min(this.config.pollMaxInterval, 200 * i),
);
}
}
说明
经过以上的调整cube.js 与dremio 的查询结合dremio 的数据反射能力,基本都可以在1s内响应了
参考资料
https://github.com/cube-js/cube.js/blob/master/CONTRIBUTING.md
https://github.com/cube-js/cube.js/pull/2475
https://github.com/cube-js/cube.js/blob/master/packages/cubejs-bigquery-driver/driver/BigQueryDriver.js#L60
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2019-04-01 hasura graphql-engine 集成zombodb
2019-04-01 zombodb安装试用
2017-04-01 linux 磁盘挂载操作
2017-04-01 nginx 缓存处理
2016-04-01 jQuery.retryAjax
2015-04-01 C# 通用DataTable 拆分小表