Skip to content

Commit

Permalink
[CONJ-1111] ensure using same ip in place of DNS when creating a conn…
Browse files Browse the repository at this point in the history
…ection to kill running query
  • Loading branch information
rusher committed Oct 10, 2023
1 parent e41f024 commit 65c2f1e
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 14 deletions.
11 changes: 9 additions & 2 deletions src/main/java/org/mariadb/jdbc/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,15 @@ public void setPoolConnection(MariaDbPoolConnection poolConnection) {
* @throws SQLException never thrown
*/
public void cancelCurrentQuery() throws SQLException {
try (Client cli =
new StandardClient(conf, client.getHostAddress(), new ReentrantLock(), true)) {
// prefer relying on IP compare to DNS if not using Unix socket/PIPE
String currentIp = client.getSocketIp();
HostAddress hostAddress =
currentIp == null
? client.getHostAddress()
: HostAddress.from(
currentIp, client.getHostAddress().port, client.getHostAddress().primary);

try (Client cli = new StandardClient(conf, hostAddress, new ReentrantLock(), true)) {
cli.execute(new QueryPacket("KILL QUERY " + client.getContext().getThreadId()), false);
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/mariadb/jdbc/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,11 @@ void readStreamingResults(
* @return connection host
*/
HostAddress getHostAddress();

/**
* Get current socket IP or null (for Pipe / unix socket)
*
* @return Socket current IP
*/
String getSocketIp();
}
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,11 @@ public HostAddress getHostAddress() {
return currentClient.getHostAddress();
}

@Override
public String getSocketIp() {
return currentClient.getSocketIp();
}

public boolean isPrimary() {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,12 @@ public void close() {
}
}

public String getSocketIp() {
return this.socket.getInetAddress() == null
? null
: this.socket.getInetAddress().getHostAddress();
}

public boolean isPrimary() {
return hostAddress.primary;
}
Expand Down
76 changes: 67 additions & 9 deletions src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.jupiter.api.*;
import org.mariadb.jdbc.*;
import org.mariadb.jdbc.integration.util.SocketFactoryBasicTest;
Expand Down Expand Up @@ -937,7 +939,10 @@ public void useNoDatabase() throws SQLException {
public void windowsNamedPipe() throws SQLException {
ResultSet rs = null;
try {
rs = sharedConn.createStatement().executeQuery("select @@named_pipe,@@socket,@@named_pipe_full_access_group");
rs =
sharedConn
.createStatement()
.executeQuery("select @@named_pipe,@@socket,@@named_pipe_full_access_group");
} catch (SQLException sqle) {
// on non Windows system, named_pipe doesn't exist.
}
Expand All @@ -956,15 +961,14 @@ public void windowsNamedPipe() throws SQLException {
// skip test if no namedPipeName was obtained because then we do not use a socket connection
Assumptions.assumeTrue(namedPipeName != null);
String connUrl =
password == null || password.isEmpty()
? String.format(
"jdbc:mariadb:///%s?user=%s%s", database, user, defaultOther)
: String.format(
"jdbc:mariadb:///%s?user=%s&password=%s%s",
database, user, password, defaultOther);
password == null || password.isEmpty()
? String.format("jdbc:mariadb:///%s?user=%s%s", database, user, defaultOther)
: String.format(
"jdbc:mariadb:///%s?user=%s&password=%s%s",
database, user, password, defaultOther);


try (Connection connection = DriverManager.getConnection(connUrl + "&pipe=" + namedPipeName)) {
try (Connection connection =
DriverManager.getConnection(connUrl + "&pipe=" + namedPipeName)) {
java.sql.Statement stmt = connection.createStatement();
try (ResultSet rs2 = stmt.executeQuery("SELECT 1")) {
assertTrue(rs2.next());
Expand All @@ -987,6 +991,60 @@ public void windowsNamedPipe() throws SQLException {
}
}

@Test
public void windowsNamedPipeCancel() throws SQLException {
Assumptions.assumeFalse(isMariaDBServer());
ResultSet rs = null;
try {
rs =
sharedConn
.createStatement()
.executeQuery("select @@named_pipe,@@socket,@@named_pipe_full_access_group");
} catch (SQLException sqle) {
// on non Windows system, named_pipe doesn't exist.
}
if (rs != null) {
assertTrue(rs.next());
System.out.println("named_pipe:" + rs.getString(1));
Assumptions.assumeTrue(rs.getBoolean(1));
String namedPipeName = rs.getString(2);
System.out.println("namedPipeName:" + namedPipeName);
if (!isMariaDBServer() && minVersion(8, 0, 14)) {
String namedPipeFullAccess = rs.getString(3);
System.out.println("namedPipeFullAccess:" + namedPipeFullAccess);
Assumptions.assumeTrue(namedPipeFullAccess != null && !namedPipeFullAccess.isEmpty());
}

// skip test if no namedPipeName was obtained because then we do not use a socket connection
Assumptions.assumeTrue(namedPipeName != null);
String connUrl =
password == null || password.isEmpty()
? String.format("jdbc:mariadb:///%s?user=%s%s", database, user, defaultOther)
: String.format(
"jdbc:mariadb:///%s?user=%s&password=%s%s",
database, user, password, defaultOther);

try (Connection connection =
DriverManager.getConnection(connUrl + "&pipe=" + namedPipeName)) {
Statement stmt = connection.createStatement();
stmt.cancel(); // will do nothing

ExecutorService exec = Executors.newFixedThreadPool(1);

Common.assertThrowsContains(
SQLTimeoutException.class,
() -> {
exec.execute(new StatementTest.CancelThread(stmt));
stmt.execute(
"select * from information_schema.columns as c1, information_schema.tables, information_schema"
+ ".tables as t2");
exec.shutdown();
},
"Query execution was interrupted");
}
}
}

@Test
public void localSocket() throws Exception {
Assumptions.assumeTrue(
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/org/mariadb/jdbc/integration/StatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,7 @@ public void testWarnings() throws SQLException {
@Test
public void cancel() throws Exception {
Assumptions.assumeTrue(
isMariaDBServer()
&& !"maxscale".equals(System.getenv("srv"))
!"maxscale".equals(System.getenv("srv"))
&& !"skysql".equals(System.getenv("srv"))
&& !"skysql-ha".equals(System.getenv("srv"))
&& !isXpand());
Expand Down Expand Up @@ -937,7 +936,7 @@ public void closeOnCompletion() throws SQLException {
assertTrue(stmt.isClosed());
}

private static class CancelThread implements Runnable {
static class CancelThread implements Runnable {

private final java.sql.Statement stmt;

Expand Down

0 comments on commit 65c2f1e

Please sign in to comment.