YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/sst_file_manager_impl.cc
Line
Count
Source (jump to first uncovered line)
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
21
#include "yb/rocksdb/util/sst_file_manager_impl.h"
22
23
#include <vector>
24
25
#include "yb/rocksdb/port/port.h"
26
#include "yb/rocksdb/env.h"
27
#include "yb/rocksdb/util/mutexlock.h"
28
#include "yb/rocksdb/util/sync_point.h"
29
30
#include "yb/util/result.h"
31
32
namespace rocksdb {
33
34
SstFileManagerImpl::SstFileManagerImpl(Env* env, std::shared_ptr<Logger> logger,
35
                                       const std::string& trash_dir,
36
                                       int64_t rate_bytes_per_sec)
37
    : env_(env),
38
      logger_(logger),
39
      total_files_size_(0),
40
      max_allowed_space_(0),
41
      delete_scheduler_(env, trash_dir, rate_bytes_per_sec, logger.get(),
42
11
                        this) {}
43
44
11
SstFileManagerImpl::~SstFileManagerImpl() {}
45
46
216
Status SstFileManagerImpl::OnAddFile(const std::string& file_path) {
47
216
  uint64_t file_size;
48
216
  Status s = env_->GetFileSize(file_path, &file_size);
49
216
  if (s.ok()) {
50
216
    MutexLock l(&mu_);
51
216
    OnAddFileImpl(file_path, file_size);
52
216
  }
53
216
  TEST_SYNC_POINT("SstFileManagerImpl::OnAddFile");
54
216
  return s;
55
216
}
56
57
192
Status SstFileManagerImpl::OnDeleteFile(const std::string& file_path) {
58
192
  {
59
192
    MutexLock l(&mu_);
60
192
    OnDeleteFileImpl(file_path);
61
192
  }
62
192
  TEST_SYNC_POINT("SstFileManagerImpl::OnDeleteFile");
63
192
  return Status::OK();
64
192
}
65
66
Status SstFileManagerImpl::OnMoveFile(const std::string& old_path,
67
32
                                      const std::string& new_path) {
68
32
  {
69
32
    MutexLock l(&mu_);
70
32
    OnAddFileImpl(new_path, tracked_files_[old_path]);
71
32
    OnDeleteFileImpl(old_path);
72
32
  }
73
32
  TEST_SYNC_POINT("SstFileManagerImpl::OnMoveFile");
74
32
  return Status::OK();
75
32
}
76
77
6
void SstFileManagerImpl::SetMaxAllowedSpaceUsage(uint64_t max_allowed_space) {
78
6
  MutexLock l(&mu_);
79
6
  max_allowed_space_ = max_allowed_space;
80
6
}
81
82
104
bool SstFileManagerImpl::IsMaxAllowedSpaceReached() {
83
104
  MutexLock l(&mu_);
84
104
  if (max_allowed_space_ <= 0) {
85
48
    return false;
86
48
  }
87
56
  return total_files_size_ >= max_allowed_space_;
88
56
}
89
90
4
uint64_t SstFileManagerImpl::GetTotalSize() {
91
4
  MutexLock l(&mu_);
92
4
  return total_files_size_;
93
4
}
94
95
std::unordered_map<std::string, uint64_t>
96
28
SstFileManagerImpl::GetTrackedFiles() {
97
28
  MutexLock l(&mu_);
98
28
  return tracked_files_;
99
28
}
100
101
0
int64_t SstFileManagerImpl::GetDeleteRateBytesPerSecond() {
102
0
  return delete_scheduler_.GetRateBytesPerSecond();
103
0
}
104
105
192
Status SstFileManagerImpl::ScheduleFileDeletion(const std::string& file_path) {
106
192
  return delete_scheduler_.DeleteFile(file_path);
107
192
}
108
109
4
void SstFileManagerImpl::WaitForEmptyTrash() {
110
4
  delete_scheduler_.WaitForEmptyTrash();
111
4
}
112
113
void SstFileManagerImpl::OnAddFileImpl(const std::string& file_path,
114
248
                                       uint64_t file_size) {
115
248
  auto tracked_file = tracked_files_.find(file_path);
116
248
  if (tracked_file != tracked_files_.end()) {
117
    // File was added before, we will just update the size
118
4
    total_files_size_ -= tracked_file->second;
119
4
    total_files_size_ += file_size;
120
244
  } else {
121
244
    total_files_size_ += file_size;
122
244
  }
123
248
  tracked_files_[file_path] = file_size;
124
248
}
125
126
224
void SstFileManagerImpl::OnDeleteFileImpl(const std::string& file_path) {
127
224
  auto tracked_file = tracked_files_.find(file_path);
128
224
  if (tracked_file == tracked_files_.end()) {
129
    // File is not tracked
130
0
    return;
131
0
  }
132
133
224
  total_files_size_ -= tracked_file->second;
134
224
  tracked_files_.erase(tracked_file);
135
224
}
136
137
yb::Result<SstFileManager*> NewSstFileManager(Env* env, std::shared_ptr<Logger> info_log,
138
                                              std::string trash_dir,
139
                                              int64_t rate_bytes_per_sec,
140
11
                                              bool delete_exisitng_trash) {
141
11
  SstFileManagerImpl* res =
142
11
      new SstFileManagerImpl(env, info_log, trash_dir, rate_bytes_per_sec);
143
144
11
  if (trash_dir != "" && rate_bytes_per_sec > 0) {
145
3
    RETURN_NOT_OK(env->CreateDirIfMissing(trash_dir));
146
3
    if (delete_exisitng_trash) {
147
0
      std::vector<std::string> files_in_trash;
148
0
      RETURN_NOT_OK(env->GetChildren(trash_dir, &files_in_trash));
149
0
      for (const std::string& trash_file : files_in_trash) {
150
0
        if (trash_file == "." || trash_file == "..") {
151
0
          continue;
152
0
        }
153
154
0
        std::string path_in_trash = trash_dir + "/" + trash_file;
155
0
        RETURN_NOT_OK(res->OnAddFile(path_in_trash));
156
0
        RETURN_NOT_OK(res->ScheduleFileDeletion(path_in_trash));
157
0
      }
158
0
    }
159
3
  }
160
161
11
  return res;
162
11
}
163
164
}  // namespace rocksdb