We have an application with C# and AspNet Core 2.2 and we use AWS document db, our db is configured with replicaSet which 1 is main and 3 secondaries.
The application start fine on production but when it reaches a certain amount of writes the application starts to fail and new instances of mongo are created, this happens until no more mongo connections can be created and the timeout error starts and aws starts terminating machines and recreating them again, even thou it starts fast, there is still a time in which the application doesn't work giving out 500 errors.
I have alredy checked A timeout occured after 30000ms selecting a server using CompositeServerSelector which is a problem with mongolabs and it doesn't solve our problem and tried to add "?connect=replicaSet" to the end of our connection string and the errors still happened.
Here is our connection string
ConnectionString": "mongodb://root:someurl:27017/?ssl=true&ssl_ca_certs=archive.pem&replicaSet=rs0
Here is where we make mongo connection
Our MongoDbContext:
using DataMongo.Entities;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using System;
using System.IO;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver.Core.Events;
namespace DataMongo
{
public class MongoDbContext
{
private IMongoDatabase _database { get; }
public MongoDbContext(MongoSettings settings, bool readOnly = false)
{
try
{
var connectionString = readOnly ? settings.ConnectionStringReadOnly : settings.ConnectionString;
var ms = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
ms.AllowInsecureTls = true;
var client = new MongoClient(ms);
if (client != null)
_database = client.GetDatabase(settings.Database);
}
catch (Exception ex)
{
throw new Exception("Não foi possível se conectar com o servidor.", ex);
}
}
public IMongoCollection<Session> Sessions
{
get
{
return _database.GetCollection<Session>("Sessions");
}
}
public IMongoCollection<PageViewList> PageViewList
{
get
{
return _database.GetCollection<PageViewList>("PageViewList");
}
}
public IMongoCollection<Pagina> Paginas
{
get
{
return _database.GetCollection<Pagina>("Paginas");
}
}
public IMongoCollection<Cache> Caches
{
get
{
return _database.GetCollection<Cache>("Caches");
}
}
}
}
And our MongoDbContextFactory:
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DataMongo
{
public class MongoDbContextFactory : IMongoDbContextFactory
{
private MongoDbContext _db;
private MongoDbContext _dbEdit;
private string _connection;
private string _connectionEdit;
public MongoDbContextFactory(MongoSettings mongoSettings)
{
_mongoSettings = mongoSettings;
}
private MongoSettings _mongoSettings;
public MongoDbContext MongoContext
{
get { return _db ?? (_db = Create()); }
}
public MongoDbContext MongoContextReadOnly
{
get { return _dbEdit ?? (_dbEdit = Create(readOnly: true)); }
}
private MongoDbContext Create(bool readOnly = false)
{
return new MongoDbContext(_mongoSettings, readOnly);
}
}
}
Here is the Stacktrace:
TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Automatic", Type : "Standalone", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27017" }", EndPoint: "Unspecified/localhost:27017", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: Connection refused [::1]:27017 at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) at System.Net.Sockets.Socket.<>c.<ConnectAsync>b__271_0(IAsyncResult iar) --- End of stack trace from previous location where exception was thrown --- at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken) --- End of inner exception stack trace --- at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken) at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)", LastUpdateTimestamp: "2020-03-06T18:05:54.1680430Z" }] }.