OmniSciDB  a5dc49c757
 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 693 of file Importer.h.

Constructor & Destructor Documentation

import_export::DataStreamSink::DataStreamSink ( )
inline

Definition at line 695 of file Importer.h.

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

Definition at line 696 of file Importer.h.

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

Definition at line 698 of file Importer.h.

698 {}

Member Function Documentation

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

Definition at line 3560 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().

3561  {
3562  // in generalized importing scheme, reaching here file_path may
3563  // contain a file path, a url or a wildcard of file paths.
3564  // see CopyTableStmt::execute.
3565 
3566  std::vector<std::string> file_paths;
3567  try {
3572  file_paths = shared::local_glob_filter_sort_files(file_path, options);
3573  } catch (const shared::FileNotFoundException& e) {
3574  // After finding no matching files locally, file_path may still be an s3 url
3575  file_paths.push_back(file_path);
3576  }
3577 
3578  // sum up sizes of all local files -- only for local files. if
3579  // file_path is a s3 url, sizes will be obtained via S3Archive.
3580  for (const auto& file_path : file_paths) {
3582  }
3583 
3584  // s3 parquet goes different route because the files do not use libarchive
3585  // but parquet api, and they need to landed like .7z files.
3586  //
3587  // note: parquet must be explicitly specified by a WITH parameter
3588  // "source_type='parquet_file'", because for example spark sql users may specify a
3589  // output url w/o file extension like this:
3590  // df.write
3591  // .mode("overwrite")
3592  // .parquet("s3://bucket/folder/parquet/mydata")
3593  // without the parameter, it means plain or compressed csv files.
3594  // note: .ORC and AVRO files should follow a similar path to Parquet?
3596 #ifdef ENABLE_IMPORT_PARQUET
3597  import_parquet(file_paths, session_info);
3598 #else
3599  throw std::runtime_error("Parquet not supported!");
3600 #endif
3601  } else {
3602  import_compressed(file_paths, session_info);
3603  }
3604 
3605  return import_status_;
3606 }
void import_compressed(std::vector< std::string > &file_paths, const Catalog_Namespace::SessionInfo *session_info)
Definition: Importer.cpp:4086
auto get_filesize(const std::string &file_path)
Definition: Importer.cpp:115
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:720

+ 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 710 of file Importer.h.

References copy_params.

Referenced by DBHandler::detect_column_types().

710  {
711  return copy_params;
712  }

+ 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 4086 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().

4088  {
4089  // a new requirement is to have one single input stream into
4090  // Importer::importDelimited, so need to move pipe related
4091  // stuff to the outmost block.
4092  int fd[2];
4093 #ifdef _WIN32
4094  // For some reason when folly is used to create the pipe, reader can
4095  // read nothing.
4096  auto pipe_res =
4097  _pipe(fd, static_cast<unsigned int>(copy_params.buffer_size), _O_BINARY);
4098 #else
4099  auto pipe_res = pipe(fd);
4100 #endif
4101  if (pipe_res < 0) {
4102  throw std::runtime_error(std::string("failed to create a pipe: ") + strerror(errno));
4103  }
4104 #ifndef _WIN32
4105  signal(SIGPIPE, SIG_IGN);
4106 #endif
4107 
4108  std::exception_ptr teptr;
4109  // create a thread to read uncompressed byte stream out of pipe and
4110  // feed into importDelimited()
4111  ImportStatus ret1;
4112  auto th_pipe_reader = std::thread([&]() {
4113  try {
4114  // importDelimited will read from FILE* p_file
4115  if (0 == (p_file = fdopen(fd[0], "r"))) {
4116  throw std::runtime_error(std::string("failed to open a pipe: ") +
4117  strerror(errno));
4118  }
4119 
4120  // in future, depending on data types of this uncompressed stream
4121  // it can be feed into other function such like importParquet, etc
4122  ret1 = importDelimited(file_path, true, session_info);
4123 
4124  } catch (...) {
4125  if (!teptr) { // no replace
4126  teptr = std::current_exception();
4127  }
4128  }
4129 
4130  if (p_file) {
4131  fclose(p_file);
4132  }
4133  p_file = 0;
4134  });
4135 
4136  // create a thread to iterate all files (in all archives) and
4137  // forward the uncompressed byte stream to fd[1] which is
4138  // then feed into importDelimited, importParquet, and etc.
4139  auto th_pipe_writer = std::thread([&]() {
4140  std::unique_ptr<S3Archive> us3arch;
4141  bool stop = false;
4142  for (size_t fi = 0; !stop && fi < file_paths.size(); fi++) {
4143  try {
4144  auto file_path = file_paths[fi];
4145  std::unique_ptr<Archive> uarch;
4146  std::map<int, std::string> url_parts;
4147  Archive::parse_url(file_path, url_parts);
4148  const std::string S3_objkey_url_scheme = "s3ok";
4149  if ("file" == url_parts[2] || "" == url_parts[2]) {
4150  uarch.reset(new PosixFileArchive(file_path, copy_params.plain_text));
4151  } else if ("s3" == url_parts[2]) {
4152 #ifdef HAVE_AWS_S3
4153  // new a S3Archive with a shared s3client.
4154  // should be safe b/c no wildcard with s3 url
4155  us3arch.reset(new S3Archive(file_path,
4165  us3arch->init_for_read();
4166  total_file_size += us3arch->get_total_file_size();
4167  // not land all files here but one by one in following iterations
4168  for (const auto& objkey : us3arch->get_objkeys()) {
4169  file_paths.emplace_back(std::string(S3_objkey_url_scheme) + "://" + objkey);
4170  }
4171  continue;
4172 #else
4173  throw std::runtime_error("AWS S3 support not available");
4174 #endif // HAVE_AWS_S3
4175  } else if (S3_objkey_url_scheme == url_parts[2]) {
4176 #ifdef HAVE_AWS_S3
4177  auto objkey = file_path.substr(3 + S3_objkey_url_scheme.size());
4178  auto file_path =
4179  us3arch->land(objkey, teptr, nullptr != dynamic_cast<Detector*>(this));
4180  if (0 == file_path.size()) {
4181  throw std::runtime_error(std::string("failed to land s3 object: ") + objkey);
4182  }
4183  uarch.reset(new PosixFileArchive(file_path, copy_params.plain_text));
4184  // file not removed until file closed
4185  us3arch->vacuum(objkey);
4186 #else
4187  throw std::runtime_error("AWS S3 support not available");
4188 #endif // HAVE_AWS_S3
4189  }
4190 #if 0 // TODO(ppan): implement and enable any other archive class
4191  else
4192  if ("hdfs" == url_parts[2])
4193  uarch.reset(new HdfsArchive(file_path));
4194 #endif
4195  else {
4196  throw std::runtime_error(std::string("unsupported archive url: ") + file_path);
4197  }
4198 
4199  // init the archive for read
4200  auto& arch = *uarch;
4201 
4202  // coming here, the archive of url should be ready to be read, unarchived
4203  // and uncompressed by libarchive into a byte stream (in csv) for the pipe
4204  const void* buf;
4205  size_t size;
4206  bool just_saw_archive_header;
4207  bool is_detecting = nullptr != dynamic_cast<Detector*>(this);
4208  bool first_text_header_skipped = false;
4209  // start reading uncompressed bytes of this archive from libarchive
4210  // note! this archive may contain more than one files!
4211  file_offsets.push_back(0);
4212  size_t num_block_read = 0;
4213  while (!stop && !!(just_saw_archive_header = arch.read_next_header())) {
4214  bool insert_line_delim_after_this_file = false;
4215  while (!stop) {
4216  int64_t offset{-1};
4217  auto ok = arch.read_data_block(&buf, &size, &offset);
4218  // can't use (uncompressed) size, so track (max) file offset.
4219  // also we want to capture offset even on e.o.f.
4220  if (offset > 0) {
4221  std::unique_lock<std::mutex> lock(file_offsets_mutex);
4222  file_offsets.back() = offset;
4223  }
4224  if (!ok) {
4225  break;
4226  }
4227  // one subtle point here is now we concatenate all files
4228  // to a single FILE stream with which we call importDelimited
4229  // only once. this would make it misunderstand that only one
4230  // header line is with this 'single' stream, while actually
4231  // we may have one header line for each of the files.
4232  // so we need to skip header lines here instead in importDelimited.
4233  const char* buf2 = (const char*)buf;
4234  int size2 = size;
4236  just_saw_archive_header && (first_text_header_skipped || !is_detecting)) {
4237  while (size2-- > 0) {
4238  if (*buf2++ == copy_params.line_delim) {
4239  break;
4240  }
4241  }
4242  if (size2 <= 0) {
4243  LOG(WARNING) << "No line delimiter in block." << std::endl;
4244  } else {
4245  just_saw_archive_header = false;
4246  first_text_header_skipped = true;
4247  }
4248  }
4249  // In very rare occasions the write pipe somehow operates in a mode similar
4250  // to non-blocking while pipe(fds) should behave like pipe2(fds, 0) which
4251  // means blocking mode. On such a unreliable blocking mode, a possible fix
4252  // is to loop reading till no bytes left, otherwise the annoying `failed to
4253  // write pipe: Success`...
4254  if (size2 > 0) {
4255  int nremaining = size2;
4256  while (nremaining > 0) {
4257  // try to write the entire remainder of the buffer to the pipe
4258  int nwritten = write(fd[1], buf2, nremaining);
4259  // how did we do?
4260  if (nwritten < 0) {
4261  // something bad happened
4262  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
4263  // ignore these, assume nothing written, try again
4264  nwritten = 0;
4265  } else if (errno == EPIPE &&
4267  // the reader thread has shut down the pipe from the read end
4268  stop = true;
4269  break;
4270  } else {
4271  // a real error
4272  throw std::runtime_error(
4273  std::string("failed or interrupted write to pipe: ") +
4274  strerror(errno));
4275  }
4276  } else if (nwritten == nremaining) {
4277  // we wrote everything; we're done
4278  break;
4279  }
4280  // only wrote some (or nothing), try again
4281  nremaining -= nwritten;
4282  buf2 += nwritten;
4283  // no exception when too many rejected
4286  stop = true;
4287  break;
4288  }
4289  }
4290  // check that this file (buf for size) ended with a line delim
4291  if (size > 0) {
4292  const char* plast = static_cast<const char*>(buf) + (size - 1);
4293  insert_line_delim_after_this_file = (*plast != copy_params.line_delim);
4294  }
4295  }
4296  ++num_block_read;
4297  }
4298 
4299  // if that file didn't end with a line delim, we insert one here to terminate
4300  // that file's stream use a loop for the same reason as above
4301  if (insert_line_delim_after_this_file) {
4302  while (true) {
4303  // write the delim char to the pipe
4304  int nwritten = write(fd[1], &copy_params.line_delim, 1);
4305  // how did we do?
4306  if (nwritten < 0) {
4307  // something bad happened
4308  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
4309  // ignore these, assume nothing written, try again
4310  nwritten = 0;
4311  } else if (errno == EPIPE &&
4313  // the reader thread has shut down the pipe from the read end
4314  stop = true;
4315  break;
4316  } else {
4317  // a real error
4318  throw std::runtime_error(
4319  std::string("failed or interrupted write to pipe: ") +
4320  strerror(errno));
4321  }
4322  } else if (nwritten == 1) {
4323  // we wrote it; we're done
4324  break;
4325  }
4326  }
4327  }
4328  }
4329  } catch (...) {
4330  // when import is aborted because too many data errors or because end of a
4331  // detection, any exception thrown by s3 sdk or libarchive is okay and should be
4332  // suppressed.
4335  break;
4336  }
4337  if (import_status_.rows_completed > 0) {
4338  if (nullptr != dynamic_cast<Detector*>(this)) {
4339  break;
4340  }
4341  }
4342  if (!teptr) { // no replace
4343  teptr = std::current_exception();
4344  }
4345  break;
4346  }
4347  }
4348  // close writer end
4349  close(fd[1]);
4350  });
4351 
4352  th_pipe_reader.join();
4353  th_pipe_writer.join();
4354 
4355  // rethrow any exception happened herebefore
4356  if (teptr) {
4357  std::rethrow_exception(teptr);
4358  }
4359 }
std::string s3_secret_key
Definition: CopyParams.h:62
#define LOG(tag)
Definition: Logger.h:285
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:143
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:170
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:725
std::optional< std::string > file_sort_order_by
Definition: CopyParams.h:86
heavyai::shared_mutex import_mutex_
Definition: Importer.h:723
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:164
const std::string file_path
Definition: Importer.h:720

+ 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 725 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 726 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 720 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: