Skip to content

Commit 6b07241

Browse files
committed
Fix: we should always regenerate snapshot
1 parent 9f5c9f5 commit 6b07241

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

backend/FwHeadless/Services/SyncHostedService.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -239,22 +239,26 @@ public async Task<SyncJobResult> ExecuteSync(CancellationToken stoppingToken, bo
239239
}
240240
}
241241

242-
//We defer regenerating the snapshot until we know the changes applied to fwdata were successfully pushed.
243-
//Otherwise, the changes might have been rolled back. In that case, the next sync could overwrite
244-
//CRDT data with old/rolled back FW data.
245-
if (result.CrdtChanges > 0)
246-
{
247-
logger.LogInformation("Regenerating project snapshot to include {CrdtChangeCount} crdt changes", result.CrdtChanges);
248-
//note we are using the crdt API, this avoids issues where some data isn't synced yet
249-
//later when we add the ability to sync that data we need the snapshot to reflect the synced state, not what was in the FW project
250-
//related to https://github.com/sillsdev/languageforge-lexbox/issues/1912
251-
await projectSnapshotService.RegenerateProjectSnapshot(miniLcmApi, fwdataApi.Project, keepBackup: false);
252-
}
253-
else
254-
{
255-
logger.LogInformation("Skipping regenerating project snapshot, because there were no crdt changes");
256-
}
257-
242+
/*
243+
Notes:
244+
1) We defer regenerating the snapshot until we know the changes applied to fwdata were successfully pushed.
245+
Otherwise, the changes might have been rolled back. In that case, the next sync could overwrite CRDT data with old/rolled back FW data.
246+
2) We are intentionaly using the crdt API. This avoids issues when new data/fields don't yet get synced.
247+
When we start syncing that data/those fields we need the snapshot to reflect the CRDT state, rather than the FW project,
248+
otherwise existing FW data will never be synced to CRDT. Related to https://github.com/sillsdev/languageforge-lexbox/issues/1912
249+
3) We should always regenerate the snapshot even if no changes were detected.
250+
If the same change was made in both fwdata and crdt, no changes would be detected,
251+
but we still need that change to get into the snapshot
252+
*/
253+
logger.LogInformation("Regenerating project snapshot");
254+
await projectSnapshotService.RegenerateProjectSnapshot(miniLcmApi, fwdataApi.Project, keepBackup: false);
255+
256+
/*
257+
Push new changes to Harmony (changes that came from FW)
258+
Important: we only sync the Harmony project AFTER regenerating the snapshot.
259+
Otherwise, the sync could pull changes into the snapshot that were not respected during the sync.
260+
Could presumably be skipped if 0 CrdtChanges, but it's cheap.
261+
*/
258262
await crdtSyncService.SyncHarmonyProject();
259263

260264
activity?.SetStatus(ActivityStatusCode.Ok, "Sync finished");

backend/Testing/FwHeadless/Services/SyncWorkerTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public async Task ExecuteSync_CrdtChangesNoFwChanges_RegeneratesSnapshotWithoutP
5151
}
5252

5353
[Fact]
54-
public async Task ExecuteSync_NoCrdtChanges_DoesNotRegenerateSnapshot()
54+
public async Task ExecuteSync_NoChanges_RegeneratesSnapshotAnyway()
5555
{
5656
using var h = new SyncWorkerTestHarness();
5757
var syncResult = new SyncResult(CrdtChanges: 0, FwdataChanges: 0);
@@ -67,6 +67,7 @@ public async Task ExecuteSync_NoCrdtChanges_DoesNotRegenerateSnapshot()
6767
MediaSyncCrdt,
6868
GetSnapshot,
6969
Sync,
70+
RegenerateSnapshot,
7071
HarmonySync);
7172
}
7273

@@ -238,6 +239,7 @@ public async Task ExecuteSync_FwDataFileMissing_ClonesProject()
238239
MediaSyncCrdt,
239240
GetSnapshot,
240241
Sync,
242+
RegenerateSnapshot,
241243
HarmonySync);
242244
}
243245

0 commit comments

Comments
 (0)