Skip to content

Commit

Permalink
fix middle of strings (ydb-platform#5169)
Browse files Browse the repository at this point in the history
  • Loading branch information
nshestakov authored Jun 4, 2024
1 parent 5a274a6 commit c5e7f87
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
34 changes: 29 additions & 5 deletions ydb/core/persqueue/partition_key_range/partition_key_range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace NKikimr {
namespace NPQ {

TString MiddleOf(const TString& from, const TString& to) {
Y_ABORT_UNLESS(to.empty() || from < to);

auto GetChar = [](const TString& str, size_t i, unsigned char defaultValue) {
if (i >= str.size()) {
return defaultValue;
Expand All @@ -24,10 +26,12 @@ TString MiddleOf(const TString& from, const TString& to) {

size_t maxSize = std::max(from.size(), to.size());
result.reserve(maxSize + 1);

size_t size = std::max(from.size(), to.size());
size_t i = 0;
for (; i < maxSize; ++i) {
for (; i < size; ++i) {
ui16 f = GetChar(from, i, 0);
ui16 t = GetChar(to, i, 0xFF);
ui16 t = GetChar(to, i, 0);

if (f != t) {
diffFound = true;
Expand Down Expand Up @@ -76,12 +80,32 @@ TString MiddleOf(const TString& from, const TString& to) {
}
}

if (!splitted) {
for (; i < from.size() && !splitted; ++i) {
ui16 f = GetChar(from, i, 0);
if (f < 0xFF) {
auto n = (f + 0x100u) / 2u;
result << static_cast<unsigned char>(n);
splitted = true;
} else {
result << static_cast<unsigned char>(0xFFu);
}
}

for (; i < to.size() && !splitted; ++i) {
ui16 t = GetChar(to, i, 0);
auto n = t / 2u;
result << static_cast<unsigned char>(n);
splitted = !!n;
}
}

if (result == from) {
size_t j = splitted ? i : maxSize - 1;
size_t j = maxSize - 1;
ui16 f = GetChar(from, j, 0);
ui16 t = GetChar(to, j, 0xFF);
ui16 t = GetChar(to, j, to.empty() ? 0xFF : 0x00);

result << static_cast<unsigned char>((f - t) & 1 ? 0xFFu: 0x7Fu);
result << static_cast<unsigned char>((f - t) & 1 ? 0x7Fu : 0xFFu);
}

return result;
Expand Down
14 changes: 9 additions & 5 deletions ydb/core/persqueue/ut/autoscaling_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ Y_UNIT_TEST_SUITE(TopicAutoscaling) {
.EndConfigurePartitioningSettings();
client.CreateTopic(TEST_TOPIC, createSettings).Wait();

auto msg = TString("a", 1_MB);
auto msg = TString(1_MB, 'a');

auto writeSession = CreateWriteSession(client, "producer-1", 0);
UNIT_ASSERT(writeSession->Write(Msg(msg, 1)));
Expand Down Expand Up @@ -671,7 +671,7 @@ Y_UNIT_TEST_SUITE(TopicAutoscaling) {

{
auto res = NKikimr::NPQ::MiddleOf(AsString({0x01}), AsString({0x02}));
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "01 FF");
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "01 7F");
}

{
Expand All @@ -686,7 +686,7 @@ Y_UNIT_TEST_SUITE(TopicAutoscaling) {

{
auto res = NKikimr::NPQ::MiddleOf(AsString({0x01, 0xFF}), AsString({0x02, 0x00}));
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "01 FF FF");
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "01 FF 7F");
}

{
Expand Down Expand Up @@ -718,8 +718,12 @@ Y_UNIT_TEST_SUITE(TopicAutoscaling) {
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "02 00 00 07");
}
{
auto res = NKikimr::NPQ::MiddleOf(AsString({0xFF, 0xFF}), AsString({0xFF}));
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "FF FF 7F");
auto res = NKikimr::NPQ::MiddleOf(AsString({0xFF}), AsString({0xFF, 0xFF}));
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "FF 7F");
}
{
auto res = NKikimr::NPQ::MiddleOf(AsString({0x99, 0xFF}), AsString({0x9A}));
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "99 FF 7F");
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions ydb/core/tx/schemeshard/schemeshard__operation_alter_pq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,10 @@ class TAlterPQ: public TSubOperation {
bool splitMergeEnabled = AppData()->FeatureFlags.GetEnableTopicSplitMerge();
if (splitMergeEnabled) {

auto Hex = [](const auto& value) {
return HexText(TBasicStringBuf(value));
};

ui32 nextId = topic->NextPartitionId;
ui32 nextGroupId = topic->TotalGroupCount;

Expand Down Expand Up @@ -558,15 +562,16 @@ class TAlterPQ: public TSubOperation {
if (keyRange) {
if (keyRange->FromBound && splitBoundary <= *keyRange->FromBound) {
errStr = TStringBuilder()
<< "Split boundary less or equals FromBound of partition: " << splitBoundary
<< " <= " << *keyRange->FromBound;
<< "Split boundary less or equals FromBound of partition: '" << Hex(splitBoundary)
<< "' <= '" << Hex(*keyRange->FromBound) << "'";
result->SetError(NKikimrScheme::StatusInvalidParameter, errStr);
return result;
}
if (keyRange->ToBound && splitBoundary >= *keyRange->ToBound) {
errStr = TStringBuilder()
<< "Split boundary greate or equals ToBound of partition: " << splitBoundary
<< " >= " << *keyRange->ToBound;
<< "Split boundary greate or equals ToBound of partition: '" << Hex(splitBoundary)
<< "' >= '" << Hex(*keyRange->ToBound)
<< "' (FromBound is '" << Hex(keyRange->FromBound ? *keyRange->FromBound : TString{}) << "')";
result->SetError(NKikimrScheme::StatusInvalidParameter, errStr);
return result;
}
Expand Down

0 comments on commit c5e7f87

Please sign in to comment.