Skip to content

Commit 3d74bf9

Browse files
deffrianjmederosalvarado
authored andcommitted
Fix snapshot resume + op snapshot config (#6309)
* Resume snapshot download only if 206 * Delete after unzipping & new hash * Fix formatting * Fix PartialContent issue --------- Co-authored-by: Jorge Mederos <[email protected]>
1 parent 0034333 commit 3d74bf9

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

src/Nethermind/Nethermind.Init.Snapshot/InitDatabaseSnapshot.cs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
using Nethermind.Init.Steps;
77
using Nethermind.Logging;
88
using System.IO.Compression;
9+
using System.Net;
910
using System.Security.Cryptography;
10-
using Nethermind.Core;
1111
using Nethermind.Core.Extensions;
1212

1313
namespace Nethermind.Init.Snapshot;
@@ -58,15 +58,21 @@ private async Task InitDbFromSnapshot(CancellationToken cancellationToken)
5858
string snapshotFileName = Path.Combine(snapshotConfig.SnapshotDirectory, snapshotConfig.SnapshotFileName);
5959
Directory.CreateDirectory(snapshotConfig.SnapshotDirectory);
6060

61-
await DownloadSnapshotTo(snapshotUrl, snapshotFileName, cancellationToken);
62-
// schedule the snapshot file deletion, but only if the download completed
63-
// otherwise leave it to resume the download later
64-
using Reactive.AnonymousDisposable deleteSnapshot = new(() =>
61+
while (true)
6562
{
66-
if (_logger.IsInfo)
67-
_logger.Info($"Deleting snapshot file {snapshotFileName}.");
68-
File.Delete(snapshotFileName);
69-
});
63+
try
64+
{
65+
await DownloadSnapshotTo(snapshotUrl, snapshotFileName, cancellationToken);
66+
break;
67+
}
68+
catch (IOException e)
69+
{
70+
if (_logger.IsError)
71+
_logger.Error($"Snapshot download failed. Retrying in 5 seconds. Error: {e}");
72+
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
73+
}
74+
cancellationToken.ThrowIfCancellationRequested();
75+
}
7076

7177
if (snapshotConfig.Checksum is not null)
7278
{
@@ -88,7 +94,12 @@ private async Task InitDbFromSnapshot(CancellationToken cancellationToken)
8894
await ExtractSnapshotTo(snapshotFileName, dbPath, cancellationToken);
8995

9096
if (_logger.IsInfo)
97+
{
9198
_logger.Info("Database successfully initialized from snapshot.");
99+
_logger.Info($"Deleting snapshot file {snapshotFileName}.");
100+
}
101+
102+
File.Delete(snapshotFileName);
92103
}
93104

94105
private async Task DownloadSnapshotTo(
@@ -116,9 +127,24 @@ private async Task DownloadSnapshotTo(
116127
(await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken))
117128
.EnsureSuccessStatusCode();
118129

130+
FileMode snapshotFileMode = FileMode.Create;
131+
if (snapshotFileInfo.Exists && response.StatusCode == HttpStatusCode.PartialContent)
132+
{
133+
snapshotFileMode = FileMode.Append;
134+
}
135+
else if (response.StatusCode == HttpStatusCode.OK)
136+
{
137+
if (snapshotFileInfo.Exists && _logger.IsWarn)
138+
_logger.Warn("Download couldn't be resumed. Starting from the beginning.");
139+
}
140+
else
141+
{
142+
throw new IOException($"Unexpected status code: {response.StatusCode}");
143+
}
144+
119145
await using Stream contentStream = await response.Content.ReadAsStreamAsync(cancellationToken);
120146
await using FileStream snapshotFileStream = new(
121-
snapshotFileName, FileMode.Append, FileAccess.Write, FileShare.None, BufferSize, true);
147+
snapshotFileName, snapshotFileMode, FileAccess.Write, FileShare.None, BufferSize, true);
122148

123149
long totalBytesRead = snapshotFileStream.Length;
124150
long? totalBytesToRead = totalBytesRead + response.Content.Headers.ContentLength;

src/Nethermind/Nethermind.Runner/configs/op-mainnet.cfg

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
},
2525
"Snapshot": {
2626
"Enabled": true,
27-
"DownloadUrl": "http://optimism-snapshot.nethermind.io/op-mainnet-genesis.zip",
28-
"SnapshotFileName": "op-mainnet-genesis.zip",
29-
"Checksum": "0xe6efe4bc27f8a7eb57f1a6c3d88199c104cc7dcffe6e5d083fbaccea508f8ed5"
27+
"DownloadUrl": "http://optimism-snapshot.nethermind.io/op-mainnet-genesis-v1.zip",
28+
"SnapshotFileName": "op-mainnet-genesis-v1.zip",
29+
"Checksum": "0xd7e15b26175c4c924acf75c5790e75d5eaa044977ca8e1904dc62d5d0769eba3"
3030
}
3131
}

0 commit comments

Comments
 (0)