db_filesnapshot.cc 3.95 KB
Newer Older
1
2
3
4
5
//  Copyright (c) 2013, Facebook, Inc.  All rights reserved.
//  This source code is licensed under the BSD-style license found in the
//  LICENSE file in the root directory of this source tree. An additional grant
//  of patent rights can be found in the PATENTS file in the same directory.
//
6
7
// Copyright (c) 2012 Facebook.
// Use of this source code is governed by a BSD-style license that can be
Abhishek Kona's avatar
Abhishek Kona committed
8
// found in the LICENSE file.
9

Igor Canadi's avatar
Igor Canadi committed
10
11
#ifndef ROCKSDB_LITE

liuhuahang's avatar
liuhuahang committed
12
#ifndef __STDC_FORMAT_MACROS
Igor Canadi's avatar
Igor Canadi committed
13
#define __STDC_FORMAT_MACROS
liuhuahang's avatar
liuhuahang committed
14
15
#endif

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

31
namespace rocksdb {
32
33
34

Status DBImpl::DisableFileDeletions() {
  MutexLock l(&mutex_);
35
36
  ++disable_delete_obsolete_files_;
  if (disable_delete_obsolete_files_ == 1) {
37
38
    Log(InfoLogLevel::INFO_LEVEL, db_options_.info_log,
        "File Deletions Disabled");
39
  } else {
40
    Log(InfoLogLevel::WARN_LEVEL, db_options_.info_log,
41
42
        "File Deletions Disabled, but already disabled. Counter: %d",
        disable_delete_obsolete_files_);
43
  }
44
45
46
  return Status::OK();
}

47
Status DBImpl::EnableFileDeletions(bool force) {
Igor Canadi's avatar
Igor Canadi committed
48
  JobContext job_context;
49
  bool should_purge_files = false;
Igor Canadi's avatar
Igor Canadi committed
50
51
  {
    MutexLock l(&mutex_);
52
53
54
55
56
57
58
    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)  {
59
60
      Log(InfoLogLevel::INFO_LEVEL, db_options_.info_log,
          "File Deletions Enabled");
61
      should_purge_files = true;
Igor Canadi's avatar
Igor Canadi committed
62
      FindObsoleteFiles(&job_context, true);
63
    } else {
64
      Log(InfoLogLevel::WARN_LEVEL, db_options_.info_log,
65
66
          "File Deletions Enable, but not really enabled. Counter: %d",
          disable_delete_obsolete_files_);
67
68
69
    }
  }
  if (should_purge_files)  {
Igor Canadi's avatar
Igor Canadi committed
70
    PurgeObsoleteFiles(job_context);
Igor Canadi's avatar
Igor Canadi committed
71
  }
Igor Canadi's avatar
Igor Canadi committed
72
  job_context.Clean();
73
  LogFlush(db_options_.info_log);
74
75
76
  return Status::OK();
}

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

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

  *manifest_file_size = 0;
86

87
88
  mutex_.Lock();

89
90
  if (flush_memtable) {
    // flush all dirty data to disk.
91
92
93
94
95
96
    Status status;
    for (auto cfd : *versions_->GetColumnFamilySet()) {
      cfd->Ref();
      mutex_.Unlock();
      status = FlushMemTable(cfd, FlushOptions());
      mutex_.Lock();
97
      cfd->Unref();
98
99
100
101
      if (!status.ok()) {
        break;
      }
    }
102
103
    versions_->GetColumnFamilySet()->FreeDeadColumnFamilies();

104
    if (!status.ok()) {
105
      mutex_.Unlock();
106
107
      Log(InfoLogLevel::ERROR_LEVEL, db_options_.info_log,
          "Cannot Flush data %s\n", status.ToString().c_str());
108
109
      return status;
    }
110
111
112
  }

  // Make a set of all of the live *.sst files
113
  std::vector<FileDescriptor> live;
114
115
116
  for (auto cfd : *versions_->GetColumnFamilySet()) {
    cfd->current()->AddLiveFiles(&live);
  }
117

118
119
  ret.clear();
  ret.reserve(live.size() + 2); //*.sst + CURRENT + MANIFEST
120
121
122

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

127
  ret.push_back(CurrentFileName(""));
128
  ret.push_back(DescriptorFileName("", versions_->manifest_file_number()));
129

130
  // find length of manifest file while holding the mutex lock
131
  *manifest_file_size = versions_->manifest_file_size();
132

133
  mutex_.Unlock();
134
135
136
  return Status::OK();
}

137
Status DBImpl::GetSortedWalFiles(VectorLogPtr& files) {
Igor Canadi's avatar
Igor Canadi committed
138
  return wal_manager_.GetSortedWalFiles(files);
139
}
140

141
}
Igor Canadi's avatar
Igor Canadi committed
142
143

#endif  // ROCKSDB_LITE