Skip to content

Commit

Permalink
Fix npe and mod wait lock (#152)
Browse files Browse the repository at this point in the history
* modify socketTimeout to 120s

* add regular match

* support rc + Pessimistic

* revert Fix pessimistic commit wait error

* revert revert

* fix npe bug and mo wait lock
  • Loading branch information
aressu1985 authored Sep 26, 2023
1 parent ed284d3 commit 6903fae
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 deletions.
Binary file modified lib/mo-tester-1.0-SNAPSHOT.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ builtindb: mo_catalog,mo_task,information_schema,mysql,system_metrics,system,que
outfiles: json/export_1.csv,json/export_2.csv,into_outfile,into_outfile_2

#wait timeout
waittime: 5000
waittime: 2000
2 changes: 1 addition & 1 deletion src/main/java/io/mo/db/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static Connection getConnection(int index, String userName, String pwd){
Class.forName(driver);
if (connections[index] == null || connections[index].isClosed()) {
connections[index] = DriverManager.getConnection(jdbcURL, userName, pwd);
LOG.info("New conneciton from mo with[user="+userName+", pwd="+pwd+"] has been initialized.");
LOG.debug("New conneciton from mo with[user="+userName+", pwd="+pwd+"] has been initialized.");
return connections[index];
}
return connections[index];
Expand Down
70 changes: 60 additions & 10 deletions src/main/java/io/mo/db/Executor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class Executor {

private static final Logger LOG = Logger.getLogger(Executor.class.getName());
private static Thread waitThread = null;
/**
* run test file function
*/
Expand Down Expand Up @@ -78,7 +79,7 @@ public static void run(TestScript script){
//if the the command is marked to ignore flag and the IGNORE_MODEL = true
//skip the command directly
if (COMMON.IGNORE_MODEL && command.isIgnore()) {
LOG.warn("Ignored sql command: [issue#" + command.getIssueNo() + "][" + script.getFileName() + "][row:" + command.getPosition() + "][" + command.getCommand().trim() + "]");
LOG.debug("Ignored sql command: [issue#" + command.getIssueNo() + "][" + script.getFileName() + "][row:" + command.getPosition() + "][" + command.getCommand().trim() + "]");
script.addIgnoredCmd(command);
command.getTestResult().setResult(RESULT.RESULT_TYPE_IGNORED);
command.getTestResult().setErrorCode(RESULT.ERROR_CASE_IGNORE_CODE);
Expand Down Expand Up @@ -108,7 +109,9 @@ public static void run(TestScript script){
}

if(last_commit_id != command.getConn_id()){
LOG.info(String.format("Connection id had been turned from %d to %d",last_commit_id,command.getConn_id()));
LOG.debug(String.format("[%s][row:%d][%s]Connection id had been turned from %d to %d",
command.getScriptFile(),command.getPosition(),command.getCommand(),
last_commit_id,command.getConn_id()));
syncCommit(connection);
}

Expand All @@ -125,6 +128,25 @@ public static void run(TestScript script){
execWaitOperation(command);
}
statement.execute(sqlCmd);
if(command.isNeedWait()){
Thread.sleep(COMMON.WAIT_TIMEOUT/10);
if(waitThread != null && waitThread.isAlive()){
try {
LOG.error(String.format("Command[%s][row:%d] has been executed before connection[id=%d] commit.\nBut still need to wait for connection[id=%d] being committed",
command.getCommand(),command.getPosition(),command.getWaitConnId(),command.getWaitConnId()));
waitThread.join();
script.addFailedCmd(command);
command.getTestResult().setErrorCode(RESULT.ERROR_CHECK_FAILED_CODE);
command.getTestResult().setErrorDesc(RESULT.ERROR_CHECK_FAILED_DESC);
command.getTestResult().setResult(RESULT.RESULT_TYPE_FAILED);
LOG.error("[" + script.getFileName() + "][row:" + command.getPosition() + "][" + command.getCommand().trim() + "] was executed failed, con[id="
+ command.getConn_id()+", user=" +command.getConn_user()+", pwd="+command.getConn_pswd()+"].");
continue;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
ResultSet resultSet = statement.getResultSet();
if (resultSet != null) {
RSSet rsSet = new RSSet(resultSet);
Expand Down Expand Up @@ -229,6 +251,8 @@ public static void run(TestScript script){
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

Expand Down Expand Up @@ -331,6 +355,25 @@ public static boolean genRS(TestScript script){
execWaitOperation(command);
}
statement.execute(sqlCmd);
if(command.isNeedWait()){
Thread.sleep(COMMON.WAIT_TIMEOUT/10);
if(waitThread != null && waitThread.isAlive()){
try {
LOG.error(String.format("Command[%s][row:%d] has been executed before connection[id=%d] commit.\nBut still need to wait for connection[id=%d] being committed",
command.getCommand(),command.getPosition(),command.getWaitConnId(),command.getWaitConnId()));
waitThread.join();
script.addFailedCmd(command);
command.getTestResult().setErrorCode(RESULT.ERROR_CHECK_FAILED_CODE);
command.getTestResult().setErrorDesc(RESULT.ERROR_CHECK_FAILED_DESC);
command.getTestResult().setResult(RESULT.RESULT_TYPE_FAILED);
LOG.error("[" + script.getFileName() + "][row:" + command.getPosition() + "][" + command.getCommand().trim() + "] was executed failed, con[id="
+ command.getConn_id()+", user=" +command.getConn_user()+", pwd="+command.getConn_pswd()+"].");
continue;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
ResultSet resultSet = statement.getResultSet();
if(resultSet != null){
RSSet rsSet = new RSSet(resultSet);
Expand Down Expand Up @@ -385,6 +428,8 @@ public static boolean genRS(TestScript script){

if(j < commands.size() -1)
rs_writer.newLine();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
rs_writer.newLine();
Expand Down Expand Up @@ -606,7 +651,7 @@ public static void syncCommit(Connection connection){
try {
Statement statement = connection.createStatement();
statement.execute("select mo_ctl('cn','synccommit','')");
LOG.info("select mo_ctl('cn','synccommit','') successfully.");
LOG.debug("select mo_ctl('cn','synccommit','') successfully.");
} catch (SQLException e) {
LOG.error("select mo_ctl('cn','synccommit','') failed. cause: " + e.getMessage());
}
Expand Down Expand Up @@ -644,11 +689,13 @@ public static void executeSysCmd(String cmd){
}

public static void execWaitOperation(SqlCommand command){
new Thread(new Runnable() {
waitThread = new Thread(new Runnable() {
@Override
public void run() {
if (command.isNeedWait()) {
try {
LOG.info(String.format("Command[%s][row:%d] needs to wait connection[id:%d] %s",
command.getCommand(),command.getPosition(),command.getWaitConnId(),command.getWaitOperation()));
Thread.sleep(COMMON.WAIT_TIMEOUT);
Connection conn = ConnectionManager.getConnection(command.getWaitConnId());
if(command.getWaitOperation().equalsIgnoreCase("commit")) {
Expand All @@ -658,28 +705,31 @@ public void run() {
Statement statement = conn.createStatement();
statement.execute("commit");
}

LOG.info(String.format("Connection[id=%d] has committed automatically.",command.getWaitConnId()));

LOG.info(String.format("Connection[id=%d] has committed automatically,for command[%s][row:%d]",
command.getWaitConnId(),command.getCommand(),command.getPosition()));
}

if(command.getWaitOperation().equalsIgnoreCase("rollback")) {
if(!conn.getAutoCommit())
conn.rollback();
else {
Statement statement = conn.createStatement();
statement.execute("rollback");
}
LOG.info(String.format("Connection[id=%d] has rollback automatically.",command.getWaitConnId()));
LOG.info(String.format("Connection[id=%d] has rollback automatically,for command[%s][row:%d]",
command.getWaitConnId(),command.getCommand(),command.getPosition()));
}

} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}).start();
});
waitThread.start();
}

public static void main(String[] args){
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/mo/result/StmtResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ public boolean equals(StmtResult stmtResult){
}

public boolean regularMatch(StmtResult stmtResult){

if(stmtResult.errorMessage == null || stmtResult.errorMessage.equalsIgnoreCase("")){
LOG.error("NULL or EMPTY can not regularly match the expected result");
return false;
}
if(stmtResult.errorMessage.matches(this.errorMessage))
return true;
else {
Expand Down

0 comments on commit 6903fae

Please sign in to comment.