Skip to content

Commit 0dbbf24

Browse files
author
Sandeep Nair
committed
Add Simulated Run
1 parent 93065d2 commit 0dbbf24

File tree

9 files changed

+46
-22
lines changed

9 files changed

+46
-22
lines changed

MongoMigrationWebApp/Components/MigrationDetails.razor

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
<textarea id="nameSpaces" disabled="@(!newMode)" rows="4" placeholder="e.g. db1.col1,db1.col2,db2.col1,db2.col5" class="form-control" @bind="namespaces"></textarea>
4444
</div>
4545

46+
<div class="mb-3">
47+
<label for="isSimulatedRun" class="form-label">Simulation Mode: No Target Writes</label>
48+
<input type="checkbox" id="isSimulatedRun" disabled="@(!newMode)" @bind="isSimulatedRun" />
49+
</div>
50+
4651
@if (!string.IsNullOrEmpty(errorMessage))
4752
{
4853
<div class="alert alert-danger mt-2">@errorMessage</div>
@@ -93,7 +98,8 @@
9398
private string errorMessage = string.Empty;
9499
private bool useMongoDump = true; // Variable to track the selected value
95100
private string selectedOption = "Use MongoDump and MongoRestore"; // Default dropdown value
96-
101+
private bool isSimulatedRun = false; // Variable to track the dummy run option
102+
97103
private void OnSelectionChanged(ChangeEventArgs e)
98104
{
99105
selectedOption = e.Value.ToString();
@@ -168,7 +174,8 @@
168174
SourceEndpoint = tmpSrcEndpoint,
169175
TargetEndpoint = tmpTgtEndpoint,
170176
NameSpaces = namespaces,
171-
UseMongoDump = useMongoDump
177+
UseMongoDump = useMongoDump,
178+
IsSimulatedRun = isSimulatedRun
172179
};
173180
await OnSubmit.InvokeAsync(job);
174181
}

MongoMigrationWebApp/Shared/MainLayout.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<main>
77
<div class="top-row px-4 d-flex justify-content-between align-items-center">
88
<h2 class="text-left">Migrate to Azure Cosmos DB for MongoDB (vCore based)</h2>
9-
<div class="ms-2 small">v0.7.10</div>
9+
<div class="ms-2 small">v0.7.11</div>
1010
</div>
1111

1212
<article class="content px-4">

OnlineMongoMigrationProcessor/Helpers/Helper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ public static bool IsOfflineJobCompleted(MigrationJob migrationJob)
267267
{
268268
if (mu.SourceStatus == CollectionStatus.OK)
269269
{
270+
if(mu.DumpComplete && migrationJob.IsSimulatedRun)
271+
return true;
272+
270273
if (!mu.RestoreComplete || !mu.DumpComplete)
271274
return false;
272275
}

OnlineMongoMigrationProcessor/MigrationWorker.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ public async Task StartMigrationAsync(MigrationJob job, string sourceConnectionS
146146
{
147147
_sourceClient = new MongoClient(sourceConnectionString);
148148
Log.WriteLine("Source Client Created");
149+
if(job.IsSimulatedRun)
150+
{
151+
Log.WriteLine("Simulated Run. No changes will be made to the target.");
152+
}
149153
Log.Save();
150154

151155

@@ -213,7 +217,7 @@ public async Task StartMigrationAsync(MigrationJob job, string sourceConnectionS
213217

214218

215219

216-
if (!_job.UseMongoDump)
220+
if (!_job.UseMongoDump && !job.IsSimulatedRun)
217221
{
218222
var database = _sourceClient.GetDatabase(unit.DatabaseName);
219223
var collection = database.GetCollection<BsonDocument>(unit.CollectionName);

OnlineMongoMigrationProcessor/Models/DataModels.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class MigrationJob
8888
public bool IsStarted { get; set; }
8989
public bool CurrentlyActive { get; set; }
9090
public bool UseMongoDump { get; set; }
91+
public bool IsSimulatedRun { get; set; }
9192
public List<MigrationUnit>? MigrationUnits { get; set; }
9293
}
9394

OnlineMongoMigrationProcessor/Processors/CopyProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void Migrate(MigrationUnit item, string sourceConnectionString, string ta
138138

139139
var documentCopier = new MongoDocumentCopier();
140140
documentCopier.Initialize(_targetClient, collection, dbName, colName, _config.MongoCopyPageSize);
141-
var result = documentCopier.CopyDocumentsAsync(_jobs, item, i, initialPercent, contributionFactor, docCount, filter, _cts.Token).GetAwaiter().GetResult();
141+
var result = documentCopier.CopyDocumentsAsync(_jobs, item, i, initialPercent, contributionFactor, docCount, filter, _cts.Token,_job.IsSimulatedRun).GetAwaiter().GetResult();
142142

143143
if (result)
144144
{

OnlineMongoMigrationProcessor/Processors/DumpRestoreProcessor.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public void Migrate(MigrationUnit item, string sourceConnectionString, string ta
179179
_jobs?.Save(); // Persist state
180180
dumpAttempts = 0;
181181

182-
if (!restoreInvoked)
182+
if (!restoreInvoked )
183183
{
184184
Log.WriteLine($"{dbName}.{colName} Uploader invoked");
185185

@@ -282,7 +282,7 @@ private void Upload(MigrationUnit item, string targetConnectionString)
282282

283283
Log.WriteLine($"{dbName}.{colName} Uploader started");
284284

285-
while (!item.RestoreComplete && Directory.Exists(folder) && !_executionCancelled && _job.CurrentlyActive)
285+
while (!item.RestoreComplete && Directory.Exists(folder) && !_executionCancelled && _job.CurrentlyActive && !_job.IsSimulatedRun)
286286
{
287287
int restoredChunks = 0;
288288
long restoredDocs = 0;
@@ -314,7 +314,7 @@ private void Upload(MigrationUnit item, string targetConnectionString)
314314
backoff = TimeSpan.FromSeconds(2);
315315
bool continueProcessing = true;
316316
bool skipRestore = false;
317-
while (restoreAttempts < maxRetries && !_executionCancelled && continueProcessing && !item.RestoreComplete && _job.CurrentlyActive)
317+
while (restoreAttempts < maxRetries && !_executionCancelled && continueProcessing && !item.RestoreComplete && _job.CurrentlyActive )
318318
{
319319
restoreAttempts++;
320320
skipRestore=false;
@@ -468,11 +468,14 @@ private void Upload(MigrationUnit item, string targetConnectionString)
468468
}
469469
}
470470
}
471-
if (item.RestoreComplete && item.DumpComplete)
471+
if ((item.RestoreComplete && item.DumpComplete)|| (item.DumpComplete && _job.IsSimulatedRun))
472472
{
473473
try
474474
{
475-
Directory.Delete(folder, true);
475+
476+
if (Directory.Exists(folder))
477+
Directory.Delete(folder, true);
478+
476479
// Process change streams
477480
if (_job.IsOnline && !_executionCancelled)
478481
{

OnlineMongoMigrationProcessor/Processors/MongoChangeStreamProcessor.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ private bool ProcessCursor(MigrationJob job, ChangeStreamDocument<BsonDocument>
219219

220220
// Output change details to the console
221221
Log.AddVerboseMessage($"{change.OperationType} operation detected in {targetCollection.CollectionNamespace} for _id: {change.DocumentKey["_id"]} having TS (UTC): {MongoHelper.BsonTimestampToUtcDateTime(timestamp)}");
222-
ProcessChange(change, targetCollection);
222+
ProcessChange(change, targetCollection,job.IsSimulatedRun);
223223
item.CursorUtcTimestamp = MongoHelper.BsonTimestampToUtcDateTime(timestamp);
224224
}
225225
else if (!job.SourceServerVersion.StartsWith("3") && change.WallTime != null) //for vcore
@@ -229,14 +229,14 @@ private bool ProcessCursor(MigrationJob job, ChangeStreamDocument<BsonDocument>
229229

230230
// Output change details to the console
231231
Log.AddVerboseMessage($"{change.OperationType} operation detected in {targetCollection.CollectionNamespace} for _id: {change.DocumentKey["_id"]} having TS (UTC): {timestamp.Value}");
232-
ProcessChange(change, targetCollection);
232+
ProcessChange(change, targetCollection, job.IsSimulatedRun);
233233
item.CursorUtcTimestamp = timestamp.Value;
234234
}
235235
else
236236
{
237237
// Output change details to the console
238238
Log.AddVerboseMessage($"{change.OperationType} operation detected in {targetCollection.CollectionNamespace} for _id: {change.DocumentKey["_id"]}");
239-
ProcessChange(change, targetCollection);
239+
ProcessChange(change, targetCollection, job.IsSimulatedRun);
240240
}
241241
item.ResumeToken = cursor.Current.FirstOrDefault().ResumeToken.ToJson();
242242
_jobs?.Save(); // persists state
@@ -262,8 +262,11 @@ private bool ProcessCursor(MigrationJob job, ChangeStreamDocument<BsonDocument>
262262
}
263263

264264

265-
private void ProcessChange(ChangeStreamDocument<BsonDocument> change, IMongoCollection<BsonDocument> targetCollection)
265+
private void ProcessChange(ChangeStreamDocument<BsonDocument> change, IMongoCollection<BsonDocument> targetCollection, bool isWriteSimulated)
266266
{
267+
if (isWriteSimulated)
268+
return;
269+
267270
try
268271
{
269272
switch (change.OperationType)

OnlineMongoMigrationProcessor/Processors/MongoDocumentCopier.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public async Task<bool> CopyDocumentsAsync(
7979
double contribFactor,
8080
long targetCount,
8181
FilterDefinition<BsonDocument> filter,
82-
CancellationToken cancellationToken)
82+
CancellationToken cancellationToken,
83+
bool isWriteSimulated)
8384
{
8485
ConcurrentBag<Exception> errors = new ConcurrentBag<Exception>();
8586
try
@@ -132,7 +133,7 @@ public async Task<bool> CopyDocumentsAsync(
132133
try
133134
{
134135

135-
await ProcessSegmentAsync(segment, combinedFilter, jobList, item, migrationChunkIndex, basePercent, contribFactor, targetCount, errors, cancellationToken);
136+
await ProcessSegmentAsync(segment, combinedFilter, jobList, item, migrationChunkIndex, basePercent, contribFactor, targetCount, errors, cancellationToken, isWriteSimulated);
136137
}
137138
finally
138139
{
@@ -215,7 +216,8 @@ private async Task ProcessSegmentAsync(
215216
double contribFactor,
216217
long targetCount,
217218
ConcurrentBag<Exception> errors,
218-
CancellationToken cancellationToken)
219+
CancellationToken cancellationToken,
220+
bool IsWriteSimulated)
219221
{
220222

221223
string segmentId = segment.Id;
@@ -295,13 +297,14 @@ private async Task ProcessSegmentAsync(
295297
break;
296298
}
297299

298-
var options = new InsertManyOptions { IsOrdered = false };
299-
// Insert the current batch into the target collection
300-
await _targetCollection.InsertManyAsync(set, options,cancellationToken: cancellationToken);
301-
300+
if (!IsWriteSimulated)
301+
{
302+
var options = new InsertManyOptions { IsOrdered = false };
303+
// Insert the current batch into the target collection
304+
await _targetCollection.InsertManyAsync(set, options, cancellationToken: cancellationToken);
305+
}
302306
Interlocked.Add(ref _successCount, set.Count);
303307

304-
305308
}
306309
catch (OutOfMemoryException ex)
307310
{

0 commit comments

Comments
 (0)