db_filesnapshot.cc 5.97 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
94
95
      if (status.IsColumnFamilyDropped()) {
        status = Status::OK();
      }
96
      mutex_.Lock();
Yanqin Jin's avatar
Yanqin Jin committed
97
98
99
100
101
102
103
104
105
106
107
    } 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();
108
        cfd->UnrefAndTryDelete();
109
        if (!status.ok() && !status.IsColumnFamilyDropped()) {
Yanqin Jin's avatar
Yanqin Jin committed
110
          break;
111
112
        } else if (status.IsColumnFamilyDropped()) {
          status = Status::OK();
Yanqin Jin's avatar
Yanqin Jin committed
113
        }
114
115
      }
    }
116
117
    versions_->GetColumnFamilySet()->FreeDeadColumnFamilies();

118
    if (!status.ok()) {
119
      mutex_.Unlock();
120
121
      ROCKS_LOG_ERROR(immutable_db_options_.info_log, "Cannot Flush data %s\n",
                      status.ToString().c_str());
122
123
      return status;
    }
124
125
  }

126
127
128
  // 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;
129
  for (auto cfd : *versions_->GetColumnFamilySet()) {
130
131
132
    if (cfd->IsDropped()) {
      continue;
    }
133
    cfd->current()->AddLiveFiles(&live_table_files, &live_blob_files);
134
  }
135

136
  ret.clear();
137
138
  ret.reserve(live_table_files.size() + live_blob_files.size() +
              3);  // for CURRENT + MANIFEST + OPTIONS
139
140
141

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

146
147
148
149
150
151
152
  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()));
153

154
  // find length of manifest file while holding the mutex lock
155
  *manifest_file_size = versions_->manifest_file_size();
156

157
  mutex_.Unlock();
158
159
160
  return Status::OK();
}

161
Status DBImpl::GetSortedWalFiles(VectorLogPtr& files) {
162
163
164
165
166
167
168
169
170
171
172
173
  {
    // 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
174
  return wal_manager_.GetSortedWalFiles(files);
175
}
176

177
178
179
180
181
182
183
184
185
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);
}
186
}  // namespace ROCKSDB_NAMESPACE
Igor Canadi's avatar
Igor Canadi committed
187
188

#endif  // ROCKSDB_LITE