DBZ-4339 Fix errant shard host names and add additional java docs / assertions.

This commit is contained in:
Bobby Tiernay 2022-11-25 16:12:08 -05:00 committed by Jiri Pechanec
parent 6f9ee66678
commit 8c8804665f
4 changed files with 30 additions and 12 deletions

View File

@ -299,9 +299,15 @@ jobs:
restore-keys: |
maven-debezium-test-build-${{ hashFiles('**/pom.xml') }}
# Unfortunately, this is required so that multi-node replica sets can properly resolve both within cluster using
# the user-defined Docker bridge network and from the host. While it is possible to use IPs in the `rs.initialize`
# call, this is not possible on Docker Desktop for Mac as the `docker0` bridge network is not exposed. While there
# are other techniques that can be used to make this more transparent to the user (e.g. JVM DNS hijacking via
# reflection) this seemed the easiest and most consistent / reliable. However, for local testing it does require
# adding these host names to the local `/etc/hosts` as well.
- name: Add MongoDB test container hosts to /etc/hosts
run: >
for host in test-mongo{1..3} test-mongos{1..3} test-mongo-configdb{1..3} test-shard{1..3}-replica{1..3}; do
for host in test-mongo{1..3} test-mongos{1..3} test-mongo-configdb{1..3} test-mongo-shard{1..3}-replica{1..3}; do
echo -e "127.0.0.1\t$host" | sudo tee -a /etc/hosts;
done

View File

@ -25,7 +25,13 @@
import com.mongodb.ServerAddress;
/**
* A MongoDB node.
* A container for running a single MongoDB {@code mongod} or {@code mongos} process.
* <p>
* In order to interact with a running container from the host using a client driver, the container's network alias
* ({@link #name}) must be resolvable from the host. On most systems this will require configuring {@code /etc/hosts}
* to have an entry that maps {@link #name} to {@code 127.0.0.1}. To make this portable across systems, fixed ports are
* used on the host and are mapped exactly to the container. Random free ports are assigned to minimize the chance of
* conflicts.
*/
public class MongoDbNode extends GenericContainer<MongoDbNode> {

View File

@ -27,8 +27,6 @@
/**
* A MongoDB sharded cluster.
*
* @see <a href="https://issues.redhat.com/browse/DBZ-5857">DBZ-5857</a>
*/
public class MongoDbShardedCluster implements Startable {

View File

@ -6,6 +6,10 @@
package io.debezium.connector.mongodb.cluster;
import static io.debezium.connector.mongodb.cluster.MongoDbShardedCluster.shardedCluster;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.rangeClosed;
import static java.util.stream.StreamSupport.stream;
import static org.assertj.core.api.Assertions.assertThat;
import org.bson.Document;
import org.junit.Test;
@ -13,6 +17,7 @@
import org.slf4j.LoggerFactory;
import com.mongodb.ConnectionString;
import com.mongodb.ReadPreference;
import com.mongodb.client.MongoClients;
/**
@ -28,8 +33,10 @@ public void testCluster() {
logger.info("Starting {}...", cluster);
cluster.start();
String readPreference = "primary";
var connectionString = new ConnectionString(cluster.getConnectionString() + "/?readPreference=" + readPreference);
// Create a connection string with a desired read preference
var readPreference = ReadPreference.primary();
var connectionString = new ConnectionString(cluster.getConnectionString() + "/?readPreference=" + readPreference.getName());
logger.info("Connecting to cluster: {}", connectionString);
try (var client = MongoClients.create(connectionString)) {
logger.info("Connected to cluster: {}", client.getClusterDescription());
@ -41,16 +48,17 @@ public void testCluster() {
cluster.shardCollection(databaseName, collectionName, "name");
var collection = client.getDatabase(databaseName).getCollection(collectionName);
for (int i = 1; i <= 10; i++) {
collection.insertOne(Document.parse("{name:" + i + "}"));
}
rangeClosed(1, 10)
.mapToObj(i -> Document.parse("{name:" + i + "}"))
.forEach(collection::insertOne);
for (var doc : collection.find()) {
logger.info("Doc: {}", doc);
}
var docs = stream(collection.find().spliterator(), false)
.collect(toList());
assertThat(docs).hasSize(10);
logger.info("Connected to cluster: {}", client.getClusterDescription());
cluster.addShard();
logger.info("Connected to cluster: {}", client.getClusterDescription());
cluster.removeShard();
}