| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/cache_tiered.cc |
| Date: | 2025-11-02 02:35:35 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 82 | 129 | 63.6% |
| Branches: | 52 | 132 | 39.4% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "cache_tiered.h" | ||
| 6 | |||
| 7 | #include <errno.h> | ||
| 8 | |||
| 9 | #include <string> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "quota.h" | ||
| 13 | #include "util/platform.h" | ||
| 14 | #include "util/posix.h" | ||
| 15 | |||
| 16 | |||
| 17 | ✗ | std::string TieredCacheManager::Describe() { | |
| 18 | return "Tiered Cache\n" | ||
| 19 | " - upper layer: " | ||
| 20 | ✗ | + upper_->Describe() + " - lower layer: " + lower_->Describe(); | |
| 21 | } | ||
| 22 | |||
| 23 | |||
| 24 | ✗ | bool TieredCacheManager::DoFreeState(void *data) { | |
| 25 | ✗ | SavedState *state = reinterpret_cast<SavedState *>(data); | |
| 26 | ✗ | upper_->FreeState(-1, state->state_upper); | |
| 27 | ✗ | lower_->FreeState(-1, state->state_lower); | |
| 28 | ✗ | delete state; | |
| 29 | ✗ | return true; | |
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | ✗ | int TieredCacheManager::DoRestoreState(void *data) { | |
| 34 | ✗ | SavedState *state = reinterpret_cast<SavedState *>(data); | |
| 35 | ✗ | const int new_root_fd = upper_->RestoreState(-1, state->state_upper); | |
| 36 | // The lower cache layer does not keep the root catalog open | ||
| 37 | ✗ | const int retval = lower_->RestoreState(-1, state->state_lower); | |
| 38 | ✗ | assert(retval == -1); | |
| 39 | ✗ | return new_root_fd; | |
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | ✗ | void *TieredCacheManager::DoSaveState() { | |
| 44 | ✗ | SavedState *state = new SavedState(); | |
| 45 | ✗ | state->state_upper = upper_->SaveState(-1); | |
| 46 | ✗ | state->state_lower = lower_->SaveState(-1); | |
| 47 | ✗ | return state; | |
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | 168 | int TieredCacheManager::Open(const LabeledObject &object) { | |
| 52 |
1/2✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
|
168 | const int fd = upper_->Open(object); |
| 53 |
3/4✓ Branch 0 taken 140 times.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 140 times.
|
168 | if ((fd >= 0) || (fd != -ENOENT)) { |
| 54 | 28 | return fd; | |
| 55 | } | ||
| 56 | |||
| 57 |
1/2✓ Branch 1 taken 140 times.
✗ Branch 2 not taken.
|
140 | const int fd2 = lower_->Open(object); |
| 58 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 28 times.
|
140 | if (fd2 < 0) { |
| 59 | 112 | return fd; | |
| 60 | } // NOTE: use error code from upper. | ||
| 61 | |||
| 62 | // Lower cache hit; upper cache miss. Copy object into the upper cache. | ||
| 63 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | const int64_t size = lower_->GetSize(fd2); |
| 64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (size < 0) { |
| 65 | ✗ | lower_->Close(fd2); | |
| 66 | ✗ | return fd; | |
| 67 | } | ||
| 68 | |||
| 69 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | void *txn = alloca(upper_->SizeOfTxn()); |
| 70 |
2/4✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
|
28 | if (upper_->StartTxn(object.id, size, txn) < 0) { |
| 71 | ✗ | lower_->Close(fd2); | |
| 72 | ✗ | return fd; | |
| 73 | } | ||
| 74 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | upper_->CtrlTxn(object.label, 0, txn); |
| 75 | |||
| 76 | 28 | std::vector<char> m_buffer; | |
| 77 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | m_buffer.resize(kCopyBufferSize); |
| 78 | 28 | uint64_t remaining = size; | |
| 79 | 28 | uint64_t offset = 0; | |
| 80 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
|
56 | while (remaining > 0) { |
| 81 | 28 | const unsigned nbytes = remaining > kCopyBufferSize ? kCopyBufferSize | |
| 82 | : remaining; | ||
| 83 |
1/2✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
|
28 | int64_t result = lower_->Pread(fd2, &m_buffer[0], nbytes, offset); |
| 84 | // The file we are reading is supposed to be exactly `size` bytes. | ||
| 85 |
2/4✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
|
28 | if ((result < 0) || (result != nbytes)) { |
| 86 | ✗ | lower_->Close(fd2); | |
| 87 | ✗ | upper_->AbortTxn(txn); | |
| 88 | ✗ | return fd; | |
| 89 | } | ||
| 90 |
1/2✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
|
28 | result = upper_->Write(&m_buffer[0], nbytes, txn); |
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (result < 0) { |
| 92 | ✗ | lower_->Close(fd2); | |
| 93 | ✗ | upper_->AbortTxn(txn); | |
| 94 | ✗ | return fd; | |
| 95 | } | ||
| 96 | 28 | offset += nbytes; | |
| 97 | 28 | remaining -= nbytes; | |
| 98 | } | ||
| 99 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | lower_->Close(fd2); |
| 100 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | const int fd_return = upper_->OpenFromTxn(txn); |
| 101 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (fd_return < 0) { |
| 102 | ✗ | upper_->AbortTxn(txn); | |
| 103 | ✗ | return fd; | |
| 104 | } | ||
| 105 |
2/4✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
|
28 | if (upper_->CommitTxn(txn) < 0) { |
| 106 | ✗ | upper_->Close(fd_return); | |
| 107 | ✗ | return fd; | |
| 108 | } | ||
| 109 | 28 | return fd_return; | |
| 110 | 28 | } | |
| 111 | |||
| 112 | |||
| 113 | 84 | int TieredCacheManager::StartTxn(const shash::Any &id, | |
| 114 | uint64_t size, | ||
| 115 | void *txn) { | ||
| 116 | 84 | const int upper_result = upper_->StartTxn(id, size, txn); | |
| 117 |
3/4✓ Branch 0 taken 28 times.
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
|
84 | if (lower_readonly_ || (upper_result < 0)) { |
| 118 | 56 | return upper_result; | |
| 119 | } | ||
| 120 | |||
| 121 | 28 | void *txn2 = static_cast<char *>(txn) + upper_->SizeOfTxn(); | |
| 122 | 28 | const int lower_result = lower_->StartTxn(id, size, txn2); | |
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (lower_result < 0) { |
| 124 | ✗ | upper_->AbortTxn(txn); | |
| 125 | } | ||
| 126 | 28 | return lower_result; | |
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | 272 | CacheManager *TieredCacheManager::Create(CacheManager *upper_cache, | |
| 131 | CacheManager *lower_cache) { | ||
| 132 | TieredCacheManager *cache_mgr = new TieredCacheManager(upper_cache, | ||
| 133 |
1/2✓ Branch 2 taken 272 times.
✗ Branch 3 not taken.
|
272 | lower_cache); |
| 134 |
1/2✓ Branch 0 taken 272 times.
✗ Branch 1 not taken.
|
272 | delete cache_mgr->quota_mgr_; |
| 135 | 272 | cache_mgr->quota_mgr_ = upper_cache->quota_mgr(); | |
| 136 | |||
| 137 | 272 | return cache_mgr; | |
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | 56 | void TieredCacheManager::CtrlTxn(const Label &label, | |
| 142 | const int flags, | ||
| 143 | void *txn) { | ||
| 144 | 56 | upper_->CtrlTxn(label, flags, txn); | |
| 145 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
|
56 | if (!lower_readonly_) { |
| 146 | 28 | void *txn2 = static_cast<char *>(txn) + upper_->SizeOfTxn(); | |
| 147 | 28 | lower_->CtrlTxn(label, flags, txn2); | |
| 148 | } | ||
| 149 | 56 | } | |
| 150 | |||
| 151 | |||
| 152 | 56 | int64_t TieredCacheManager::Write(const void *buf, uint64_t size, void *txn) { | |
| 153 | 56 | const int upper_result = upper_->Write(buf, size, txn); | |
| 154 |
3/4✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
|
56 | if (lower_readonly_ || (upper_result < 0)) { |
| 155 | 28 | return upper_result; | |
| 156 | } | ||
| 157 | |||
| 158 | 28 | void *txn2 = static_cast<char *>(txn) + upper_->SizeOfTxn(); | |
| 159 | 28 | return lower_->Write(buf, size, txn2); | |
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | 28 | int TieredCacheManager::Reset(void *txn) { | |
| 164 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | const int upper_result = upper_->Reset(txn); |
| 165 | |||
| 166 | 28 | int lower_result = upper_result; | |
| 167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (!lower_readonly_) { |
| 168 | ✗ | void *txn2 = static_cast<char *>(txn) + upper_->SizeOfTxn(); | |
| 169 | ✗ | lower_result = lower_->Reset(txn2); | |
| 170 | } | ||
| 171 | |||
| 172 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | return (upper_result < 0) ? upper_result : lower_result; |
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | 28 | int TieredCacheManager::AbortTxn(void *txn) { | |
| 177 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | const int upper_result = upper_->AbortTxn(txn); |
| 178 | |||
| 179 | 28 | int lower_result = upper_result; | |
| 180 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (!lower_readonly_) { |
| 181 | ✗ | void *txn2 = static_cast<char *>(txn) + upper_->SizeOfTxn(); | |
| 182 | ✗ | lower_result = lower_->AbortTxn(txn2); | |
| 183 | } | ||
| 184 | |||
| 185 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | return (upper_result < 0) ? upper_result : lower_result; |
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | 56 | int TieredCacheManager::CommitTxn(void *txn) { | |
| 190 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | const int upper_result = upper_->CommitTxn(txn); |
| 191 | |||
| 192 | 56 | int lower_result = upper_result; | |
| 193 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
|
56 | if (!lower_readonly_) { |
| 194 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | void *txn2 = static_cast<char *>(txn) + upper_->SizeOfTxn(); |
| 195 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | lower_result = lower_->CommitTxn(txn2); |
| 196 | } | ||
| 197 | |||
| 198 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | return (upper_result < 0) ? upper_result : lower_result; |
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | 240 | manifest::Breadcrumb TieredCacheManager::LoadBreadcrumb( | |
| 203 | const std::string &fqrn) { | ||
| 204 | 240 | manifest::Breadcrumb breadcrumb = upper_->LoadBreadcrumb(fqrn); | |
| 205 |
1/2✓ Branch 1 taken 240 times.
✗ Branch 2 not taken.
|
240 | if (!breadcrumb.IsValid()) |
| 206 | 240 | breadcrumb = lower_->LoadBreadcrumb(fqrn); | |
| 207 | 240 | return breadcrumb; | |
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | ✗ | bool TieredCacheManager::StoreBreadcrumb(const manifest::Manifest &manifest) { | |
| 212 | ✗ | const bool upper_success = upper_->StoreBreadcrumb(manifest); | |
| 213 | ✗ | bool lower_success = true; | |
| 214 | ✗ | if (!lower_readonly_) | |
| 215 | ✗ | lower_success = lower_->StoreBreadcrumb(manifest); | |
| 216 | ✗ | return upper_success && lower_success; | |
| 217 | } | ||
| 218 | |||
| 219 | |||
| 220 | ✗ | void TieredCacheManager::Spawn() { | |
| 221 | ✗ | upper_->Spawn(); | |
| 222 | ✗ | lower_->Spawn(); | |
| 223 | } | ||
| 224 | |||
| 225 | |||
| 226 | 1088 | TieredCacheManager::~TieredCacheManager() { | |
| 227 | 544 | quota_mgr_ = NULL; // gets deleted by upper | |
| 228 |
1/2✓ Branch 0 taken 272 times.
✗ Branch 1 not taken.
|
544 | delete upper_; |
| 229 |
1/2✓ Branch 0 taken 272 times.
✗ Branch 1 not taken.
|
544 | delete lower_; |
| 230 | 1088 | } | |
| 231 |