Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mob][photos] Revert callback sync and album refresh coalescing

ashil-pilotcbfad500383b
diff --git a/mobile/apps/photos/changes/reduce-backup-album-refreshes.md b/mobile/apps/photos/changes/reduce-backup-album-refreshes.md
deleted file mode 100644
index 377cae4d50..0000000000
--- a/mobile/apps/photos/changes/reduce-backup-album-refreshes.md
+++ /dev/null
@@ -1 +0,0 @@
-- Improved backup performance for large photo libraries.
diff --git a/mobile/apps/photos/lib/services/sync/local_sync_scheduler.dart b/mobile/apps/photos/lib/services/sync/local_sync_scheduler.dart
deleted file mode 100644
index c42de92aeb..0000000000
--- a/mobile/apps/photos/lib/services/sync/local_sync_scheduler.dart
+++ /dev/null
@@ -1,46 +0,0 @@
-import "dart:async";
-
-class LocalSyncOperation {
- LocalSyncOperation(this._sync);
-
- final Future<void> Function() _sync;
- Completer<void>? _running;
-
- Future<void>? get current => _running?.future;
-
- Future<void> run() {
- final running = _running;
- if (running != null) {
- return running.future;
- }
-
- final completion = Completer<void>();
- _running = completion;
- completion.complete(
- Future<void>.sync(_sync).whenComplete(() {
- _running = null;
- }),
- );
- return completion.future;
- }
-}
-
-class LocalChangeSyncScheduler {
- LocalChangeSyncScheduler(this._sync);
-
- final Future<void> Function() _sync;
- late final _operation = LocalSyncOperation(_drain);
- bool _requested = false;
-
- Future<void> request() {
- _requested = true;
- return _operation.run();
- }
-
- Future<void> _drain() async {
- do {
- _requested = false;
- await _sync();
- } while (_requested);
- }
-}
diff --git a/mobile/apps/photos/lib/services/sync/local_sync_service.dart b/mobile/apps/photos/lib/services/sync/local_sync_service.dart
index c520f2d007..a63ee6b910 100644
--- a/mobile/apps/photos/lib/services/sync/local_sync_service.dart
+++ b/mobile/apps/photos/lib/services/sync/local_sync_service.dart
@@ -25,7 +25,6 @@ import "package:photos/services/ignored_files_service.dart";
import "package:photos/services/sync/import/diff.dart";
import "package:photos/services/sync/import/local_assets.dart";
import "package:photos/services/sync/import/model.dart";
-import "package:photos/services/sync/local_sync_scheduler.dart";
import "package:photos/services/sync/origin_fetch_tracker.dart";
import "package:shared_preferences/shared_preferences.dart";
import "package:synchronized/synchronized.dart";
@@ -35,8 +34,7 @@ class LocalSyncService {
final _logger = Logger("LocalSyncService");
final _db = FilesDB.instance;
late SharedPreferences _prefs;
- late final _syncOperation = LocalSyncOperation(_sync);
- late final _changeSyncScheduler = LocalChangeSyncScheduler(_syncLocalChanges);
+ Completer<void>? _existingSync;
late Debouncer _changeCallbackDebouncer;
StreamSubscription<PermissionGrantedEvent>? _permissionGrantedSubscription;
bool _isChangeCallbackRegistered = false;
@@ -71,23 +69,13 @@ class LocalSyncService {
if (isLocalGalleryMode) {
// Local gallery onboarding grants permission without explicitly
// invoking SyncService, so trigger local import right away.
- checkAndSync().ignore();
+ unawaited(checkAndSync());
}
});
}
}
- Future<void> sync() {
- final existingSync = _syncOperation.current;
- if (existingSync != null) {
- _logger.warning("Sync already in progress, skipping.");
- return existingSync;
- }
-
- return _syncOperation.run();
- }
-
- Future<void> _sync() async {
+ Future<void> sync() async {
if (!permissionService.hasGrantedPermissions()) {
_logger.info("Skipping local sync since permission has not been granted");
return;
@@ -104,6 +92,11 @@ class LocalSyncService {
}
}
_registerChangeCallback();
+ if (_existingSync != null) {
+ _logger.warning("Sync already in progress, skipping.");
+ return _existingSync!.future;
+ }
+ _existingSync = Completer<void>();
final int ownerID = Configuration.instance.getUserIDV2();
// Local sync must not race downloads; that can create incorrect FilesDB
@@ -167,6 +160,9 @@ class LocalSyncService {
final duration = Duration(microseconds: endTime - startTime);
_logger.info("Load took " + duration.inMilliseconds.toString() + "ms");
});
+
+ _existingSync?.complete();
+ _existingSync = null;
}
Future<bool> _refreshDeviceFolderCountAndCover({
@@ -495,29 +491,20 @@ class LocalSyncService {
PhotoManager.addChangeCallback((value) async {
_logger.info("Something changed on disk");
_changeCallbackDebouncer.run(() async {
- checkAndSync().ignore();
+ unawaited(checkAndSync());
});
});
PhotoManager.startChangeNotify();
}
- Future<void> checkAndSync() => _changeSyncScheduler.request();
-
- Future<void> _syncLocalChanges() async {
- try {
- final existingSync = _syncOperation.current;
- if (existingSync != null) {
- await existingSync;
- }
- if (permissionService.hasGrantedLimitedPermissions()) {
- await syncAll();
- } else {
- await sync();
- await _refreshDeviceFolderCountAndCover();
- }
- } catch (error, stackTrace) {
- _logger.severe("Failed to sync local photo changes", error, stackTrace);
- rethrow;
+ Future<void> checkAndSync() async {
+ if (_existingSync != null) {
+ await _existingSync!.future;
+ }
+ if (permissionService.hasGrantedLimitedPermissions()) {
+ unawaited(syncAll());
+ } else {
+ unawaited(sync().then((value) => _refreshDeviceFolderCountAndCover()));
}
}
}
diff --git a/mobile/apps/photos/test/services/sync/local_sync_scheduler_test.dart b/mobile/apps/photos/test/services/sync/local_sync_scheduler_test.dart
deleted file mode 100644
index 485bb43651..0000000000
--- a/mobile/apps/photos/test/services/sync/local_sync_scheduler_test.dart
+++ /dev/null
@@ -1,144 +0,0 @@
-import "dart:async";
-
-import "package:flutter_test/flutter_test.dart";
-import "package:photos/services/sync/local_sync_scheduler.dart";
-
-void main() {
- test(
- "a failed local scan releases joined callers and permits retry",
- () async {
- final scan = Completer<void>();
- final failure = StateError("local scan failed");
- var attempts = 0;
- final operation = LocalSyncOperation(() async {
- attempts++;
- if (attempts == 1) await scan.future;
- });
-
- final first = operation.run();
- final joined = operation.run();
- expect(attempts, 1);
- final expectations = [
- expectLater(first, throwsA(same(failure))),
- expectLater(joined, throwsA(same(failure))),
- ];
- scan.completeError(failure);
- await Future.wait(expectations);
-
- await operation.run();
- expect(attempts, 2);
- expect(operation.current, isNull);
- },
- );
-
- test("coalesces callbacks through import and album refresh", () async {
- final imports = List.generate(2, (_) => Completer<void>());
- final refreshes = List.generate(2, (_) => Completer<void>());
- final importStarted = List.generate(2, (_) => Completer<void>());
- final refreshStarted = List.generate(2, (_) => Completer<void>());
- final library = <String>{"existing"};
- final imported = <Set<String>>[];
- var passes = 0;
- var active = 0;
- var maxActive = 0;
- final scheduler = LocalChangeSyncScheduler(() async {
- final pass = passes++;
- active++;
- if (active > maxActive) maxActive = active;
- imported.add(Set.of(library));
- importStarted[pass].complete();
- await imports[pass].future;
- refreshStarted[pass].complete();
- await refreshes[pass].future;
- active--;
- });
-
- final requests = List.generate(200, (_) => scheduler.request());
- await importStarted[0].future;
- expect(passes, 1);
- imports[0].complete();
- await refreshStarted[0].future;
-
- library.add("added during refresh");
- requests.addAll(List.generate(200, (_) => scheduler.request()));
- var completed = false;
- final completion = Future.wait(requests).then((_) => completed = true);
- await Future<void>.delayed(Duration.zero);
- expect(passes, 1);
- expect(completed, isFalse);
-
- refreshes[0].complete();
- await importStarted[1].future;
- expect(imported.last, contains("added during refresh"));
- expect(completed, isFalse);
- imports[1].complete();
- await refreshStarted[1].future;
- expect(completed, isFalse);
- refreshes[1].complete();
- await completion;
-
- expect(passes, 2);
- expect(maxActive, 1);
- expect(completed, isTrue);
- });
-
- test("picks up another change during a follow-up pass", () async {
- final gates = List.generate(3, (_) => Completer<void>());
- final started = List.generate(3, (_) => Completer<void>());
- var libraryRevision = 0;
- final importedRevisions = <int>[];
- final scheduler = LocalChangeSyncScheduler(() async {
- final pass = importedRevisions.length;
- importedRevisions.add(libraryRevision);
- started[pass].complete();
- await gates[pass].future;
- });
-
- final requests = [scheduler.request()];
- await started[0].future;
- libraryRevision++;
- requests.add(scheduler.request());
- gates[0].complete();
- await started[1].future;
-
- libraryRevision++;
- requests.add(scheduler.request());
- gates[1].complete();
- await started[2].future;
- gates[2].complete();
- await Future.wait(requests);
-
- expect(importedRevisions, [0, 1, 2]);
- });
-
- test(
- "reports a failed refresh to waiters and allows a later retry",
- () async {
- final refreshStarted = Completer<void>();
- final refresh = Completer<void>();
- final failure = StateError("album refresh failed");
- var attempts = 0;
- final scheduler = LocalChangeSyncScheduler(() async {
- attempts++;
- if (attempts == 1) {
- refreshStarted.complete();
- await refresh.future;
- throw failure;
- }
- });
-
- final first = scheduler.request();
- await refreshStarted.future;
- final waiting = scheduler.request();
- final expectations = [
- expectLater(first, throwsA(same(failure))),
- expectLater(waiting, throwsA(same(failure))),
- ];
- refresh.complete();
- await Future.wait(expectations);
-
- await scheduler.request();
- expect(attempts, 2);
- },
- );
-}