|
25 | 25 |
|
26 | 26 | import static org.assertj.core.api.Assertions.assertThat;
|
27 | 27 | import static org.junit.Assert.assertTrue;
|
| 28 | +import static org.junit.Assert.fail; |
28 | 29 |
|
29 | 30 | import com.datastax.oss.driver.api.core.CqlSession;
|
30 | 31 | import com.datastax.oss.driver.api.core.CqlSessionBuilder;
|
|
34 | 35 | import com.datastax.oss.driver.api.core.cql.Row;
|
35 | 36 | import com.datastax.oss.driver.api.core.metadata.Node;
|
36 | 37 | import com.datastax.oss.driver.api.testinfra.ccm.CcmBridge;
|
37 |
| -import com.datastax.oss.driver.api.testinfra.ccm.CustomCcmRule; |
38 | 38 | import com.datastax.oss.driver.categories.IsolatedTests;
|
39 | 39 | import com.datastax.oss.driver.internal.core.config.typesafe.DefaultProgrammaticDriverConfigLoaderBuilder;
|
40 | 40 | import com.datastax.oss.driver.internal.core.resolver.ResolverProvider;
|
|
45 | 45 | import java.net.UnknownHostException;
|
46 | 46 | import java.util.Collection;
|
47 | 47 | import java.util.Collections;
|
| 48 | +import java.util.Iterator; |
48 | 49 | import java.util.List;
|
49 | 50 | import java.util.Set;
|
50 | 51 | import java.util.stream.Collectors;
|
51 |
| -import org.junit.ClassRule; |
| 52 | +import org.junit.BeforeClass; |
52 | 53 | import org.junit.Test;
|
53 | 54 | import org.junit.experimental.categories.Category;
|
54 | 55 | import org.slf4j.Logger;
|
|
58 | 59 | public class MockResolverIT {
|
59 | 60 |
|
60 | 61 | private static final Logger LOG = LoggerFactory.getLogger(MockResolverIT.class);
|
| 62 | + private static final MockResolverFactory RESOLVER_FACTORY = new MockResolverFactory(); |
61 | 63 |
|
62 |
| - @ClassRule |
63 |
| - public static final CustomCcmRule CCM_RULE = CustomCcmRule.builder().withNodes(1).build(); |
| 64 | + private static final int CLUSTER_WAIT_SECONDS = |
| 65 | + 60; // Maximal wait time for cluster nodes to get up |
| 66 | + |
| 67 | + @BeforeClass |
| 68 | + public static void setUpResolver() { |
| 69 | + ResolverProvider.setDefaultResolverFactory(RESOLVER_FACTORY); |
| 70 | + } |
64 | 71 |
|
65 | 72 | @Test
|
66 | 73 | public void should_connect_with_mocked_hostname() {
|
67 |
| - CcmBridge ccmBridge = CCM_RULE.getCcmBridge(); |
| 74 | + CcmBridge.Builder ccmBridgeBuilder = CcmBridge.builder().withNodes(1).withIpPrefix("127.0.1."); |
| 75 | + try (CcmBridge ccmBridge = ccmBridgeBuilder.build()) { |
| 76 | + RESOLVER_FACTORY.updateResponse( |
| 77 | + "test.cluster.fake", |
| 78 | + new ValidResponse(new InetAddress[] {getNodeInetAddress(ccmBridge, 1)})); |
| 79 | + ccmBridge.create(); |
| 80 | + ccmBridge.start(); |
68 | 81 |
|
69 |
| - MockResolverFactory resolverFactory = new MockResolverFactory(); |
70 |
| - resolverFactory.updateResponse( |
71 |
| - "node-1.cluster.fake", |
72 |
| - new ValidResponse(new InetAddress[] {getNodeInetAddress(ccmBridge, 1)})); |
73 |
| - ResolverProvider.setDefaultResolverFactory(resolverFactory); |
| 82 | + DriverConfigLoader loader = |
| 83 | + new DefaultProgrammaticDriverConfigLoaderBuilder() |
| 84 | + .withBoolean(TypedDriverOption.RESOLVE_CONTACT_POINTS.getRawOption(), false) |
| 85 | + .withBoolean(TypedDriverOption.RECONNECT_ON_INIT.getRawOption(), true) |
| 86 | + .withStringList( |
| 87 | + TypedDriverOption.CONTACT_POINTS.getRawOption(), |
| 88 | + Collections.singletonList("test.cluster.fake:9042")) |
| 89 | + .build(); |
74 | 90 |
|
| 91 | + CqlSessionBuilder builder = new CqlSessionBuilder().withConfigLoader(loader); |
| 92 | + try (CqlSession session = builder.build()) { |
| 93 | + ResultSet rs = session.execute("SELECT * FROM system.local"); |
| 94 | + List<Row> rows = rs.all(); |
| 95 | + assertThat(rows).hasSize(1); |
| 96 | + LOG.trace("system.local contents: {}", rows.get(0).getFormattedContents()); |
| 97 | + Collection<Node> nodes = session.getMetadata().getNodes().values(); |
| 98 | + for (Node node : nodes) { |
| 99 | + LOG.trace("Found metadata node: {}", node); |
| 100 | + } |
| 101 | + Set<Node> filteredNodes; |
| 102 | + filteredNodes = |
| 103 | + nodes.stream() |
| 104 | + .filter(x -> x.toString().contains("test.cluster.fake")) |
| 105 | + .collect(Collectors.toSet()); |
| 106 | + assertThat(filteredNodes).hasSize(1); |
| 107 | + InetSocketAddress address = |
| 108 | + (InetSocketAddress) filteredNodes.iterator().next().getEndPoint().resolve(); |
| 109 | + assertTrue(address.isUnresolved()); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void replace_cluster_test() { |
| 116 | + final int numberOfNodes = 3; |
75 | 117 | DriverConfigLoader loader =
|
76 | 118 | new DefaultProgrammaticDriverConfigLoaderBuilder()
|
77 | 119 | .withBoolean(TypedDriverOption.RESOLVE_CONTACT_POINTS.getRawOption(), false)
|
78 | 120 | .withBoolean(TypedDriverOption.RECONNECT_ON_INIT.getRawOption(), true)
|
79 | 121 | .withStringList(
|
80 | 122 | TypedDriverOption.CONTACT_POINTS.getRawOption(),
|
81 |
| - Collections.singletonList("node-1.cluster.fake:9042")) |
| 123 | + Collections.singletonList("test.cluster.fake:9042")) |
82 | 124 | .build();
|
83 | 125 |
|
84 | 126 | CqlSessionBuilder builder = new CqlSessionBuilder().withConfigLoader(loader);
|
85 |
| - try (CqlSession session = builder.build()) { |
| 127 | + CqlSession session; |
| 128 | + |
| 129 | + try (CcmBridge ccmBridge = |
| 130 | + CcmBridge.builder().withNodes(numberOfNodes).withIpPrefix("127.0.1.").build()) { |
| 131 | + RESOLVER_FACTORY.updateResponse( |
| 132 | + "test.cluster.fake", |
| 133 | + new ValidResponse( |
| 134 | + new InetAddress[] { |
| 135 | + getNodeInetAddress(ccmBridge, 1), |
| 136 | + getNodeInetAddress(ccmBridge, 2), |
| 137 | + getNodeInetAddress(ccmBridge, 3) |
| 138 | + })); |
| 139 | + ccmBridge.create(); |
| 140 | + ccmBridge.start(); |
| 141 | + session = builder.build(); |
| 142 | + boolean allNodesUp = false; |
| 143 | + int nodesUp = 0; |
| 144 | + for (int i = 0; i < CLUSTER_WAIT_SECONDS; i++) { |
| 145 | + try { |
| 146 | + Collection<Node> nodes = session.getMetadata().getNodes().values(); |
| 147 | + nodesUp = 0; |
| 148 | + for (Node node : nodes) { |
| 149 | + if (node.getUpSinceMillis() > 0) { |
| 150 | + nodesUp++; |
| 151 | + } |
| 152 | + } |
| 153 | + if (nodesUp == numberOfNodes) { |
| 154 | + allNodesUp = true; |
| 155 | + break; |
| 156 | + } |
| 157 | + Thread.sleep(1000); |
| 158 | + } catch (InterruptedException e) { |
| 159 | + break; |
| 160 | + } |
| 161 | + } |
| 162 | + if (!allNodesUp) { |
| 163 | + LOG.error( |
| 164 | + "Driver sees only {} nodes UP instead of {} after waiting {}s", |
| 165 | + nodesUp, |
| 166 | + numberOfNodes, |
| 167 | + CLUSTER_WAIT_SECONDS); |
| 168 | + } |
86 | 169 | ResultSet rs = session.execute("SELECT * FROM system.local");
|
87 |
| - List<Row> rows = rs.all(); |
88 |
| - assertThat(rows).hasSize(1); |
89 |
| - LOG.trace("system.local contents: {}", rows.get(0).getFormattedContents()); |
| 170 | + assertThat(rs).isNotNull(); |
| 171 | + Row row = rs.one(); |
| 172 | + assertThat(row).isNotNull(); |
90 | 173 | Collection<Node> nodes = session.getMetadata().getNodes().values();
|
91 |
| - for (Node node : nodes) { |
92 |
| - LOG.trace("Found metadata node: {}", node); |
| 174 | + assertThat(nodes).hasSize(numberOfNodes); |
| 175 | + Iterator<Node> iterator = nodes.iterator(); |
| 176 | + while (iterator.hasNext()) { |
| 177 | + LOG.trace("Metadata node: " + iterator.next().toString()); |
93 | 178 | }
|
94 | 179 | Set<Node> filteredNodes;
|
95 | 180 | filteredNodes =
|
96 | 181 | nodes.stream()
|
97 |
| - .filter(x -> x.toString().contains("node-1.cluster.fake")) |
| 182 | + .filter(x -> x.toString().contains("test.cluster.fake")) |
98 | 183 | .collect(Collectors.toSet());
|
99 | 184 | assertThat(filteredNodes).hasSize(1);
|
100 |
| - InetSocketAddress address = |
101 |
| - (InetSocketAddress) filteredNodes.iterator().next().getEndPoint().resolve(); |
102 |
| - assertTrue(address.isUnresolved()); |
| 185 | + } |
| 186 | + try (CcmBridge ccmBridge = |
| 187 | + CcmBridge.builder().withNodes(numberOfNodes).withIpPrefix("127.0.1.").build()) { |
| 188 | + ccmBridge.create(); |
| 189 | + ccmBridge.start(); |
| 190 | + boolean allNodesUp = false; |
| 191 | + int nodesUp = 0; |
| 192 | + for (int i = 0; i < CLUSTER_WAIT_SECONDS; i++) { |
| 193 | + try { |
| 194 | + Collection<Node> nodes = session.getMetadata().getNodes().values(); |
| 195 | + nodesUp = 0; |
| 196 | + for (Node node : nodes) { |
| 197 | + if (node.getUpSinceMillis() > 0) { |
| 198 | + nodesUp++; |
| 199 | + } |
| 200 | + } |
| 201 | + if (nodesUp == numberOfNodes) { |
| 202 | + allNodesUp = true; |
| 203 | + break; |
| 204 | + } |
| 205 | + Thread.sleep(1000); |
| 206 | + } catch (InterruptedException e) { |
| 207 | + break; |
| 208 | + } |
| 209 | + } |
| 210 | + if (!allNodesUp) { |
| 211 | + LOG.error( |
| 212 | + "Driver sees only {} nodes UP instead of {} after waiting {}s", |
| 213 | + nodesUp, |
| 214 | + numberOfNodes, |
| 215 | + CLUSTER_WAIT_SECONDS); |
| 216 | + } |
| 217 | + ResultSet rs = session.execute("SELECT * FROM system.local"); |
| 218 | + assertThat(rs).isNotNull(); |
| 219 | + Row row = rs.one(); |
| 220 | + assertThat(row).isNotNull(); |
| 221 | + |
| 222 | + Collection<Node> nodes = session.getMetadata().getNodes().values(); |
| 223 | + assertThat(nodes).hasSize(numberOfNodes); |
| 224 | + Iterator<Node> iterator = nodes.iterator(); |
| 225 | + while (iterator.hasNext()) { |
| 226 | + LOG.trace("Metadata node: " + iterator.next().toString()); |
| 227 | + } |
| 228 | + Set<Node> filteredNodes; |
| 229 | + filteredNodes = |
| 230 | + nodes.stream() |
| 231 | + .filter(x -> x.toString().contains("test.cluster.fake")) |
| 232 | + .collect(Collectors.toSet()); |
| 233 | + if (filteredNodes.size() == 0) { |
| 234 | + LOG.error( |
| 235 | + "No metadata node with \"test.cluster.fake\" substring. The unresolved endpoint socket was likely " |
| 236 | + + "replaced with resolved one."); |
| 237 | + } else if (filteredNodes.size() > 1) { |
| 238 | + fail( |
| 239 | + "Somehow there is more than 1 node in metadata with unresolved hostname. This should not ever happen."); |
| 240 | + } |
| 241 | + } |
| 242 | + session.close(); |
| 243 | + } |
| 244 | + |
| 245 | + @SuppressWarnings("unused") |
| 246 | + public void run_replace_test_20_times() { |
| 247 | + for (int i = 1; i <= 20; i++) { |
| 248 | + LOG.info( |
| 249 | + "Running ({}/20}) {}", i, MockResolverIT.class.toString() + "#replace_cluster_test()"); |
| 250 | + replace_cluster_test(); |
103 | 251 | }
|
104 | 252 | }
|
105 | 253 |
|
106 |
| - private InetAddress getNodeInetAddress(CcmBridge ccmBridge, int nodeid) { |
| 254 | + private static InetAddress getNodeInetAddress(CcmBridge ccmBridge, int nodeid) { |
107 | 255 | try {
|
108 | 256 | return InetAddress.getByName(ccmBridge.getNodeIpAddress(nodeid));
|
109 | 257 | } catch (UnknownHostException e) {
|
|
0 commit comments