From 5199a17a4b0db6cf380ba7c2b4aa0386fa48a2a3 Mon Sep 17 00:00:00 2001 From: HaiBo Li Date: Mon, 28 Sep 2020 13:35:52 +0800 Subject: [PATCH] [cache][be]Fix the bug of cross-border access cache (#4639) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * When the different partition of the table is updated frequently, the partition key list of the cache is discontinuous, and the partition key in the request cannot hit the key list in the cache, resulting in the access overrun,the BE will crash. * Add some unit test case,add test cases that fail to hit the boundary value of cache --- be/src/runtime/cache/result_node.cpp | 17 +++++---- .../runtime/cache/partition_cache_test.cpp | 36 +++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/be/src/runtime/cache/result_node.cpp b/be/src/runtime/cache/result_node.cpp index 7813458e85..6924baec40 100644 --- a/be/src/runtime/cache/result_node.cpp +++ b/be/src/runtime/cache/result_node.cpp @@ -167,14 +167,19 @@ PCacheStatus ResultNode::fetch_partition(const PFetchCacheRequest* request, request->params(param_idx).partition_key() > (*part_it)->get_partition_key()) { part_it++; } - while (param_idx < request->params_size() && - request->params(param_idx).partition_key() < (*part_it)->get_partition_key()) { - param_idx++; - } - if (request->params(param_idx).partition_key() == (*part_it)->get_partition_key()) { - find = true; + if (part_it != _partition_list.end()) { + while (param_idx < request->params_size() && + request->params(param_idx).partition_key() < (*part_it)->get_partition_key()) { + param_idx++; + } + if (param_idx < request->params_size()) { + if (request->params(param_idx).partition_key() == (*part_it)->get_partition_key()) { + find = true; + } + } } } + if (find) { #ifdef PARTITION_CACHE_DEV LOG(INFO) << "Find! Param index : " << param_idx diff --git a/be/test/runtime/cache/partition_cache_test.cpp b/be/test/runtime/cache/partition_cache_test.cpp index e370a786a7..4d650201fd 100644 --- a/be/test/runtime/cache/partition_cache_test.cpp +++ b/be/test/runtime/cache/partition_cache_test.cpp @@ -192,6 +192,42 @@ TEST_F(PartitionCacheTest, fetch_range_data) { clear(); } +TEST_F(PartitionCacheTest, fetch_invalid_right_range) { + init_default(); + init_batch_data(1, 1, 3); + + set_sql_key(_fetch_request->mutable_sql_key(), 1, 1); + PCacheParam* p1 = _fetch_request->add_params(); + p1->set_partition_key(4); + p1->set_last_version(4); + p1->set_last_version_time(4); + PCacheParam* p2 = _fetch_request->add_params(); + p2->set_partition_key(5); + p2->set_last_version(5); + p2->set_last_version_time(5); + _cache->fetch(_fetch_request, _fetch_result); + + ASSERT_TRUE(_fetch_result->status() == PCacheStatus::NO_PARTITION_KEY); + ASSERT_EQ(_fetch_result->values_size(), 0); + clear(); +} + +TEST_F(PartitionCacheTest, fetch_invalid_left_range) { + init_default(); + init_batch_data(1, 1, 3); + + set_sql_key(_fetch_request->mutable_sql_key(), 1, 1); + PCacheParam* p1 = _fetch_request->add_params(); + p1->set_partition_key(0); + p1->set_last_version(0); + p1->set_last_version_time(0); + _cache->fetch(_fetch_request, _fetch_result); + + ASSERT_TRUE(_fetch_result->status() == PCacheStatus::NO_PARTITION_KEY); + ASSERT_EQ(_fetch_result->values_size(), 0); + clear(); +} + TEST_F(PartitionCacheTest, fetch_invalid_key_range) { init_default(); init_batch_data(1, 2, 1);