| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/fetch.cc |
| Date: | 2025-11-09 02:35:23 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 155 | 157 | 98.7% |
| Branches: | 101 | 173 | 58.4% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | |||
| 6 | #include "fetch.h" | ||
| 7 | |||
| 8 | #include <unistd.h> | ||
| 9 | |||
| 10 | #include "backoff.h" | ||
| 11 | #include "cache.h" | ||
| 12 | #include "clientctx.h" | ||
| 13 | #include "interrupt.h" | ||
| 14 | #include "network/download.h" | ||
| 15 | #include "quota.h" | ||
| 16 | #include "statistics.h" | ||
| 17 | #include "util/concurrency.h" | ||
| 18 | #include "util/logging.h" | ||
| 19 | #include "util/posix.h" | ||
| 20 | |||
| 21 | using namespace std; // NOLINT | ||
| 22 | |||
| 23 | namespace cvmfs { | ||
| 24 | |||
| 25 | 92 | void TLSDestructor(void *data) { | |
| 26 | 92 | Fetcher::ThreadLocalStorage *tls = static_cast<Fetcher::ThreadLocalStorage *>( | |
| 27 | data); | ||
| 28 | 92 | std::vector<Fetcher::ThreadLocalStorage *> *tls_blocks = &tls->fetcher | |
| 29 | ->tls_blocks_; | ||
| 30 | |||
| 31 | { | ||
| 32 | 92 | const MutexLockGuard m(tls->fetcher->lock_tls_blocks_); | |
| 33 | 92 | for (vector<Fetcher::ThreadLocalStorage *>::iterator | |
| 34 | 92 | i = tls_blocks->begin(), | |
| 35 | 92 | iEnd = tls_blocks->end(); | |
| 36 |
1/2✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
|
184 | i != iEnd; |
| 37 | 92 | ++i) { | |
| 38 |
2/2✓ Branch 1 taken 92 times.
✓ Branch 2 taken 92 times.
|
184 | if ((*i) == tls) { |
| 39 |
1/2✓ Branch 2 taken 92 times.
✗ Branch 3 not taken.
|
92 | tls_blocks->erase(i); |
| 40 | 92 | break; | |
| 41 | } | ||
| 42 | } | ||
| 43 | 92 | } | |
| 44 | 92 | tls->fetcher->CleanupTls(tls); | |
| 45 | 92 | } | |
| 46 | |||
| 47 | |||
| 48 | /** | ||
| 49 | * Called when a thread exists, releases a ThreadLocalStorage object and | ||
| 50 | * removes the pointer to it from tls_blocks_. | ||
| 51 | */ | ||
| 52 | 899 | void Fetcher::CleanupTls(ThreadLocalStorage *tls) { | |
| 53 | 899 | ClosePipe(tls->pipe_wait); | |
| 54 |
1/2✓ Branch 0 taken 899 times.
✗ Branch 1 not taken.
|
899 | delete tls; |
| 55 | 899 | } | |
| 56 | |||
| 57 | |||
| 58 | /** | ||
| 59 | * Initialized thread-local storage if called the first time in a new thread. | ||
| 60 | */ | ||
| 61 | 1747 | Fetcher::ThreadLocalStorage *Fetcher::GetTls() { | |
| 62 | ThreadLocalStorage *tls = static_cast<ThreadLocalStorage *>( | ||
| 63 | 1747 | pthread_getspecific(thread_local_storage_)); | |
| 64 |
2/2✓ Branch 0 taken 848 times.
✓ Branch 1 taken 899 times.
|
1747 | if (tls != NULL) |
| 65 | 848 | return tls; | |
| 66 | |||
| 67 |
2/4✓ Branch 1 taken 899 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 899 times.
✗ Branch 5 not taken.
|
899 | tls = new ThreadLocalStorage(); |
| 68 | 899 | tls->fetcher = this; | |
| 69 |
1/2✓ Branch 1 taken 899 times.
✗ Branch 2 not taken.
|
899 | MakePipe(tls->pipe_wait); |
| 70 | 899 | tls->download_job.SetCompressed(true); | |
| 71 | 899 | tls->download_job.SetProbeHosts(true); | |
| 72 | 899 | const int retval = pthread_setspecific(thread_local_storage_, tls); | |
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 899 times.
|
899 | assert(retval == 0); |
| 74 | |||
| 75 | 899 | const MutexLockGuard m(lock_tls_blocks_); | |
| 76 |
1/2✓ Branch 1 taken 899 times.
✗ Branch 2 not taken.
|
899 | tls_blocks_.push_back(tls); |
| 77 | |||
| 78 | 899 | return tls; | |
| 79 | 899 | } | |
| 80 | |||
| 81 | |||
| 82 | 1759 | int Fetcher::Fetch(const CacheManager::LabeledObject &object, | |
| 83 | const std::string &alt_url) { | ||
| 84 | int fd_return; // Read-only file descriptor that is returned | ||
| 85 | int retval; | ||
| 86 | |||
| 87 | 1759 | perf::Inc(n_invocations); | |
| 88 | |||
| 89 | // Try to open from local cache | ||
| 90 |
3/4✓ Branch 1 taken 1759 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 472 times.
✓ Branch 4 taken 1287 times.
|
1759 | if ((fd_return = OpenSelect(object)) >= 0) { |
| 91 |
1/2✓ Branch 2 taken 472 times.
✗ Branch 3 not taken.
|
472 | LogCvmfs(kLogCache, kLogDebug, "hit: %s", object.label.path.c_str()); |
| 92 | 472 | return fd_return; | |
| 93 | } | ||
| 94 | |||
| 95 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1287 times.
|
1287 | if (object.id.IsNull()) { |
| 96 | // This has been seen when trying to load the root catalog signed by an | ||
| 97 | // invalid certificate on an empty cache | ||
| 98 | // TODO(jblomer): check if still necessary after the catalog reload refactor | ||
| 99 | ✗ | LogCvmfs(kLogCache, kLogDebug, "cancel attempt to download null hash"); | |
| 100 | ✗ | return -EIO; | |
| 101 | } | ||
| 102 | |||
| 103 |
1/2✓ Branch 1 taken 1287 times.
✗ Branch 2 not taken.
|
1287 | ThreadLocalStorage *tls = GetTls(); |
| 104 | |||
| 105 | // Synchronization point: either act as a master thread for this object or | ||
| 106 | // enqueue to the list of waiting threads. | ||
| 107 | 1287 | pthread_mutex_lock(lock_queues_download_); | |
| 108 | 1287 | const ThreadQueues::iterator iDownloadQueue = queues_download_.find( | |
| 109 |
1/2✓ Branch 1 taken 1287 times.
✗ Branch 2 not taken.
|
1287 | object.id); |
| 110 |
2/2✓ Branch 2 taken 46 times.
✓ Branch 3 taken 1241 times.
|
1287 | if (iDownloadQueue != queues_download_.end()) { |
| 111 |
1/2✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
|
46 | LogCvmfs(kLogCache, kLogDebug, "waiting for download of %s", |
| 112 | object.label.path.c_str()); | ||
| 113 | |||
| 114 |
1/2✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
|
46 | iDownloadQueue->second->push_back(tls->pipe_wait[1]); |
| 115 | 46 | pthread_mutex_unlock(lock_queues_download_); | |
| 116 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | ReadPipe(tls->pipe_wait[0], &fd_return, sizeof(int)); |
| 117 | |||
| 118 |
1/2✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
|
46 | LogCvmfs(kLogCache, kLogDebug, "received from another thread fd %d for %s", |
| 119 | fd_return, object.label.path.c_str()); | ||
| 120 | 46 | return fd_return; | |
| 121 | } else { | ||
| 122 | // Seems we are the first one, check again in the cache (race condition) | ||
| 123 |
1/2✓ Branch 1 taken 1241 times.
✗ Branch 2 not taken.
|
1241 | fd_return = OpenSelect(object); |
| 124 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1195 times.
|
1241 | if (fd_return >= 0) { |
| 125 | 46 | pthread_mutex_unlock(lock_queues_download_); | |
| 126 | 46 | return fd_return; | |
| 127 | } | ||
| 128 | |||
| 129 | // Create a new queue for this chunk | ||
| 130 |
1/2✓ Branch 1 taken 1195 times.
✗ Branch 2 not taken.
|
1195 | queues_download_[object.id] = &tls->other_pipes_waiting; |
| 131 | 1195 | pthread_mutex_unlock(lock_queues_download_); | |
| 132 | } | ||
| 133 | |||
| 134 | 1195 | perf::Inc(n_downloads); | |
| 135 | |||
| 136 | // Involve the download manager | ||
| 137 |
1/2✓ Branch 2 taken 1195 times.
✗ Branch 3 not taken.
|
1195 | LogCvmfs(kLogCache, kLogDebug, "downloading %s", object.label.path.c_str()); |
| 138 | 1195 | std::string url; | |
| 139 |
2/2✓ Branch 1 taken 138 times.
✓ Branch 2 taken 1057 times.
|
1195 | if (object.label.IsExternal()) { |
| 140 |
2/4✓ Branch 1 taken 138 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 138 times.
✗ Branch 5 not taken.
|
138 | url = !alt_url.empty() ? alt_url : object.label.path; |
| 141 | } else { | ||
| 142 |
8/14✓ Branch 1 taken 46 times.
✓ Branch 2 taken 1011 times.
✓ Branch 4 taken 46 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1011 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1011 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1057 times.
✗ Branch 14 not taken.
✓ Branch 18 taken 1011 times.
✓ Branch 19 taken 46 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1057 | url = "/" + (alt_url.size() ? alt_url : "data/" + object.id.MakePath()); |
| 143 | } | ||
| 144 |
1/2✓ Branch 1 taken 1195 times.
✗ Branch 2 not taken.
|
1195 | void *txn = alloca(cache_mgr_->SizeOfTxn()); |
| 145 |
1/2✓ Branch 1 taken 1195 times.
✗ Branch 2 not taken.
|
1195 | retval = cache_mgr_->StartTxn(object.id, object.label.size, txn); |
| 146 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1149 times.
|
1195 | if (retval < 0) { |
| 147 |
1/2✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
|
46 | LogCvmfs(kLogCache, kLogDebug, "could not start transaction on %s", |
| 148 | object.label.path.c_str()); | ||
| 149 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | SignalWaitingThreads(retval, object.id, tls); |
| 150 | 46 | return retval; | |
| 151 | } | ||
| 152 |
1/2✓ Branch 1 taken 1149 times.
✗ Branch 2 not taken.
|
1149 | cache_mgr_->CtrlTxn(object.label, 0, txn); |
| 153 | |||
| 154 |
1/2✓ Branch 3 taken 1149 times.
✗ Branch 4 not taken.
|
1149 | LogCvmfs(kLogCache, kLogDebug, "miss: %s %s", object.label.path.c_str(), |
| 155 | url.c_str()); | ||
| 156 | 1149 | TransactionSink sink(cache_mgr_, txn); | |
| 157 | 1149 | tls->download_job.SetUrl(&url); | |
| 158 | 1149 | tls->download_job.SetSink(&sink); | |
| 159 | 1149 | tls->download_job.SetExpectedHash(&object.id); | |
| 160 | 1149 | tls->download_job.SetExtraInfo(&object.label.path); | |
| 161 |
1/2✓ Branch 1 taken 1149 times.
✗ Branch 2 not taken.
|
1149 | ClientCtx *ctx = ClientCtx::GetInstance(); |
| 162 |
3/4✓ Branch 1 taken 1149 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 70 times.
✓ Branch 4 taken 1079 times.
|
1149 | if (ctx->IsSet()) { |
| 163 |
1/2✓ Branch 5 taken 70 times.
✗ Branch 6 not taken.
|
70 | ctx->Get(tls->download_job.GetUidPtr(), |
| 164 | tls->download_job.GetGidPtr(), | ||
| 165 | tls->download_job.GetPidPtr(), | ||
| 166 | tls->download_job.GetInterruptCuePtr()); | ||
| 167 | } else { | ||
| 168 | 1079 | *(tls->download_job.GetUidPtr()) = -1; | |
| 169 | 1079 | *(tls->download_job.GetGidPtr()) = -1; | |
| 170 | 1079 | *(tls->download_job.GetPidPtr()) = -1; | |
| 171 | 1079 | *(tls->download_job.GetInterruptCuePtr()) = NULL; | |
| 172 | } | ||
| 173 | 1149 | tls->download_job.SetCompressed(object.label.zip_algorithm | |
| 174 | == zlib::kZlibDefault); | ||
| 175 | 1149 | tls->download_job.SetRangeOffset(object.label.range_offset); | |
| 176 | 1149 | tls->download_job.SetRangeSize(static_cast<int64_t>(object.label.size)); | |
| 177 |
1/2✓ Branch 1 taken 1149 times.
✗ Branch 2 not taken.
|
1149 | download_mgr_->Fetch(&tls->download_job); |
| 178 | |||
| 179 |
2/2✓ Branch 1 taken 936 times.
✓ Branch 2 taken 213 times.
|
1149 | if (tls->download_job.error_code() == download::kFailOk) { |
| 180 |
1/2✓ Branch 2 taken 936 times.
✗ Branch 3 not taken.
|
936 | LogCvmfs(kLogCache, kLogDebug, "finished downloading of %s", url.c_str()); |
| 181 | |||
| 182 |
1/2✓ Branch 1 taken 936 times.
✗ Branch 2 not taken.
|
936 | fd_return = cache_mgr_->OpenFromTxn(txn); |
| 183 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 890 times.
|
936 | if (fd_return < 0) { |
| 184 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | cache_mgr_->AbortTxn(txn); |
| 185 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | SignalWaitingThreads(fd_return, object.id, tls); |
| 186 | 46 | return fd_return; | |
| 187 | } | ||
| 188 | |||
| 189 |
1/2✓ Branch 1 taken 890 times.
✗ Branch 2 not taken.
|
890 | retval = cache_mgr_->CommitTxn(txn); |
| 190 |
2/2✓ Branch 0 taken 92 times.
✓ Branch 1 taken 798 times.
|
890 | if (retval < 0) { |
| 191 |
1/2✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
|
92 | cache_mgr_->Close(fd_return); |
| 192 |
1/2✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
|
92 | SignalWaitingThreads(retval, object.id, tls); |
| 193 | 92 | return retval; | |
| 194 | } | ||
| 195 |
1/2✓ Branch 1 taken 798 times.
✗ Branch 2 not taken.
|
798 | SignalWaitingThreads(fd_return, object.id, tls); |
| 196 | 798 | return fd_return; | |
| 197 | } | ||
| 198 | |||
| 199 | // Download failed | ||
| 200 |
1/5✗ Branch 4 not taken.
✓ Branch 5 taken 213 times.
✗ Branch 6 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
426 | LogCvmfs(kLogCache, kLogDebug | kLogSyslogErr, |
| 201 | "failed to fetch %s (hash: %s, error %d [%s])", | ||
| 202 |
1/2✓ Branch 1 taken 213 times.
✗ Branch 2 not taken.
|
426 | object.label.path.c_str(), object.id.ToString().c_str(), |
| 203 | 213 | tls->download_job.error_code(), | |
| 204 | download::Code2Ascii(tls->download_job.error_code())); | ||
| 205 |
1/2✓ Branch 1 taken 213 times.
✗ Branch 2 not taken.
|
213 | cache_mgr_->AbortTxn(txn); |
| 206 |
1/2✓ Branch 1 taken 213 times.
✗ Branch 2 not taken.
|
213 | backoff_throttle_->Throttle(); |
| 207 |
1/2✓ Branch 1 taken 213 times.
✗ Branch 2 not taken.
|
213 | SignalWaitingThreads(-EIO, object.id, tls); |
| 208 | 213 | return -EIO; | |
| 209 | 1195 | } | |
| 210 | |||
| 211 | |||
| 212 | 1968 | Fetcher::Fetcher(CacheManager *cache_mgr, | |
| 213 | download::DownloadManager *download_mgr, | ||
| 214 | BackoffThrottle *backoff_throttle, | ||
| 215 | 1968 | perf::StatisticsTemplate statistics) | |
| 216 | 1968 | : lock_queues_download_(NULL) | |
| 217 | 1968 | , lock_tls_blocks_(NULL) | |
| 218 | 1968 | , cache_mgr_(cache_mgr) | |
| 219 | 1968 | , download_mgr_(download_mgr) | |
| 220 | 1968 | , backoff_throttle_(backoff_throttle) { | |
| 221 | int retval; | ||
| 222 | 1968 | retval = pthread_key_create(&thread_local_storage_, TLSDestructor); | |
| 223 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1968 times.
|
1968 | assert(retval == 0); |
| 224 | 1968 | lock_queues_download_ = reinterpret_cast<pthread_mutex_t *>( | |
| 225 | 1968 | smalloc(sizeof(pthread_mutex_t))); | |
| 226 | 1968 | retval = pthread_mutex_init(lock_queues_download_, NULL); | |
| 227 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1968 times.
|
1968 | assert(retval == 0); |
| 228 | 1968 | lock_tls_blocks_ = reinterpret_cast<pthread_mutex_t *>( | |
| 229 | 1968 | smalloc(sizeof(pthread_mutex_t))); | |
| 230 | 1968 | retval = pthread_mutex_init(lock_tls_blocks_, NULL); | |
| 231 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1968 times.
|
1968 | assert(retval == 0); |
| 232 |
3/6✓ Branch 2 taken 1968 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 1968 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1968 times.
✗ Branch 10 not taken.
|
1968 | n_downloads = statistics.RegisterTemplated( |
| 233 | "n_downloads", | ||
| 234 | "overall number of downloaded files (incl. catalogs, chunks)"); | ||
| 235 |
3/6✓ Branch 2 taken 1968 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 1968 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1968 times.
✗ Branch 10 not taken.
|
1968 | n_invocations = statistics.RegisterTemplated( |
| 236 | "n_invocations", | ||
| 237 | "overall number of object requests (incl. catalogs, chunks)"); | ||
| 238 | 1968 | } | |
| 239 | |||
| 240 | |||
| 241 | 1968 | Fetcher::~Fetcher() { | |
| 242 | int retval; | ||
| 243 | |||
| 244 | { | ||
| 245 | 1968 | const MutexLockGuard m(lock_tls_blocks_); | |
| 246 |
2/2✓ Branch 1 taken 807 times.
✓ Branch 2 taken 1968 times.
|
2775 | for (unsigned i = 0; i < tls_blocks_.size(); ++i) |
| 247 | 807 | CleanupTls(tls_blocks_[i]); | |
| 248 | 1968 | } | |
| 249 | |||
| 250 | 1968 | retval = pthread_mutex_destroy(lock_tls_blocks_); | |
| 251 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1968 times.
|
1968 | assert(retval == 0); |
| 252 | 1968 | free(lock_tls_blocks_); | |
| 253 | |||
| 254 | 1968 | retval = pthread_mutex_destroy(lock_queues_download_); | |
| 255 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1968 times.
|
1968 | assert(retval == 0); |
| 256 | 1968 | free(lock_queues_download_); | |
| 257 | |||
| 258 | 1968 | retval = pthread_key_delete(thread_local_storage_); | |
| 259 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1968 times.
|
1968 | assert(retval == 0); |
| 260 | 1968 | } | |
| 261 | |||
| 262 | |||
| 263 | /** | ||
| 264 | * Depending on the object type, uses either Open() or OpenPinned() from the | ||
| 265 | * cache manager | ||
| 266 | */ | ||
| 267 | 3000 | int Fetcher::OpenSelect(const CacheManager::LabeledObject &object) { | |
| 268 |
5/6✓ Branch 1 taken 1143 times.
✓ Branch 2 taken 1857 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1143 times.
✓ Branch 6 taken 1857 times.
✓ Branch 7 taken 1143 times.
|
3000 | if (object.label.IsCatalog() || object.label.IsPinned()) { |
| 269 | 1857 | return cache_mgr_->OpenPinned(object); | |
| 270 | } else { | ||
| 271 | 1143 | return cache_mgr_->Open(object); | |
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | 1333 | void Fetcher::SignalWaitingThreads(const int fd, | |
| 277 | const shash::Any &id, | ||
| 278 | ThreadLocalStorage *tls) { | ||
| 279 | 1333 | const MutexLockGuard m(lock_queues_download_); | |
| 280 |
2/2✓ Branch 1 taken 184 times.
✓ Branch 2 taken 1333 times.
|
1517 | for (unsigned i = 0, s = tls->other_pipes_waiting.size(); i < s; ++i) { |
| 281 |
3/4✓ Branch 0 taken 138 times.
✓ Branch 1 taken 46 times.
✓ Branch 3 taken 138 times.
✗ Branch 4 not taken.
|
184 | int fd_dup = (fd >= 0) ? cache_mgr_->Dup(fd) : fd; |
| 282 |
1/2✓ Branch 2 taken 184 times.
✗ Branch 3 not taken.
|
184 | WritePipe(tls->other_pipes_waiting[i], &fd_dup, sizeof(int)); |
| 283 | } | ||
| 284 | 1333 | tls->other_pipes_waiting.clear(); | |
| 285 |
1/2✓ Branch 1 taken 1333 times.
✗ Branch 2 not taken.
|
1333 | queues_download_.erase(id); |
| 286 | 1333 | } | |
| 287 | |||
| 288 | } // namespace cvmfs | ||
| 289 |