From 043e5bb78a3c33dd3884361afffb5297a6d37b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20B=C4=85czkowski?= Date: Thu, 2 Oct 2025 21:19:55 +0200 Subject: [PATCH 1/2] Close sessions properly in DefaultMetadataTabletMapIT Moves additional sessions used in this test into try-with-resources statements, ensuring that they are closed afterwards. --- .../core/metadata/DefaultMetadataTabletMapIT.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/integration-tests/src/test/java/com/datastax/oss/driver/core/metadata/DefaultMetadataTabletMapIT.java b/integration-tests/src/test/java/com/datastax/oss/driver/core/metadata/DefaultMetadataTabletMapIT.java index 6ae7fd5e26c..a92ec530ee8 100644 --- a/integration-tests/src/test/java/com/datastax/oss/driver/core/metadata/DefaultMetadataTabletMapIT.java +++ b/integration-tests/src/test/java/com/datastax/oss/driver/core/metadata/DefaultMetadataTabletMapIT.java @@ -232,7 +232,7 @@ public void every_statement_should_deliver_tablet_info() { // Preparation of the statements without KS will fail on the session with no ks specified continue; } - CqlSession session = sessionEntry.getValue().get(); + try (CqlSession session = sessionEntry.getValue().get()) { // Empty out tablets information if (session.getMetadata().getTabletMap().isPresent()) { session @@ -281,6 +281,7 @@ public void every_statement_should_deliver_tablet_info() { "Statement %s on session %s was routed to different nodes", stmtEntry.getKey(), sessionEntry.getKey())); } + } } } @@ -293,9 +294,9 @@ public void every_statement_should_deliver_tablet_info() { @Test public void should_receive_each_tablet_exactly_once() { - CqlSession session = - CqlSession.builder().addContactEndPoints(CCM_RULE.getContactPoints()).build(); int counter = 0; + try (CqlSession session = + CqlSession.builder().addContactEndPoints(CCM_RULE.getContactPoints()).build()) { PreparedStatement preparedStatement = session.prepare(STMT_INSERT); for (int i = 1; i <= QUERIES; i++) { if (executeAndReturnIfResultHasTabletsInfo(session, preparedStatement.bind(i, i))) { @@ -304,11 +305,11 @@ public void should_receive_each_tablet_exactly_once() { } Assert.assertEquals(INITIAL_TABLETS, counter); assertSessionTabletMapIsFilled(session); - session.close(); + } - session = CqlSession.builder().addContactEndPoints(CCM_RULE.getContactPoints()).build(); + try (CqlSession session = CqlSession.builder().addContactEndPoints(CCM_RULE.getContactPoints()).build()) { counter = 0; - preparedStatement = session.prepare(STMT_SELECT); + PreparedStatement preparedStatement = session.prepare(STMT_SELECT); for (int i = 1; i <= QUERIES; i++) { if (executeAndReturnIfResultHasTabletsInfo(session, preparedStatement.bind(i, i))) { counter++; @@ -334,6 +335,7 @@ public void should_receive_each_tablet_exactly_once() { "Received non empty payload with tablets routing information: " + payload); } } + } } private static boolean waitSessionLearnedTabletInfo(CqlSession session) { From 690831c201ac6f71195deec9ee30ec48e9707735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20B=C4=85czkowski?= Date: Mon, 6 Oct 2025 14:51:20 +0200 Subject: [PATCH 2/2] Apply auto-formatter Adding as a separate commit because otherwise the diff is hard to understand in contrast to what it actually does. --- .../metadata/DefaultMetadataTabletMapIT.java | 141 +++++++++--------- 1 file changed, 71 insertions(+), 70 deletions(-) diff --git a/integration-tests/src/test/java/com/datastax/oss/driver/core/metadata/DefaultMetadataTabletMapIT.java b/integration-tests/src/test/java/com/datastax/oss/driver/core/metadata/DefaultMetadataTabletMapIT.java index a92ec530ee8..9146d379980 100644 --- a/integration-tests/src/test/java/com/datastax/oss/driver/core/metadata/DefaultMetadataTabletMapIT.java +++ b/integration-tests/src/test/java/com/datastax/oss/driver/core/metadata/DefaultMetadataTabletMapIT.java @@ -233,54 +233,54 @@ public void every_statement_should_deliver_tablet_info() { continue; } try (CqlSession session = sessionEntry.getValue().get()) { - // Empty out tablets information - if (session.getMetadata().getTabletMap().isPresent()) { - session - .getMetadata() - .getTabletMap() - .get() - .removeByKeyspace(CqlIdentifier.fromCql(KEYSPACE_NAME)); - } - Statement stmt; - try { - stmt = stmtEntry.getValue().apply(session); - } catch (Exception e) { - RuntimeException ex = - new RuntimeException( + // Empty out tablets information + if (session.getMetadata().getTabletMap().isPresent()) { + session + .getMetadata() + .getTabletMap() + .get() + .removeByKeyspace(CqlIdentifier.fromCql(KEYSPACE_NAME)); + } + Statement stmt; + try { + stmt = stmtEntry.getValue().apply(session); + } catch (Exception e) { + RuntimeException ex = + new RuntimeException( + String.format( + "Failed to build statement %s on session %s", + stmtEntry.getKey(), sessionEntry.getKey())); + ex.addSuppressed(e); + throw ex; + } + try { + if (!executeOnAllHostsAndReturnIfResultHasTabletsInfo(session, stmt)) { + testErrors.add( String.format( - "Failed to build statement %s on session %s", + "Statement %s on session %s got no tablet info", stmtEntry.getKey(), sessionEntry.getKey())); - ex.addSuppressed(e); - throw ex; - } - try { - if (!executeOnAllHostsAndReturnIfResultHasTabletsInfo(session, stmt)) { + continue; + } + } catch (Exception e) { + testErrors.add( + String.format( + "Failed to execute statement %s on session %s: %s", + stmtEntry.getKey(), sessionEntry.getKey(), e)); + continue; + } + if (!waitSessionLearnedTabletInfo(session)) { testErrors.add( String.format( - "Statement %s on session %s got no tablet info", + "Statement %s on session %s did not trigger session tablets update", stmtEntry.getKey(), sessionEntry.getKey())); continue; } - } catch (Exception e) { - testErrors.add( - String.format( - "Failed to execute statement %s on session %s: %s", - stmtEntry.getKey(), sessionEntry.getKey(), e)); - continue; - } - if (!waitSessionLearnedTabletInfo(session)) { - testErrors.add( - String.format( - "Statement %s on session %s did not trigger session tablets update", - stmtEntry.getKey(), sessionEntry.getKey())); - continue; - } - if (!checkIfRoutedProperly(session, stmt)) { - testErrors.add( - String.format( - "Statement %s on session %s was routed to different nodes", - stmtEntry.getKey(), sessionEntry.getKey())); - } + if (!checkIfRoutedProperly(session, stmt)) { + testErrors.add( + String.format( + "Statement %s on session %s was routed to different nodes", + stmtEntry.getKey(), sessionEntry.getKey())); + } } } } @@ -296,46 +296,47 @@ public void every_statement_should_deliver_tablet_info() { public void should_receive_each_tablet_exactly_once() { int counter = 0; try (CqlSession session = - CqlSession.builder().addContactEndPoints(CCM_RULE.getContactPoints()).build()) { - PreparedStatement preparedStatement = session.prepare(STMT_INSERT); - for (int i = 1; i <= QUERIES; i++) { - if (executeAndReturnIfResultHasTabletsInfo(session, preparedStatement.bind(i, i))) { - counter++; + CqlSession.builder().addContactEndPoints(CCM_RULE.getContactPoints()).build()) { + PreparedStatement preparedStatement = session.prepare(STMT_INSERT); + for (int i = 1; i <= QUERIES; i++) { + if (executeAndReturnIfResultHasTabletsInfo(session, preparedStatement.bind(i, i))) { + counter++; + } } - } - Assert.assertEquals(INITIAL_TABLETS, counter); - assertSessionTabletMapIsFilled(session); + Assert.assertEquals(INITIAL_TABLETS, counter); + assertSessionTabletMapIsFilled(session); } - try (CqlSession session = CqlSession.builder().addContactEndPoints(CCM_RULE.getContactPoints()).build()) { - counter = 0; - PreparedStatement preparedStatement = session.prepare(STMT_SELECT); - for (int i = 1; i <= QUERIES; i++) { - if (executeAndReturnIfResultHasTabletsInfo(session, preparedStatement.bind(i, i))) { - counter++; + try (CqlSession session = + CqlSession.builder().addContactEndPoints(CCM_RULE.getContactPoints()).build()) { + counter = 0; + PreparedStatement preparedStatement = session.prepare(STMT_SELECT); + for (int i = 1; i <= QUERIES; i++) { + if (executeAndReturnIfResultHasTabletsInfo(session, preparedStatement.bind(i, i))) { + counter++; + } } - } - LOG.debug("Ran first set of queries"); + LOG.debug("Ran first set of queries"); - // With enough queries we should hit a wrong node for each tablet exactly once. - Assert.assertEquals(INITIAL_TABLETS, counter); - assertSessionTabletMapIsFilled(session); + // With enough queries we should hit a wrong node for each tablet exactly once. + Assert.assertEquals(INITIAL_TABLETS, counter); + assertSessionTabletMapIsFilled(session); - // All tablet information should be available by now (unless for some reason cluster did sth on - // its own) - // We should not receive any tablet payloads now, since they are sent only on mismatch. - for (int i = 1; i <= QUERIES; i++) { + // All tablet information should be available by now (unless for some reason cluster did sth + // on its own). We should not receive any tablet payloads now, since they are sent only on + // mismatch. + for (int i = 1; i <= QUERIES; i++) { - ResultSet rs = session.execute(preparedStatement.bind(i, i)); - Map payload = rs.getExecutionInfo().getIncomingPayload(); + ResultSet rs = session.execute(preparedStatement.bind(i, i)); + Map payload = rs.getExecutionInfo().getIncomingPayload(); - if (payload.containsKey(TabletInfo.TABLETS_ROUTING_V1_CUSTOM_PAYLOAD_KEY)) { - throw new RuntimeException( - "Received non empty payload with tablets routing information: " + payload); + if (payload.containsKey(TabletInfo.TABLETS_ROUTING_V1_CUSTOM_PAYLOAD_KEY)) { + throw new RuntimeException( + "Received non empty payload with tablets routing information: " + payload); + } } } - } } private static boolean waitSessionLearnedTabletInfo(CqlSession session) {