OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SystemValidator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "SystemValidator.h"
18 
19 namespace system_validator {
20 std::string SingleNodeValidator::validate() const {
21  std::ostringstream validation_result;
22  const auto tables = catalog_.getAllTableMetadata();
23  for (const auto& table : tables) {
24  if (should_validate_epoch(table)) {
25  const auto table_epochs =
26  catalog_.getTableEpochs(catalog_.getDatabaseId(), table->tableId);
27  validation_result << validate_table_epochs(table_epochs, table->tableName, false);
28  }
29  }
30 
31  if (validation_result.str().length() > 0) {
32  return validation_result.str();
33  } else {
34  return "Instance OK";
35  }
36 }
37 
38 bool should_validate_epoch(const TableDescriptor* table_descriptor) {
39  // Epoch validation only applies to persisted local tables. Validation uses the logical
40  // table id to validate epoch consistency across shards.
41  return (table_descriptor->shard == -1 && !table_descriptor->isForeignTable() &&
42  !table_descriptor->isTemporaryTable() && !table_descriptor->isView);
43 }
44 
46  const std::vector<Catalog_Namespace::TableEpochInfo>& table_epochs,
47  const std::string& table_name,
48  const bool is_cluster_validation) {
49  std::ostringstream validation_result;
50  CHECK(!table_epochs.empty());
51  bool epochs_are_inconsistent{false};
52  auto first_epoch = table_epochs[0].table_epoch;
53  for (const auto& table_epoch : table_epochs) {
54  if (first_epoch != table_epoch.table_epoch) {
55  epochs_are_inconsistent = true;
56  break;
57  }
58  }
59 
60  if (epochs_are_inconsistent) {
61  validation_result << "\nEpoch values for table \"" << table_name
62  << "\" are inconsistent:\n"
63  << std::left;
64  // Only add "Node" header for cluster validation
65  if (is_cluster_validation) {
66  validation_result << std::setw(10) << "Node";
67  }
68  validation_result << std::setw(10) << "Table Id" << std::setw(10) << "Epoch"
69  << "\n========= ========= ";
70  // Add separator for "Node" header if this is a cluster validation
71  if (is_cluster_validation) {
72  validation_result << "========= ";
73  }
74  for (const auto& table_epoch : table_epochs) {
75  validation_result << "\n";
76  // Only add leaf index for cluster validation
77  if (is_cluster_validation) {
78  validation_result << std::setw(10)
79  << ("Leaf " + std::to_string(table_epoch.leaf_index));
80  }
81  validation_result << std::setw(10) << table_epoch.table_id << std::setw(10)
82  << table_epoch.table_epoch;
83  }
84  validation_result << "\n";
85  } else if (first_epoch < 0) {
86  validation_result << "\nNegative epoch value found for table \"" << table_name
87  << "\". Epoch: " << first_epoch << ".";
88  }
89 
90  return validation_result.str();
91 }
92 } // namespace system_validator
std::list< const TableDescriptor * > getAllTableMetadata() const
Definition: Catalog.cpp:2188
bool isForeignTable() const
std::string to_string(char const *&&v)
Catalog_Namespace::Catalog & catalog_
std::string validate_table_epochs(const std::vector< Catalog_Namespace::TableEpochInfo > &table_epochs, const std::string &table_name, const bool is_cluster_validation)
int getDatabaseId() const
Definition: Catalog.h:326
bool should_validate_epoch(const TableDescriptor *table_descriptor)
bool isTemporaryTable() const
#define CHECK(condition)
Definition: Logger.h:291
std::vector< TableEpochInfo > getTableEpochs(const int32_t db_id, const int32_t table_id) const
Definition: Catalog.cpp:3821