blob: 50af414aa3d4fded4aabc5011d8c8191e359b736 [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 Hepler4da1fcb2020-01-30 17:32:18 -080023#include "pw_kvs_private/macros.h"
Keir Mierle8c352dc2020-02-02 13:58:19 -080024#include "pw_log/log.h"
Wyatt Heplerb7609542020-01-24 10:29:54 -080025
Wyatt Hepler2ad60672020-01-21 08:00:16 -080026namespace pw::kvs {
Wyatt Heplera00d1ef2020-02-14 14:31:26 -080027namespace {
Wyatt Heplerb7609542020-01-24 10:29:54 -080028
Wyatt Hepleracaacf92020-01-24 10:58:30 -080029using std::byte;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080030using std::string_view;
Wyatt Hepleracaacf92020-01-24 10:58:30 -080031
Wyatt Heplera00d1ef2020-02-14 14:31:26 -080032constexpr bool InvalidKey(std::string_view key) {
Wyatt Heplerbdd8e5a2020-02-20 19:27:26 -080033 return key.empty() || (key.size() > internal::Entry::kMaxKeyLength);
Wyatt Heplera00d1ef2020-02-14 14:31:26 -080034}
35
Wyatt Heplerab3b2492020-03-11 16:15:16 -070036// Returns true if the container conatins the value.
37// TODO: At some point move this to pw_containers, along with adding tests.
38template <typename Container, typename T>
39bool Contains(const Container& container, const T& value) {
40 return std::find(std::begin(container), std::end(container), value) !=
41 std::end(container);
42}
43
Wyatt Heplera00d1ef2020-02-14 14:31:26 -080044} // namespace
45
Wyatt Heplerad0a7932020-02-06 08:20:38 -080046KeyValueStore::KeyValueStore(FlashPartition* partition,
Wyatt Hepler22d0d9f2020-03-05 14:57:11 -080047 span<const EntryFormat> formats,
Wyatt Hepler7ded6da2020-03-11 18:24:43 -070048 const Options& options,
49 size_t redundancy,
50 Vector<SectorDescriptor>& sector_descriptor_list,
51 const SectorDescriptor** temp_sectors_to_skip,
52 Vector<KeyDescriptor>& key_descriptor_list,
53 Address* addresses)
Wyatt Heplerad0a7932020-02-06 08:20:38 -080054 : partition_(*partition),
Wyatt Hepler22d0d9f2020-03-05 14:57:11 -080055 formats_(formats),
Wyatt Hepler7ded6da2020-03-11 18:24:43 -070056 entry_cache_(key_descriptor_list, addresses, redundancy),
Wyatt Heplerd2298282020-02-20 17:12:45 -080057 sectors_(sector_descriptor_list),
Wyatt Hepler7ded6da2020-03-11 18:24:43 -070058 temp_sectors_to_skip_(temp_sectors_to_skip),
David Rogers49766d92020-03-20 10:55:54 -070059 options_(options),
60 initialized_(false),
61 error_detected_(false),
62 last_new_sector_(nullptr),
63 last_transaction_id_(0) {}
Wyatt Heplerad0a7932020-02-06 08:20:38 -080064
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080065Status KeyValueStore::Init() {
Keir Mierlebf904812020-03-11 17:28:22 -070066 initialized_ = false;
David Rogers49766d92020-03-20 10:55:54 -070067 error_detected_ = false;
Keir Mierlebf904812020-03-11 17:28:22 -070068 last_new_sector_ = nullptr;
69 last_transaction_id_ = 0;
Wyatt Hepler7ded6da2020-03-11 18:24:43 -070070 entry_cache_.Reset();
Wyatt Heplerd2298282020-02-20 17:12:45 -080071
David Rogers2e9e0c82020-02-13 15:06:06 -080072 INF("Initializing key value store");
Wyatt Hepler38ce30f2020-02-19 11:48:31 -080073 if (partition_.sector_count() > sectors_.max_size()) {
David Rogers2e9e0c82020-02-13 15:06:06 -080074 ERR("KVS init failed: kMaxUsableSectors (=%zu) must be at least as "
75 "large as the number of sectors in the flash partition (=%zu)",
Wyatt Hepler38ce30f2020-02-19 11:48:31 -080076 sectors_.max_size(),
David Rogers2e9e0c82020-02-13 15:06:06 -080077 partition_.sector_count());
Wyatt Heplerad0a7932020-02-06 08:20:38 -080078 return Status::FAILED_PRECONDITION;
79 }
80
Keir Mierle8c352dc2020-02-02 13:58:19 -080081 const size_t sector_size_bytes = partition_.sector_size_bytes();
Keir Mierle8c352dc2020-02-02 13:58:19 -080082
David Rogers49766d92020-03-20 10:55:54 -070083 // TODO: investigate doing this as a static assert/compile-time check.
84 if (sector_size_bytes > SectorDescriptor::max_sector_size()) {
85 ERR("KVS init failed: sector_size_bytes (=%zu) is greater than maximum "
86 "allowed sector size (=%zu)",
87 sector_size_bytes,
88 SectorDescriptor::max_sector_size());
89 return Status::FAILED_PRECONDITION;
90 }
91
Keir Mierle8c352dc2020-02-02 13:58:19 -080092 DBG("First pass: Read all entries from all sectors");
Wyatt Hepler2c7eca02020-02-18 16:01:42 -080093 Address sector_address = 0;
Keir Mierle8c352dc2020-02-02 13:58:19 -080094
Wyatt Heplerd2298282020-02-20 17:12:45 -080095 sectors_.assign(partition_.sector_count(),
96 SectorDescriptor(sector_size_bytes));
97
Alexei Frolovd4adf912020-02-21 13:29:15 -080098 size_t total_corrupt_bytes = 0;
99 int corrupt_entries = 0;
David Rogers91627482020-02-27 17:38:12 -0800100 bool empty_sector_found = false;
Alexei Frolovd4adf912020-02-21 13:29:15 -0800101
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800102 for (SectorDescriptor& sector : sectors_) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800103 Address entry_address = sector_address;
104
Alexei Frolovd4adf912020-02-21 13:29:15 -0800105 size_t sector_corrupt_bytes = 0;
106
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800107 for (int num_entries_in_sector = 0; true; num_entries_in_sector++) {
108 DBG("Load entry: sector=%" PRIx32 ", entry#=%d, address=%" PRIx32,
109 sector_address,
Keir Mierle8c352dc2020-02-02 13:58:19 -0800110 num_entries_in_sector,
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800111 entry_address);
Keir Mierle8c352dc2020-02-02 13:58:19 -0800112
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800113 if (!AddressInSector(sector, entry_address)) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800114 DBG("Fell off end of sector; moving to the next sector");
115 break;
116 }
117
118 Address next_entry_address;
119 Status status = LoadEntry(entry_address, &next_entry_address);
120 if (status == Status::NOT_FOUND) {
121 DBG("Hit un-written data in sector; moving to the next sector");
122 break;
123 }
124 if (status == Status::DATA_LOSS) {
Alexei Frolovd4adf912020-02-21 13:29:15 -0800125 // The entry could not be read, indicating data corruption within the
126 // sector. Try to scan the remainder of the sector for other entries.
David Rogersa2562b52020-03-05 15:30:05 -0800127 WRN("KVS init: data loss detected in sector %u at address %zu",
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800128 SectorIndex(&sector),
129 size_t(entry_address));
Alexei Frolovd4adf912020-02-21 13:29:15 -0800130
David Rogers49766d92020-03-20 10:55:54 -0700131 error_detected_ = true;
Alexei Frolovd4adf912020-02-21 13:29:15 -0800132 corrupt_entries++;
133
134 status = ScanForEntry(sector,
135 entry_address + Entry::kMinAlignmentBytes,
136 &next_entry_address);
137 if (status == Status::NOT_FOUND) {
138 // No further entries in this sector. Mark the remaining bytes in the
139 // sector as corrupt (since we can't reliably know the size of the
140 // corrupt entry).
141 sector_corrupt_bytes +=
142 sector_size_bytes - (entry_address - sector_address);
143 break;
144 }
145
146 if (!status.ok()) {
147 ERR("Unexpected error in KVS initialization: %s", status.str());
148 return Status::UNKNOWN;
149 }
150
151 sector_corrupt_bytes += next_entry_address - entry_address;
152 } else if (!status.ok()) {
153 ERR("Unexpected error in KVS initialization: %s", status.str());
154 return Status::UNKNOWN;
Keir Mierle8c352dc2020-02-02 13:58:19 -0800155 }
Keir Mierle8c352dc2020-02-02 13:58:19 -0800156
157 // Entry loaded successfully; so get ready to load the next one.
158 entry_address = next_entry_address;
159
160 // Update of the number of writable bytes in this sector.
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800161 sector.set_writable_bytes(sector_size_bytes -
162 (entry_address - sector_address));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800163 }
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800164
Alexei Frolovd4adf912020-02-21 13:29:15 -0800165 if (sector_corrupt_bytes > 0) {
166 // If the sector contains corrupt data, prevent any further entries from
167 // being written to it by indicating that it has no space. This should
168 // also make it a decent GC candidate. Valid keys in the sector are still
169 // readable as normal.
David Rogers49766d92020-03-20 10:55:54 -0700170 sector.mark_corrupt();
171 error_detected_ = true;
Alexei Frolovd4adf912020-02-21 13:29:15 -0800172
173 WRN("Sector %u contains %zuB of corrupt data",
174 SectorIndex(&sector),
175 sector_corrupt_bytes);
176 }
177
David Rogers91627482020-02-27 17:38:12 -0800178 if (sector.Empty(sector_size_bytes)) {
179 empty_sector_found = true;
180 }
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800181 sector_address += sector_size_bytes;
Alexei Frolovd4adf912020-02-21 13:29:15 -0800182 total_corrupt_bytes += sector_corrupt_bytes;
Keir Mierle8c352dc2020-02-02 13:58:19 -0800183 }
184
185 DBG("Second pass: Count valid bytes in each sector");
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700186 Address newest_key = 0;
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800187
Wyatt Hepler02946272020-03-18 10:36:22 -0700188 // For every valid entry, count the valid bytes in that sector. Track which
189 // entry has the newest transaction ID for initializing last_new_sector_.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700190 for (const EntryMetadata& metadata : entry_cache_) {
David Rogers49766d92020-03-20 10:55:54 -0700191 if (metadata.addresses().size() < redundancy()) {
192 error_detected_ = true;
193 }
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700194 for (Address address : metadata.addresses()) {
David Rogersf56131c2020-03-04 10:19:22 -0800195 Entry entry;
David Rogersa2562b52020-03-05 15:30:05 -0800196 TRY(Entry::Read(partition_, address, formats_, &entry));
David Rogersf56131c2020-03-04 10:19:22 -0800197 SectorFromAddress(address)->AddValidBytes(entry.size());
198 }
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700199 if (metadata.IsNewerThan(last_transaction_id_)) {
200 last_transaction_id_ = metadata.transaction_id();
201 newest_key = metadata.addresses().back();
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800202 }
Keir Mierle8c352dc2020-02-02 13:58:19 -0800203 }
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800204
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700205 last_new_sector_ = SectorFromAddress(newest_key);
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800206
David Rogers49766d92020-03-20 10:55:54 -0700207 if (error_detected_) {
208 Status recovery_status = Repair();
209 if (recovery_status.ok()) {
210 INF("KVS init: Corruption detected and fully repaired");
211 } else {
212 ERR("KVS init: Corruption detected and unable repair");
213 }
214 }
215
David Rogers91627482020-02-27 17:38:12 -0800216 if (!empty_sector_found) {
217 // TODO: Record/report the error condition and recovery result.
218 Status gc_result = GarbageCollectPartial();
219
220 if (!gc_result.ok()) {
221 ERR("KVS init failed: Unable to maintain required free sector");
222 return Status::INTERNAL;
223 }
224 }
225
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800226 initialized_ = true;
David Rogers2e9e0c82020-02-13 15:06:06 -0800227
Armando Montanez5464d5f2020-02-20 10:12:20 -0800228 INF("KeyValueStore init complete: active keys %zu, deleted keys %zu, sectors "
David Rogers2e9e0c82020-02-13 15:06:06 -0800229 "%zu, logical sector size %zu bytes",
230 size(),
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700231 (entry_cache_.total_entries() - size()),
David Rogers2e9e0c82020-02-13 15:06:06 -0800232 sectors_.size(),
233 partition_.sector_size_bytes());
234
Alexei Frolovd4adf912020-02-21 13:29:15 -0800235 if (total_corrupt_bytes > 0) {
236 WRN("Found %zu corrupt bytes and %d corrupt entries during init process; "
237 "some keys may be missing",
238 total_corrupt_bytes,
239 corrupt_entries);
240 return Status::DATA_LOSS;
241 }
242
Keir Mierle8c352dc2020-02-02 13:58:19 -0800243 return Status::OK;
244}
245
Alexei Frolov9e235832020-02-24 12:44:45 -0800246KeyValueStore::StorageStats KeyValueStore::GetStorageStats() const {
247 StorageStats stats{0, 0, 0};
248 const size_t sector_size = partition_.sector_size_bytes();
249 bool found_empty_sector = false;
250
251 for (const SectorDescriptor& sector : sectors_) {
252 stats.in_use_bytes += sector.valid_bytes();
253 stats.reclaimable_bytes += sector.RecoverableBytes(sector_size);
254
255 if (!found_empty_sector && sector.Empty(sector_size)) {
256 // The KVS tries to always keep an empty sector for GC, so don't count
257 // the first empty sector seen as writable space. However, a free sector
258 // cannot always be assumed to exist; if a GC operation fails, all sectors
259 // may be partially written, in which case the space reported might be
260 // inaccurate.
261 found_empty_sector = true;
262 continue;
263 }
264
265 stats.writable_bytes += sector.writable_bytes();
266 }
267
268 return stats;
269}
270
Keir Mierle8c352dc2020-02-02 13:58:19 -0800271Status KeyValueStore::LoadEntry(Address entry_address,
272 Address* next_entry_address) {
Wyatt Heplere541e072020-02-14 09:10:53 -0800273 Entry entry;
Wyatt Hepler22d0d9f2020-03-05 14:57:11 -0800274 TRY(Entry::Read(partition_, entry_address, formats_, &entry));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800275
276 // Read the key from flash & validate the entry (which reads the value).
Wyatt Heplera00d1ef2020-02-14 14:31:26 -0800277 Entry::KeyBuffer key_buffer;
Wyatt Heplere541e072020-02-14 09:10:53 -0800278 TRY_ASSIGN(size_t key_length, entry.ReadKey(key_buffer));
279 const string_view key(key_buffer.data(), key_length);
Wyatt Heplerbab0e202020-02-04 07:40:08 -0800280
Wyatt Hepler22d0d9f2020-03-05 14:57:11 -0800281 TRY(entry.VerifyChecksumInFlash());
David Rogersf56131c2020-03-04 10:19:22 -0800282
283 // A valid entry was found, so update the next entry address before doing any
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700284 // of the checks that happen in AddNewOrUpdateExisting.
David Rogersf56131c2020-03-04 10:19:22 -0800285 *next_entry_address = entry.next_address();
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700286 return entry_cache_.AddNewOrUpdateExisting(
287 entry.descriptor(key), entry.address(), partition_.sector_size_bytes());
Keir Mierle8c352dc2020-02-02 13:58:19 -0800288}
289
Alexei Frolovd4adf912020-02-21 13:29:15 -0800290// Scans flash memory within a sector to find a KVS entry magic.
Alexei Frolovd4adf912020-02-21 13:29:15 -0800291Status KeyValueStore::ScanForEntry(const SectorDescriptor& sector,
292 Address start_address,
293 Address* next_entry_address) {
294 DBG("Scanning sector %u for entries starting from address %zx",
295 SectorIndex(&sector),
296 size_t(start_address));
297
298 // Entries must start at addresses which are aligned on a multiple of
299 // Entry::kMinAlignmentBytes. However, that multiple can vary between entries.
300 // When scanning, we don't have an entry to tell us what the current alignment
301 // is, so the minimum alignment is used to be exhaustive.
302 for (Address address = AlignUp(start_address, Entry::kMinAlignmentBytes);
303 AddressInSector(sector, address);
304 address += Entry::kMinAlignmentBytes) {
Alexei Frolovd4adf912020-02-21 13:29:15 -0800305 uint32_t magic;
306 TRY(partition_.Read(address, as_writable_bytes(span(&magic, 1))));
Wyatt Hepler22d0d9f2020-03-05 14:57:11 -0800307 if (formats_.KnownMagic(magic)) {
Alexei Frolovd4adf912020-02-21 13:29:15 -0800308 DBG("Found entry magic at address %zx", size_t(address));
309 *next_entry_address = address;
310 return Status::OK;
311 }
312 }
313
314 return Status::NOT_FOUND;
315}
316
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800317StatusWithSize KeyValueStore::Get(string_view key,
Wyatt Hepler5f6efc02020-02-18 16:54:31 -0800318 span<byte> value_buffer,
319 size_t offset_bytes) const {
Wyatt Hepler50f70772020-02-13 11:25:10 -0800320 TRY_WITH_SIZE(CheckOperation(key));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800321
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700322 EntryMetadata metadata;
323 TRY_WITH_SIZE(entry_cache_.FindExisting(partition_, key, &metadata));
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800324
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700325 return Get(key, metadata, value_buffer, offset_bytes);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800326}
327
Wyatt Heplerfac81132020-02-27 17:26:33 -0800328Status KeyValueStore::PutBytes(string_view key, span<const byte> value) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800329 DBG("Writing key/value; key length=%zu, value length=%zu",
330 key.size(),
331 value.size());
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800332
333 TRY(CheckOperation(key));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800334
Wyatt Hepler5406a672020-02-18 15:42:38 -0800335 if (Entry::size(partition_, key, value) > partition_.sector_size_bytes()) {
336 DBG("%zu B value with %zu B key cannot fit in one sector",
337 value.size(),
338 key.size());
339 return Status::INVALID_ARGUMENT;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800340 }
341
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700342 EntryMetadata metadata;
343 Status status = entry_cache_.Find(partition_, key, &metadata);
Wyatt Hepler2d401692020-02-13 16:01:23 -0800344
345 if (status.ok()) {
David Rogersf56131c2020-03-04 10:19:22 -0800346 // TODO: figure out logging how to support multiple addresses.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700347 DBG("Overwriting entry for key 0x%08" PRIx32 " in %zu sectors including %u",
348 metadata.hash(),
349 metadata.addresses().size(),
350 SectorIndex(SectorFromAddress(metadata.first_address())));
351 return WriteEntryForExistingKey(metadata, EntryState::kValid, key, value);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800352 }
David Rogers2761aeb2020-01-31 17:09:00 -0800353
Wyatt Hepler2d401692020-02-13 16:01:23 -0800354 if (status == Status::NOT_FOUND) {
355 return WriteEntryForNewKey(key, value);
356 }
357
358 return status;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800359}
360
361Status KeyValueStore::Delete(string_view key) {
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800362 TRY(CheckOperation(key));
363
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700364 EntryMetadata metadata;
365 TRY(entry_cache_.FindExisting(partition_, key, &metadata));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800366
David Rogersf56131c2020-03-04 10:19:22 -0800367 // TODO: figure out logging how to support multiple addresses.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700368 DBG("Writing tombstone for key 0x%08" PRIx32 " in %zu sectors including %u",
369 metadata.hash(),
370 metadata.addresses().size(),
371 SectorIndex(SectorFromAddress(metadata.first_address())));
372 return WriteEntryForExistingKey(metadata, EntryState::kDeleted, key, {});
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800373}
374
Wyatt Hepler08d37d82020-02-27 15:45:37 -0800375void KeyValueStore::Item::ReadKey() {
376 key_buffer_.fill('\0');
377
378 Entry entry;
David Rogersf3884eb2020-03-08 19:21:40 -0700379 // TODO: add support for using one of the redundant entries if reading the
380 // first copy fails.
Wyatt Hepler22d0d9f2020-03-05 14:57:11 -0800381 if (Entry::Read(
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700382 kvs_.partition_, iterator_->first_address(), kvs_.formats_, &entry)
Wyatt Hepler22d0d9f2020-03-05 14:57:11 -0800383 .ok()) {
Wyatt Hepler08d37d82020-02-27 15:45:37 -0800384 entry.ReadKey(key_buffer_);
385 }
386}
387
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800388KeyValueStore::iterator& KeyValueStore::iterator::operator++() {
389 // Skip to the next entry that is valid (not deleted).
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700390 while (++item_.iterator_ != item_.kvs_.entry_cache_.end() &&
Wyatt Hepler02946272020-03-18 10:36:22 -0700391 item_.iterator_->state() != EntryState::kValid) {
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800392 }
393 return *this;
394}
395
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800396KeyValueStore::iterator KeyValueStore::begin() const {
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700397 internal::EntryCache::iterator cache_iterator = entry_cache_.begin();
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800398 // Skip over any deleted entries at the start of the descriptor list.
Wyatt Hepler02946272020-03-18 10:36:22 -0700399 while (cache_iterator != entry_cache_.end() &&
400 cache_iterator->state() != EntryState::kValid) {
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700401 ++cache_iterator;
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800402 }
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700403 return iterator(*this, cache_iterator);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800404}
405
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700406StatusWithSize KeyValueStore::ValueSize(string_view key) const {
Wyatt Hepler50f70772020-02-13 11:25:10 -0800407 TRY_WITH_SIZE(CheckOperation(key));
Wyatt Heplered163b02020-02-03 17:49:32 -0800408
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700409 EntryMetadata metadata;
410 TRY_WITH_SIZE(entry_cache_.FindExisting(partition_, key, &metadata));
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800411
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700412 return ValueSize(metadata);
Wyatt Heplerfac81132020-02-27 17:26:33 -0800413}
Wyatt Heplered163b02020-02-03 17:49:32 -0800414
Wyatt Heplerfac81132020-02-27 17:26:33 -0800415StatusWithSize KeyValueStore::Get(string_view key,
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700416 const EntryMetadata& metadata,
Wyatt Heplerfac81132020-02-27 17:26:33 -0800417 span<std::byte> value_buffer,
418 size_t offset_bytes) const {
419 Entry entry;
David Rogersa2562b52020-03-05 15:30:05 -0800420 // TODO: add support for using one of the redundant entries if reading the
421 // first copy fails.
Wyatt Hepler22d0d9f2020-03-05 14:57:11 -0800422 TRY_WITH_SIZE(
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700423 Entry::Read(partition_, metadata.first_address(), formats_, &entry));
Wyatt Heplerfac81132020-02-27 17:26:33 -0800424
425 StatusWithSize result = entry.ReadValue(value_buffer, offset_bytes);
426 if (result.ok() && options_.verify_on_read && offset_bytes == 0u) {
Wyatt Hepler22d0d9f2020-03-05 14:57:11 -0800427 Status verify_result =
428 entry.VerifyChecksum(key, value_buffer.first(result.size()));
Wyatt Heplerfac81132020-02-27 17:26:33 -0800429 if (!verify_result.ok()) {
430 std::memset(value_buffer.data(), 0, result.size());
431 return StatusWithSize(verify_result, 0);
432 }
433
434 return StatusWithSize(verify_result, result.size());
435 }
436 return result;
Wyatt Heplered163b02020-02-03 17:49:32 -0800437}
438
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800439Status KeyValueStore::FixedSizeGet(std::string_view key,
Wyatt Heplerfac81132020-02-27 17:26:33 -0800440 void* value,
441 size_t size_bytes) const {
442 TRY(CheckOperation(key));
443
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700444 EntryMetadata metadata;
445 TRY(entry_cache_.FindExisting(partition_, key, &metadata));
Wyatt Heplerfac81132020-02-27 17:26:33 -0800446
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700447 return FixedSizeGet(key, metadata, value, size_bytes);
Wyatt Heplerfac81132020-02-27 17:26:33 -0800448}
449
450Status KeyValueStore::FixedSizeGet(std::string_view key,
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700451 const EntryMetadata& metadata,
Wyatt Heplerfac81132020-02-27 17:26:33 -0800452 void* value,
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800453 size_t size_bytes) const {
454 // Ensure that the size of the stored value matches the size of the type.
455 // Otherwise, report error. This check avoids potential memory corruption.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700456 TRY_ASSIGN(const size_t actual_size, ValueSize(metadata));
Wyatt Heplerfac81132020-02-27 17:26:33 -0800457
458 if (actual_size != size_bytes) {
459 DBG("Requested %zu B read, but value is %zu B", size_bytes, actual_size);
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800460 return Status::INVALID_ARGUMENT;
Wyatt Heplerbab0e202020-02-04 07:40:08 -0800461 }
Wyatt Heplerfac81132020-02-27 17:26:33 -0800462
463 StatusWithSize result =
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700464 Get(key, metadata, span(static_cast<byte*>(value), size_bytes), 0);
Wyatt Heplerfac81132020-02-27 17:26:33 -0800465
466 return result.status();
467}
468
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700469StatusWithSize KeyValueStore::ValueSize(const EntryMetadata& metadata) const {
Wyatt Heplerfac81132020-02-27 17:26:33 -0800470 Entry entry;
David Rogersf3884eb2020-03-08 19:21:40 -0700471 // TODO: add support for using one of the redundant entries if reading the
472 // first copy fails.
Wyatt Hepler22d0d9f2020-03-05 14:57:11 -0800473 TRY_WITH_SIZE(
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700474 Entry::Read(partition_, metadata.first_address(), formats_, &entry));
Wyatt Heplerfac81132020-02-27 17:26:33 -0800475
476 return StatusWithSize(entry.value_size());
Keir Mierle8c352dc2020-02-02 13:58:19 -0800477}
478
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800479Status KeyValueStore::CheckOperation(string_view key) const {
Wyatt Hepleracaacf92020-01-24 10:58:30 -0800480 if (InvalidKey(key)) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800481 return Status::INVALID_ARGUMENT;
482 }
Wyatt Heplerd2298282020-02-20 17:12:45 -0800483 if (!initialized()) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800484 return Status::FAILED_PRECONDITION;
485 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800486 return Status::OK;
487}
488
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700489Status KeyValueStore::WriteEntryForExistingKey(EntryMetadata& metadata,
490 EntryState new_state,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800491 string_view key,
492 span<const byte> value) {
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700493 // Read the original entry to get the size for sector accounting purposes.
494 Entry entry;
David Rogersa2562b52020-03-05 15:30:05 -0800495 // TODO: add support for using one of the redundant entries if reading the
496 // first copy fails.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700497 TRY(Entry::Read(partition_, metadata.first_address(), formats_, &entry));
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800498
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700499 return WriteEntry(key, value, new_state, &metadata, entry.size());
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800500}
501
502Status KeyValueStore::WriteEntryForNewKey(string_view key,
503 span<const byte> value) {
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700504 if (entry_cache_.full()) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800505 WRN("KVS full: trying to store a new entry, but can't. Have %zu entries",
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700506 entry_cache_.total_entries());
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800507 return Status::RESOURCE_EXHAUSTED;
508 }
509
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700510 return WriteEntry(key, value, EntryState::kValid);
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700511}
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800512
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700513Status KeyValueStore::WriteEntry(string_view key,
514 span<const byte> value,
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700515 EntryState new_state,
516 EntryMetadata* prior_metadata,
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700517 size_t prior_size) {
518 const size_t entry_size = Entry::size(partition_, key, value);
519
520 // List of addresses for sectors with space for this entry.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700521 Address* reserved_addresses = entry_cache_.TempReservedAddressesForWrite();
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700522
523 // Find sectors to write the entry to. This may involve garbage collecting one
524 // or more sectors.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700525 for (size_t i = 0; i < redundancy(); i++) {
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700526 SectorDescriptor* sector;
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700527 TRY(GetSectorForWrite(&sector, entry_size, span(reserved_addresses, i)));
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700528
529 DBG("Found space for entry in sector %u", SectorIndex(sector));
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700530 reserved_addresses[i] = NextWritableAddress(sector);
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700531 }
532
533 // Write the entry at the first address that was found.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700534 Entry entry = CreateEntry(reserved_addresses[0], key, value, new_state);
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700535 TRY(AppendEntry(entry, key, value));
536
537 // After writing the first entry successfully, update the key descriptors.
538 // Once a single new the entry is written, the old entries are invalidated.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700539 EntryMetadata new_metadata =
540 UpdateKeyDescriptor(entry, key, prior_metadata, prior_size);
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700541
542 // Write the additional copies of the entry, if redundancy is greater than 1.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700543 for (size_t i = 1; i < redundancy(); ++i) {
544 entry.set_address(reserved_addresses[i]);
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700545 TRY(AppendEntry(entry, key, value));
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700546 new_metadata.AddNewAddress(reserved_addresses[i]);
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700547 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800548 return Status::OK;
549}
550
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700551KeyValueStore::EntryMetadata KeyValueStore::UpdateKeyDescriptor(
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700552 const Entry& entry,
553 string_view key,
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700554 EntryMetadata* prior_metadata,
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700555 size_t prior_size) {
556 // If there is no prior descriptor, create a new one.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700557 if (prior_metadata == nullptr) {
558 return entry_cache_.AddNew(entry.descriptor(key), entry.address());
David Rogersa2562b52020-03-05 15:30:05 -0800559 }
560
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700561 // Remove valid bytes for the old entry and its copies, which are now stale.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700562 for (Address address : prior_metadata->addresses()) {
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700563 SectorFromAddress(address)->RemoveValidBytes(prior_size);
David Rogersa2562b52020-03-05 15:30:05 -0800564 }
565
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700566 prior_metadata->Reset(entry.descriptor(key), entry.address());
567 return *prior_metadata;
David Rogersa2562b52020-03-05 15:30:05 -0800568}
569
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700570// Finds a sector to use for writing a new entry to. Does automatic garbage
David Rogersa2562b52020-03-05 15:30:05 -0800571// collection if needed and allowed.
572//
573// OK: Sector found with needed space.
574// RESOURCE_EXHAUSTED: No sector available with the needed space.
575Status KeyValueStore::GetSectorForWrite(SectorDescriptor** sector,
576 size_t entry_size,
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700577 span<const Address> reserved) {
David Rogersc9d545e2020-03-11 17:47:43 -0700578 Status result =
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700579 FindSectorWithSpace(sector, entry_size, kAppendEntry, {}, reserved);
David Rogersa2562b52020-03-05 15:30:05 -0800580
David Rogersf3884eb2020-03-08 19:21:40 -0700581 size_t gc_sector_count = 0;
David Rogersa2562b52020-03-05 15:30:05 -0800582 bool do_auto_gc = options_.gc_on_write != GargbageCollectOnWrite::kDisabled;
583
584 // Do garbage collection as needed, so long as policy allows.
585 while (result == Status::RESOURCE_EXHAUSTED && do_auto_gc) {
586 if (options_.gc_on_write == GargbageCollectOnWrite::kOneSector) {
587 // If GC config option is kOneSector clear the flag to not do any more
588 // GC after this try.
589 do_auto_gc = false;
590 }
591 // Garbage collect and then try again to find the best sector.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700592 Status gc_status = GarbageCollectPartial(reserved);
David Rogersa2562b52020-03-05 15:30:05 -0800593 if (!gc_status.ok()) {
594 if (gc_status == Status::NOT_FOUND) {
595 // Not enough space, and no reclaimable bytes, this KVS is full!
596 return Status::RESOURCE_EXHAUSTED;
597 }
598 return gc_status;
599 }
600
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700601 result =
602 FindSectorWithSpace(sector, entry_size, kAppendEntry, {}, reserved);
David Rogersf3884eb2020-03-08 19:21:40 -0700603
604 gc_sector_count++;
605 // Allow total sectors + 2 number of GC cycles so that once reclaimable
606 // bytes in all the sectors have been reclaimed can try and free up space by
607 // moving entries for keys other than the one being worked on in to sectors
608 // that have copies of the key trying to be written.
609 if (gc_sector_count > (partition_.sector_count() + 2)) {
610 ERR("Did more GC sectors than total sectors!!!!");
611 return Status::RESOURCE_EXHAUSTED;
612 }
David Rogersa2562b52020-03-05 15:30:05 -0800613 }
614
615 if (!result.ok()) {
616 WRN("Unable to find sector to write %zu B", entry_size);
617 }
618 return result;
619}
620
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700621Status KeyValueStore::AppendEntry(const Entry& entry,
David Rogersa2562b52020-03-05 15:30:05 -0800622 string_view key,
623 span<const byte> value) {
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700624 const StatusWithSize result = entry.Write(key, value);
David Rogersa2562b52020-03-05 15:30:05 -0800625
David Rogersa2562b52020-03-05 15:30:05 -0800626 // Remove any bytes that were written, even if the write was not successful.
627 // This is important to retain the writable space invariant on the sectors.
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700628 SectorDescriptor* const sector = SectorFromAddress(entry.address());
629 sector->RemoveWritableBytes(result.size());
David Rogersa2562b52020-03-05 15:30:05 -0800630
631 if (!result.ok()) {
632 ERR("Failed to write %zu bytes at %#zx. %zu actually written",
633 entry.size(),
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700634 size_t(entry.address()),
David Rogersa2562b52020-03-05 15:30:05 -0800635 result.size());
636 return result.status();
637 }
638
639 if (options_.verify_on_write) {
640 TRY(entry.VerifyChecksumInFlash());
641 }
642
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700643 sector->AddValidBytes(result.size());
David Rogersa2562b52020-03-05 15:30:05 -0800644 return Status::OK;
645}
646
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700647Status KeyValueStore::RelocateEntry(const EntryMetadata& metadata,
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700648 KeyValueStore::Address& address,
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700649 span<const Address> reserved_addresses) {
David Rogersa2562b52020-03-05 15:30:05 -0800650 Entry entry;
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700651 TRY(Entry::Read(partition_, address, formats_, &entry));
David Rogersa2562b52020-03-05 15:30:05 -0800652
653 // Find a new sector for the entry and write it to the new location. For
654 // relocation the find should not not be a sector already containing the key
655 // but can be the always empty sector, since this is part of the GC process
656 // that will result in a new empty sector. Also find a sector that does not
657 // have reclaimable space (mostly for the full GC, where that would result in
658 // an immediate extra relocation).
659 SectorDescriptor* new_sector;
660
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700661 TRY(FindSectorWithSpace(&new_sector,
662 entry.size(),
663 kGarbageCollect,
664 metadata.addresses(),
665 reserved_addresses));
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700666
667 const Address new_address = NextWritableAddress(new_sector);
668 const StatusWithSize result = entry.Copy(new_address);
669 new_sector->RemoveWritableBytes(result.size());
670 TRY(result);
David Rogersa2562b52020-03-05 15:30:05 -0800671
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700672 // Entry was written successfully; update descriptor's address and the sector
David Rogersa2562b52020-03-05 15:30:05 -0800673 // descriptors to reflect the new entry.
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700674 SectorFromAddress(address)->RemoveValidBytes(result.size());
675 new_sector->AddValidBytes(result.size());
676 address = new_address;
David Rogersa2562b52020-03-05 15:30:05 -0800677
678 return Status::OK;
679}
680
David Rogers8db5a722020-02-03 18:28:34 -0800681// Find either an existing sector with enough space that is not the sector to
682// skip, or an empty sector. Maintains the invariant that there is always at
David Rogersc8fe1f52020-02-27 14:04:08 -0800683// least 1 empty sector except during GC. On GC, skip sectors that have
684// reclaimable bytes.
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800685Status KeyValueStore::FindSectorWithSpace(
686 SectorDescriptor** found_sector,
687 size_t size,
David Rogersc8fe1f52020-02-27 14:04:08 -0800688 FindSectorMode find_mode,
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700689 span<const Address> addresses_to_skip,
690 span<const Address> reserved_addresses) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800691 SectorDescriptor* first_empty_sector = nullptr;
David Rogersc8fe1f52020-02-27 14:04:08 -0800692 bool at_least_two_empty_sectors = (find_mode == kGarbageCollect);
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800693
David Rogersf3884eb2020-03-08 19:21:40 -0700694 // Used for the GC reclaimable bytes check
695 SectorDescriptor* non_empty_least_reclaimable_sector = nullptr;
696 const size_t sector_size_bytes = partition_.sector_size_bytes();
697
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700698 // Build a list of sectors to avoid.
699 //
700 // This is overly strict. reserved_addresses is populated when there are
701 // sectors reserved for a new entry. It is safe to garbage collect into
702 // these sectors, as long as there remains room for the pending entry. These
703 // reserved sectors could also be garbage collected if they have recoverable
704 // space. For simplicitly, avoid both the relocating key's redundant entries
705 // (addresses_to_skip) and the sectors reserved for pending writes
706 // (reserved_addresses).
707 // TODO(hepler): Look into improving garbage collection.
708 size_t sectors_to_skip = 0;
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700709 for (Address address : addresses_to_skip) {
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700710 temp_sectors_to_skip_[sectors_to_skip++] = SectorFromAddress(address);
711 }
712 for (Address address : reserved_addresses) {
713 temp_sectors_to_skip_[sectors_to_skip++] = SectorFromAddress(address);
David Rogersa2562b52020-03-05 15:30:05 -0800714 }
715
David Rogersf3884eb2020-03-08 19:21:40 -0700716 DBG("Find sector with %zu bytes available, starting with sector %u, %s",
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800717 size,
David Rogersf3884eb2020-03-08 19:21:40 -0700718 SectorIndex(last_new_sector_),
719 (find_mode == kAppendEntry) ? "Append" : "GC");
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700720 for (size_t i = 0; i < sectors_to_skip; ++i) {
721 DBG(" Skip sector %u", SectorIndex(temp_sectors_to_skip_[i]));
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800722 }
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800723
David Rogers8ce55cd2020-02-04 19:41:48 -0800724 // The last_new_sector_ is the sector that was last selected as the "new empty
725 // sector" to write to. This last new sector is used as the starting point for
726 // the next "find a new empty sector to write to" operation. By using the last
727 // new sector as the start point we will cycle which empty sector is selected
728 // next, spreading the wear across all the empty sectors and get a wear
729 // leveling benefit, rather than putting more wear on the lower number
730 // sectors.
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800731 SectorDescriptor* sector = last_new_sector_;
David Rogers67f4b6c2020-02-06 16:17:09 -0800732
David Rogersf3884eb2020-03-08 19:21:40 -0700733 // Look for a sector to use with enough space. The search uses a 3 priority
David Rogerscd87c322020-02-27 14:04:08 -0800734 // tier process.
735 //
David Rogersc8fe1f52020-02-27 14:04:08 -0800736 // Tier 1 is sector that already has valid data. During GC only select a
737 // sector that has no reclaimable bytes. Immediately use the first matching
738 // sector that is found.
David Rogerscd87c322020-02-27 14:04:08 -0800739 //
David Rogersc8fe1f52020-02-27 14:04:08 -0800740 // Tier 2 is find sectors that are empty/erased. While scanning for a partial
741 // sector, keep track of the first empty sector and if a second empty sector
742 // was seen. If during GC then count the second empty sector as always seen.
David Rogersf3884eb2020-03-08 19:21:40 -0700743 //
744 // Tier 3 is during garbage collection, find sectors with enough space that
745 // are not empty but have recoverable bytes. Pick the sector with the least
746 // recoverable bytes to minimize the likelyhood of this sector needing to be
747 // garbage collected soon.
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800748 for (size_t j = 0; j < sectors_.size(); j++) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800749 sector += 1;
750 if (sector == sectors_.end()) {
751 sector = sectors_.begin();
752 }
Keir Mierle8c352dc2020-02-02 13:58:19 -0800753
David Rogersf3884eb2020-03-08 19:21:40 -0700754 // Skip sectors in the skip list.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700755 if (Contains(span(temp_sectors_to_skip_, sectors_to_skip), sector)) {
David Rogers8db5a722020-02-03 18:28:34 -0800756 continue;
757 }
758
David Rogersf3884eb2020-03-08 19:21:40 -0700759 if (!sector->Empty(sector_size_bytes) && sector->HasSpace(size)) {
760 if ((find_mode == kAppendEntry) ||
761 (sector->RecoverableBytes(sector_size_bytes) == 0)) {
762 *found_sector = sector;
763 return Status::OK;
764 } else {
765 if ((non_empty_least_reclaimable_sector == nullptr) ||
766 (non_empty_least_reclaimable_sector->RecoverableBytes(
767 sector_size_bytes) <
768 sector->RecoverableBytes(sector_size_bytes))) {
769 non_empty_least_reclaimable_sector = sector;
770 }
771 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800772 }
773
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800774 if (sector->Empty(sector_size_bytes)) {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800775 if (first_empty_sector == nullptr) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800776 first_empty_sector = sector;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800777 } else {
778 at_least_two_empty_sectors = true;
Wyatt Hepler2ad60672020-01-21 08:00:16 -0800779 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800780 }
781 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800782
David Rogersf3884eb2020-03-08 19:21:40 -0700783 // Tier 2 check: If the scan for a partial sector does not find a suitable
784 // sector, use the first empty sector that was found. Normally it is required
785 // to keep 1 empty sector after the sector found here, but that rule does not
786 // apply during GC.
787 if (first_empty_sector != nullptr && at_least_two_empty_sectors) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800788 DBG(" Found a usable empty sector; returning the first found (%u)",
David Rogers8ce55cd2020-02-04 19:41:48 -0800789 SectorIndex(first_empty_sector));
790 last_new_sector_ = first_empty_sector;
791 *found_sector = first_empty_sector;
792 return Status::OK;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800793 }
David Rogers8ce55cd2020-02-04 19:41:48 -0800794
David Rogersf3884eb2020-03-08 19:21:40 -0700795 // Tier 3 check: If we got this far, use the sector with least recoverable
796 // bytes
797 if (non_empty_least_reclaimable_sector != nullptr) {
798 *found_sector = non_empty_least_reclaimable_sector;
799 DBG(" Found a usable sector %u, with %zu B recoverable, in GC",
800 SectorIndex(*found_sector),
801 (*found_sector)->RecoverableBytes(sector_size_bytes));
802 return Status::OK;
803 }
804
David Rogers8ce55cd2020-02-04 19:41:48 -0800805 // No sector was found.
David Rogers67f4b6c2020-02-06 16:17:09 -0800806 DBG(" Unable to find a usable sector");
David Rogers8ce55cd2020-02-04 19:41:48 -0800807 *found_sector = nullptr;
808 return Status::RESOURCE_EXHAUSTED;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800809}
810
David Rogersf3884eb2020-03-08 19:21:40 -0700811// TODO: Break up this function in to smaller sub-chunks including create an
812// abstraction for the sector list. Look in to being able to unit test this as
813// its own thing
814KeyValueStore::SectorDescriptor* KeyValueStore::FindSectorToGarbageCollect(
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700815 span<const Address> reserved_addresses) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800816 const size_t sector_size_bytes = partition_.sector_size_bytes();
David Rogers2761aeb2020-01-31 17:09:00 -0800817 SectorDescriptor* sector_candidate = nullptr;
David Rogersa12786b2020-01-31 16:02:33 -0800818 size_t candidate_bytes = 0;
819
David Rogersf3884eb2020-03-08 19:21:40 -0700820 // Build a vector of sectors to avoid.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700821 for (size_t i = 0; i < reserved_addresses.size(); ++i) {
822 temp_sectors_to_skip_[i] = SectorFromAddress(reserved_addresses[i]);
823 DBG(" Skip sector %u",
824 SectorIndex(SectorFromAddress(reserved_addresses[i])));
David Rogersf3884eb2020-03-08 19:21:40 -0700825 }
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700826 const span sectors_to_skip(temp_sectors_to_skip_, reserved_addresses.size());
David Rogersf3884eb2020-03-08 19:21:40 -0700827
David Rogersa12786b2020-01-31 16:02:33 -0800828 // Step 1: Try to find a sectors with stale keys and no valid keys (no
829 // relocation needed). If any such sectors are found, use the sector with the
830 // most reclaimable bytes.
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800831 for (auto& sector : sectors_) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800832 if ((sector.valid_bytes() == 0) &&
David Rogersf3884eb2020-03-08 19:21:40 -0700833 (sector.RecoverableBytes(sector_size_bytes) > candidate_bytes) &&
834 !Contains(sectors_to_skip, &sector)) {
David Rogersa12786b2020-01-31 16:02:33 -0800835 sector_candidate = &sector;
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800836 candidate_bytes = sector.RecoverableBytes(sector_size_bytes);
David Rogersa12786b2020-01-31 16:02:33 -0800837 }
838 }
839
David Rogersc9d545e2020-03-11 17:47:43 -0700840 // Step 2: If step 1 yields no sectors, just find the sector with the most
David Rogersf3884eb2020-03-08 19:21:40 -0700841 // reclaimable bytes but no addresses to avoid.
842 if (sector_candidate == nullptr) {
843 for (auto& sector : sectors_) {
844 if ((sector.RecoverableBytes(sector_size_bytes) > candidate_bytes) &&
845 !Contains(sectors_to_skip, &sector)) {
846 sector_candidate = &sector;
847 candidate_bytes = sector.RecoverableBytes(sector_size_bytes);
848 }
849 }
850 }
851
David Rogersf3884eb2020-03-08 19:21:40 -0700852 // Step 3: If no sectors with reclaimable bytes, select the sector with the
853 // most free bytes. This at least will allow entries of existing keys to get
854 // spread to other sectors, including sectors that already have copies of the
855 // current key being written.
856 if (sector_candidate == nullptr) {
857 for (auto& sector : sectors_) {
858 if ((sector.valid_bytes() > candidate_bytes) &&
859 !Contains(sectors_to_skip, &sector)) {
860 sector_candidate = &sector;
861 candidate_bytes = sector.valid_bytes();
862 DBG(" Doing GC on sector with no reclaimable bytes!");
863 }
864 }
865 }
866
David Rogers5981f312020-02-13 13:33:56 -0800867 if (sector_candidate != nullptr) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800868 DBG("Found sector %u to Garbage Collect, %zu recoverable bytes",
David Rogers5981f312020-02-13 13:33:56 -0800869 SectorIndex(sector_candidate),
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800870 sector_candidate->RecoverableBytes(sector_size_bytes));
David Rogers5981f312020-02-13 13:33:56 -0800871 } else {
872 DBG("Unable to find sector to garbage collect!");
873 }
David Rogersa12786b2020-01-31 16:02:33 -0800874 return sector_candidate;
875}
876
David Rogerscd87c322020-02-27 14:04:08 -0800877Status KeyValueStore::GarbageCollectFull() {
878 DBG("Garbage Collect all sectors");
David Rogerscd87c322020-02-27 14:04:08 -0800879 SectorDescriptor* sector = last_new_sector_;
880
881 // TODO: look in to making an iterator method for cycling through sectors
882 // starting from last_new_sector_.
883 for (size_t j = 0; j < sectors_.size(); j++) {
884 sector += 1;
885 if (sector == sectors_.end()) {
886 sector = sectors_.begin();
887 }
888
889 if (sector->RecoverableBytes(partition_.sector_size_bytes()) > 0) {
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700890 TRY(GarbageCollectSector(sector, {}));
David Rogerscd87c322020-02-27 14:04:08 -0800891 }
892 }
893
894 DBG("Garbage Collect all complete");
David Rogerscd87c322020-02-27 14:04:08 -0800895 return Status::OK;
896}
897
David Rogersc9d545e2020-03-11 17:47:43 -0700898Status KeyValueStore::GarbageCollectPartial(
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700899 span<const Address> reserved_addresses) {
David Rogersc9d545e2020-03-11 17:47:43 -0700900 DBG("Garbage Collect a single sector");
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700901 for (Address address : reserved_addresses) {
David Rogersc9d545e2020-03-11 17:47:43 -0700902 DBG(" Avoid address %u", unsigned(address));
903 }
David Rogers67f4b6c2020-02-06 16:17:09 -0800904
David Rogersa12786b2020-01-31 16:02:33 -0800905 // Step 1: Find the sector to garbage collect
David Rogersc9d545e2020-03-11 17:47:43 -0700906 SectorDescriptor* sector_to_gc =
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700907 FindSectorToGarbageCollect(reserved_addresses);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800908
David Rogersa12786b2020-01-31 16:02:33 -0800909 if (sector_to_gc == nullptr) {
David Rogersa2562b52020-03-05 15:30:05 -0800910 // Nothing to GC.
911 return Status::NOT_FOUND;
David Rogersa12786b2020-01-31 16:02:33 -0800912 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800913
David Rogersc9d545e2020-03-11 17:47:43 -0700914 // Step 2: Garbage collect the selected sector.
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700915 return GarbageCollectSector(sector_to_gc, reserved_addresses);
David Rogerscd87c322020-02-27 14:04:08 -0800916}
917
David Rogersf3884eb2020-03-08 19:21:40 -0700918Status KeyValueStore::RelocateKeyAddressesInSector(
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700919 SectorDescriptor& sector_to_gc,
920 const EntryMetadata& metadata,
921 span<const Address> reserved_addresses) {
922 for (FlashPartition::Address& address : metadata.addresses()) {
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700923 if (AddressInSector(sector_to_gc, address)) {
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700924 DBG(" Relocate entry for Key 0x%08" PRIx32 ", sector %u",
925 metadata.hash(),
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700926 SectorIndex(SectorFromAddress(address)));
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700927 TRY(RelocateEntry(metadata, address, reserved_addresses));
David Rogersf3884eb2020-03-08 19:21:40 -0700928 }
929 }
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700930
David Rogersf3884eb2020-03-08 19:21:40 -0700931 return Status::OK;
932};
933
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700934Status KeyValueStore::GarbageCollectSector(
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700935 SectorDescriptor* sector_to_gc, span<const Address> reserved_addresses) {
David Rogersf3884eb2020-03-08 19:21:40 -0700936 // Step 1: Move any valid entries in the GC sector to other sectors
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700937 if (sector_to_gc->valid_bytes() != 0) {
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700938 for (const EntryMetadata& metadata : entry_cache_) {
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700939 TRY(RelocateKeyAddressesInSector(
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700940 *sector_to_gc, metadata, reserved_addresses));
David Rogersf3884eb2020-03-08 19:21:40 -0700941 }
942 }
943
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800944 if (sector_to_gc->valid_bytes() != 0) {
David Rogers67f4b6c2020-02-06 16:17:09 -0800945 ERR(" Failed to relocate valid entries from sector being garbage "
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800946 "collected, %zu valid bytes remain",
947 sector_to_gc->valid_bytes());
Wyatt Heplerb7609542020-01-24 10:29:54 -0800948 return Status::INTERNAL;
949 }
950
David Rogerscd87c322020-02-27 14:04:08 -0800951 // Step 2: Reinitialize the sector
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800952 sector_to_gc->set_writable_bytes(0);
David Rogersa12786b2020-01-31 16:02:33 -0800953 TRY(partition_.Erase(SectorBaseAddress(sector_to_gc), 1));
Wyatt Hepler2c7eca02020-02-18 16:01:42 -0800954 sector_to_gc->set_writable_bytes(partition_.sector_size_bytes());
Wyatt Heplerb7609542020-01-24 10:29:54 -0800955
David Rogerscd87c322020-02-27 14:04:08 -0800956 DBG(" Garbage Collect sector %u complete", SectorIndex(sector_to_gc));
David Rogersa12786b2020-01-31 16:02:33 -0800957 return Status::OK;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800958}
959
Wyatt Heplerbdd8e5a2020-02-20 19:27:26 -0800960KeyValueStore::Entry KeyValueStore::CreateEntry(Address address,
Wyatt Heplerab3b2492020-03-11 16:15:16 -0700961 string_view key,
Wyatt Heplerbdd8e5a2020-02-20 19:27:26 -0800962 span<const byte> value,
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700963 EntryState state) {
Keir Mierle9e38b402020-02-21 13:06:21 -0800964 // Always bump the transaction ID when creating a new entry.
965 //
966 // Burning transaction IDs prevents inconsistencies between flash and memory
967 // that which could happen if a write succeeds, but for some reason the read
968 // and verify step fails. Here's how this would happen:
969 //
970 // 1. The entry is written but for some reason the flash reports failure OR
971 // The write succeeds, but the read / verify operation fails.
972 // 2. The transaction ID is NOT incremented, because of the failure
973 // 3. (later) A new entry is written, re-using the transaction ID (oops)
974 //
975 // By always burning transaction IDs, the above problem can't happen.
976 last_transaction_id_ += 1;
977
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700978 if (state == EntryState::kDeleted) {
Wyatt Hepler7465be32020-02-21 15:30:53 -0800979 return Entry::Tombstone(
Wyatt Hepler22d0d9f2020-03-05 14:57:11 -0800980 partition_, address, formats_.primary(), key, last_transaction_id_);
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800981 }
982 return Entry::Valid(partition_,
983 address,
Wyatt Hepler22d0d9f2020-03-05 14:57:11 -0800984 formats_.primary(),
Wyatt Hepler1fc11042020-02-19 17:17:51 -0800985 key,
986 value,
Keir Mierle9e38b402020-02-21 13:06:21 -0800987 last_transaction_id_);
Wyatt Heplerd2298282020-02-20 17:12:45 -0800988}
989
Wyatt Hepler7ded6da2020-03-11 18:24:43 -0700990void KeyValueStore::LogDebugInfo() const {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800991 const size_t sector_size_bytes = partition_.sector_size_bytes();
992 DBG("====================== KEY VALUE STORE DUMP =========================");
993 DBG(" ");
994 DBG("Flash partition:");
Wyatt Heplerad0a7932020-02-06 08:20:38 -0800995 DBG(" Sector count = %zu", partition_.sector_count());
Wyatt Hepler38ce30f2020-02-19 11:48:31 -0800996 DBG(" Sector max count = %zu", sectors_.max_size());
Wyatt Hepler1c329ca2020-02-07 18:07:23 -0800997 DBG(" Sectors in use = %zu", sectors_.size());
Keir Mierle8c352dc2020-02-02 13:58:19 -0800998 DBG(" Sector size = %zu", sector_size_bytes);
999 DBG(" Total size = %zu", partition_.size_bytes());
1000 DBG(" Alignment = %zu", partition_.alignment_bytes());
1001 DBG(" ");
1002 DBG("Key descriptors:");
Wyatt Hepler7ded6da2020-03-11 18:24:43 -07001003 DBG(" Entry count = %zu", entry_cache_.total_entries());
1004 DBG(" Max entry count = %zu", entry_cache_.max_entries());
Keir Mierle8c352dc2020-02-02 13:58:19 -08001005 DBG(" ");
1006 DBG(" # hash version address address (hex)");
Wyatt Hepler7ded6da2020-03-11 18:24:43 -07001007 size_t i = 0;
1008 for (const EntryMetadata& metadata : entry_cache_) {
Keir Mierle8c352dc2020-02-02 13:58:19 -08001009 DBG(" |%3zu: | %8zx |%8zu | %8zu | %8zx",
Wyatt Hepler7ded6da2020-03-11 18:24:43 -07001010 i++,
1011 size_t(metadata.hash()),
1012 size_t(metadata.transaction_id()),
1013 size_t(metadata.first_address()),
1014 size_t(metadata.first_address()));
Keir Mierle8c352dc2020-02-02 13:58:19 -08001015 }
1016 DBG(" ");
1017
1018 DBG("Sector descriptors:");
1019 DBG(" # tail free valid has_space");
Wyatt Hepler1c329ca2020-02-07 18:07:23 -08001020 for (size_t sector_id = 0; sector_id < sectors_.size(); ++sector_id) {
1021 const SectorDescriptor& sd = sectors_[sector_id];
Keir Mierle8c352dc2020-02-02 13:58:19 -08001022 DBG(" |%3zu: | %8zu |%8zu | %s",
1023 sector_id,
Wyatt Hepler2c7eca02020-02-18 16:01:42 -08001024 size_t(sd.writable_bytes()),
1025 sd.valid_bytes(),
1026 sd.writable_bytes() ? "YES" : "");
Keir Mierle8c352dc2020-02-02 13:58:19 -08001027 }
1028 DBG(" ");
1029
1030 // TODO: This should stop logging after some threshold.
1031 // size_t dumped_bytes = 0;
1032 DBG("Sector raw data:");
Wyatt Hepler1c329ca2020-02-07 18:07:23 -08001033 for (size_t sector_id = 0; sector_id < sectors_.size(); ++sector_id) {
Keir Mierle8c352dc2020-02-02 13:58:19 -08001034 // Read sector data. Yes, this will blow the stack on embedded.
Wyatt Hepler1c329ca2020-02-07 18:07:23 -08001035 std::array<byte, 500> raw_sector_data; // TODO!!!
Keir Mierle8c352dc2020-02-02 13:58:19 -08001036 StatusWithSize sws =
1037 partition_.Read(sector_id * sector_size_bytes, raw_sector_data);
1038 DBG("Read: %zu bytes", sws.size());
1039
1040 DBG(" base addr offs 0 1 2 3 4 5 6 7");
1041 for (size_t i = 0; i < sector_size_bytes; i += 8) {
1042 DBG(" %3zu %8zx %5zu | %02x %02x %02x %02x %02x %02x %02x %02x",
1043 sector_id,
1044 (sector_id * sector_size_bytes) + i,
1045 i,
1046 static_cast<unsigned int>(raw_sector_data[i + 0]),
1047 static_cast<unsigned int>(raw_sector_data[i + 1]),
1048 static_cast<unsigned int>(raw_sector_data[i + 2]),
1049 static_cast<unsigned int>(raw_sector_data[i + 3]),
1050 static_cast<unsigned int>(raw_sector_data[i + 4]),
1051 static_cast<unsigned int>(raw_sector_data[i + 5]),
1052 static_cast<unsigned int>(raw_sector_data[i + 6]),
1053 static_cast<unsigned int>(raw_sector_data[i + 7]));
1054
1055 // TODO: Fix exit condition.
1056 if (i > 128) {
1057 break;
1058 }
1059 }
1060 DBG(" ");
1061 }
1062
1063 DBG("////////////////////// KEY VALUE STORE DUMP END /////////////////////");
1064}
1065
David Rogerscf680ab2020-02-12 23:28:32 -08001066void KeyValueStore::LogSectors() const {
1067 DBG("Sector descriptors: count %zu", sectors_.size());
Wyatt Hepler1c329ca2020-02-07 18:07:23 -08001068 for (auto& sector : sectors_) {
Wyatt Hepler2c7eca02020-02-18 16:01:42 -08001069 DBG(" - Sector %u: valid %zu, recoverable %zu, free %zu",
David Rogers50185ad2020-02-07 00:02:46 -08001070 SectorIndex(&sector),
Wyatt Hepler2c7eca02020-02-18 16:01:42 -08001071 sector.valid_bytes(),
1072 sector.RecoverableBytes(partition_.sector_size_bytes()),
1073 sector.writable_bytes());
David Rogers50185ad2020-02-07 00:02:46 -08001074 }
1075}
1076
David Rogerscf680ab2020-02-12 23:28:32 -08001077void KeyValueStore::LogKeyDescriptor() const {
Wyatt Hepler7ded6da2020-03-11 18:24:43 -07001078 DBG("Key descriptors: count %zu", entry_cache_.total_entries());
1079 for (auto& metadata : entry_cache_) {
1080 DBG(" - Key: %s, hash %#zx, transaction ID %zu, first address %#zx",
Wyatt Hepler02946272020-03-18 10:36:22 -07001081 metadata.state() == EntryState::kDeleted ? "Deleted" : "Valid",
Wyatt Hepler7ded6da2020-03-11 18:24:43 -07001082 static_cast<size_t>(metadata.hash()),
1083 static_cast<size_t>(metadata.transaction_id()),
1084 static_cast<size_t>(metadata.first_address()));
David Rogerscf680ab2020-02-12 23:28:32 -08001085 }
1086}
1087
Wyatt Hepler2ad60672020-01-21 08:00:16 -08001088} // namespace pw::kvs