in src/ccf/ccf-provider-common/AzFileShare.cs [327:413]
public static async Task DeleteUncommittedLedgerFiles(
string networkName,
string shareName,
JsonObject providerConfig,
IProgress<string> progress)
{
FileServiceResource fileService = await GetFileService(providerConfig);
FileShareCollection fileShareCollection = fileService.GetFileShares();
AsyncPageable<FileShareResource> response = fileShareCollection.GetAllAsync();
bool filesDeleted = false;
await foreach (FileShareResource fsItem in response)
{
// Get or else Metadata is not populated.
FileShareResource fileShare = await fsItem.GetAsync();
if (!fileShare.Data.Metadata.TryGetValue(CcfNetworkNameKey, out var value) ||
value != networkName)
{
continue;
}
if (fileShare.Data.Name != shareName)
{
continue;
}
progress.Report(
$"Inspecting file share '{fileShare.Data.Name}' for uncommitted ledger files.");
var share = await GetShareClient(fileShare.Data.Name, providerConfig);
var dir = share.GetDirectoryClient("ledger");
if (await dir.ExistsAsync())
{
await foreach (ShareFileItem item in dir.GetFilesAndDirectoriesAsync())
{
progress.Report($"Found file {item.Name} on file share " +
$"'{fileShare.Data.Name}'.");
// fileName = ledger_13_14 or ledger_12.
// Avoid .committed files so avoid any file that has a . in its name.
var fileName = item.Name;
if (fileName.StartsWith("ledger_") && !fileName.Contains('.'))
{
// Delete the file.
progress.Report($"Removing file {item.Name} from file share " +
$"'{fileShare.Data.Name}'.");
await DeleteFileWithRetries(dir, item.Name);
filesDeleted = true;
}
}
}
}
if (!filesDeleted)
{
progress.Report($"Did not find any uncommitted ledger files to delete on file share " +
$"'{shareName}'.");
}
async Task DeleteFileWithRetries(ShareDirectoryClient dir, string fileName)
{
TimeSpan retryTimeout = TimeSpan.FromSeconds(90);
var stopwatch = Stopwatch.StartNew();
int attempt = 1;
while (true)
{
try
{
await dir.DeleteFileAsync(fileName);
return;
}
catch (RequestFailedException rfe) when (rfe.ErrorCode == "SharingViolation")
{
if (stopwatch.Elapsed > retryTimeout)
{
throw;
}
progress.Report(
$"Waiting for file {fileName} to be not in use (SharingViolation) " +
$"before again deleting it. Attempt: {attempt}.");
attempt++;
await Task.Delay(TimeSpan.FromSeconds(5));
}
}
}
}