From c1643039f4f954e5ee0ba7bf9835b68b499c30cb Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Mon, 22 Jul 2024 11:37:42 +0800 Subject: [PATCH 1/2] [Improve] add connection check when get connection Signed-off-by: yuluo-yx --- .../common/cache/AbstractConnection.java | 15 +++++++++----- .../collect/common/cache/JdbcConnect.java | 16 +++++++++++++++ .../collect/common/cache/JmxConnect.java | 15 ++++++++++++++ .../collect/common/cache/MongodbConnect.java | 15 ++++++++++++++ .../collect/common/cache/RedfishConnect.java | 15 ++++++++++++++ .../collect/common/cache/RedisConnect.java | 15 ++++++++++++++ .../collect/common/cache/SshConnect.java | 15 ++++++++++++++ .../collect/redis/RedisCommonCollectImpl.java | 20 +++++++++++-------- .../collect/common/cache/CommonCacheTest.java | 4 ++++ 9 files changed, 117 insertions(+), 13 deletions(-) diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/AbstractConnection.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/AbstractConnection.java index 7b3ce9f65e7..082ac90b8e2 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/AbstractConnection.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/AbstractConnection.java @@ -17,13 +17,11 @@ package org.apache.hertzbeat.collector.collect.common.cache; -import lombok.extern.slf4j.Slf4j; - /** * AbstractConnection */ -@Slf4j -public abstract class AbstractConnection implements AutoCloseable { + +public abstract class AbstractConnection implements AutoCloseable { /** * @return Returns the connection. @@ -35,8 +33,15 @@ public abstract class AbstractConnection implements AutoCloseable { */ public abstract void closeConnection() throws Exception; + /** + * Check connection when get connection. + */ + public abstract void check() throws Exception; + @Override public void close() throws Exception{ - closeConnection(); + + this.closeConnection(); } + } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JdbcConnect.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JdbcConnect.java index ad828871c2b..b66d807ca3b 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JdbcConnect.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JdbcConnect.java @@ -18,6 +18,7 @@ package org.apache.hertzbeat.collector.collect.common.cache; import java.sql.Connection; +import java.sql.SQLException; import lombok.extern.slf4j.Slf4j; /** @@ -39,8 +40,23 @@ public void closeConnection() throws Exception { } } + @Override + public void check() throws SQLException { + + if (connection.isClosed()) { + throw new SQLException("Connection is closed"); + } + } + @Override public Connection getConnection() { + + try { + this.check(); + } + catch (SQLException e) { + throw new RuntimeException(e.getMessage()); + } return connection; } } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JmxConnect.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JmxConnect.java index f0e03602c26..696bf0d05e2 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JmxConnect.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JmxConnect.java @@ -40,8 +40,23 @@ public void closeConnection() throws Exception { } } + @Override + public void check() throws Exception { + + if (connection.getConnectionId().isEmpty()) { + throw new RuntimeException("connection is closed"); + } + } + @Override public JMXConnector getConnection() { + + try { + this.check(); + } + catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } return connection; } } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/MongodbConnect.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/MongodbConnect.java index 73e1e920863..c41c30fa84f 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/MongodbConnect.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/MongodbConnect.java @@ -19,6 +19,7 @@ import com.mongodb.client.MongoClient; import lombok.extern.slf4j.Slf4j; +import org.bson.Document; /** * mongodb connect client @@ -38,8 +39,22 @@ public void closeConnection() throws Exception { } } + @Override + public void check() throws Exception { + + mongoClient.getDatabase("admin").runCommand(new Document("ping", 1)); + } + @Override public MongoClient getConnection() { + + try { + this.check(); + } + catch (Exception e) { + throw new RuntimeException("Connection is closed"); + } + return mongoClient; } } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedfishConnect.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedfishConnect.java index b0750f72cf8..9b46f0890a2 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedfishConnect.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedfishConnect.java @@ -38,8 +38,23 @@ public void closeConnection() throws Exception { } } + @Override + public void check() throws Exception { + + if (!reddishConnectSession.isOpen()) { + throw new RuntimeException("Connection is closed"); + } + } + @Override public ConnectSession getConnection() { + + try { + this.check(); + } + catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } return reddishConnectSession; } } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedisConnect.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedisConnect.java index e24be18a542..1507229b15b 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedisConnect.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedisConnect.java @@ -39,8 +39,23 @@ public void closeConnection() throws Exception { } } + @Override + public void check() throws Exception { + + if (!connection.isOpen()) { + throw new RuntimeException("Connection is closed"); + } + } + @Override public StatefulConnection getConnection() { + + try { + this.check(); + } + catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } return connection; } } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/SshConnect.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/SshConnect.java index 5e05aada1ce..30f301d3fa6 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/SshConnect.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/SshConnect.java @@ -38,7 +38,22 @@ public void closeConnection() throws Exception { } } + @Override + public void check() throws Exception { + + if (!clientSession.isOpen()) { + throw new Exception("ssh connection is not open"); + } + } + public ClientSession getConnection() { + + try { + this.check(); + } + catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } return clientSession; } } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/redis/RedisCommonCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/redis/RedisCommonCollectImpl.java index ef4a5bbb230..a4eced347d3 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/redis/RedisCommonCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/redis/RedisCommonCollectImpl.java @@ -236,22 +236,26 @@ private StatefulRedisClusterConnection getClusterConnection(Redi * @return connection */ private StatefulConnection getStatefulConnection(CacheIdentifier identifier) { - StatefulConnection connection = null; + Optional cacheOption = connectionCommonCache.getCache(identifier, true); + if (cacheOption.isPresent()) { RedisConnect redisConnect = cacheOption.get(); - connection = redisConnect.getConnection(); - if (!connection.isOpen()) { + + try { + return redisConnect.getConnection(); + } catch (RuntimeException e) { + log.info("The Redis connection from cache is invalid, closing and removing: {}", e.getMessage()); try { - connection.closeAsync(); - } catch (Exception e) { - log.info("The redis connect form cache, close error: {}", e.getMessage()); + redisConnect.getConnection().closeAsync(); + } catch (Exception closeException) { + log.info("Error closing Redis connection: {}", closeException.getMessage()); } - connection = null; connectionCommonCache.removeCache(identifier); } } - return connection; + + return null; } /** diff --git a/collector/src/test/java/org/apache/hertzbeat/collector/collect/common/cache/CommonCacheTest.java b/collector/src/test/java/org/apache/hertzbeat/collector/collect/common/cache/CommonCacheTest.java index 91bcb8e51a8..25f671b107b 100644 --- a/collector/src/test/java/org/apache/hertzbeat/collector/collect/common/cache/CommonCacheTest.java +++ b/collector/src/test/java/org/apache/hertzbeat/collector/collect/common/cache/CommonCacheTest.java @@ -45,6 +45,10 @@ public Object getConnection() { @Override public void closeConnection() throws Exception { } + + @Override + public void check() throws Exception { + } }; } From c5652bc60455282088e439992055e8ee84495ea6 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Tue, 23 Jul 2024 11:33:31 +0800 Subject: [PATCH 2/2] fix Signed-off-by: yuluo-yx --- .../hertzbeat/collector/collect/common/cache/JdbcConnect.java | 3 ++- .../hertzbeat/collector/collect/common/cache/JmxConnect.java | 3 ++- .../collector/collect/common/cache/MongodbConnect.java | 3 ++- .../collector/collect/common/cache/RedfishConnect.java | 3 ++- .../hertzbeat/collector/collect/common/cache/RedisConnect.java | 3 ++- .../hertzbeat/collector/collect/common/cache/SshConnect.java | 3 ++- 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JdbcConnect.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JdbcConnect.java index b66d807ca3b..4f1a4d8065b 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JdbcConnect.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JdbcConnect.java @@ -55,7 +55,8 @@ public Connection getConnection() { this.check(); } catch (SQLException e) { - throw new RuntimeException(e.getMessage()); + log.error(e.getMessage()); + return null; } return connection; } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JmxConnect.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JmxConnect.java index 696bf0d05e2..911ca6c2fbb 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JmxConnect.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/JmxConnect.java @@ -55,7 +55,8 @@ public JMXConnector getConnection() { this.check(); } catch (Exception e) { - throw new RuntimeException(e.getMessage()); + log.error(e.getMessage()); + return null; } return connection; } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/MongodbConnect.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/MongodbConnect.java index c41c30fa84f..0d5afc256da 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/MongodbConnect.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/MongodbConnect.java @@ -52,7 +52,8 @@ public MongoClient getConnection() { this.check(); } catch (Exception e) { - throw new RuntimeException("Connection is closed"); + log.error(e.getMessage()); + return null; } return mongoClient; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedfishConnect.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedfishConnect.java index 9b46f0890a2..4b8233a8e93 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedfishConnect.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedfishConnect.java @@ -53,7 +53,8 @@ public ConnectSession getConnection() { this.check(); } catch (Exception e) { - throw new RuntimeException(e.getMessage()); + log.error(e.getMessage()); + return null; } return reddishConnectSession; } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedisConnect.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedisConnect.java index 1507229b15b..db5f2e2d389 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedisConnect.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/RedisConnect.java @@ -54,7 +54,8 @@ public StatefulConnection getConnection() { this.check(); } catch (Exception e) { - throw new RuntimeException(e.getMessage()); + log.error(e.getMessage()); + return null; } return connection; } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/SshConnect.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/SshConnect.java index 30f301d3fa6..4d6ed801206 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/SshConnect.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/common/cache/SshConnect.java @@ -52,7 +52,8 @@ public ClientSession getConnection() { this.check(); } catch (Exception e) { - throw new RuntimeException(e.getMessage()); + log.error(e.getMessage()); + return null; } return clientSession; }