db_filesnapshot.cc 4.37 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
    ROCKS_LOG_INFO(immutable_db_options_.info_log, "File Deletions Disabled");
38
  } else {
39
40
41
    ROCKS_LOG_WARN(immutable_db_options_.info_log,
                   "File Deletions Disabled, but already disabled. Counter: %d",
                   disable_delete_obsolete_files_);
42
  }
43
44
45
  return Status::OK();
}

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

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

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

87
88
  mutex_.Lock();

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

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

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

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

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

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

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

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

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

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

#endif  // ROCKSDB_LITE