Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 15 | #include "pw_kvs/key_value_store.h" |
| 16 | |
Wyatt Hepler | bab0e20 | 2020-02-04 07:40:08 -0800 | [diff] [blame] | 17 | #include <algorithm> |
Wyatt Hepler | 5a33d8c | 2020-02-06 09:32:58 -0800 | [diff] [blame] | 18 | #include <cinttypes> |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 19 | #include <cstring> |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 20 | #include <type_traits> |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 21 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 22 | #define PW_LOG_USE_ULTRA_SHORT_NAMES 1 |
Wyatt Hepler | bdd8e5a | 2020-02-20 19:27:26 -0800 | [diff] [blame] | 23 | #include "pw_kvs/internal/entry.h" |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 24 | #include "pw_kvs_private/macros.h" |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 25 | #include "pw_log/log.h" |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 26 | |
Wyatt Hepler | 2ad6067 | 2020-01-21 08:00:16 -0800 | [diff] [blame] | 27 | namespace pw::kvs { |
Wyatt Hepler | a00d1ef | 2020-02-14 14:31:26 -0800 | [diff] [blame] | 28 | namespace { |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 29 | |
Wyatt Hepler | acaacf9 | 2020-01-24 10:58:30 -0800 | [diff] [blame] | 30 | using std::byte; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 31 | using std::string_view; |
Wyatt Hepler | acaacf9 | 2020-01-24 10:58:30 -0800 | [diff] [blame] | 32 | |
Wyatt Hepler | a00d1ef | 2020-02-14 14:31:26 -0800 | [diff] [blame] | 33 | constexpr bool InvalidKey(std::string_view key) { |
Wyatt Hepler | bdd8e5a | 2020-02-20 19:27:26 -0800 | [diff] [blame] | 34 | return key.empty() || (key.size() > internal::Entry::kMaxKeyLength); |
Wyatt Hepler | a00d1ef | 2020-02-14 14:31:26 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | } // namespace |
| 38 | |
Wyatt Hepler | ad0a793 | 2020-02-06 08:20:38 -0800 | [diff] [blame] | 39 | KeyValueStore::KeyValueStore(FlashPartition* partition, |
Wyatt Hepler | 38ce30f | 2020-02-19 11:48:31 -0800 | [diff] [blame] | 40 | Vector<KeyDescriptor>& key_descriptor_list, |
| 41 | Vector<SectorDescriptor>& sector_descriptor_list, |
Wyatt Hepler | 88adfe8 | 2020-02-20 19:33:27 -0800 | [diff] [blame] | 42 | const EntryFormat& format, |
Wyatt Hepler | ad0a793 | 2020-02-06 08:20:38 -0800 | [diff] [blame] | 43 | const Options& options) |
| 44 | : partition_(*partition), |
| 45 | entry_header_format_(format), |
Wyatt Hepler | 38ce30f | 2020-02-19 11:48:31 -0800 | [diff] [blame] | 46 | key_descriptors_(key_descriptor_list), |
Wyatt Hepler | d229828 | 2020-02-20 17:12:45 -0800 | [diff] [blame] | 47 | sectors_(sector_descriptor_list), |
| 48 | options_(options) { |
| 49 | Reset(); |
| 50 | } |
Wyatt Hepler | ad0a793 | 2020-02-06 08:20:38 -0800 | [diff] [blame] | 51 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 52 | Status KeyValueStore::Init() { |
Wyatt Hepler | d229828 | 2020-02-20 17:12:45 -0800 | [diff] [blame] | 53 | Reset(); |
| 54 | |
David Rogers | 2e9e0c8 | 2020-02-13 15:06:06 -0800 | [diff] [blame] | 55 | INF("Initializing key value store"); |
Wyatt Hepler | 38ce30f | 2020-02-19 11:48:31 -0800 | [diff] [blame] | 56 | if (partition_.sector_count() > sectors_.max_size()) { |
David Rogers | 2e9e0c8 | 2020-02-13 15:06:06 -0800 | [diff] [blame] | 57 | ERR("KVS init failed: kMaxUsableSectors (=%zu) must be at least as " |
| 58 | "large as the number of sectors in the flash partition (=%zu)", |
Wyatt Hepler | 38ce30f | 2020-02-19 11:48:31 -0800 | [diff] [blame] | 59 | sectors_.max_size(), |
David Rogers | 2e9e0c8 | 2020-02-13 15:06:06 -0800 | [diff] [blame] | 60 | partition_.sector_count()); |
Wyatt Hepler | ad0a793 | 2020-02-06 08:20:38 -0800 | [diff] [blame] | 61 | return Status::FAILED_PRECONDITION; |
| 62 | } |
| 63 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 64 | const size_t sector_size_bytes = partition_.sector_size_bytes(); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 65 | |
David Rogers | f0a3544 | 2020-02-04 12:16:38 -0800 | [diff] [blame] | 66 | if (working_buffer_.size() < sector_size_bytes) { |
Wyatt Hepler | ce7b8df | 2020-02-21 10:38:39 -0800 | [diff] [blame] | 67 | ERR("KVS init failed: working_buffer_ (%zu B) is smaller than sector size " |
| 68 | "(%zu B)", |
David Rogers | f0a3544 | 2020-02-04 12:16:38 -0800 | [diff] [blame] | 69 | working_buffer_.size(), |
| 70 | sector_size_bytes); |
| 71 | return Status::INVALID_ARGUMENT; |
| 72 | } |
| 73 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 74 | DBG("First pass: Read all entries from all sectors"); |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 75 | Address sector_address = 0; |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 76 | |
Wyatt Hepler | d229828 | 2020-02-20 17:12:45 -0800 | [diff] [blame] | 77 | sectors_.assign(partition_.sector_count(), |
| 78 | SectorDescriptor(sector_size_bytes)); |
| 79 | |
Alexei Frolov | d4adf91 | 2020-02-21 13:29:15 -0800 | [diff] [blame^] | 80 | size_t total_corrupt_bytes = 0; |
| 81 | int corrupt_entries = 0; |
| 82 | |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 83 | for (SectorDescriptor& sector : sectors_) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 84 | Address entry_address = sector_address; |
| 85 | |
Alexei Frolov | d4adf91 | 2020-02-21 13:29:15 -0800 | [diff] [blame^] | 86 | size_t sector_corrupt_bytes = 0; |
| 87 | |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 88 | for (int num_entries_in_sector = 0; true; num_entries_in_sector++) { |
| 89 | DBG("Load entry: sector=%" PRIx32 ", entry#=%d, address=%" PRIx32, |
| 90 | sector_address, |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 91 | num_entries_in_sector, |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 92 | entry_address); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 93 | |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 94 | if (!AddressInSector(sector, entry_address)) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 95 | DBG("Fell off end of sector; moving to the next sector"); |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | Address next_entry_address; |
| 100 | Status status = LoadEntry(entry_address, &next_entry_address); |
| 101 | if (status == Status::NOT_FOUND) { |
| 102 | DBG("Hit un-written data in sector; moving to the next sector"); |
| 103 | break; |
| 104 | } |
| 105 | if (status == Status::DATA_LOSS) { |
Alexei Frolov | d4adf91 | 2020-02-21 13:29:15 -0800 | [diff] [blame^] | 106 | // The entry could not be read, indicating data corruption within the |
| 107 | // sector. Try to scan the remainder of the sector for other entries. |
| 108 | ERR("KVS init: data loss detected in sector %u at address %zu", |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 109 | SectorIndex(§or), |
| 110 | size_t(entry_address)); |
Alexei Frolov | d4adf91 | 2020-02-21 13:29:15 -0800 | [diff] [blame^] | 111 | |
| 112 | corrupt_entries++; |
| 113 | |
| 114 | status = ScanForEntry(sector, |
| 115 | entry_address + Entry::kMinAlignmentBytes, |
| 116 | &next_entry_address); |
| 117 | if (status == Status::NOT_FOUND) { |
| 118 | // No further entries in this sector. Mark the remaining bytes in the |
| 119 | // sector as corrupt (since we can't reliably know the size of the |
| 120 | // corrupt entry). |
| 121 | sector_corrupt_bytes += |
| 122 | sector_size_bytes - (entry_address - sector_address); |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | if (!status.ok()) { |
| 127 | ERR("Unexpected error in KVS initialization: %s", status.str()); |
| 128 | return Status::UNKNOWN; |
| 129 | } |
| 130 | |
| 131 | sector_corrupt_bytes += next_entry_address - entry_address; |
| 132 | } else if (!status.ok()) { |
| 133 | ERR("Unexpected error in KVS initialization: %s", status.str()); |
| 134 | return Status::UNKNOWN; |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 135 | } |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 136 | |
| 137 | // Entry loaded successfully; so get ready to load the next one. |
| 138 | entry_address = next_entry_address; |
| 139 | |
| 140 | // Update of the number of writable bytes in this sector. |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 141 | sector.set_writable_bytes(sector_size_bytes - |
| 142 | (entry_address - sector_address)); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 143 | } |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 144 | |
Alexei Frolov | d4adf91 | 2020-02-21 13:29:15 -0800 | [diff] [blame^] | 145 | if (sector_corrupt_bytes > 0) { |
| 146 | // If the sector contains corrupt data, prevent any further entries from |
| 147 | // being written to it by indicating that it has no space. This should |
| 148 | // also make it a decent GC candidate. Valid keys in the sector are still |
| 149 | // readable as normal. |
| 150 | sector.set_writable_bytes(0); |
| 151 | |
| 152 | WRN("Sector %u contains %zuB of corrupt data", |
| 153 | SectorIndex(§or), |
| 154 | sector_corrupt_bytes); |
| 155 | } |
| 156 | |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 157 | sector_address += sector_size_bytes; |
Alexei Frolov | d4adf91 | 2020-02-21 13:29:15 -0800 | [diff] [blame^] | 158 | total_corrupt_bytes += sector_corrupt_bytes; |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | DBG("Second pass: Count valid bytes in each sector"); |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 162 | const KeyDescriptor* newest_key = nullptr; |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 163 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 164 | // For every valid key, increment the valid bytes for that sector. |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 165 | for (KeyDescriptor& key_descriptor : key_descriptors_) { |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 166 | Entry entry; |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 167 | TRY(Entry::Read(partition_, key_descriptor.address(), &entry)); |
| 168 | SectorFromKey(key_descriptor)->AddValidBytes(entry.size()); |
| 169 | |
| 170 | if (key_descriptor.IsNewerThan(last_transaction_id_)) { |
| 171 | last_transaction_id_ = key_descriptor.transaction_id(); |
| 172 | newest_key = &key_descriptor; |
| 173 | } |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 174 | } |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 175 | |
| 176 | if (newest_key == nullptr) { |
| 177 | last_new_sector_ = sectors_.begin(); |
| 178 | } else { |
| 179 | last_new_sector_ = SectorFromKey(newest_key); |
| 180 | } |
| 181 | |
Wyatt Hepler | 729f28c | 2020-02-05 09:46:00 -0800 | [diff] [blame] | 182 | initialized_ = true; |
David Rogers | 2e9e0c8 | 2020-02-13 15:06:06 -0800 | [diff] [blame] | 183 | |
Armando Montanez | 5464d5f | 2020-02-20 10:12:20 -0800 | [diff] [blame] | 184 | INF("KeyValueStore init complete: active keys %zu, deleted keys %zu, sectors " |
David Rogers | 2e9e0c8 | 2020-02-13 15:06:06 -0800 | [diff] [blame] | 185 | "%zu, logical sector size %zu bytes", |
| 186 | size(), |
| 187 | (key_descriptors_.size() - size()), |
| 188 | sectors_.size(), |
| 189 | partition_.sector_size_bytes()); |
| 190 | |
Alexei Frolov | d4adf91 | 2020-02-21 13:29:15 -0800 | [diff] [blame^] | 191 | if (total_corrupt_bytes > 0) { |
| 192 | WRN("Found %zu corrupt bytes and %d corrupt entries during init process; " |
| 193 | "some keys may be missing", |
| 194 | total_corrupt_bytes, |
| 195 | corrupt_entries); |
| 196 | return Status::DATA_LOSS; |
| 197 | } |
| 198 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 199 | return Status::OK; |
| 200 | } |
| 201 | |
| 202 | Status KeyValueStore::LoadEntry(Address entry_address, |
| 203 | Address* next_entry_address) { |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 204 | Entry entry; |
| 205 | TRY(Entry::Read(partition_, entry_address, &entry)); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 206 | |
| 207 | // TODO: Handle multiple magics for formats that have changed. |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 208 | if (entry.magic() != entry_header_format_.magic) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 209 | // TODO: It may be cleaner to have some logging helpers for these cases. |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 210 | ERR("Found corrupt magic: %zx; expecting %zx; at address %zx", |
| 211 | size_t(entry.magic()), |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 212 | size_t(entry_header_format_.magic), |
| 213 | size_t(entry_address)); |
| 214 | return Status::DATA_LOSS; |
| 215 | } |
| 216 | |
| 217 | // Read the key from flash & validate the entry (which reads the value). |
Wyatt Hepler | a00d1ef | 2020-02-14 14:31:26 -0800 | [diff] [blame] | 218 | Entry::KeyBuffer key_buffer; |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 219 | TRY_ASSIGN(size_t key_length, entry.ReadKey(key_buffer)); |
| 220 | const string_view key(key_buffer.data(), key_length); |
Wyatt Hepler | bab0e20 | 2020-02-04 07:40:08 -0800 | [diff] [blame] | 221 | |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 222 | TRY(entry.VerifyChecksumInFlash(entry_header_format_.checksum)); |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 223 | TRY(AppendNewOrOverwriteStaleExistingDescriptor(entry.descriptor(key))); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 224 | |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 225 | *next_entry_address = entry.next_address(); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 226 | return Status::OK; |
| 227 | } |
| 228 | |
Alexei Frolov | d4adf91 | 2020-02-21 13:29:15 -0800 | [diff] [blame^] | 229 | // Scans flash memory within a sector to find a KVS entry magic. |
| 230 | // TODO(frolv): This needs to be unit tested! |
| 231 | Status KeyValueStore::ScanForEntry(const SectorDescriptor& sector, |
| 232 | Address start_address, |
| 233 | Address* next_entry_address) { |
| 234 | DBG("Scanning sector %u for entries starting from address %zx", |
| 235 | SectorIndex(§or), |
| 236 | size_t(start_address)); |
| 237 | |
| 238 | // Entries must start at addresses which are aligned on a multiple of |
| 239 | // Entry::kMinAlignmentBytes. However, that multiple can vary between entries. |
| 240 | // When scanning, we don't have an entry to tell us what the current alignment |
| 241 | // is, so the minimum alignment is used to be exhaustive. |
| 242 | for (Address address = AlignUp(start_address, Entry::kMinAlignmentBytes); |
| 243 | AddressInSector(sector, address); |
| 244 | address += Entry::kMinAlignmentBytes) { |
| 245 | // TODO: Handle multiple magics for formats that have changed. |
| 246 | uint32_t magic; |
| 247 | TRY(partition_.Read(address, as_writable_bytes(span(&magic, 1)))); |
| 248 | if (magic == entry_header_format_.magic) { |
| 249 | DBG("Found entry magic at address %zx", size_t(address)); |
| 250 | *next_entry_address = address; |
| 251 | return Status::OK; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return Status::NOT_FOUND; |
| 256 | } |
| 257 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 258 | // TODO: This method is the trigger of the O(valid_entries * all_entries) time |
| 259 | // complexity for reading. At some cost to memory, this could be optimized by |
| 260 | // using a hash table instead of scanning, but in practice this should be fine |
| 261 | // for a small number of keys |
| 262 | Status KeyValueStore::AppendNewOrOverwriteStaleExistingDescriptor( |
| 263 | const KeyDescriptor& key_descriptor) { |
| 264 | // With the new key descriptor, either add it to the descriptor table or |
| 265 | // overwrite an existing entry with an older version of the key. |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 266 | KeyDescriptor* existing_descriptor = FindDescriptor(key_descriptor.hash()); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 267 | |
Wyatt Hepler | 5406a67 | 2020-02-18 15:42:38 -0800 | [diff] [blame] | 268 | // Write a new entry. |
| 269 | if (existing_descriptor == nullptr) { |
| 270 | if (key_descriptors_.full()) { |
| 271 | return Status::RESOURCE_EXHAUSTED; |
| 272 | } |
| 273 | key_descriptors_.push_back(key_descriptor); |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 274 | } else if (key_descriptor.IsNewerThan( |
| 275 | existing_descriptor->transaction_id())) { |
Wyatt Hepler | 5406a67 | 2020-02-18 15:42:38 -0800 | [diff] [blame] | 276 | // Existing entry is old; replace the existing entry with the new one. |
| 277 | *existing_descriptor = key_descriptor; |
| 278 | } else { |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 279 | // Otherwise, check if the entries have a duplicate transaction ID, which is |
| 280 | // not valid. |
| 281 | if (existing_descriptor->transaction_id() == |
| 282 | key_descriptor.transaction_id()) { |
| 283 | ERR("Data loss: Duplicated old(=%zu) and new(=%zu) transaction ID", |
| 284 | size_t(existing_descriptor->transaction_id()), |
| 285 | size_t(key_descriptor.transaction_id())); |
Wyatt Hepler | 5406a67 | 2020-02-18 15:42:38 -0800 | [diff] [blame] | 286 | return Status::DATA_LOSS; |
| 287 | } |
| 288 | DBG("Found stale entry when appending; ignoring"); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 289 | } |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 290 | return Status::OK; |
| 291 | } |
| 292 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 293 | KeyValueStore::KeyDescriptor* KeyValueStore::FindDescriptor(uint32_t hash) { |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 294 | for (KeyDescriptor& key_descriptor : key_descriptors_) { |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 295 | if (key_descriptor.hash() == hash) { |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 296 | return &key_descriptor; |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | return nullptr; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | StatusWithSize KeyValueStore::Get(string_view key, |
Wyatt Hepler | 5f6efc0 | 2020-02-18 16:54:31 -0800 | [diff] [blame] | 303 | span<byte> value_buffer, |
| 304 | size_t offset_bytes) const { |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 305 | TRY_WITH_SIZE(CheckOperation(key)); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 306 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 307 | const KeyDescriptor* key_descriptor; |
Wyatt Hepler | 2d40169 | 2020-02-13 16:01:23 -0800 | [diff] [blame] | 308 | TRY_WITH_SIZE(FindExistingKeyDescriptor(key, &key_descriptor)); |
Wyatt Hepler | 6c24c06 | 2020-02-05 15:30:49 -0800 | [diff] [blame] | 309 | |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 310 | Entry entry; |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 311 | TRY_WITH_SIZE(Entry::Read(partition_, key_descriptor->address(), &entry)); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 312 | |
Wyatt Hepler | 5f6efc0 | 2020-02-18 16:54:31 -0800 | [diff] [blame] | 313 | StatusWithSize result = entry.ReadValue(value_buffer, offset_bytes); |
| 314 | if (result.ok() && options_.verify_on_read && offset_bytes == 0u) { |
Wyatt Hepler | ce7b8df | 2020-02-21 10:38:39 -0800 | [diff] [blame] | 315 | Status verify_result = entry.VerifyChecksum( |
| 316 | entry_header_format_.checksum, key, value_buffer.first(result.size())); |
David Rogers | cf680ab | 2020-02-12 23:28:32 -0800 | [diff] [blame] | 317 | if (!verify_result.ok()) { |
Wyatt Hepler | ce7b8df | 2020-02-21 10:38:39 -0800 | [diff] [blame] | 318 | std::memset(value_buffer.data(), 0, result.size()); |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 319 | return StatusWithSize(verify_result); |
David Rogers | cf680ab | 2020-02-12 23:28:32 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | return StatusWithSize(verify_result, result.size()); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 323 | } |
| 324 | return result; |
| 325 | } |
| 326 | |
| 327 | Status KeyValueStore::Put(string_view key, span<const byte> value) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 328 | DBG("Writing key/value; key length=%zu, value length=%zu", |
| 329 | key.size(), |
| 330 | value.size()); |
Wyatt Hepler | 729f28c | 2020-02-05 09:46:00 -0800 | [diff] [blame] | 331 | |
| 332 | TRY(CheckOperation(key)); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 333 | |
Wyatt Hepler | 5406a67 | 2020-02-18 15:42:38 -0800 | [diff] [blame] | 334 | if (Entry::size(partition_, key, value) > partition_.sector_size_bytes()) { |
| 335 | DBG("%zu B value with %zu B key cannot fit in one sector", |
| 336 | value.size(), |
| 337 | key.size()); |
| 338 | return Status::INVALID_ARGUMENT; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 339 | } |
| 340 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 341 | KeyDescriptor* key_descriptor; |
Wyatt Hepler | 2d40169 | 2020-02-13 16:01:23 -0800 | [diff] [blame] | 342 | Status status = FindKeyDescriptor(key, &key_descriptor); |
| 343 | |
| 344 | if (status.ok()) { |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 345 | DBG("Overwriting entry for key %#08" PRIx32 " in sector %u", |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 346 | key_descriptor->hash(), |
| 347 | SectorIndex(SectorFromKey(key_descriptor))); |
Wyatt Hepler | 5a33d8c | 2020-02-06 09:32:58 -0800 | [diff] [blame] | 348 | return WriteEntryForExistingKey( |
| 349 | key_descriptor, KeyDescriptor::kValid, key, value); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 350 | } |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 351 | |
Wyatt Hepler | 2d40169 | 2020-02-13 16:01:23 -0800 | [diff] [blame] | 352 | if (status == Status::NOT_FOUND) { |
| 353 | return WriteEntryForNewKey(key, value); |
| 354 | } |
| 355 | |
| 356 | return status; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | Status KeyValueStore::Delete(string_view key) { |
Wyatt Hepler | 729f28c | 2020-02-05 09:46:00 -0800 | [diff] [blame] | 360 | TRY(CheckOperation(key)); |
| 361 | |
Wyatt Hepler | 6c24c06 | 2020-02-05 15:30:49 -0800 | [diff] [blame] | 362 | KeyDescriptor* key_descriptor; |
Wyatt Hepler | 2d40169 | 2020-02-13 16:01:23 -0800 | [diff] [blame] | 363 | TRY(FindExistingKeyDescriptor(key, &key_descriptor)); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 364 | |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 365 | DBG("Writing tombstone for key %#08" PRIx32 " in sector %u", |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 366 | key_descriptor->hash(), |
| 367 | SectorIndex(SectorFromKey(key_descriptor))); |
Wyatt Hepler | 5a33d8c | 2020-02-06 09:32:58 -0800 | [diff] [blame] | 368 | return WriteEntryForExistingKey( |
| 369 | key_descriptor, KeyDescriptor::kDeleted, key, {}); |
Wyatt Hepler | 6c24c06 | 2020-02-05 15:30:49 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | KeyValueStore::iterator& KeyValueStore::iterator::operator++() { |
| 373 | // Skip to the next entry that is valid (not deleted). |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 374 | while (++index_ < item_.kvs_.key_descriptors_.size() && |
Wyatt Hepler | 6c24c06 | 2020-02-05 15:30:49 -0800 | [diff] [blame] | 375 | descriptor().deleted()) { |
| 376 | } |
| 377 | return *this; |
| 378 | } |
| 379 | |
| 380 | const KeyValueStore::Item& KeyValueStore::iterator::operator*() { |
| 381 | std::memset(item_.key_buffer_.data(), 0, item_.key_buffer_.size()); |
| 382 | |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 383 | Entry entry; |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 384 | if (Entry::Read(item_.kvs_.partition_, descriptor().address(), &entry).ok()) { |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 385 | entry.ReadKey(item_.key_buffer_); |
Wyatt Hepler | 6c24c06 | 2020-02-05 15:30:49 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | return item_; |
| 389 | } |
| 390 | |
| 391 | KeyValueStore::iterator KeyValueStore::begin() const { |
| 392 | size_t i = 0; |
| 393 | // Skip over any deleted entries at the start of the descriptor list. |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 394 | while (i < key_descriptors_.size() && key_descriptors_[i].deleted()) { |
Wyatt Hepler | 6c24c06 | 2020-02-05 15:30:49 -0800 | [diff] [blame] | 395 | i += 1; |
| 396 | } |
| 397 | return iterator(*this, i); |
| 398 | } |
| 399 | |
| 400 | // TODO(hepler): The valid entry count could be tracked in the KVS to avoid the |
| 401 | // need for this for-loop. |
| 402 | size_t KeyValueStore::size() const { |
| 403 | size_t valid_entries = 0; |
| 404 | |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 405 | for (const KeyDescriptor& key_descriptor : key_descriptors_) { |
| 406 | if (!key_descriptor.deleted()) { |
Wyatt Hepler | 6c24c06 | 2020-02-05 15:30:49 -0800 | [diff] [blame] | 407 | valid_entries += 1; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | return valid_entries; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Wyatt Hepler | ed163b0 | 2020-02-03 17:49:32 -0800 | [diff] [blame] | 414 | StatusWithSize KeyValueStore::ValueSize(std::string_view key) const { |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 415 | TRY_WITH_SIZE(CheckOperation(key)); |
Wyatt Hepler | ed163b0 | 2020-02-03 17:49:32 -0800 | [diff] [blame] | 416 | |
| 417 | const KeyDescriptor* key_descriptor; |
Wyatt Hepler | 2d40169 | 2020-02-13 16:01:23 -0800 | [diff] [blame] | 418 | TRY_WITH_SIZE(FindExistingKeyDescriptor(key, &key_descriptor)); |
Wyatt Hepler | 6c24c06 | 2020-02-05 15:30:49 -0800 | [diff] [blame] | 419 | |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 420 | Entry entry; |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 421 | TRY_WITH_SIZE(Entry::Read(partition_, key_descriptor->address(), &entry)); |
Wyatt Hepler | ed163b0 | 2020-02-03 17:49:32 -0800 | [diff] [blame] | 422 | |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 423 | return StatusWithSize(entry.value_size()); |
Wyatt Hepler | ed163b0 | 2020-02-03 17:49:32 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Wyatt Hepler | 6e3a83b | 2020-02-04 07:36:45 -0800 | [diff] [blame] | 426 | Status KeyValueStore::FixedSizeGet(std::string_view key, |
| 427 | byte* value, |
| 428 | size_t size_bytes) const { |
| 429 | // Ensure that the size of the stored value matches the size of the type. |
| 430 | // Otherwise, report error. This check avoids potential memory corruption. |
| 431 | StatusWithSize result = ValueSize(key); |
| 432 | if (!result.ok()) { |
| 433 | return result.status(); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 434 | } |
Wyatt Hepler | 6e3a83b | 2020-02-04 07:36:45 -0800 | [diff] [blame] | 435 | if (result.size() != size_bytes) { |
Wyatt Hepler | 6c24c06 | 2020-02-05 15:30:49 -0800 | [diff] [blame] | 436 | DBG("Requested %zu B read, but value is %zu B", size_bytes, result.size()); |
Wyatt Hepler | 6e3a83b | 2020-02-04 07:36:45 -0800 | [diff] [blame] | 437 | return Status::INVALID_ARGUMENT; |
Wyatt Hepler | bab0e20 | 2020-02-04 07:40:08 -0800 | [diff] [blame] | 438 | } |
Wyatt Hepler | 6e3a83b | 2020-02-04 07:36:45 -0800 | [diff] [blame] | 439 | return Get(key, span(value, size_bytes)).status(); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Wyatt Hepler | 729f28c | 2020-02-05 09:46:00 -0800 | [diff] [blame] | 442 | Status KeyValueStore::CheckOperation(string_view key) const { |
Wyatt Hepler | acaacf9 | 2020-01-24 10:58:30 -0800 | [diff] [blame] | 443 | if (InvalidKey(key)) { |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 444 | return Status::INVALID_ARGUMENT; |
| 445 | } |
Wyatt Hepler | d229828 | 2020-02-20 17:12:45 -0800 | [diff] [blame] | 446 | if (!initialized()) { |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 447 | return Status::FAILED_PRECONDITION; |
| 448 | } |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 449 | return Status::OK; |
| 450 | } |
| 451 | |
Wyatt Hepler | 2d40169 | 2020-02-13 16:01:23 -0800 | [diff] [blame] | 452 | // Searches for a KeyDescriptor that matches this key and sets *result to point |
| 453 | // to it if one is found. |
| 454 | // |
| 455 | // OK: there is a matching descriptor and *result is set |
| 456 | // NOT_FOUND: there is no descriptor that matches this key, but this key |
| 457 | // has a unique hash (and could potentially be added to the KVS) |
| 458 | // ALREADY_EXISTS: there is no descriptor that matches this key, but the |
| 459 | // key's hash collides with the hash for an existing descriptor |
| 460 | // |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 461 | Status KeyValueStore::FindKeyDescriptor(string_view key, |
| 462 | const KeyDescriptor** result) const { |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 463 | const uint32_t hash = internal::Hash(key); |
Wyatt Hepler | a00d1ef | 2020-02-14 14:31:26 -0800 | [diff] [blame] | 464 | Entry::KeyBuffer key_buffer; |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 465 | |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 466 | for (auto& descriptor : key_descriptors_) { |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 467 | if (descriptor.hash() == hash) { |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 468 | TRY(Entry::ReadKey( |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 469 | partition_, descriptor.address(), key.size(), key_buffer.data())); |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 470 | |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 471 | if (key == string_view(key_buffer.data(), key.size())) { |
Wyatt Hepler | 5a33d8c | 2020-02-06 09:32:58 -0800 | [diff] [blame] | 472 | DBG("Found match for key hash 0x%08" PRIx32, hash); |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 473 | *result = &descriptor; |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 474 | return Status::OK; |
Wyatt Hepler | 2d40169 | 2020-02-13 16:01:23 -0800 | [diff] [blame] | 475 | } else { |
| 476 | WRN("Found key hash collision for 0x%08" PRIx32, hash); |
| 477 | return Status::ALREADY_EXISTS; |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 478 | } |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 479 | } |
| 480 | } |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 481 | return Status::NOT_FOUND; |
| 482 | } |
| 483 | |
Wyatt Hepler | 2d40169 | 2020-02-13 16:01:23 -0800 | [diff] [blame] | 484 | // Searches for a KeyDescriptor that matches this key and sets *result to point |
| 485 | // to it if one is found. |
| 486 | // |
| 487 | // OK: there is a matching descriptor and *result is set |
| 488 | // NOT_FOUND: there is no descriptor that matches this key |
| 489 | // |
| 490 | Status KeyValueStore::FindExistingKeyDescriptor( |
| 491 | string_view key, const KeyDescriptor** result) const { |
| 492 | Status status = FindKeyDescriptor(key, result); |
| 493 | |
| 494 | // If the key's hash collides with an existing key or if the key is deleted, |
| 495 | // treat it as if it is not in the KVS. |
| 496 | if (status == Status::ALREADY_EXISTS || |
| 497 | (status.ok() && (*result)->deleted())) { |
| 498 | return Status::NOT_FOUND; |
| 499 | } |
| 500 | return status; |
| 501 | } |
| 502 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 503 | Status KeyValueStore::WriteEntryForExistingKey(KeyDescriptor* key_descriptor, |
Wyatt Hepler | 5a33d8c | 2020-02-06 09:32:58 -0800 | [diff] [blame] | 504 | KeyDescriptor::State new_state, |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 505 | string_view key, |
| 506 | span<const byte> value) { |
Wyatt Hepler | 5a33d8c | 2020-02-06 09:32:58 -0800 | [diff] [blame] | 507 | // Find the original entry and sector to update the sector's valid_bytes. |
Wyatt Hepler | 30a5215 | 2020-02-12 11:26:05 -0800 | [diff] [blame] | 508 | Entry original_entry; |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 509 | TRY(Entry::Read(partition_, key_descriptor->address(), &original_entry)); |
| 510 | SectorDescriptor* old_sector = SectorFromKey(key_descriptor); |
Wyatt Hepler | 6c24c06 | 2020-02-05 15:30:49 -0800 | [diff] [blame] | 511 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 512 | SectorDescriptor* sector; |
Wyatt Hepler | 5406a67 | 2020-02-18 15:42:38 -0800 | [diff] [blame] | 513 | TRY(FindOrRecoverSectorWithSpace(§or, |
| 514 | Entry::size(partition_, key, value))); |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 515 | DBG("Writing existing entry; found sector %u (%#" PRIx32 ")", |
| 516 | SectorIndex(sector), |
| 517 | SectorBaseAddress(sector)); |
David Rogers | 3464d0a | 2020-02-07 11:45:46 -0800 | [diff] [blame] | 518 | |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 519 | if (old_sector != SectorFromKey(key_descriptor)) { |
David Rogers | 3464d0a | 2020-02-07 11:45:46 -0800 | [diff] [blame] | 520 | DBG("Sector for old entry (size %zu) was garbage collected. Old entry " |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 521 | "relocated to sector %u", |
David Rogers | 3464d0a | 2020-02-07 11:45:46 -0800 | [diff] [blame] | 522 | original_entry.size(), |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 523 | SectorIndex(SectorFromKey(key_descriptor))); |
David Rogers | 3464d0a | 2020-02-07 11:45:46 -0800 | [diff] [blame] | 524 | |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 525 | old_sector = SectorFromKey(key_descriptor); |
David Rogers | 3464d0a | 2020-02-07 11:45:46 -0800 | [diff] [blame] | 526 | } |
| 527 | |
Wyatt Hepler | 5a33d8c | 2020-02-06 09:32:58 -0800 | [diff] [blame] | 528 | TRY(AppendEntry(sector, key_descriptor, key, value, new_state)); |
| 529 | |
David Rogers | 3464d0a | 2020-02-07 11:45:46 -0800 | [diff] [blame] | 530 | old_sector->RemoveValidBytes(original_entry.size()); |
Wyatt Hepler | 5a33d8c | 2020-02-06 09:32:58 -0800 | [diff] [blame] | 531 | return Status::OK; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | Status KeyValueStore::WriteEntryForNewKey(string_view key, |
| 535 | span<const byte> value) { |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 536 | if (key_descriptors_.full()) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 537 | WRN("KVS full: trying to store a new entry, but can't. Have %zu entries", |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 538 | key_descriptors_.size()); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 539 | return Status::RESOURCE_EXHAUSTED; |
| 540 | } |
| 541 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 542 | SectorDescriptor* sector; |
Wyatt Hepler | 5406a67 | 2020-02-18 15:42:38 -0800 | [diff] [blame] | 543 | TRY(FindOrRecoverSectorWithSpace(§or, |
| 544 | Entry::size(partition_, key, value))); |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 545 | DBG("Writing new entry; found sector: %u", SectorIndex(sector)); |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 546 | |
| 547 | // Create the KeyDescriptor that will be added to the list. The transaction ID |
| 548 | // and address will be set by AppendEntry. |
| 549 | KeyDescriptor key_descriptor(key); |
Wyatt Hepler | 0af6ad9 | 2020-02-13 15:54:46 -0800 | [diff] [blame] | 550 | TRY(AppendEntry(sector, &key_descriptor, key, value, KeyDescriptor::kValid)); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 551 | |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 552 | // Only add the entry when we are certain the write succeeded. |
| 553 | key_descriptors_.push_back(key_descriptor); |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 554 | return Status::OK; |
| 555 | } |
| 556 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 557 | Status KeyValueStore::RelocateEntry(KeyDescriptor& key_descriptor) { |
David Rogers | f0a3544 | 2020-02-04 12:16:38 -0800 | [diff] [blame] | 558 | struct TempEntry { |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 559 | Entry::KeyBuffer key; |
Wyatt Hepler | ce7b8df | 2020-02-21 10:38:39 -0800 | [diff] [blame] | 560 | std::array<byte, sizeof(working_buffer_) - sizeof(key)> value; |
David Rogers | f0a3544 | 2020-02-04 12:16:38 -0800 | [diff] [blame] | 561 | }; |
Wyatt Hepler | ce7b8df | 2020-02-21 10:38:39 -0800 | [diff] [blame] | 562 | auto [key_buffer, value_buffer] = |
| 563 | *std::launder(reinterpret_cast<TempEntry*>(working_buffer_.data())); |
David Rogers | f0a3544 | 2020-02-04 12:16:38 -0800 | [diff] [blame] | 564 | |
Wyatt Hepler | ce7b8df | 2020-02-21 10:38:39 -0800 | [diff] [blame] | 565 | DBG("Relocating entry at %zx for key %" PRIx32, |
| 566 | size_t(key_descriptor.address()), |
| 567 | key_descriptor.hash()); |
David Rogers | df025cd | 2020-02-06 17:05:34 -0800 | [diff] [blame] | 568 | |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 569 | // Read the entry to be relocated. Store the entry in a local variable and |
David Rogers | f0a3544 | 2020-02-04 12:16:38 -0800 | [diff] [blame] | 570 | // store the key and value in the TempEntry stored in the static allocated |
| 571 | // working_buffer_. |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 572 | Entry entry; |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 573 | TRY(Entry::Read(partition_, key_descriptor.address(), &entry)); |
Wyatt Hepler | ce7b8df | 2020-02-21 10:38:39 -0800 | [diff] [blame] | 574 | |
| 575 | TRY_ASSIGN(size_t key_length, entry.ReadKey(key_buffer)); |
| 576 | string_view key = string_view(key_buffer.data(), key_length); |
| 577 | |
| 578 | StatusWithSize result = entry.ReadValue(value_buffer); |
| 579 | if (!result.ok()) { |
David Rogers | f0a3544 | 2020-02-04 12:16:38 -0800 | [diff] [blame] | 580 | return Status::INTERNAL; |
| 581 | } |
| 582 | |
Wyatt Hepler | ce7b8df | 2020-02-21 10:38:39 -0800 | [diff] [blame] | 583 | const span value = span(value_buffer.data(), result.size()); |
| 584 | TRY(entry.VerifyChecksum(entry_header_format_.checksum, key, value)); |
David Rogers | f0a3544 | 2020-02-04 12:16:38 -0800 | [diff] [blame] | 585 | |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 586 | SectorDescriptor* old_sector = SectorFromKey(key_descriptor); |
David Rogers | f0a3544 | 2020-02-04 12:16:38 -0800 | [diff] [blame] | 587 | |
| 588 | // Find a new sector for the entry and write it to the new location. |
David Rogers | 8ce55cd | 2020-02-04 19:41:48 -0800 | [diff] [blame] | 589 | SectorDescriptor* new_sector; |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 590 | TRY(FindSectorWithSpace(&new_sector, entry.size(), old_sector, true)); |
Wyatt Hepler | ce7b8df | 2020-02-21 10:38:39 -0800 | [diff] [blame] | 591 | TRY(AppendEntry( |
| 592 | new_sector, &key_descriptor, key, value, key_descriptor.state())); |
David Rogers | df025cd | 2020-02-06 17:05:34 -0800 | [diff] [blame] | 593 | |
Wyatt Hepler | d229828 | 2020-02-20 17:12:45 -0800 | [diff] [blame] | 594 | // Do the valid bytes accounting for the sector the entry was relocated from. |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 595 | old_sector->RemoveValidBytes(entry.size()); |
David Rogers | df025cd | 2020-02-06 17:05:34 -0800 | [diff] [blame] | 596 | |
| 597 | return Status::OK; |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 598 | } |
| 599 | |
David Rogers | 8db5a72 | 2020-02-03 18:28:34 -0800 | [diff] [blame] | 600 | // Find either an existing sector with enough space that is not the sector to |
| 601 | // skip, or an empty sector. Maintains the invariant that there is always at |
| 602 | // least 1 empty sector unless set to bypass the rule. |
Wyatt Hepler | 5a33d8c | 2020-02-06 09:32:58 -0800 | [diff] [blame] | 603 | Status KeyValueStore::FindSectorWithSpace( |
| 604 | SectorDescriptor** found_sector, |
| 605 | size_t size, |
| 606 | const SectorDescriptor* sector_to_skip, |
| 607 | bool bypass_empty_sector_rule) { |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 608 | SectorDescriptor* first_empty_sector = nullptr; |
| 609 | bool at_least_two_empty_sectors = bypass_empty_sector_rule; |
| 610 | |
| 611 | DBG("Find sector with %zu bytes available, starting with sector %u", |
| 612 | size, |
| 613 | SectorIndex(last_new_sector_)); |
| 614 | if (sector_to_skip != nullptr) { |
| 615 | DBG(" Skip sector %u", SectorIndex(sector_to_skip)); |
| 616 | } |
| 617 | if (bypass_empty_sector_rule) { |
| 618 | DBG(" Bypassing empty sector rule"); |
| 619 | } |
| 620 | |
David Rogers | 8ce55cd | 2020-02-04 19:41:48 -0800 | [diff] [blame] | 621 | // The last_new_sector_ is the sector that was last selected as the "new empty |
| 622 | // sector" to write to. This last new sector is used as the starting point for |
| 623 | // the next "find a new empty sector to write to" operation. By using the last |
| 624 | // new sector as the start point we will cycle which empty sector is selected |
| 625 | // next, spreading the wear across all the empty sectors and get a wear |
| 626 | // leveling benefit, rather than putting more wear on the lower number |
| 627 | // sectors. |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 628 | SectorDescriptor* sector = last_new_sector_; |
David Rogers | 67f4b6c | 2020-02-06 16:17:09 -0800 | [diff] [blame] | 629 | |
David Rogers | 8ce55cd | 2020-02-04 19:41:48 -0800 | [diff] [blame] | 630 | // Look for a partial sector to use with enough space. Immediately use the |
| 631 | // first one of those that is found. While scanning for a partial sector, keep |
| 632 | // track of the first empty sector and if a second sector was seen. |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 633 | for (size_t j = 0; j < sectors_.size(); j++) { |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 634 | sector += 1; |
| 635 | if (sector == sectors_.end()) { |
| 636 | sector = sectors_.begin(); |
| 637 | } |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 638 | |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 639 | if (sector_to_skip == sector) { |
David Rogers | 8db5a72 | 2020-02-03 18:28:34 -0800 | [diff] [blame] | 640 | continue; |
| 641 | } |
| 642 | |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 643 | const size_t sector_size_bytes = partition_.sector_size_bytes(); |
| 644 | if (!sector->Empty(sector_size_bytes) && sector->HasSpace(size)) { |
| 645 | *found_sector = sector; |
David Rogers | 8ce55cd | 2020-02-04 19:41:48 -0800 | [diff] [blame] | 646 | return Status::OK; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 647 | } |
| 648 | |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 649 | if (sector->Empty(sector_size_bytes)) { |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 650 | if (first_empty_sector == nullptr) { |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 651 | first_empty_sector = sector; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 652 | } else { |
| 653 | at_least_two_empty_sectors = true; |
Wyatt Hepler | 2ad6067 | 2020-01-21 08:00:16 -0800 | [diff] [blame] | 654 | } |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 655 | } |
| 656 | } |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 657 | |
David Rogers | 8ce55cd | 2020-02-04 19:41:48 -0800 | [diff] [blame] | 658 | // If the scan for a partial sector does not find a suitable sector, use the |
| 659 | // first empty sector that was found. Normally it is required to keep 1 empty |
| 660 | // sector after the sector found here, but that rule can be bypassed in |
| 661 | // special circumstances (such as during garbage collection). |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 662 | if (at_least_two_empty_sectors) { |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 663 | DBG(" Found a usable empty sector; returning the first found (%u)", |
David Rogers | 8ce55cd | 2020-02-04 19:41:48 -0800 | [diff] [blame] | 664 | SectorIndex(first_empty_sector)); |
| 665 | last_new_sector_ = first_empty_sector; |
| 666 | *found_sector = first_empty_sector; |
| 667 | return Status::OK; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 668 | } |
David Rogers | 8ce55cd | 2020-02-04 19:41:48 -0800 | [diff] [blame] | 669 | |
| 670 | // No sector was found. |
David Rogers | 67f4b6c | 2020-02-06 16:17:09 -0800 | [diff] [blame] | 671 | DBG(" Unable to find a usable sector"); |
David Rogers | 8ce55cd | 2020-02-04 19:41:48 -0800 | [diff] [blame] | 672 | *found_sector = nullptr; |
| 673 | return Status::RESOURCE_EXHAUSTED; |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 674 | } |
| 675 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 676 | Status KeyValueStore::FindOrRecoverSectorWithSpace(SectorDescriptor** sector, |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 677 | size_t size) { |
David Rogers | 8ce55cd | 2020-02-04 19:41:48 -0800 | [diff] [blame] | 678 | Status result = FindSectorWithSpace(sector, size); |
Wyatt Hepler | 2d40169 | 2020-02-13 16:01:23 -0800 | [diff] [blame] | 679 | if (result == Status::RESOURCE_EXHAUSTED && options_.partial_gc_on_write) { |
David Rogers | 1541d61 | 2020-02-06 23:47:02 -0800 | [diff] [blame] | 680 | // Garbage collect and then try again to find the best sector. |
| 681 | TRY(GarbageCollectOneSector()); |
| 682 | return FindSectorWithSpace(sector, size); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 683 | } |
David Rogers | 8ce55cd | 2020-02-04 19:41:48 -0800 | [diff] [blame] | 684 | return result; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 685 | } |
| 686 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 687 | KeyValueStore::SectorDescriptor* KeyValueStore::FindSectorToGarbageCollect() { |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 688 | const size_t sector_size_bytes = partition_.sector_size_bytes(); |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 689 | SectorDescriptor* sector_candidate = nullptr; |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 690 | size_t candidate_bytes = 0; |
| 691 | |
| 692 | // Step 1: Try to find a sectors with stale keys and no valid keys (no |
| 693 | // relocation needed). If any such sectors are found, use the sector with the |
| 694 | // most reclaimable bytes. |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 695 | for (auto& sector : sectors_) { |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 696 | if ((sector.valid_bytes() == 0) && |
| 697 | (sector.RecoverableBytes(sector_size_bytes) > candidate_bytes)) { |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 698 | sector_candidate = §or; |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 699 | candidate_bytes = sector.RecoverableBytes(sector_size_bytes); |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
| 703 | // Step 2: If step 1 yields no sectors, just find the sector with the most |
| 704 | // reclaimable bytes. |
| 705 | if (sector_candidate == nullptr) { |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 706 | for (auto& sector : sectors_) { |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 707 | if (sector.RecoverableBytes(sector_size_bytes) > candidate_bytes) { |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 708 | sector_candidate = §or; |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 709 | candidate_bytes = sector.RecoverableBytes(sector_size_bytes); |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | } |
| 713 | |
David Rogers | 5981f31 | 2020-02-13 13:33:56 -0800 | [diff] [blame] | 714 | if (sector_candidate != nullptr) { |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 715 | DBG("Found sector %u to Garbage Collect, %zu recoverable bytes", |
David Rogers | 5981f31 | 2020-02-13 13:33:56 -0800 | [diff] [blame] | 716 | SectorIndex(sector_candidate), |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 717 | sector_candidate->RecoverableBytes(sector_size_bytes)); |
David Rogers | 5981f31 | 2020-02-13 13:33:56 -0800 | [diff] [blame] | 718 | } else { |
| 719 | DBG("Unable to find sector to garbage collect!"); |
| 720 | } |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 721 | return sector_candidate; |
| 722 | } |
| 723 | |
David Rogers | 1541d61 | 2020-02-06 23:47:02 -0800 | [diff] [blame] | 724 | Status KeyValueStore::GarbageCollectOneSector() { |
David Rogers | 67f4b6c | 2020-02-06 16:17:09 -0800 | [diff] [blame] | 725 | DBG("Garbage Collect a single sector"); |
| 726 | |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 727 | // Step 1: Find the sector to garbage collect |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 728 | SectorDescriptor* sector_to_gc = FindSectorToGarbageCollect(); |
David Rogers | 3464d0a | 2020-02-07 11:45:46 -0800 | [diff] [blame] | 729 | LogSectors(); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 730 | |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 731 | if (sector_to_gc == nullptr) { |
| 732 | return Status::RESOURCE_EXHAUSTED; |
| 733 | } |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 734 | |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 735 | // Step 2: Move any valid entries in the GC sector to other sectors |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 736 | if (sector_to_gc->valid_bytes() != 0) { |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 737 | for (auto& descriptor : key_descriptors_) { |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 738 | if (AddressInSector(*sector_to_gc, descriptor.address())) { |
David Rogers | 67f4b6c | 2020-02-06 16:17:09 -0800 | [diff] [blame] | 739 | DBG(" Relocate entry"); |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 740 | TRY(RelocateEntry(descriptor)); |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 741 | } |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 742 | } |
| 743 | } |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 744 | |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 745 | if (sector_to_gc->valid_bytes() != 0) { |
David Rogers | 67f4b6c | 2020-02-06 16:17:09 -0800 | [diff] [blame] | 746 | ERR(" Failed to relocate valid entries from sector being garbage " |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 747 | "collected, %zu valid bytes remain", |
| 748 | sector_to_gc->valid_bytes()); |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 749 | return Status::INTERNAL; |
| 750 | } |
| 751 | |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 752 | // Step 3: Reinitialize the sector |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 753 | sector_to_gc->set_writable_bytes(0); |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 754 | TRY(partition_.Erase(SectorBaseAddress(sector_to_gc), 1)); |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 755 | sector_to_gc->set_writable_bytes(partition_.sector_size_bytes()); |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 756 | |
David Rogers | 67f4b6c | 2020-02-06 16:17:09 -0800 | [diff] [blame] | 757 | DBG(" Garbage Collect complete"); |
David Rogers | 50185ad | 2020-02-07 00:02:46 -0800 | [diff] [blame] | 758 | LogSectors(); |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 759 | return Status::OK; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 760 | } |
| 761 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 762 | Status KeyValueStore::AppendEntry(SectorDescriptor* sector, |
| 763 | KeyDescriptor* key_descriptor, |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 764 | string_view key, |
Wyatt Hepler | 5a33d8c | 2020-02-06 09:32:58 -0800 | [diff] [blame] | 765 | span<const byte> value, |
| 766 | KeyDescriptor::State new_state) { |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 767 | const Address address = NextWritableAddress(sector); |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 768 | Entry entry = CreateEntry(address, key, value, new_state); |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 769 | |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 770 | DBG("Appending %zu B entry with transaction ID %" PRIu32 " to address %#zx", |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 771 | entry.size(), |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 772 | entry.transaction_id(), |
| 773 | size_t(address)); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 774 | |
David Rogers | 6592d29 | 2020-02-14 14:19:26 -0800 | [diff] [blame] | 775 | StatusWithSize result = entry.Write(key, value); |
| 776 | // Remove any bytes that were written, even if the write was not successful. |
Keir Mierle | 0a52aed | 2020-02-21 09:24:36 -0800 | [diff] [blame] | 777 | // This is important to retain the writable space invariant on the sectors. |
David Rogers | 6592d29 | 2020-02-14 14:19:26 -0800 | [diff] [blame] | 778 | sector->RemoveWritableBytes(result.size()); |
| 779 | |
| 780 | if (!result.ok()) { |
Keir Mierle | 0a52aed | 2020-02-21 09:24:36 -0800 | [diff] [blame] | 781 | // TODO: Once fake flash errors are supported in tests, test this branch. |
| 782 | ERR("Failed to write %zu bytes at %" PRIx32 ". %zu actually written", |
David Rogers | 6592d29 | 2020-02-14 14:19:26 -0800 | [diff] [blame] | 783 | entry.size(), |
Keir Mierle | 0a52aed | 2020-02-21 09:24:36 -0800 | [diff] [blame] | 784 | address, |
David Rogers | 6592d29 | 2020-02-14 14:19:26 -0800 | [diff] [blame] | 785 | result.size()); |
| 786 | return result.status(); |
| 787 | } |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 788 | |
| 789 | if (options_.verify_on_write) { |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 790 | TRY(entry.VerifyChecksumInFlash(entry_header_format_.checksum)); |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 791 | } |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 792 | |
Keir Mierle | 9e38b40 | 2020-02-21 13:06:21 -0800 | [diff] [blame] | 793 | // Entry was written successfully; update the key descriptor and the sector |
| 794 | // descriptor to reflect the new entry. |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 795 | entry.UpdateDescriptor(key_descriptor); |
David Rogers | 6592d29 | 2020-02-14 14:19:26 -0800 | [diff] [blame] | 796 | sector->AddValidBytes(result.size()); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 797 | return Status::OK; |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 798 | } |
| 799 | |
Wyatt Hepler | bdd8e5a | 2020-02-20 19:27:26 -0800 | [diff] [blame] | 800 | KeyValueStore::Entry KeyValueStore::CreateEntry(Address address, |
| 801 | std::string_view key, |
| 802 | span<const byte> value, |
| 803 | KeyDescriptor::State state) { |
Keir Mierle | 9e38b40 | 2020-02-21 13:06:21 -0800 | [diff] [blame] | 804 | // Always bump the transaction ID when creating a new entry. |
| 805 | // |
| 806 | // Burning transaction IDs prevents inconsistencies between flash and memory |
| 807 | // that which could happen if a write succeeds, but for some reason the read |
| 808 | // and verify step fails. Here's how this would happen: |
| 809 | // |
| 810 | // 1. The entry is written but for some reason the flash reports failure OR |
| 811 | // The write succeeds, but the read / verify operation fails. |
| 812 | // 2. The transaction ID is NOT incremented, because of the failure |
| 813 | // 3. (later) A new entry is written, re-using the transaction ID (oops) |
| 814 | // |
| 815 | // By always burning transaction IDs, the above problem can't happen. |
| 816 | last_transaction_id_ += 1; |
| 817 | |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 818 | if (state == KeyDescriptor::kDeleted) { |
| 819 | return Entry::Tombstone(partition_, |
| 820 | address, |
Wyatt Hepler | 88adfe8 | 2020-02-20 19:33:27 -0800 | [diff] [blame] | 821 | entry_header_format_, |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 822 | key, |
| 823 | partition_.alignment_bytes(), |
Keir Mierle | 9e38b40 | 2020-02-21 13:06:21 -0800 | [diff] [blame] | 824 | last_transaction_id_); |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 825 | } |
| 826 | return Entry::Valid(partition_, |
| 827 | address, |
Wyatt Hepler | 88adfe8 | 2020-02-20 19:33:27 -0800 | [diff] [blame] | 828 | entry_header_format_, |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 829 | key, |
| 830 | value, |
| 831 | partition_.alignment_bytes(), |
Keir Mierle | 9e38b40 | 2020-02-21 13:06:21 -0800 | [diff] [blame] | 832 | last_transaction_id_); |
Wyatt Hepler | d229828 | 2020-02-20 17:12:45 -0800 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | void KeyValueStore::Reset() { |
| 836 | initialized_ = false; |
| 837 | key_descriptors_.clear(); |
| 838 | last_new_sector_ = nullptr; |
| 839 | last_transaction_id_ = 0; |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 840 | } |
| 841 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 842 | void KeyValueStore::LogDebugInfo() { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 843 | const size_t sector_size_bytes = partition_.sector_size_bytes(); |
| 844 | DBG("====================== KEY VALUE STORE DUMP ========================="); |
| 845 | DBG(" "); |
| 846 | DBG("Flash partition:"); |
Wyatt Hepler | ad0a793 | 2020-02-06 08:20:38 -0800 | [diff] [blame] | 847 | DBG(" Sector count = %zu", partition_.sector_count()); |
Wyatt Hepler | 38ce30f | 2020-02-19 11:48:31 -0800 | [diff] [blame] | 848 | DBG(" Sector max count = %zu", sectors_.max_size()); |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 849 | DBG(" Sectors in use = %zu", sectors_.size()); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 850 | DBG(" Sector size = %zu", sector_size_bytes); |
| 851 | DBG(" Total size = %zu", partition_.size_bytes()); |
| 852 | DBG(" Alignment = %zu", partition_.alignment_bytes()); |
| 853 | DBG(" "); |
| 854 | DBG("Key descriptors:"); |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 855 | DBG(" Entry count = %zu", key_descriptors_.size()); |
Wyatt Hepler | 38ce30f | 2020-02-19 11:48:31 -0800 | [diff] [blame] | 856 | DBG(" Max entry count = %zu", key_descriptors_.max_size()); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 857 | DBG(" "); |
| 858 | DBG(" # hash version address address (hex)"); |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 859 | for (size_t i = 0; i < key_descriptors_.size(); ++i) { |
| 860 | const KeyDescriptor& kd = key_descriptors_[i]; |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 861 | DBG(" |%3zu: | %8zx |%8zu | %8zu | %8zx", |
| 862 | i, |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 863 | size_t(kd.hash()), |
| 864 | size_t(kd.transaction_id()), |
| 865 | size_t(kd.address()), |
| 866 | size_t(kd.address())); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 867 | } |
| 868 | DBG(" "); |
| 869 | |
| 870 | DBG("Sector descriptors:"); |
| 871 | DBG(" # tail free valid has_space"); |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 872 | for (size_t sector_id = 0; sector_id < sectors_.size(); ++sector_id) { |
| 873 | const SectorDescriptor& sd = sectors_[sector_id]; |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 874 | DBG(" |%3zu: | %8zu |%8zu | %s", |
| 875 | sector_id, |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 876 | size_t(sd.writable_bytes()), |
| 877 | sd.valid_bytes(), |
| 878 | sd.writable_bytes() ? "YES" : ""); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 879 | } |
| 880 | DBG(" "); |
| 881 | |
| 882 | // TODO: This should stop logging after some threshold. |
| 883 | // size_t dumped_bytes = 0; |
| 884 | DBG("Sector raw data:"); |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 885 | for (size_t sector_id = 0; sector_id < sectors_.size(); ++sector_id) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 886 | // Read sector data. Yes, this will blow the stack on embedded. |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 887 | std::array<byte, 500> raw_sector_data; // TODO!!! |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 888 | StatusWithSize sws = |
| 889 | partition_.Read(sector_id * sector_size_bytes, raw_sector_data); |
| 890 | DBG("Read: %zu bytes", sws.size()); |
| 891 | |
| 892 | DBG(" base addr offs 0 1 2 3 4 5 6 7"); |
| 893 | for (size_t i = 0; i < sector_size_bytes; i += 8) { |
| 894 | DBG(" %3zu %8zx %5zu | %02x %02x %02x %02x %02x %02x %02x %02x", |
| 895 | sector_id, |
| 896 | (sector_id * sector_size_bytes) + i, |
| 897 | i, |
| 898 | static_cast<unsigned int>(raw_sector_data[i + 0]), |
| 899 | static_cast<unsigned int>(raw_sector_data[i + 1]), |
| 900 | static_cast<unsigned int>(raw_sector_data[i + 2]), |
| 901 | static_cast<unsigned int>(raw_sector_data[i + 3]), |
| 902 | static_cast<unsigned int>(raw_sector_data[i + 4]), |
| 903 | static_cast<unsigned int>(raw_sector_data[i + 5]), |
| 904 | static_cast<unsigned int>(raw_sector_data[i + 6]), |
| 905 | static_cast<unsigned int>(raw_sector_data[i + 7])); |
| 906 | |
| 907 | // TODO: Fix exit condition. |
| 908 | if (i > 128) { |
| 909 | break; |
| 910 | } |
| 911 | } |
| 912 | DBG(" "); |
| 913 | } |
| 914 | |
| 915 | DBG("////////////////////// KEY VALUE STORE DUMP END /////////////////////"); |
| 916 | } |
| 917 | |
David Rogers | cf680ab | 2020-02-12 23:28:32 -0800 | [diff] [blame] | 918 | void KeyValueStore::LogSectors() const { |
| 919 | DBG("Sector descriptors: count %zu", sectors_.size()); |
Wyatt Hepler | 1c329ca | 2020-02-07 18:07:23 -0800 | [diff] [blame] | 920 | for (auto& sector : sectors_) { |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 921 | DBG(" - Sector %u: valid %zu, recoverable %zu, free %zu", |
David Rogers | 50185ad | 2020-02-07 00:02:46 -0800 | [diff] [blame] | 922 | SectorIndex(§or), |
Wyatt Hepler | 2c7eca0 | 2020-02-18 16:01:42 -0800 | [diff] [blame] | 923 | sector.valid_bytes(), |
| 924 | sector.RecoverableBytes(partition_.sector_size_bytes()), |
| 925 | sector.writable_bytes()); |
David Rogers | 50185ad | 2020-02-07 00:02:46 -0800 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | |
David Rogers | cf680ab | 2020-02-12 23:28:32 -0800 | [diff] [blame] | 929 | void KeyValueStore::LogKeyDescriptor() const { |
| 930 | DBG("Key descriptors: count %zu", key_descriptors_.size()); |
| 931 | for (auto& key : key_descriptors_) { |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 932 | DBG(" - Key: %s, hash %#zx, transaction ID %zu, address %#zx", |
David Rogers | cf680ab | 2020-02-12 23:28:32 -0800 | [diff] [blame] | 933 | key.deleted() ? "Deleted" : "Valid", |
Wyatt Hepler | 1fc1104 | 2020-02-19 17:17:51 -0800 | [diff] [blame] | 934 | static_cast<size_t>(key.hash()), |
| 935 | static_cast<size_t>(key.transaction_id()), |
| 936 | static_cast<size_t>(key.address())); |
David Rogers | cf680ab | 2020-02-12 23:28:32 -0800 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | |
Wyatt Hepler | 2ad6067 | 2020-01-21 08:00:16 -0800 | [diff] [blame] | 940 | } // namespace pw::kvs |