You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@impala.apache.org by as...@apache.org on 2021/06/24 14:56:17 UTC

[impala] 01/04: IMPALA-10636: Add testcases for abnormal cases of spilling to a remote filesystem

This is an automated email from the ASF dual-hosted git repository.

asherman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git

commit 1d8146869abed84aaee4055ef336c034b6a54f61
Author: Yida Wu <wy...@gmail.com>
AuthorDate: Wed Jun 9 15:37:13 2021 -0700

    IMPALA-10636: Add testcases for abnormal cases of spilling to a remote filesystem
    
    Testcases are added for some abnormal cases of spill to a remote fs,
    such as errors happen during spilling to a remote fs, or configured
    a non-existent remote scratch path.
    
    Added two unit tests in TmpFileMgrTest,
    TestRemoteUploadToNonExistentPath and TestRemoteUploadFailed.
    
    Testing:
    * ran core tests
    * passed new testcases
    
    Change-Id: I600f93d67ba2267c14d179822056a24bedca31ef
    Reviewed-on: http://gerrit.cloudera.org:8080/17572
    Reviewed-by: Impala Public Jenkins <im...@cloudera.com>
    Tested-by: Impala Public Jenkins <im...@cloudera.com>
---
 be/src/runtime/io/disk-file.h          |   2 +
 be/src/runtime/tmp-file-mgr-internal.h |   1 +
 be/src/runtime/tmp-file-mgr-test.cc    | 116 ++++++++++++++++++++++++++++++++-
 be/src/runtime/tmp-file-mgr.cc         |  10 ++-
 4 files changed, 126 insertions(+), 3 deletions(-)

diff --git a/be/src/runtime/io/disk-file.h b/be/src/runtime/io/disk-file.h
index 2febae7..9711242 100644
--- a/be/src/runtime/io/disk-file.h
+++ b/be/src/runtime/io/disk-file.h
@@ -30,6 +30,7 @@
 namespace impala {
 class TmpFileMgr;
 class TmpFileRemote;
+class TmpFileMgrTest;
 namespace io {
 class RemoteOperRange;
 class ScanRange;
@@ -147,6 +148,7 @@ class DiskFile {
   friend class WriteRange;
   friend class impala::TmpFileMgr;
   friend class impala::TmpFileRemote;
+  friend class impala::TmpFileMgrTest;
 
   /// Path of the physical file in the filesystem.
   const std::string path_;
diff --git a/be/src/runtime/tmp-file-mgr-internal.h b/be/src/runtime/tmp-file-mgr-internal.h
index 1df4fe4..1827e4f 100644
--- a/be/src/runtime/tmp-file-mgr-internal.h
+++ b/be/src/runtime/tmp-file-mgr-internal.h
@@ -263,6 +263,7 @@ class TmpFileRemote : public TmpFile {
   friend class TmpFileMgr;
   friend class TmpFileGroup;
   friend class TmpFileBufferPool;
+  friend class TmpFileMgrTest;
 
   /// The default file size of the temporary file, but the actual file size can be a
   /// little over it if the size of the last page written to the file is over the
diff --git a/be/src/runtime/tmp-file-mgr-test.cc b/be/src/runtime/tmp-file-mgr-test.cc
index c191d6d..9aee1a7 100644
--- a/be/src/runtime/tmp-file-mgr-test.cc
+++ b/be/src/runtime/tmp-file-mgr-test.cc
@@ -22,7 +22,10 @@
 #include <numeric>
 
 #include <boost/filesystem.hpp>
+#include <boost/lexical_cast.hpp>
 #include <boost/scoped_ptr.hpp>
+#include <boost/uuid/random_generator.hpp>
+#include <boost/uuid/uuid_io.hpp>
 #include <gtest/gtest.h>
 
 #include "common/init.h"
@@ -44,6 +47,7 @@
 #include "common/names.h"
 
 using boost::filesystem::path;
+using boost::uuids::random_generator;
 
 DECLARE_bool(disk_spill_encryption);
 DECLARE_int64(disk_spill_compression_buffer_limit_bytes);
@@ -54,6 +58,7 @@ DECLARE_int32(stress_scratch_write_delay_ms);
 #endif
 DECLARE_string(remote_tmp_file_size);
 DECLARE_bool(allow_spill_to_hdfs);
+DECLARE_int32(wait_for_spill_buffer_timeout_s);
 
 namespace impala {
 
@@ -193,6 +198,35 @@ class TmpFileMgrTest : public ::testing::Test {
     group->io_ctx_->Cancel();
   }
 
+  /// Helper to set an invalid remote path to create an error.
+  static void SetInvalidRemotePath(TmpFileMgr* tmp_file_mgr, TmpFileGroup* group) {
+    string dir = tmp_file_mgr->tmp_dirs_remote_->path;
+    int dev_id = 0;
+    string invalid_path = "";
+    std::vector<std::shared_ptr<TmpFile>> tmp_files_remote;
+    std::unordered_map<TmpFile*, std::shared_ptr<TmpFile>> tmp_files_remote_ptrs;
+    for (auto tmp_file_shared_ptr : group->tmp_files_remote_) {
+      auto org_tmp_file = static_cast<TmpFileRemote*>(tmp_file_shared_ptr.get());
+      auto local_path = org_tmp_file->local_buffer_path_;
+      TmpFileRemote* tmp_file_r =
+          new TmpFileRemote(group, dev_id, invalid_path, local_path, false, dir.c_str());
+      tmp_file_r->at_capacity_ = org_tmp_file->at_capacity_;
+      tmp_file_r->enqueued_ = org_tmp_file->enqueued_;
+      tmp_file_r->local_buffer_path_ = org_tmp_file->local_buffer_path_;
+      tmp_file_r->file_size_ = org_tmp_file->file_size_;
+      tmp_file_r->allocation_offset_ = org_tmp_file->allocation_offset_;
+      tmp_file_r->disk_buffer_file_.swap(org_tmp_file->disk_buffer_file_);
+      // Set buffer returned to avoid DCHECK failure while destruction.
+      org_tmp_file->buffer_returned_ = true;
+      shared_ptr<TmpFile> tmp_file_remote(move(tmp_file_r));
+      tmp_files_remote.emplace_back(move(tmp_file_remote));
+      tmp_files_remote_ptrs.emplace(
+          tmp_files_remote.back().get(), tmp_files_remote.back());
+    }
+    group->tmp_files_remote_.swap(tmp_files_remote);
+    group->tmp_files_remote_ptrs_.swap(tmp_files_remote_ptrs);
+  }
+
   static void SetWriteRangeContext(WriteRange* write_range, TmpFileGroup* group) {
     write_range->SetRequestContext(group->io_ctx_.get());
   }
@@ -1688,7 +1722,7 @@ void TmpFileMgrTest::TestTmpFileBufferPoolHelper(TmpFileMgr& tmp_file_mgr,
   write_range->SetDiskFile(file3->GetWriteFile());
   SetWriteRangeContext(write_range.get(), file_group);
   // The space hasn't been reserved for file3 because the pool size is 2K, and there are
-  // two files in use, we use the AsyncWriteRange() function to wait untial a file is
+  // two files in use, we use the AsyncWriteRange() function to wait until a file is
   // available to be evicted, then finish the writing of file3.
   EXPECT_FALSE(file3->GetWriteFile()->IsSpaceReserved());
   ASSERT_OK(tmp_file_mgr.AsyncWriteRange(write_range.get(), file3));
@@ -1818,4 +1852,84 @@ TEST_F(TmpFileMgrTest, TestRemoteRemoveBuffer) {
   file_group.Close();
   test_env_->TearDownQueries();
 }
+
+/// Test config a non-existent remote path.
+TEST_F(TmpFileMgrTest, TestRemoteUploadToNonExistentPath) {
+  TmpFileMgr tmp_file_mgr;
+  vector<string> tmp_dirs{{LOCAL_BUFFER_PATH}};
+  RemoveAndCreateDirs(tmp_dirs);
+  // Create a non-existent remote path.
+  string random_name = lexical_cast<string>(random_generator()());
+  string remote_file_path_to_crt = "s3a://non-existent_" + random_name + "/tmp/test";
+  tmp_dirs.push_back(remote_file_path_to_crt);
+
+  ASSERT_OK(tmp_file_mgr.InitCustom(tmp_dirs, true, "", false, metrics_.get()));
+  TUniqueId id;
+  TmpFileGroup file_group(&tmp_file_mgr, io_mgr(), profile_, id);
+  string data = "arbitrary data";
+  MemRange data_mem_range(reinterpret_cast<uint8_t*>(&data[0]), data.size());
+  unique_ptr<TmpWriteHandle> handle;
+  WriteRange::WriteDoneCallback callback = [](const Status& status) {};
+  Status status = file_group.Write(data_mem_range, callback, &handle);
+  // Failed to create the connection to a non-existent remote path.
+  EXPECT_FALSE(status.ok());
+  file_group.Close();
+  test_env_->TearDownQueries();
+}
+
+/// Test upload failure and lead to query cancelled due to timeout.
+TEST_F(TmpFileMgrTest, TestRemoteUploadFailed) {
+  TmpFileMgr tmp_file_mgr;
+  int64_t buffer_limit = 2048;
+  int64_t alloc_size = 512;
+  // Set the timeout value to the minimum, 1 second. Because otherwise, the testcase
+  // needs to wait for the default timeout interval, which is much larger than 1, before
+  // the testcase can end and exit.
+  FLAGS_wait_for_spill_buffer_timeout_s = 1;
+  // The file size is the same as file_size, so each write uses one file.
+  FLAGS_remote_tmp_file_size = "1KB";
+  vector<string> tmp_create_dirs{{LOCAL_BUFFER_PATH}};
+  vector<string> tmp_dirs{{Substitute(LOCAL_BUFFER_PATH + ":$0", buffer_limit)}};
+  RemoveAndCreateDirs(tmp_create_dirs);
+  tmp_dirs.push_back(HDFS_LOCAL_URL);
+
+  ASSERT_OK(tmp_file_mgr.InitCustom(tmp_dirs, true, "", false, metrics_.get()));
+  TUniqueId id;
+  TmpFileGroup file_group(&tmp_file_mgr, io_mgr(), profile_, id);
+  uint8_t* data = (uint8_t*)malloc(alloc_size);
+  ASSERT_TRUE(data != nullptr);
+  MemRange data_mem_range(data, alloc_size);
+
+  int write_times = buffer_limit / alloc_size;
+
+  // We set invalid remote paths to the tmp files, so that the upload of
+  // the tmp files would fail. Because all of the uploads fail, the query
+  // should be cancelled in the end due to timeout (the local buffer is full,
+  // the later file waits for the buffer till timeout happens).
+  for (int i = 0; i < write_times + 1; i++) {
+    // Set the remote paths to invalid paths to create upload failures.
+    if (i != write_times) SetInvalidRemotePath(&tmp_file_mgr, &file_group);
+    unique_ptr<TmpWriteHandle> handle;
+    WriteRange::WriteDoneCallback callback = [this, i, write_times](
+                                                 const Status& status) {
+      // The first several pages have been successfully written because the local
+      // buffer isn't full. However, because all of the uploads fail, the existing
+      // local buffer can't be evicted and would be full eventually. In the end, the
+      // query is cancelled because of the timeout.
+      if (i < write_times) {
+        EXPECT_OK(status);
+      } else {
+        EXPECT_EQ(status.code(), TErrorCode::CANCELLED_INTERNALLY);
+      }
+      SignalCallback(status);
+    };
+    EXPECT_OK(file_group.Write(data_mem_range, callback, &handle));
+    WaitForWrite(handle.get());
+    WaitForCallbacks(1);
+  }
+
+  file_group.Close();
+  test_env_->TearDownQueries();
+}
+
 } // namespace impala
diff --git a/be/src/runtime/tmp-file-mgr.cc b/be/src/runtime/tmp-file-mgr.cc
index 91c3b07..7c8fcfd 100644
--- a/be/src/runtime/tmp-file-mgr.cc
+++ b/be/src/runtime/tmp-file-mgr.cc
@@ -521,8 +521,14 @@ void TmpFileMgr::RemoveRemoteDir(TmpFileGroup* file_group, DeviceId device_id) {
   hdfsFS hdfs_conn;
   Status status =
       HdfsFsCache::instance()->GetConnection(files_dir.str(), &hdfs_conn, &hdfs_conns_);
-  DCHECK(hdfs_conn != nullptr);
-  hdfsDelete(hdfs_conn, files_dir.str().c_str(), 1);
+  if (status.ok()) {
+    DCHECK(hdfs_conn != nullptr);
+    hdfsDelete(hdfs_conn, files_dir.str().c_str(), 1);
+  } else {
+    LOG(WARNING) << "Failed to remove the remote directory because unable to create a "
+                    "connection to "
+                 << files_dir.str();
+  }
 }
 
 Status TmpFileMgr::AsyncWriteRange(WriteRange* write_range, TmpFile* tmp_file) {