1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/services/storage/indexed_db/leveldb/fake_leveldb_factory.h"
#include <mutex>
#include <optional>
#include <string>
#include <thread>
#include <utility>
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/synchronization/lock.h"
#include "base/thread_annotations.h"
#include "components/services/storage/indexed_db/leveldb/leveldb_state.h"
#include "third_party/leveldatabase/src/include/leveldb/db.h"
#include "third_party/leveldatabase/src/include/leveldb/slice.h"
#include "third_party/leveldatabase/src/include/leveldb/status.h"
namespace content::indexed_db {
namespace {
class FlakyIterator;
// FlakyDB is a leveldb::DB wrapper that will flake in a predictable pattern
// given a queue of |flake_points|. After a flake, the database will continue
// operating as normal.
class FlakyDB : public leveldb::DB {
public:
FlakyDB(std::unique_ptr<leveldb::DB> db,
std::queue<FakeLevelDBFactory::FlakePoint> flake_points)
: db_(std::move(db)), flake_points_(std::move(flake_points)) {
DCHECK(db_);
}
~FlakyDB() override = default;
// Returns a FlakePoint if the current operation is flaky with the flake
// information. Otherwise it returns a std::nullopt.
// This call is threadsafe.
std::optional<FakeLevelDBFactory::FlakePoint> FlakePointForNextOperation() {
base::AutoLock lock(lock_);
if (flake_points_.empty())
return std::nullopt;
DCHECK_GE(flake_points_.front().calls_before_flake, 0);
flake_points_.front().calls_before_flake--;
FakeLevelDBFactory::FlakePoint flake_point = flake_points_.front();
if (flake_point.calls_before_flake >= 0)
return std::nullopt;
flake_points_.pop();
return flake_point;
}
// Implementations of the DB interface
leveldb::Status Put(const leveldb::WriteOptions& options,
const leveldb::Slice& key,
const leveldb::Slice& value) override {
std::optional<FakeLevelDBFactory::FlakePoint> flake_status =
FlakePointForNextOperation();
if (flake_status.has_value())
return flake_status->flake_status;
return db_->Put(options, key, value);
}
leveldb::Status Delete(const leveldb::WriteOptions& options,
const leveldb::Slice& key) override {
std::optional<FakeLevelDBFactory::FlakePoint> flake_status =
FlakePointForNextOperation();
if (flake_status.has_value())
return flake_status->flake_status;
return db_->Delete(options, key);
}
leveldb::Status Write(const leveldb::WriteOptions& options,
leveldb::WriteBatch* updates) override {
std::optional<FakeLevelDBFactory::FlakePoint> flake_status =
FlakePointForNextOperation();
if (flake_status.has_value())
return flake_status->flake_status;
return db_->Write(options, updates);
}
leveldb::Status Get(const leveldb::ReadOptions& options,
const leveldb::Slice& key,
std::string* value) override {
std::optional<FakeLevelDBFactory::FlakePoint> flake_status =
FlakePointForNextOperation();
if (flake_status.has_value()) {
if (flake_status->flake_status.ok())
*value = flake_status->replaced_get_result;
return flake_status->flake_status;
}
return db_->Get(options, key, value);
}
leveldb::Iterator* NewIterator(const leveldb::ReadOptions& options) override;
const leveldb::Snapshot* GetSnapshot() override { return db_->GetSnapshot(); }
void ReleaseSnapshot(const leveldb::Snapshot* snapshot) override {
return db_->ReleaseSnapshot(snapshot);
}
bool GetProperty(const leveldb::Slice& property,
std::string* value) override {
return db_->GetProperty(property, value);
}
void GetApproximateSizes(const leveldb::Range* range,
int n,
uint64_t* sizes) override {
db_->GetApproximateSizes(range, n, sizes);
}
void CompactRange(const leveldb::Slice* begin,
const leveldb::Slice* end) override {
db_->CompactRange(begin, end);
}
private:
base::Lock lock_;
const std::unique_ptr<leveldb::DB> db_;
std::queue<FakeLevelDBFactory::FlakePoint> flake_points_ GUARDED_BY(lock_);
};
// FlakyIterator calls its parent's FlakePointForNextOperation method to
// determine the validity at any position. Because an iterator maintains state,
// it stores the current result from FlakePointForNextOperation in the
// current_flake_ variable. This is reset & optionally set during a Seek*, Next,
// or Prev call. LevelDB iterators are not thread-safe.
class FlakyIterator : public leveldb::Iterator {
public:
FlakyIterator(FlakyDB* db, std::unique_ptr<leveldb::Iterator> delegate)
: db_(db), delegate_(std::move(delegate)) {}
bool Valid() const override {
if (current_flake_ && !current_flake_->flake_status.ok())
return false;
return delegate_->Valid();
}
void SeekToFirst() override {
current_flake_ = db_->FlakePointForNextOperation();
delegate_->SeekToFirst();
}
void SeekToLast() override {
current_flake_ = db_->FlakePointForNextOperation();
delegate_->SeekToLast();
}
void Seek(const leveldb::Slice& target) override {
current_flake_ = db_->FlakePointForNextOperation();
delegate_->Seek(target);
}
void Next() override {
current_flake_ = db_->FlakePointForNextOperation();
delegate_->Next();
}
void Prev() override {
current_flake_ = db_->FlakePointForNextOperation();
delegate_->Prev();
}
leveldb::Slice key() const override {
if (current_flake_ && !current_flake_->flake_status.ok())
return leveldb::Slice();
return delegate_->key();
}
leveldb::Slice value() const override {
if (current_flake_ && !current_flake_->flake_status.ok())
return leveldb::Slice();
if (current_flake_)
return current_flake_->replaced_get_result;
return delegate_->value();
}
leveldb::Status status() const override {
if (current_flake_)
return current_flake_->flake_status;
return delegate_->status();
}
private:
// The raw pointer is safe because iterators must be deleted before their
// databases.
const raw_ptr<FlakyDB> db_;
// The current flake is cleared & optionally set on every call to Seek*, Next,
// and Prev.
std::optional<FakeLevelDBFactory::FlakePoint> current_flake_;
std::unique_ptr<leveldb::Iterator> delegate_;
};
leveldb::Iterator* FlakyDB::NewIterator(const leveldb::ReadOptions& options) {
return new FlakyIterator(this, base::WrapUnique(db_->NewIterator(options)));
}
class BrokenIterator : public leveldb::Iterator {
public:
BrokenIterator(leveldb::Status returned_status)
: returned_status_(returned_status) {}
bool Valid() const override { return false; }
void SeekToFirst() override {}
void SeekToLast() override {}
void Seek(const leveldb::Slice& target) override {}
void Next() override {}
void Prev() override {}
leveldb::Slice key() const override { return leveldb::Slice(); }
leveldb::Slice value() const override { return leveldb::Slice(); }
leveldb::Status status() const override { return returned_status_; }
private:
leveldb::Status returned_status_;
};
// BrokenDB is a fake leveldb::DB that will return a given error status on every
// method call, or no-op if there is nothing to return.
class BrokenDB : public leveldb::DB {
public:
BrokenDB(leveldb::Status returned_status)
: returned_status_(std::move(returned_status)) {}
~BrokenDB() override = default;
// Implementations of the DB interface
leveldb::Status Put(const leveldb::WriteOptions&,
const leveldb::Slice& key,
const leveldb::Slice& value) override {
return returned_status_;
}
leveldb::Status Delete(const leveldb::WriteOptions&,
const leveldb::Slice& key) override {
return returned_status_;
}
leveldb::Status Write(const leveldb::WriteOptions& options,
leveldb::WriteBatch* updates) override {
return returned_status_;
}
leveldb::Status Get(const leveldb::ReadOptions& options,
const leveldb::Slice& key,
std::string* value) override {
return returned_status_;
}
leveldb::Iterator* NewIterator(const leveldb::ReadOptions&) override {
return new BrokenIterator(returned_status_);
}
const leveldb::Snapshot* GetSnapshot() override { return nullptr; }
void ReleaseSnapshot(const leveldb::Snapshot* snapshot) override {}
bool GetProperty(const leveldb::Slice& property,
std::string* value) override {
return false;
}
void GetApproximateSizes(const leveldb::Range* range,
int n,
uint64_t* sizes) override {}
void CompactRange(const leveldb::Slice* begin,
const leveldb::Slice* end) override {}
private:
leveldb::Status returned_status_;
};
// BreakOnCallbackDB is a leveldb::DB wrapper that will return a given error
// status or fail every method call after the |Break| method is called. This is
// thread-safe, just like leveldb::DB.
class BreakOnCallbackDB : public leveldb::DB {
public:
BreakOnCallbackDB(std::unique_ptr<leveldb::DB> db) : db_(std::move(db)) {}
~BreakOnCallbackDB() override = default;
void Break(leveldb::Status broken_status) {
base::AutoLock lock(lock_);
broken_status_ = std::move(broken_status);
}
// Implementations of the DB interface
leveldb::Status Put(const leveldb::WriteOptions& options,
const leveldb::Slice& key,
const leveldb::Slice& value) override {
{
base::AutoLock lock(lock_);
if (broken_status_)
return broken_status_.value();
}
return db_->Put(options, key, value);
}
leveldb::Status Delete(const leveldb::WriteOptions& options,
const leveldb::Slice& key) override {
{
base::AutoLock lock(lock_);
if (broken_status_)
return broken_status_.value();
}
return db_->Delete(options, key);
}
leveldb::Status Write(const leveldb::WriteOptions& options,
leveldb::WriteBatch* updates) override {
{
base::AutoLock lock(lock_);
if (broken_status_)
return broken_status_.value();
}
return db_->Write(options, updates);
}
leveldb::Status Get(const leveldb::ReadOptions& options,
const leveldb::Slice& key,
std::string* value) override {
{
base::AutoLock lock(lock_);
if (broken_status_)
return broken_status_.value();
}
return db_->Get(options, key, value);
}
leveldb::Iterator* NewIterator(const leveldb::ReadOptions& options) override {
{
base::AutoLock lock(lock_);
if (broken_status_)
return new BrokenIterator(broken_status_.value());
}
return db_->NewIterator(options);
}
const leveldb::Snapshot* GetSnapshot() override {
{
base::AutoLock lock(lock_);
if (broken_status_)
return nullptr;
}
return db_->GetSnapshot();
}
void ReleaseSnapshot(const leveldb::Snapshot* snapshot) override {
{
base::AutoLock lock(lock_);
if (broken_status_)
return;
}
return db_->ReleaseSnapshot(snapshot);
}
bool GetProperty(const leveldb::Slice& property,
std::string* value) override {
{
base::AutoLock lock(lock_);
if (broken_status_)
return false;
}
return db_->GetProperty(property, value);
}
void GetApproximateSizes(const leveldb::Range* range,
int n,
uint64_t* sizes) override {
{
base::AutoLock lock(lock_);
if (broken_status_)
return;
}
db_->GetApproximateSizes(range, n, sizes);
}
void CompactRange(const leveldb::Slice* begin,
const leveldb::Slice* end) override {
{
base::AutoLock lock(lock_);
if (broken_status_)
return;
}
db_->CompactRange(begin, end);
}
private:
base::Lock lock_;
const std::unique_ptr<leveldb::DB> db_;
std::optional<leveldb::Status> broken_status_ GUARDED_BY(lock_);
};
} // namespace
// static
std::unique_ptr<leveldb::DB> FakeLevelDBFactory::CreateFlakyDB(
std::unique_ptr<leveldb::DB> db,
std::queue<FlakePoint> flake_points) {
return std::make_unique<FlakyDB>(std::move(db), std::move(flake_points));
}
// static
std::pair<std::unique_ptr<leveldb::DB>,
base::OnceCallback<void(leveldb::Status)>>
FakeLevelDBFactory::CreateBreakableDB(std::unique_ptr<leveldb::DB> db) {
std::unique_ptr<BreakOnCallbackDB> breakable_db =
std::make_unique<BreakOnCallbackDB>(std::move(db));
base::OnceCallback<void(leveldb::Status)> callback = base::BindOnce(
&BreakOnCallbackDB::Break, base::Unretained(breakable_db.get()));
return {std::move(breakable_db), std::move(callback)};
}
// static
scoped_refptr<LevelDBState> FakeLevelDBFactory::GetBrokenLevelDB(
leveldb::Status error_to_return,
const base::FilePath& reported_file_path) {
return LevelDBState::CreateForDiskDB(
leveldb::BytewiseComparator(),
std::make_unique<BrokenDB>(error_to_return), reported_file_path);
}
} // namespace content::indexed_db
|