YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/master/ysql_tablespace_manager.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#include "yb/master/ysql_tablespace_manager.h"
15
16
#include "yb/master/catalog_entity_info.h"
17
18
#include "yb/util/atomic.h"
19
#include "yb/util/result.h"
20
21
DECLARE_bool(enable_ysql_tablespaces_for_placement);
22
23
namespace yb {
24
namespace master {
25
26
YsqlTablespaceManager::YsqlTablespaceManager(
27
    std::shared_ptr<TablespaceIdToReplicationInfoMap> tablespace_map,
28
    std::shared_ptr<TableToTablespaceIdMap> table_to_tablespace_map)
29
    : tablespace_id_to_replication_info_map_(std::move(tablespace_map)),
30
5.94k
      table_to_tablespace_map_(std::move(table_to_tablespace_map)) {
31
5.94k
}
32
33
std::shared_ptr<YsqlTablespaceManager> YsqlTablespaceManager::CreateCloneWithTablespaceMap(
34
0
    std::shared_ptr<TablespaceIdToReplicationInfoMap> tablespace_map) {
35
36
0
  return std::make_shared<YsqlTablespaceManager>(tablespace_map, table_to_tablespace_map_);
37
0
}
38
39
Result<boost::optional<ReplicationInfoPB>> YsqlTablespaceManager::GetTablespaceReplicationInfo(
40
32.3k
    const TablespaceId& tablespace_id) {
41
42
32.3k
  if (!GetAtomicFlag(&FLAGS_enable_ysql_tablespaces_for_placement)) {
43
    // Tablespaces feature has been disabled.
44
0
    return boost::none;
45
0
  }
46
47
32.3k
  if (tablespace_id.empty()) {
48
    // No tablespace id passed in. Return.
49
32.3k
    return boost::none;
50
32.3k
  }
51
52
0
  if (tablespace_id_to_replication_info_map_) {
53
0
    auto iter = tablespace_id_to_replication_info_map_->find(tablespace_id);
54
0
    if (iter != tablespace_id_to_replication_info_map_->end()) {
55
0
      return iter->second;
56
0
    }
57
0
  }
58
59
0
  return STATUS(InternalError, "pg_tablespace info for tablespace " +
60
0
      tablespace_id + " not found");
61
0
}
62
63
Result<boost::optional<TablespaceId>> YsqlTablespaceManager::GetTablespaceForTable(
64
120k
  const scoped_refptr<const TableInfo>& table) const {
65
66
120k
  if (!GetAtomicFlag(&FLAGS_enable_ysql_tablespaces_for_placement) ||
67
120k
      !table->UsesTablespacesForPlacement()) {
68
71.5k
    return boost::none;
69
71.5k
  }
70
71
48.9k
  if (!tablespace_id_to_replication_info_map_) {
72
0
    return STATUS(InternalError, "Tablespace information not found for table " + table->id());
73
0
  }
74
75
48.9k
  if (!ContainsCustomTablespaces()) {
76
48.9k
    return boost::none;
77
48.9k
  }
78
79
0
  if (!table_to_tablespace_map_) {
80
0
    return STATUS(InternalError, "Tablespace information not found for table " + table->id());
81
0
  }
82
83
  // Lookup the tablespace for this table.
84
0
  const auto& iter = table_to_tablespace_map_->find(table->id());
85
0
  if (iter == table_to_tablespace_map_->end()) {
86
0
    return STATUS(InternalError, "Tablespace information not found for table " + table->id());
87
0
  }
88
89
0
  return iter->second;
90
0
}
91
92
Result<boost::optional<ReplicationInfoPB>> YsqlTablespaceManager::GetTableReplicationInfo(
93
120k
    const scoped_refptr<const TableInfo>& table) const {
94
95
  // Lookup tablespace for the given table.
96
120k
  auto tablespace_id = VERIFY_RESULT(GetTablespaceForTable(table));
97
98
120k
  if (!tablespace_id) {
99
120k
    return boost::none;
100
120k
  }
101
102
  // Lookup the placement info associated with the above tablespace.
103
0
  const auto iter = tablespace_id_to_replication_info_map_->find(tablespace_id.value());
104
0
  if (iter == tablespace_id_to_replication_info_map_->end()) {
105
0
    return STATUS(InternalError, "Placement policy not found for " + tablespace_id.value());
106
0
  }
107
0
  return iter->second;
108
0
}
109
110
bool YsqlTablespaceManager::NeedsRefreshToFindTablePlacement(
111
121k
    const scoped_refptr<TableInfo>& table) {
112
113
121k
  if (!GetAtomicFlag(&FLAGS_enable_ysql_tablespaces_for_placement) ||
114
121k
      !table->UsesTablespacesForPlacement()) {
115
72.6k
    return false;
116
72.6k
  }
117
118
  // The tablespace maps itself have not been initialized yet, which means the background
119
  // task has not run even once since startup.
120
48.9k
  if (!tablespace_id_to_replication_info_map_) {
121
1
    return true;
122
1
  }
123
124
48.9k
  if (!ContainsCustomTablespaces()) {
125
48.9k
    return false;
126
48.9k
  }
127
128
  // The table_to_tablespace_map_ is not initialized.
129
0
  if (!table_to_tablespace_map_) {
130
0
    return true;
131
0
  }
132
133
  // First find the tablespace id for the table.
134
0
  const auto& iter = table_to_tablespace_map_->find(table->id());
135
0
  if (iter == table_to_tablespace_map_->end()) {
136
    // No entry found for this table. The background task has not picked up the info
137
    // for this table yet, maybe this is a recently created table. Need to wait for
138
    // the next run of the background task to get the placement info for this table.
139
0
    return true;
140
0
  }
141
142
0
  if (!iter->second) {
143
    // This is a boost::none value, which indicates that this table does not have a custom
144
    // tablespace specified for it. It defaults to the cluster placement policy. Nothing
145
    // more to do for this table.
146
0
    return false;
147
0
  }
148
149
  // The table was associated with a custom tablespace. Now find the placement policy
150
  // corresponding to this tablespace.
151
0
  const auto tablespace_iter = tablespace_id_to_replication_info_map_->find(iter->second.value());
152
0
  if (tablespace_iter == tablespace_id_to_replication_info_map_->end()) {
153
    // No entry found for this tablespace. This was not picked up by the background task,
154
    // probably a recently created tablespace. Need to wait for the next run of the
155
    // background task to get the placement info for this table.
156
0
    return true;
157
0
  }
158
  // Entry found for the tablespace. Placement information for this table is present in memory.
159
0
  return false;
160
0
}
161
162
97.8k
bool YsqlTablespaceManager::ContainsCustomTablespaces() const {
163
97.8k
  return tablespace_id_to_replication_info_map_->size() > kYsqlNumDefaultTablespaces;
164
97.8k
}
165
166
}  // namespace master
167
}  // namespace yb