Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improve] add connection check when get connection #2333

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@

package org.apache.hertzbeat.collector.collect.common.cache;

import lombok.extern.slf4j.Slf4j;

/**
* AbstractConnection
*/
@Slf4j
public abstract class AbstractConnection<T> implements AutoCloseable {

public abstract class AbstractConnection<T> implements AutoCloseable {

/**
* @return Returns the connection.
Expand All @@ -35,8 +33,15 @@ public abstract class AbstractConnection<T> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning null here and printing the logs would probably block the @tomsun28

}
return connection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.mongodb.client.MongoClient;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;

/**
* mongodb connect client
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> getConnection() {

try {
this.check();
}
catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return connection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,22 +236,26 @@ private StatefulRedisClusterConnection<String, String> getClusterConnection(Redi
* @return connection
*/
private StatefulConnection<String, String> getStatefulConnection(CacheIdentifier identifier) {
StatefulConnection<String, String> connection = null;

Optional<RedisConnect> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public Object getConnection() {
@Override
public void closeConnection() throws Exception {
}

@Override
public void check() throws Exception {
}
};
}

Expand Down