From 2fbc59a34830734baa111704b3fa2296b09f21d5 Mon Sep 17 00:00:00 2001 From: Islam AbdelRahman Date: Mon, 25 Jan 2016 13:47:07 -0800 Subject: [PATCH] Disallow SstFileWriter from creating empty sst files Summary: SstFileWriter may create an sst file with no entries Right now this will fail when being ingested using DB::AddFile() saying that the keys are corrupted Test Plan: make check Reviewers: yhchiang, rven, anthony, sdong Reviewed By: sdong Subscribers: dhruba Differential Revision: https://reviews.facebook.net/D52815 --- db/db_impl.cc | 3 +++ db/db_test.cc | 6 ++++++ table/sst_file_writer.cc | 3 +++ 3 files changed, 12 insertions(+) diff --git a/db/db_impl.cc b/db/db_impl.cc index 8f9c0168e..dcc90e31c 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -3502,6 +3502,9 @@ Status DBImpl::AddFile(ColumnFamilyHandle* column_family, auto cfh = reinterpret_cast(column_family); ColumnFamilyData* cfd = cfh->cfd(); + if (file_info->num_entries == 0) { + return Status::InvalidArgument("File contain no entries"); + } if (file_info->version != 1) { return Status::InvalidArgument("Generated table version is not supported"); } diff --git a/db/db_test.cc b/db/db_test.cc index 1143eef1b..6bef10ca9 100644 --- a/db/db_test.cc +++ b/db/db_test.cc @@ -8801,6 +8801,12 @@ TEST_F(DBTest, AddExternalSstFile) { ASSERT_EQ(file5_info.smallest_key, Key(400)); ASSERT_EQ(file5_info.largest_key, Key(499)); + // Cannot create an empty sst file + std::string file_empty = sst_files_folder + "file_empty.sst"; + ExternalSstFileInfo file_empty_info; + s = sst_file_writer.Finish(&file_empty_info); + ASSERT_NOK(s); + DestroyAndReopen(options); // Add file using file path s = db_->AddFile(file1); diff --git a/table/sst_file_writer.cc b/table/sst_file_writer.cc index 1c21a25f7..d13adbe08 100644 --- a/table/sst_file_writer.cc +++ b/table/sst_file_writer.cc @@ -163,6 +163,9 @@ Status SstFileWriter::Finish(ExternalSstFileInfo* file_info) { if (!r->builder) { return Status::InvalidArgument("File is not opened"); } + if (r->file_info.num_entries == 0) { + return Status::InvalidArgument("Cannot create sst file with no entries"); + } Status s = r->builder->Finish(); if (s.ok()) {