db_filesnapshot.cc 5.1 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

9
#include <cinttypes>
Wanning Jiang's avatar
Wanning Jiang committed
10
#include <stdint.h>
11
#include <algorithm>
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 {
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");
Igor Canadi's avatar
Igor Canadi committed
60
    PurgeObsoleteFiles(job_context);
61
62
63
64
  } 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
65
  }
Igor Canadi's avatar
Igor Canadi committed
66
  job_context.Clean();
67
  LogFlush(immutable_db_options_.info_log);
68
69
70
  return Status::OK();
}

71
int DBImpl::IsFileDeletionsEnabled() const {
72
  return !disable_delete_obsolete_files_;
73
74
}

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

80
81
  mutex_.Lock();

82
83
  if (flush_memtable) {
    // flush all dirty data to disk.
84
    Status status;
85
    if (immutable_db_options_.atomic_flush) {
Yanqin Jin's avatar
Yanqin Jin committed
86
87
      autovector<ColumnFamilyData*> cfds;
      SelectColumnFamiliesForAtomicFlush(&cfds);
88
      mutex_.Unlock();
Yanqin Jin's avatar
Yanqin Jin committed
89
90
      status = AtomicFlushMemTables(cfds, FlushOptions(),
                                    FlushReason::kGetLiveFiles);
91
      mutex_.Lock();
Yanqin Jin's avatar
Yanqin Jin committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    } 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();
        cfd->Unref();
        if (!status.ok()) {
          break;
        }
107
108
      }
    }
109
110
    versions_->GetColumnFamilySet()->FreeDeadColumnFamilies();

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

  // Make a set of all of the live *.sst files
120
  std::vector<FileDescriptor> live;
121
  for (auto cfd : *versions_->GetColumnFamilySet()) {
122
123
124
    if (cfd->IsDropped()) {
      continue;
    }
125
126
    cfd->current()->AddLiveFiles(&live);
  }
127

128
  ret.clear();
Wanning Jiang's avatar
Wanning Jiang committed
129
  ret.reserve(live.size() + 3);  // *.sst + CURRENT + MANIFEST + OPTIONS
130
131
132

  // create names of the live files. The names are not absolute
  // paths, instead they are relative to dbname_;
133
  for (const auto& live_file : live) {
134
    ret.push_back(MakeTableFileName("", live_file.GetNumber()));
135
136
  }

137
  ret.push_back(CurrentFileName(""));
138
  ret.push_back(DescriptorFileName("", versions_->manifest_file_number()));
Wanning Jiang's avatar
Wanning Jiang committed
139
  ret.push_back(OptionsFileName("", versions_->options_file_number()));
140

141
  // find length of manifest file while holding the mutex lock
142
  *manifest_file_size = versions_->manifest_file_size();
143

144
  mutex_.Unlock();
145
146
147
  return Status::OK();
}

148
Status DBImpl::GetSortedWalFiles(VectorLogPtr& files) {
149
150
151
152
153
154
155
156
157
158
159
160
  {
    // 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
161
  return wal_manager_.GetSortedWalFiles(files);
162
}
163

164
}
Igor Canadi's avatar
Igor Canadi committed
165
166

#endif  // ROCKSDB_LITE