OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Importer_NS::DataStreamSink Class Referenceabstract

#include <Importer.h>

+ Inheritance diagram for Importer_NS::DataStreamSink:
+ Collaboration diagram for Importer_NS::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)=0
 
const CopyParamsget_copy_params () const
 
void import_compressed (std::vector< std::string > &file_paths)
 

Protected Member Functions

ImportStatus archivePlumber ()
 

Protected Attributes

CopyParams copy_params
 
const std::string file_path
 
FILE * p_file = nullptr
 
ImportStatus import_status
 
bool load_failed = false
 
size_t total_file_size {0}
 
std::vector< size_t > file_offsets
 
std::mutex file_offsets_mutex
 

Detailed Description

Definition at line 635 of file Importer.h.

Constructor & Destructor Documentation

Importer_NS::DataStreamSink::DataStreamSink ( )
inline

Definition at line 637 of file Importer.h.

637 {}
Importer_NS::DataStreamSink::DataStreamSink ( const CopyParams copy_params,
const std::string  file_path 
)
inline

Definition at line 638 of file Importer.h.

virtual Importer_NS::DataStreamSink::~DataStreamSink ( )
inlinevirtual

Definition at line 640 of file Importer.h.

640 {}

Member Function Documentation

ImportStatus Importer_NS::DataStreamSink::archivePlumber ( )
protected

Definition at line 3146 of file Importer.cpp.

References copy_params, file_path, Importer_NS::CopyParams::file_type, get_filesize(), import_compressed(), import_status, mapd_glob(), and total_file_size.

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

3146  {
3147  // in generalized importing scheme, reaching here file_path may
3148  // contain a file path, a url or a wildcard of file paths.
3149  // see CopyTableStmt::execute.
3150  auto file_paths = mapd_glob(file_path);
3151  if (file_paths.size() == 0) {
3152  file_paths.push_back(file_path);
3153  }
3154 
3155  // sum up sizes of all local files -- only for local files. if
3156  // file_path is a s3 url, sizes will be obtained via S3Archive.
3157  for (const auto& file_path : file_paths) {
3159  }
3160 
3161 #ifdef ENABLE_IMPORT_PARQUET
3162  // s3 parquet goes different route because the files do not use libarchive
3163  // but parquet api, and they need to landed like .7z files.
3164  //
3165  // note: parquet must be explicitly specified by a WITH parameter "parquet='true'",
3166  // because for example spark sql users may specify a output url w/o file
3167  // extension like this:
3168  // df.write
3169  // .mode("overwrite")
3170  // .parquet("s3://bucket/folder/parquet/mydata")
3171  // without the parameter, it means plain or compressed csv files.
3172  // note: .ORC and AVRO files should follow a similar path to Parquet?
3173  if (copy_params.file_type == FileType::PARQUET) {
3174  import_parquet(file_paths);
3175  } else
3176 #endif
3177  {
3178  import_compressed(file_paths);
3179  }
3180  return import_status;
3181 }
void import_compressed(std::vector< std::string > &file_paths)
Definition: Importer.cpp:3542
auto get_filesize(const std::string &file_path)
Definition: Importer.cpp:77
std::vector< std::string > mapd_glob(const std::string &pattern)
Definition: mapd_glob.cpp:22
ImportStatus import_status
Definition: Importer.h:656
const std::string file_path
Definition: Importer.h:654

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 647 of file Importer.h.

References copy_params.

Referenced by MapDHandler::detect_column_types().

647 { return copy_params; }

+ Here is the caller graph for this function:

void Importer_NS::DataStreamSink::import_compressed ( std::vector< std::string > &  file_paths)

Definition at line 3542 of file Importer.cpp.

References File_Namespace::close(), copy_params, file_offsets, file_offsets_mutex, Importer_NS::CopyParams::has_header, import_status, importDelimited(), Importer_NS::CopyParams::line_delim, Importer_NS::ImportStatus::load_truncated, LOG, Importer_NS::NO_HEADER, p_file, Archive::parse_url(), Importer_NS::CopyParams::plain_text, Importer_NS::ImportStatus::rows_completed, Importer_NS::CopyParams::s3_access_key, Importer_NS::CopyParams::s3_endpoint, Importer_NS::CopyParams::s3_region, Importer_NS::CopyParams::s3_secret_key, Importer_NS::status_mutex, total_file_size, logger::WARNING, and File_Namespace::write().

Referenced by archivePlumber().

3542  {
3543  // a new requirement is to have one single input stream into
3544  // Importer::importDelimited, so need to move pipe related
3545  // stuff to the outmost block.
3546  int fd[2];
3547  if (pipe(fd) < 0) {
3548  throw std::runtime_error(std::string("failed to create a pipe: ") + strerror(errno));
3549  }
3550  signal(SIGPIPE, SIG_IGN);
3551 
3552  std::exception_ptr teptr;
3553  // create a thread to read uncompressed byte stream out of pipe and
3554  // feed into importDelimited()
3555  ImportStatus ret;
3556  auto th_pipe_reader = std::thread([&]() {
3557  try {
3558  // importDelimited will read from FILE* p_file
3559  if (0 == (p_file = fdopen(fd[0], "r"))) {
3560  throw std::runtime_error(std::string("failed to open a pipe: ") +
3561  strerror(errno));
3562  }
3563 
3564  // in future, depending on data types of this uncompressed stream
3565  // it can be feed into other function such like importParquet, etc
3566  ret = importDelimited(file_path, true);
3567  } catch (...) {
3568  if (!teptr) { // no replace
3569  teptr = std::current_exception();
3570  }
3571  }
3572 
3573  if (p_file) {
3574  fclose(p_file);
3575  }
3576  p_file = 0;
3577  });
3578 
3579  // create a thread to iterate all files (in all archives) and
3580  // forward the uncompressed byte stream to fd[1] which is
3581  // then feed into importDelimited, importParquet, and etc.
3582  auto th_pipe_writer = std::thread([&]() {
3583  std::unique_ptr<S3Archive> us3arch;
3584  bool stop = false;
3585  for (size_t fi = 0; !stop && fi < file_paths.size(); fi++) {
3586  try {
3587  auto file_path = file_paths[fi];
3588  std::unique_ptr<Archive> uarch;
3589  std::map<int, std::string> url_parts;
3590  Archive::parse_url(file_path, url_parts);
3591  const std::string S3_objkey_url_scheme = "s3ok";
3592  if ("file" == url_parts[2] || "" == url_parts[2]) {
3593  uarch.reset(new PosixFileArchive(file_path, copy_params.plain_text));
3594  } else if ("s3" == url_parts[2]) {
3595 #ifdef HAVE_AWS_S3
3596  // new a S3Archive with a shared s3client.
3597  // should be safe b/c no wildcard with s3 url
3598  us3arch.reset(new S3Archive(file_path,
3604  us3arch->init_for_read();
3605  total_file_size += us3arch->get_total_file_size();
3606  // not land all files here but one by one in following iterations
3607  for (const auto& objkey : us3arch->get_objkeys()) {
3608  file_paths.emplace_back(std::string(S3_objkey_url_scheme) + "://" + objkey);
3609  }
3610  continue;
3611 #else
3612  throw std::runtime_error("AWS S3 support not available");
3613 #endif // HAVE_AWS_S3
3614  } else if (S3_objkey_url_scheme == url_parts[2]) {
3615 #ifdef HAVE_AWS_S3
3616  auto objkey = file_path.substr(3 + S3_objkey_url_scheme.size());
3617  auto file_path =
3618  us3arch->land(objkey, teptr, nullptr != dynamic_cast<Detector*>(this));
3619  if (0 == file_path.size()) {
3620  throw std::runtime_error(std::string("failed to land s3 object: ") + objkey);
3621  }
3622  uarch.reset(new PosixFileArchive(file_path, copy_params.plain_text));
3623  // file not removed until file closed
3624  us3arch->vacuum(objkey);
3625 #else
3626  throw std::runtime_error("AWS S3 support not available");
3627 #endif // HAVE_AWS_S3
3628  }
3629 #if 0 // TODO(ppan): implement and enable any other archive class
3630  else
3631  if ("hdfs" == url_parts[2])
3632  uarch.reset(new HdfsArchive(file_path));
3633 #endif
3634  else {
3635  throw std::runtime_error(std::string("unsupported archive url: ") + file_path);
3636  }
3637 
3638  // init the archive for read
3639  auto& arch = *uarch;
3640 
3641  // coming here, the archive of url should be ready to be read, unarchived
3642  // and uncompressed by libarchive into a byte stream (in csv) for the pipe
3643  const void* buf;
3644  size_t size;
3645  bool just_saw_archive_header;
3646  bool is_detecting = nullptr != dynamic_cast<Detector*>(this);
3647  bool first_text_header_skipped = false;
3648  // start reading uncompressed bytes of this archive from libarchive
3649  // note! this archive may contain more than one files!
3650  file_offsets.push_back(0);
3651  while (!stop && !!(just_saw_archive_header = arch.read_next_header())) {
3652  bool insert_line_delim_after_this_file = false;
3653  while (!stop) {
3654  int64_t offset{-1};
3655  auto ok = arch.read_data_block(&buf, &size, &offset);
3656  // can't use (uncompressed) size, so track (max) file offset.
3657  // also we want to capture offset even on e.o.f.
3658  if (offset > 0) {
3659  std::unique_lock<std::mutex> lock(file_offsets_mutex);
3660  file_offsets.back() = offset;
3661  }
3662  if (!ok) {
3663  break;
3664  }
3665  // one subtle point here is now we concatenate all files
3666  // to a single FILE stream with which we call importDelimited
3667  // only once. this would make it misunderstand that only one
3668  // header line is with this 'single' stream, while actually
3669  // we may have one header line for each of the files.
3670  // so we need to skip header lines here instead in importDelimited.
3671  const char* buf2 = (const char*)buf;
3672  int size2 = size;
3674  just_saw_archive_header && (first_text_header_skipped || !is_detecting)) {
3675  while (size2-- > 0) {
3676  if (*buf2++ == copy_params.line_delim) {
3677  break;
3678  }
3679  }
3680  if (size2 <= 0) {
3681  LOG(WARNING) << "No line delimiter in block." << std::endl;
3682  } else {
3683  just_saw_archive_header = false;
3684  first_text_header_skipped = true;
3685  }
3686  }
3687  // In very rare occasions the write pipe somehow operates in a mode similar to
3688  // non-blocking while pipe(fds) should behave like pipe2(fds, 0) which means
3689  // blocking mode. On such a unreliable blocking mode, a possible fix is to
3690  // loop reading till no bytes left, otherwise the annoying `failed to write
3691  // pipe: Success`...
3692  if (size2 > 0) {
3693  int nremaining = size2;
3694  while (nremaining > 0) {
3695  // try to write the entire remainder of the buffer to the pipe
3696  int nwritten = write(fd[1], buf2, nremaining);
3697  // how did we do?
3698  if (nwritten < 0) {
3699  // something bad happened
3700  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
3701  // ignore these, assume nothing written, try again
3702  nwritten = 0;
3703  } else {
3704  // a real error
3705  throw std::runtime_error(
3706  std::string("failed or interrupted write to pipe: ") +
3707  strerror(errno));
3708  }
3709  } else if (nwritten == nremaining) {
3710  // we wrote everything; we're done
3711  break;
3712  }
3713  // only wrote some (or nothing), try again
3714  nremaining -= nwritten;
3715  buf2 += nwritten;
3716  // no exception when too many rejected
3717  // @simon.eves how would this get set? from the other thread? mutex
3718  // needed?
3720  stop = true;
3721  break;
3722  }
3723  }
3724  // check that this file (buf for size) ended with a line delim
3725  if (size > 0) {
3726  const char* plast = static_cast<const char*>(buf) + (size - 1);
3727  insert_line_delim_after_this_file = (*plast != copy_params.line_delim);
3728  }
3729  }
3730  }
3731  // if that file didn't end with a line delim, we insert one here to terminate
3732  // that file's stream use a loop for the same reason as above
3733  if (insert_line_delim_after_this_file) {
3734  while (true) {
3735  // write the delim char to the pipe
3736  int nwritten = write(fd[1], &copy_params.line_delim, 1);
3737  // how did we do?
3738  if (nwritten < 0) {
3739  // something bad happened
3740  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
3741  // ignore these, assume nothing written, try again
3742  nwritten = 0;
3743  } else {
3744  // a real error
3745  throw std::runtime_error(
3746  std::string("failed or interrupted write to pipe: ") +
3747  strerror(errno));
3748  }
3749  } else if (nwritten == 1) {
3750  // we wrote it; we're done
3751  break;
3752  }
3753  }
3754  }
3755  }
3756  } catch (...) {
3757  // when import is aborted because too many data errors or because end of a
3758  // detection, any exception thrown by s3 sdk or libarchive is okay and should be
3759  // suppressed.
3760  mapd_shared_lock<mapd_shared_mutex> read_lock(status_mutex);
3762  break;
3763  }
3764  if (import_status.rows_completed > 0) {
3765  if (nullptr != dynamic_cast<Detector*>(this)) {
3766  break;
3767  }
3768  }
3769  if (!teptr) { // no replace
3770  teptr = std::current_exception();
3771  }
3772  break;
3773  }
3774  }
3775  // close writer end
3776  close(fd[1]);
3777  });
3778 
3779  th_pipe_reader.join();
3780  th_pipe_writer.join();
3781 
3782  // rethrow any exception happened herebefore
3783  if (teptr) {
3784  std::rethrow_exception(teptr);
3785  }
3786 }
std::vector< size_t > file_offsets
Definition: Importer.h:659
#define LOG(tag)
Definition: Logger.h:185
virtual ImportStatus importDelimited(const std::string &file_path, const bool decompressed)=0
static mapd_shared_mutex status_mutex
Definition: Importer.cpp:146
std::string s3_access_key
Definition: CopyParams.h:62
ImportHeaderRow has_header
Definition: CopyParams.h:48
std::string s3_endpoint
Definition: CopyParams.h:65
std::mutex file_offsets_mutex
Definition: Importer.h:660
std::string s3_region
Definition: CopyParams.h:64
ImportStatus import_status
Definition: Importer.h:656
size_t write(FILE *f, const size_t offset, const size_t size, int8_t *buf)
Writes the specified number of bytes to the offset position in file f from buf.
Definition: File.cpp:121
void close(FILE *f)
Closes the file pointed to by the FILE pointer.
Definition: File.cpp:102
std::string s3_secret_key
Definition: CopyParams.h:63
const std::string file_path
Definition: Importer.h:654
static void parse_url(const std::string url, std::map< int, std::string > &url_parts)
Definition: Archive.h:154

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

virtual ImportStatus Importer_NS::DataStreamSink::importDelimited ( const std::string &  file_path,
const bool  decompressed 
)
pure virtual

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

Referenced by import_compressed().

+ Here is the caller graph for this function:

Member Data Documentation

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

Definition at line 659 of file Importer.h.

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

std::mutex Importer_NS::DataStreamSink::file_offsets_mutex
protected

Definition at line 660 of file Importer.h.

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

const std::string Importer_NS::DataStreamSink::file_path
protected

Definition at line 654 of file Importer.h.

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

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

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