You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by to...@apache.org on 2016/09/07 00:22:02 UTC

[3/8] kudu git commit: KUDU-668. log block manager should tolerate empty metadata

KUDU-668. log block manager should tolerate empty metadata

This solves a commonly seen issue after a server crashes due to "Too
many open files". In many cases, this type of crash would leave an empty
metadata file and a missing data file.

This isn't a full fix for KUDU-668, but handles the above common case by
just checking the size and existence of these files. If the metadata is
too small to be valid, and the data is empty, then we just remove the
container and continue with startup.

Change-Id: Ie4fd9d0f510835d100d35efaf541d34829118f56
Reviewed-on: http://gerrit.cloudera.org:8080/4178
Reviewed-by: Adar Dembo <ad...@cloudera.com>
Tested-by: Kudu Jenkins


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/3dcd7de2
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/3dcd7de2
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/3dcd7de2

Branch: refs/heads/master
Commit: 3dcd7de20e50373b2c08c4afca73a95b9e523b40
Parents: 15671a1
Author: Todd Lipcon <to...@apache.org>
Authored: Tue Aug 30 18:53:09 2016 -0700
Committer: Todd Lipcon <to...@apache.org>
Committed: Tue Sep 6 21:28:47 2016 +0000

----------------------------------------------------------------------
 src/kudu/fs/block_manager-test.cc | 21 ++++++++++---
 src/kudu/fs/log_block_manager.cc  | 55 +++++++++++++++++++++++++++++-----
 src/kudu/util/pb_util.cc          |  2 ++
 src/kudu/util/pb_util.h           |  3 ++
 4 files changed, 69 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/3dcd7de2/src/kudu/fs/block_manager-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/fs/block_manager-test.cc b/src/kudu/fs/block_manager-test.cc
index f3ad6e6..47fba51 100644
--- a/src/kudu/fs/block_manager-test.cc
+++ b/src/kudu/fs/block_manager-test.cc
@@ -822,6 +822,7 @@ TEST_F(LogBlockManagerTest, TestMetadataTruncation) {
 
   string path = LogBlockManager::ContainerPathForTests(bm_->available_containers_.front());
   string metadata_path = path + LogBlockManager::kContainerMetadataFileSuffix;
+  string data_path = path + LogBlockManager::kContainerDataFileSuffix;
 
   uint64_t good_meta_size;
   ASSERT_OK(env_->GetFileSize(metadata_path, &good_meta_size));
@@ -950,10 +951,10 @@ TEST_F(LogBlockManagerTest, TestMetadataTruncation) {
   // unsigned integer. This will cause the length field to represent a large
   // value and also cause the length checksum not to validate.
   data[offset + 3] ^= 1 << 7;
-  gscoped_ptr<WritableFile> writable_meta;
-  ASSERT_OK(env_->NewWritableFile(metadata_path, &writable_meta));
-  ASSERT_OK(writable_meta->Append(data));
-  ASSERT_OK(writable_meta->Close());
+  gscoped_ptr<WritableFile> writable_file;
+  ASSERT_OK(env_->NewWritableFile(metadata_path, &writable_file));
+  ASSERT_OK(writable_file->Append(data));
+  ASSERT_OK(writable_file->Close());
 
   // Now try to reopen the container.
   // This should look like a bad checksum, and it's not recoverable.
@@ -963,6 +964,18 @@ TEST_F(LogBlockManagerTest, TestMetadataTruncation) {
                                false);
   ASSERT_TRUE(s.IsCorruption());
   ASSERT_STR_CONTAINS(s.ToString(), "Incorrect checksum");
+
+  // Now truncate both the data and metadata files.
+  // This should be recoverable. See KUDU-668.
+  ASSERT_OK(env_->NewWritableFile(metadata_path, &writable_file));
+  ASSERT_OK(writable_file->Close());
+  ASSERT_OK(env_->NewWritableFile(data_path, &writable_file));
+  ASSERT_OK(writable_file->Close());
+
+  ASSERT_OK(this->ReopenBlockManager(scoped_refptr<MetricEntity>(),
+                                     shared_ptr<MemTracker>(),
+                                     { GetTestDataDirectory() },
+                                     false));
 }
 
 TEST_F(LogBlockManagerTest, TestDiskSpaceCheck) {

http://git-wip-us.apache.org/repos/asf/kudu/blob/3dcd7de2/src/kudu/fs/log_block_manager.cc
----------------------------------------------------------------------
diff --git a/src/kudu/fs/log_block_manager.cc b/src/kudu/fs/log_block_manager.cc
index 28b582f..594c756 100644
--- a/src/kudu/fs/log_block_manager.cc
+++ b/src/kudu/fs/log_block_manager.cc
@@ -178,6 +178,10 @@ class LogBlockContainer {
   //
   // Every container is comprised of two files: "<dir>/<id>.data" and
   // "<dir>/<id>.metadata". Together, 'dir' and 'id' fully describe both files.
+  //
+  // Returns Status::Aborted() in the case that the metadata and data files
+  // both appear to have no data (e.g. due to a crash just after creating
+  // one of them but before writing any records).
   static Status Open(LogBlockManager* block_manager,
                      PathInstanceMetadataPB* instance,
                      const std::string& dir,
@@ -395,28 +399,59 @@ Status LogBlockContainer::Open(LogBlockManager* block_manager,
                                PathInstanceMetadataPB* instance,
                                const string& root_path, const string& id,
                                gscoped_ptr<LogBlockContainer>* container) {
+  Env* env = block_manager->env();
   string common_path = JoinPathSegments(root_path, id);
+  string metadata_path = StrCat(common_path, LogBlockManager::kContainerMetadataFileSuffix);
+  string data_path = StrCat(common_path, LogBlockManager::kContainerDataFileSuffix);
+
+  // Check that both the metadata and data files exist and have valid lengths.
+  // This covers a commonly seen case at startup, where the previous incarnation
+  // of the server crashed due to "too many open files" just as it was trying
+  // to create a data file. This orphans an empty metadata file, which we can
+  // safely delete.
+  {
+    uint64_t metadata_size = 0;
+    uint64_t data_size = 0;
+    Status s = env->GetFileSize(metadata_path, &metadata_size);
+    if (s.IsNotFound()) {
+      LOG(WARNING) << "Missing metadata path at " << metadata_path;
+    } else {
+      RETURN_NOT_OK_PREPEND(s, "unable to determine metadata file size");
+    }
+    s = env->GetFileSize(data_path, &data_size);
+    if (s.IsNotFound()) {
+      LOG(WARNING) << "Missing data path at " << data_path;
+    } else {
+      RETURN_NOT_OK_PREPEND(s, "unable to determine data file size");
+    }
+
+    if (metadata_size < pb_util::kPBContainerMinimumValidLength && data_size == 0) {
+      LOG(WARNING) << "Data and metadata files for container  " << common_path
+                   << " are both missing or empty. Deleting container.";
+      IgnoreResult(env->DeleteFile(metadata_path));
+      IgnoreResult(env->DeleteFile(data_path));
+      return Status::Aborted("orphaned empty metadata and data files removed");
+    }
+  }
 
   // Open the existing metadata and data files for writing.
-  string metadata_path = StrCat(common_path, LogBlockManager::kContainerMetadataFileSuffix);
   gscoped_ptr<RWFile> metadata_writer;
   RWFileOptions wr_opts;
   wr_opts.mode = Env::OPEN_EXISTING;
 
-  RETURN_NOT_OK(block_manager->env()->NewRWFile(wr_opts,
-                                                metadata_path,
-                                                &metadata_writer));
+  RETURN_NOT_OK(env->NewRWFile(wr_opts,
+                               metadata_path,
+                               &metadata_writer));
   gscoped_ptr<WritablePBContainerFile> metadata_pb_writer(
       new WritablePBContainerFile(std::move(metadata_writer)));
   RETURN_NOT_OK(metadata_pb_writer->Reopen());
 
-  string data_path = StrCat(common_path, LogBlockManager::kContainerDataFileSuffix);
   gscoped_ptr<RWFile> data_file;
   RWFileOptions rw_opts;
   rw_opts.mode = Env::OPEN_EXISTING;
-  RETURN_NOT_OK(block_manager->env()->NewRWFile(rw_opts,
-                                                data_path,
-                                                &data_file));
+  RETURN_NOT_OK(env->NewRWFile(rw_opts,
+                               data_path,
+                               &data_file));
 
   // Create the in-memory container and populate it.
   gscoped_ptr<LogBlockContainer> open_container(new LogBlockContainer(block_manager,
@@ -1603,6 +1638,10 @@ void LogBlockManager::OpenRootPath(const string& root_path,
     gscoped_ptr<LogBlockContainer> container;
     s = LogBlockContainer::Open(this, metadata->metadata(),
                                 root_path, id, &container);
+    if (s.IsAborted()) {
+      // Skip the container. Open() already handled logging for us.
+      continue;
+    }
     if (!s.ok()) {
       *result_status = s.CloneAndPrepend(Substitute(
           "Could not open container $0", id));

http://git-wip-us.apache.org/repos/asf/kudu/blob/3dcd7de2/src/kudu/util/pb_util.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/pb_util.cc b/src/kudu/util/pb_util.cc
index cc4ae86..6df4c84 100644
--- a/src/kudu/util/pb_util.cc
+++ b/src/kudu/util/pb_util.cc
@@ -114,6 +114,8 @@ static const int kPBContainerV1HeaderLen =
 static const int kPBContainerV2HeaderLen =
     kPBContainerV1HeaderLen + kPBContainerChecksumLen; // Same as V1 plus a checksum.
 
+const int kPBContainerMinimumValidLength = kPBContainerV1HeaderLen;
+
 static_assert(arraysize(kPBContainerMagic) - 1 == kPBContainerMagicLen,
               "kPBContainerMagic does not match expected length");
 

http://git-wip-us.apache.org/repos/asf/kudu/blob/3dcd7de2/src/kudu/util/pb_util.h
----------------------------------------------------------------------
diff --git a/src/kudu/util/pb_util.h b/src/kudu/util/pb_util.h
index 64b8a86..e59a930 100644
--- a/src/kudu/util/pb_util.h
+++ b/src/kudu/util/pb_util.h
@@ -67,6 +67,9 @@ enum class FileState {
   CLOSED
 };
 
+// The minimum valid length of a PBC file.
+extern const int kPBContainerMinimumValidLength;
+
 // See MessageLite::AppendToString
 bool AppendToString(const MessageLite &msg, faststring *output);