Skip to content

Commit

Permalink
Non-deterministic collection initialization fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
NorekZ authored and wassim-k committed Mar 5, 2024
1 parent 536af98 commit 20ce847
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions Orleans.Providers.MongoDB/Utils/CollectionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@ public class CollectionBase<TEntity>

private readonly IMongoDatabase mongoDatabase;
private readonly IMongoClient mongoClient;
private readonly Lazy<IMongoCollection<TEntity>> mongoCollection;
private readonly Action<MongoCollectionSettings> collectionConfigurator;
private IMongoCollection<TEntity> mongoCollection;
private readonly object mongoCollectionInitializerLock = new();
private readonly bool createShardKey;

protected IMongoCollection<TEntity> Collection
{
get { return mongoCollection.Value; }
get
{
if (mongoCollection == null)
{
lock (mongoCollectionInitializerLock)
{
mongoCollection ??= CreateCollection(collectionConfigurator);
}
}

return mongoCollection;
}
}

protected IMongoDatabase Database
Expand All @@ -44,9 +57,9 @@ protected CollectionBase(IMongoClient mongoClient, string databaseName,
Action<MongoCollectionSettings> collectionConfigurator, bool createShardKey)
{
this.mongoClient = mongoClient;
this.collectionConfigurator = collectionConfigurator;

mongoDatabase = mongoClient.GetDatabase(databaseName);
mongoCollection = CreateCollection(collectionConfigurator);

this.createShardKey = createShardKey;
}
Expand All @@ -65,51 +78,40 @@ protected virtual void SetupCollection(IMongoCollection<TEntity> collection)
{
}

private Lazy<IMongoCollection<TEntity>> CreateCollection(Action<MongoCollectionSettings> collectionConfigurator)
private IMongoCollection<TEntity> CreateCollection(Action<MongoCollectionSettings> collectionConfigurator)
{
return new Lazy<IMongoCollection<TEntity>>(() =>
{
var collectionFilter = new ListCollectionNamesOptions
{
Filter = Builders<BsonDocument>.Filter.Eq("name", CollectionName())
};
var collectionName = CollectionName();

if (!mongoDatabase.ListCollectionNames(collectionFilter).Any())
{
mongoDatabase.CreateCollection(CollectionName());
}
var collectionSettings = CollectionSettings() ?? new MongoCollectionSettings();
var collectionSettings = CollectionSettings() ?? new MongoCollectionSettings();

collectionConfigurator?.Invoke(collectionSettings);
collectionConfigurator?.Invoke(collectionSettings);

var databaseCollection = mongoDatabase.GetCollection<TEntity>(
CollectionName(),
collectionSettings);
var databaseCollection = mongoDatabase.GetCollection<TEntity>(
collectionName,
collectionSettings);

if (this.createShardKey)
if (createShardKey)
{
try
{
try
mongoClient.GetDatabase("admin").RunCommand<BsonDocument>(new BsonDocument
{
Database.RunCommand<BsonDocument>(new BsonDocument
["shardCollection"] = $"{mongoDatabase.DatabaseNamespace.DatabaseName}.{collectionName}",
["key"] = new BsonDocument
{
["key"] = new BsonDocument
{
["_id"] = "hashed"
},
["shardCollection"] = $"{mongoDatabase.DatabaseNamespace.DatabaseName}.{CollectionName()}"
});
}
catch (MongoException)
{
// Shared key probably created already.
}
["_id"] = "hashed"
}
});
}
catch (MongoException)
{
// Shared key probably created already.
}
}

SetupCollection(databaseCollection);
SetupCollection(databaseCollection);

return databaseCollection;
});
return databaseCollection;
}
}
}

0 comments on commit 20ce847

Please sign in to comment.