-
Notifications
You must be signed in to change notification settings - Fork 720
Fix snapshot disk space check to run before download #12861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
263 changes: 263 additions & 0 deletions
263
src/Nethermind/Nethermind.Init.Snapshot.Test/InitDatabaseSnapshotTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,263 @@ | ||
| // SPDX-FileCopyrightText: 2026 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| using System; | ||
| using System.Formats.Tar; | ||
| using System.IO; | ||
| using System.IO.Abstractions; | ||
| using System.Net; | ||
| using System.Net.Sockets; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Nethermind.Api; | ||
| using Nethermind.Core.Test.IO; | ||
| using Nethermind.Logging; | ||
| using NSubstitute; | ||
| using NUnit.Framework; | ||
| using Testably.Abstractions; | ||
|
|
||
| namespace Nethermind.Init.Snapshot.Test; | ||
|
|
||
| public class InitDatabaseSnapshotTests | ||
| { | ||
| private const int SnapshotPayloadSize = 100_000; | ||
|
|
||
| private TempPath _tempDir = null!; | ||
| private string _dbPath = null!; | ||
| private string _snapshotPath = null!; | ||
| private SnapshotConfig _snapshotConfig = null!; | ||
| private INethermindApi _api = null!; | ||
|
|
||
| [SetUp] | ||
| public void SetUp() | ||
| { | ||
| _tempDir = TempPath.GetTempDirectory(); | ||
| string snapshotDirectory = Path.Combine(_tempDir.Path, "snapshot"); | ||
| Directory.CreateDirectory(snapshotDirectory); | ||
| _dbPath = Path.Combine(_tempDir.Path, "db"); | ||
| _snapshotConfig = new SnapshotConfig | ||
| { | ||
| Enabled = true, | ||
| DownloadUrl = "http://127.0.0.1:1/snapshot.tar", | ||
| SnapshotDirectory = snapshotDirectory, | ||
| SnapshotFileName = "snapshot.tar", | ||
| StripComponents = 1 | ||
| }; | ||
| _snapshotPath = Path.Combine(snapshotDirectory, _snapshotConfig.SnapshotFileName); | ||
|
|
||
| _api = Substitute.For<INethermindApi>(); | ||
| _api.Config<ISnapshotConfig>().Returns(_snapshotConfig); | ||
| _api.Config<IInitConfig>().Returns(new InitConfig { BaseDbPath = _dbPath }); | ||
| _api.LogManager.Returns(LimboLogs.Instance); | ||
| _api.FileSystem.Returns(new RealFileSystem()); | ||
| } | ||
|
|
||
| [TearDown] | ||
| public void TearDown() => _tempDir.Dispose(); | ||
|
|
||
| [Test] | ||
| public async Task Execute_FreeSpaceCoversExtractionButNotLegacyMultiplier_ExtractsSnapshot() | ||
| { | ||
| long snapshotSize = WriteSnapshotTar(); | ||
| AdvanceCheckpoint(SnapshotStage.Verified); | ||
| long freeSpace = snapshotSize * 2; | ||
| Assert.That(freeSpace, Is.GreaterThanOrEqualTo(InitDatabaseSnapshot.GetRequiredSpaceForExtraction(snapshotSize)), | ||
| "precondition: free space must cover the extraction estimate"); | ||
| Assert.That(freeSpace, Is.LessThan((long)(snapshotSize * 2.5)), | ||
| "precondition: free space must be below the legacy 2.5x requirement to prove the regression is fixed"); | ||
| InitDatabaseSnapshot step = new(_api, DrivesWithFreeSpace(freeSpace)); | ||
|
|
||
| await step.Execute(CancellationToken.None); | ||
|
|
||
| Assert.That(File.Exists(Path.Combine(_dbPath, "state.bin")), Is.True, | ||
| "the snapshot content should be extracted into the database directory"); | ||
| Assert.That(File.Exists(_snapshotPath), Is.False, | ||
| "the snapshot archive should be deleted after a successful extraction"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Execute_FreeSpaceBelowExtractionEstimate_ThrowsIOException() | ||
| { | ||
| long snapshotSize = WriteSnapshotTar(); | ||
| AdvanceCheckpoint(SnapshotStage.Verified); | ||
| InitDatabaseSnapshot step = new(_api, DrivesWithFreeSpace(snapshotSize)); | ||
|
|
||
| IOException exception = Assert.ThrowsAsync<IOException>(() => step.Execute(CancellationToken.None))!; | ||
|
|
||
| Assert.That(exception.Message, Does.Contain("Insufficient disk space"), | ||
| "the extraction should be rejected when free space is below the extraction estimate"); | ||
| Assert.That(Directory.Exists(_dbPath), Is.False, | ||
| "nothing should be extracted when free space is insufficient"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Execute_FreeSpaceBelowDownloadRequirement_ThrowsIOExceptionBeforeDownloading() | ||
| { | ||
| using SnapshotServer server = SnapshotServer.Start(contentLength: 1_000_000); | ||
| _snapshotConfig.DownloadUrl = server.Url; | ||
| InitDatabaseSnapshot step = new(_api, DrivesWithFreeSpace(1_000_000)); | ||
|
|
||
| IOException exception = Assert.ThrowsAsync<IOException>(() => step.Execute(CancellationToken.None))!; | ||
|
|
||
| Assert.That(exception.Message, Does.Contain("Insufficient disk space"), | ||
| "the download should be rejected when free space cannot fit the snapshot and its extraction"); | ||
| Assert.That(File.Exists(_snapshotPath), Is.False, | ||
| "no bytes should be downloaded when free space is insufficient"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Execute_ServerDoesNotReportSize_SkipsPreDownloadCheckAndCompletes() | ||
| { | ||
| byte[] tarBytes = BuildSnapshotTar(); | ||
| using SnapshotServer server = SnapshotServer.Start(payload: tarBytes); | ||
| _snapshotConfig.DownloadUrl = server.Url; | ||
| InitDatabaseSnapshot step = new(_api, DrivesWithFreeSpace(tarBytes.Length * 2L)); | ||
|
|
||
| await step.Execute(CancellationToken.None); | ||
|
|
||
| Assert.That(File.Exists(Path.Combine(_dbPath, "state.bin")), Is.True, | ||
| "the download and extraction should proceed when the server reports no snapshot size"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Execute_SizeProbeFailsTransiently_SkipsPreDownloadCheckAndCompletes() | ||
| { | ||
| byte[] tarBytes = BuildSnapshotTar(); | ||
| using SnapshotServer server = SnapshotServer.Start(contentLength: tarBytes.Length, payload: tarBytes, failFirstRequests: 1); | ||
| _snapshotConfig.DownloadUrl = server.Url; | ||
| InitDatabaseSnapshot step = new(_api, DrivesWithFreeSpace(tarBytes.Length * 2L)); | ||
|
|
||
| await step.Execute(CancellationToken.None); | ||
|
|
||
| Assert.That(File.Exists(Path.Combine(_dbPath, "state.bin")), Is.True, | ||
| "the download and extraction should proceed when the size probe fails transiently"); | ||
| } | ||
|
|
||
| [TestCase(1_000, 0, 2_500, TestName = "FreshDownload")] | ||
| [TestCase(1_000, 400, 2_100, TestName = "ResumedDownload")] | ||
| public void GetRequiredSpaceForDownload_ForGivenSizes_AddsRemainingBytesToExtractionEstimate( | ||
| long totalSize, long existingSize, long expected) => | ||
| Assert.That(InitDatabaseSnapshot.GetRequiredSpaceForDownload(totalSize, existingSize), Is.EqualTo(expected), | ||
| "the pre-download requirement should be the remaining bytes plus the extraction estimate of the full snapshot"); | ||
|
|
||
| private long WriteSnapshotTar() | ||
| { | ||
| byte[] tarBytes = BuildSnapshotTar(); | ||
| File.WriteAllBytes(_snapshotPath, tarBytes); | ||
| return tarBytes.Length; | ||
| } | ||
|
|
||
| private static byte[] BuildSnapshotTar() | ||
| { | ||
| using MemoryStream tarStream = new(); | ||
| using (TarWriter tarWriter = new(tarStream, leaveOpen: true)) | ||
| { | ||
| tarWriter.WriteEntry(new PaxTarEntry(TarEntryType.Directory, "data")); | ||
| PaxTarEntry fileEntry = new(TarEntryType.RegularFile, "data/state.bin") | ||
| { | ||
| DataStream = new MemoryStream(new byte[SnapshotPayloadSize]) | ||
| }; | ||
| tarWriter.WriteEntry(fileEntry); | ||
| } | ||
|
|
||
| return tarStream.ToArray(); | ||
| } | ||
|
|
||
| private void AdvanceCheckpoint(SnapshotStage stage) => | ||
| new SnapshotCheckpoint(_snapshotConfig, LimboLogs.Instance).Advance(stage); | ||
|
|
||
| private static IDriveInfo[] DrivesWithFreeSpace(long freeSpace) | ||
| { | ||
| IDriveInfo drive = Substitute.For<IDriveInfo>(); | ||
| drive.AvailableFreeSpace.Returns(freeSpace); | ||
| drive.RootDirectory.FullName.Returns("/"); | ||
| return [drive]; | ||
| } | ||
|
|
||
| private sealed class SnapshotServer : IDisposable | ||
| { | ||
| private readonly HttpListener _listener; | ||
|
|
||
| public string Url { get; } | ||
|
|
||
| private SnapshotServer(HttpListener listener, string url) | ||
| { | ||
| _listener = listener; | ||
| Url = url; | ||
| } | ||
|
|
||
| public static SnapshotServer Start(long? contentLength = null, byte[] payload = null, int failFirstRequests = 0) | ||
| { | ||
| (HttpListener listener, int port) = StartListener(); | ||
| int requestIndex = 0; | ||
|
|
||
| _ = Task.Run(async () => | ||
| { | ||
| while (listener.IsListening) | ||
| { | ||
| HttpListenerContext context; | ||
| try | ||
| { | ||
| context = await listener.GetContextAsync(); | ||
| } | ||
| catch (Exception) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| if (requestIndex++ < failFirstRequests) | ||
| { | ||
| context.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable; | ||
| context.Response.Close(); | ||
| continue; | ||
| } | ||
|
|
||
| byte[] body = payload ?? new byte[1024]; | ||
| if (contentLength is not null) | ||
| context.Response.ContentLength64 = contentLength.Value; | ||
| else | ||
| context.Response.SendChunked = true; | ||
| await context.Response.OutputStream.WriteAsync(body); | ||
| context.Response.OutputStream.Close(); | ||
| } | ||
| catch (Exception) | ||
| { | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return new SnapshotServer(listener, $"http://127.0.0.1:{port}/snapshot.tar"); | ||
| } | ||
|
|
||
| private static (HttpListener Listener, int Port) StartListener() | ||
| { | ||
| for (int attempt = 0; ; attempt++) | ||
| { | ||
| TcpListener portProbe = new(IPAddress.Loopback, 0); | ||
| portProbe.Start(); | ||
| int port = ((IPEndPoint)portProbe.LocalEndpoint).Port; | ||
| portProbe.Stop(); | ||
|
|
||
| HttpListener listener = new(); | ||
| listener.Prefixes.Add($"http://127.0.0.1:{port}/"); | ||
| try | ||
| { | ||
| listener.Start(); | ||
| return (listener, port); | ||
| } | ||
| catch (HttpListenerException) when (attempt < 5) | ||
| { | ||
| listener.Close(); | ||
| } | ||
| } | ||
| } | ||
|
svlachakis marked this conversation as resolved.
|
||
|
|
||
| public void Dispose() | ||
| { | ||
| _listener.Stop(); | ||
| _listener.Close(); | ||
| } | ||
| } | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
src/Nethermind/Nethermind.Init.Snapshot.Test/Nethermind.Init.Snapshot.Test.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <Import Project="../tests.props" /> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="NSubstitute" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Nethermind.Core.Test\Nethermind.Core.Test.csproj" /> | ||
| <ProjectReference Include="..\Nethermind.Init.Snapshot\Nethermind.Init.Snapshot.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.