OmniSciDB  cecceef8da
 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 610 of file Importer.h.

Constructor & Destructor Documentation

Importer_NS::DataStreamSink::DataStreamSink ( )
inline

Definition at line 612 of file Importer.h.

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

Definition at line 613 of file Importer.h.

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

Definition at line 615 of file Importer.h.

615 {}

Member Function Documentation

ImportStatus Importer_NS::DataStreamSink::archivePlumber ( )
protected

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

3204  {
3205  // in generalized importing scheme, reaching here file_path may
3206  // contain a file path, a url or a wildcard of file paths.
3207  // see CopyTableStmt::execute.
3208  auto file_paths = mapd_glob(file_path);
3209  if (file_paths.size() == 0) {
3210  file_paths.push_back(file_path);
3211  }
3212 
3213  // sum up sizes of all local files -- only for local files. if
3214  // file_path is a s3 url, sizes will be obtained via S3Archive.
3215  for (const auto& file_path : file_paths) {
3217  }
3218 
3219 #ifdef ENABLE_IMPORT_PARQUET
3220  // s3 parquet goes different route because the files do not use libarchive
3221  // but parquet api, and they need to landed like .7z files.
3222  //
3223  // note: parquet must be explicitly specified by a WITH parameter "parquet='true'",
3224  // because for example spark sql users may specify a output url w/o file
3225  // extension like this:
3226  // df.write
3227  // .mode("overwrite")
3228  // .parquet("s3://bucket/folder/parquet/mydata")
3229  // without the parameter, it means plain or compressed csv files.
3230  // note: .ORC and AVRO files should follow a similar path to Parquet?
3231  if (copy_params.file_type == FileType::PARQUET) {
3232  import_parquet(file_paths);
3233  } else
3234 #endif
3235  {
3236  import_compressed(file_paths);
3237  }
3238  return import_status;
3239 }
void import_compressed(std::vector< std::string > &file_paths)
Definition: Importer.cpp:3601
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:631
const std::string file_path
Definition: Importer.h:629

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

References copy_params.

Referenced by MapDHandler::detect_column_types().

622 { 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 3601 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().

3601  {
3602  // a new requirement is to have one single input stream into
3603  // Importer::importDelimited, so need to move pipe related
3604  // stuff to the outmost block.
3605  int fd[2];
3606  if (pipe(fd) < 0) {
3607  throw std::runtime_error(std::string("failed to create a pipe: ") + strerror(errno));
3608  }
3609  signal(SIGPIPE, SIG_IGN);
3610 
3611  std::exception_ptr teptr;
3612  // create a thread to read uncompressed byte stream out of pipe and
3613  // feed into importDelimited()
3614  ImportStatus ret;
3615  auto th_pipe_reader = std::thread([&]() {
3616  try {
3617  // importDelimited will read from FILE* p_file
3618  if (0 == (p_file = fdopen(fd[0], "r"))) {
3619  throw std::runtime_error(std::string("failed to open a pipe: ") +
3620  strerror(errno));
3621  }
3622 
3623  // in future, depending on data types of this uncompressed stream
3624  // it can be feed into other function such like importParquet, etc
3625  ret = importDelimited(file_path, true);
3626  } catch (...) {
3627  if (!teptr) { // no replace
3628  teptr = std::current_exception();
3629  }
3630  }
3631 
3632  if (p_file) {
3633  fclose(p_file);
3634  }
3635  p_file = 0;
3636  });
3637 
3638  // create a thread to iterate all files (in all archives) and
3639  // forward the uncompressed byte stream to fd[1] which is
3640  // then feed into importDelimited, importParquet, and etc.
3641  auto th_pipe_writer = std::thread([&]() {
3642  std::unique_ptr<S3Archive> us3arch;
3643  bool stop = false;
3644  for (size_t fi = 0; !stop && fi < file_paths.size(); fi++) {
3645  try {
3646  auto file_path = file_paths[fi];
3647  std::unique_ptr<Archive> uarch;
3648  std::map<int, std::string> url_parts;
3649  Archive::parse_url(file_path, url_parts);
3650  const std::string S3_objkey_url_scheme = "s3ok";
3651  if ("file" == url_parts[2] || "" == url_parts[2]) {
3652  uarch.reset(new PosixFileArchive(file_path, copy_params.plain_text));
3653  } else if ("s3" == url_parts[2]) {
3654 #ifdef HAVE_AWS_S3
3655  // new a S3Archive with a shared s3client.
3656  // should be safe b/c no wildcard with s3 url
3657  us3arch.reset(new S3Archive(file_path,
3663  us3arch->init_for_read();
3664  total_file_size += us3arch->get_total_file_size();
3665  // not land all files here but one by one in following iterations
3666  for (const auto& objkey : us3arch->get_objkeys()) {
3667  file_paths.emplace_back(std::string(S3_objkey_url_scheme) + "://" + objkey);
3668  }
3669  continue;
3670 #else
3671  throw std::runtime_error("AWS S3 support not available");
3672 #endif // HAVE_AWS_S3
3673  } else if (S3_objkey_url_scheme == url_parts[2]) {
3674 #ifdef HAVE_AWS_S3
3675  auto objkey = file_path.substr(3 + S3_objkey_url_scheme.size());
3676  auto file_path =
3677  us3arch->land(objkey, teptr, nullptr != dynamic_cast<Detector*>(this));
3678  if (0 == file_path.size()) {
3679  throw std::runtime_error(std::string("failed to land s3 object: ") + objkey);
3680  }
3681  uarch.reset(new PosixFileArchive(file_path, copy_params.plain_text));
3682  // file not removed until file closed
3683  us3arch->vacuum(objkey);
3684 #else
3685  throw std::runtime_error("AWS S3 support not available");
3686 #endif // HAVE_AWS_S3
3687  }
3688 #if 0 // TODO(ppan): implement and enable any other archive class
3689  else
3690  if ("hdfs" == url_parts[2])
3691  uarch.reset(new HdfsArchive(file_path));
3692 #endif
3693  else {
3694  throw std::runtime_error(std::string("unsupported archive url: ") + file_path);
3695  }
3696 
3697  // init the archive for read
3698  auto& arch = *uarch;
3699 
3700  // coming here, the archive of url should be ready to be read, unarchived
3701  // and uncompressed by libarchive into a byte stream (in csv) for the pipe
3702  const void* buf;
3703  size_t size;
3704  bool just_saw_archive_header;
3705  bool is_detecting = nullptr != dynamic_cast<Detector*>(this);
3706  bool first_text_header_skipped = false;
3707  // start reading uncompressed bytes of this archive from libarchive
3708  // note! this archive may contain more than one files!
3709  file_offsets.push_back(0);
3710  while (!stop && !!(just_saw_archive_header = arch.read_next_header())) {
3711  bool insert_line_delim_after_this_file = false;
3712  while (!stop) {
3713  int64_t offset{-1};
3714  auto ok = arch.read_data_block(&buf, &size, &offset);
3715  // can't use (uncompressed) size, so track (max) file offset.
3716  // also we want to capture offset even on e.o.f.
3717  if (offset > 0) {
3718  std::unique_lock<std::mutex> lock(file_offsets_mutex);
3719  file_offsets.back() = offset;
3720  }
3721  if (!ok) {
3722  break;
3723  }
3724  // one subtle point here is now we concatenate all files
3725  // to a single FILE stream with which we call importDelimited
3726  // only once. this would make it misunderstand that only one
3727  // header line is with this 'single' stream, while actually
3728  // we may have one header line for each of the files.
3729  // so we need to skip header lines here instead in importDelimited.
3730  const char* buf2 = (const char*)buf;
3731  int size2 = size;
3733  just_saw_archive_header && (first_text_header_skipped || !is_detecting)) {
3734  while (size2-- > 0) {
3735  if (*buf2++ == copy_params.line_delim) {
3736  break;
3737  }
3738  }
3739  if (size2 <= 0) {
3740  LOG(WARNING) << "No line delimiter in block." << std::endl;
3741  } else {
3742  just_saw_archive_header = false;
3743  first_text_header_skipped = true;
3744  }
3745  }
3746  // In very rare occasions the write pipe somehow operates in a mode similar to
3747  // non-blocking while pipe(fds) should behave like pipe2(fds, 0) which means
3748  // blocking mode. On such a unreliable blocking mode, a possible fix is to
3749  // loop reading till no bytes left, otherwise the annoying `failed to write
3750  // pipe: Success`...
3751  if (size2 > 0) {
3752  int nremaining = size2;
3753  while (nremaining > 0) {
3754  // try to write the entire remainder of the buffer to the pipe
3755  int nwritten = write(fd[1], buf2, nremaining);
3756  // how did we do?
3757  if (nwritten < 0) {
3758  // something bad happened
3759  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
3760  // ignore these, assume nothing written, try again
3761  nwritten = 0;
3762  } else {
3763  // a real error
3764  throw std::runtime_error(
3765  std::string("failed or interrupted write to pipe: ") +
3766  strerror(errno));
3767  }
3768  } else if (nwritten == nremaining) {
3769  // we wrote everything; we're done
3770  break;
3771  }
3772  // only wrote some (or nothing), try again
3773  nremaining -= nwritten;
3774  buf2 += nwritten;
3775  // no exception when too many rejected
3776  // @simon.eves how would this get set? from the other thread? mutex
3777  // needed?
3779  stop = true;
3780  break;
3781  }
3782  }
3783  // check that this file (buf for size) ended with a line delim
3784  if (size > 0) {
3785  const char* plast = static_cast<const char*>(buf) + (size - 1);
3786  insert_line_delim_after_this_file = (*plast != copy_params.line_delim);
3787  }
3788  }
3789  }
3790  // if that file didn't end with a line delim, we insert one here to terminate
3791  // that file's stream use a loop for the same reason as above
3792  if (insert_line_delim_after_this_file) {
3793  while (true) {
3794  // write the delim char to the pipe
3795  int nwritten = write(fd[1], &copy_params.line_delim, 1);
3796  // how did we do?
3797  if (nwritten < 0) {
3798  // something bad happened
3799  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
3800  // ignore these, assume nothing written, try again
3801  nwritten = 0;
3802  } else {
3803  // a real error
3804  throw std::runtime_error(
3805  std::string("failed or interrupted write to pipe: ") +
3806  strerror(errno));
3807  }
3808  } else if (nwritten == 1) {
3809  // we wrote it; we're done
3810  break;
3811  }
3812  }
3813  }
3814  }
3815  } catch (...) {
3816  // when import is aborted because too many data errors or because end of a
3817  // detection, any exception thrown by s3 sdk or libarchive is okay and should be
3818  // suppressed.
3819  mapd_shared_lock<mapd_shared_mutex> read_lock(status_mutex);
3821  break;
3822  }
3823  if (import_status.rows_completed > 0) {
3824  if (nullptr != dynamic_cast<Detector*>(this)) {
3825  break;
3826  }
3827  }
3828  if (!teptr) { // no replace
3829  teptr = std::current_exception();
3830  }
3831  break;
3832  }
3833  }
3834  // close writer end
3835  close(fd[1]);
3836  });
3837 
3838  th_pipe_reader.join();
3839  th_pipe_writer.join();
3840 
3841  // rethrow any exception happened herebefore
3842  if (teptr) {
3843  std::rethrow_exception(teptr);
3844  }
3845 }
std::vector< size_t > file_offsets
Definition: Importer.h:634
#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:635
std::string s3_region
Definition: CopyParams.h:64
ImportStatus import_status
Definition: Importer.h:631
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:629
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 634 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 635 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 629 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: