Skip to content

Commit da282ad

Browse files
committed
Move to src folder
1 parent 3fa56cd commit da282ad

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed
File renamed without changes.
File renamed without changes.

Program.cs renamed to src/Program.cs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private static async Task<int> Main()
1717
Log.Logger = new LoggerConfiguration()
1818
.MinimumLevel.Debug()
1919
.WriteTo.Console()
20-
.WriteTo.File("mongo.txt")
20+
//.WriteTo.File("mongo.txt")
2121
.CreateLogger();
2222

2323
var envMongoConnectionString = Environment.GetEnvironmentVariable("MongoConnectionString");
@@ -50,8 +50,10 @@ private static async Task<int> Main()
5050
Log.Information("Starting.. IsDryRun: {0}. Connection information: {@1}", isDryRun, mongoSettings);
5151

5252
var sw = new Stopwatch();
53+
var filesDeleted = 0;
54+
var orphanedChunks = 0UL;
5355
var validFiles = new HashSet<ObjectId>();
54-
var deletedFiles = new HashSet<ObjectId>();
56+
var filesToDelete = new HashSet<ObjectId>();
5557

5658
// The projection and hint let us do this whole thing as a covered query
5759
// https://docs.mongodb.com/manual/core/query-optimization/#covered-query
@@ -61,9 +63,6 @@ private static async Task<int> Main()
6163
Hint = "files_id_1_n_1"
6264
};
6365

64-
// Just to see that the program is working as this can take a while.
65-
var timer = new Timer((s) => Log.Information("Valid files count: {0}", validFiles.Count), null, 0, 10_000);
66-
6766
try
6867
{
6968
Log.Information("Creating cursor and starting search of orphaned chunks..");
@@ -72,12 +71,14 @@ private static async Task<int> Main()
7271
using var chunksCursor = await chunks.FindAsync(Builders<BsonDocument>.Filter.Empty, findOptions);
7372
while (await chunksCursor.MoveNextAsync())
7473
{
74+
orphanedChunks += (ulong)chunksCursor.Current.Count();
7575
var uniqueFileIds = chunksCursor.Current.Select(x => x["files_id"].AsObjectId).ToHashSet();
76+
7677
foreach (var fileId in uniqueFileIds)
7778
{
7879
// Don't check it if we already know it's valid
7980
// Don't check if we already deleted
80-
if (validFiles.Contains(fileId) || deletedFiles.Contains(fileId))
81+
if (validFiles.Contains(fileId) || filesToDelete.Contains(fileId))
8182
{
8283
continue;
8384
}
@@ -90,33 +91,42 @@ private static async Task<int> Main()
9091
continue;
9192
}
9293

93-
var deleteResult = 0L;
94-
// If we know it isn't valid, delete all possible chunks with a single command
95-
if (isDryRun)
96-
{
97-
deleteResult = await chunks.CountDocumentsAsync(Builders<BsonDocument>.Filter.Eq("files_id", fileId));
98-
Log.Information("Dry-run: Could delete {0} chunks from orphaned file {@1}", deleteResult, new { Id = fileId.ToString(), fileId.CreationTime });
99-
}
100-
else
101-
{
102-
var deleteResults = await chunks.DeleteManyAsync(Builders<BsonDocument>.Filter.Eq("files_id", fileId));
103-
deleteResult = deleteResults.DeletedCount;
104-
Log.Information("Deleted {0} chunks from orphaned file {@1}", deleteResult, new { Id = fileId.ToString(), fileId.CreationTime });
105-
}
94+
filesToDelete.Add(fileId);
95+
}
96+
}
97+
98+
Log.Information("Found '{0}' files and {1} orphaned chunks.", filesToDelete.Count, orphanedChunks);
99+
Log.Information("Elapsed time: {0}", sw.Elapsed.ToString());
100+
Log.Information("Starting deletion process..");
106101

107-
deletedFiles.Add(fileId);
102+
// Do this outside the cursor loop to avoid cursor timeout
103+
foreach (var files_id in filesToDelete)
104+
{
105+
if (isDryRun)
106+
{
107+
var deleteResult = await chunks.CountDocumentsAsync(Builders<BsonDocument>.Filter.Eq("files_id", files_id));
108+
Log.Information("Dry-run: Could delete {0} chunks from orphaned file {@1}", deleteResult, new { Id = files_id.ToString(), files_id.CreationTime });
109+
filesDeleted++;
110+
}
111+
else
112+
{
113+
// If we know it isn't valid, delete all possible chunks with a single command
114+
var deleteResult = await chunks.DeleteManyAsync(Builders<BsonDocument>.Filter.Eq("files_id", files_id));
115+
Log.Information("Deleted {0} chunks from orphaned file {@1}", deleteResult.DeletedCount, new { Id = files_id.ToString(), files_id.CreationTime });
116+
filesDeleted++;
108117
}
109118
}
110119
}
111120
catch (Exception ex)
112121
{
113-
Log.Error(ex, "Something went wrong.");
122+
Log.Error(ex, "Deletion process stopped. Something went wrong.");
114123
}
115124
finally
116125
{
117126
sw.Stop();
118-
Log.Information("Elapsed time: {0} minutes.", sw.Elapsed.TotalMinutes);
119-
Log.Information("Deleted '{0}' files: {1}", deletedFiles.Count, string.Join(",", deletedFiles.Select(x => x.ToString())));
127+
Log.Information("Deletion process finished.");
128+
Log.Information("Elapsed time: {0}.", sw.Elapsed.ToString());
129+
Log.Information("Deleted '{0}' files", filesDeleted);
120130
Log.CloseAndFlush();
121131
}
122132

0 commit comments

Comments
 (0)