db_filesnapshot.cc 5.77 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
Siying Dong's avatar
Siying Dong committed
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).
5
//
6

Igor Canadi's avatar
Igor Canadi committed
7
8
#ifndef ROCKSDB_LITE

Wanning Jiang's avatar
Wanning Jiang committed
9
#include <stdint.h>
10
#include <algorithm>
11
#include <cinttypes>
12
#include <string>
13
#include "db/db_impl/db_impl.h"
Igor Canadi's avatar
Igor Canadi committed
14
#include "db/job_context.h"
15
#include "db/version_set.h"
16
17
#include "file/file_util.h"
#include "file/filename.h"
Wanning Jiang's avatar
Wanning Jiang committed
18
#include "port/port.h"
19
20
#include "rocksdb/db.h"
#include "rocksdb/env.h"
21
#include "test_util/sync_point.h"
22
#include "util/mutexlock.h"
23

24
namespace ROCKSDB_NAMESPACE {
25
26

Status DBImpl::DisableFileDeletions() {
27
  InstrumentedMutexLock l(&mutex_);
28
29
  ++disable_delete_obsolete_files_;
  if (disable_delete_obsolete_files_ == 1) {
30
    ROCKS_LOG_INFO(immutable_db_options_.info_log, "File Deletions Disabled");
31
  } else {
32
33
34
    ROCKS_LOG_WARN(immutable_db_options_.info_log,
                   "File Deletions Disabled, but already disabled. Counter: %d",
                   disable_delete_obsolete_files_);
35
  }
36
37
38
  return Status::OK();
}

39
Status DBImpl::EnableFileDeletions(bool force) {
40
41
42
  // Job id == 0 means that this is not our background process, but rather
  // user thread
  JobContext job_context(0);
43
  bool file_deletion_enabled = false;
Igor Canadi's avatar
Igor Canadi committed
44
  {
45
    InstrumentedMutexLock l(&mutex_);
46
47
48
49
50
51
52
    if (force) {
      // if force, we need to enable file deletions right away
      disable_delete_obsolete_files_ = 0;
    } else if (disable_delete_obsolete_files_ > 0) {
      --disable_delete_obsolete_files_;
    }
    if (disable_delete_obsolete_files_ == 0)  {
53
      file_deletion_enabled = true;
Igor Canadi's avatar
Igor Canadi committed
54
      FindObsoleteFiles(&job_context, true);
55
      bg_cv_.SignalAll();
56
57
    }
  }
58
59
  if (file_deletion_enabled) {
    ROCKS_LOG_INFO(immutable_db_options_.info_log, "File Deletions Enabled");
60
61
62
    if (job_context.HaveSomethingToDelete()) {
      PurgeObsoleteFiles(job_context);
    }
63
64
65
66
  } else {
    ROCKS_LOG_WARN(immutable_db_options_.info_log,
                   "File Deletions Enable, but not really enabled. Counter: %d",
                   disable_delete_obsolete_files_);
Igor Canadi's avatar
Igor Canadi committed
67
  }
Igor Canadi's avatar
Igor Canadi committed
68
  job_context.Clean();
69
  LogFlush(immutable_db_options_.info_log);
70
71
72
  return Status::OK();
}

73
int DBImpl::IsFileDeletionsEnabled() const {
74
  return !disable_delete_obsolete_files_;
75
76
}

Abhishek Kona's avatar
Abhishek Kona committed
77
Status DBImpl::GetLiveFiles(std::vector<std::string>& ret,
78
79
                            uint64_t* manifest_file_size,
                            bool flush_memtable) {
80
  *manifest_file_size = 0;
81

82
83
  mutex_.Lock();

84
85
  if (flush_memtable) {
    // flush all dirty data to disk.
86
    Status status;
87
    if (immutable_db_options_.atomic_flush) {
Yanqin Jin's avatar
Yanqin Jin committed
88
89
      autovector<ColumnFamilyData*> cfds;
      SelectColumnFamiliesForAtomicFlush(&cfds);
90
      mutex_.Unlock();
Yanqin Jin's avatar
Yanqin Jin committed
91
92
      status = AtomicFlushMemTables(cfds, FlushOptions(),
                                    FlushReason::kGetLiveFiles);
93
      mutex_.Lock();
Yanqin Jin's avatar
Yanqin Jin committed
94
95
96
97
98
99
100
101
102
103
104
    } else {
      for (auto cfd : *versions_->GetColumnFamilySet()) {
        if (cfd->IsDropped()) {
          continue;
        }
        cfd->Ref();
        mutex_.Unlock();
        status = FlushMemTable(cfd, FlushOptions(), FlushReason::kGetLiveFiles);
        TEST_SYNC_POINT("DBImpl::GetLiveFiles:1");
        TEST_SYNC_POINT("DBImpl::GetLiveFiles:2");
        mutex_.Lock();
105
        cfd->UnrefAndTryDelete();
Yanqin Jin's avatar
Yanqin Jin committed
106
107
108
        if (!status.ok()) {
          break;
        }
109
110
      }
    }
111
112
    versions_->GetColumnFamilySet()->FreeDeadColumnFamilies();

113
    if (!status.ok()) {
114
      mutex_.Unlock();
115
116
      ROCKS_LOG_ERROR(immutable_db_options_.info_log, "Cannot Flush data %s\n",
                      status.ToString().c_str());
117
118
      return status;
    }
119
120
  }

121
122
123
  // Make a set of all of the live table and blob files
  std::vector<uint64_t> live_table_files;
  std::vector<uint64_t> live_blob_files;
124
  for (auto cfd : *versions_->GetColumnFamilySet()) {
125
126
127
    if (cfd->IsDropped()) {
      continue;
    }
128
    cfd->current()->AddLiveFiles(&live_table_files, &live_blob_files);
129
  }
130

131
  ret.clear();
132
133
  ret.reserve(live_table_files.size() + live_blob_files.size() +
              3);  // for CURRENT + MANIFEST + OPTIONS
134
135
136

  // create names of the live files. The names are not absolute
  // paths, instead they are relative to dbname_;
137
138
  for (const auto& table_file_number : live_table_files) {
    ret.emplace_back(MakeTableFileName("", table_file_number));
139
140
  }

141
142
143
144
145
146
147
  for (const auto& blob_file_number : live_blob_files) {
    ret.emplace_back(BlobFileName("", blob_file_number));
  }

  ret.emplace_back(CurrentFileName(""));
  ret.emplace_back(DescriptorFileName("", versions_->manifest_file_number()));
  ret.emplace_back(OptionsFileName("", versions_->options_file_number()));
148

149
  // find length of manifest file while holding the mutex lock
150
  *manifest_file_size = versions_->manifest_file_size();
151

152
  mutex_.Unlock();
153
154
155
  return Status::OK();
}

156
Status DBImpl::GetSortedWalFiles(VectorLogPtr& files) {
157
158
159
160
161
162
163
164
165
166
167
168
  {
    // If caller disabled deletions, this function should return files that are
    // guaranteed not to be deleted until deletions are re-enabled. We need to
    // wait for pending purges to finish since WalManager doesn't know which
    // files are going to be purged. Additional purges won't be scheduled as
    // long as deletions are disabled (so the below loop must terminate).
    InstrumentedMutexLock l(&mutex_);
    while (disable_delete_obsolete_files_ > 0 &&
           pending_purge_obsolete_files_ > 0) {
      bg_cv_.Wait();
    }
  }
Igor Canadi's avatar
Igor Canadi committed
169
  return wal_manager_.GetSortedWalFiles(files);
170
}
171

172
173
174
175
176
177
178
179
180
Status DBImpl::GetCurrentWalFile(std::unique_ptr<LogFile>* current_log_file) {
  uint64_t current_logfile_number;
  {
    InstrumentedMutexLock l(&mutex_);
    current_logfile_number = logfile_number_;
  }

  return wal_manager_.GetLiveWalFile(current_logfile_number, current_log_file);
}
181
}  // namespace ROCKSDB_NAMESPACE
Igor Canadi's avatar
Igor Canadi committed
182
183

#endif  // ROCKSDB_LITE