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

Non-deterministic collection initialization fixed #133

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
wassim-k marked this conversation as resolved.
Show resolved Hide resolved
{
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;
}
}
}
Loading