Skip to content

Commit

Permalink
CSHARP-3431: Make MongoClient IDisposable (mongodb#1461)
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisDog authored Oct 9, 2024
1 parent 2086b5a commit 10658fe
Show file tree
Hide file tree
Showing 78 changed files with 598 additions and 602 deletions.
10 changes: 6 additions & 4 deletions benchmarks/MongoDB.Driver.Benchmarks/BenchmarkHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.TestHelpers;

namespace MongoDB.Benchmarks
{
Expand Down Expand Up @@ -79,13 +78,16 @@ public static class MongoConfiguration
public const string PerfTestDatabaseName = "perftest";
public const string PerfTestCollectionName = "corpus";

public static DisposableMongoClient CreateDisposableClient()
public static IMongoClient CreateClient()
{
var mongoUri = Environment.GetEnvironmentVariable("MONGODB_URI");
var client = mongoUri != null ? new MongoClient(mongoUri) : new MongoClient();
var settings = mongoUri != null ? MongoClientSettings.FromConnectionString(mongoUri) : new();
settings.ClusterSource = DisposingClusterSource.Instance;

var client = new MongoClient(settings);
client.DropDatabase(PerfTestDatabaseName);

return new DisposableMongoClient(client, null);
return client;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
using MongoDB.Bson.TestHelpers;
using MongoDB.Driver;
using MongoDB.Driver.Encryption;
using MongoDB.Driver.TestHelpers;

namespace MongoDB.Benchmarks
{
Expand All @@ -31,7 +30,7 @@ public class LibmongocryptBindingBenchmark
private const string LocalMasterKey = "Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk";

private byte[] _encryptedValuesDocumentBytes;
private DisposableMongoClient _disposableKeyVaultClient;
private IMongoClient _disposableKeyVaultClient;
private IAutoEncryptionLibMongoCryptController _libMongoCryptController;

[Params(1)]
Expand All @@ -56,8 +55,9 @@ public void Setup()

var clientSettings = MongoClientSettings.FromConnectionString("mongodb://localhost");
clientSettings.AutoEncryptionOptions = autoEncryptionOptions;
clientSettings.ClusterSource = DisposingClusterSource.Instance;

_disposableKeyVaultClient = new DisposableMongoClient(new MongoClient(clientSettings), null);
_disposableKeyVaultClient = new MongoClient(clientSettings);

var keyVaultDatabase = _disposableKeyVaultClient.GetDatabase(keyVaultNamespace.DatabaseNamespace.DatabaseName);
keyVaultDatabase.DropCollection(keyVaultNamespace.CollectionName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace MongoDB.Benchmarks.MultiDoc
[BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)]
public class FindManyBenchmark
{
private DisposableMongoClient _client;
private IMongoClient _client;
private IMongoCollection<BsonDocument> _collection;
private BsonDocument _tweetDocument;

Expand All @@ -36,7 +36,7 @@ public class FindManyBenchmark
[GlobalSetup]
public void Setup()
{
_client = MongoConfiguration.CreateDisposableClient();
_client = MongoConfiguration.CreateClient();
_tweetDocument = ReadExtendedJson("single_and_multi_document/tweet.json");
_collection = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName).GetCollection<BsonDocument>(MongoConfiguration.PerfTestCollectionName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
using System.IO;
using BenchmarkDotNet.Attributes;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.TestHelpers;
using static MongoDB.Benchmarks.BenchmarkHelper;

namespace MongoDB.Benchmarks.MultiDoc
Expand All @@ -26,7 +26,7 @@ namespace MongoDB.Benchmarks.MultiDoc
[BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)]
public class GridFsDownloadBenchmark
{
private DisposableMongoClient _client;
private IMongoClient _client;
private ObjectId _fileId;
private GridFSBucket _gridFsBucket;

Expand All @@ -36,7 +36,7 @@ public class GridFsDownloadBenchmark
[GlobalSetup]
public void Setup()
{
_client = MongoConfiguration.CreateDisposableClient();
_client = MongoConfiguration.CreateClient();
_gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName));
_fileId = _gridFsBucket.UploadFromStream("gridfstest", File.OpenRead($"{DataFolderPath}single_and_multi_document/gridfs_large.bin"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

using System.IO;
using BenchmarkDotNet.Attributes;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.TestHelpers;
using static MongoDB.Benchmarks.BenchmarkHelper;

namespace MongoDB.Benchmarks.MultiDoc
Expand All @@ -25,7 +25,7 @@ namespace MongoDB.Benchmarks.MultiDoc
[BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)]
public class GridFsUploadBenchmark
{
private DisposableMongoClient _client;
private IMongoClient _client;
private byte[] _fileBytes;
private GridFSBucket _gridFsBucket;

Expand All @@ -35,7 +35,7 @@ public class GridFsUploadBenchmark
[GlobalSetup]
public void Setup()
{
_client = MongoConfiguration.CreateDisposableClient();
_client = MongoConfiguration.CreateClient();
_fileBytes = File.ReadAllBytes($"{DataFolderPath}single_and_multi_document/gridfs_large.bin");
_gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace MongoDB.Benchmarks.MultiDoc
[BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)]
public class InsertManyLargeBenchmark
{
private DisposableMongoClient _client;
private IMongoClient _client;
private IMongoCollection<BsonDocument> _collection;
private IMongoDatabase _database;
private IEnumerable<BsonDocument> _largeDocuments;
Expand All @@ -38,7 +38,7 @@ public class InsertManyLargeBenchmark
[GlobalSetup]
public void Setup()
{
_client = MongoConfiguration.CreateDisposableClient();
_client = MongoConfiguration.CreateClient();
_database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName);

var largeDocument = ReadExtendedJson("single_and_multi_document/large_doc.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace MongoDB.Benchmarks.MultiDoc
[BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)]
public class InsertManySmallBenchmark
{
private DisposableMongoClient _client;
private IMongoClient _client;
private IMongoCollection<BsonDocument> _collection;
private IMongoDatabase _database;
private IEnumerable<BsonDocument> _smallDocuments;
Expand All @@ -38,7 +38,7 @@ public class InsertManySmallBenchmark
[GlobalSetup]
public void Setup()
{
_client = MongoConfiguration.CreateDisposableClient();
_client = MongoConfiguration.CreateClient();
_database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName);

var smallDocument = ReadExtendedJson("single_and_multi_document/small_doc.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
using System.IO;
using BenchmarkDotNet.Attributes;
using MongoDB.Bson.TestHelpers;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.TestHelpers;
using static MongoDB.Benchmarks.BenchmarkHelper;

namespace MongoDB.Benchmarks.ParallelBench
Expand All @@ -27,7 +27,7 @@ namespace MongoDB.Benchmarks.ParallelBench
[BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)]
public class GridFSMultiFileDownloadBenchmark
{
private DisposableMongoClient _client;
private IMongoClient _client;
private GridFSBucket _gridFsBucket;
private string _tmpDirectoryPath;
private ConcurrentQueue<(string, int)> _filesToDownload;
Expand All @@ -38,7 +38,7 @@ public class GridFSMultiFileDownloadBenchmark
[GlobalSetup]
public void Setup()
{
_client = MongoConfiguration.CreateDisposableClient();
_client = MongoConfiguration.CreateClient();
_gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName));
_gridFsBucket.Drop();
_tmpDirectoryPath = $"{DataFolderPath}parallel/tmpGridFS";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
using System.IO;
using BenchmarkDotNet.Attributes;
using MongoDB.Bson.TestHelpers;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.TestHelpers;
using static MongoDB.Benchmarks.BenchmarkHelper;

namespace MongoDB.Benchmarks.ParallelBench
Expand All @@ -27,7 +27,7 @@ namespace MongoDB.Benchmarks.ParallelBench
[BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)]
public class GridFSMultiFileUploadBenchmark
{
private DisposableMongoClient _client;
private IMongoClient _client;
private GridFSBucket _gridFsBucket;
private ConcurrentQueue<(string, int)> _filesToUpload;

Expand All @@ -37,7 +37,7 @@ public class GridFSMultiFileUploadBenchmark
[GlobalSetup]
public void Setup()
{
_client = MongoConfiguration.CreateDisposableClient();
_client = MongoConfiguration.CreateClient();
_gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName));
_filesToUpload = new ConcurrentQueue<(string, int)>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace MongoDB.Benchmarks.ParallelBench
[BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)]
public class MultiFileExportBenchmark
{
private DisposableMongoClient _client;
private IMongoClient _client;
private IMongoCollection<BsonDocument> _collection;
private IMongoDatabase _database;
private string _tmpDirectoryPath;
Expand All @@ -42,7 +42,7 @@ public class MultiFileExportBenchmark
[GlobalSetup]
public void Setup()
{
_client = MongoConfiguration.CreateDisposableClient();
_client = MongoConfiguration.CreateClient();
_database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName);
_collection = _database.GetCollection<BsonDocument>(MongoConfiguration.PerfTestCollectionName);
_tmpDirectoryPath = $"{DataFolderPath}parallel/tmpLDJSON";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace MongoDB.Benchmarks.ParallelBench
[BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)]
public class MultiFileImportBenchmark
{
private DisposableMongoClient _client;
private IMongoClient _client;
private IMongoCollection<BsonDocument> _collection;
private IMongoDatabase _database;
private ConcurrentQueue<(string, int)> _filesToUpload;
Expand All @@ -41,7 +41,7 @@ public class MultiFileImportBenchmark
[GlobalSetup]
public void Setup()
{
_client = MongoConfiguration.CreateDisposableClient();
_client = MongoConfiguration.CreateClient();
_database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName);
_filesToUpload = new ConcurrentQueue<(string, int)>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace MongoDB.Benchmarks.SingleDoc
[BenchmarkCategory(DriverBenchmarkCategory.SingleBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)]
public class FindOneBenchmark
{
private DisposableMongoClient _client;
private IMongoClient _client;
private IMongoCollection<BsonDocument> _collection;
private BsonDocument _tweetDocument;

Expand All @@ -36,7 +36,7 @@ public class FindOneBenchmark
[GlobalSetup]
public void Setup()
{
_client = MongoConfiguration.CreateDisposableClient();
_client = MongoConfiguration.CreateClient();
_collection = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName).GetCollection<BsonDocument>(MongoConfiguration.PerfTestCollectionName);
_tweetDocument = ReadExtendedJson("single_and_multi_document/tweet.json");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace MongoDB.Benchmarks.SingleDoc
[BenchmarkCategory(DriverBenchmarkCategory.SingleBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)]
public class InsertOneLargeBenchmark
{
private DisposableMongoClient _client;
private IMongoClient _client;
private IMongoCollection<BsonDocument> _collection;
private IMongoDatabase _database;
private BsonDocument _largeDocument;
Expand All @@ -36,7 +36,7 @@ public class InsertOneLargeBenchmark
[GlobalSetup]
public void Setup()
{
_client = MongoConfiguration.CreateDisposableClient();
_client = MongoConfiguration.CreateClient();
_database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName);
_largeDocument = ReadExtendedJson("single_and_multi_document/large_doc.json");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace MongoDB.Benchmarks.SingleDoc
[BenchmarkCategory(DriverBenchmarkCategory.SingleBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)]
public class InsertOneSmallBenchmark
{
private DisposableMongoClient _client;
private IMongoClient _client;
private IMongoCollection<BsonDocument> _collection;
private IMongoDatabase _database;
private BsonDocument _smallDocument;
Expand All @@ -36,7 +36,7 @@ public class InsertOneSmallBenchmark
[GlobalSetup]
public void Setup()
{
_client = MongoConfiguration.CreateDisposableClient();
_client = MongoConfiguration.CreateClient();
_database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName);
_smallDocument = ReadExtendedJson("single_and_multi_document/small_doc.json");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace MongoDB.Benchmarks.SingleDoc
[BenchmarkCategory("RunBench")]
public class RunCommandBenchmark
{
private DisposableMongoClient _client;
private IMongoClient _client;
private IMongoDatabase _database;

[Params(130_000)]
Expand All @@ -33,7 +33,7 @@ public class RunCommandBenchmark
[GlobalSetup]
public void Setup()
{
_client = BenchmarkHelper.MongoConfiguration.CreateDisposableClient();
_client = BenchmarkHelper.MongoConfiguration.CreateClient();
_database = _client.GetDatabase("admin");
}

Expand Down
41 changes: 41 additions & 0 deletions src/MongoDB.Driver/IClusterSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using MongoDB.Driver.Core.Clusters;

namespace MongoDB.Driver
{
internal interface IClusterSource
{
public IClusterInternal Get(ClusterKey key);
public void Return(IClusterInternal cluster);
}

internal sealed class DefaultClusterSource : IClusterSource
{
public static IClusterSource Instance { get; } = new DefaultClusterSource();

public IClusterInternal Get(ClusterKey key) => ClusterRegistry.Instance.GetOrCreateCluster(key);
public void Return(IClusterInternal cluster) { /* Do nothing for now, until cluster caching can handle disposing */ }
}

internal sealed class DisposingClusterSource : IClusterSource
{
public static IClusterSource Instance { get; } = new DisposingClusterSource();

public IClusterInternal Get(ClusterKey key) => ClusterRegistry.Instance.GetOrCreateCluster(key);
public void Return(IClusterInternal cluster) => ClusterRegistry.Instance.UnregisterAndDisposeCluster(cluster);
}
}
3 changes: 2 additions & 1 deletion src/MongoDB.Driver/IMongoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* limitations under the License.
*/

using System;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
Expand All @@ -23,7 +24,7 @@ namespace MongoDB.Driver
/// <summary>
/// The client interface to MongoDB.
/// </summary>
public interface IMongoClient
public interface IMongoClient : IDisposable
{
/// <summary>
/// Gets the cluster.
Expand Down
Loading

0 comments on commit 10658fe

Please sign in to comment.