Skip to content

Commit

Permalink
!12 end to end ping integraty and available check of DBserver when se…
Browse files Browse the repository at this point in the history
…tup backend connection

Merge pull request !12 from chenpingzeng/master
  • Loading branch information
it-is-a-robot authored and gitee-org committed Dec 7, 2020
2 parents 40ffe07 + 5bdb5cc commit 4d06e87
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/main/java/io/mycat/net/FrontendConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,9 @@ public void stmtClose(byte[] data) {
}
}

public void ping() {
public boolean ping(boolean sendResponse) {
write(writeToBuffer(OkPacket.OK, allocate()));
return true;
}

public void heartbeat(byte[] data) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/mycat/net/handler/FrontendAuthenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public void handle(byte[] data) {
return;
}

// ping DBserver to make sure backend connection setup successfully
if (!source.ping(false)) {
failure(ErrorCode.ER_SERVER_SHUTDOWN, "DBserver shutdown.");
return;
}
success(auth);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void handle(byte[] data)
break;
case MySQLPacket.COM_PING:
commands.doPing();
source.ping();
source.ping(true);
break;
case MySQLPacket.COM_QUIT:
commands.doQuit();
Expand Down Expand Up @@ -119,7 +119,7 @@ public void handle(byte[] data)
MycatConfig config = MycatServer.getInstance().getConfig();
if( config.getSystem().getIgnoreUnknownCommand()==1){
LOGGER.warn("Unknown command:{}",data[4]);
source.ping();
source.ping(true);
}else {
LOGGER.error("Unknown command:{}",new String(data));
source.writeErrMessage(ErrorCode.ER_UNKNOWN_COM_ERROR,
Expand Down
60 changes: 56 additions & 4 deletions src/main/java/io/mycat/server/ServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@

import java.io.IOException;
import java.nio.channels.NetworkChannel;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.mycat.backend.jdbc.JDBCConnection;
import io.mycat.net.mysql.OkPacket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -167,10 +171,58 @@ public void setLocked(boolean isLocked) {
this.isLocked = isLocked;
}

@Override
public void ping() {
Ping.response(this);
}
@Override
public boolean ping(boolean sendResponse) {
if (!(backConn instanceof JDBCConnection)) {
if (sendResponse) {
write(writeToBuffer(OkPacket.OK, allocate()));
}
return true;
}

Connection con = ((JDBCConnection)backConn).getCon();
ResultSet rsOutput = null;
Statement stmtOutput = null;
String executeSql = "/* ping */ select 1";
boolean isActive = true;

try {
con.setCatalog(catalog);
con.setSchema(schema);

stmtOutput = con.createStatement();
rsOutput = stmtOutput.executeQuery(executeSql);

if (sendResponse) {
// ping server by select ok, response to client
write(writeToBuffer(OkPacket.OK, allocate()));
}
} catch (SQLException e) {
if (sendResponse) {
writeErrMessage(ErrorCode.ER_SERVER_SHUTDOWN, "DBServer shutdown.");
}

isActive = false;
} finally {
if (rsOutput != null) {
try {
rsOutput.close();
} catch (SQLException e) {
; // do nothing
}
}

if (stmtOutput != null) {
try {
stmtOutput.close();
} catch (SQLException e) {
; // do nothing
}
}
}

return isActive;
}

@Override
public void heartbeat(byte[] data) {
Expand Down

0 comments on commit 4d06e87

Please sign in to comment.