{- | A replica set is a set of servers that mirror each other (a non-replicated server can act like a replica set of one). One server in a replica set is the master and the rest are slaves. When the master goes down, one of the slaves becomes master. The ReplicaSet object in this client maintains a list of servers that it currently knows are in the set. It refreshes this list every time it establishes a new connection with one of the servers in the set. Each server in the set knows who the other member in the set are, and who is master. The user asks the ReplicaSet object for a new master or slave connection. When a connection fails, the user must ask the ReplicaSet for a new connection (which most likely will connect to another server since the previous one failed). When connecting to a new server you loose all session state that was stored with the old server, which includes open cursors and temporary map-reduce output collections. Attempting to read from a lost cursor on a new server will raise a ServerFailure exception. Attempting to read a lost map-reduce temp output on a new server will return an empty set (not an error, like it maybe should). -}
-- ^ Read string \"host:port\" as 'Server host port' or \"host\" as 'server host' (default port). Error if string does not match either syntax.
readHostPort=runIdentity.readHostPortF
-- * Replica Set
newtypeReplicaSet=ReplicaSet(IORef[Server])
-- ^ Reference to a replica set of servers. Ok if really not a replica set and just a stand-alone server, in which case it acts like a replica set of one.
replicaSet::[Server]->IOReplicaSet
-- ^ Create a reference to a replica set with servers as the initial seed list (a subset of the servers in the replica set)
-- ^ Configuration info of a server in a replica set. Contains all the servers in the replica set plus its role in that set (master, slave, or arbiter)
isMaster::ReplicaInfo->Bool
-- ^ Is the replica server described by this info a master/primary (not slave or arbiter)?
isMaster(ReplicaInfo_i)=true1"ismaster"i
isSlave::ReplicaInfo->Bool
-- ^ Is the replica server described by this info a slave/secondary (not master or arbiter)
isSlave=not.isMaster-- TODO: distinguish between slave and arbiter
allReplicas::ReplicaInfo->[Server]
-- ^ All replicas in set according to this replica configuration info.
-- If server is stand-alone then it won't have \"hosts\" in it configuration, in which case we return the server by itself.
-- ^ Create a connection to a master or slave in the replica set. Don't forget to close connection when you are done using it even if Failure exception is raised when using it. newConnection returns Left if failed to connect to any server in replica set.
-- TODO: prefer slave over master when SlaveOk and both are available.
-- ^ Connect to first server that succeeds and is master/slave, otherwise return list of failed connections plus info of connections that succeeded but were not master/slave.
connectFirstmos=connectFirst'([],[])where
connectFirst'(fs,is)[]=return$Left(fs,is)
connectFirst'(fs,is)(s:ss)=do
e<-runErrorT$do
c<-ErrorT(connects)
i<-ErrorT(getReplicaInfo'c)
return(c,i)
caseeof
Leftf->connectFirst'((s,f):fs,is)ss
Right(c,i)->ifisMSmosi
thenreturn$Right(c,i)
elsedo
closeConnectionc
connectFirst'((s,userError$"not a "++showmos):fs,i:is)ss
connect::Server->IO(EitherIOErrorConnection)
-- ^ Create a connection to the given server (as opposed to connecting to some server in a replica set via 'newConnection'). Return Left IOError if failed to connect.