0
votes

We have one mongodb Replica Set with two instances(127.0.0.1:27017 - Primary, 127.0.0.1:27018- Secondary) and one arbiter(127.0.0.1:27019). When I step down from primary by using rs.stepDown(60) it should become secondary and secondary should become primary instance, and all write operations should happen in the secondary instance(primary after step down). But after stepping down I am getting the exception "Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.".

App.Config has the following keys:

<appSettings>
    <add key="mongoServerIP" value="127.0.0.1" />
    <add key="mongoServerPort" value="27017"/>
    <!--<add key="dbname" value="PSLRatingEngine"/>-->
    <add key="MongoDbDatabaseName" value="PSLRatingEngine" />
    <add key="testcollectionname" value="Test"/>
</appSettings>

C# code :

    using MongoDB.Driver;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ReplicaSetTest
    {
        class Program
        {
            static void Main(string[] args)
            {
    string mongoserverip = ConfigurationManager.AppSettings["mongoServerIP"].ToString();

    int port =   Convert.ToInt32(ConfigurationManager.AppSettings["mongoServerPort"].ToString());

   string dbname = ConfigurationManager.AppSettings["MongoDbDatabaseName"].ToString();

 string customercollectionname = ConfigurationManager.AppSettings["testcollectionname"].ToString();
            try
            {
                MongoServerSettings settings = new MongoServerSettings();
               settings.Server = new MongoServerAddress(mongoserverip, port);


                    // Create server object to communicate with our server
                    MongoServer server = new MongoServer(settings);
                    // Get our database instance to reach collections and data
                    var db = server.GetDatabase(dbname);
                    // Get user collection reference
                    var collection = db.GetCollection(customercollectionname);
                    int i = 0;
                    while(true)
                    {

                        Employee emp = new Employee();
                        emp.EmpId = i + 1000;
                        Random r = new Random();
                        emp.Age =  r.Next(20, 70);
                        emp.Salary = r.NextDouble() * 100000;
                        emp.Name = "emp" + i;
                        emp.Dept = "Engineering";
                        collection.Insert<Employee>(emp);
                        i++;
                    }   
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }

Please let me know how I can automatically switch from primary to secondary by C# code if anything happens to primary instance of the replica set.

1

1 Answers

1
votes

Suppose you send an acknowledge write to the primary and then primary is fried by an orbital ion cannon after it partially completes the write but before it finishes and acknowledges the write is complete. How should you "automatically switch from primary to secondary" in that case? What does it mean for that connection and operation?

Elections take a few seconds to happen, during which there is no primary to send writes to. What does the driver do while there's no primary? How can it maintain the same connections when the connections go to a node that isn't available anymore?

The point is, the errors from the application during failover are expected and your application needs to be able to deal with them. When the primary fails, MongoDB/the driver can't magically make everything work like nothing happened in all cases. The driver will, after a new primary is elected, automatically switch to the new one and then it will seem as if nothing happened.