Skip to content

Commit b27a3f8

Browse files
Merge pull request #71 from sandeepsnairms/main
Add Simulated Run
2 parents ba2d6bb + 0dbbf24 commit b27a3f8

File tree

11 files changed

+61
-24
lines changed

11 files changed

+61
-24
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/MongoMigrationWebApp.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
<DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
99
</PropertyGroup>
1010

11+
<ItemGroup>
12+
<None Remove="readme.txt" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<Content Include="readme.txt">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</Content>
19+
</ItemGroup>
20+
1121
<ItemGroup>
1222
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
1323
</ItemGroup>

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.9</div>
9+
<div class="ms-2 small">v0.7.11</div>
1010
</div>
1111

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

MongoMigrationWebApp/readme.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Version: 0.7.10
2+
Instructions: https://github.com/AzureCosmosDB/MongoMigrationWebBasedUtility
3+
Purpose: An online and offline migration utility designed to migrate any MongoDB source to Azure Cosmos DB for Mongo vCore. It operates as an Azure Web App (or on-premises) and offers multiple methods for data migration. It can either use mongodump and mongorestore for data movement or employ the MongoDB driver to read data from the source and write it

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: 9 additions & 6 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;
@@ -348,7 +348,7 @@ private void Upload(MigrationUnit item, string targetConnectionString)
348348
}
349349

350350
// checking if source and target doc counts are same
351-
if (item.MigrationChunks[i].DocCountInTarget == item.MigrationChunks[i].DumpResultDocCount)
351+
if (item.MigrationChunks[i].DocCountInTarget == item.MigrationChunks[i].DumpQueryDocCount)
352352
{
353353
Log.WriteLine($"Restore for {dbName}.{colName}-{i} No documents missing, count in Target: {item.MigrationChunks[i].DocCountInTarget}");
354354
Log.Save();
@@ -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)

0 commit comments

Comments
 (0)