2

I get the following output followed by an exception. I am working on writing integration test cases for the transactional feature in embeded mongoDB with Spring Boot.

21-03-07 01:44:12.169  INFO 20256 --- [localhost:27021] org.mongodb.driver.cluster               : Server localhost:27021 does not appear to be a member of an initiated replica set.
2021-03-07 01:44:12.667  INFO 20256 --- [localhost:27023] org.mongodb.driver.cluster               : Server localhost:27023 does not appear to be a member of an initiated replica set.
2021-03-07 01:44:12.668  INFO 20256 --- [localhost:27021] org.mongodb.driver.cluster               : Server localhost:27021 does not appear to be a member of an initiated replica set.
2021-03-07 01:44:13.168  INFO 20256 --- [localhost:27023] org.mongodb.driver.cluster               : Server localhost:27023 does not appear to be a member of an initiated replica set.
2021-03-07 01:44:13.169  INFO 20256 --- [localhost:27021] org.mongodb.driver.cluster               : Server localhost:27021 does not appear to be a member of an initiated replica set.

I provide below the code for reference. What is wrong here. Please help me.

@Profile("test")
@ActiveProfiles("test")
//@Configuration
@TestConfiguration
public class TestMongoDBConfig implements InitializingBean, DisposableBean {

    private MongodExecutable executable;
    
    private MongoTemplate mongoTemplate;
    
    private MongodProcess node1Mongod;
    private MongodProcess node2Mongod;
    private MongodExecutable node1MongodExe;
    private MongodExecutable node2MongodExe;
    private int node1Port = 27021;
    private int node2Port = 27023;

    private static final String CONNECTION_STRING = "mongodb://%s:%d";
    private int port = 27021;

    
    @Override
    public void afterPropertiesSet() throws Exception {


        
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        MongodStarter runtime = MongodStarter.getDefaultInstance();
        node1MongodExe = runtime.prepare(new MongodConfigBuilder().version(Version.Main.V4_0)
                  .withLaunchArgument("--replSet", "rs0")
                  .cmdOptions(new MongoCmdOptionsBuilder().useNoJournal(false).build())
                  .net(new Net(node1Port, Network.localhostIsIPv6())).build());
          node1Mongod = node1MongodExe.start();

          node2MongodExe = runtime.prepare(new MongodConfigBuilder().version(Version.Main.V4_0)
                  .withLaunchArgument("--replSet", "rs0")
                  .cmdOptions(new MongoCmdOptionsBuilder().useNoJournal(false).build())
                  .net(new Net(node2Port, Network.localhostIsIPv6())).build());
          node2Mongod = node2MongodExe.start();
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          
    }

    /**
     * Mongo client.
     *
     * @return the mongo client
     */
//  @Primary
    @Bean(name = "test1")
    public MongoClient mongoClient() {
        
        ClusterSettings.Builder clusterBuilder = ClusterSettings.builder();
        List<ServerAddress> seeds = new ArrayList<>();
        seeds.add(new ServerAddress("localhost", node1Port));
        seeds.add(new ServerAddress("localhost", node2Port));
        
        clusterBuilder.hosts(seeds);
        MongoClientSettings.Builder builder = MongoClientSettings.builder();
        ClusterSettings clusterSettings = clusterBuilder.requiredReplicaSetName("rs0").build();
        MongoClientSettings settings = builder
                .applyToClusterSettings(builder1 -> builder1.applySettings(clusterSettings))
                .writeConcern(WriteConcern.ACKNOWLEDGED)
                .build();
        MongoClient mongoClient = MongoClients.create(settings);
        System.out.println("--------------------------------------");
        System.out.println("MongoClient : "+mongoClient);
        System.out.println("--------------------------------------");
        
        
        
        MongoDatabase adminDatabase = mongoClient.getDatabase("admin");
        Document config = new Document("_id", "rs0");
        BasicDBList members = new BasicDBList();
        members.add(new Document("_id", 0).append("host", "localhost:" + node1Port));
        members.add(new Document("_id", 1).append("host", "localhost:" + node2Port));
        config.put("members", members);
        adminDatabase.runCommand(new Document("replSetInitiate", config));
        System.out.println(">>>>>>>>" + adminDatabase.runCommand(new Document("replSetGetStatus", 1)));
        MongoDatabase funDb = mongoClient.getDatabase("fun");
        MongoCollection<Document> testCollection = funDb.getCollection("test");
        System.out.println(">>>>>>>> inserting data");
        testCollection.insertOne(new Document("fancy", "value"));
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println(">>>>>>>> finding data");
        System.out.println("2222222>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        

        return mongoClient;
    }

    /**
     * Destroy.
     *
     * @throws Exception the exception
     */
    @Override
    public void destroy() throws Exception {
//      executable.stop();
        node1MongodExe.stop();
        node2MongodExe.stop();
    }
}
PythonLearner
  • 1,416
  • 7
  • 22

1 Answers1

0

When creating a new replica set you must configure each node with the same replica set name, and then initiate the replica set so the nodes know about each other.

After you initially start both nodes, neither is aware that there are other nodes in the replica set, and neither one has initialized the replica set configuration document, or the operations log.

When you build the connection options, you are explicitly setting requiredReplicaSetName("rs0"), but there is no replica set with that name yet, so when you actually connect, the driver attempts to find out which node is primary, and discovers there is no configuration yet. That logs is an informational message to let you know.

In this case, there is no replica set because it hasn't been configured just yet, which is expected because you are connecting for the purpose of running replSetInitiate.

Joe
  • 25,000
  • 3
  • 22
  • 44