Skip to content

armgit / ente

Public Imported from GitHub
0

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

Merge upstream/main into ml_fg_bg_lock

laurenspriemb92f11b86832
diff --cc mobile/apps/photos/lib/module/upload/service/file_uploader.dart
index a6152190f2,8c1599c2e2..f1673e2520
--- a/mobile/apps/photos/lib/module/upload/service/file_uploader.dart
+++ b/mobile/apps/photos/lib/module/upload/service/file_uploader.dart
@@@ -72,12 -69,11 +71,10 @@@ class FileUploader
);
final _existingUploadResolver = ExistingUploadResolver.forApp();
final kSafeBufferForLockExpiry = const Duration(hours: 4).inMicroseconds;
- final kBGTaskDeathTimeout = const Duration(seconds: 5).inMicroseconds;
Map<String, BackupItem> get allBackups => _queue.backupItems;
- /// Returns true if any file uploads are currently in progress
bool get isUploading => _queue.isUploading;
- /// Returns true if an upload is queued, running, or waiting in background.
bool get hasPendingUploads => _queue.hasPendingUploads;
late ProcessType _processType;
late SharedPreferences _prefs;
diff --cc mobile/apps/photos/lib/services/machine_learning/ml_service.dart
index f5ffe58f4b,ad81ff3f18..3fe3c34793
--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
@@@ -44,9 -40,9 +44,8 @@@ import "package:photos/utils/ram_check_
class MLService {
final _logger = Logger("MLService");
- // Singleton pattern
MLService._privateConstructor();
static final instance = MLService._privateConstructor();
- factory MLService() => instance;
bool _isInitialized = false;
@@@ -93,7 -86,11 +92,6 @@@
: MLDataDB.instance;
}
- /// Only call this function once at app startup, after that you can directly call [runAllML]
- bool _hasModeChanged(MLMode mode) {
- return (isLocalGalleryMode ? MLMode.localGallery : MLMode.enteGallery) !=
- mode;
- }
-
Future<void> init() async {
if (_isInitialized) {
_schedulePredownloadLocalModels();
@@@ -546,48 -447,10 +541,44 @@@
MLIndexingIsolate.instance.shouldPauseIndexingAndClustering = false;
}
- Future<void> fetchAndIndexAllImages({required MLMode mode}) async {
- if (!_canRunMLFunction(function: "Indexing")) return;
- if (mode == MLMode.enteGallery && !isLocalGalleryMode) {
- await _waitForOwnedRemoteHydrationIfRunning();
+ void triggerML() {
+ if (_mlControllerStatus && !MlProcessLock.instance.isBusy) {
+ unawaited(runAllML());
+ }
+ }
+
+ /// Requests a durable stop of all acquiring or active ML runs. Active runs
+ /// drain already-started work and do not begin further stages; a new run may
+ /// start later through the normal triggers.
+ void stopActiveRun(MlStopReason reason) {
+ for (final control in _runControls.toList(growable: false)) {
+ control.requestStop(reason);
+ }
+ }
+
- /// Analyzes all the images in the user library with the latest ml version and stores the results in the database.
- ///
- /// This function first fetches from remote and checks if the image has already been analyzed
- /// with the lastest faceMlVersion and stored on remote or local database. If so, it skips the image.
+ Future<MlLockAttempt> fetchAndIndexAllImages({required MLMode mode}) async {
+ final control = MlRunControl();
+ final attempt = await _runExclusiveWithControl(
+ MlOperation.indexing,
+ control,
+ () => _fetchAndIndexAllImages(mode: mode, control: control),
+ );
+ if (attempt != MlLockAttempt.ran) {
+ _logger.info(
+ "fetchAndIndexAllImages denied the ml process lock (${attempt.name})",
+ );
+ }
+ return attempt;
+ }
+
+ Future<void> _fetchAndIndexAllImages({
+ required MLMode mode,
+ required MlRunControl control,
+ }) async {
+ assert(MlProcessLock.instance.isBusy, "ml funnel must be held");
+ if (control.stopRequested) {
+ _logRunStopped(control, "before indexing started");
+ return;
}
if (!_canRunMLFunction(function: "Indexing")) return;
@@@ -728,14 -575,12 +719,12 @@@
}
try {
- // Get a sense of the total number of faces in the database
- final int totalFaces = await _mlDataDB.getTotalFaceCount();
+ final int totalFaces = await mlDataDB.getTotalFaceCount();
final fileIDToCreationTime = isLocalGalleryMode
? await _getLocalGalleryFileIdToCreationTime()
: await FilesDB.instance.getFileIDToCreationTime();
final startEmbeddingFetch = DateTime.now();
- // read all embeddings
- final result = await _mlDataDB.getFaceInfoForClustering(
+ final result = await mlDataDB.getFaceInfoForClustering(
maxFaces: totalFaces,
);
final Set<int> missingFileIDs = {};
@@@ -761,8 -605,7 +749,7 @@@
'and ${missingFileIDs.length} missing fileIDs',
);
- // Get the current cluster statistics
- final Map<String, (Uint8List, int)> oldClusterSummaries = await _mlDataDB
+ final Map<String, (Uint8List, int)> oldClusterSummaries = await mlDataDB
.getAllClusterSummary();
if (clusterInBuckets) {
@@@ -908,9 -753,8 +893,8 @@@
instruction,
filePath,
);
- // Check if there's no result simply because the run was stopped
if (result == null) {
- if (!_shouldPauseIndexingAndClustering &&
+ if (!(_activeRunControl?.stopRequested ?? false) &&
!MLIndexingIsolate.instance.shouldPauseIndexingAndClustering) {
_logger.severe(
"Failed to analyze image with fileID: ${instruction.fileKey}",