Repository navigation
Get rid of Hash::dummy from BinaryCacheStore #3935
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
Changes from all commits
9fbc31a
412b3a5
3f226f7
c40c832
5db83dd
1832436
25fffdd
6c31297
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -142,17 +142,10 @@ struct FileSource : FdSource | |||||
| } | ||||||
| }; | ||||||
|
|
||||||
| void BinaryCacheStore::addToStore(const ValidPathInfo & info, Source & narSource, | ||||||
| RepairFlag repair, CheckSigsFlag checkSigs) | ||||||
| ref<const ValidPathInfo> BinaryCacheStore::addToStoreCommon( | ||||||
| Source & narSource, RepairFlag repair, CheckSigsFlag checkSigs, | ||||||
| std::function<ValidPathInfo(HashResult)> mkInfo) | ||||||
| { | ||||||
| assert(info.narSize); | ||||||
|
|
||||||
| if (!repair && isValidPath(info.path)) { | ||||||
| // FIXME: copyNAR -> null sink | ||||||
| narSource.drain(); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| auto [fdTemp, fnTemp] = createTempFile(); | ||||||
|
|
||||||
| AutoDelete autoDelete(fnTemp); | ||||||
|
|
@@ -162,23 +155,24 @@ void BinaryCacheStore::addToStore(const ValidPathInfo & info, Source & narSource | |||||
| /* Read the NAR simultaneously into a CompressionSink+FileSink (to | ||||||
| write the compressed NAR to disk), into a HashSink (to get the | ||||||
| NAR hash), and into a NarAccessor (to get the NAR listing). */ | ||||||
| HashSink fileHashSink(htSHA256); | ||||||
| HashSink fileHashSink { htSHA256 }; | ||||||
| std::shared_ptr<FSAccessor> narAccessor; | ||||||
| HashSink narHashSink { htSHA256 }; | ||||||
| { | ||||||
| FdSink fileSink(fdTemp.get()); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is beyond the scope of this PR, but since we're revisiting these store API methods it's worth noting that we could optimize away the tmpfile if we know a little bit more in advance. Although optimizing away a tmpfile seems unimpressive, it changes the time taken from When the file is known to be small (low hanging fruit) Add a size parameter to When we know the nar hash in advance For http binary caches this does require us to change the binary cache filenames to match uncompressed hashes, which seems to be equivalent and can only result in one-time duplication in existing caches when new paths are uploaded. In this case it does make sense to have both
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided not to try to optimize away the temporary file for small NARs because the overhead is likely to be insignificant compared to stuff like HTTP requests. |
||||||
| TeeSink teeSink(fileSink, fileHashSink); | ||||||
| auto compressionSink = makeCompressionSink(compression, teeSink); | ||||||
| TeeSource teeSource(narSource, *compressionSink); | ||||||
| TeeSink teeSinkCompressed { fileSink, fileHashSink }; | ||||||
| auto compressionSink = makeCompressionSink(compression, teeSinkCompressed); | ||||||
| TeeSink teeSinkUncompressed { *compressionSink, narHashSink }; | ||||||
| TeeSource teeSource { narSource, teeSinkUncompressed }; | ||||||
| narAccessor = makeNarAccessor(teeSource); | ||||||
| compressionSink->finish(); | ||||||
| fileSink.flush(); | ||||||
| } | ||||||
|
|
||||||
| auto now2 = std::chrono::steady_clock::now(); | ||||||
|
|
||||||
| auto info = mkInfo(narHashSink.finish()); | ||||||
| auto narInfo = make_ref<NarInfo>(info); | ||||||
| narInfo->narSize = info.narSize; | ||||||
| narInfo->narHash = info.narHash; | ||||||
| narInfo->compression = compression; | ||||||
| auto [fileHash, fileSize] = fileHashSink.finish(); | ||||||
| narInfo->fileHash = fileHash; | ||||||
|
|
@@ -300,6 +294,41 @@ void BinaryCacheStore::addToStore(const ValidPathInfo & info, Source & narSource | |||||
| writeNarInfo(narInfo); | ||||||
|
|
||||||
| stats.narInfoWrite++; | ||||||
|
|
||||||
| return narInfo; | ||||||
| } | ||||||
|
|
||||||
| void BinaryCacheStore::addToStore(const ValidPathInfo & info, Source & narSource, | ||||||
| RepairFlag repair, CheckSigsFlag checkSigs) | ||||||
| { | ||||||
| if (!repair && isValidPath(info.path)) { | ||||||
| // FIXME: copyNAR -> null sink | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to parse the NAR to determine the end if we make the caller responsible for ending
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think agree it's better to make the caller responsible, but I'm a bit wary on changing the direction of this FIXME as it and this code already existed, I just moved it here. I'll let @edolstra decide :). |
||||||
| narSource.drain(); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| addToStoreCommon(narSource, repair, checkSigs, {[&](HashResult nar) { | ||||||
| /* FIXME reinstate these, once we can correctly do hash modulo sink as | ||||||
| needed. We need to throw here in case we uploaded a corrupted store path. */ | ||||||
| // assert(info.narHash == nar.first); | ||||||
| // assert(info.narSize == nar.second); | ||||||
| return info; | ||||||
| }}); | ||||||
| } | ||||||
|
|
||||||
| StorePath BinaryCacheStore::addToStoreFromDump(Source & dump, const string & name, | ||||||
| FileIngestionMethod method, HashType hashAlgo, RepairFlag repair) | ||||||
| { | ||||||
| if (method != FileIngestionMethod::Recursive || hashAlgo != htSHA256) | ||||||
| unsupported("addToStoreFromDump"); | ||||||
| return addToStoreCommon(dump, repair, CheckSigs, [&](HashResult nar) { | ||||||
| ValidPathInfo info { | ||||||
| makeFixedOutputPath(method, nar.first, name), | ||||||
| nar.first, | ||||||
| }; | ||||||
| info.narSize = nar.second; | ||||||
| return info; | ||||||
| })->path; | ||||||
| } | ||||||
|
|
||||||
| bool BinaryCacheStore::isValidPathUncached(const StorePath & storePath) | ||||||
|
|
@@ -367,50 +396,52 @@ void BinaryCacheStore::queryPathInfoUncached(const StorePath & storePath, | |||||
| StorePath BinaryCacheStore::addToStore(const string & name, const Path & srcPath, | ||||||
| FileIngestionMethod method, HashType hashAlgo, PathFilter & filter, RepairFlag repair) | ||||||
| { | ||||||
| // FIXME: some cut&paste from LocalStore::addToStore(). | ||||||
| /* FIXME: Make BinaryCacheStore::addToStoreCommon support | ||||||
| non-recursive+sha256 so we can just use the default | ||||||
| implementation of this method in terms of addToStoreFromDump. */ | ||||||
|
|
||||||
| /* Read the whole path into memory. This is not a very scalable | ||||||
| method for very large paths, but `copyPath' is mainly used for | ||||||
| small files. */ | ||||||
| StringSink sink; | ||||||
| std::optional<Hash> h; | ||||||
| HashSink sink { hashAlgo }; | ||||||
| if (method == FileIngestionMethod::Recursive) { | ||||||
| dumpPath(srcPath, sink, filter); | ||||||
| h = hashString(hashAlgo, *sink.s); | ||||||
| } else { | ||||||
| auto s = readFile(srcPath); | ||||||
| dumpString(s, sink); | ||||||
| h = hashString(hashAlgo, s); | ||||||
| readFile(srcPath, sink); | ||||||
| } | ||||||
| auto h = sink.finish().first; | ||||||
|
|
||||||
| ValidPathInfo info { | ||||||
| makeFixedOutputPath(method, *h, name), | ||||||
| Hash::dummy, // Will be fixed in addToStore, which recomputes nar hash | ||||||
| }; | ||||||
|
|
||||||
| auto source = StringSource { *sink.s }; | ||||||
| addToStore(info, source, repair, CheckSigs); | ||||||
|
|
||||||
| return std::move(info.path); | ||||||
| auto source = sinkToSource([&](Sink & sink) { | ||||||
| dumpPath(srcPath, sink, filter); | ||||||
| }); | ||||||
| return addToStoreCommon(*source, repair, CheckSigs, [&](HashResult nar) { | ||||||
| ValidPathInfo info { | ||||||
| makeFixedOutputPath(method, h, name), | ||||||
| nar.first, | ||||||
| }; | ||||||
| info.narSize = nar.second; | ||||||
| info.ca = FixedOutputHash { | ||||||
| .method = method, | ||||||
| .hash = h, | ||||||
| }; | ||||||
| return info; | ||||||
| })->path; | ||||||
| } | ||||||
|
|
||||||
| StorePath BinaryCacheStore::addTextToStore(const string & name, const string & s, | ||||||
| const StorePathSet & references, RepairFlag repair) | ||||||
| { | ||||||
| ValidPathInfo info { | ||||||
| computeStorePathForText(name, s, references), | ||||||
| Hash::dummy, // Will be fixed in addToStore, which recomputes nar hash | ||||||
| }; | ||||||
| info.references = references; | ||||||
|
|
||||||
| if (repair || !isValidPath(info.path)) { | ||||||
| StringSink sink; | ||||||
| dumpString(s, sink); | ||||||
| auto source = StringSource { *sink.s }; | ||||||
| addToStore(info, source, repair, CheckSigs); | ||||||
| } | ||||||
|
|
||||||
| return std::move(info.path); | ||||||
| auto textHash = hashString(htSHA256, s); | ||||||
| auto path = makeTextPath(name, textHash, references); | ||||||
|
|
||||||
| if (!repair && isValidPath(path)) | ||||||
| return path; | ||||||
|
|
||||||
| auto source = StringSource { s }; | ||||||
| return addToStoreCommon(source, repair, CheckSigs, [&](HashResult nar) { | ||||||
| ValidPathInfo info { path, nar.first }; | ||||||
| info.narSize = nar.second; | ||||||
| info.ca = TextHash { textHash }; | ||||||
| info.references = references; | ||||||
| return info; | ||||||
| })->path; | ||||||
| } | ||||||
|
|
||||||
| ref<FSAccessor> BinaryCacheStore::getFSAccessor() | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.