|
| 1 | +package com.datastax.oss.driver.core.metadata; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.junit.Assume.assumeTrue; |
| 5 | + |
| 6 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 7 | +import com.datastax.oss.driver.api.core.Version; |
| 8 | +import com.datastax.oss.driver.api.core.config.DefaultDriverOption; |
| 9 | +import com.datastax.oss.driver.api.core.config.DriverConfigLoader; |
| 10 | +import com.datastax.oss.driver.api.core.cql.ResultSet; |
| 11 | +import com.datastax.oss.driver.api.core.metadata.EndPoint; |
| 12 | +import com.datastax.oss.driver.api.core.metadata.Node; |
| 13 | +import com.datastax.oss.driver.api.testinfra.ccm.CcmBridge; |
| 14 | +import com.datastax.oss.driver.api.testinfra.session.SessionUtils; |
| 15 | +import java.net.InetAddress; |
| 16 | +import java.net.InetSocketAddress; |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.HashSet; |
| 19 | +import java.util.Objects; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.stream.Collectors; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +public class ZeroTokenNodesIT { |
| 26 | + |
| 27 | + @Before |
| 28 | + public void checkScyllaVersion() { |
| 29 | + // minOSS = "6.2.0", |
| 30 | + // minEnterprise = "2024.2.2", |
| 31 | + // Zero-token nodes introduced in scylladb/scylladb#19684 |
| 32 | + assumeTrue(CcmBridge.SCYLLA_ENABLEMENT); |
| 33 | + if (CcmBridge.SCYLLA_ENTERPRISE) { |
| 34 | + assumeTrue( |
| 35 | + CcmBridge.VERSION.compareTo(Objects.requireNonNull(Version.parse("2024.2.2"))) >= 0); |
| 36 | + } else { |
| 37 | + assumeTrue(CcmBridge.VERSION.compareTo(Objects.requireNonNull(Version.parse("6.2.0"))) >= 0); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void should_not_ignore_zero_token_peer_when_option_is_enabled() { |
| 43 | + CqlSession session = null; |
| 44 | + CcmBridge.Builder ccmBridgeBuilder = CcmBridge.builder(); |
| 45 | + try (CcmBridge ccmBridge = ccmBridgeBuilder.withNodes(3).withIpPrefix("127.0.1.").build()) { |
| 46 | + ccmBridge.create(); |
| 47 | + ccmBridge.startWithArgs("--wait-for-binary-proto"); |
| 48 | + ccmBridge.addWithoutStart(4, "dc1"); |
| 49 | + ccmBridge.updateNodeConfig(4, "join_ring", false); |
| 50 | + ccmBridge.startWithArgs(4, "--wait-for-binary-proto"); |
| 51 | + DriverConfigLoader loader = |
| 52 | + SessionUtils.configLoaderBuilder() |
| 53 | + .withBoolean(DefaultDriverOption.METADATA_ALLOW_ZERO_TOKEN_PEERS, true) |
| 54 | + .build(); |
| 55 | + session = |
| 56 | + CqlSession.builder() |
| 57 | + .withConfigLoader(loader) |
| 58 | + .addContactPoint(new InetSocketAddress(ccmBridge.getNodeIpAddress(1), 9042)) |
| 59 | + .build(); |
| 60 | + |
| 61 | + Collection<Node> nodes = session.getMetadata().getNodes().values(); |
| 62 | + Set<String> toStrings = |
| 63 | + nodes.stream().map(Node::getEndPoint).map(EndPoint::toString).collect(Collectors.toSet()); |
| 64 | + assertThat(toStrings) |
| 65 | + .containsOnly("/127.0.1.1:9042", "/127.0.1.2:9042", "/127.0.1.3:9042", "/127.0.1.4:9042"); |
| 66 | + } finally { |
| 67 | + if (session != null) session.close(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void should_not_discover_zero_token_DC_when_option_is_disabled() { |
| 73 | + CqlSession session = null; |
| 74 | + CcmBridge.Builder ccmBridgeBuilder = CcmBridge.builder(); |
| 75 | + try (CcmBridge ccmBridge = ccmBridgeBuilder.withNodes(2, 2).withIpPrefix("127.0.1.").build()) { |
| 76 | + ccmBridge.create(); |
| 77 | + ccmBridge.updateNodeConfig(3, "join_ring", false); |
| 78 | + ccmBridge.updateNodeConfig(4, "join_ring", false); |
| 79 | + ccmBridge.startWithArgs("--wait-for-binary-proto"); |
| 80 | + |
| 81 | + // Not adding .withBoolean(DefaultDriverOption.METADATA_ALLOW_ZERO_TOKEN_PEERS, false) |
| 82 | + // It is implicitly false |
| 83 | + DriverConfigLoader loader = SessionUtils.configLoaderBuilder().build(); |
| 84 | + session = |
| 85 | + CqlSession.builder() |
| 86 | + .withLocalDatacenter("dc1") |
| 87 | + .withConfigLoader(loader) |
| 88 | + .addContactPoint(new InetSocketAddress(ccmBridge.getNodeIpAddress(1), 9042)) |
| 89 | + .build(); |
| 90 | + |
| 91 | + Set<String> landedOn = new HashSet<>(); |
| 92 | + for (int i = 0; i < 30; i++) { |
| 93 | + ResultSet rs = session.execute("SELECT * FROM system.local"); |
| 94 | + InetAddress broadcastRpcInetAddress = |
| 95 | + Objects.requireNonNull(rs.one()).getInetAddress("rpc_address"); |
| 96 | + landedOn.add(Objects.requireNonNull(broadcastRpcInetAddress).toString()); |
| 97 | + } |
| 98 | + assertThat(landedOn).containsOnly("/127.0.1.1", "/127.0.1.2"); |
| 99 | + Collection<Node> nodes = session.getMetadata().getNodes().values(); |
| 100 | + Set<String> toStrings = |
| 101 | + nodes.stream().map(Node::getEndPoint).map(EndPoint::toString).collect(Collectors.toSet()); |
| 102 | + assertThat(toStrings).containsOnly("/127.0.1.1:9042", "/127.0.1.2:9042"); |
| 103 | + } finally { |
| 104 | + if (session != null) session.close(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void should_discover_zero_token_DC_when_option_is_enabled() { |
| 110 | + CqlSession session = null; |
| 111 | + CcmBridge.Builder ccmBridgeBuilder = CcmBridge.builder(); |
| 112 | + try (CcmBridge ccmBridge = ccmBridgeBuilder.withNodes(2, 2).withIpPrefix("127.0.1.").build()) { |
| 113 | + ccmBridge.create(); |
| 114 | + ccmBridge.updateNodeConfig(3, "join_ring", false); |
| 115 | + ccmBridge.updateNodeConfig(4, "join_ring", false); |
| 116 | + ccmBridge.startWithArgs("--wait-for-binary-proto"); |
| 117 | + |
| 118 | + DriverConfigLoader loader = |
| 119 | + SessionUtils.configLoaderBuilder() |
| 120 | + .withBoolean(DefaultDriverOption.METADATA_ALLOW_ZERO_TOKEN_PEERS, true) |
| 121 | + .build(); |
| 122 | + session = |
| 123 | + CqlSession.builder() |
| 124 | + .withLocalDatacenter("dc1") |
| 125 | + .withConfigLoader(loader) |
| 126 | + .addContactPoint(new InetSocketAddress(ccmBridge.getNodeIpAddress(1), 9042)) |
| 127 | + .build(); |
| 128 | + |
| 129 | + Set<String> landedOn = new HashSet<>(); |
| 130 | + for (int i = 0; i < 30; i++) { |
| 131 | + ResultSet rs = session.execute("SELECT * FROM system.local"); |
| 132 | + InetAddress broadcastRpcInetAddress = |
| 133 | + Objects.requireNonNull(rs.one()).getInetAddress("rpc_address"); |
| 134 | + landedOn.add(Objects.requireNonNull(broadcastRpcInetAddress).toString()); |
| 135 | + } |
| 136 | + // LBP should still target local datacenter: |
| 137 | + assertThat(landedOn).containsOnly("/127.0.1.1", "/127.0.1.2"); |
| 138 | + Collection<Node> nodes = session.getMetadata().getNodes().values(); |
| 139 | + Set<String> toStrings = |
| 140 | + nodes.stream().map(Node::getEndPoint).map(EndPoint::toString).collect(Collectors.toSet()); |
| 141 | + // Metadata should have all nodes: |
| 142 | + assertThat(toStrings) |
| 143 | + .containsOnly("/127.0.1.1:9042", "/127.0.1.2:9042", "/127.0.1.3:9042", "/127.0.1.4:9042"); |
| 144 | + } finally { |
| 145 | + if (session != null) session.close(); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void should_connect_to_zero_token_contact_point() { |
| 151 | + CqlSession session = null; |
| 152 | + CcmBridge.Builder ccmBridgeBuilder = CcmBridge.builder(); |
| 153 | + try (CcmBridge ccmBridge = ccmBridgeBuilder.withNodes(2).withIpPrefix("127.0.1.").build()) { |
| 154 | + ccmBridge.create(); |
| 155 | + ccmBridge.startWithArgs("--wait-for-binary-proto"); |
| 156 | + ccmBridge.addWithoutStart(3, "dc1"); |
| 157 | + ccmBridge.updateNodeConfig(3, "join_ring", false); |
| 158 | + ccmBridge.startWithArgs(3, "--wait-for-binary-proto"); |
| 159 | + // DefaultDriverOption.METADATA_ALLOW_ZERO_TOKEN_PEERS is implicitly false |
| 160 | + DriverConfigLoader loader = SessionUtils.configLoaderBuilder().build(); |
| 161 | + session = |
| 162 | + CqlSession.builder() |
| 163 | + .withConfigLoader(loader) |
| 164 | + .addContactPoint(new InetSocketAddress(ccmBridge.getNodeIpAddress(3), 9042)) |
| 165 | + .build(); |
| 166 | + |
| 167 | + Collection<Node> nodes = session.getMetadata().getNodes().values(); |
| 168 | + Set<String> toStrings = |
| 169 | + nodes.stream().map(Node::getEndPoint).map(EndPoint::toString).collect(Collectors.toSet()); |
| 170 | + assertThat(toStrings).containsOnly("/127.0.1.1:9042", "/127.0.1.2:9042", "/127.0.1.3:9042"); |
| 171 | + } finally { |
| 172 | + if (session != null) session.close(); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void should_connect_to_zero_token_DC() { |
| 178 | + // This test is similar but not exactly the same as should_connect_to_zero_token_contact_point. |
| 179 | + // In the future we may want to have different behavior for arbiter DCs and adjust this test |
| 180 | + // method. |
| 181 | + CqlSession session = null; |
| 182 | + CcmBridge.Builder ccmBridgeBuilder = CcmBridge.builder(); |
| 183 | + try (CcmBridge ccmBridge = ccmBridgeBuilder.withNodes(2, 2).withIpPrefix("127.0.1.").build()) { |
| 184 | + ccmBridge.create(); |
| 185 | + ccmBridge.updateNodeConfig(3, "join_ring", false); |
| 186 | + ccmBridge.updateNodeConfig(4, "join_ring", false); |
| 187 | + ccmBridge.startWithArgs("--wait-for-binary-proto"); |
| 188 | + |
| 189 | + DriverConfigLoader loader = |
| 190 | + SessionUtils.configLoaderBuilder() |
| 191 | + .withBoolean(DefaultDriverOption.METADATA_ALLOW_ZERO_TOKEN_PEERS, false) |
| 192 | + .build(); |
| 193 | + session = |
| 194 | + CqlSession.builder() |
| 195 | + .withLocalDatacenter("dc1") |
| 196 | + .withConfigLoader(loader) |
| 197 | + .addContactPoint(new InetSocketAddress(ccmBridge.getNodeIpAddress(3), 9042)) |
| 198 | + .build(); |
| 199 | + |
| 200 | + Set<String> landedOn = new HashSet<>(); |
| 201 | + for (int i = 0; i < 30; i++) { |
| 202 | + ResultSet rs = session.execute("SELECT * FROM system.local"); |
| 203 | + InetAddress broadcastRpcInetAddress = |
| 204 | + Objects.requireNonNull(rs.one()).getInetAddress("rpc_address"); |
| 205 | + landedOn.add(Objects.requireNonNull(broadcastRpcInetAddress).toString()); |
| 206 | + } |
| 207 | + // LBP should still target local datacenter: |
| 208 | + assertThat(landedOn).containsOnly("/127.0.1.1", "/127.0.1.2"); |
| 209 | + Collection<Node> nodes = session.getMetadata().getNodes().values(); |
| 210 | + Set<String> toStrings = |
| 211 | + nodes.stream().map(Node::getEndPoint).map(EndPoint::toString).collect(Collectors.toSet()); |
| 212 | + // Metadata should have valid ordinary peers plus zero-token contact point: |
| 213 | + assertThat(toStrings).containsOnly("/127.0.1.1:9042", "/127.0.1.2:9042", "/127.0.1.3:9042"); |
| 214 | + } finally { |
| 215 | + if (session != null) session.close(); |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments