Skip to content

Commit b8b1d62

Browse files
committed
re-check if index exists after syncing from local
1 parent c3babdc commit b8b1d62

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

src/Examine.Lucene/Directories/SyncedFileSystemDirectoryFactory.cs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,28 @@ internal CreateResult TryCreateDirectory(LuceneIndex luceneIndex, bool forceUnlo
102102

103103
if (mainIndexExists)
104104
{
105-
mainResult = CheckIndexHealthAndFix(mainLuceneDir, luceneIndex.Name, _tryFixMainIndexIfCorrupt);
105+
mainResult = CheckIndexHealthAndFix(mainLuceneDir, mainLuceneIndexFolder, luceneIndex.Name, _tryFixMainIndexIfCorrupt);
106106
}
107107

108108
// the main index is/was unhealthy or missing, lets check the local index if it exists
109109
if (localIndexExists && (!mainIndexExists || mainResult.HasFlag(CreateResult.NotClean) || mainResult.HasFlag(CreateResult.MissingSegments)))
110110
{
111111
// TODO: add details here and more below too
112-
_logger.LogInformation("");
113112

114-
var localResult = CheckIndexHealthAndFix(localLuceneDir, luceneIndex.Name, false);
113+
var localResult = CheckIndexHealthAndFix(localLuceneDir, localLuceneIndexFolder, luceneIndex.Name, false);
115114

116115
if (localResult == CreateResult.Init)
117116
{
118117
// it was read successfully, we can sync back to main
119-
localResult |= TryGetIndexWriter(OpenMode.APPEND, localLuceneDir, false, luceneIndex.Name, out var indexWriter);
118+
localResult |= TryGetIndexWriter(OpenMode.APPEND, localLuceneDir, localLuceneIndexFolder, false, luceneIndex.Name, out var indexWriter);
120119
using (indexWriter)
121120
{
122121
if (localResult.HasFlag(CreateResult.OpenedSuccessfully))
123122
{
124123
SyncIndex(indexWriter, true, luceneIndex.Name, mainLuceneIndexFolder, tempDir);
125124
mainResult |= CreateResult.SyncedFromLocal;
125+
// we need to check the main index again, as it may have been fixed by the sync
126+
mainIndexExists = DirectoryReader.IndexExists(mainLuceneDir);
126127
}
127128
}
128129
}
@@ -137,7 +138,7 @@ internal CreateResult TryCreateDirectory(LuceneIndex luceneIndex, bool forceUnlo
137138
? OpenMode.APPEND
138139
: OpenMode.CREATE;
139140

140-
mainResult |= TryGetIndexWriter(openMode, mainLuceneDir, true, luceneIndex.Name, out var indexWriter);
141+
mainResult |= TryGetIndexWriter(openMode, mainLuceneDir, mainLuceneIndexFolder, true, luceneIndex.Name, out var indexWriter);
141142
using (indexWriter)
142143
{
143144
if (!mainResult.HasFlag(CreateResult.SyncedFromLocal))
@@ -192,6 +193,7 @@ protected override Directory CreateDirectory(LuceneIndex luceneIndex, bool force
192193
private CreateResult TryGetIndexWriter(
193194
OpenMode openMode,
194195
Directory luceneDirectory,
196+
DirectoryInfo directoryInfo,
195197
bool createNewIfCorrupt,
196198
string indexName,
197199
out IndexWriter indexWriter)
@@ -219,9 +221,10 @@ private CreateResult TryGetIndexWriter(
219221
if (createNewIfCorrupt)
220222
{
221223
// Index is corrupted, typically this will be FileNotFoundException or CorruptIndexException
222-
_logger.LogError(ex, "{IndexName} index is corrupt, a new one will be created", indexName);
224+
_logger.LogError(ex, "{IndexName} at {IndexPath} index is corrupt, a new one will be created", indexName, directoryInfo.FullName);
223225

224-
// TODO: Here I think we need to totally clear all files in the directory
226+
// Totally clear all files in the directory
227+
ClearDirectory(directoryInfo);
225228

226229
indexWriter = GetIndexWriter(luceneDirectory, OpenMode.CREATE);
227230
}
@@ -234,16 +237,21 @@ private CreateResult TryGetIndexWriter(
234237
}
235238
}
236239

237-
private void SyncIndex(IndexWriter sourceIndexWriter, bool forceUnlock, string indexName, DirectoryInfo destinationDirectory, DirectoryInfo tempDir)
240+
private void ClearDirectory(DirectoryInfo directoryInfo)
238241
{
239-
// First, we need to clear the main index. If for some reason it is at the same revision, the syncing won't do anything.
240-
if (destinationDirectory.Exists)
242+
if (directoryInfo.Exists)
241243
{
242-
foreach (var file in destinationDirectory.EnumerateFiles())
244+
foreach (var file in directoryInfo.EnumerateFiles())
243245
{
244246
file.Delete();
245247
}
246248
}
249+
}
250+
251+
private void SyncIndex(IndexWriter sourceIndexWriter, bool forceUnlock, string indexName, DirectoryInfo destinationDirectory, DirectoryInfo tempDir)
252+
{
253+
// First, we need to clear the main index. If for some reason it is at the same revision, the syncing won't do anything.
254+
ClearDirectory(destinationDirectory);
247255

248256
using (var sourceIndex = new LuceneIndex(_loggerFactory, indexName, new TempOptions(), sourceIndexWriter))
249257
using (var destinationLuceneDirectory = FSDirectory.Open(destinationDirectory, LockFactory.GetLockFactory(destinationDirectory)))
@@ -261,6 +269,7 @@ private void SyncIndex(IndexWriter sourceIndexWriter, bool forceUnlock, string i
261269

262270
private CreateResult CheckIndexHealthAndFix(
263271
Directory luceneDir,
272+
DirectoryInfo directoryInfo,
264273
string indexName,
265274
bool doFix)
266275
{
@@ -280,17 +289,17 @@ private CreateResult CheckIndexHealthAndFix(
280289

281290
if (status.MissingSegments)
282291
{
283-
_logger.LogWarning("{IndexName} index is missing segments, it will be deleted.", indexName);
292+
_logger.LogWarning("{IndexName} index at {IndexPath} is missing segments, it will be deleted.", indexName, directoryInfo.FullName);
284293
result = CreateResult.MissingSegments;
285294
}
286295
else if (!status.Clean)
287296
{
288-
_logger.LogWarning("Checked main index {IndexName} and it is not clean.", indexName);
297+
_logger.LogWarning("Checked index {IndexName} at {IndexPath} and it is not clean.", indexName, directoryInfo.FullName);
289298
result = CreateResult.NotClean;
290299

291300
if (doFix)
292301
{
293-
_logger.LogWarning("Attempting to fix {IndexName}. {DocumentsLost} documents will be lost.", indexName, status.TotLoseDocCount);
302+
_logger.LogWarning("Attempting to fix {IndexName} at {IndexPath}. {DocumentsLost} documents will be lost.", indexName, status.TotLoseDocCount, directoryInfo.FullName);
294303

295304
try
296305
{
@@ -299,25 +308,25 @@ private CreateResult CheckIndexHealthAndFix(
299308

300309
if (!status.Clean)
301310
{
302-
_logger.LogError("{IndexName} index could not be fixed, it will be deleted.", indexName);
311+
_logger.LogError("{IndexName} index at {IndexPath} could not be fixed, it will be deleted.", indexName, directoryInfo.FullName);
303312
result |= CreateResult.NotFixed;
304313
}
305314
else
306315
{
307-
_logger.LogInformation("Index {IndexName} fixed. {DocumentsLost} documents were lost.", indexName, status.TotLoseDocCount);
316+
_logger.LogInformation("Index {IndexName} at {IndexPath} fixed. {DocumentsLost} documents were lost.", indexName, status.TotLoseDocCount, directoryInfo.FullName);
308317
result |= CreateResult.Fixed;
309318
}
310319
}
311320
catch (Exception ex)
312321
{
313-
_logger.LogError(ex, "{IndexName} index could not be fixed, it will be deleted.", indexName);
322+
_logger.LogError(ex, "{IndexName} index at {IndexPath} could not be fixed, it will be deleted.", indexName, directoryInfo.FullName);
314323
result |= CreateResult.ExceptionNotFixed;
315324
}
316325
}
317326
}
318327
else
319328
{
320-
_logger.LogInformation("Checked main index {IndexName} and it is clean.", indexName);
329+
_logger.LogInformation("Checked index {IndexName} at {IndexPath} and it is clean.", indexName, directoryInfo.FullName);
321330
}
322331

323332
return result;
@@ -332,7 +341,8 @@ private static IndexWriter GetIndexWriter(Directory mainDir, OpenMode openMode)
332341
new StandardAnalyzer(LuceneInfo.CurrentVersion))
333342
{
334343
OpenMode = openMode,
335-
IndexDeletionPolicy = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy())
344+
IndexDeletionPolicy = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy()),
345+
MergePolicy = new TieredMergePolicy()
336346
});
337347

338348
return indexWriter;

0 commit comments

Comments
 (0)