OmniSciDB  2e3a973ef4
ForeignTableRefresh.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, 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 "ForeignTableRefresh.h"
18 
19 #include "LockMgr/LockMgr.h"
20 
21 namespace foreign_storage {
23  const std::string& table_name,
24  const bool evict_cached_entries) {
25  auto& data_mgr = catalog.getDataMgr();
26  auto table_lock =
27  std::make_unique<lockmgr::TableSchemaLockContainer<lockmgr::WriteLock>>(
29  catalog, table_name, false));
30 
31  const TableDescriptor* td = (*table_lock)();
32  if (td->storageType != StorageType::FOREIGN_TABLE) {
33  throw std::runtime_error{
34  table_name +
35  " is not a foreign table. Refreshes are applicable to only foreign tables."};
36  }
37 
38  catalog.removeFragmenterForTable(td->tableId);
39  ChunkKey table_key{catalog.getCurrentDB().dbId, td->tableId};
40  data_mgr.deleteChunksWithPrefix(table_key, MemoryLevel::CPU_LEVEL);
41  data_mgr.deleteChunksWithPrefix(table_key, MemoryLevel::GPU_LEVEL);
42 
43  try {
44  data_mgr.getPersistentStorageMgr()->getForeignStorageMgr()->refreshTable(
45  table_key, evict_cached_entries);
46  catalog.updateForeignTableRefreshTimes(td->tableId);
47  } catch (PostEvictionRefreshException& e) {
48  catalog.updateForeignTableRefreshTimes(td->tableId);
49  throw e.getOriginalException();
50  }
51 }
52 
53 void ForeignTableRefreshScheduler::start(std::atomic<bool>& is_program_running) {
54  if (!is_scheduler_running_) {
55  scheduler_thread_ = std::thread([&is_program_running]() {
56  while (is_program_running) {
57  auto& sys_catalog = Catalog_Namespace::SysCatalog::instance();
58  for (const auto& catalog : sys_catalog.getCatalogsForAllDbs()) {
59  auto tables = catalog->getAllForeignTablesForRefresh();
60  for (auto table : tables) {
61  try {
62  refresh_foreign_table(*catalog, table->tableName, false);
63  } catch (std::runtime_error& e) {
64  LOG(ERROR) << "Scheduled refresh for table \"" << table->tableName
65  << "\" resulted in an error. " << e.what();
66  }
67  has_refreshed_table_ = true;
68  }
69  }
70  std::this_thread::sleep_for(thread_wait_duration_);
71  }
72  });
73  is_scheduler_running_ = true;
74  }
75 }
76 
79  scheduler_thread_.join();
80  is_scheduler_running_ = false;
81  }
82 }
83 
84 void ForeignTableRefreshScheduler::setWaitDuration(int64_t duration_in_seconds) {
85  thread_wait_duration_ = std::chrono::seconds{duration_in_seconds};
86 }
87 
89  return is_scheduler_running_;
90 }
91 
93  return has_refreshed_table_;
94 }
95 
97  has_refreshed_table_ = false;
98 }
99 
104 } // namespace foreign_storage
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:97
Data_Namespace::DataMgr & getDataMgr() const
Definition: Catalog.h:209
#define LOG(tag)
Definition: Logger.h:188
void refresh_foreign_table(Catalog_Namespace::Catalog &catalog, const std::string &table_name, const bool evict_cached_entries)
std::runtime_error getOriginalException()
static void setWaitDuration(int64_t duration_in_seconds)
static SysCatalog & instance()
Definition: SysCatalog.h:286
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:208
static void start(std::atomic< bool > &is_program_running)
void removeFragmenterForTable(const int table_id)
Definition: Catalog.cpp:3172
std::vector< int > ChunkKey
Definition: types.h:37
specifies the content in-memory of a row in the table metadata table
static constexpr char const * FOREIGN_TABLE
void updateForeignTableRefreshTimes(const int32_t table_id)
Definition: Catalog.cpp:4329