Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix row key not in standard format (#7901) #7910

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions dbms/src/Storages/DeltaMerge/RowKeyRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <Core/Types.h>
#include <Functions/FunctionHelpers.h>
#include <IO/WriteHelpers.h>
#include <Poco/Logger.h>
#include <Storages/DeltaMerge/DeltaMergeDefines.h>
#include <Storages/DeltaMerge/DeltaMergeHelpers.h>
#include <Storages/Transaction/DatumCodec.h>
Expand All @@ -25,6 +26,7 @@
#include <Storages/Transaction/TiKVKeyValue.h>
#include <Storages/Transaction/TiKVRecordFormat.h>
#include <Storages/Transaction/Types.h>
#include <common/logger_useful.h>

namespace DB::DM
{
Expand Down Expand Up @@ -83,6 +85,46 @@ struct RowKeyValue
{
size_t cursor = 0;
int_value = DB::DecodeInt64(cursor, *value);
if (unlikely(value->size() != sizeof(Int64)))
{
// For int type handle, the standard key enconding format should be t{table_id}_r{handle_value}.
// But TiKV may generate region range keys which are not strictly following the standard format.
// More concretely, the key may be t{table_id}_r{handle_value} + some other bytes.
// We need to adapt the key to the standard format.
// For example, the key may be t100_r1000 + 0x00, we need to adapt it to t100_r1001.
// This is ok, because
// 1) if the key is the start range, then [t100_r1000 + 0x00, xxx) has the same semantics with [t100_r1001, xxx)
// 2) if the key is the end range, then [xxx, t100_r1000 + 0x00) also has the same semantics with [xxx, t100_r1001)
//
// Note if the `int_value` is Int64::max_value,
// it is a value generated by tiflash itself to means +inf()(which is RowKeyValue::INT_HANDLE_MAX_KEY).
// So we can just ignore it.
if (value->size() != sizeof(UInt64) + 1 || value->back() != 0x00)
{
LOG_FMT_WARNING(
&Poco::Logger::get("RowKeyValue"),
"Meet rowkey {} with unexpected encoding format",
Redact::keyToDebugString(value->data(), value->size()));
}
else
{
if (int_value < std::numeric_limits<Int64>::max())
{
LOG_FMT_WARNING(
&Poco::Logger::get("RowKeyValue"),
"Meet rowkey {} which has an extra zero suffix",
Redact::keyToDebugString(value->data(), value->size()));
int_value = int_value + 1;
WriteBufferFromOwnString ss;
DB::EncodeInt64(int_value, ss);
value = std::make_shared<String>(ss.releaseStr());
}
else
{
// ignore RowKeyValue::INT_HANDLE_MAX_KEY
}
}
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions dbms/src/Storages/DeltaMerge/tests/gtest_key_range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ TEST(RowKeyRange_test, RedactRangeFromCommonHandle)
Redact::setRedactLog(false); // restore flags
}

TEST(RowKey, DecodeKeyWithExtraZeroSuffix)
{
// Note: {20,00} will be regarded as Key=21 in RowKeyRange::fromRegionRange.
auto key_end = RecordKVFormat::genRawKey(1, 20);
key_end.push_back(0);
auto tikv_key_end = RecordKVFormat::encodeAsTiKVKey(key_end);
const auto range_keys = std::make_shared<RegionRangeKeys>(
RecordKVFormat::genKey(1, 0),
std::move(tikv_key_end));
const auto range = RowKeyRange::fromRegionRange(
range_keys,
/* table_id */ 1,
/* is_common_handle */ false,
/* row_key_column_size */ 1);
EXPECT_EQ(0, compare(RowKeyValue::fromHandle(21).toRowKeyValueRef(), range.getEnd()));
}
} // namespace tests
} // namespace DM
} // namespace DB