summaryrefslogtreecommitdiff
path: root/chromium/components/services/storage/indexed_db/transactional_leveldb/transactional_leveldb_database.cc
blob: 473a0b1d85a4c957b1e40065b4423e6e51657a0d (plain)
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
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// 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/transactional_leveldb/transactional_leveldb_database.h"

#include <inttypes.h>
#include <stdint.h>

#include <algorithm>
#include <cerrno>
#include <memory>
#include <utility>

#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/files/file.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/system/sys_info.h"
#include "base/time/default_clock.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/process_memory_dump.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "components/services/storage/indexed_db/scopes/leveldb_scopes.h"
#include "components/services/storage/indexed_db/transactional_leveldb/leveldb_write_batch.h"
#include "components/services/storage/indexed_db/transactional_leveldb/transactional_leveldb_factory.h"
#include "components/services/storage/indexed_db/transactional_leveldb/transactional_leveldb_iterator.h"
#include "components/services/storage/indexed_db/transactional_leveldb/transactional_leveldb_transaction.h"
#include "third_party/leveldatabase/env_chromium.h"
#include "third_party/leveldatabase/leveldb_chrome.h"
#include "third_party/leveldatabase/src/include/leveldb/comparator.h"
#include "third_party/leveldatabase/src/include/leveldb/db.h"
#include "third_party/leveldatabase/src/include/leveldb/slice.h"

using base::StringPiece;
using leveldb_env::DBTracker;

namespace content {

namespace {

// Forcing flushes to disk at the end of a transaction guarantees that the
// data hit disk, but drastically impacts throughput when the filesystem is
// busy with background compactions. Not syncing trades off reliability for
// performance. Note that background compactions which move data from the
// log to SSTs are always done with reliable writes.
//
// Sync writes are necessary on Windows for quota calculations; POSIX
// calculates file sizes correctly even when not synced to disk.
#if BUILDFLAG(IS_WIN)
const bool kSyncWrites = true;
#else
// TODO(dgrogan): Either remove the #if block or change this back to false.
// See http://crbug.com/338385.
const bool kSyncWrites = true;
#endif

}  // namespace

LevelDBSnapshot::LevelDBSnapshot(TransactionalLevelDBDatabase* db)
    : db_(db->db()), snapshot_(db_->GetSnapshot()) {}

LevelDBSnapshot::~LevelDBSnapshot() {
  db_->ReleaseSnapshot(snapshot_);
}

// static
constexpr const size_t
    TransactionalLevelDBDatabase::kDefaultMaxOpenIteratorsPerDatabase;

TransactionalLevelDBDatabase::TransactionalLevelDBDatabase(
    scoped_refptr<LevelDBState> level_db_state,
    std::unique_ptr<LevelDBScopes> leveldb_scopes,
    TransactionalLevelDBFactory* class_factory,
    scoped_refptr<base::SequencedTaskRunner> task_runner,
    size_t max_open_iterators)
    : level_db_state_(std::move(level_db_state)),
      scopes_(std::move(leveldb_scopes)),
      class_factory_(class_factory),
      clock_(new base::DefaultClock()),
      iterator_lru_(max_open_iterators) {
  if (task_runner) {
    base::trace_event::MemoryDumpManager::GetInstance()
        ->RegisterDumpProviderWithSequencedTaskRunner(
            this, "IndexedDBBackingStore", std::move(task_runner),
            base::trace_event::MemoryDumpProvider::Options());
  }
  DCHECK(max_open_iterators);
}

TransactionalLevelDBDatabase::~TransactionalLevelDBDatabase() {
  LOCAL_HISTOGRAM_COUNTS_10000("Storage.IndexedDB.LevelDB.MaxIterators",
                               max_iterators_);
  base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
      this);
}

leveldb::Status TransactionalLevelDBDatabase::Put(const StringPiece& key,
                                                  std::string* value) {
  leveldb::WriteOptions write_options;
  write_options.sync = kSyncWrites;

  const leveldb::Status s =
      db()->Put(write_options, leveldb_env::MakeSlice(key),
                leveldb_env::MakeSlice(*value));
  EvictAllIterators();
  last_modified_ = clock_->Now();
  return s;
}

leveldb::Status TransactionalLevelDBDatabase::Remove(const StringPiece& key) {
  leveldb::WriteOptions write_options;
  write_options.sync = kSyncWrites;

  const leveldb::Status s =
      db()->Delete(write_options, leveldb_env::MakeSlice(key));

  EvictAllIterators();
  last_modified_ = clock_->Now();
  return s;
}

leveldb::Status TransactionalLevelDBDatabase::Get(const StringPiece& key,
                                                  std::string* value,
                                                  bool* found) {
  *found = false;
  leveldb::ReadOptions read_options = DefaultReadOptions();

  const leveldb::Status s =
      db()->Get(read_options, leveldb_env::MakeSlice(key), value);
  if (s.ok()) {
    *found = true;
    return s;
  }
  if (LIKELY(s.IsNotFound()))
    return leveldb::Status::OK();
  return s;
}

leveldb::Status TransactionalLevelDBDatabase::Write(
    LevelDBWriteBatch* write_batch) {
  DCHECK(write_batch);
  base::TimeTicks begin_time = base::TimeTicks::Now();
  leveldb::WriteOptions write_options;
  write_options.sync = kSyncWrites;

  const leveldb::Status s =
      db()->Write(write_options, write_batch->write_batch_.get());
  UMA_HISTOGRAM_TIMES("WebCore.IndexedDB.LevelDB.WriteTime",
                      base::TimeTicks::Now() - begin_time);
  EvictAllIterators();
  last_modified_ = clock_->Now();
  return s;
}

std::unique_ptr<TransactionalLevelDBIterator>
TransactionalLevelDBDatabase::CreateIterator(leveldb::ReadOptions options) {
  DCHECK(!options.snapshot);
  num_iterators_++;
  max_iterators_ = std::max(max_iterators_, num_iterators_);
  std::unique_ptr<LevelDBSnapshot> snapshot =
      std::make_unique<LevelDBSnapshot>(this);
  options.snapshot = snapshot->snapshot();
  // Iterator isn't added to |iterator_lru_| until it is used, as memory isn't
  // loaded for the iterator until its first Seek call.
  std::unique_ptr<leveldb::Iterator> i(db()->NewIterator(options));
  auto it = class_factory_->CreateIterator(
      std::move(i), weak_factory_for_iterators_.GetWeakPtr(),
      base::WeakPtr<TransactionalLevelDBTransaction>(), std::move(snapshot));
  db_only_loaded_iterators_.insert(it.get());
  return it;
}

std::unique_ptr<TransactionalLevelDBIterator>
TransactionalLevelDBDatabase::CreateIterator(
    base::WeakPtr<TransactionalLevelDBTransaction> txn,
    leveldb::ReadOptions options) {
  DCHECK(!options.snapshot);
  // Note - this iterator is NOT added to |db_only_*_iterators_|, as it is
  // associated with a transaction, and will be in that transaction's
  // iterator lists. The implementation assumes that the iterator lives in
  // either the database list or the transaction list, and not both.
  num_iterators_++;
  max_iterators_ = std::max(max_iterators_, num_iterators_);
  std::unique_ptr<LevelDBSnapshot> snapshot =
      std::make_unique<LevelDBSnapshot>(this);
  options.snapshot = snapshot->snapshot();
  // Iterator isn't added to |iterator_lru_| until it is used, as memory isn't
  // loaded for the iterator until its first Seek call.
  std::unique_ptr<leveldb::Iterator> i(db()->NewIterator(options));
  return class_factory_->CreateIterator(
      std::move(i), weak_factory_for_iterators_.GetWeakPtr(), std::move(txn),
      std::move(snapshot));
}

void TransactionalLevelDBDatabase::Compact(const base::StringPiece& start,
                                           const base::StringPiece& stop) {
  TRACE_EVENT0("leveldb", "LevelDBDatabase::Compact");
  const leveldb::Slice start_slice = leveldb_env::MakeSlice(start);
  const leveldb::Slice stop_slice = leveldb_env::MakeSlice(stop);
  // nullptr batch means just wait for earlier writes to be done
  db()->Write(leveldb::WriteOptions(), nullptr);
  db()->CompactRange(&start_slice, &stop_slice);
}

void TransactionalLevelDBDatabase::CompactAll() {
  db()->CompactRange(nullptr, nullptr);
}

leveldb::ReadOptions TransactionalLevelDBDatabase::DefaultReadOptions() {
  leveldb::ReadOptions read_options;
  // Always verify checksums on leveldb blocks for IndexedDB databases, which
  // detects corruptions.
  read_options.verify_checksums = true;
  read_options.snapshot = nullptr;
  return read_options;
}

bool TransactionalLevelDBDatabase::OnMemoryDump(
    const base::trace_event::MemoryDumpArgs& args,
    base::trace_event::ProcessMemoryDump* pmd) {
  if (!level_db_state_)
    return false;
  // All leveldb databases are already dumped by leveldb_env::DBTracker. Add
  // an edge to the existing database.
  auto* db_tracker_dump =
      leveldb_env::DBTracker::GetOrCreateAllocatorDump(pmd, db());
  if (!db_tracker_dump)
    return true;

  auto* db_dump = pmd->CreateAllocatorDump(
      base::StringPrintf("site_storage/index_db/db_0x%" PRIXPTR,
                         reinterpret_cast<uintptr_t>(db())));
  db_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
                     base::trace_event::MemoryAllocatorDump::kUnitsBytes,
                     db_tracker_dump->GetSizeInternal());
  pmd->AddOwnershipEdge(db_dump->guid(), db_tracker_dump->guid());

  if (env() && leveldb_chrome::IsMemEnv(env())) {
    // All leveldb env's are already dumped by leveldb_env::DBTracker. Add
    // an edge to the existing env.
    auto* env_tracker_dump = DBTracker::GetOrCreateAllocatorDump(pmd, env());
    auto* env_dump = pmd->CreateAllocatorDump(
        base::StringPrintf("site_storage/index_db/memenv_0x%" PRIXPTR,
                           reinterpret_cast<uintptr_t>(env())));
    env_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
                        base::trace_event::MemoryAllocatorDump::kUnitsBytes,
                        env_tracker_dump->GetSizeInternal());
    pmd->AddOwnershipEdge(env_dump->guid(), env_tracker_dump->guid());
  }

  // Dumps in BACKGROUND mode can only have whitelisted strings (and there are
  // currently none) so return early.
  if (args.level_of_detail ==
      base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND) {
    return true;
  }

  db_dump->AddString("file_name", "", level_db_state_->name_for_tracing());

  return true;
}

void TransactionalLevelDBDatabase::SetClockForTesting(
    std::unique_ptr<base::Clock> clock) {
  clock_ = std::move(clock);
}

TransactionalLevelDBDatabase::DetachIteratorOnDestruct::
    DetachIteratorOnDestruct(TransactionalLevelDBIterator* it)
    : it(it) {}
TransactionalLevelDBDatabase::DetachIteratorOnDestruct::
    DetachIteratorOnDestruct(DetachIteratorOnDestruct&& that) {
  it = that.it;
  that.it = nullptr;
}
TransactionalLevelDBDatabase::DetachIteratorOnDestruct::
    ~DetachIteratorOnDestruct() {
  if (it)
    it->EvictLevelDBIterator();
}

void TransactionalLevelDBDatabase::EvictAllIterators() {
  if (db_only_loaded_iterators_.empty())
    return;
  is_evicting_all_loaded_iterators_ = true;
  base::flat_set<TransactionalLevelDBIterator*> to_be_evicted =
      std::move(db_only_loaded_iterators_);
  for (TransactionalLevelDBIterator* iter : to_be_evicted) {
    iter->EvictLevelDBIterator();
  }
  is_evicting_all_loaded_iterators_ = false;
}

void TransactionalLevelDBDatabase::OnIteratorUsed(
    TransactionalLevelDBIterator* iter) {
  // This line updates the LRU if the item exists.
  if (iterator_lru_.Get(iter) != iterator_lru_.end())
    return;
  DetachIteratorOnDestruct purger(iter);
  iterator_lru_.Put(iter, std::move(purger));
}

void TransactionalLevelDBDatabase::OnIteratorLoaded(
    TransactionalLevelDBIterator* iterator) {
  DCHECK(db_only_evicted_iterators_.find(iterator) !=
         db_only_evicted_iterators_.end());
  db_only_loaded_iterators_.insert(iterator);
  db_only_evicted_iterators_.erase(iterator);
}

void TransactionalLevelDBDatabase::OnIteratorEvicted(
    TransactionalLevelDBIterator* iterator) {
  DCHECK(db_only_loaded_iterators_.find(iterator) !=
             db_only_loaded_iterators_.end() ||
         is_evicting_all_loaded_iterators_);
  db_only_loaded_iterators_.erase(iterator);
  db_only_evicted_iterators_.insert(iterator);
}

void TransactionalLevelDBDatabase::OnIteratorDestroyed(
    TransactionalLevelDBIterator* iter) {
  DCHECK_GT(num_iterators_, 0u);
  db_only_loaded_iterators_.erase(iter);
  db_only_evicted_iterators_.erase(iter);
  --num_iterators_;
  auto lru_iterator = iterator_lru_.Peek(iter);
  if (lru_iterator == iterator_lru_.end())
    return;
  // Set the |lru_iterator|'s stored iterator to |nullptr| to avoid it
  // unnecessarily calling EvictLevelDBIterator.
  lru_iterator->second.it = nullptr;
  iterator_lru_.Erase(lru_iterator);
}

}  // namespace content