/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 | 12.0k | table_to_tablespace_map_(std::move(table_to_tablespace_map)) { |
31 | 12.0k | } |
32 | | |
33 | | std::shared_ptr<YsqlTablespaceManager> YsqlTablespaceManager::CreateCloneWithTablespaceMap( |
34 | 19 | std::shared_ptr<TablespaceIdToReplicationInfoMap> tablespace_map) { |
35 | | |
36 | 19 | return std::make_shared<YsqlTablespaceManager>(tablespace_map, table_to_tablespace_map_); |
37 | 19 | } |
38 | | |
39 | | Result<boost::optional<ReplicationInfoPB>> YsqlTablespaceManager::GetTablespaceReplicationInfo( |
40 | 57.2k | const TablespaceId& tablespace_id) { |
41 | | |
42 | 57.2k | if (!GetAtomicFlag(&FLAGS_enable_ysql_tablespaces_for_placement)) { |
43 | | // Tablespaces feature has been disabled. |
44 | 10 | return boost::none; |
45 | 10 | } |
46 | | |
47 | 57.2k | if (tablespace_id.empty()) { |
48 | | // No tablespace id passed in. Return. |
49 | 55.9k | return boost::none; |
50 | 55.9k | } |
51 | | |
52 | 1.25k | if (tablespace_id_to_replication_info_map_) { |
53 | 1.25k | auto iter = tablespace_id_to_replication_info_map_->find(tablespace_id); |
54 | 1.25k | if (iter != tablespace_id_to_replication_info_map_->end()) { |
55 | 1.17k | return iter->second; |
56 | 1.17k | } |
57 | 1.25k | } |
58 | | |
59 | 79 | return STATUS(InternalError, "pg_tablespace info for tablespace " + |
60 | 1.25k | tablespace_id + " not found"); |
61 | 1.25k | } |
62 | | |
63 | | Result<boost::optional<TablespaceId>> YsqlTablespaceManager::GetTablespaceForTable( |
64 | 878k | const scoped_refptr<const TableInfo>& table) const { |
65 | | |
66 | 878k | if (!GetAtomicFlag(&FLAGS_enable_ysql_tablespaces_for_placement) || |
67 | 878k | !table->UsesTablespacesForPlacement()877k ) { |
68 | 523k | return boost::none; |
69 | 523k | } |
70 | | |
71 | 355k | if (!tablespace_id_to_replication_info_map_) { |
72 | 0 | return STATUS(InternalError, "Tablespace information not found for table " + table->id()); |
73 | 0 | } |
74 | | |
75 | 355k | if (!ContainsCustomTablespaces()) { |
76 | 342k | return boost::none; |
77 | 342k | } |
78 | | |
79 | 13.0k | 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 | 13.0k | const auto& iter = table_to_tablespace_map_->find(table->id()); |
85 | 13.0k | if (iter == table_to_tablespace_map_->end()) { |
86 | 0 | return STATUS(InternalError, "Tablespace information not found for table " + table->id()); |
87 | 0 | } |
88 | | |
89 | 13.0k | return iter->second; |
90 | 13.0k | } |
91 | | |
92 | | Result<boost::optional<ReplicationInfoPB>> YsqlTablespaceManager::GetTableReplicationInfo( |
93 | 878k | const scoped_refptr<const TableInfo>& table) const { |
94 | | |
95 | | // Lookup tablespace for the given table. |
96 | 878k | auto tablespace_id = VERIFY_RESULT(GetTablespaceForTable(table)); |
97 | | |
98 | 878k | if (!tablespace_id) { |
99 | 872k | return boost::none; |
100 | 872k | } |
101 | | |
102 | | // Lookup the placement info associated with the above tablespace. |
103 | 6.43k | const auto iter = tablespace_id_to_replication_info_map_->find(tablespace_id.value()); |
104 | 6.43k | 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 | 6.43k | return iter->second; |
108 | 6.43k | } |
109 | | |
110 | | bool YsqlTablespaceManager::NeedsRefreshToFindTablePlacement( |
111 | 895k | const scoped_refptr<TableInfo>& table) { |
112 | | |
113 | 895k | if (!GetAtomicFlag(&FLAGS_enable_ysql_tablespaces_for_placement) || |
114 | 895k | !table->UsesTablespacesForPlacement()894k ) { |
115 | 539k | return false; |
116 | 539k | } |
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 | 356k | if (!tablespace_id_to_replication_info_map_) { |
121 | 60 | return true; |
122 | 60 | } |
123 | | |
124 | 356k | if (!ContainsCustomTablespaces()) { |
125 | 341k | return false; |
126 | 341k | } |
127 | | |
128 | | // The table_to_tablespace_map_ is not initialized. |
129 | 14.7k | if (!table_to_tablespace_map_) { |
130 | 695 | return true; |
131 | 695 | } |
132 | | |
133 | | // First find the tablespace id for the table. |
134 | 14.0k | const auto& iter = table_to_tablespace_map_->find(table->id()); |
135 | 14.0k | 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 | 2.06k | return true; |
140 | 2.06k | } |
141 | | |
142 | 12.0k | 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 | 5.52k | return false; |
147 | 5.52k | } |
148 | | |
149 | | // The table was associated with a custom tablespace. Now find the placement policy |
150 | | // corresponding to this tablespace. |
151 | 6.51k | const auto tablespace_iter = tablespace_id_to_replication_info_map_->find(iter->second.value()); |
152 | 6.51k | 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 | 72 | return true; |
157 | 72 | } |
158 | | // Entry found for the tablespace. Placement information for this table is present in memory. |
159 | 6.44k | return false; |
160 | 6.51k | } |
161 | | |
162 | 711k | bool YsqlTablespaceManager::ContainsCustomTablespaces() const { |
163 | 711k | return tablespace_id_to_replication_info_map_->size() > kYsqlNumDefaultTablespaces; |
164 | 711k | } |
165 | | |
166 | | } // namespace master |
167 | | } // namespace yb |