db_filesnapshot.cc 4.41 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
3
4
5
//  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>
Wanning Jiang's avatar
Wanning Jiang committed
17
#include <stdint.h>
18
#include <algorithm>
19
#include <string>
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"
Wanning Jiang's avatar
Wanning Jiang committed
24
#include "port/port.h"
25
26
#include "rocksdb/db.h"
#include "rocksdb/env.h"
Wanning Jiang's avatar
Wanning Jiang committed
27
#include "util/file_util.h"
28
#include "util/mutexlock.h"
29
#include "util/sync_point.h"
30

31
namespace rocksdb {
32
33

Status DBImpl::DisableFileDeletions() {
34
  InstrumentedMutexLock l(&mutex_);
35
36
  ++disable_delete_obsolete_files_;
  if (disable_delete_obsolete_files_ == 1) {
37
    Log(InfoLogLevel::INFO_LEVEL, immutable_db_options_.info_log,
38
        "File Deletions Disabled");
39
  } else {
40
    Log(InfoLogLevel::WARN_LEVEL, immutable_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) {
48
49
50
  // Job id == 0 means that this is not our background process, but rather
  // user thread
  JobContext job_context(0);
51
  bool should_purge_files = false;
Igor Canadi's avatar
Igor Canadi committed
52
  {
53
    InstrumentedMutexLock l(&mutex_);
54
55
56
57
58
59
60
    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)  {
61
      Log(InfoLogLevel::INFO_LEVEL, immutable_db_options_.info_log,
62
          "File Deletions Enabled");
63
      should_purge_files = true;
Igor Canadi's avatar
Igor Canadi committed
64
      FindObsoleteFiles(&job_context, true);
65
    } else {
66
      Log(InfoLogLevel::WARN_LEVEL, immutable_db_options_.info_log,
67
68
          "File Deletions Enable, but not really enabled. Counter: %d",
          disable_delete_obsolete_files_);
69
70
71
    }
  }
  if (should_purge_files)  {
Igor Canadi's avatar
Igor Canadi committed
72
    PurgeObsoleteFiles(job_context);
Igor Canadi's avatar
Igor Canadi committed
73
  }
Igor Canadi's avatar
Igor Canadi committed
74
  job_context.Clean();
75
  LogFlush(immutable_db_options_.info_log);
76
77
78
  return Status::OK();
}

79
80
81
82
int DBImpl::IsFileDeletionsEnabled() const {
  return disable_delete_obsolete_files_;
}

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

88
89
  mutex_.Lock();

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

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

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

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

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

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

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

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

147
Status DBImpl::GetSortedWalFiles(VectorLogPtr& files) {
Igor Canadi's avatar
Igor Canadi committed
148
  return wal_manager_.GetSortedWalFiles(files);
149
}
150

151
}
Igor Canadi's avatar
Igor Canadi committed
152
153

#endif  // ROCKSDB_LITE