-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storage: Fix max-id being mis-reused cause data corruption after chan…
- Loading branch information
1 parent
24663e9
commit a20f385
Showing
27 changed files
with
504 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
dbms/src/Storages/DeltaMerge/StoragePool/GlobalPageIdAllocator.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <Common/Logger.h> | ||
#include <Common/SyncPoint/SyncPoint.h> | ||
#include <Storages/DeltaMerge/StoragePool/GlobalPageIdAllocator.h> | ||
#include <Storages/Page/PageDefinesBase.h> | ||
#include <common/logger_useful.h> | ||
|
||
#include <atomic> | ||
|
||
namespace DB::DM | ||
{ | ||
|
||
void GlobalPageIdAllocator::raiseTargetByLowerBound(std::atomic<PageIdU64> & target, PageIdU64 lower_bound) | ||
{ | ||
PageIdU64 old_value = target.load(); | ||
while (true) | ||
{ | ||
// already satisfied the lower_bound, done. | ||
if (old_value >= lower_bound) | ||
break; | ||
SYNC_FOR("before_GlobalPageIdAllocator::raiseLowerBoundCAS_1"); | ||
// try raise to the lower_bound | ||
if (target.compare_exchange_strong(old_value, lower_bound)) | ||
{ | ||
// raise success, done. | ||
break; | ||
} | ||
// else the `old_value` is updated, try again | ||
} | ||
} | ||
|
||
} // namespace DB::DM |
54 changes: 54 additions & 0 deletions
54
dbms/src/Storages/DeltaMerge/StoragePool/GlobalPageIdAllocator.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <Storages/KVStore/Types.h> | ||
#include <Storages/Page/PageDefinesBase.h> | ||
#include <Storages/Page/PageStorage_fwd.h> | ||
|
||
#include <atomic> | ||
|
||
namespace DB::DM | ||
{ | ||
|
||
class GlobalPageIdAllocator : private boost::noncopyable | ||
{ | ||
public: | ||
GlobalPageIdAllocator() = default; | ||
|
||
void raiseDataPageIdLowerBound(PageIdU64 lower_bound) { raiseTargetByLowerBound(max_data_page_id, lower_bound); } | ||
void raiseLogPageIdLowerBound(PageIdU64 lower_bound) { raiseTargetByLowerBound(max_log_page_id, lower_bound); } | ||
void raiseMetaPageIdLowerBound(PageIdU64 lower_bound) { raiseTargetByLowerBound(max_meta_page_id, lower_bound); } | ||
|
||
PageIdU64 newDataPageIdForDTFile() { return ++max_data_page_id; } | ||
PageIdU64 newLogPageId() { return ++max_log_page_id; } | ||
PageIdU64 newMetaPageId() { return ++max_meta_page_id; } | ||
|
||
std::tuple<PageIdU64, PageIdU64, PageIdU64> getCurrentIds() const | ||
{ | ||
return std::make_tuple(max_log_page_id.load(), max_data_page_id.load(), max_meta_page_id.load()); | ||
} | ||
|
||
private: | ||
static void raiseTargetByLowerBound(std::atomic<PageIdU64> & target, PageIdU64 lower_bound); | ||
|
||
private: | ||
std::atomic<PageIdU64> max_log_page_id = 0; | ||
std::atomic<PageIdU64> max_data_page_id = 0; | ||
// The meta_page_id == 1 is reserved for first segment in each physical table | ||
std::atomic<PageIdU64> max_meta_page_id = 1; | ||
}; | ||
|
||
} // namespace DB::DM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.