Skip to content

Commit ab2a8f0

Browse files
committed
תיקון issue #2467 — מחיקת הדור הקודם של וקטורי החיפוש החכם אחרי פתיחה תקינה
המנוע (otzaria_search_engine 7.5.3) שומר את הדור הקודם של הסט לגיבוי עד ההתקנה הבאה, ומספק retirePreviousSemanticGeneration כדי לוותר עליו אחרי שהדור החי נפתח. התוכנה מעולם לא קראה לו, ולכן אחרי עדכון נשאר על הדיסק סט שלם נוסף. עכשיו אחרי פתיחה שלא נפלה לגיבוי (recoveredFromPrevious) המאגר מוחק את הדור הקודם ברקע, פעם אחת לכל דור, במקום האיחוד, כך שהתקנה, מחיקה והעברה ממתינות לו. סט תפוס נדחה לפתיחה הבאה; כשל אחר נרשם בלבד ולא מוצג.
1 parent 1110646 commit ab2a8f0

5 files changed

Lines changed: 92 additions & 0 deletions

File tree

‎lib/semantic_search/engine/onnx_semantic_engine_adapter.dart‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ class OnnxSemanticEngineAdapter implements SemanticEngineBackend {
146146
);
147147
});
148148

149+
@override
150+
Future<bool> retirePreviousGeneration(String vectorsDir) => _guard(
151+
() async => (await (await _engine()).retirePreviousSemanticGeneration(
152+
vectorsDir: vectorsDir,
153+
)).retired,
154+
);
155+
149156
@override
150157
Future<void> disable() async => (await _engine()).disableSemantic();
151158

‎lib/semantic_search/engine/semantic_engine_backend.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ abstract interface class SemanticEngineBackend {
6565
required SemanticCancelHandle cancel,
6666
});
6767

68+
/// מוחק את הדור הקודם ששמור לגיבוי ליד הדור החי; `false` כשלא היה כזה.
69+
/// לקרוא רק אחרי שהדור החי נפתח בלי ליפול לגיבוי.
70+
Future<bool> retirePreviousGeneration(String vectorsDir);
71+
6872
/// סוגר את ה-session הפתוח.
6973
Future<void> disable();
7074

@@ -151,6 +155,10 @@ class UnavailableSemanticEngineBackend implements SemanticEngineBackend {
151155
required SemanticCancelHandle cancel,
152156
}) => Future.error(_unavailable);
153157

158+
@override
159+
Future<bool> retirePreviousGeneration(String vectorsDir) =>
160+
Future.error(_unavailable);
161+
154162
@override
155163
Future<void> disable() async {}
156164

‎lib/semantic_search/repository/semantic_search_repository.dart‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ class SemanticSearchRepository {
222222
Future<void>? _compaction;
223223
SemanticCancelHandle _compactCancel = SemanticCancelHandle();
224224

225+
/// הדור שהגיבוי שלו כבר נמחק בהרצה הזו.
226+
int? _retiredGeneration;
227+
225228
/// ה-base שהשרשרת אליו נכשלה; אליו מורידים base בלבד.
226229
String? _deltaChainFailedFor;
227230
SemanticAvailabilityPhase? _jobPhase;
@@ -927,6 +930,7 @@ class SemanticSearchRepository {
927930
}
928931
_openQuantization = quantization;
929932
unawaited(_warmUp());
933+
_retirePreviousIfNeeded();
930934
}
931935

932936
Future<void> _warmUp() async {
@@ -1643,6 +1647,40 @@ class SemanticSearchRepository {
16431647
}
16441648
}
16451649

1650+
/// אחרי פתיחה מהדור החי מוחק ברקע את הדור הקודם, שהמנוע שומר לגיבוי עד
1651+
/// ההתקנה הבאה. רץ במקום האיחוד, כדי שהתקנה, מחיקה והעברה ימתינו לו.
1652+
void _retirePreviousIfNeeded() {
1653+
final vectors = _vectors;
1654+
if (vectors == null ||
1655+
!vectors.present ||
1656+
vectors.recoveredFromPrevious ||
1657+
vectors.generation == _retiredGeneration ||
1658+
_compaction != null ||
1659+
_moveInProgress) {
1660+
return;
1661+
}
1662+
_retiredGeneration = vectors.generation;
1663+
_compaction = _retirePrevious().whenComplete(() => _compaction = null);
1664+
}
1665+
1666+
Future<void> _retirePrevious() async {
1667+
try {
1668+
final dir = (await _paths()).vectorsDirectory;
1669+
if (await _backend.retirePreviousGeneration(dir)) {
1670+
_vectors = await _backend.vectorsInfo(dir);
1671+
}
1672+
} on SemanticFailure catch (failure) {
1673+
// התקנה או איחוד תופסים את הסט: הפתיחה הבאה תנסה שוב.
1674+
if (failure.kind == SemanticFailureKind.vectorsBusy) {
1675+
_retiredGeneration = null;
1676+
} else {
1677+
_log('retirePrevious', failure, StackTrace.current);
1678+
}
1679+
} catch (error, stackTrace) {
1680+
_log('retirePrevious', error, stackTrace);
1681+
}
1682+
}
1683+
16461684
/// מבטל איחוד שרץ וממתין לו: התקנה, מחיקה והעברה צריכות את הסט פנוי.
16471685
Future<void> _stopCompaction() async {
16481686
_compactCancel.cancel();

‎test/semantic_search/engine/onnx_semantic_engine_adapter_test.dart‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ class _FakeEngine implements SearchEngine {
7777
recoveredFromPrevious: vectorsRecovered,
7878
);
7979

80+
String? retiredDir;
81+
82+
@override
83+
Future<SemanticGenerationRetireReport> retirePreviousSemanticGeneration({
84+
required String vectorsDir,
85+
}) async {
86+
final error = failure;
87+
if (error != null) throw error;
88+
retiredDir = vectorsDir;
89+
return SemanticGenerationRetireReport(
90+
generation: BigInt.two,
91+
retired: true,
92+
retiredGeneration: BigInt.one,
93+
bytesFreed: BigInt.from(10),
94+
);
95+
}
96+
8097
@override
8198
Future<SemanticStatus> semanticStatus() async => status;
8299

@@ -380,6 +397,27 @@ void main() {
380397
);
381398
});
382399

400+
test('retirePreviousGeneration מעביר את התיקייה וממפה סט תפוס', () async {
401+
expect(await adapter.retirePreviousGeneration('/v'), isTrue);
402+
expect(engine.retiredDir, '/v');
403+
404+
engine.failure = const SemanticError(
405+
kind: SemanticErrorKind.vectorsBusy,
406+
message: 'compaction is running',
407+
field: 'vectors_dir',
408+
);
409+
await expectLater(
410+
adapter.retirePreviousGeneration('/v'),
411+
throwsA(
412+
isA<SemanticFailure>().having(
413+
(f) => f.kind,
414+
'kind',
415+
SemanticFailureKind.vectorsBusy,
416+
),
417+
),
418+
);
419+
});
420+
383421
test('search: ביטול מגיע ל-token, והדירוג ו-fallbackKind ממופים', () async {
384422
final cancel = SemanticCancelHandle();
385423
final outcome = await adapter.search(

‎test/semantic_search/semantic_test_support.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ class FakeBackend implements SemanticEngineBackend {
291291
final List<SemanticFailure> retireFailures = [];
292292
final List<String> retireDirs = [];
293293

294+
@override
294295
Future<bool> retirePreviousGeneration(String vectorsDir) async {
295296
calls.add('retire');
296297
retireDirs.add(vectorsDir);

0 commit comments

Comments
 (0)