You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by li...@apache.org on 2020/03/26 12:08:02 UTC

[incubator-dolphinscheduler] branch dev updated: Decouple DruidDataSource in ConnectionFactory and cache DataSource instance (#2232)

This is an automated email from the ASF dual-hosted git repository.

lidongdai pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-dolphinscheduler.git


The following commit(s) were added to refs/heads/dev by this push:
     new 74525c2  Decouple DruidDataSource in ConnectionFactory and cache DataSource instance (#2232)
74525c2 is described below

commit 74525c28b5f8eb69d4ffb134e41c340fcc102585
Author: tswstarplanet <ts...@apache.org>
AuthorDate: Thu Mar 26 20:07:52 2020 +0800

    Decouple DruidDataSource in ConnectionFactory and cache DataSource instance (#2232)
    
    * Decouple DruidDataSource in ConnectionFactory and cache DataSource
    instance
    
    * init dataSource first
    
    * fix code smell
    
    * add unit test
    
    * add unit test
    
    * add unit test
    
    * add unit test
    
    * fix unit test
    
    * delete useless logger
---
 .../org/apache/dolphinscheduler/dao/AlertDao.java  |  8 ++++-
 .../dao/datasource/ConnectionFactory.java          | 14 ++++++---
 .../dolphinscheduler/dao/upgrade/UpgradeDao.java   | 12 +++-----
 .../apache/dolphinscheduler/dao/AlertDaoTest.java  | 35 ++++++++++++++++++----
 .../dao/mapper/ConnectionFactoryTest.java          |  2 +-
 5 files changed, 51 insertions(+), 20 deletions(-)

diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java
index 8546f01..d61a181 100644
--- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java
+++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java
@@ -169,5 +169,11 @@ public class AlertDao extends AbstractBaseDao {
         return userAlertGroupMapper.listUserByAlertgroupId(alertgroupId);
     }
 
-
+    /**
+     * for test
+     * @return
+     */
+    public AlertMapper getAlertMapper() {
+        return alertMapper;
+    }
 }
diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java
index 199fed0..51c56fd 100644
--- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java
+++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java
@@ -51,6 +51,7 @@ public class ConnectionFactory extends SpringConnectionFactory {
 
     private ConnectionFactory() {
         try {
+            dataSource = buildDataSource();
             sqlSessionFactory = getSqlSessionFactory();
             sqlSessionTemplate = getSqlSessionTemplate();
         } catch (Exception e) {
@@ -69,12 +70,18 @@ public class ConnectionFactory extends SpringConnectionFactory {
      */
     private SqlSessionTemplate sqlSessionTemplate;
 
+    private DataSource dataSource;
+
+    public DataSource getDataSource() {
+        return dataSource;
+    }
+
     /**
      * get the data source
      *
      * @return druid dataSource
      */
-    public DruidDataSource getDataSource() {
+    private DataSource buildDataSource() {
 
         DruidDataSource druidDataSource = new DruidDataSource();
 
@@ -112,10 +119,9 @@ public class ConnectionFactory extends SpringConnectionFactory {
      * @throws Exception sqlSessionFactory exception
      */
     private SqlSessionFactory getSqlSessionFactory() throws Exception {
-        DataSource dataSource = getDataSource();
         TransactionFactory transactionFactory = new JdbcTransactionFactory();
 
-        Environment environment = new Environment("development", transactionFactory, dataSource);
+        Environment environment = new Environment("development", transactionFactory, getDataSource());
 
         MybatisConfiguration configuration = new MybatisConfiguration();
         configuration.setEnvironment(environment);
@@ -125,7 +131,7 @@ public class ConnectionFactory extends SpringConnectionFactory {
 
         MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
         sqlSessionFactoryBean.setConfiguration(configuration);
-        sqlSessionFactoryBean.setDataSource(dataSource);
+        sqlSessionFactoryBean.setDataSource(getDataSource());
 
         sqlSessionFactoryBean.setTypeEnumsPackage("org.apache.dolphinscheduler.*.enums");
         sqlSessionFactory = sqlSessionFactoryBean.getObject();
diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/UpgradeDao.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/UpgradeDao.java
index 384c360..3552d4e 100644
--- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/UpgradeDao.java
+++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/UpgradeDao.java
@@ -27,6 +27,7 @@ import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.sql.DataSource;
 import java.io.*;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
@@ -40,7 +41,7 @@ public abstract class UpgradeDao extends AbstractBaseDao {
     private static final String T_VERSION_NAME = "t_escheduler_version";
     private static final String T_NEW_VERSION_NAME = "t_ds_version";
     private static final String rootDir = System.getProperty("user.dir");
-    protected static final DruidDataSource dataSource = getDataSource();
+    protected static final DataSource dataSource = getDataSource();
     private static final DbType dbType = getCurrentDbType();
 
     @Override
@@ -52,13 +53,8 @@ public abstract class UpgradeDao extends AbstractBaseDao {
      * get datasource
      * @return DruidDataSource
      */
-    public static DruidDataSource getDataSource(){
-        DruidDataSource dataSource = ConnectionFactory.getInstance().getDataSource();
-        dataSource.setInitialSize(2);
-        dataSource.setMinIdle(2);
-        dataSource.setMaxActive(2);
-
-        return dataSource;
+    public static DataSource getDataSource(){
+        return ConnectionFactory.getInstance().getDataSource();
     }
 
     /**
diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/AlertDaoTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/AlertDaoTest.java
index b4f197a..b94162c 100644
--- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/AlertDaoTest.java
+++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/AlertDaoTest.java
@@ -16,19 +16,42 @@
  */
 package org.apache.dolphinscheduler.dao;
 
+import org.apache.dolphinscheduler.common.enums.AlertStatus;
+import org.apache.dolphinscheduler.common.enums.AlertType;
+import org.apache.dolphinscheduler.common.enums.ShowType;
+import org.apache.dolphinscheduler.dao.entity.Alert;
 import org.junit.Assert;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class AlertDaoTest {
-    private static final Logger logger = LoggerFactory.getLogger(AlertDaoTest.class);
+import java.util.Arrays;
+import java.util.List;
 
+public class AlertDaoTest {
     @Test
-    public void testGetAlertDao() {
-        logger.info("testGetAlertDao start");
+    public void testAlertDao(){
         AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class);
-        Assert.assertNotNull(alertDao);
-        logger.info("testGetAlertDao end");
+        Alert alert = new Alert();
+        alert.setTitle("Mysql Exception");
+        alert.setShowType(ShowType.TEXT);
+        alert.setContent("[\"alarm time:2018-02-05\", \"service name:MYSQL_ALTER\", \"alarm name:MYSQL_ALTER_DUMP\", " +
+                "\"get the alarm exception.!,interface error,exception information:timed out\", \"request address:http://blog.csdn.net/dreamInTheWorld/article/details/78539286\"]");
+        alert.setAlertType(AlertType.EMAIL);
+        alert.setAlertGroupId(1);
+        alert.setAlertStatus(AlertStatus.WAIT_EXECUTION);
+        alertDao.addAlert(alert);
+
+
+        List<Alert> alerts = alertDao.listWaitExecutionAlert();
+        Assert.assertNotNull(alerts);
+        Assert.assertNotEquals(0, alerts.size());
+        int id = alerts.get(0).getId();
+        AlertStatus alertStatus = alerts.get(0).getAlertStatus();
+        alertDao.updateAlert(AlertStatus.EXECUTION_SUCCESS, "", id);
+
+        alerts = alertDao.listWaitExecutionAlert();
+        Assert.assertEquals(0, alerts.size());
+        alertDao.getAlertMapper().deleteById(id);
     }
 }
diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java
index f413944..1d419a8 100644
--- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java
+++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java
@@ -31,7 +31,7 @@ public class ConnectionFactoryTest {
      */
     @Test
     public void testConnection()throws Exception{
-        Connection connection = ConnectionFactory.getInstance().getDataSource().getPooledConnection().getConnection();
+        Connection connection = ConnectionFactory.getInstance().getDataSource().getConnection();
         Assert.assertTrue(connection != null);
     }
 }
\ No newline at end of file