Skip to content

Commit

Permalink
fix(Delete index files): Retry policy
Browse files Browse the repository at this point in the history
  • Loading branch information
bkapustik committed Oct 22, 2024
1 parent 9a12b22 commit 15cd9f0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/Kentico.Xperience.Lucene.Admin/UIPages/IndexListingPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,30 +178,30 @@ public async Task<ICommandResponse<RowActionResult>> Rebuild(int id, Cancellatio
}

[PageCommand(Permission = SystemPermissions.DELETE)]
public Task<ICommandResponse> Delete(int id, CancellationToken _)
public async Task<ICommandResponse> Delete(int id, CancellationToken _)
{
var index = indexManager.GetIndex(id);
var result = new RowActionResult(false);

if (index is null)
{
return Task.FromResult<ICommandResponse>(ResponseFrom(result)
return await Task.FromResult<ICommandResponse>(ResponseFrom(result)
.AddErrorMessage(string.Format("Error loading Lucene index with identifier {0}.", id)));
}

bool indexDeleted = luceneClient.DeleteIndex(index!);
bool indexDeleted = await luceneClient.DeleteIndex(index!);

if (!indexDeleted)
{
return Task.FromResult<ICommandResponse>(ResponseFrom(result)
return await Task.FromResult<ICommandResponse>(ResponseFrom(result)
.AddErrorMessage(string.Format("Errors occurred while deleting the '{0}' index. Please check the Event Log for more details.", index.IndexName)));
}

configurationStorageService.TryDeleteIndex(id);

var response = NavigateTo(pageLinkGenerator.GetPath<IndexListingPage>());

return Task.FromResult<ICommandResponse>(response);
return await Task.FromResult<ICommandResponse>(response);
}

private static void AddMissingStatistics(ref ICollection<LuceneIndexStatisticsModel> statistics, ILuceneIndexManager indexManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ public Task<int> UpsertRecords(IEnumerable<Document> documents, string indexName
return UpsertRecordsInternal(documents, indexName);
}

public bool DeleteIndex(LuceneIndex luceneIndex) =>
luceneIndex?.StorageContext.DeleteIndex() ?? false;
public async Task<bool> DeleteIndex(LuceneIndex luceneIndex) =>
await luceneIndex.StorageContext.DeleteIndex();

private Task<int> DeleteRecordsInternal(IEnumerable<string> itemGuids, string indexName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface ILuceneClient
/// Removes Lucene index.
/// </summary>
/// <param name="luceneIndex">The index to be deleted.</param>
bool DeleteIndex(LuceneIndex luceneIndex);
Task<bool> DeleteIndex(LuceneIndex luceneIndex);

/// <summary>
/// Gets the indices of the Lucene application with basic statistics.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public interface ILuceneIndexStorageStrategy
/// Deletes all files associated with an index.
/// </summary>
/// <param name="indexStoragePath">Path root of the index storage.</param>
bool DeleteIndex(string indexStoragePath);
Task<bool> DeleteIndex(string indexStoragePath);
}

internal class GenerationStorageStrategy : ILuceneIndexStorageStrategy
Expand Down Expand Up @@ -152,7 +152,7 @@ public bool ScheduleRemoval(IndexStorageModel storage)
return true;
}

public bool DeleteIndex(string indexStoragePath)
public async Task<bool> DeleteIndex(string indexStoragePath)
{
var deleteDir = new DirectoryInfo(indexStoragePath);

Expand All @@ -161,6 +161,25 @@ public bool DeleteIndex(string indexStoragePath)
return true;
}

int numberOfRetries = 10;
int millisecondsRetryDelay = 100;

for (int i = 0; i < numberOfRetries; i++)
{
try
{
Trace.WriteLine($"D={deleteDir.Name}: delete *.*", $"GenerationStorageStrategy.DeleteIndex");
deleteDir.Delete(true);
return true;
}
catch
{
// Do nothing with exception and retry.
// The directory may be locked by another process, but we can not know about it without trying to delete it.
// The exact exception is not known and is not written in .NET documentation.
await Task.Delay(millisecondsRetryDelay);
}
}
try
{
Trace.WriteLine($"D={deleteDir.Name}: delete *.*", $"GenerationStorageStrategy.DeleteIndex");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,6 @@ public void EnforceRetentionPolicy()
storageStrategy.PerformCleanup(indexStoragePathRoot);
}

public bool DeleteIndex() =>
storageStrategy.DeleteIndex(indexStoragePathRoot);
public async Task<bool> DeleteIndex() =>
await storageStrategy.DeleteIndex(indexStoragePathRoot);
}

0 comments on commit 15cd9f0

Please sign in to comment.