Skip to content

Commit

Permalink
storage: Fix null pointer crash in FileCache (#175)
Browse files Browse the repository at this point in the history
Signed-off-by: Wish <breezewish@outlook.com>
  • Loading branch information
breezewish authored and JaySon-Huang committed Aug 6, 2024
1 parent 5b2725b commit 947945e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions dbms/src/Common/FailPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ namespace DB
M(proactive_flush_force_set_type) \
M(exception_when_fetch_disagg_pages) \
M(cop_send_failure) \
M(file_cache_fg_download_fail) \
M(force_set_parallel_prehandle_threshold) \
M(force_raise_prehandle_exception) \
M(force_agg_on_partial_block) \
Expand Down
30 changes: 28 additions & 2 deletions dbms/src/Storages/DeltaMerge/tests/gtest_dm_vector_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@
#include <Storages/KVStore/KVStore.h>
#include <Storages/PathPool.h>
#include <Storages/S3/FileCache.h>
#include <Storages/S3/FileCachePerf.h>
#include <TestUtils/FunctionTestUtils.h>
#include <TestUtils/InputStreamTestUtils.h>
#include <TestUtils/TiFlashStorageTestBasic.h>
#include <gtest/gtest.h>
#include <tipb/executor.pb.h>

#include <ext/scope_guard.h>
#include <filesystem>

#include "Storages/S3/FileCachePerf.h"

namespace CurrentMetrics
{
extern const Metric DT_SnapshotOfRead;
Expand All @@ -46,6 +46,7 @@ extern const Metric DT_SnapshotOfRead;
namespace DB::FailPoints
{
extern const char force_use_dmfile_format_v3[];
extern const char file_cache_fg_download_fail[];
} // namespace DB::FailPoints

namespace DB::DM::tests
Expand Down Expand Up @@ -1878,5 +1879,30 @@ try
}
CATCH

TEST_F(VectorIndexSegmentOnS3Test, S3Failure)
try
{
prepareWriteNodeStable();
DB::FailPointHelper::enableFailPoint(DB::FailPoints::file_cache_fg_download_fail);
SCOPE_EXIT({ DB::FailPointHelper::disableFailPoint(DB::FailPoints::file_cache_fg_download_fail); });

{
auto * file_cache = FileCache::instance();
ASSERT_EQ(0, file_cache->getAll().size());
}
{
auto scan_context = std::make_shared<ScanContext>();
auto stream = computeNodeANNQuery({5.0}, 1, scan_context);

ASSERT_THROW(
{
stream->readPrefix();
stream->read();
},
DB::Exception);
}
}
CATCH


} // namespace DB::DM::tests
9 changes: 8 additions & 1 deletion dbms/src/Storages/S3/FileCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <Common/CurrentMetrics.h>
#include <Common/Exception.h>
#include <Common/FailPoint.h>
#include <Common/ProfileEvents.h>
#include <Common/Stopwatch.h>
#include <Common/SyncPoint/SyncPoint.h>
Expand Down Expand Up @@ -54,6 +55,11 @@ extern const int S3_ERROR;
extern const int FILE_DOESNT_EXIST;
} // namespace DB::ErrorCodes

namespace DB::FailPoints
{
extern const char file_cache_fg_download_fail[];
} // namespace DB::FailPoints

namespace DB
{
using FileType = FileSegment::FileType;
Expand Down Expand Up @@ -283,7 +289,7 @@ FileSegmentPtr FileCache::getOrWait(const S3::S3FilenameView & s3_fname, const s

PerfContext::file_cache.fg_download_from_s3++;
fgDownload(lock, s3_key, file_seg);
if (!file_seg->isReadyToRead())
if (!file_seg || !file_seg->isReadyToRead())
throw Exception( //
ErrorCodes::S3_ERROR,
"Download object {} failed",
Expand Down Expand Up @@ -679,6 +685,7 @@ void FileCache::fgDownload(std::unique_lock<std::mutex> & cache_lock, const Stri

try
{
FAIL_POINT_TRIGGER_EXCEPTION(FailPoints::file_cache_fg_download_fail);
GET_METRIC(tiflash_storage_remote_cache, type_dtfile_download).Increment();
downloadImpl(s3_key, file_seg);
}
Expand Down

0 comments on commit 947945e

Please sign in to comment.