Skip to content

Commit 79ced69

Browse files
committed
Support config a seperate datasource for quartz
1 parent 90a8cdc commit 79ced69

6 files changed

Lines changed: 148 additions & 0 deletions

File tree

dolphinscheduler-master/src/main/resources/application.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ mybatis-plus:
6767
id-type: auto
6868
banner: false
6969

70+
scheduler-plugin:
71+
quartz:
72+
# By default, will not use a separate datasource for quartz
73+
# If you want to use a separate datasource, set it to true and configure the datasource below
74+
# If set true, and you do not configure hikari below, it will use the same configuration as spring.datasource.hikari
75+
using-separate-datasource: false
76+
# hikari:
7077

7178
registry:
7279
type: zookeeper
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Introduction
2+
3+
This module is the quartz-scheduler plugin module, this plugin will use quartz as the scheduler.
4+
5+
# How to use
6+
7+
By default, you don't need to do any configuration, the quartz-scheduler use the same datasource with master.
8+
9+
```yaml
10+
scheduler-plugin:
11+
quartz:
12+
# By default, will not use a separate datasource for quartz
13+
# If you want to use a separate datasource, set it to true and configure the datasource below
14+
# If set true and you do not configure hikari below, it will use the same configuration as spring.datasource.hikari
15+
using-separate-datasource: false
16+
# hikari:
17+
```
18+
19+
## Use a separate datasource for quartz-scheduler
20+
21+
If your have a lot of workflow to be scheduled, it is better to set a separate datasource for quartz-scheduler,
22+
otherwise the performance of quartz thread may be affected. After set `using-separate-datasource` to true, the
23+
quartz-scheduler will use a new datasource, and the datasource configuration is same with the master datasource
24+
configuration.
25+
26+
```yaml
27+
scheduler-plugin:
28+
quartz:
29+
using-separate-datasource: true
30+
```
31+
32+
If you want to use a separate datasource for quartz-scheduler, and set the configuration, you need to configure the
33+
hikari configuration like below:
34+
35+
```yaml
36+
scheduler-plugin:
37+
quartz:
38+
using-separate-datasource: true
39+
hikari:
40+
jdbc-url: jdbc:mysql://localhost:3306/dolphinscheduler
41+
username: root
42+
password: root
43+
minimum-idle: 8
44+
maximum-pool-size: 8
45+
```
46+

dolphinscheduler-scheduler-plugin/dolphinscheduler-scheduler-quartz/src/main/java/org/apache/dolphinscheduler/scheduler/quartz/QuartzSchedulerAutoConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration;
2727
import org.springframework.context.annotation.Bean;
2828

29+
// Right now we only have one implementation of SchedulerApi: QuartzScheduler
30+
// If we have more implementations in the future, we can use @ConditionalOnProperty to distinguish them, rather than using @ConditionalOnClass
2931
@AutoConfiguration(after = {QuartzAutoConfiguration.class})
3032
@ConditionalOnClass(value = Scheduler.class)
3133
public class QuartzSchedulerAutoConfiguration {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dolphinscheduler.scheduler.quartz;
19+
20+
import java.util.Optional;
21+
22+
import javax.sql.DataSource;
23+
24+
import lombok.Data;
25+
import lombok.extern.slf4j.Slf4j;
26+
27+
import org.quartz.Scheduler;
28+
import org.springframework.boot.autoconfigure.AutoConfiguration;
29+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
31+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
32+
import org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration;
33+
import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer;
34+
import org.springframework.boot.context.properties.ConfigurationProperties;
35+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
36+
import org.springframework.context.annotation.Bean;
37+
import org.springframework.core.annotation.Order;
38+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
39+
40+
import com.zaxxer.hikari.HikariConfig;
41+
import com.zaxxer.hikari.HikariDataSource;
42+
43+
@Slf4j
44+
@AutoConfiguration(after = {DataSourceAutoConfiguration.class}, before = QuartzSchedulerAutoConfiguration.class)
45+
@ConditionalOnClass(value = Scheduler.class)
46+
@ConditionalOnProperty(prefix = "scheduler-plugin.quartz", name = "using-separate-datasource", havingValue = "true")
47+
@EnableConfigurationProperties(QuartzSchedulerDataSourceAutoConfiguration.QuartzDataSourceConfig.class)
48+
public class QuartzSchedulerDataSourceAutoConfiguration {
49+
50+
/**
51+
* Initialize After {@link QuartzAutoConfiguration.JdbcStoreTypeConfiguration#dataSourceCustomizer}
52+
*/
53+
@Bean
54+
@Order(1)
55+
public SchedulerFactoryBeanCustomizer schedulerFactoryBeanCustomizer(QuartzDataSourceConfig quartzDataSourceConfig,
56+
DataSource defaultDataSource) {
57+
return schedulerFactoryBean -> {
58+
if (!quartzDataSourceConfig.isUsingSeparateDatasource()) {
59+
return;
60+
}
61+
final HikariConfig quartzHikariConfig = Optional.ofNullable(quartzDataSourceConfig.getHikari())
62+
.orElse(generateQuartzHikariConfigFromDataSource((HikariDataSource) defaultDataSource));
63+
final HikariDataSource quartzDataSource = new HikariDataSource(quartzHikariConfig);
64+
schedulerFactoryBean.setDataSource(quartzDataSource);
65+
schedulerFactoryBean.setTransactionManager(new DataSourceTransactionManager(quartzDataSource));
66+
};
67+
}
68+
69+
@Data
70+
@ConfigurationProperties(prefix = "scheduler-plugin.quartz")
71+
public static class QuartzDataSourceConfig {
72+
73+
private boolean usingSeparateDatasource = false;
74+
private HikariConfig hikari;
75+
}
76+
77+
private HikariConfig generateQuartzHikariConfigFromDataSource(HikariDataSource hikariDataSource) {
78+
HikariConfig defaultQuartzHikariConfig = new HikariConfig();
79+
hikariDataSource.copyStateTo(defaultQuartzHikariConfig);
80+
defaultQuartzHikariConfig.setPoolName("QuartzHikariCP");
81+
return defaultQuartzHikariConfig;
82+
}
83+
84+
}

dolphinscheduler-scheduler-plugin/dolphinscheduler-scheduler-quartz/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
#
1717

1818
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
19+
org.apache.dolphinscheduler.scheduler.quartz.QuartzSchedulerDataSourceAutoConfiguration,\
1920
org.apache.dolphinscheduler.scheduler.quartz.QuartzSchedulerAutoConfiguration

dolphinscheduler-standalone-server/src/main/resources/application.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ spring:
3232
url: jdbc:h2:mem:dolphinscheduler;MODE=MySQL;DB_CLOSE_DELAY=-1;DATABASE_TO_LOWER=true
3333
username: sa
3434
password: ""
35+
hikari:
36+
connection-test-query: select 1
37+
pool-name: DolphinSchedulerDataSource
3538
quartz:
3639
job-store-type: jdbc
3740
jdbc:
@@ -67,6 +70,11 @@ spring:
6770
matching-strategy: ANT_PATH_MATCHER
6871
cloud.discovery.client.composite-indicator.enabled: false
6972

73+
scheduler-plugin:
74+
quartz:
75+
using-separate-datasource: false
76+
# hikari:
77+
7078
mybatis-plus:
7179
mapper-locations: classpath:org/apache/dolphinscheduler/dao/mapper/*Mapper.xml
7280
type-aliases-package: org.apache.dolphinscheduler.dao.entity

0 commit comments

Comments
 (0)