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

liuhuahang's avatar
liuhuahang committed
9
#ifndef __STDC_FORMAT_MACROS
Igor Canadi's avatar
Igor Canadi committed
10
#define __STDC_FORMAT_MACROS
liuhuahang's avatar
liuhuahang committed
11
12
#endif

Igor Canadi's avatar
Igor Canadi committed
13
#include <inttypes.h>
Wanning Jiang's avatar
Wanning Jiang committed
14
#include <stdint.h>
15
#include <algorithm>
16
#include <string>
17
#include "db/db_impl.h"
Igor Canadi's avatar
Igor Canadi committed
18
#include "db/job_context.h"
19
#include "db/version_set.h"
Wanning Jiang's avatar
Wanning Jiang committed
20
#include "port/port.h"
21
22
#include "rocksdb/db.h"
#include "rocksdb/env.h"
Wanning Jiang's avatar
Wanning Jiang committed
23
#include "util/file_util.h"
24
#include "util/filename.h"
25
#include "util/mutexlock.h"
26
#include "util/sync_point.h"
27

28
namespace rocksdb {
29
30

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

43
Status DBImpl::EnableFileDeletions(bool force) {
44
45
46
  // Job id == 0 means that this is not our background process, but rather
  // user thread
  JobContext job_context(0);
47
  bool file_deletion_enabled = false;
Igor Canadi's avatar
Igor Canadi committed
48
  {
49
    InstrumentedMutexLock l(&mutex_);
50
51
52
53
54
55
56
    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)  {
57
      file_deletion_enabled = true;
Igor Canadi's avatar
Igor Canadi committed
58
      FindObsoleteFiles(&job_context, true);
59
      bg_cv_.SignalAll();
60
61
    }
  }
62
63
  if (file_deletion_enabled) {
    ROCKS_LOG_INFO(immutable_db_options_.info_log, "File Deletions Enabled");
Igor Canadi's avatar
Igor Canadi committed
64
    PurgeObsoleteFiles(job_context);
65
66
67
68
  } 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
69
  }
Igor Canadi's avatar
Igor Canadi committed
70
  job_context.Clean();
71
  LogFlush(immutable_db_options_.info_log);
72
73
74
  return Status::OK();
}

75
int DBImpl::IsFileDeletionsEnabled() const {
76
  return !disable_delete_obsolete_files_;
77
78
}

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

84
85
  mutex_.Lock();

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

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

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

132
  ret.clear();
Wanning Jiang's avatar
Wanning Jiang committed
133
  ret.reserve(live.size() + 3);  // *.sst + 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
  for (const auto& live_file : live) {
138
    ret.push_back(MakeTableFileName("", live_file.GetNumber()));
139
140
  }

141
  ret.push_back(CurrentFileName(""));
142
  ret.push_back(DescriptorFileName("", versions_->manifest_file_number()));
Wanning Jiang's avatar
Wanning Jiang committed
143
  ret.push_back(OptionsFileName("", versions_->options_file_number()));
144

145
  // find length of manifest file while holding the mutex lock
146
  *manifest_file_size = versions_->manifest_file_size();
147

148
  mutex_.Unlock();
149
150
151
  return Status::OK();
}

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

168
}
Igor Canadi's avatar
Igor Canadi committed
169
170

#endif  // ROCKSDB_LITE