db_filesnapshot.cc 4.5 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
3
4
//  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.
5
6
//  This source code is also licensed under the GPLv2 license found in the
//  COPYING file in the root directory of this source tree.
7
//
8
9
// 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
10
// found in the LICENSE file.
11

Igor Canadi's avatar
Igor Canadi committed
12
13
#ifndef ROCKSDB_LITE

liuhuahang's avatar
liuhuahang committed
14
#ifndef __STDC_FORMAT_MACROS
Igor Canadi's avatar
Igor Canadi committed
15
#define __STDC_FORMAT_MACROS
liuhuahang's avatar
liuhuahang committed
16
17
#endif

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

33
namespace rocksdb {
34
35

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

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

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

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

89
90
  mutex_.Lock();

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

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

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

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

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

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

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

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

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

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

#endif  // ROCKSDB_LITE