OmniSciDB  c0231cc57d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
import_export::DataStreamSink Class Referenceabstract

#include <Importer.h>

+ Inheritance diagram for import_export::DataStreamSink:
+ Collaboration diagram for import_export::DataStreamSink:

Public Member Functions

 DataStreamSink ()
 
 DataStreamSink (const CopyParams &copy_params, const std::string file_path)
 
virtual ~DataStreamSink ()
 
virtual ImportStatus importDelimited (const std::string &file_path, const bool decompressed, const Catalog_Namespace::SessionInfo *session_info)=0
 
const CopyParamsget_copy_params () const
 
void import_compressed (std::vector< std::string > &file_paths, const Catalog_Namespace::SessionInfo *session_info)
 

Protected Member Functions

ImportStatus archivePlumber (const Catalog_Namespace::SessionInfo *session_info)
 

Protected Attributes

CopyParams copy_params
 
const std::string file_path
 
FILE * p_file = nullptr
 
ImportStatus import_status_
 
heavyai::shared_mutex import_mutex_
 
size_t total_file_size {0}
 
std::vector< size_t > file_offsets
 
std::mutex file_offsets_mutex
 

Detailed Description

Definition at line 687 of file Importer.h.

Constructor & Destructor Documentation

import_export::DataStreamSink::DataStreamSink ( )
inline

Definition at line 689 of file Importer.h.

689 {}
import_export::DataStreamSink::DataStreamSink ( const CopyParams copy_params,
const std::string  file_path 
)
inline

Definition at line 690 of file Importer.h.

virtual import_export::DataStreamSink::~DataStreamSink ( )
inlinevirtual

Definition at line 692 of file Importer.h.

692 {}

Member Function Documentation

ImportStatus import_export::DataStreamSink::archivePlumber ( const Catalog_Namespace::SessionInfo session_info)
protected

Definition at line 3677 of file Importer.cpp.

References copy_params, file_path, import_export::CopyParams::file_sort_order_by, import_export::CopyParams::file_sort_regex, get_filesize(), import_compressed(), import_status_, import_export::kParquetFile, shared::local_glob_filter_sort_files(), import_export::CopyParams::regex_path_filter, import_export::CopyParams::source_type, total_file_size, and shared::validate_sort_options().

Referenced by import_export::Importer::import(), and import_export::Detector::read_file().

3678  {
3679  // in generalized importing scheme, reaching here file_path may
3680  // contain a file path, a url or a wildcard of file paths.
3681  // see CopyTableStmt::execute.
3682 
3683  std::vector<std::string> file_paths;
3684  try {
3689  file_paths = shared::local_glob_filter_sort_files(file_path, options);
3690  } catch (const shared::FileNotFoundException& e) {
3691  // After finding no matching files locally, file_path may still be an s3 url
3692  file_paths.push_back(file_path);
3693  }
3694 
3695  // sum up sizes of all local files -- only for local files. if
3696  // file_path is a s3 url, sizes will be obtained via S3Archive.
3697  for (const auto& file_path : file_paths) {
3699  }
3700 
3701  // s3 parquet goes different route because the files do not use libarchive
3702  // but parquet api, and they need to landed like .7z files.
3703  //
3704  // note: parquet must be explicitly specified by a WITH parameter
3705  // "source_type='parquet_file'", because for example spark sql users may specify a
3706  // output url w/o file extension like this:
3707  // df.write
3708  // .mode("overwrite")
3709  // .parquet("s3://bucket/folder/parquet/mydata")
3710  // without the parameter, it means plain or compressed csv files.
3711  // note: .ORC and AVRO files should follow a similar path to Parquet?
3713 #ifdef ENABLE_IMPORT_PARQUET
3714  import_parquet(file_paths, session_info);
3715 #else
3716  throw std::runtime_error("Parquet not supported!");
3717 #endif
3718  } else {
3719  import_compressed(file_paths, session_info);
3720  }
3721 
3722  return import_status_;
3723 }
void import_compressed(std::vector< std::string > &file_paths, const Catalog_Namespace::SessionInfo *session_info)
Definition: Importer.cpp:4203
auto get_filesize(const std::string &file_path)
Definition: Importer.cpp:116
void validate_sort_options(const FilePathOptions &options)
std::optional< std::string > regex_path_filter
Definition: CopyParams.h:85
import_export::SourceType source_type
Definition: CopyParams.h:57
std::vector< std::string > local_glob_filter_sort_files(const std::string &file_path, const FilePathOptions &options, const bool recurse)
std::optional< std::string > file_sort_order_by
Definition: CopyParams.h:86
std::optional< std::string > file_sort_regex
Definition: CopyParams.h:87
const std::string file_path
Definition: Importer.h:712

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const CopyParams& import_export::DataStreamSink::get_copy_params ( ) const
inline

Definition at line 704 of file Importer.h.

References copy_params.

Referenced by DBHandler::detect_column_types().

704 { return copy_params; }

+ Here is the caller graph for this function:

void import_export::DataStreamSink::import_compressed ( std::vector< std::string > &  file_paths,
const Catalog_Namespace::SessionInfo session_info 
)

Definition at line 4203 of file Importer.cpp.

References heavyai::close(), file_offsets, file_offsets_mutex, import_mutex_, import_status_, importDelimited(), import_export::kImportRowLimit, import_export::kNoHeader, import_export::ImportStatus::load_failed, LOG, p_file, Archive::parse_url(), import_export::ImportStatus::rows_completed, total_file_size, logger::WARNING, and File_Namespace::write().

Referenced by archivePlumber().

4205  {
4206  // a new requirement is to have one single input stream into
4207  // Importer::importDelimited, so need to move pipe related
4208  // stuff to the outmost block.
4209  int fd[2];
4210 #ifdef _WIN32
4211  // For some reason when folly is used to create the pipe, reader can
4212  // read nothing.
4213  auto pipe_res =
4214  _pipe(fd, static_cast<unsigned int>(copy_params.buffer_size), _O_BINARY);
4215 #else
4216  auto pipe_res = pipe(fd);
4217 #endif
4218  if (pipe_res < 0) {
4219  throw std::runtime_error(std::string("failed to create a pipe: ") + strerror(errno));
4220  }
4221 #ifndef _WIN32
4222  signal(SIGPIPE, SIG_IGN);
4223 #endif
4224 
4225  std::exception_ptr teptr;
4226  // create a thread to read uncompressed byte stream out of pipe and
4227  // feed into importDelimited()
4228  ImportStatus ret1;
4229  auto th_pipe_reader = std::thread([&]() {
4230  try {
4231  // importDelimited will read from FILE* p_file
4232  if (0 == (p_file = fdopen(fd[0], "r"))) {
4233  throw std::runtime_error(std::string("failed to open a pipe: ") +
4234  strerror(errno));
4235  }
4236 
4237  // in future, depending on data types of this uncompressed stream
4238  // it can be feed into other function such like importParquet, etc
4239  ret1 = importDelimited(file_path, true, session_info);
4240 
4241  } catch (...) {
4242  if (!teptr) { // no replace
4243  teptr = std::current_exception();
4244  }
4245  }
4246 
4247  if (p_file) {
4248  fclose(p_file);
4249  }
4250  p_file = 0;
4251  });
4252 
4253  // create a thread to iterate all files (in all archives) and
4254  // forward the uncompressed byte stream to fd[1] which is
4255  // then feed into importDelimited, importParquet, and etc.
4256  auto th_pipe_writer = std::thread([&]() {
4257  std::unique_ptr<S3Archive> us3arch;
4258  bool stop = false;
4259  for (size_t fi = 0; !stop && fi < file_paths.size(); fi++) {
4260  try {
4261  auto file_path = file_paths[fi];
4262  std::unique_ptr<Archive> uarch;
4263  std::map<int, std::string> url_parts;
4264  Archive::parse_url(file_path, url_parts);
4265  const std::string S3_objkey_url_scheme = "s3ok";
4266  if ("file" == url_parts[2] || "" == url_parts[2]) {
4267  uarch.reset(new PosixFileArchive(file_path, copy_params.plain_text));
4268  } else if ("s3" == url_parts[2]) {
4269 #ifdef HAVE_AWS_S3
4270  // new a S3Archive with a shared s3client.
4271  // should be safe b/c no wildcard with s3 url
4272  us3arch.reset(new S3Archive(file_path,
4282  us3arch->init_for_read();
4283  total_file_size += us3arch->get_total_file_size();
4284  // not land all files here but one by one in following iterations
4285  for (const auto& objkey : us3arch->get_objkeys()) {
4286  file_paths.emplace_back(std::string(S3_objkey_url_scheme) + "://" + objkey);
4287  }
4288  continue;
4289 #else
4290  throw std::runtime_error("AWS S3 support not available");
4291 #endif // HAVE_AWS_S3
4292  } else if (S3_objkey_url_scheme == url_parts[2]) {
4293 #ifdef HAVE_AWS_S3
4294  auto objkey = file_path.substr(3 + S3_objkey_url_scheme.size());
4295  auto file_path =
4296  us3arch->land(objkey, teptr, nullptr != dynamic_cast<Detector*>(this));
4297  if (0 == file_path.size()) {
4298  throw std::runtime_error(std::string("failed to land s3 object: ") + objkey);
4299  }
4300  uarch.reset(new PosixFileArchive(file_path, copy_params.plain_text));
4301  // file not removed until file closed
4302  us3arch->vacuum(objkey);
4303 #else
4304  throw std::runtime_error("AWS S3 support not available");
4305 #endif // HAVE_AWS_S3
4306  }
4307 #if 0 // TODO(ppan): implement and enable any other archive class
4308  else
4309  if ("hdfs" == url_parts[2])
4310  uarch.reset(new HdfsArchive(file_path));
4311 #endif
4312  else {
4313  throw std::runtime_error(std::string("unsupported archive url: ") + file_path);
4314  }
4315 
4316  // init the archive for read
4317  auto& arch = *uarch;
4318 
4319  // coming here, the archive of url should be ready to be read, unarchived
4320  // and uncompressed by libarchive into a byte stream (in csv) for the pipe
4321  const void* buf;
4322  size_t size;
4323  bool just_saw_archive_header;
4324  bool is_detecting = nullptr != dynamic_cast<Detector*>(this);
4325  bool first_text_header_skipped = false;
4326  // start reading uncompressed bytes of this archive from libarchive
4327  // note! this archive may contain more than one files!
4328  file_offsets.push_back(0);
4329  size_t num_block_read = 0;
4330  while (!stop && !!(just_saw_archive_header = arch.read_next_header())) {
4331  bool insert_line_delim_after_this_file = false;
4332  while (!stop) {
4333  int64_t offset{-1};
4334  auto ok = arch.read_data_block(&buf, &size, &offset);
4335  // can't use (uncompressed) size, so track (max) file offset.
4336  // also we want to capture offset even on e.o.f.
4337  if (offset > 0) {
4338  std::unique_lock<std::mutex> lock(file_offsets_mutex);
4339  file_offsets.back() = offset;
4340  }
4341  if (!ok) {
4342  break;
4343  }
4344  // one subtle point here is now we concatenate all files
4345  // to a single FILE stream with which we call importDelimited
4346  // only once. this would make it misunderstand that only one
4347  // header line is with this 'single' stream, while actually
4348  // we may have one header line for each of the files.
4349  // so we need to skip header lines here instead in importDelimited.
4350  const char* buf2 = (const char*)buf;
4351  int size2 = size;
4353  just_saw_archive_header && (first_text_header_skipped || !is_detecting)) {
4354  while (size2-- > 0) {
4355  if (*buf2++ == copy_params.line_delim) {
4356  break;
4357  }
4358  }
4359  if (size2 <= 0) {
4360  LOG(WARNING) << "No line delimiter in block." << std::endl;
4361  } else {
4362  just_saw_archive_header = false;
4363  first_text_header_skipped = true;
4364  }
4365  }
4366  // In very rare occasions the write pipe somehow operates in a mode similar
4367  // to non-blocking while pipe(fds) should behave like pipe2(fds, 0) which
4368  // means blocking mode. On such a unreliable blocking mode, a possible fix
4369  // is to loop reading till no bytes left, otherwise the annoying `failed to
4370  // write pipe: Success`...
4371  if (size2 > 0) {
4372  int nremaining = size2;
4373  while (nremaining > 0) {
4374  // try to write the entire remainder of the buffer to the pipe
4375  int nwritten = write(fd[1], buf2, nremaining);
4376  // how did we do?
4377  if (nwritten < 0) {
4378  // something bad happened
4379  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
4380  // ignore these, assume nothing written, try again
4381  nwritten = 0;
4382  } else if (errno == EPIPE &&
4384  // the reader thread has shut down the pipe from the read end
4385  stop = true;
4386  break;
4387  } else {
4388  // a real error
4389  throw std::runtime_error(
4390  std::string("failed or interrupted write to pipe: ") +
4391  strerror(errno));
4392  }
4393  } else if (nwritten == nremaining) {
4394  // we wrote everything; we're done
4395  break;
4396  }
4397  // only wrote some (or nothing), try again
4398  nremaining -= nwritten;
4399  buf2 += nwritten;
4400  // no exception when too many rejected
4403  stop = true;
4404  break;
4405  }
4406  }
4407  // check that this file (buf for size) ended with a line delim
4408  if (size > 0) {
4409  const char* plast = static_cast<const char*>(buf) + (size - 1);
4410  insert_line_delim_after_this_file = (*plast != copy_params.line_delim);
4411  }
4412  }
4413  ++num_block_read;
4414  }
4415 
4416  // if that file didn't end with a line delim, we insert one here to terminate
4417  // that file's stream use a loop for the same reason as above
4418  if (insert_line_delim_after_this_file) {
4419  while (true) {
4420  // write the delim char to the pipe
4421  int nwritten = write(fd[1], &copy_params.line_delim, 1);
4422  // how did we do?
4423  if (nwritten < 0) {
4424  // something bad happened
4425  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
4426  // ignore these, assume nothing written, try again
4427  nwritten = 0;
4428  } else if (errno == EPIPE &&
4430  // the reader thread has shut down the pipe from the read end
4431  stop = true;
4432  break;
4433  } else {
4434  // a real error
4435  throw std::runtime_error(
4436  std::string("failed or interrupted write to pipe: ") +
4437  strerror(errno));
4438  }
4439  } else if (nwritten == 1) {
4440  // we wrote it; we're done
4441  break;
4442  }
4443  }
4444  }
4445  }
4446  } catch (...) {
4447  // when import is aborted because too many data errors or because end of a
4448  // detection, any exception thrown by s3 sdk or libarchive is okay and should be
4449  // suppressed.
4452  break;
4453  }
4454  if (import_status_.rows_completed > 0) {
4455  if (nullptr != dynamic_cast<Detector*>(this)) {
4456  break;
4457  }
4458  }
4459  if (!teptr) { // no replace
4460  teptr = std::current_exception();
4461  }
4462  break;
4463  }
4464  }
4465  // close writer end
4466  close(fd[1]);
4467  });
4468 
4469  th_pipe_reader.join();
4470  th_pipe_writer.join();
4471 
4472  // rethrow any exception happened herebefore
4473  if (teptr) {
4474  std::rethrow_exception(teptr);
4475  }
4476 }
std::string s3_secret_key
Definition: CopyParams.h:62
heavyai::shared_lock< heavyai::shared_mutex > read_lock
#define LOG(tag)
Definition: Logger.h:216
size_t write(FILE *f, const size_t offset, const size_t size, const int8_t *buf)
Writes the specified number of bytes to the offset position in file f from buf.
Definition: File.cpp:150
std::shared_lock< T > shared_lock
ImportHeaderRow has_header
Definition: CopyParams.h:46
virtual ImportStatus importDelimited(const std::string &file_path, const bool decompressed, const Catalog_Namespace::SessionInfo *session_info)=0
std::optional< std::string > regex_path_filter
Definition: CopyParams.h:85
std::string s3_session_token
Definition: CopyParams.h:63
static const size_t kImportRowLimit
Definition: Importer.cpp:169
void close(const int fd)
Definition: heavyai_fs.cpp:70
std::string s3_access_key
Definition: CopyParams.h:61
std::vector< size_t > file_offsets
Definition: Importer.h:717
std::optional< std::string > file_sort_order_by
Definition: CopyParams.h:86
heavyai::shared_mutex import_mutex_
Definition: Importer.h:715
std::optional< std::string > file_sort_regex
Definition: CopyParams.h:87
static void parse_url(const std::string url, std::map< int, std::string > &url_parts)
Definition: Archive.h:156
const std::string file_path
Definition: Importer.h:712

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

virtual ImportStatus import_export::DataStreamSink::importDelimited ( const std::string &  file_path,
const bool  decompressed,
const Catalog_Namespace::SessionInfo session_info 
)
pure virtual

Implemented in import_export::Importer, and import_export::Detector.

Referenced by import_compressed().

+ Here is the caller graph for this function:

Member Data Documentation

std::vector<size_t> import_export::DataStreamSink::file_offsets
protected

Definition at line 717 of file Importer.h.

Referenced by import_compressed(), and import_export::Importer::importDelimited().

std::mutex import_export::DataStreamSink::file_offsets_mutex
protected

Definition at line 718 of file Importer.h.

Referenced by import_compressed(), and import_export::Importer::importDelimited().

const std::string import_export::DataStreamSink::file_path
protected

Definition at line 712 of file Importer.h.

Referenced by archivePlumber(), and import_export::Importer::Importer().

size_t import_export::DataStreamSink::total_file_size {0}
protected

The documentation for this class was generated from the following files: