Wenjun Ruan
7 months ago
committed by
GitHub
35 changed files with 875 additions and 365 deletions
@ -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> |
@ -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(); |
||||||
|
|
||||||
|
} |
@ -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 |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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(); |
||||||
|
|
||||||
|
} |
@ -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> |
@ -0,0 +1,52 @@ |
|||||||
|
/* |
||||||
|
* 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.h2; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.dao.plugin.api.DaoPluginConfiguration; |
||||||
|
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor; |
||||||
|
import org.apache.dolphinscheduler.dao.plugin.h2.monitor.H2Monitor; |
||||||
|
|
||||||
|
import javax.sql.DataSource; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.context.annotation.Profile; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.DbType; |
||||||
|
|
||||||
|
@Profile("h2") |
||||||
|
@Configuration |
||||||
|
public class H2DaoPluginConfiguration implements DaoPluginConfiguration { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private DataSource dataSource; |
||||||
|
|
||||||
|
@Override |
||||||
|
public DbType dbType() { |
||||||
|
return DbType.H2; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public DatabaseMonitor databaseMonitor() { |
||||||
|
return new H2Monitor(dataSource); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,71 @@ |
|||||||
|
/* |
||||||
|
* 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.h2.monitor; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMetrics; |
||||||
|
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor; |
||||||
|
|
||||||
|
import java.sql.Connection; |
||||||
|
import java.sql.ResultSet; |
||||||
|
import java.sql.Statement; |
||||||
|
import java.util.Date; |
||||||
|
|
||||||
|
import javax.sql.DataSource; |
||||||
|
|
||||||
|
import lombok.SneakyThrows; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.DbType; |
||||||
|
|
||||||
|
public class H2Monitor implements DatabaseMonitor { |
||||||
|
|
||||||
|
private final DataSource dataSource; |
||||||
|
|
||||||
|
public H2Monitor(DataSource dataSource) { |
||||||
|
this.dataSource = dataSource; |
||||||
|
} |
||||||
|
|
||||||
|
@SneakyThrows |
||||||
|
@Override |
||||||
|
public DatabaseMetrics getDatabaseMetrics() { |
||||||
|
DatabaseMetrics monitorRecord = new DatabaseMetrics(); |
||||||
|
monitorRecord.setDate(new Date()); |
||||||
|
monitorRecord.setDbType(DbType.H2); |
||||||
|
monitorRecord.setState(DatabaseMetrics.DatabaseHealthStatus.YES); |
||||||
|
|
||||||
|
try ( |
||||||
|
Connection connection = dataSource.getConnection(); |
||||||
|
Statement pstmt = connection.createStatement()) { |
||||||
|
|
||||||
|
try ( |
||||||
|
ResultSet rs1 = pstmt |
||||||
|
.executeQuery("select count(1) as total from information_schema.sessions;")) { |
||||||
|
if (rs1.next()) { |
||||||
|
int currentSessions = rs1.getInt("total"); |
||||||
|
monitorRecord.setThreadsConnections(currentSessions); |
||||||
|
monitorRecord.setMaxUsedConnections(currentSessions); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
return monitorRecord; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,41 @@ |
|||||||
|
<?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-mysql</artifactId> |
||||||
|
|
||||||
|
<dependencies> |
||||||
|
<dependency> |
||||||
|
<groupId>org.apache.dolphinscheduler</groupId> |
||||||
|
<artifactId>dolphinscheduler-dao-api</artifactId> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>mysql</groupId> |
||||||
|
<artifactId>mysql-connector-java</artifactId> |
||||||
|
</dependency> |
||||||
|
</dependencies> |
||||||
|
|
||||||
|
</project> |
@ -0,0 +1,50 @@ |
|||||||
|
/* |
||||||
|
* 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.mysql; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.dao.plugin.api.DaoPluginConfiguration; |
||||||
|
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor; |
||||||
|
import org.apache.dolphinscheduler.dao.plugin.mysql.monitor.MysqlMonitor; |
||||||
|
|
||||||
|
import javax.sql.DataSource; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.context.annotation.Profile; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.DbType; |
||||||
|
|
||||||
|
@Profile("mysql") |
||||||
|
@Configuration |
||||||
|
public class MysqlDaoPluginConfiguration implements DaoPluginConfiguration { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private DataSource dataSource; |
||||||
|
|
||||||
|
@Override |
||||||
|
public DbType dbType() { |
||||||
|
return DbType.MYSQL; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public DatabaseMonitor databaseMonitor() { |
||||||
|
return new MysqlMonitor(dataSource); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,79 @@ |
|||||||
|
/* |
||||||
|
* 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.mysql.monitor; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMetrics; |
||||||
|
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor; |
||||||
|
|
||||||
|
import java.sql.Connection; |
||||||
|
import java.sql.ResultSet; |
||||||
|
import java.sql.Statement; |
||||||
|
import java.util.Date; |
||||||
|
|
||||||
|
import javax.sql.DataSource; |
||||||
|
|
||||||
|
import lombok.SneakyThrows; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.DbType; |
||||||
|
|
||||||
|
public class MysqlMonitor implements DatabaseMonitor { |
||||||
|
|
||||||
|
private final DataSource dataSource; |
||||||
|
|
||||||
|
public MysqlMonitor(DataSource dataSource) { |
||||||
|
this.dataSource = dataSource; |
||||||
|
} |
||||||
|
|
||||||
|
@SneakyThrows |
||||||
|
@Override |
||||||
|
public DatabaseMetrics getDatabaseMetrics() { |
||||||
|
DatabaseMetrics monitorRecord = new DatabaseMetrics(); |
||||||
|
monitorRecord.setDate(new Date()); |
||||||
|
monitorRecord.setDbType(DbType.MYSQL); |
||||||
|
monitorRecord.setState(DatabaseMetrics.DatabaseHealthStatus.YES); |
||||||
|
|
||||||
|
try ( |
||||||
|
Connection connection = dataSource.getConnection(); |
||||||
|
Statement pstmt = connection.createStatement()) { |
||||||
|
|
||||||
|
try (ResultSet rs1 = pstmt.executeQuery("show global variables")) { |
||||||
|
while (rs1.next()) { |
||||||
|
if ("MAX_CONNECTIONS".equalsIgnoreCase(rs1.getString("variable_name"))) { |
||||||
|
monitorRecord.setMaxConnections(Long.parseLong(rs1.getString("value"))); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
try (ResultSet rs2 = pstmt.executeQuery("show global status")) { |
||||||
|
while (rs2.next()) { |
||||||
|
if ("MAX_USED_CONNECTIONS".equalsIgnoreCase(rs2.getString("variable_name"))) { |
||||||
|
monitorRecord.setMaxUsedConnections(Long.parseLong(rs2.getString("value"))); |
||||||
|
} else if ("THREADS_CONNECTED".equalsIgnoreCase(rs2.getString("variable_name"))) { |
||||||
|
monitorRecord.setThreadsConnections(Long.parseLong(rs2.getString("value"))); |
||||||
|
} else if ("THREADS_RUNNING".equalsIgnoreCase(rs2.getString("variable_name"))) { |
||||||
|
monitorRecord.setThreadsRunningConnections(Long.parseLong(rs2.getString("value"))); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return monitorRecord; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,56 @@ |
|||||||
|
<?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-plugin-all</artifactId> |
||||||
|
|
||||||
|
<dependencies> |
||||||
|
<dependency> |
||||||
|
<groupId>org.apache.dolphinscheduler</groupId> |
||||||
|
<artifactId>dolphinscheduler-dao-api</artifactId> |
||||||
|
<version>${project.version}</version> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.apache.dolphinscheduler</groupId> |
||||||
|
<artifactId>dolphinscheduler-dao-h2</artifactId> |
||||||
|
<version>${project.version}</version> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.apache.dolphinscheduler</groupId> |
||||||
|
<artifactId>dolphinscheduler-dao-postgresql</artifactId> |
||||||
|
<version>${project.version}</version> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.apache.dolphinscheduler</groupId> |
||||||
|
<artifactId>dolphinscheduler-dao-mysql</artifactId> |
||||||
|
<version>${project.version}</version> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
</dependencies> |
||||||
|
|
||||||
|
</project> |
@ -0,0 +1,41 @@ |
|||||||
|
<?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-postgresql</artifactId> |
||||||
|
|
||||||
|
<dependencies> |
||||||
|
<dependency> |
||||||
|
<groupId>org.apache.dolphinscheduler</groupId> |
||||||
|
<artifactId>dolphinscheduler-dao-api</artifactId> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.postgresql</groupId> |
||||||
|
<artifactId>postgresql</artifactId> |
||||||
|
</dependency> |
||||||
|
</dependencies> |
||||||
|
|
||||||
|
</project> |
@ -0,0 +1,51 @@ |
|||||||
|
/* |
||||||
|
* 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.postgresql; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.dao.plugin.api.DaoPluginConfiguration; |
||||||
|
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor; |
||||||
|
import org.apache.dolphinscheduler.dao.plugin.postgresql.monitor.PostgresqlMonitor; |
||||||
|
|
||||||
|
import javax.sql.DataSource; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.context.annotation.Profile; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.DbType; |
||||||
|
|
||||||
|
@Profile("postgresql") |
||||||
|
@Configuration |
||||||
|
public class PostgresqlDaoPluginConfiguration implements DaoPluginConfiguration { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private DataSource dataSource; |
||||||
|
|
||||||
|
@Override |
||||||
|
public DbType dbType() { |
||||||
|
return DbType.POSTGRE_SQL; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public DatabaseMonitor databaseMonitor() { |
||||||
|
return new PostgresqlMonitor(dataSource); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,79 @@ |
|||||||
|
/* |
||||||
|
* 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.postgresql.monitor; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMetrics; |
||||||
|
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor; |
||||||
|
|
||||||
|
import java.sql.Connection; |
||||||
|
import java.sql.ResultSet; |
||||||
|
import java.sql.Statement; |
||||||
|
import java.util.Date; |
||||||
|
|
||||||
|
import javax.sql.DataSource; |
||||||
|
|
||||||
|
import lombok.SneakyThrows; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.DbType; |
||||||
|
|
||||||
|
public class PostgresqlMonitor implements DatabaseMonitor { |
||||||
|
|
||||||
|
private final DataSource dataSource; |
||||||
|
|
||||||
|
public PostgresqlMonitor(DataSource dataSource) { |
||||||
|
this.dataSource = dataSource; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
@SneakyThrows |
||||||
|
public DatabaseMetrics getDatabaseMetrics() { |
||||||
|
DatabaseMetrics monitorRecord = new DatabaseMetrics(); |
||||||
|
monitorRecord.setDate(new Date()); |
||||||
|
monitorRecord.setState(DatabaseMetrics.DatabaseHealthStatus.YES); |
||||||
|
monitorRecord.setDbType(DbType.POSTGRE_SQL); |
||||||
|
|
||||||
|
try ( |
||||||
|
Connection connection = dataSource.getConnection(); |
||||||
|
Statement pstmt = connection.createStatement()) { |
||||||
|
|
||||||
|
try (ResultSet rs1 = pstmt.executeQuery("select count(*) from pg_stat_activity;")) { |
||||||
|
if (rs1.next()) { |
||||||
|
monitorRecord.setThreadsConnections(rs1.getInt("count")); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
try (ResultSet rs2 = pstmt.executeQuery("show max_connections")) { |
||||||
|
if (rs2.next()) { |
||||||
|
monitorRecord.setMaxConnections(rs2.getInt("max_connections")); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
try ( |
||||||
|
ResultSet rs3 = |
||||||
|
pstmt.executeQuery("select count(*) from pg_stat_activity pg where pg.state = 'active';")) { |
||||||
|
if (rs3.next()) { |
||||||
|
monitorRecord.setThreadsRunningConnections(rs3.getInt("count")); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return monitorRecord; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,50 @@ |
|||||||
|
<?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</artifactId> |
||||||
|
<version>dev-SNAPSHOT</version> |
||||||
|
</parent> |
||||||
|
|
||||||
|
<artifactId>dolphinscheduler-dao-plugin</artifactId> |
||||||
|
<packaging>pom</packaging> |
||||||
|
|
||||||
|
<modules> |
||||||
|
<module>dolphinscheduler-dao-api</module> |
||||||
|
<module>dolphinscheduler-dao-plugin-all</module> |
||||||
|
<module>dolphinscheduler-dao-h2</module> |
||||||
|
<module>dolphinscheduler-dao-mysql</module> |
||||||
|
<module>dolphinscheduler-dao-postgresql</module> |
||||||
|
</modules> |
||||||
|
|
||||||
|
<dependencyManagement> |
||||||
|
<dependencies> |
||||||
|
<dependency> |
||||||
|
<groupId>org.apache.dolphinscheduler</groupId> |
||||||
|
<artifactId>dolphinscheduler-bom</artifactId> |
||||||
|
<version>${project.version}</version> |
||||||
|
<type>pom</type> |
||||||
|
<scope>import</scope> |
||||||
|
</dependency> |
||||||
|
</dependencies> |
||||||
|
</dependencyManagement> |
||||||
|
|
||||||
|
</project> |
@ -1,77 +0,0 @@ |
|||||||
/* |
|
||||||
* 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; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.dao.entity.MonitorRecord; |
|
||||||
import org.apache.dolphinscheduler.dao.utils.H2Performance; |
|
||||||
import org.apache.dolphinscheduler.dao.utils.MySQLPerformance; |
|
||||||
import org.apache.dolphinscheduler.dao.utils.PostgreSQLPerformance; |
|
||||||
import org.apache.dolphinscheduler.spi.enums.DbType; |
|
||||||
|
|
||||||
import java.sql.Connection; |
|
||||||
import java.sql.DriverManager; |
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
import javax.sql.DataSource; |
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j; |
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
import org.springframework.stereotype.Component; |
|
||||||
|
|
||||||
@Component |
|
||||||
@Slf4j |
|
||||||
public class MonitorDBDao { |
|
||||||
|
|
||||||
public static final String VARIABLE_NAME = "variable_name"; |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private DataSource dataSource; |
|
||||||
|
|
||||||
private MonitorRecord getCurrentDbPerformance() { |
|
||||||
try (final Connection conn = dataSource.getConnection()) { |
|
||||||
String driverClassName = DriverManager.getDriver(conn.getMetaData().getURL()).getClass().getName(); |
|
||||||
if (driverClassName.contains(DbType.MYSQL.toString().toLowerCase())) { |
|
||||||
return new MySQLPerformance().getMonitorRecord(conn); |
|
||||||
} else if (driverClassName.contains(DbType.POSTGRESQL.toString().toLowerCase())) { |
|
||||||
return new PostgreSQLPerformance().getMonitorRecord(conn); |
|
||||||
} else if (driverClassName.contains(DbType.H2.toString().toLowerCase())) { |
|
||||||
return new H2Performance().getMonitorRecord(conn); |
|
||||||
} |
|
||||||
} catch (Exception e) { |
|
||||||
log.error("SQLException: {}", e.getMessage(), e); |
|
||||||
} |
|
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* query database state |
|
||||||
* |
|
||||||
* @return MonitorRecord list |
|
||||||
*/ |
|
||||||
public List<MonitorRecord> queryDatabaseState() { |
|
||||||
List<MonitorRecord> list = new ArrayList<>(1); |
|
||||||
|
|
||||||
MonitorRecord monitorRecord = getCurrentDbPerformance(); |
|
||||||
if (monitorRecord != null) { |
|
||||||
list.add(monitorRecord); |
|
||||||
} |
|
||||||
return list; |
|
||||||
} |
|
||||||
} |
|
@ -1,65 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.utils; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.enums.Flag; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.MonitorRecord; |
|
||||||
import org.apache.dolphinscheduler.spi.enums.DbType; |
|
||||||
|
|
||||||
import java.sql.Connection; |
|
||||||
import java.sql.ResultSet; |
|
||||||
import java.sql.SQLException; |
|
||||||
import java.sql.Statement; |
|
||||||
import java.util.Date; |
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j; |
|
||||||
|
|
||||||
/** |
|
||||||
* H2 MEMORY DB Performance Monitor |
|
||||||
*/ |
|
||||||
@Slf4j |
|
||||||
public class H2Performance extends BaseDBPerformance { |
|
||||||
|
|
||||||
/** |
|
||||||
* return the current database performance |
|
||||||
* |
|
||||||
* @param conn connection |
|
||||||
* @return MonitorRecord |
|
||||||
*/ |
|
||||||
@Override |
|
||||||
public MonitorRecord getMonitorRecord(Connection conn) { |
|
||||||
MonitorRecord monitorRecord = new MonitorRecord(); |
|
||||||
monitorRecord.setDate(new Date()); |
|
||||||
monitorRecord.setDbType(DbType.H2); |
|
||||||
monitorRecord.setState(Flag.YES); |
|
||||||
|
|
||||||
try (Statement pstmt = conn.createStatement()) { |
|
||||||
try ( |
|
||||||
ResultSet rs1 = pstmt |
|
||||||
.executeQuery("select count(1) as total from information_schema.sessions;")) { |
|
||||||
if (rs1.next()) { |
|
||||||
monitorRecord.setThreadsConnections(rs1.getInt("total")); |
|
||||||
} |
|
||||||
} |
|
||||||
} catch (SQLException e) { |
|
||||||
monitorRecord.setState(Flag.NO); |
|
||||||
log.error("SQLException ", e); |
|
||||||
} |
|
||||||
return monitorRecord; |
|
||||||
} |
|
||||||
} |
|
@ -1,78 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.utils; |
|
||||||
|
|
||||||
import static org.apache.dolphinscheduler.dao.MonitorDBDao.VARIABLE_NAME; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.enums.Flag; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.MonitorRecord; |
|
||||||
import org.apache.dolphinscheduler.spi.enums.DbType; |
|
||||||
|
|
||||||
import java.sql.Connection; |
|
||||||
import java.sql.ResultSet; |
|
||||||
import java.sql.Statement; |
|
||||||
import java.util.Date; |
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j; |
|
||||||
|
|
||||||
/** |
|
||||||
* MySQL performance |
|
||||||
*/ |
|
||||||
@Slf4j |
|
||||||
public class MySQLPerformance extends BaseDBPerformance { |
|
||||||
|
|
||||||
/** |
|
||||||
* get monitor record |
|
||||||
* @param conn connection |
|
||||||
* @return MonitorRecord |
|
||||||
*/ |
|
||||||
@Override |
|
||||||
public MonitorRecord getMonitorRecord(Connection conn) { |
|
||||||
MonitorRecord monitorRecord = new MonitorRecord(); |
|
||||||
monitorRecord.setDate(new Date()); |
|
||||||
monitorRecord.setDbType(DbType.MYSQL); |
|
||||||
monitorRecord.setState(Flag.YES); |
|
||||||
|
|
||||||
try (Statement pstmt = conn.createStatement()) { |
|
||||||
try (ResultSet rs1 = pstmt.executeQuery("show global variables")) { |
|
||||||
while (rs1.next()) { |
|
||||||
if ("MAX_CONNECTIONS".equalsIgnoreCase(rs1.getString(VARIABLE_NAME))) { |
|
||||||
monitorRecord.setMaxConnections(Long.parseLong(rs1.getString("value"))); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
try (ResultSet rs2 = pstmt.executeQuery("show global status")) { |
|
||||||
while (rs2.next()) { |
|
||||||
if ("MAX_USED_CONNECTIONS".equalsIgnoreCase(rs2.getString(VARIABLE_NAME))) { |
|
||||||
monitorRecord.setMaxUsedConnections(Long.parseLong(rs2.getString("value"))); |
|
||||||
} else if ("THREADS_CONNECTED".equalsIgnoreCase(rs2.getString(VARIABLE_NAME))) { |
|
||||||
monitorRecord.setThreadsConnections(Long.parseLong(rs2.getString("value"))); |
|
||||||
} else if ("THREADS_RUNNING".equalsIgnoreCase(rs2.getString(VARIABLE_NAME))) { |
|
||||||
monitorRecord.setThreadsRunningConnections(Long.parseLong(rs2.getString("value"))); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} catch (Exception e) { |
|
||||||
monitorRecord.setState(Flag.NO); |
|
||||||
log.error("SQLException ", e); |
|
||||||
} |
|
||||||
return monitorRecord; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -1,73 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.utils; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.enums.Flag; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.MonitorRecord; |
|
||||||
import org.apache.dolphinscheduler.spi.enums.DbType; |
|
||||||
|
|
||||||
import java.sql.Connection; |
|
||||||
import java.sql.ResultSet; |
|
||||||
import java.sql.Statement; |
|
||||||
import java.util.Date; |
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j; |
|
||||||
|
|
||||||
@Slf4j |
|
||||||
public class PostgreSQLPerformance extends BaseDBPerformance { |
|
||||||
|
|
||||||
/** |
|
||||||
* get monitor record |
|
||||||
* |
|
||||||
* @param conn connection |
|
||||||
* @return MonitorRecord |
|
||||||
*/ |
|
||||||
@Override |
|
||||||
public MonitorRecord getMonitorRecord(Connection conn) { |
|
||||||
MonitorRecord monitorRecord = new MonitorRecord(); |
|
||||||
monitorRecord.setDate(new Date()); |
|
||||||
monitorRecord.setState(Flag.YES); |
|
||||||
monitorRecord.setDbType(DbType.POSTGRESQL); |
|
||||||
|
|
||||||
try (Statement pstmt = conn.createStatement()) { |
|
||||||
try (ResultSet rs1 = pstmt.executeQuery("select count(*) from pg_stat_activity;")) { |
|
||||||
if (rs1.next()) { |
|
||||||
monitorRecord.setThreadsConnections(rs1.getInt("count")); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
try (ResultSet rs2 = pstmt.executeQuery("show max_connections")) { |
|
||||||
if (rs2.next()) { |
|
||||||
monitorRecord.setMaxConnections(rs2.getInt("max_connections")); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
try ( |
|
||||||
ResultSet rs3 = |
|
||||||
pstmt.executeQuery("select count(*) from pg_stat_activity pg where pg.state = 'active';")) { |
|
||||||
if (rs3.next()) { |
|
||||||
monitorRecord.setThreadsRunningConnections(rs3.getInt("count")); |
|
||||||
} |
|
||||||
} |
|
||||||
} catch (Exception e) { |
|
||||||
monitorRecord.setState(Flag.NO); |
|
||||||
log.error("SQLException ", e); |
|
||||||
} |
|
||||||
return monitorRecord; |
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue