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

posted on   荣锋亮  阅读(118)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.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 拆分小表

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示