db_filesnapshot.cc 4.2 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
Siying Dong's avatar
Siying Dong committed
2
3
4
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).
5
//
6

Igor Canadi's avatar
Igor Canadi committed
7
8
#ifndef ROCKSDB_LITE

liuhuahang's avatar
liuhuahang committed
9
#ifndef __STDC_FORMAT_MACROS
Igor Canadi's avatar
Igor Canadi committed
10
#define __STDC_FORMAT_MACROS
liuhuahang's avatar
liuhuahang committed
11
12
#endif

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

28
namespace rocksdb {
29
30

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

43
Status DBImpl::EnableFileDeletions(bool force) {
44
45
46
  // Job id == 0 means that this is not our background process, but rather
  // user thread
  JobContext job_context(0);
47
  bool should_purge_files = false;
Igor Canadi's avatar
Igor Canadi committed
48
  {
49
    InstrumentedMutexLock 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
      ROCKS_LOG_INFO(immutable_db_options_.info_log, "File Deletions Enabled");
58
      should_purge_files = true;
Igor Canadi's avatar
Igor Canadi committed
59
      FindObsoleteFiles(&job_context, true);
60
    } else {
61
62
      ROCKS_LOG_WARN(
          immutable_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
  }
Igor Canadi's avatar
Igor Canadi committed
70
  job_context.Clean();
71
  LogFlush(immutable_db_options_.info_log);
72
73
74
  return Status::OK();
}

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

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

84
85
  mutex_.Lock();

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

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

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

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

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

132
  ret.push_back(CurrentFileName(""));
133
  ret.push_back(DescriptorFileName("", versions_->manifest_file_number()));
Wanning Jiang's avatar
Wanning Jiang committed
134
  ret.push_back(OptionsFileName("", versions_->options_file_number()));
135

136
  // find length of manifest file while holding the mutex lock
137
  *manifest_file_size = versions_->manifest_file_size();
138

139
  mutex_.Unlock();
140
141
142
  return Status::OK();
}

143
Status DBImpl::GetSortedWalFiles(VectorLogPtr& files) {
Igor Canadi's avatar
Igor Canadi committed
144
  return wal_manager_.GetSortedWalFiles(files);
145
}
146

147
}
Igor Canadi's avatar
Igor Canadi committed
148
149

#endif  // ROCKSDB_LITE