db_filesnapshot.cc 3.87 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"
22
#include "db/version_set.h"
23
24
#include "rocksdb/db.h"
#include "rocksdb/env.h"
25
26
#include "port/port.h"
#include "util/mutexlock.h"
27
#include "util/sync_point.h"
28

29
namespace rocksdb {
30
31
32

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

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

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

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

  *manifest_file_size = 0;
83

84
85
  mutex_.Lock();

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

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

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

115
116
  ret.clear();
  ret.reserve(live.size() + 2); //*.sst + CURRENT + MANIFEST
117
118
119

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

124
  ret.push_back(CurrentFileName(""));
125
  ret.push_back(DescriptorFileName("", versions_->manifest_file_number()));
126

127
  // find length of manifest file while holding the mutex lock
128
  *manifest_file_size = versions_->manifest_file_size();
129

130
  mutex_.Unlock();
131
132
133
  return Status::OK();
}

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

#endif  // ROCKSDB_LITE