OmniSciDB  085a039ca4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
anonymous_namespace{TableArchiver.cpp} Namespace Reference

Functions

std::string abs_path (const File_Namespace::GlobalFileMgr *global_file_mgr)
 
std::string run (const std::string &cmd, const std::string &chdir="")
 
std::string simple_file_cat (const std::string &archive_path, const std::string &file_name, const std::string &compression)
 
std::string get_table_schema (const std::string &archive_path, const std::string &table, const std::string &compression)
 
void rewrite_column_ids_in_page_headers (const boost::filesystem::path &path, const std::unordered_map< int, int > &column_ids_map, const int32_t table_epoch)
 
void adjust_altered_table_files (const int32_t table_epoch, const std::string &temp_data_dir, const std::unordered_map< int, int > &column_ids_map)
 
void delete_old_symlinks (const std::string &table_data_dir)
 
void add_data_file_symlinks (const std::string &table_data_dir)
 
void rename_table_directories (const File_Namespace::GlobalFileMgr *global_file_mgr, const std::string &temp_data_dir, const std::vector< std::string > &target_paths, const std::string &name_prefix)
 

Variables

auto simple_file_closer = [](FILE* f) { std::fclose(f); }
 

Function Documentation

std::string anonymous_namespace{TableArchiver.cpp}::abs_path ( const File_Namespace::GlobalFileMgr global_file_mgr)
inline

Definition at line 71 of file TableArchiver.cpp.

References File_Namespace::GlobalFileMgr::getBasePath().

Referenced by TableArchiver::dumpTable(), rename_table_directories(), and TableArchiver::restoreTable().

71  {
72  return boost::filesystem::canonical(global_file_mgr->getBasePath()).string();
73 }
std::string getBasePath() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{TableArchiver.cpp}::add_data_file_symlinks ( const std::string &  table_data_dir)

Definition at line 278 of file TableArchiver.cpp.

References DATA_FILE_EXT, and File_Namespace::kLegacyDataFileExtension.

Referenced by rename_table_directories().

278  {
279  std::map<boost::filesystem::path, boost::filesystem::path> old_to_new_paths;
280  for (boost::filesystem::directory_iterator it(table_data_dir), end_it; it != end_it;
281  it++) {
282  const auto path = boost::filesystem::canonical(it->path());
283  if (path.extension().string() == DATA_FILE_EXT) {
284  auto old_path = path;
285  old_path.replace_extension(File_Namespace::kLegacyDataFileExtension);
286  // Add a symlink to data file, if one does not exist.
287  if (!boost::filesystem::exists(old_path)) {
288  old_to_new_paths[old_path] = path;
289  }
290  }
291  }
292  for (const auto& [old_path, new_path] : old_to_new_paths) {
293  boost::filesystem::create_symlink(new_path.filename(), old_path);
294  }
295 }
#define DATA_FILE_EXT
Definition: File.h:25
constexpr auto kLegacyDataFileExtension
Definition: File.h:36

+ Here is the caller graph for this function:

void anonymous_namespace{TableArchiver.cpp}::adjust_altered_table_files ( const int32_t  table_epoch,
const std::string &  temp_data_dir,
const std::unordered_map< int, int > &  column_ids_map 
)

Definition at line 247 of file TableArchiver.cpp.

References ThreadController_NS::SimpleThreadController< FutureReturnType >::checkThreadsStatus(), cpu_threads(), ThreadController_NS::SimpleThreadController< FutureReturnType >::finish(), rewrite_column_ids_in_page_headers(), and ThreadController_NS::SimpleThreadController< FutureReturnType >::startThread().

Referenced by TableArchiver::restoreTable().

249  {
250  boost::filesystem::path base_path(temp_data_dir);
251  boost::filesystem::recursive_directory_iterator end_it;
253  for (boost::filesystem::recursive_directory_iterator fit(base_path); fit != end_it;
254  ++fit) {
255  if (!boost::filesystem::is_symlink(fit->path()) &&
256  boost::filesystem::is_regular_file(fit->status())) {
257  thread_controller.startThread(
258  rewrite_column_ids_in_page_headers, fit->path(), column_ids_map, table_epoch);
259  thread_controller.checkThreadsStatus();
260  }
261  }
262  thread_controller.finish();
263 }
void rewrite_column_ids_in_page_headers(const boost::filesystem::path &path, const std::unordered_map< int, int > &column_ids_map, const int32_t table_epoch)
int cpu_threads()
Definition: thread_count.h:24

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{TableArchiver.cpp}::delete_old_symlinks ( const std::string &  table_data_dir)

Definition at line 265 of file TableArchiver.cpp.

Referenced by rename_table_directories().

265  {
266  std::vector<boost::filesystem::path> symlinks;
267  for (boost::filesystem::directory_iterator it(table_data_dir), end_it; it != end_it;
268  it++) {
269  if (boost::filesystem::is_symlink(it->path())) {
270  symlinks.emplace_back(it->path());
271  }
272  }
273  for (const auto& symlink : symlinks) {
274  boost::filesystem::remove_all(symlink);
275  }
276 }

+ Here is the caller graph for this function:

std::string anonymous_namespace{TableArchiver.cpp}::get_table_schema ( const std::string &  archive_path,
const std::string &  table,
const std::string &  compression 
)
inline

Definition at line 165 of file TableArchiver.cpp.

References simple_file_cat(), and table_schema_filename.

Referenced by TableArchiver::restoreTable().

167  {
168  const auto schema_str =
169  simple_file_cat(archive_path, table_schema_filename, compression);
170  std::regex regex("@T");
171  return std::regex_replace(schema_str, regex, table);
172 }
static constexpr char const * table_schema_filename
std::string simple_file_cat(const std::string &archive_path, const std::string &file_name, const std::string &compression)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{TableArchiver.cpp}::rename_table_directories ( const File_Namespace::GlobalFileMgr global_file_mgr,
const std::string &  temp_data_dir,
const std::vector< std::string > &  target_paths,
const std::string &  name_prefix 
)

Definition at line 297 of file TableArchiver.cpp.

References abs_path(), add_data_file_symlinks(), delete_old_symlinks(), and File_Namespace::FileMgr::renameAndSymlinkLegacyFiles().

Referenced by TableArchiver::restoreTable().

300  {
301  boost::filesystem::path base_path(temp_data_dir);
302  boost::filesystem::directory_iterator end_it;
303  int target_path_index = 0;
304  for (boost::filesystem::directory_iterator fit(base_path); fit != end_it; ++fit) {
305  if (!boost::filesystem::is_regular_file(fit->status())) {
306  const std::string file_path = fit->path().string();
307  const std::string file_name = fit->path().filename().string();
308  if (boost::istarts_with(file_name, name_prefix)) {
309  const std::string target_path =
310  abs_path(global_file_mgr) + "/" + target_paths[target_path_index++];
311  if (std::rename(file_path.c_str(), target_path.c_str())) {
312  throw std::runtime_error("Failed to rename file " + file_path + " to " +
313  target_path + ": " + std::strerror(errno));
314  }
315  // Delete any old/invalid symlinks contained in table dump.
316  delete_old_symlinks(target_path);
318  // For post-rebrand table dumps, symlinks need to be added here, since file mgr
319  // migration would already have been executed for the dumped table.
320  add_data_file_symlinks(target_path);
321  }
322  }
323  }
324 }
void delete_old_symlinks(const std::string &table_data_dir)
std::string abs_path(const File_Namespace::GlobalFileMgr *global_file_mgr)
static void renameAndSymlinkLegacyFiles(const std::string &table_data_dir)
Definition: FileMgr.cpp:1110
void add_data_file_symlinks(const std::string &table_data_dir)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{TableArchiver.cpp}::rewrite_column_ids_in_page_headers ( const boost::filesystem::path &  path,
const std::unordered_map< int, int > &  column_ids_map,
const int32_t  table_epoch 
)

Definition at line 176 of file TableArchiver.cpp.

References CHECK, DATA_FILE_EXT, heavyai::file_size(), heavyai::fopen(), File_Namespace::is_page_deleted_with_checkpoint(), simple_file_closer, split(), and to_string().

Referenced by adjust_altered_table_files().

179  {
180  const std::string file_path = path.string();
181  const std::string file_name = path.filename().string();
182  std::vector<std::string> tokens;
183  boost::split(tokens, file_name, boost::is_any_of("."));
184 
185  // ref. FileMgr::init for hint of data file name layout
186  if (tokens.size() <= 2 || !(DATA_FILE_EXT == "." + tokens[2] || tokens[2] == "mapd")) {
187  // We are only interested in files in the form <id>.<page_size>.<DATA_FILE_EXT>
188  return;
189  }
190 
191  const auto page_size = boost::lexical_cast<int64_t>(tokens[1]);
192  const auto file_size = boost::filesystem::file_size(file_path);
193  std::unique_ptr<FILE, decltype(simple_file_closer)> fp(
194  std::fopen(file_path.c_str(), "r+"), simple_file_closer);
195  if (!fp) {
196  throw std::runtime_error("Failed to open " + file_path +
197  " for update: " + std::strerror(errno));
198  }
199  // TODO(Misiu): Rather than reference an exernal layout we should de-duplicate this
200  // page-reading code in a single location. This will also reduce the need for comments
201  // below.
202  // ref. FileInfo::openExistingFile for hint of chunk header layout
203  for (size_t page = 0; page < file_size / page_size; ++page) {
204  int32_t header_info[8];
205  if (0 != std::fseek(fp.get(), page * page_size, SEEK_SET)) {
206  throw std::runtime_error("Failed to seek to page# " + std::to_string(page) +
207  file_path + " for read: " + std::strerror(errno));
208  }
209  if (1 != fread(header_info, sizeof header_info, 1, fp.get())) {
210  throw std::runtime_error("Failed to read " + file_path + ": " +
211  std::strerror(errno));
212  }
213  if (const auto header_size = header_info[0]; header_size > 0) {
214  // header_info[1] is the page's db_id; but can also be used as an "is deleted"
215  // indicator if negative.
216  auto& contingent = header_info[1];
217  // header_info[2] is the page's table_id; but can also used to store the page's
218  // epoch since the FileMgr stores table_id information separately.
219  auto& epoch = header_info[2];
220  auto& col_id = header_info[3];
222  table_epoch, epoch, contingent)) {
223  continue;
224  }
225  auto column_map_it = column_ids_map.find(col_id);
226  CHECK(column_map_it != column_ids_map.end()) << "could not find " << col_id;
227  // If a header contains a column id that is remapped to new location
228  // then write that change to the file.
229  if (const auto dest_col_id = column_map_it->second; col_id != dest_col_id) {
230  col_id = dest_col_id;
231  if (0 != std::fseek(fp.get(), page * page_size, SEEK_SET)) {
232  throw std::runtime_error("Failed to seek to page# " + std::to_string(page) +
233  file_path + " for write: " + std::strerror(errno));
234  }
235  if (1 != fwrite(header_info, sizeof header_info, 1, fp.get())) {
236  throw std::runtime_error("Failed to write " + file_path + ": " +
237  std::strerror(errno));
238  }
239  }
240  }
241  }
242 }
#define DATA_FILE_EXT
Definition: File.h:25
std::string to_string(char const *&&v)
std::vector< std::string > split(std::string_view str, std::string_view delim, std::optional< size_t > maxsplit)
split apart a string into a vector of substrings
::FILE * fopen(const char *filename, const char *mode)
Definition: omnisci_fs.cpp:72
bool is_page_deleted_with_checkpoint(int32_t table_epoch, int32_t page_epoch, int32_t contingent)
Definition: FileInfo.cpp:267
#define CHECK(condition)
Definition: Logger.h:223
size_t file_size(const int fd)
Definition: omnisci_fs.cpp:31

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string anonymous_namespace{TableArchiver.cpp}::run ( const std::string &  cmd,
const std::string &  chdir = "" 
)
inline

Definition at line 75 of file TableArchiver.cpp.

References logger::ERROR, measure< TimeT >::execution(), LOG, to_lower(), to_string(), and VLOG.

75  {
76  VLOG(3) << "running cmd: " << cmd;
77  int rcode;
78  std::error_code ec;
79  std::string output, errors;
80  const auto time_ms = measure<>::execution([&]() {
81  using namespace boost::process;
82  ipstream stdout, stderr;
83  if (!chdir.empty()) {
84  rcode = system(cmd, std_out > stdout, std_err > stderr, ec, start_dir = chdir);
85  } else {
86  rcode = system(cmd, std_out > stdout, std_err > stderr, ec);
87  }
88  std::ostringstream ss_output, ss_errors;
89  stdout >> ss_output.rdbuf();
90  stderr >> ss_errors.rdbuf();
91  output = ss_output.str();
92  errors = ss_errors.str();
93  });
94  if (rcode || ec) {
95  LOG(ERROR) << "failed cmd: " << cmd;
96  LOG(ERROR) << "exit code: " << rcode;
97  LOG(ERROR) << "error code: " << ec.value() << " - " << ec.message();
98  LOG(ERROR) << "stdout: " << output;
99  LOG(ERROR) << "stderr: " << errors;
100 #if defined(__APPLE__)
101  // osx bsdtar options "--use-compress-program" and "--fast-read" together
102  // run into pipe write error after tar extracts the first occurrence of a
103  // file and closes the read end while the decompression program still writes
104  // to the pipe. bsdtar doesn't handle this situation well like gnu tar does.
105  if (1 == rcode && cmd.find("--fast-read") &&
106  (errors.find("cannot write decoded block") != std::string::npos ||
107  errors.find("Broken pipe") != std::string::npos)) {
108  // ignore this error, or lose speed advantage of "--fast-read" on osx.
109  LOG(ERROR) << "tar error ignored on osx for --fast-read";
110  } else
111 #endif
112  // circumvent tar warning on reading file that is "changed as we read it".
113  // this warning results from reading a table file under concurrent inserts
114  if (1 == rcode && errors.find("changed as we read") != std::string::npos) {
115  LOG(ERROR) << "tar error ignored under concurrent inserts";
116  } else {
117  int error_code;
118  std::string error_message;
119  if (ec) {
120  error_code = ec.value();
121  error_message = ec.message();
122  } else {
123  error_code = rcode;
124  // Show a more concise message for permission errors instead of the default
125  // verbose message. Error logs will still contain all details.
126  if (to_lower(errors).find("permission denied") != std::string::npos) {
127  error_message = "Insufficient file read/write permission.";
128  } else {
129  error_message = errors;
130  }
131  }
132  throw std::runtime_error(
133  "An error occurred while executing an internal command. Error code: " +
134  std::to_string(error_code) + ", message: " + error_message);
135  }
136  } else {
137  VLOG(3) << "finished cmd: " << cmd;
138  VLOG(3) << "time: " << time_ms << " ms";
139  VLOG(3) << "stdout: " << output;
140  }
141  return output;
142 }
std::string to_lower(const std::string &str)
static TimeT::rep execution(F func, Args &&...args)
Definition: sample.cpp:29
#define LOG(tag)
Definition: Logger.h:217
std::string to_string(char const *&&v)
#define VLOG(n)
Definition: Logger.h:317

+ Here is the call graph for this function:

std::string anonymous_namespace{TableArchiver.cpp}::simple_file_cat ( const std::string &  archive_path,
const std::string &  file_name,
const std::string &  compression 
)
inline

Definition at line 144 of file TableArchiver.cpp.

References get_quoted_string(), ddl_utils::IMPORT, run, and ddl_utils::validate_allowed_file_path().

Referenced by get_table_schema(), and TableArchiver::restoreTable().

146  {
149 #if defined(__APPLE__)
150  constexpr static auto opt_occurrence = "--fast-read";
151 #else
152  constexpr static auto opt_occurrence = "--occurrence=1";
153 #endif
154  boost::filesystem::path temp_dir =
155  boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
156  boost::filesystem::create_directories(temp_dir);
157  run("tar " + compression + " -xvf " + get_quoted_string(archive_path) + " " +
158  opt_occurrence + " " + file_name,
159  temp_dir.string());
160  const auto output = run("cat " + (temp_dir / file_name).string());
161  boost::filesystem::remove_all(temp_dir);
162  return output;
163 }
std::string get_quoted_string(const std::string &filename, char quote, char escape)
Quote a string while escaping any existing quotes in the string.
void validate_allowed_file_path(const std::string &file_path, const DataTransferType data_transfer_type, const bool allow_wildcards)
Definition: DdlUtils.cpp:771
static bool run

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

auto anonymous_namespace{TableArchiver.cpp}::simple_file_closer = [](FILE* f) { std::fclose(f); }
inline