db_filesnapshot.cc 4.8 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 should_purge_files = 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
      ROCKS_LOG_INFO(immutable_db_options_.info_log, "File Deletions Enabled");
58
      should_purge_files = true;
Igor Canadi's avatar
Igor Canadi committed
59
      FindObsoleteFiles(&job_context, true);
60
      bg_cv_.SignalAll();
61
    } else {
62
63
      ROCKS_LOG_WARN(
          immutable_db_options_.info_log,
64
65
          "File Deletions Enable, but not really enabled. Counter: %d",
          disable_delete_obsolete_files_);
66
67
68
    }
  }
  if (should_purge_files)  {
Igor Canadi's avatar
Igor Canadi committed
69
    PurgeObsoleteFiles(job_context);
Igor Canadi's avatar
Igor Canadi committed
70
  }
Igor Canadi's avatar
Igor Canadi committed
71
  job_context.Clean();
72
  LogFlush(immutable_db_options_.info_log);
73
74
75
  return Status::OK();
}

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

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

85
86
  mutex_.Lock();

87
88
  if (flush_memtable) {
    // flush all dirty data to disk.
89
90
    Status status;
    for (auto cfd : *versions_->GetColumnFamilySet()) {
91
92
93
      if (cfd->IsDropped()) {
        continue;
      }
94
95
      cfd->Ref();
      mutex_.Unlock();
96
      status = FlushMemTable(cfd, FlushOptions(), FlushReason::kGetLiveFiles);
97
98
      TEST_SYNC_POINT("DBImpl::GetLiveFiles:1");
      TEST_SYNC_POINT("DBImpl::GetLiveFiles:2");
99
      mutex_.Lock();
100
      cfd->Unref();
101
102
103
104
      if (!status.ok()) {
        break;
      }
    }
105
106
    versions_->GetColumnFamilySet()->FreeDeadColumnFamilies();

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

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

124
  ret.clear();
Wanning Jiang's avatar
Wanning Jiang committed
125
  ret.reserve(live.size() + 3);  // *.sst + CURRENT + MANIFEST + OPTIONS
126
127
128

  // create names of the live files. The names are not absolute
  // paths, instead they are relative to dbname_;
129
  for (auto live_file : live) {
130
    ret.push_back(MakeTableFileName("", live_file.GetNumber()));
131
132
  }

133
  ret.push_back(CurrentFileName(""));
134
  ret.push_back(DescriptorFileName("", versions_->manifest_file_number()));
Wanning Jiang's avatar
Wanning Jiang committed
135
  ret.push_back(OptionsFileName("", versions_->options_file_number()));
136

137
  // find length of manifest file while holding the mutex lock
138
  *manifest_file_size = versions_->manifest_file_size();
139

140
  mutex_.Unlock();
141
142
143
  return Status::OK();
}

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

160
}
Igor Canadi's avatar
Igor Canadi committed
161
162

#endif  // ROCKSDB_LITE