Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dbms/src/Storages/Page/V3/PageEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ struct fmt::formatter<DB::PS::V3::PageEntryV3>

return fmt::format_to(
ctx.out(),
"PageEntry{{file: {}, offset: 0x{:X}, size: {}, checksum: 0x{:X}, tag: {}, field_offsets: [{}], "
"PageEntry{{file: {}, offset: {}, size: {}, checksum: 0x{:X}, tag: {}, field_offsets: [{}], "
"checkpoint_info: {}}}",
entry.file_id,
entry.offset,
Expand Down
119 changes: 88 additions & 31 deletions dbms/src/Storages/Page/tools/PageCtl/PageStorageCtlV3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
// limitations under the License.

#include <IO/Encryption/MockKeyManager.h>
#include <IO/FileProvider/FileProvider.h>
#include <Interpreters/Context.h>
#include <Poco/ConsoleChannel.h>
#include <Poco/PatternFormatter.h>
#include <Server/CLIService.h>
#include <Storages/Page/PageDefinesBase.h>
#include <Storages/Page/V3/PageDefines.h>
#include <Storages/Page/V3/PageDirectory.h>
#include <Storages/Page/V3/PageDirectoryFactory.h>
#include <Storages/Page/V3/PageStorageImpl.h>
#include <Storages/Page/V3/Universal/RaftDataReader.h>
#include <Storages/Page/V3/Universal/UniversalPageId.h>
#include <Storages/Page/V3/Universal/UniversalPageIdFormatImpl.h>
#include <Storages/Page/V3/Universal/UniversalPageStorage.h>
#include <Storages/PathPool.h>
Expand All @@ -29,6 +33,7 @@
#include <common/types.h>

#include <boost/program_options.hpp>
#include <cstdint>
#include <magic_enum.hpp>
#include <unordered_set>

Expand All @@ -47,12 +52,15 @@ struct ControlOptions
CHECK_ALL_DATA_CRC = 4,
DISPLAY_WAL_ENTRIES = 5,
DISPLAY_REGION_INFO = 6,
DISPLAY_BLOB_DATA = 7,
};

std::vector<std::string> paths;
DisplayType mode = DisplayType::DISPLAY_SUMMARY_INFO;
UInt64 page_id = UINT64_MAX;
UInt32 blob_id = UINT32_MAX;
BlobFileId blob_id = INVALID_BLOBFILE_ID;
BlobFileOffset blob_offset = INVALID_BLOBFILE_OFFSET;
size_t blob_size = UINT64_MAX;
UInt64 namespace_id = DB::TEST_NAMESPACE_ID;
StorageType storage_type = StorageType::Unknown; // only useful for universal page storage
UInt32 keyspace_id = NullspaceID; // only useful for universal page storage
Expand Down Expand Up @@ -85,6 +93,7 @@ ControlOptions ControlOptions::parse(int argc, char ** argv)
4 is check every data is valid
5 is dump entries in WAL log files
6 is display all region info
7 is display blob data (in hex)
)") //
("show_entries",
value<bool>()->default_value(true),
Expand All @@ -106,8 +115,14 @@ ControlOptions ControlOptions::parse(int argc, char ** argv)
value<UInt64>()->default_value(UINT64_MAX),
"Query a single Page id, and print its version chain.") //
("blob_id,B",
value<UInt32>()->default_value(UINT32_MAX),
"Query a single Blob id, and print its data distribution.") //
value<BlobFileId>()->default_value(INVALID_BLOBFILE_ID),
"Specify the blob_id") //
("blob_offset",
value<BlobFileOffset>()->default_value(INVALID_BLOBFILE_OFFSET),
"Specify the offset.") //
("blob_size",
value<size_t>()->default_value(0),
"Specify the size.") //
//
("imitative,I",
value<bool>()->default_value(true),
Expand Down Expand Up @@ -140,7 +155,9 @@ ControlOptions ControlOptions::parse(int argc, char ** argv)
opt.paths = options["paths"].as<std::vector<std::string>>();
auto mode_int = options["mode"].as<int>();
opt.page_id = options["page_id"].as<UInt64>();
opt.blob_id = options["blob_id"].as<UInt32>();
opt.blob_id = options["blob_id"].as<BlobFileId>();
opt.blob_offset = options["blob_offset"].as<BlobFileOffset>();
opt.blob_size = options["blob_size"].as<size_t>();
opt.show_entries = options["show_entries"].as<bool>();
opt.check_fields = options["check_fields"].as<bool>();
auto storage_type_int = options["storage_type"].as<int>();
Expand Down Expand Up @@ -251,29 +268,36 @@ class PageStorageControlV3
}

private:
static int getPageStorageV3Info(Context & context, const ControlOptions & options)
static DB::PSDiskDelegatorPtr getDelegator(const ControlOptions & opts)
{
DB::PSDiskDelegatorPtr delegator;
if (options.paths.size() == 1)
if (opts.paths.size() == 1)
{
delegator = std::make_shared<DB::tests::MockDiskDelegatorSingle>(options.paths[0]);
return std::make_shared<DB::tests::MockDiskDelegatorSingle>(opts.paths[0]);
}
else
{
delegator = std::make_shared<DB::tests::MockDiskDelegatorMulti>(options.paths);
return std::make_shared<DB::tests::MockDiskDelegatorMulti>(opts.paths);
}
}

FileProviderPtr provider;
if (options.is_imitative)
static FileProviderPtr getProvider(Context & context, const ControlOptions & opts)
{
if (opts.is_imitative)
{
auto key_manager = std::make_shared<DB::MockKeyManager>(false);
provider = std::make_shared<DB::FileProvider>(key_manager, false);
return std::make_shared<DB::FileProvider>(key_manager, false);
}
else
{
provider = context.getFileProvider();
return context.getFileProvider();
}
}

static int getPageStorageV3Info(Context & context, const ControlOptions & options)
{
DB::PSDiskDelegatorPtr delegator = getDelegator(options);

FileProviderPtr provider = getProvider(context, options);

constexpr static std::string_view NAME = "PageStorageControlV3";
PageStorageConfig config;
Expand All @@ -294,13 +318,13 @@ class PageStorageControlV3
{
case ControlOptions::DisplayType::DISPLAY_SUMMARY_INFO:
{
std::cout << getSummaryInfo(mvcc_table_directory, blob_store) << std::endl;
fmt::println("{}", getSummaryInfo(mvcc_table_directory, blob_store));
break;
}
case ControlOptions::DisplayType::DISPLAY_DIRECTORY_INFO:
{
fmt::print(
"{}\n",
fmt::println(
"{}",
getDirectoryInfo(
mvcc_table_directory,
opts.show_entries,
Expand All @@ -312,40 +336,47 @@ class PageStorageControlV3
}
case ControlOptions::DisplayType::DISPLAY_BLOBS_INFO:
{
std::cout << getBlobsInfo(blob_store, opts.blob_id) << std::endl;
fmt::println("{}", getBlobsInfo(blob_store, opts.blob_id));
break;
}
case ControlOptions::DisplayType::CHECK_ALL_DATA_CRC:
{
if (opts.page_id != UINT64_MAX)
{
std::cout << checkSinglePage(
mvcc_table_directory,
blob_store,
opts.storage_type,
opts.keyspace_id,
opts.namespace_id,
opts.page_id)
<< std::endl;
fmt::println(
"{}",
checkSinglePage(
mvcc_table_directory,
blob_store,
opts.storage_type,
opts.keyspace_id,
opts.namespace_id,
opts.page_id));
}
else
{
std::cout << checkAllDataCrc(mvcc_table_directory, blob_store, opts.check_fields) << std::endl;
fmt::println("{}", checkAllDataCrc(mvcc_table_directory, blob_store, opts.check_fields));
}
break;
}
case ControlOptions::DisplayType::DISPLAY_REGION_INFO:
{
if constexpr (std::is_same_v<Trait, universal::PageStorageControlV3Trait>)
{
std::cout << getAllRegionInfo(mvcc_table_directory) << std::endl;
fmt::println("{}", getAllRegionInfo(mvcc_table_directory));
}
else
{
std::cout << "Only UniversalPageStorage support this mode." << std::endl;
fmt::println("Only UniversalPageStorage support this mode.");
}
break;
}
case ControlOptions::DisplayType::DISPLAY_BLOB_DATA:
{
String hex_data = getBlobData(blob_store, opts.blob_id, opts.blob_offset, opts.blob_size);
fmt::println("hex:{}", hex_data);
break;
}
default:
std::cout << "Invalid display mode." << std::endl;
break;
Expand All @@ -372,7 +403,7 @@ class PageStorageControlV3
return 0;
}

static String getBlobsInfo(typename Trait::BlobStore & blob_store, UInt32 blob_id)
static String getBlobsInfo(typename Trait::BlobStore & blob_store, BlobFileId blob_id)
{
auto stat_info = [](const BlobStats::BlobStatPtr & stat, const String & path) {
FmtBuffer stat_str;
Expand Down Expand Up @@ -402,7 +433,7 @@ class PageStorageControlV3
{
for (const auto & stat : stats)
{
if (blob_id != UINT32_MAX)
if (blob_id != INVALID_BLOBFILE_ID)
{
if (stat->id == blob_id)
{
Expand All @@ -416,7 +447,7 @@ class PageStorageControlV3
}
}

if (blob_id != UINT32_MAX)
if (blob_id != INVALID_BLOBFILE_ID)
{
stats_info.fmtAppend(" no found blob {}", blob_id);
}
Expand Down Expand Up @@ -821,6 +852,32 @@ class PageStorageControlV3
return error_msg.toString();
}

static String getBlobData(
typename Trait::BlobStore & blob_store,
BlobFileId blob_id,
BlobFileOffset offset,
size_t size)
{
auto page_id = []() {
if constexpr (std::is_same_v<Trait, u128::PageStorageControlV3Trait>)
return PageIdV3Internal(0, 0);
else
return UniversalPageId("");
}();
char * buffer = new char[size];
blob_store.read(page_id, blob_id, offset, buffer, size, nullptr, false);

using ChecksumClass = Digest::CRC64;
ChecksumClass digest;
digest.update(buffer, size);
auto checksum = digest.checksum();
fmt::print("checksum: 0x{:X}\n", checksum);

auto hex_str = Redact::keyToHexString(buffer, size);
delete[] buffer;
return hex_str;
}

private:
ControlOptions options;
};
Expand Down