Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: 作业包含大量主机,执行作业请求响应时间过长 #1697

Closed
jsonwan opened this issue Jan 14, 2023 · 2 comments
Closed

perf: 作业包含大量主机,执行作业请求响应时间过长 #1697

jsonwan opened this issue Jan 14, 2023 · 2 comments
Assignees
Labels
done 已上线到正式环境并验收通过 kind/enhancement 功能改进特性

Comments

@jsonwan
Copy link
Collaborator

jsonwan commented Jan 14, 2023

快速执行接口传入的执行目标解析出的主机超过3k时,响应时间超过30s,导致某些超时参数设置较短的客户端超时。
导致超时的慢查询语句:
insert into job_execute.task_instance_host (task_instance_id, host_id, ip, ipv6) values (?, ?, ?, ?)
需要优化。

@wangyu096
Copy link
Collaborator

wangyu096 commented Jun 28, 2023

image

耗时比较长的这个,主要就是批量写入 task_instance_host 表, DAO 层是使用 Jooq。代码如下:

    public void saveTaskInstanceHosts(long taskInstanceId,
                                      Collection<HostDTO> hosts) {
        BatchBindStep batchInsert = ctx.batch(
            ctx.insertInto(TASK_INSTANCE_HOST, TASK_INSTANCE_HOST.TASK_INSTANCE_ID,
                TASK_INSTANCE_HOST.HOST_ID, TASK_INSTANCE_HOST.IP, TASK_INSTANCE_HOST.IPV6)
                .values((Long) null, null, null, null)
        );

        for (HostDTO host : hosts) {
            batchInsert = batchInsert.bind(
                taskInstanceId,
                host.getHostId(),
                host.getIp(),
                host.getIpv6()
            );
        }
        batchInsert.execute();
    }

@wangyu096
Copy link
Collaborator

wangyu096 commented Jun 28, 2023

原因

如果 jdbc 为设置rewriteBatchedStatements= false (默认), 那么即使是调用 Statement.executeBatch(), 也是逐个执行 SQL。关于rewriteBatchedStatements这个参数的介绍:

MySQL的JDBC连接的url中要加rewriteBatchedStatements参数,并保证5.1.13以上版本的驱动,才能实现高性能的批量插入。MySQL JDBC驱动在默认情况下会无视executeBatch()语句,把我们期望批量执行的一组sql语句拆散,一条一条地发给MySQL数据库,批量插入实际上是单条插入,直接造成较低的性能。只有把rewriteBatchedStatements参数置为true, 驱动才会帮你批量执行SQL。另外这个选项对INSERT/UPDATE/DELETE都有效。

参考

@wangyu096 wangyu096 changed the title perf: 接口传入大量目标主机或含有大量主机的节点或动态分组时响应时间过长优化 perf: 作业包含大量主机,执行作业请求响应时间过长 Jun 28, 2023
@wangyu096 wangyu096 added the todo 进入开发排期的状态,纳入了最近的迭代 label Jun 29, 2023
jsonwan added a commit that referenced this issue Jun 29, 2023
perf: 作业包含大量主机,执行作业请求响应时间过长 #1697
jsonwan added a commit that referenced this issue Jun 30, 2023
perf: 作业包含大量主机,执行作业请求响应时间过长 #1697
@bkjob-bot bkjob-bot added for test 可以在测试环境进行验收 done 已上线到正式环境并验收通过 and removed todo 进入开发排期的状态,纳入了最近的迭代 for test 可以在测试环境进行验收 labels Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
done 已上线到正式环境并验收通过 kind/enhancement 功能改进特性
Projects
None yet
Development

No branches or pull requests

4 participants