Skip to content

Commit

Permalink
Add dolphinscheduler-dao-plugin module
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanwenjun committed Oct 14, 2023
1 parent 0a2cd61 commit c71fcc3
Show file tree
Hide file tree
Showing 31 changed files with 867 additions and 365 deletions.
6 changes: 6 additions & 0 deletions dolphinscheduler-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.dolphinscheduler</groupId>
<artifactId>dolphinscheduler-dao</artifactId>
</dependency>

<dependency>
<groupId>org.apache.dolphinscheduler</groupId>
<artifactId>dolphinscheduler-meter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.common.model.Server;
import org.apache.dolphinscheduler.common.model.WorkerServerModel;
import org.apache.dolphinscheduler.dao.MonitorDBDao;
import org.apache.dolphinscheduler.dao.entity.MonitorRecord;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor;
import org.apache.dolphinscheduler.registry.api.RegistryClient;
import org.apache.dolphinscheduler.registry.api.enums.RegistryNodeType;

Expand All @@ -39,6 +38,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
Expand All @@ -49,7 +49,7 @@
public class MonitorServiceImpl extends BaseServiceImpl implements MonitorService {

@Autowired
private MonitorDBDao monitorDBDao;
private DatabaseMonitor databaseMonitor;

@Autowired
private RegistryClient registryClient;
Expand All @@ -63,8 +63,7 @@ public class MonitorServiceImpl extends BaseServiceImpl implements MonitorServic
@Override
public Map<String, Object> queryDatabaseState(User loginUser) {
Map<String, Object> result = new HashMap<>();
List<MonitorRecord> monitorRecordList = monitorDBDao.queryDatabaseState();
result.put(Constants.DATA_LIST, monitorRecordList);
result.put(Constants.DATA_LIST, Lists.newArrayList(databaseMonitor.getDatabaseMetrics()));
putMsg(result, Status.SUCCESS);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
import org.apache.dolphinscheduler.common.enums.AuthorizationType;
import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.common.model.Server;
import org.apache.dolphinscheduler.dao.MonitorDBDao;
import org.apache.dolphinscheduler.dao.entity.MonitorRecord;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMetrics;
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor;
import org.apache.dolphinscheduler.registry.api.RegistryClient;
import org.apache.dolphinscheduler.registry.api.enums.RegistryNodeType;
import org.apache.dolphinscheduler.spi.enums.DbType;

import org.apache.commons.collections4.CollectionUtils;

Expand All @@ -53,6 +53,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baomidou.mybatisplus.annotation.DbType;

/**
* monitor service test
*/
Expand All @@ -66,7 +68,7 @@ public class MonitorServiceTest {
private MonitorServiceImpl monitorService;

@Mock
private MonitorDBDao monitorDBDao;
private DatabaseMonitor databaseMonitor;

@Mock
private ResourcePermissionCheckService resourcePermissionCheckService;
Expand All @@ -88,7 +90,7 @@ public void init() {
@Test
public void testQueryDatabaseState() {
mockPermissionCheck(ApiFuncIdentificationConstant.MONITOR_DATABASES_VIEW, true);
Mockito.when(monitorDBDao.queryDatabaseState()).thenReturn(getList());
Mockito.when(databaseMonitor.getDatabaseMetrics()).thenReturn(getDatabaseMetrics());
Map<String, Object> result = monitorService.queryDatabaseState(user);
logger.info(result.toString());
Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
Expand Down Expand Up @@ -138,14 +140,8 @@ private void mockPermissionCheck(String permissionKey, boolean result) {
serviceLogger)).thenReturn(true);
}

private List<MonitorRecord> getList() {
List<MonitorRecord> monitorRecordList = new ArrayList<>();
monitorRecordList.add(getEntity());
return monitorRecordList;
}

private MonitorRecord getEntity() {
MonitorRecord monitorRecord = new MonitorRecord();
private DatabaseMetrics getDatabaseMetrics() {
DatabaseMetrics monitorRecord = new DatabaseMetrics();
monitorRecord.setDbType(DbType.MYSQL);
return monitorRecord;
}
Expand Down
7 changes: 6 additions & 1 deletion dolphinscheduler-api/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
#

spring:
profiles:
active: h2
main:
banner-mode: off
sql:
init:
schema-locations: classpath:sql/dolphinscheduler_h2.sql
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:dolphinscheduler;MODE=MySQL;DB_CLOSE_DELAY=-1;DATABASE_TO_LOWER=true;INIT=runscript from 'classpath:sql/dolphinscheduler_h2.sql'
url: jdbc:h2:mem:dolphinscheduler;MODE=MySQL;DB_CLOSE_DELAY=-1;DATABASE_TO_LOWER=true;
username: sa
password: ""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

@Slf4j
public class ClasspathSqlScriptParser implements SqlScriptParser {

private final String sqlScriptPath;
Expand All @@ -46,6 +50,10 @@ public ClasspathSqlScriptParser(String sqlScriptPath) {
@Override
public List<String> getAllSql() throws IOException {
Resource sqlScriptResource = new ClassPathResource(sqlScriptPath);
if (!sqlScriptResource.exists()) {
log.warn("The sql script file {} doesn't exist", sqlScriptPath);
return Collections.emptyList();
}
List<String> result = new ArrayList<>();
try (
InputStream inputStream = sqlScriptResource.getInputStream();
Expand Down
46 changes: 46 additions & 0 deletions dolphinscheduler-dao-plugin/dolphinscheduler-dao-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.dolphinscheduler</groupId>
<artifactId>dolphinscheduler-dao-plugin</artifactId>
<version>dev-SNAPSHOT</version>
</parent>

<artifactId>dolphinscheduler-dao-api</artifactId>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

package org.apache.dolphinscheduler.dao.plugin.api;

import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor;

import com.baomidou.mybatisplus.annotation.DbType;

/**
* DaoPluginConfiguration used to configure the dao plugin.
*/
public interface DaoPluginConfiguration {

DbType dbType();

DatabaseMonitor databaseMonitor();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

package org.apache.dolphinscheduler.dao.plugin.api.monitor;

import java.util.Date;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import com.baomidou.mybatisplus.annotation.DbType;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class DatabaseMetrics {

private DbType dbType;

private DatabaseHealthStatus state;

private long maxConnections;

private long maxUsedConnections;

private long threadsConnections;

private long threadsRunningConnections;

private Date date;

public enum DatabaseHealthStatus {
YES,
NO
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

package org.apache.dolphinscheduler.dao.plugin.api.monitor;

public interface DatabaseMonitor {

DatabaseMetrics getDatabaseMetrics();

}
42 changes: 42 additions & 0 deletions dolphinscheduler-dao-plugin/dolphinscheduler-dao-h2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.dolphinscheduler</groupId>
<artifactId>dolphinscheduler-dao-plugin</artifactId>
<version>dev-SNAPSHOT</version>
</parent>

<artifactId>dolphinscheduler-dao-h2</artifactId>

<dependencies>

<dependency>
<groupId>org.apache.dolphinscheduler</groupId>
<artifactId>dolphinscheduler-dao-api</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>

</project>
Loading

0 comments on commit c71fcc3

Please sign in to comment.