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

Add sys user sync #158

Merged
merged 19 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Binary file modified lib/mo-tester-1.0-SNAPSHOT.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions mo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jdbc:
user:
name: "dump"
password: "111"
sysuser: "dump"
syspass: "111"

#debug info
debug:
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/io/mo/db/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ public class ConnectionManager {
private static String userName = MoConfUtil.getUserName();
private static String pwd = MoConfUtil.getUserpwd();
private static String driver = MoConfUtil.getDriver();

private static String sysuser = MoConfUtil.getSysUserName();
private static String syspass = MoConfUtil.getSyspwd();

private static Connection[] connections = new Connection[COMMON.DEFAULT_CONNECTION_NUM];
private static Connection sysconn = null;

private static final Logger LOG = Logger.getLogger(ConnectionManager.class.getName());
private static boolean server_up = true;

Expand Down Expand Up @@ -84,6 +89,35 @@ public static Connection getConnection(int index, String userName, String pwd){
server_up = false;
return null;
}

public static Connection getConnectionForSys(){
//if mo server crash,return null;
if(!server_up) return null;

//get db connection,if failed,retry 3 times 10 s interval
for(int i = 0; i < 3; i++) {
try {
Class.forName(driver);
if (sysconn == null || sysconn.isClosed()) {
sysconn = DriverManager.getConnection(jdbcURL, sysuser, syspass);
}
return sysconn;
} catch (SQLException e) {
LOG.error("The mo-tester can not get valid conneciton from mo with[user="+userName+", pwd="+pwd+"] for sys user, and will wait 10 seconds and retry...");
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

LOG.error("The mo-tester still can not get valid conneciton from mo for sys user, the following cases wil not be executed!");
server_up = false;
return null;
}

public static void reset(){
for(int i = 1; i < connections.length;i++){
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/io/mo/db/Executor.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static void run(TestScript script){
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);
syncCommit();
}

last_commit_id = command.getConn_id();
Expand Down Expand Up @@ -220,8 +220,10 @@ public static void run(TestScript script){
LOG.warn(String.format("Failed to close connection[id=%d], but effect nothing.",command.getConn_id()));
}
connection = getConnection(command);
if(connection != null && !connection.isClosed())
if(connection != null && !connection.isClosed()) {
connection.setCatalog(command.getUseDB());
syncCommit();
}
continue;
}

Expand Down Expand Up @@ -655,11 +657,16 @@ public static void dropTestDB(Connection connection,TestScript script){
dropTestDB(connection,script.getUseDB());
}

public static void syncCommit(Connection connection){
public static void syncCommit(){
Connection connection = ConnectionManager.getConnectionForSys();
if(connection == null){
LOG.error("select mo_ctl('cn','synccommit','') failed. cause: Can not get invalid connection for sys user.");
}

try {
Statement statement = connection.createStatement();
statement.execute("select mo_ctl('cn','synccommit','')");
LOG.debug("select mo_ctl('cn','synccommit','') successfully.");
LOG.info("select mo_ctl('cn','synccommit','') with sys user[" + MoConfUtil.getSysUserName() + "] successfully.");
} catch (SQLException e) {
LOG.error("select mo_ctl('cn','synccommit','') failed. cause: " + e.getMessage());
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/io/mo/util/MoConfUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ public static String getUserpwd(){
return user.get("password").toString();
}

public static String getSysUserName(){
if(conf == null) init();

Map user = (Map)conf.get("user");
return user.get("sysuser").toString();
}

public static String getSyspwd(){
if(conf == null) init();

Map user = (Map)conf.get("user");
return user.get("syspass").toString();
}

public static String getDefaultDatabase(){
if(conf == null) init();

Expand Down
Loading