Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic support for replica set connections #2690

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 40 additions & 10 deletions mongodb/vibe/db/mongo/connection.d
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,17 @@ final class MongoConnection {
this(MongoClientSettings cfg)
{
m_settings = cfg;

// Now let's check for features that are not yet supported.
if(m_settings.hosts.length > 1)
logWarn("Multiple mongodb hosts are not yet supported. Using first one: %s:%s",
m_settings.hosts[0].name, m_settings.hosts[0].port);
}

void connect()
{
void connectToHost(MongoHost host) {
bool isTLS;

/*
* TODO: Connect to one of the specified hosts taking into consideration
* options such as connect timeouts and so on.
*/
try {
m_conn = connectTCP(m_settings.hosts[0].name, m_settings.hosts[0].port);
m_conn = connectTCP(host.name, host.port);
m_conn.tcpNoDelay = true;
if (m_settings.ssl) {
auto ctx = createTLSContext(TLSContextKind.client);
Expand All @@ -178,7 +172,7 @@ final class MongoConnection {
ctx.useTrustedCertificateFile(m_settings.sslCAFile);
}

m_stream = createTLSStream(m_conn, ctx, m_settings.hosts[0].name);
m_stream = createTLSStream(m_conn, ctx, host.name);
isTLS = true;
}
else {
Expand All @@ -187,7 +181,7 @@ final class MongoConnection {
m_outRange = streamOutputRange(m_stream);
}
catch (Exception e) {
throw new MongoDriverException(format("Failed to connect to MongoDB server at %s:%s.", m_settings.hosts[0].name, m_settings.hosts[0].port), __FILE__, __LINE__, e);
throw new MongoDriverException(format("Failed to connect to MongoDB server at %s:%s.", host.name, host.port), __FILE__, __LINE__, e);
}

m_allowReconnect = false;
Expand Down Expand Up @@ -272,6 +266,40 @@ final class MongoConnection {
authenticate();
break;
}

logInfo("Connected to: %s master=%s secondary=%s", m_description.me, m_description.ismaster, m_description.secondary);
}

void connect()
{
Exception e;

foreach (host; m_settings.hosts) {
try {
connectToHost(host);
} catch(Exception ex) {
e = ex;
logError(e.msg);
}

if(!m_description.ismaster && m_description.secondary) {
logInfo("Connected to a secondary MongoDb node. The primary is: %s", m_description.primary);

auto split = m_description.primary.indexOf(":");

if(split != -1 && split < m_description.primary.length - 1) {
disconnect();
logInfo("Disconnected from the secondary MongoDb node.");

connectToHost(MongoHost(m_description.primary[0..split], m_description.primary[split+1..$].to!ushort));
return;
}
}
}

if(e !is null) {
throw e;
}
}

void disconnect()
Expand Down Expand Up @@ -746,6 +774,8 @@ struct ServerDescription
Nullable!int setVersion;
Nullable!BsonObjectID electionId;
string primary;
bool secondary;
bool ismaster;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find this in the documentation, is this an undocumented field, or am I just looking in the wrong place?

string lastUpdateTime = "infinity ago";
Nullable!int logicalSessionTimeoutMinutes;

Expand Down