db_impl_readonly.cc 7.8 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
S
Siying Dong 已提交
2 3 4
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).
H
heyongqiang 已提交
5

6
#include "db/db_impl/db_impl_readonly.h"
7 8

#include "db/compacted_db_impl.h"
V
Vijay Nadimpalli 已提交
9
#include "db/db_impl/db_impl.h"
L
Lei Jin 已提交
10
#include "db/db_iter.h"
A
Andrew Kryczka 已提交
11
#include "db/merge_context.h"
12
#include "monitoring/perf_context_imp.h"
H
heyongqiang 已提交
13

14
namespace rocksdb {
H
heyongqiang 已提交
15

16 17
#ifndef ROCKSDB_LITE

L
Lei Jin 已提交
18
DBImplReadOnly::DBImplReadOnly(const DBOptions& db_options,
I
Igor Canadi 已提交
19
                               const std::string& dbname)
L
Lei Jin 已提交
20
    : DBImpl(db_options, dbname) {
21 22
  ROCKS_LOG_INFO(immutable_db_options_.info_log,
                 "Opening the db in read only mode");
23
  LogFlush(immutable_db_options_.info_log);
H
heyongqiang 已提交
24 25
}

26
DBImplReadOnly::~DBImplReadOnly() {}
H
heyongqiang 已提交
27 28

// Implementations of the DB interface
L
Lei Jin 已提交
29
Status DBImplReadOnly::Get(const ReadOptions& read_options,
30
                           ColumnFamilyHandle* column_family, const Slice& key,
M
Maysam Yabandeh 已提交
31 32
                           PinnableSlice* pinnable_val) {
  assert(pinnable_val != nullptr);
33 34
  // TODO: stopwatch DB_GET needed?, perf timer needed?
  PERF_TIMER_GUARD(get_snapshot_time);
H
heyongqiang 已提交
35 36
  Status s;
  SequenceNumber snapshot = versions_->LastSequence();
37 38
  auto cfh = reinterpret_cast<ColumnFamilyHandleImpl*>(column_family);
  auto cfd = cfh->cfd();
39 40 41 42 43 44
  if (tracer_) {
    InstrumentedMutexLock lock(&trace_mutex_);
    if (tracer_) {
      tracer_->Get(column_family, key);
    }
  }
45
  SuperVersion* super_version = cfd->GetSuperVersion();
46
  MergeContext merge_context;
47
  SequenceNumber max_covering_tombstone_seq = 0;
H
heyongqiang 已提交
48
  LookupKey lkey(key, snapshot);
49
  PERF_TIMER_STOP(get_snapshot_time);
M
Maysam Yabandeh 已提交
50
  if (super_version->mem->Get(lkey, pinnable_val->GetSelf(), &s, &merge_context,
51
                              &max_covering_tombstone_seq, read_options)) {
M
Maysam Yabandeh 已提交
52
    pinnable_val->PinSelf();
53
    RecordTick(stats_, MEMTABLE_HIT);
54
  } else {
55
    PERF_TIMER_GUARD(get_from_output_files_time);
M
Maysam Yabandeh 已提交
56
    super_version->current->Get(read_options, lkey, pinnable_val, &s,
57
                                &merge_context, &max_covering_tombstone_seq);
58
    RecordTick(stats_, MEMTABLE_MISS);
59
  }
60 61 62
  RecordTick(stats_, NUMBER_KEYS_READ);
  size_t size = pinnable_val->size();
  RecordTick(stats_, BYTES_READ, size);
S
Siying Dong 已提交
63
  RecordInHistogram(stats_, BYTES_PER_READ, size);
64
  PERF_COUNTER_ADD(get_read_bytes, size);
H
heyongqiang 已提交
65 66 67
  return s;
}

68
Iterator* DBImplReadOnly::NewIterator(const ReadOptions& read_options,
69 70 71
                                      ColumnFamilyHandle* column_family) {
  auto cfh = reinterpret_cast<ColumnFamilyHandleImpl*>(column_family);
  auto cfd = cfh->cfd();
72 73
  SuperVersion* super_version = cfd->GetSuperVersion()->Ref();
  SequenceNumber latest_snapshot = versions_->LastSequence();
74 75 76 77 78
  SequenceNumber read_seq =
      read_options.snapshot != nullptr
          ? reinterpret_cast<const SnapshotImpl*>(read_options.snapshot)
                ->number_
          : latest_snapshot;
Y
Yi Wu 已提交
79
  ReadCallback* read_callback = nullptr;  // No read callback provided.
I
Igor Canadi 已提交
80
  auto db_iter = NewArenaWrappedDbIterator(
81
      env_, read_options, *cfd->ioptions(), super_version->mutable_cf_options,
82
      read_seq,
83
      super_version->mutable_cf_options.max_sequential_skip_in_iterations,
Y
Yi Wu 已提交
84
      super_version->version_number, read_callback);
A
Andrew Kryczka 已提交
85 86
  auto internal_iter =
      NewInternalIterator(read_options, cfd, super_version, db_iter->GetArena(),
87
                          db_iter->GetRangeDelAggregator(), read_seq);
I
Igor Canadi 已提交
88 89 90 91 92
  db_iter->SetIterUnderDBIter(internal_iter);
  return db_iter;
}

Status DBImplReadOnly::NewIterators(
93
    const ReadOptions& read_options,
I
Igor Canadi 已提交
94 95
    const std::vector<ColumnFamilyHandle*>& column_families,
    std::vector<Iterator*>* iterators) {
Y
Yi Wu 已提交
96
  ReadCallback* read_callback = nullptr;  // No read callback provided.
I
Igor Canadi 已提交
97 98 99 100 101 102
  if (iterators == nullptr) {
    return Status::InvalidArgument("iterators not allowed to be nullptr");
  }
  iterators->clear();
  iterators->reserve(column_families.size());
  SequenceNumber latest_snapshot = versions_->LastSequence();
103 104 105 106 107
  SequenceNumber read_seq =
      read_options.snapshot != nullptr
          ? reinterpret_cast<const SnapshotImpl*>(read_options.snapshot)
                ->number_
          : latest_snapshot;
I
Igor Canadi 已提交
108 109

  for (auto cfh : column_families) {
110 111 112
    auto* cfd = reinterpret_cast<ColumnFamilyHandleImpl*>(cfh)->cfd();
    auto* sv = cfd->GetSuperVersion()->Ref();
    auto* db_iter = NewArenaWrappedDbIterator(
113
        env_, read_options, *cfd->ioptions(), sv->mutable_cf_options, read_seq,
114
        sv->mutable_cf_options.max_sequential_skip_in_iterations,
Y
Yi Wu 已提交
115
        sv->version_number, read_callback);
A
Andrew Kryczka 已提交
116 117
    auto* internal_iter =
        NewInternalIterator(read_options, cfd, sv, db_iter->GetArena(),
118
                            db_iter->GetRangeDelAggregator(), read_seq);
I
Igor Canadi 已提交
119 120 121 122 123
    db_iter->SetIterUnderDBIter(internal_iter);
    iterators->push_back(db_iter);
  }

  return Status::OK();
H
heyongqiang 已提交
124 125 126
}

Status DB::OpenForReadOnly(const Options& options, const std::string& dbname,
A
Andrew Kryczka 已提交
127
                           DB** dbptr, bool /*error_if_log_file_exist*/) {
128
  *dbptr = nullptr;
H
heyongqiang 已提交
129

L
Lei Jin 已提交
130 131 132 133 134 135 136
  // Try to first open DB as fully compacted DB
  Status s;
  s = CompactedDBImpl::Open(options, dbname, dbptr);
  if (s.ok()) {
    return s;
  }

137 138 139 140
  DBOptions db_options(options);
  ColumnFamilyOptions cf_options(options);
  std::vector<ColumnFamilyDescriptor> column_families;
  column_families.push_back(
141 142 143
      ColumnFamilyDescriptor(kDefaultColumnFamilyName, cf_options));
  std::vector<ColumnFamilyHandle*> handles;

L
Lei Jin 已提交
144
  s = DB::OpenForReadOnly(db_options, dbname, column_families, &handles, dbptr);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  if (s.ok()) {
    assert(handles.size() == 1);
    // i can delete the handle since DBImpl is always holding a
    // reference to default column family
    delete handles[0];
  }
  return s;
}

Status DB::OpenForReadOnly(
    const DBOptions& db_options, const std::string& dbname,
    const std::vector<ColumnFamilyDescriptor>& column_families,
    std::vector<ColumnFamilyHandle*>* handles, DB** dbptr,
    bool error_if_log_file_exist) {
  *dbptr = nullptr;
  handles->clear();
I
Igor Canadi 已提交
161

162
  SuperVersionContext sv_context(/* create_superversion */ true);
I
Igor Canadi 已提交
163 164
  DBImplReadOnly* impl = new DBImplReadOnly(db_options, dbname);
  impl->mutex_.Lock();
165 166
  Status s = impl->Recover(column_families, true /* read only */,
                           error_if_log_file_exist);
167 168 169 170 171 172 173 174 175 176 177 178
  if (s.ok()) {
    // set column family handles
    for (auto cf : column_families) {
      auto cfd =
          impl->versions_->GetColumnFamilySet()->GetColumnFamily(cf.name);
      if (cfd == nullptr) {
        s = Status::InvalidArgument("Column family not found: ", cf.name);
        break;
      }
      handles->push_back(new ColumnFamilyHandleImpl(cfd, impl, &impl->mutex_));
    }
  }
179
  if (s.ok()) {
180
    for (auto cfd : *impl->versions_->GetColumnFamilySet()) {
181 182
      sv_context.NewSuperVersion();
      cfd->InstallSuperVersion(&sv_context, &impl->mutex_);
183
    }
184
  }
H
heyongqiang 已提交
185
  impl->mutex_.Unlock();
186
  sv_context.Clean();
H
heyongqiang 已提交
187 188
  if (s.ok()) {
    *dbptr = impl;
Y
Yueh-Hsuan Chiang 已提交
189 190 191 192
    for (auto* h : *handles) {
      impl->NewThreadStatusCfInfo(
          reinterpret_cast<ColumnFamilyHandleImpl*>(h)->cfd());
    }
H
heyongqiang 已提交
193
  } else {
194 195 196 197
    for (auto h : *handles) {
      delete h;
    }
    handles->clear();
H
heyongqiang 已提交
198 199 200 201 202
    delete impl;
  }
  return s;
}

203
#else   // !ROCKSDB_LITE
204

205 206 207
Status DB::OpenForReadOnly(const Options& /*options*/,
                           const std::string& /*dbname*/, DB** /*dbptr*/,
                           bool /*error_if_log_file_exist*/) {
208 209 210 211
  return Status::NotSupported("Not supported in ROCKSDB_LITE.");
}

Status DB::OpenForReadOnly(
212 213 214 215
    const DBOptions& /*db_options*/, const std::string& /*dbname*/,
    const std::vector<ColumnFamilyDescriptor>& /*column_families*/,
    std::vector<ColumnFamilyHandle*>* /*handles*/, DB** /*dbptr*/,
    bool /*error_if_log_file_exist*/) {
216 217 218
  return Status::NotSupported("Not supported in ROCKSDB_LITE.");
}
#endif  // !ROCKSDB_LITE
219

220
}  // namespace rocksdb