OmniSciDB  29e35f4d58
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

◆ DataStreamSink() [1/2]

Importer_NS::DataStreamSink::DataStreamSink ( )
inline

Definition at line 637 of file Importer.h.

637 {}

◆ DataStreamSink() [2/2]

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

Definition at line 638 of file Importer.h.

◆ ~DataStreamSink()

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

Definition at line 640 of file Importer.h.

640 {}

Member Function Documentation

◆ archivePlumber()

ImportStatus Importer_NS::DataStreamSink::archivePlumber ( )
protected

Definition at line 3150 of file Importer.cpp.

References arrow_throw_if(), Importer_NS::Importer::buffer, copy_params, cpu_threads(), Importer_NS::TypedImportBuffer::del_values(), Importer_NS::CopyParams::delimiter, logger::ERROR, Importer_NS::CopyParams::escape, measure< TimeT >::execution(), Importer_NS::BadRowsTracker::file_name, file_offsets, file_offsets_mutex, file_path, Importer_NS::CopyParams::file_type, ThreadController_NS::SimpleThreadController< FutureReturnType >::finish(), Importer_NS::Importer::get_column_descs(), get_filesize(), Importer_NS::Importer::getLoader(), Importer_NS::HAS_HEADER, Importer_NS::CopyParams::has_header, Importer_NS::Importer::import_buffers_vec, import_compressed(), import_status, Importer_NS::BadRowsTracker::importer, logger::INFO, kARRAY, kBIGINT, kBOOLEAN, kCHAR, kDATE, kDECIMAL, kDOUBLE, kFLOAT, kINT, kLINESTRING, kMULTIPOLYGON, kNUMERIC, kPOINT, kPOLYGON, kSMALLINT, kTEXT, kTIME, kTIMESTAMP, kTINYINT, kVARCHAR, Importer_NS::CopyParams::line_delim, Importer_NS::Importer::load(), load_failed, Importer_NS::ImportStatus::load_truncated, Importer_NS::Importer::loader, LOG, mapd_glob(), Importer_NS::CopyParams::max_reject, Importer_NS::Importer::max_threads, num_rows, Archive::parse_url(), Importer_NS::CopyParams::plain_text, Importer_NS::CopyParams::quote, Importer_NS::CopyParams::quoted, Importer_NS::BadRowsTracker::row_group, Importer_NS::BadRowsTracker::rows, Importer_NS::ImportStatus::rows_completed, Importer_NS::ImportStatus::rows_estimated, Importer_NS::ImportStatus::rows_rejected, Importer_NS::CopyParams::s3_access_key, Importer_NS::CopyParams::s3_endpoint, Importer_NS::CopyParams::s3_region, Importer_NS::CopyParams::s3_secret_key, ThreadController_NS::SimpleThreadController< FutureReturnType >::startThread(), Importer_NS::CopyParams::threads, to_string(), total_file_size, run_benchmark_import::type, anonymous_namespace{ArrowImporter.h}::value_getter(), and VLOG.

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

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

◆ get_copy_params()

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

Definition at line 647 of file Importer.h.

Referenced by MapDHandler::detect_column_types().

647 { return copy_params; }
+ Here is the caller graph for this function:

◆ import_compressed()

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

Definition at line 3546 of file Importer.cpp.

References File_Namespace::close(), copy_params, file_offsets, file_offsets_mutex, Importer_NS::CopyParams::has_header, import_status, Importer_NS::Importer::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, total_file_size, logger::WARNING, and File_Namespace::write().

Referenced by archivePlumber().

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

◆ importDelimited()

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

Member Data Documentation

◆ copy_params

◆ file_offsets

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

◆ file_offsets_mutex

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

◆ file_path

◆ import_status

◆ load_failed

◆ p_file

FILE* Importer_NS::DataStreamSink::p_file = nullptr
protected

◆ total_file_size

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

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