Okay, a little more research has revealed some (hopefully) useful
info:
When the primary goes down, the secondary becomes the new primary, but
the instance's "State" property never changes from connected, so the
State property setter's "if (state != value)" line prevents
InstanceStateChanged ever getting called on that instance after it
becomes the new primary.
It seems that once a write is attempted...
books.Insert(new BsonDocument { { "Author", "Ernest Hemingway" },
{ "Title", "For Whom The Bell Tolls" } });
...the below call stack happens...
MongoDB.Driver.MongoServer.InstanceStateChanged(object sender =
{MongoDB.Driver.MongoServerInstance}, object args = null)
MongoDB.Driver.MongoServerInstance.State.set(MongoDB.Driver.MongoServerState
value = Connected)
MongoDB.Driver.MongoServerInstance.VerifyState(MongoDB.Driver.Internal.MongoConnection
connection = {MongoDB.Driver.Internal.MongoConnection})
MongoDB.Driver.MongoServerInstance.Connect(bool slaveOk = true)
MongoDB.Driver.Internal.ReplicaSetConnector.ConnectWorkItem(object
argsObject =
{MongoDB.Driver.Internal.ReplicaSetConnector.ConnectArgs})
[External Code]
...which flips the instance's state from connected to connecting (and
eventually back again to connected), causing Instances.Primary to be
set to this new primary instance, however, this call stack ONLY
happens when a write is attempted. If I just continue attempting to
read (since SlaveOk is true), the state remains "Connected"
indefinitely, and no new Primary gets set.
Do you need me to create a JIRA ticket for this?
Thanks again,
Mo
P.S. This may have been difficult to catch, because if there are
multiple secondaries when the primary goes down, one of them will
become the next primary and, although MongServer.Primary will continue
to be null, the following part of MongServer.ChooseServerInstance(bool
slaveOk) will ensure that reads continue to work fine against one of
the other secondaries...
if (instance.State == MongoServerState.Connected &&
(instance.IsSecondary || instance.IsPassive)) {
return instance;