blob: 8e859f2c90a4a5373e89e96fc849ae3e6fc4d4de [file] [log] [blame]
Wyatt Heplerb7609542020-01-24 10:29:54 -08001// 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 Heplerb7609542020-01-24 10:29:54 -080015#include "pw_kvs/key_value_store.h"
16
Wyatt Heplerbab0e202020-02-04 07:40:08 -080017#include <algorithm>
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -080018#include <cinttypes>
Wyatt Heplerb7609542020-01-24 10:29:54 -080019#include <cstring>
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080020#include <type_traits>
Wyatt Heplerb7609542020-01-24 10:29:54 -080021
Keir Mierle8c352dc2020-02-02 13:58:19 -080022#define PW_LOG_USE_ULTRA_SHORT_NAMES 1
Wyatt Heplerbdd8e5a2020-02-20 19:27:26 -080023#include "pw_kvs/internal/entry.h"
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080024#include "pw_kvs_private/macros.h"
Keir Mierle8c352dc2020-02-02 13:58:19 -080025#include "pw_log/log.h"
Wyatt Heplerb7609542020-01-24 10:29:54 -080026
Wyatt Hepler2ad60672020-01-21 08:00:16 -080027namespace pw::kvs {
Wyatt Heplera00d1ef2020-02-14 14:31:26 -080028namespace {
Wyatt Heplerb7609542020-01-24 10:29:54 -080029
Wyatt Hepleracaacf92020-01-24 10:58:30 -080030using std::byte;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080031using std::string_view;
Wyatt Hepleracaacf92020-01-24 10:58:30 -080032
Wyatt Heplera00d1ef2020-02-14 14:31:26 -080033constexpr bool InvalidKey(std::string_view key) {
Wyatt Heplerbdd8e5a2020-02-20 19:27:26 -080034 return key.empty() || (key.size() > internal::Entry::kMaxKeyLength);
Wyatt Heplera00d1ef2020-02-14 14:31:26 -080035}
36
37} // namespace
38
Wyatt Heplerad0a7932020-02-06 08:20:38 -080039KeyValueStore::KeyValueStore(FlashPartition* partition,
Wyatt Hepler38ce30f2020-02-19 11:48:31 -080040 Vector<KeyDescriptor>& key_descriptor_list,
41 Vector<SectorDescriptor>& sector_descriptor_list,
Wyatt Hepler88adfe82020-02-20 19:33:27 -080042 const EntryFormat& format,
Wyatt Heplerad0a7932020-02-06 08:20:38 -080043 const Options& options)
44 : partition_(*partition),
45 entry_header_format_(format),
Wyatt Hepler38ce30f2020-02-19 11:48:31 -080046 key_descriptors_(key_descriptor_list),
Wyatt Heplerd2298282020-02-20 17:12:45 -080047 sectors_(sector_descriptor_list),
48 options_(options) {
49 Reset();
50}
Wyatt Heplerad0a7932020-02-06 08:20:38 -080051
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080052Status KeyValueStore::Init() {
Wyatt Heplerd2298282020-02-20 17:12:45 -080053 Reset();
54
David Rogers2e9e0c82020-02-13 15:06:06 -080055 INF("Initializing key value store");
Wyatt Hepler38ce30f2020-02-19 11:48:31 -080056 if (partition_.sector_count() > sectors_.max_size()) {
David Rogers2e9e0c82020-02-13 15:06:06 -080057 ERR("KVS init failed: kMaxUsableSectors (=%zu) must be at least as "
58 "large as the number of sectors in the flash partition (=%zu)",
Wyatt Hepler38ce30f2020-02-19 11:48:31 -080059 sectors_.max_size(),
David Rogers2e9e0c82020-02-13 15:06:06 -080060 partition_.sector_count());
Wyatt Heplerad0a7932020-02-06 08:20:38 -080061 return Status::FAILED_PRECONDITION;
62 }
63
Keir Mierle8c352dc2020-02-02 13:58:19 -080064 const size_t sector_size_bytes = partition_.sector_size_bytes();
Keir Mierle8c352dc2020-02-02 13:58:19 -080065
David Rogersf0a35442020-02-04 12:16:38 -080066 if (working_buffer_.size() < sector_size_bytes) {
Wyatt Heplerce7b8df2020-02-21 10:38:39 -080067 ERR("KVS init failed: working_buffer_ (%zu B) is smaller than sector size "
68 "(%zu B)",
David Rogersf0a35442020-02-04 12:16:38 -080069 working_buffer_.size(),
70 sector_size_bytes);
71 return Status::INVALID_ARGUMENT;
72 }
73
Keir Mierle8c352dc2020-02-02 13:58:19 -080074 DBG("First pass: Read all entries from all sectors");
Wyatt Hepler2c7eca02020-02-18 16:01:42 -080075 Address sector_address = 0;
Keir Mierle8c352dc2020-02-02 13:58:19 -080076
Wyatt Heplerd2298282020-02-20 17:12:45 -080077 sectors_.assign(partition_.sector_count(),
78 SectorDescriptor(sector_size_bytes));
79
Alexei Frolovd4adf912020-02-21 13:29:15 -080080 size_t total_corrupt_bytes = 0;
81 int corrupt_entries = 0;
David Rogers91627482020-02-27 17:38:12 -080082 bool empty_sector_found = false;
Alexei Frolovd4adf912020-02-21 13:29:15 -080083
Wyatt Hepler2c7eca02020-02-18 16:01:42 -080084 for (SectorDescriptor& sector : sectors_) {
Keir Mierle8c352dc2020-02-02 13:58:19 -080085 Address entry_address = sector_address;
86
Alexei Frolovd4adf912020-02-21 13:29:15 -080087 size_t sector_corrupt_bytes = 0;
88
Wyatt Hepler2c7eca02020-02-18 16:01:42 -080089 for (int num_entries_in_sector = 0; true; num_entries_in_sector++) {
90 DBG("Load entry: sector=%" PRIx32 ", entry#=%d, address=%" PRIx32,
91 sector_address,
Keir Mierle8c352dc2020-02-02 13:58:19 -080092 num_entries_in_sector,
Wyatt Hepler2c7eca02020-02-18 16:01:42 -080093 entry_address);
Keir Mierle8c352dc2020-02-02 13:58:19 -080094
Wyatt Hepler2c7eca02020-02-18 16:01:42 -080095 if (!AddressInSector(sector, entry_address)) {
Keir Mierle8c352dc2020-02-02 13:58:19 -080096 DBG("Fell off end of sector; moving to the next sector");
97 break;
98 }
99
100 Address next_entry_address;
101 Status status = LoadEntry(entry_address, &next_entry_address);
102 if (status == Status::NOT_FOUND) {
103 DBG("Hit un-written data in sector; moving to the next sector");
104 break;
105 }
106 if (status == Status::DATA_LOSS) {
Alexei Frolovd4adf912020-02-21 13:29:15 -0800107 // The entry could not be read, indicating data corruption within the
108 // sector. Try to scan the remainder of the sector for other entries.
109 ERR("KVS init: data loss detected in sector %u at address %zu",
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800110 SectorIndex(&sector),
111 size_t(entry_address));
Alexei Frolovd4adf912020-02-21 13:29:15 -0800112
113 corrupt_entries++;
114
115 status = ScanForEntry(sector,
116 entry_address + Entry::kMinAlignmentBytes,
117 &next_entry_address);
118 if (status == Status::NOT_FOUND) {
119 // No further entries in this sector. Mark the remaining bytes in the
120 // sector as corrupt (since we can't reliably know the size of the
121 // corrupt entry).
122 sector_corrupt_bytes +=
123 sector_size_bytes - (entry_address - sector_address);
124 break;
125 }
126
127 if (!status.ok()) {
128 ERR("Unexpected error in KVS initialization: %s", status.str());
129 return Status::UNKNOWN;
130 }
131
132 sector_corrupt_bytes += next_entry_address - entry_address;
133 } else if (!status.ok()) {
134 ERR("Unexpected error in KVS initialization: %s", status.str());
135 return Status::UNKNOWN;
Keir Mierle8c352dc2020-02-02 13:58:19 -0800136 }
Keir Mierle8c352dc2020-02-02 13:58:19 -0800137
138 // Entry loaded successfully; so get ready to load the next one.
139 entry_address = next_entry_address;
140
141 // Update of the number of writable bytes in this sector.
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800142 sector.set_writable_bytes(sector_size_bytes -
143 (entry_address - sector_address));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800144 }
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800145
Alexei Frolovd4adf912020-02-21 13:29:15 -0800146 if (sector_corrupt_bytes > 0) {
147 // If the sector contains corrupt data, prevent any further entries from
148 // being written to it by indicating that it has no space. This should
149 // also make it a decent GC candidate. Valid keys in the sector are still
150 // readable as normal.
151 sector.set_writable_bytes(0);
152
153 WRN("Sector %u contains %zuB of corrupt data",
154 SectorIndex(&sector),
155 sector_corrupt_bytes);
156 }
157
David Rogers91627482020-02-27 17:38:12 -0800158 if (sector.Empty(sector_size_bytes)) {
159 empty_sector_found = true;
160 }
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800161 sector_address += sector_size_bytes;
Alexei Frolovd4adf912020-02-21 13:29:15 -0800162 total_corrupt_bytes += sector_corrupt_bytes;
Keir Mierle8c352dc2020-02-02 13:58:19 -0800163 }
164
165 DBG("Second pass: Count valid bytes in each sector");
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800166 const KeyDescriptor* newest_key = nullptr;
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800167
Keir Mierle8c352dc2020-02-02 13:58:19 -0800168 // For every valid key, increment the valid bytes for that sector.
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800169 for (KeyDescriptor& key_descriptor : key_descriptors_) {
David Rogersf56131c2020-03-04 10:19:22 -0800170 for (auto& address : key_descriptor.addresses()) {
171 Entry entry;
172 TRY(Entry::Read(partition_, address, &entry));
173 SectorFromAddress(address)->AddValidBytes(entry.size());
174 }
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800175 if (key_descriptor.IsNewerThan(last_transaction_id_)) {
176 last_transaction_id_ = key_descriptor.transaction_id();
177 newest_key = &key_descriptor;
178 }
Keir Mierle8c352dc2020-02-02 13:58:19 -0800179 }
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800180
181 if (newest_key == nullptr) {
182 last_new_sector_ = sectors_.begin();
183 } else {
David Rogersf56131c2020-03-04 10:19:22 -0800184 last_new_sector_ = SectorFromAddress(newest_key->addresses().back());
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800185 }
186
David Rogers91627482020-02-27 17:38:12 -0800187 if (!empty_sector_found) {
188 // TODO: Record/report the error condition and recovery result.
189 Status gc_result = GarbageCollectPartial();
190
191 if (!gc_result.ok()) {
192 ERR("KVS init failed: Unable to maintain required free sector");
193 return Status::INTERNAL;
194 }
195 }
196
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800197 initialized_ = true;
David Rogers2e9e0c82020-02-13 15:06:06 -0800198
Armando Montanez5464d5f2020-02-20 10:12:20 -0800199 INF("KeyValueStore init complete: active keys %zu, deleted keys %zu, sectors "
David Rogers2e9e0c82020-02-13 15:06:06 -0800200 "%zu, logical sector size %zu bytes",
201 size(),
202 (key_descriptors_.size() - size()),
203 sectors_.size(),
204 partition_.sector_size_bytes());
205
Alexei Frolovd4adf912020-02-21 13:29:15 -0800206 if (total_corrupt_bytes > 0) {
207 WRN("Found %zu corrupt bytes and %d corrupt entries during init process; "
208 "some keys may be missing",
209 total_corrupt_bytes,
210 corrupt_entries);
211 return Status::DATA_LOSS;
212 }
213
Keir Mierle8c352dc2020-02-02 13:58:19 -0800214 return Status::OK;
215}
216
Alexei Frolov9e235832020-02-24 12:44:45 -0800217KeyValueStore::StorageStats KeyValueStore::GetStorageStats() const {
218 StorageStats stats{0, 0, 0};
219 const size_t sector_size = partition_.sector_size_bytes();
220 bool found_empty_sector = false;
221
222 for (const SectorDescriptor& sector : sectors_) {
223 stats.in_use_bytes += sector.valid_bytes();
224 stats.reclaimable_bytes += sector.RecoverableBytes(sector_size);
225
226 if (!found_empty_sector && sector.Empty(sector_size)) {
227 // The KVS tries to always keep an empty sector for GC, so don't count
228 // the first empty sector seen as writable space. However, a free sector
229 // cannot always be assumed to exist; if a GC operation fails, all sectors
230 // may be partially written, in which case the space reported might be
231 // inaccurate.
232 found_empty_sector = true;
233 continue;
234 }
235
236 stats.writable_bytes += sector.writable_bytes();
237 }
238
239 return stats;
240}
241
Keir Mierle8c352dc2020-02-02 13:58:19 -0800242Status KeyValueStore::LoadEntry(Address entry_address,
243 Address* next_entry_address) {
Wyatt Heplere541e072020-02-14 09:10:53 -0800244 Entry entry;
245 TRY(Entry::Read(partition_, entry_address, &entry));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800246
247 // TODO: Handle multiple magics for formats that have changed.
Wyatt Heplere541e072020-02-14 09:10:53 -0800248 if (entry.magic() != entry_header_format_.magic) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800249 // TODO: It may be cleaner to have some logging helpers for these cases.
Wyatt Heplere541e072020-02-14 09:10:53 -0800250 ERR("Found corrupt magic: %zx; expecting %zx; at address %zx",
251 size_t(entry.magic()),
Keir Mierle8c352dc2020-02-02 13:58:19 -0800252 size_t(entry_header_format_.magic),
253 size_t(entry_address));
254 return Status::DATA_LOSS;
255 }
256
257 // Read the key from flash & validate the entry (which reads the value).
Wyatt Heplera00d1ef2020-02-14 14:31:26 -0800258 Entry::KeyBuffer key_buffer;
Wyatt Heplere541e072020-02-14 09:10:53 -0800259 TRY_ASSIGN(size_t key_length, entry.ReadKey(key_buffer));
260 const string_view key(key_buffer.data(), key_length);
Wyatt Heplerbab0e202020-02-04 07:40:08 -0800261
Wyatt Heplere541e072020-02-14 09:10:53 -0800262 TRY(entry.VerifyChecksumInFlash(entry_header_format_.checksum));
David Rogersf56131c2020-03-04 10:19:22 -0800263
264 // A valid entry was found, so update the next entry address before doing any
265 // of the checks that happen in AppendNewOrOverwriteStaleExistingDescriptor().
266 *next_entry_address = entry.next_address();
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800267 TRY(AppendNewOrOverwriteStaleExistingDescriptor(entry.descriptor(key)));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800268
Keir Mierle8c352dc2020-02-02 13:58:19 -0800269 return Status::OK;
270}
271
Alexei Frolovd4adf912020-02-21 13:29:15 -0800272// Scans flash memory within a sector to find a KVS entry magic.
Alexei Frolovd4adf912020-02-21 13:29:15 -0800273Status KeyValueStore::ScanForEntry(const SectorDescriptor& sector,
274 Address start_address,
275 Address* next_entry_address) {
276 DBG("Scanning sector %u for entries starting from address %zx",
277 SectorIndex(&sector),
278 size_t(start_address));
279
280 // Entries must start at addresses which are aligned on a multiple of
281 // Entry::kMinAlignmentBytes. However, that multiple can vary between entries.
282 // When scanning, we don't have an entry to tell us what the current alignment
283 // is, so the minimum alignment is used to be exhaustive.
284 for (Address address = AlignUp(start_address, Entry::kMinAlignmentBytes);
285 AddressInSector(sector, address);
286 address += Entry::kMinAlignmentBytes) {
287 // TODO: Handle multiple magics for formats that have changed.
288 uint32_t magic;
289 TRY(partition_.Read(address, as_writable_bytes(span(&magic, 1))));
290 if (magic == entry_header_format_.magic) {
291 DBG("Found entry magic at address %zx", size_t(address));
292 *next_entry_address = address;
293 return Status::OK;
294 }
295 }
296
297 return Status::NOT_FOUND;
298}
299
Keir Mierle8c352dc2020-02-02 13:58:19 -0800300// TODO: This method is the trigger of the O(valid_entries * all_entries) time
301// complexity for reading. At some cost to memory, this could be optimized by
302// using a hash table instead of scanning, but in practice this should be fine
303// for a small number of keys
304Status KeyValueStore::AppendNewOrOverwriteStaleExistingDescriptor(
305 const KeyDescriptor& key_descriptor) {
306 // With the new key descriptor, either add it to the descriptor table or
307 // overwrite an existing entry with an older version of the key.
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800308 KeyDescriptor* existing_descriptor = FindDescriptor(key_descriptor.hash());
Keir Mierle8c352dc2020-02-02 13:58:19 -0800309
Wyatt Hepler5406a672020-02-18 15:42:38 -0800310 // Write a new entry.
311 if (existing_descriptor == nullptr) {
312 if (key_descriptors_.full()) {
313 return Status::RESOURCE_EXHAUSTED;
314 }
315 key_descriptors_.push_back(key_descriptor);
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800316 } else if (key_descriptor.IsNewerThan(
317 existing_descriptor->transaction_id())) {
Wyatt Hepler5406a672020-02-18 15:42:38 -0800318 // Existing entry is old; replace the existing entry with the new one.
319 *existing_descriptor = key_descriptor;
David Rogersf56131c2020-03-04 10:19:22 -0800320 } else if (existing_descriptor->transaction_id() ==
321 key_descriptor.transaction_id()) {
322 // If the entries have a duplicate transaction ID, add the new (redundant)
323 // entry to the existing descriptor.
324 if (existing_descriptor->hash() != key_descriptor.hash()) {
325 ERR("Duplicate entry for key %#010" PRIx32 " with transaction ID %" PRIu32
326 " has non-matching hash",
327 key_descriptor.hash(),
328 key_descriptor.transaction_id());
Wyatt Hepler5406a672020-02-18 15:42:38 -0800329 return Status::DATA_LOSS;
330 }
David Rogersf56131c2020-03-04 10:19:22 -0800331
332 // Verify that this entry is not in the same sector as an existing copy of
333 // this same key.
334 for (auto address : existing_descriptor->addresses()) {
335 if (SectorFromAddress(address) ==
336 SectorFromAddress(key_descriptor.address())) {
337 DBG("Multiple Redundant entries in same sector %u",
338 SectorIndex(SectorFromAddress(address)));
339 return Status::DATA_LOSS;
340 }
341 }
342 existing_descriptor->addresses().push_back(key_descriptor.address());
343 } else {
Wyatt Hepler5406a672020-02-18 15:42:38 -0800344 DBG("Found stale entry when appending; ignoring");
Keir Mierle8c352dc2020-02-02 13:58:19 -0800345 }
Keir Mierle8c352dc2020-02-02 13:58:19 -0800346 return Status::OK;
347}
348
Keir Mierle8c352dc2020-02-02 13:58:19 -0800349KeyValueStore::KeyDescriptor* KeyValueStore::FindDescriptor(uint32_t hash) {
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800350 for (KeyDescriptor& key_descriptor : key_descriptors_) {
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800351 if (key_descriptor.hash() == hash) {
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800352 return &key_descriptor;
Keir Mierle8c352dc2020-02-02 13:58:19 -0800353 }
354 }
355 return nullptr;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800356}
357
358StatusWithSize KeyValueStore::Get(string_view key,
Wyatt Hepler5f6efc02020-02-18 16:54:31 -0800359 span<byte> value_buffer,
360 size_t offset_bytes) const {
Wyatt Hepler50f70772020-02-13 11:25:10 -0800361 TRY_WITH_SIZE(CheckOperation(key));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800362
David Rogers2761aeb2020-01-31 17:09:00 -0800363 const KeyDescriptor* key_descriptor;
Wyatt Hepler2d401692020-02-13 16:01:23 -0800364 TRY_WITH_SIZE(FindExistingKeyDescriptor(key, &key_descriptor));
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800365
Wyatt Heplerfac81132020-02-27 17:26:33 -0800366 return Get(key, *key_descriptor, value_buffer, offset_bytes);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800367}
368
Wyatt Heplerfac81132020-02-27 17:26:33 -0800369Status KeyValueStore::PutBytes(string_view key, span<const byte> value) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800370 DBG("Writing key/value; key length=%zu, value length=%zu",
371 key.size(),
372 value.size());
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800373
374 TRY(CheckOperation(key));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800375
Wyatt Hepler5406a672020-02-18 15:42:38 -0800376 if (Entry::size(partition_, key, value) > partition_.sector_size_bytes()) {
377 DBG("%zu B value with %zu B key cannot fit in one sector",
378 value.size(),
379 key.size());
380 return Status::INVALID_ARGUMENT;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800381 }
382
David Rogers2761aeb2020-01-31 17:09:00 -0800383 KeyDescriptor* key_descriptor;
Wyatt Hepler2d401692020-02-13 16:01:23 -0800384 Status status = FindKeyDescriptor(key, &key_descriptor);
385
386 if (status.ok()) {
David Rogersf56131c2020-03-04 10:19:22 -0800387 // TODO: figure out logging how to support multiple addresses.
388 DBG("Overwriting entry for key %#08" PRIx32 " in %u sectors including %u",
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800389 key_descriptor->hash(),
David Rogersf56131c2020-03-04 10:19:22 -0800390 unsigned(key_descriptor->addresses().size()),
391 SectorIndex(SectorFromAddress(key_descriptor->address())));
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800392 return WriteEntryForExistingKey(
393 key_descriptor, KeyDescriptor::kValid, key, value);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800394 }
David Rogers2761aeb2020-01-31 17:09:00 -0800395
Wyatt Hepler2d401692020-02-13 16:01:23 -0800396 if (status == Status::NOT_FOUND) {
397 return WriteEntryForNewKey(key, value);
398 }
399
400 return status;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800401}
402
403Status KeyValueStore::Delete(string_view key) {
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800404 TRY(CheckOperation(key));
405
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800406 KeyDescriptor* key_descriptor;
Wyatt Hepler2d401692020-02-13 16:01:23 -0800407 TRY(FindExistingKeyDescriptor(key, &key_descriptor));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800408
David Rogersf56131c2020-03-04 10:19:22 -0800409 // TODO: figure out logging how to support multiple addresses.
410 DBG("Writing tombstone for key %#08" PRIx32 " in %u sectors including %u",
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800411 key_descriptor->hash(),
David Rogersf56131c2020-03-04 10:19:22 -0800412 unsigned(key_descriptor->addresses().size()),
413 SectorIndex(SectorFromAddress(key_descriptor->address())));
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800414 return WriteEntryForExistingKey(
415 key_descriptor, KeyDescriptor::kDeleted, key, {});
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800416}
417
Wyatt Hepler08d37d82020-02-27 15:45:37 -0800418void KeyValueStore::Item::ReadKey() {
419 key_buffer_.fill('\0');
420
421 Entry entry;
422 if (Entry::Read(kvs_.partition_, descriptor_->address(), &entry).ok()) {
423 entry.ReadKey(key_buffer_);
424 }
425}
426
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800427KeyValueStore::iterator& KeyValueStore::iterator::operator++() {
428 // Skip to the next entry that is valid (not deleted).
Wyatt Hepler08d37d82020-02-27 15:45:37 -0800429 while (++item_.descriptor_ != item_.kvs_.key_descriptors_.end() &&
430 item_.descriptor_->deleted()) {
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800431 }
432 return *this;
433}
434
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800435KeyValueStore::iterator KeyValueStore::begin() const {
Wyatt Hepler08d37d82020-02-27 15:45:37 -0800436 const KeyDescriptor* descriptor = key_descriptors_.begin();
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800437 // Skip over any deleted entries at the start of the descriptor list.
Wyatt Hepler08d37d82020-02-27 15:45:37 -0800438 while (descriptor != key_descriptors_.end() && descriptor->deleted()) {
439 ++descriptor;
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800440 }
Wyatt Hepler08d37d82020-02-27 15:45:37 -0800441 return iterator(*this, descriptor);
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800442}
443
444// TODO(hepler): The valid entry count could be tracked in the KVS to avoid the
445// need for this for-loop.
446size_t KeyValueStore::size() const {
447 size_t valid_entries = 0;
448
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800449 for (const KeyDescriptor& key_descriptor : key_descriptors_) {
450 if (!key_descriptor.deleted()) {
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800451 valid_entries += 1;
452 }
453 }
454
455 return valid_entries;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800456}
457
Wyatt Heplered163b02020-02-03 17:49:32 -0800458StatusWithSize KeyValueStore::ValueSize(std::string_view key) const {
Wyatt Hepler50f70772020-02-13 11:25:10 -0800459 TRY_WITH_SIZE(CheckOperation(key));
Wyatt Heplered163b02020-02-03 17:49:32 -0800460
461 const KeyDescriptor* key_descriptor;
Wyatt Hepler2d401692020-02-13 16:01:23 -0800462 TRY_WITH_SIZE(FindExistingKeyDescriptor(key, &key_descriptor));
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800463
Wyatt Heplerfac81132020-02-27 17:26:33 -0800464 return ValueSize(*key_descriptor);
465}
Wyatt Heplered163b02020-02-03 17:49:32 -0800466
Wyatt Heplerfac81132020-02-27 17:26:33 -0800467StatusWithSize KeyValueStore::Get(string_view key,
468 const KeyDescriptor& descriptor,
469 span<std::byte> value_buffer,
470 size_t offset_bytes) const {
471 Entry entry;
472 TRY_WITH_SIZE(Entry::Read(partition_, descriptor.address(), &entry));
473
474 StatusWithSize result = entry.ReadValue(value_buffer, offset_bytes);
475 if (result.ok() && options_.verify_on_read && offset_bytes == 0u) {
476 Status verify_result = entry.VerifyChecksum(
477 entry_header_format_.checksum, key, value_buffer.first(result.size()));
478 if (!verify_result.ok()) {
479 std::memset(value_buffer.data(), 0, result.size());
480 return StatusWithSize(verify_result, 0);
481 }
482
483 return StatusWithSize(verify_result, result.size());
484 }
485 return result;
Wyatt Heplered163b02020-02-03 17:49:32 -0800486}
487
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800488Status KeyValueStore::FixedSizeGet(std::string_view key,
Wyatt Heplerfac81132020-02-27 17:26:33 -0800489 void* value,
490 size_t size_bytes) const {
491 TRY(CheckOperation(key));
492
493 const KeyDescriptor* descriptor;
494 TRY(FindExistingKeyDescriptor(key, &descriptor));
495
496 return FixedSizeGet(key, *descriptor, value, size_bytes);
497}
498
499Status KeyValueStore::FixedSizeGet(std::string_view key,
500 const KeyDescriptor& descriptor,
501 void* value,
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800502 size_t size_bytes) const {
503 // Ensure that the size of the stored value matches the size of the type.
504 // Otherwise, report error. This check avoids potential memory corruption.
Wyatt Heplerfac81132020-02-27 17:26:33 -0800505 TRY_ASSIGN(const size_t actual_size, ValueSize(descriptor));
506
507 if (actual_size != size_bytes) {
508 DBG("Requested %zu B read, but value is %zu B", size_bytes, actual_size);
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800509 return Status::INVALID_ARGUMENT;
Wyatt Heplerbab0e202020-02-04 07:40:08 -0800510 }
Wyatt Heplerfac81132020-02-27 17:26:33 -0800511
512 StatusWithSize result =
513 Get(key, descriptor, span(static_cast<byte*>(value), size_bytes), 0);
514
515 return result.status();
516}
517
518StatusWithSize KeyValueStore::ValueSize(const KeyDescriptor& descriptor) const {
519 Entry entry;
520 TRY_WITH_SIZE(Entry::Read(partition_, descriptor.address(), &entry));
521
522 return StatusWithSize(entry.value_size());
Keir Mierle8c352dc2020-02-02 13:58:19 -0800523}
524
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800525Status KeyValueStore::CheckOperation(string_view key) const {
Wyatt Hepleracaacf92020-01-24 10:58:30 -0800526 if (InvalidKey(key)) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800527 return Status::INVALID_ARGUMENT;
528 }
Wyatt Heplerd2298282020-02-20 17:12:45 -0800529 if (!initialized()) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800530 return Status::FAILED_PRECONDITION;
531 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800532 return Status::OK;
533}
534
Wyatt Hepler2d401692020-02-13 16:01:23 -0800535// Searches for a KeyDescriptor that matches this key and sets *result to point
536// to it if one is found.
537//
538// OK: there is a matching descriptor and *result is set
539// NOT_FOUND: there is no descriptor that matches this key, but this key
540// has a unique hash (and could potentially be added to the KVS)
541// ALREADY_EXISTS: there is no descriptor that matches this key, but the
542// key's hash collides with the hash for an existing descriptor
543//
David Rogers2761aeb2020-01-31 17:09:00 -0800544Status KeyValueStore::FindKeyDescriptor(string_view key,
545 const KeyDescriptor** result) const {
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800546 const uint32_t hash = internal::Hash(key);
Wyatt Heplera00d1ef2020-02-14 14:31:26 -0800547 Entry::KeyBuffer key_buffer;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800548
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800549 for (auto& descriptor : key_descriptors_) {
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800550 if (descriptor.hash() == hash) {
Wyatt Heplere541e072020-02-14 09:10:53 -0800551 TRY(Entry::ReadKey(
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800552 partition_, descriptor.address(), key.size(), key_buffer.data()));
Wyatt Heplerb7609542020-01-24 10:29:54 -0800553
Wyatt Heplere541e072020-02-14 09:10:53 -0800554 if (key == string_view(key_buffer.data(), key.size())) {
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800555 DBG("Found match for key hash 0x%08" PRIx32, hash);
David Rogers2761aeb2020-01-31 17:09:00 -0800556 *result = &descriptor;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800557 return Status::OK;
Wyatt Hepler2d401692020-02-13 16:01:23 -0800558 } else {
559 WRN("Found key hash collision for 0x%08" PRIx32, hash);
560 return Status::ALREADY_EXISTS;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800561 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800562 }
563 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800564 return Status::NOT_FOUND;
565}
566
Wyatt Hepler2d401692020-02-13 16:01:23 -0800567// Searches for a KeyDescriptor that matches this key and sets *result to point
568// to it if one is found.
569//
570// OK: there is a matching descriptor and *result is set
571// NOT_FOUND: there is no descriptor that matches this key
572//
573Status KeyValueStore::FindExistingKeyDescriptor(
574 string_view key, const KeyDescriptor** result) const {
575 Status status = FindKeyDescriptor(key, result);
576
577 // If the key's hash collides with an existing key or if the key is deleted,
578 // treat it as if it is not in the KVS.
579 if (status == Status::ALREADY_EXISTS ||
580 (status.ok() && (*result)->deleted())) {
581 return Status::NOT_FOUND;
582 }
583 return status;
584}
585
David Rogers2761aeb2020-01-31 17:09:00 -0800586Status KeyValueStore::WriteEntryForExistingKey(KeyDescriptor* key_descriptor,
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800587 KeyDescriptor::State new_state,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800588 string_view key,
589 span<const byte> value) {
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800590 // Find the original entry and sector to update the sector's valid_bytes.
Wyatt Hepler30a52152020-02-12 11:26:05 -0800591 Entry original_entry;
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800592 TRY(Entry::Read(partition_, key_descriptor->address(), &original_entry));
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800593
David Rogers2761aeb2020-01-31 17:09:00 -0800594 SectorDescriptor* sector;
Wyatt Hepler5406a672020-02-18 15:42:38 -0800595 TRY(FindOrRecoverSectorWithSpace(&sector,
596 Entry::size(partition_, key, value)));
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800597 DBG("Writing existing entry; found sector %u (%#" PRIx32 ")",
598 SectorIndex(sector),
599 SectorBaseAddress(sector));
David Rogers3464d0a2020-02-07 11:45:46 -0800600
David Rogersf56131c2020-03-04 10:19:22 -0800601 // TODO: Verify the copy does a full copy including the address vector.
602 KeyDescriptor old_key_descriptor = *key_descriptor;
David Rogers3464d0a2020-02-07 11:45:46 -0800603
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800604 TRY(AppendEntry(sector, key_descriptor, key, value, new_state));
605
David Rogersf56131c2020-03-04 10:19:22 -0800606 for (auto& address : old_key_descriptor.addresses()) {
607 SectorFromAddress(address)->RemoveValidBytes(original_entry.size());
608 }
609
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800610 return Status::OK;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800611}
612
613Status KeyValueStore::WriteEntryForNewKey(string_view key,
614 span<const byte> value) {
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800615 if (key_descriptors_.full()) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800616 WRN("KVS full: trying to store a new entry, but can't. Have %zu entries",
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800617 key_descriptors_.size());
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800618 return Status::RESOURCE_EXHAUSTED;
619 }
620
David Rogers2761aeb2020-01-31 17:09:00 -0800621 SectorDescriptor* sector;
Wyatt Hepler5406a672020-02-18 15:42:38 -0800622 TRY(FindOrRecoverSectorWithSpace(&sector,
623 Entry::size(partition_, key, value)));
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800624 DBG("Writing new entry; found sector: %u", SectorIndex(sector));
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800625
626 // Create the KeyDescriptor that will be added to the list. The transaction ID
627 // and address will be set by AppendEntry.
628 KeyDescriptor key_descriptor(key);
Wyatt Hepler0af6ad92020-02-13 15:54:46 -0800629 TRY(AppendEntry(sector, &key_descriptor, key, value, KeyDescriptor::kValid));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800630
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800631 // Only add the entry when we are certain the write succeeded.
632 key_descriptors_.push_back(key_descriptor);
Wyatt Heplerb7609542020-01-24 10:29:54 -0800633 return Status::OK;
634}
635
David Rogersf56131c2020-03-04 10:19:22 -0800636Status KeyValueStore::RelocateEntry(KeyDescriptor& key_descriptor,
637 KeyValueStore::Address address) {
David Rogersf0a35442020-02-04 12:16:38 -0800638 struct TempEntry {
Wyatt Heplere541e072020-02-14 09:10:53 -0800639 Entry::KeyBuffer key;
Wyatt Heplerce7b8df2020-02-21 10:38:39 -0800640 std::array<byte, sizeof(working_buffer_) - sizeof(key)> value;
David Rogersf0a35442020-02-04 12:16:38 -0800641 };
Wyatt Heplerce7b8df2020-02-21 10:38:39 -0800642 auto [key_buffer, value_buffer] =
643 *std::launder(reinterpret_cast<TempEntry*>(working_buffer_.data()));
David Rogersf0a35442020-02-04 12:16:38 -0800644
David Rogersf56131c2020-03-04 10:19:22 -0800645 DBG("Relocating entry at %zx for key %#010" PRIx32,
646 size_t(address),
Wyatt Heplerce7b8df2020-02-21 10:38:39 -0800647 key_descriptor.hash());
David Rogersdf025cd2020-02-06 17:05:34 -0800648
Wyatt Heplere541e072020-02-14 09:10:53 -0800649 // Read the entry to be relocated. Store the entry in a local variable and
David Rogersf0a35442020-02-04 12:16:38 -0800650 // store the key and value in the TempEntry stored in the static allocated
651 // working_buffer_.
Wyatt Heplere541e072020-02-14 09:10:53 -0800652 Entry entry;
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800653 TRY(Entry::Read(partition_, key_descriptor.address(), &entry));
Wyatt Heplerce7b8df2020-02-21 10:38:39 -0800654
655 TRY_ASSIGN(size_t key_length, entry.ReadKey(key_buffer));
656 string_view key = string_view(key_buffer.data(), key_length);
657
658 StatusWithSize result = entry.ReadValue(value_buffer);
659 if (!result.ok()) {
David Rogersf0a35442020-02-04 12:16:38 -0800660 return Status::INTERNAL;
661 }
662
Wyatt Heplerce7b8df2020-02-21 10:38:39 -0800663 const span value = span(value_buffer.data(), result.size());
664 TRY(entry.VerifyChecksum(entry_header_format_.checksum, key, value));
David Rogersf0a35442020-02-04 12:16:38 -0800665
David Rogerscd87c322020-02-27 14:04:08 -0800666 // Find a new sector for the entry and write it to the new location. For
667 // relocation the find should not not be a sector already containing the key
668 // but can be the always empty sector, since this is part of the GC process
669 // that will result in a new empty sector. Also find a sector that does not
670 // have reclaimable space (mostly for the full GC, where that would result in
671 // an immediate extra relocation).
David Rogers8ce55cd2020-02-04 19:41:48 -0800672 SectorDescriptor* new_sector;
David Rogerscd87c322020-02-27 14:04:08 -0800673
David Rogersf56131c2020-03-04 10:19:22 -0800674 // Build a vector of sectors to avoid.
675 Vector<SectorDescriptor*, internal::kEntryRedundancy> old_sectors;
676 for (auto& address : key_descriptor.addresses()) {
677 old_sectors.push_back(SectorFromAddress(address));
678 }
679
680 // TODO: Remove this once const span can take a non-const span.
681 auto old_sectors_const =
682 span(const_cast<const SectorDescriptor**>(old_sectors.data()),
683 old_sectors.size());
684
David Rogerscd87c322020-02-27 14:04:08 -0800685 TRY(FindSectorWithSpace(
David Rogersf56131c2020-03-04 10:19:22 -0800686 &new_sector, entry.size(), kGarbageCollect, old_sectors_const));
687
688 // TODO: This does an entry with new transaction ID. This needs to get changed
689 // to be a copy of this entry with the same transaction ID.
Wyatt Heplerce7b8df2020-02-21 10:38:39 -0800690 TRY(AppendEntry(
691 new_sector, &key_descriptor, key, value, key_descriptor.state()));
David Rogersdf025cd2020-02-06 17:05:34 -0800692
Wyatt Heplerd2298282020-02-20 17:12:45 -0800693 // Do the valid bytes accounting for the sector the entry was relocated from.
David Rogersf56131c2020-03-04 10:19:22 -0800694 // TODO: AppendEntry() creates an entry with new transaction ID. While that is
695 // used all the old sectors need the valid bytes to be removed. Once it is
696 // switched over to do a copy of the current entry with the same transaction
697 // ID, then the valid bytes need to be removed from only the one sector being
698 // relocated out of.
699 // SectorFromAddress(address)->RemoveValidBytes(entry.size());
700 (void)address;
701 for (auto& old_sector : old_sectors) {
702 old_sector->RemoveValidBytes(entry.size());
703 }
David Rogersdf025cd2020-02-06 17:05:34 -0800704
705 return Status::OK;
David Rogersa12786b2020-01-31 16:02:33 -0800706}
707
David Rogers8db5a722020-02-03 18:28:34 -0800708// Find either an existing sector with enough space that is not the sector to
709// skip, or an empty sector. Maintains the invariant that there is always at
David Rogersc8fe1f52020-02-27 14:04:08 -0800710// least 1 empty sector except during GC. On GC, skip sectors that have
711// reclaimable bytes.
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800712Status KeyValueStore::FindSectorWithSpace(
713 SectorDescriptor** found_sector,
714 size_t size,
David Rogersc8fe1f52020-02-27 14:04:08 -0800715 FindSectorMode find_mode,
716 span<const SectorDescriptor*> sectors_to_skip) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800717 SectorDescriptor* first_empty_sector = nullptr;
David Rogersc8fe1f52020-02-27 14:04:08 -0800718 bool at_least_two_empty_sectors = (find_mode == kGarbageCollect);
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800719
720 DBG("Find sector with %zu bytes available, starting with sector %u",
721 size,
722 SectorIndex(last_new_sector_));
David Rogerscd87c322020-02-27 14:04:08 -0800723 for (auto& skip_sector : sectors_to_skip) {
724 DBG(" Skip sector %u", SectorIndex(skip_sector));
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800725 }
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800726
David Rogers8ce55cd2020-02-04 19:41:48 -0800727 // The last_new_sector_ is the sector that was last selected as the "new empty
728 // sector" to write to. This last new sector is used as the starting point for
729 // the next "find a new empty sector to write to" operation. By using the last
730 // new sector as the start point we will cycle which empty sector is selected
731 // next, spreading the wear across all the empty sectors and get a wear
732 // leveling benefit, rather than putting more wear on the lower number
733 // sectors.
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800734 SectorDescriptor* sector = last_new_sector_;
David Rogers67f4b6c2020-02-06 16:17:09 -0800735
David Rogerscd87c322020-02-27 14:04:08 -0800736 // Look for a sector to use with enough space. The search uses a 2 priority
737 // tier process.
738 //
David Rogersc8fe1f52020-02-27 14:04:08 -0800739 // Tier 1 is sector that already has valid data. During GC only select a
740 // sector that has no reclaimable bytes. Immediately use the first matching
741 // sector that is found.
David Rogerscd87c322020-02-27 14:04:08 -0800742 //
David Rogersc8fe1f52020-02-27 14:04:08 -0800743 // Tier 2 is find sectors that are empty/erased. While scanning for a partial
744 // sector, keep track of the first empty sector and if a second empty sector
745 // was seen. If during GC then count the second empty sector as always seen.
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800746 for (size_t j = 0; j < sectors_.size(); j++) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800747 sector += 1;
748 if (sector == sectors_.end()) {
749 sector = sectors_.begin();
750 }
Keir Mierle8c352dc2020-02-02 13:58:19 -0800751
David Rogerscd87c322020-02-27 14:04:08 -0800752 if (std::find(sectors_to_skip.begin(), sectors_to_skip.end(), sector) !=
753 sectors_to_skip.end()) {
David Rogers8db5a722020-02-03 18:28:34 -0800754 continue;
755 }
756
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800757 const size_t sector_size_bytes = partition_.sector_size_bytes();
David Rogerscd87c322020-02-27 14:04:08 -0800758 if (!sector->Empty(sector_size_bytes) && sector->HasSpace(size) &&
David Rogersc8fe1f52020-02-27 14:04:08 -0800759 ((find_mode == kAppendEntry) ||
David Rogerscd87c322020-02-27 14:04:08 -0800760 (sector->RecoverableBytes(sector_size_bytes) == 0))) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800761 *found_sector = sector;
David Rogers8ce55cd2020-02-04 19:41:48 -0800762 return Status::OK;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800763 }
764
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800765 if (sector->Empty(sector_size_bytes)) {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800766 if (first_empty_sector == nullptr) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800767 first_empty_sector = sector;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800768 } else {
769 at_least_two_empty_sectors = true;
Wyatt Hepler2ad60672020-01-21 08:00:16 -0800770 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800771 }
772 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800773
David Rogers8ce55cd2020-02-04 19:41:48 -0800774 // If the scan for a partial sector does not find a suitable sector, use the
775 // first empty sector that was found. Normally it is required to keep 1 empty
David Rogersc8fe1f52020-02-27 14:04:08 -0800776 // sector after the sector found here, but that rule does not apply during GC.
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800777 if (at_least_two_empty_sectors) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800778 DBG(" Found a usable empty sector; returning the first found (%u)",
David Rogers8ce55cd2020-02-04 19:41:48 -0800779 SectorIndex(first_empty_sector));
780 last_new_sector_ = first_empty_sector;
781 *found_sector = first_empty_sector;
782 return Status::OK;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800783 }
David Rogers8ce55cd2020-02-04 19:41:48 -0800784
785 // No sector was found.
David Rogers67f4b6c2020-02-06 16:17:09 -0800786 DBG(" Unable to find a usable sector");
David Rogers8ce55cd2020-02-04 19:41:48 -0800787 *found_sector = nullptr;
788 return Status::RESOURCE_EXHAUSTED;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800789}
790
David Rogers2761aeb2020-01-31 17:09:00 -0800791Status KeyValueStore::FindOrRecoverSectorWithSpace(SectorDescriptor** sector,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800792 size_t size) {
David Rogersc8fe1f52020-02-27 14:04:08 -0800793 Status result = FindSectorWithSpace(sector, size, kAppendEntry);
David Rogers890acb52020-02-28 09:06:50 -0800794 if (result == Status::RESOURCE_EXHAUSTED &&
795 options_.gc_on_write != GargbageCollectOnWrite::kDisabled) {
David Rogers1541d612020-02-06 23:47:02 -0800796 // Garbage collect and then try again to find the best sector.
David Rogerscd87c322020-02-27 14:04:08 -0800797 TRY(GarbageCollectPartial());
David Rogersc8fe1f52020-02-27 14:04:08 -0800798 return FindSectorWithSpace(sector, size, kAppendEntry);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800799 }
David Rogers8ce55cd2020-02-04 19:41:48 -0800800 return result;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800801}
802
David Rogers2761aeb2020-01-31 17:09:00 -0800803KeyValueStore::SectorDescriptor* KeyValueStore::FindSectorToGarbageCollect() {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800804 const size_t sector_size_bytes = partition_.sector_size_bytes();
David Rogers2761aeb2020-01-31 17:09:00 -0800805 SectorDescriptor* sector_candidate = nullptr;
David Rogersa12786b2020-01-31 16:02:33 -0800806 size_t candidate_bytes = 0;
807
808 // Step 1: Try to find a sectors with stale keys and no valid keys (no
809 // relocation needed). If any such sectors are found, use the sector with the
810 // most reclaimable bytes.
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800811 for (auto& sector : sectors_) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800812 if ((sector.valid_bytes() == 0) &&
813 (sector.RecoverableBytes(sector_size_bytes) > candidate_bytes)) {
David Rogersa12786b2020-01-31 16:02:33 -0800814 sector_candidate = &sector;
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800815 candidate_bytes = sector.RecoverableBytes(sector_size_bytes);
David Rogersa12786b2020-01-31 16:02:33 -0800816 }
817 }
818
819 // Step 2: If step 1 yields no sectors, just find the sector with the most
820 // reclaimable bytes.
821 if (sector_candidate == nullptr) {
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800822 for (auto& sector : sectors_) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800823 if (sector.RecoverableBytes(sector_size_bytes) > candidate_bytes) {
David Rogersa12786b2020-01-31 16:02:33 -0800824 sector_candidate = &sector;
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800825 candidate_bytes = sector.RecoverableBytes(sector_size_bytes);
David Rogersa12786b2020-01-31 16:02:33 -0800826 }
827 }
828 }
829
David Rogers5981f312020-02-13 13:33:56 -0800830 if (sector_candidate != nullptr) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800831 DBG("Found sector %u to Garbage Collect, %zu recoverable bytes",
David Rogers5981f312020-02-13 13:33:56 -0800832 SectorIndex(sector_candidate),
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800833 sector_candidate->RecoverableBytes(sector_size_bytes));
David Rogers5981f312020-02-13 13:33:56 -0800834 } else {
835 DBG("Unable to find sector to garbage collect!");
836 }
David Rogersa12786b2020-01-31 16:02:33 -0800837 return sector_candidate;
838}
839
David Rogerscd87c322020-02-27 14:04:08 -0800840Status KeyValueStore::GarbageCollectFull() {
841 DBG("Garbage Collect all sectors");
David Rogerscd87c322020-02-27 14:04:08 -0800842 SectorDescriptor* sector = last_new_sector_;
843
844 // TODO: look in to making an iterator method for cycling through sectors
845 // starting from last_new_sector_.
846 for (size_t j = 0; j < sectors_.size(); j++) {
847 sector += 1;
848 if (sector == sectors_.end()) {
849 sector = sectors_.begin();
850 }
851
852 if (sector->RecoverableBytes(partition_.sector_size_bytes()) > 0) {
853 TRY(GarbageCollectSector(sector));
854 }
855 }
856
857 DBG("Garbage Collect all complete");
David Rogerscd87c322020-02-27 14:04:08 -0800858 return Status::OK;
859}
860
861Status KeyValueStore::GarbageCollectPartial() {
David Rogers67f4b6c2020-02-06 16:17:09 -0800862 DBG("Garbage Collect a single sector");
863
David Rogersa12786b2020-01-31 16:02:33 -0800864 // Step 1: Find the sector to garbage collect
David Rogers2761aeb2020-01-31 17:09:00 -0800865 SectorDescriptor* sector_to_gc = FindSectorToGarbageCollect();
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800866
David Rogersa12786b2020-01-31 16:02:33 -0800867 if (sector_to_gc == nullptr) {
David Rogerscd87c322020-02-27 14:04:08 -0800868 // Nothing to GC, all done.
869 return Status::OK;
David Rogersa12786b2020-01-31 16:02:33 -0800870 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800871
David Rogerscd87c322020-02-27 14:04:08 -0800872 TRY(GarbageCollectSector(sector_to_gc));
David Rogerscd87c322020-02-27 14:04:08 -0800873 return Status::OK;
874}
875
876Status KeyValueStore::GarbageCollectSector(SectorDescriptor* sector_to_gc) {
877 // Step 1: Move any valid entries in the GC sector to other sectors
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800878 if (sector_to_gc->valid_bytes() != 0) {
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800879 for (auto& descriptor : key_descriptors_) {
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800880 if (AddressInSector(*sector_to_gc, descriptor.address())) {
David Rogers67f4b6c2020-02-06 16:17:09 -0800881 DBG(" Relocate entry");
David Rogersf56131c2020-03-04 10:19:22 -0800882 TRY(RelocateEntry(descriptor, descriptor.address()));
David Rogersa12786b2020-01-31 16:02:33 -0800883 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800884 }
885 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800886
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800887 if (sector_to_gc->valid_bytes() != 0) {
David Rogers67f4b6c2020-02-06 16:17:09 -0800888 ERR(" Failed to relocate valid entries from sector being garbage "
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800889 "collected, %zu valid bytes remain",
890 sector_to_gc->valid_bytes());
Wyatt Heplerb7609542020-01-24 10:29:54 -0800891 return Status::INTERNAL;
892 }
893
David Rogerscd87c322020-02-27 14:04:08 -0800894 // Step 2: Reinitialize the sector
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800895 sector_to_gc->set_writable_bytes(0);
David Rogersa12786b2020-01-31 16:02:33 -0800896 TRY(partition_.Erase(SectorBaseAddress(sector_to_gc), 1));
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800897 sector_to_gc->set_writable_bytes(partition_.sector_size_bytes());
Wyatt Heplerb7609542020-01-24 10:29:54 -0800898
David Rogerscd87c322020-02-27 14:04:08 -0800899 DBG(" Garbage Collect sector %u complete", SectorIndex(sector_to_gc));
David Rogersa12786b2020-01-31 16:02:33 -0800900 return Status::OK;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800901}
902
David Rogers2761aeb2020-01-31 17:09:00 -0800903Status KeyValueStore::AppendEntry(SectorDescriptor* sector,
904 KeyDescriptor* key_descriptor,
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800905 string_view key,
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800906 span<const byte> value,
907 KeyDescriptor::State new_state) {
Wyatt Heplere541e072020-02-14 09:10:53 -0800908 const Address address = NextWritableAddress(sector);
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800909 Entry entry = CreateEntry(address, key, value, new_state);
Wyatt Heplere541e072020-02-14 09:10:53 -0800910
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800911 DBG("Appending %zu B entry with transaction ID %" PRIu32 " to address %#zx",
Wyatt Heplere541e072020-02-14 09:10:53 -0800912 entry.size(),
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800913 entry.transaction_id(),
914 size_t(address));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800915
David Rogers6592d292020-02-14 14:19:26 -0800916 StatusWithSize result = entry.Write(key, value);
917 // Remove any bytes that were written, even if the write was not successful.
Keir Mierle0a52aed2020-02-21 09:24:36 -0800918 // This is important to retain the writable space invariant on the sectors.
David Rogers6592d292020-02-14 14:19:26 -0800919 sector->RemoveWritableBytes(result.size());
920
921 if (!result.ok()) {
Keir Mierle0a52aed2020-02-21 09:24:36 -0800922 ERR("Failed to write %zu bytes at %" PRIx32 ". %zu actually written",
David Rogers6592d292020-02-14 14:19:26 -0800923 entry.size(),
Keir Mierle0a52aed2020-02-21 09:24:36 -0800924 address,
David Rogers6592d292020-02-14 14:19:26 -0800925 result.size());
926 return result.status();
927 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800928
929 if (options_.verify_on_write) {
Wyatt Heplere541e072020-02-14 09:10:53 -0800930 TRY(entry.VerifyChecksumInFlash(entry_header_format_.checksum));
Wyatt Heplerb7609542020-01-24 10:29:54 -0800931 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800932
Keir Mierle9e38b402020-02-21 13:06:21 -0800933 // Entry was written successfully; update the key descriptor and the sector
934 // descriptor to reflect the new entry.
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800935 entry.UpdateDescriptor(key_descriptor);
David Rogers6592d292020-02-14 14:19:26 -0800936 sector->AddValidBytes(result.size());
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800937 return Status::OK;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800938}
939
Wyatt Heplerbdd8e5a2020-02-20 19:27:26 -0800940KeyValueStore::Entry KeyValueStore::CreateEntry(Address address,
941 std::string_view key,
942 span<const byte> value,
943 KeyDescriptor::State state) {
Keir Mierle9e38b402020-02-21 13:06:21 -0800944 // Always bump the transaction ID when creating a new entry.
945 //
946 // Burning transaction IDs prevents inconsistencies between flash and memory
947 // that which could happen if a write succeeds, but for some reason the read
948 // and verify step fails. Here's how this would happen:
949 //
950 // 1. The entry is written but for some reason the flash reports failure OR
951 // The write succeeds, but the read / verify operation fails.
952 // 2. The transaction ID is NOT incremented, because of the failure
953 // 3. (later) A new entry is written, re-using the transaction ID (oops)
954 //
955 // By always burning transaction IDs, the above problem can't happen.
956 last_transaction_id_ += 1;
957
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800958 if (state == KeyDescriptor::kDeleted) {
Wyatt Hepler7465be32020-02-21 15:30:53 -0800959 return Entry::Tombstone(
960 partition_, address, entry_header_format_, key, last_transaction_id_);
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800961 }
962 return Entry::Valid(partition_,
963 address,
Wyatt Hepler88adfe82020-02-20 19:33:27 -0800964 entry_header_format_,
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800965 key,
966 value,
Keir Mierle9e38b402020-02-21 13:06:21 -0800967 last_transaction_id_);
Wyatt Heplerd2298282020-02-20 17:12:45 -0800968}
969
970void KeyValueStore::Reset() {
971 initialized_ = false;
972 key_descriptors_.clear();
973 last_new_sector_ = nullptr;
974 last_transaction_id_ = 0;
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800975}
976
Keir Mierle8c352dc2020-02-02 13:58:19 -0800977void KeyValueStore::LogDebugInfo() {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800978 const size_t sector_size_bytes = partition_.sector_size_bytes();
979 DBG("====================== KEY VALUE STORE DUMP =========================");
980 DBG(" ");
981 DBG("Flash partition:");
Wyatt Heplerad0a7932020-02-06 08:20:38 -0800982 DBG(" Sector count = %zu", partition_.sector_count());
Wyatt Hepler38ce30f2020-02-19 11:48:31 -0800983 DBG(" Sector max count = %zu", sectors_.max_size());
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800984 DBG(" Sectors in use = %zu", sectors_.size());
Keir Mierle8c352dc2020-02-02 13:58:19 -0800985 DBG(" Sector size = %zu", sector_size_bytes);
986 DBG(" Total size = %zu", partition_.size_bytes());
987 DBG(" Alignment = %zu", partition_.alignment_bytes());
988 DBG(" ");
989 DBG("Key descriptors:");
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800990 DBG(" Entry count = %zu", key_descriptors_.size());
Wyatt Hepler38ce30f2020-02-19 11:48:31 -0800991 DBG(" Max entry count = %zu", key_descriptors_.max_size());
Keir Mierle8c352dc2020-02-02 13:58:19 -0800992 DBG(" ");
993 DBG(" # hash version address address (hex)");
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800994 for (size_t i = 0; i < key_descriptors_.size(); ++i) {
995 const KeyDescriptor& kd = key_descriptors_[i];
Keir Mierle8c352dc2020-02-02 13:58:19 -0800996 DBG(" |%3zu: | %8zx |%8zu | %8zu | %8zx",
997 i,
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800998 size_t(kd.hash()),
999 size_t(kd.transaction_id()),
1000 size_t(kd.address()),
1001 size_t(kd.address()));
Keir Mierle8c352dc2020-02-02 13:58:19 -08001002 }
1003 DBG(" ");
1004
1005 DBG("Sector descriptors:");
1006 DBG(" # tail free valid has_space");
Wyatt Hepler1c329ca2020-02-07 18:07:23 -08001007 for (size_t sector_id = 0; sector_id < sectors_.size(); ++sector_id) {
1008 const SectorDescriptor& sd = sectors_[sector_id];
Keir Mierle8c352dc2020-02-02 13:58:19 -08001009 DBG(" |%3zu: | %8zu |%8zu | %s",
1010 sector_id,
Wyatt Hepler2c7eca02020-02-18 16:01:42 -08001011 size_t(sd.writable_bytes()),
1012 sd.valid_bytes(),
1013 sd.writable_bytes() ? "YES" : "");
Keir Mierle8c352dc2020-02-02 13:58:19 -08001014 }
1015 DBG(" ");
1016
1017 // TODO: This should stop logging after some threshold.
1018 // size_t dumped_bytes = 0;
1019 DBG("Sector raw data:");
Wyatt Hepler1c329ca2020-02-07 18:07:23 -08001020 for (size_t sector_id = 0; sector_id < sectors_.size(); ++sector_id) {
Keir Mierle8c352dc2020-02-02 13:58:19 -08001021 // Read sector data. Yes, this will blow the stack on embedded.
Wyatt Hepler1c329ca2020-02-07 18:07:23 -08001022 std::array<byte, 500> raw_sector_data; // TODO!!!
Keir Mierle8c352dc2020-02-02 13:58:19 -08001023 StatusWithSize sws =
1024 partition_.Read(sector_id * sector_size_bytes, raw_sector_data);
1025 DBG("Read: %zu bytes", sws.size());
1026
1027 DBG(" base addr offs 0 1 2 3 4 5 6 7");
1028 for (size_t i = 0; i < sector_size_bytes; i += 8) {
1029 DBG(" %3zu %8zx %5zu | %02x %02x %02x %02x %02x %02x %02x %02x",
1030 sector_id,
1031 (sector_id * sector_size_bytes) + i,
1032 i,
1033 static_cast<unsigned int>(raw_sector_data[i + 0]),
1034 static_cast<unsigned int>(raw_sector_data[i + 1]),
1035 static_cast<unsigned int>(raw_sector_data[i + 2]),
1036 static_cast<unsigned int>(raw_sector_data[i + 3]),
1037 static_cast<unsigned int>(raw_sector_data[i + 4]),
1038 static_cast<unsigned int>(raw_sector_data[i + 5]),
1039 static_cast<unsigned int>(raw_sector_data[i + 6]),
1040 static_cast<unsigned int>(raw_sector_data[i + 7]));
1041
1042 // TODO: Fix exit condition.
1043 if (i > 128) {
1044 break;
1045 }
1046 }
1047 DBG(" ");
1048 }
1049
1050 DBG("////////////////////// KEY VALUE STORE DUMP END /////////////////////");
1051}
1052
David Rogerscf680ab2020-02-12 23:28:32 -08001053void KeyValueStore::LogSectors() const {
1054 DBG("Sector descriptors: count %zu", sectors_.size());
Wyatt Hepler1c329ca2020-02-07 18:07:23 -08001055 for (auto& sector : sectors_) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -08001056 DBG(" - Sector %u: valid %zu, recoverable %zu, free %zu",
David Rogers50185ad2020-02-07 00:02:46 -08001057 SectorIndex(&sector),
Wyatt Hepler2c7eca02020-02-18 16:01:42 -08001058 sector.valid_bytes(),
1059 sector.RecoverableBytes(partition_.sector_size_bytes()),
1060 sector.writable_bytes());
David Rogers50185ad2020-02-07 00:02:46 -08001061 }
1062}
1063
David Rogerscf680ab2020-02-12 23:28:32 -08001064void KeyValueStore::LogKeyDescriptor() const {
1065 DBG("Key descriptors: count %zu", key_descriptors_.size());
1066 for (auto& key : key_descriptors_) {
Wyatt Hepler1fc11042020-02-19 17:17:51 -08001067 DBG(" - Key: %s, hash %#zx, transaction ID %zu, address %#zx",
David Rogerscf680ab2020-02-12 23:28:32 -08001068 key.deleted() ? "Deleted" : "Valid",
Wyatt Hepler1fc11042020-02-19 17:17:51 -08001069 static_cast<size_t>(key.hash()),
1070 static_cast<size_t>(key.transaction_id()),
1071 static_cast<size_t>(key.address()));
David Rogerscf680ab2020-02-12 23:28:32 -08001072 }
1073}
1074
Wyatt Hepler2ad60672020-01-21 08:00:16 -08001075} // namespace pw::kvs