From 615253f9db596fccbfa709ed1097bfbe23b02a2b Mon Sep 17 00:00:00 2001 From: Rivers Zhang Date: Fri, 20 Dec 2024 14:12:03 -0500 Subject: [PATCH] Fix check for rte response from pmux Signed-off-by: Rivers Zhang --- .../com/bloomberg/comdb2/jdbc/SockIO.java | 5 +- .../comdb2/jdbc/DatabaseDiscoveryTest.java | 64 ++++++++++++------- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/cdb2jdbc/src/main/java/com/bloomberg/comdb2/jdbc/SockIO.java b/cdb2jdbc/src/main/java/com/bloomberg/comdb2/jdbc/SockIO.java index 3c88d913c7..81a58fc1ce 100644 --- a/cdb2jdbc/src/main/java/com/bloomberg/comdb2/jdbc/SockIO.java +++ b/cdb2jdbc/src/main/java/com/bloomberg/comdb2/jdbc/SockIO.java @@ -154,7 +154,10 @@ public boolean open() { /* Errors are handled by the catch block below. */ out.write(rtepacket.getBytes()); out.flush(); - readLine(32); + /* empty string: success */ + String ret = readLine(32); + if (!ret.equals("0")) + throw new IOException("could not route connection"); } } catch (IOException e) { opened = false; diff --git a/cdb2jdbc/src/test/java/com/bloomberg/comdb2/jdbc/DatabaseDiscoveryTest.java b/cdb2jdbc/src/test/java/com/bloomberg/comdb2/jdbc/DatabaseDiscoveryTest.java index 83619d77c7..1c2df5e53d 100644 --- a/cdb2jdbc/src/test/java/com/bloomberg/comdb2/jdbc/DatabaseDiscoveryTest.java +++ b/cdb2jdbc/src/test/java/com/bloomberg/comdb2/jdbc/DatabaseDiscoveryTest.java @@ -40,8 +40,13 @@ public void testPmuxDown() throws IOException, SQLException { String db = System.getProperty("cdb2jdbc.test.database"); String cluster = System.getProperty("cdb2jdbc.test.cluster"); - Connection conn = DriverManager.getConnection( - String.format("jdbc:comdb2://%s/%s?portmuxport=8888", cluster, db)); + /* This line would succeed, because the driver would use the pmuxport for single-port */ + Connection conn1 = DriverManager.getConnection( + String.format("jdbc:comdb2://%s/%s?portmuxport=8888&allow_pmux_route=1", cluster, db)); + conn1.close(); + /* This line would fail, because the driver would attempt to query port 8888 for db port */ + Connection conn2 = DriverManager.getConnection( + String.format("jdbc:comdb2://%s/%s?portmuxport=8888&allow_pmux_route=0", cluster, db)); Assert.assertTrue("Should not reach here", false); } catch (SQLException sqle) { Assert.assertTrue("Should see correct error message.", @@ -69,21 +74,28 @@ public void testComdb2dbMachineOffline() throws IOException, SQLException { @Test public void testComdb2dbNodeDown() throws IOException, SQLException { + LogManager.getLogManager().reset(); + String fname = "/tmp/comdb2db.jdbc.mvn.test.cfg." + System.currentTimeMillis(); + String cluster = System.getProperty("cdb2jdbc.test.cluster"); + BufferedWriter writer = new BufferedWriter(new FileWriter(fname)); + writer.write("does_not_exist 0 "); + writer.write(cluster); + writer.close(); + System.setProperty("comdb2db.cfg", fname); try { - LogManager.getLogManager().reset(); - String fname = "/tmp/comdb2db.jdbc.mvn.test.cfg." + System.currentTimeMillis(); - String cluster = System.getProperty("cdb2jdbc.test.cluster"); - BufferedWriter writer = new BufferedWriter(new FileWriter(fname)); - writer.write("does_not_exist 0 "); - writer.write(cluster); - writer.close(); - System.setProperty("comdb2db.cfg", fname); - Connection conn = DriverManager.getConnection("jdbc:comdb2://dev/db?comdb2dbname=does_not_exist"); + Connection conn = DriverManager.getConnection("jdbc:comdb2://dev/db?comdb2dbname=does_not_exist&allow_pmux_route=0"); Assert.assertTrue("Should not reach here", false); } catch (SQLException sqle) { Assert.assertTrue("Should see correct error message.", sqle.getMessage().contains("Received invalid port from pmux")); } + try { + Connection conn = DriverManager.getConnection("jdbc:comdb2://dev/db?comdb2dbname=does_not_exist&allow_pmux_route=1"); + Assert.assertTrue("Should not reach here", false); + } catch (SQLException sqle) { + Assert.assertTrue("Should see correct error message.", + sqle.getMessage().contains("A network I/O error occurred")); + } } @Test @@ -128,24 +140,32 @@ public void testMalformedComdb2db() throws IOException, SQLException { @Test public void testDbDown() throws IOException, SQLException { - try { - LogManager.getLogManager().reset(); + LogManager.getLogManager().reset(); - String cluster = System.getProperty("cdb2jdbc.test.cluster"); - String fname = "/tmp/comdb2db.jdbc.mvn.test.cfg." + System.currentTimeMillis(); - BufferedWriter writer = new BufferedWriter(new FileWriter(fname)); - writer.write("does_not_exist"); - writer.write(" 0 "); - writer.write(cluster); - writer.close(); - System.setProperty("comdb2db.cfg", fname); + String cluster = System.getProperty("cdb2jdbc.test.cluster"); + String fname = "/tmp/comdb2db.jdbc.mvn.test.cfg." + System.currentTimeMillis(); + BufferedWriter writer = new BufferedWriter(new FileWriter(fname)); + writer.write("does_not_exist"); + writer.write(" 0 "); + writer.write(cluster); + writer.close(); + System.setProperty("comdb2db.cfg", fname); - Connection conn = DriverManager.getConnection("jdbc:comdb2://dev/does_not_exist?max_retries=1"); + try { + Connection conn = DriverManager.getConnection("jdbc:comdb2://dev/does_not_exist?max_retries=1&allow_pmux_route=0"); Assert.assertTrue("Should not reach here", false); } catch (SQLException sqle) { Assert.assertTrue("Should see correct error message.", sqle.getMessage().contains("Received invalid port from pmux.")); } + + try { + Connection conn = DriverManager.getConnection("jdbc:comdb2://dev/does_not_exist?max_retries=1&allow_pmux_route=1"); + Assert.assertTrue("Should not reach here", false); + } catch (SQLException sqle) { + Assert.assertTrue("Should see correct error message.", + sqle.getMessage().contains("A network I/O error occurred")); + } } @Test