ClickHouse/programs/keeper-data-dumper/KeeperDataDumper.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

108 lines
3.8 KiB
C++
Raw Normal View History

2024-05-12 16:22:09 +00:00
// NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange)
2021-03-16 11:44:43 +00:00
#include <Poco/ConsoleChannel.h>
#include <Poco/Logger.h>
2024-07-12 09:34:20 +00:00
#include <Coordination/CoordinationSettings.h>
2021-03-29 09:16:58 +00:00
#include <Coordination/KeeperStateMachine.h>
2021-03-16 11:44:43 +00:00
#include <Common/ZooKeeper/ZooKeeperCommon.h>
#include <Common/ZooKeeper/ZooKeeperIO.h>
#include <Common/Exception.h>
2021-11-02 11:41:30 +00:00
#include <libnuraft/nuraft.hxx>
2021-03-29 09:16:58 +00:00
#include <Coordination/KeeperLogStore.h>
2021-03-16 11:44:43 +00:00
#include <Coordination/Changelog.h>
2022-04-27 15:05:45 +00:00
#include <Common/logger_useful.h>
2023-05-24 09:46:07 +00:00
#include <Disks/DiskLocal.h>
2021-03-16 11:44:43 +00:00
using namespace Coordination;
using namespace DB;
2024-10-16 12:38:46 +00:00
namespace DB::CoordinationSetting
{
extern const CoordinationSettingsBool compress_logs;
}
2023-11-13 12:53:22 +00:00
void dumpMachine(std::shared_ptr<KeeperStateMachine<DB::KeeperMemoryStorage>> machine)
2021-03-16 11:44:43 +00:00
{
2023-07-05 18:23:34 +00:00
auto & storage = machine->getStorageUnsafe();
2021-03-16 11:44:43 +00:00
std::queue<std::string> keys;
keys.push("/");
while (!keys.empty())
{
auto key = keys.front();
keys.pop();
std::cout << key << "\n";
2021-03-16 12:37:46 +00:00
auto value = storage.container.getValue(key);
2024-09-03 08:07:01 +00:00
std::cout << "\tStat: {version: " << value.stats.version <<
", mtime: " << value.stats.mtime <<
", emphemeralOwner: " << value.stats.ephemeralOwner() <<
", czxid: " << value.stats.czxid <<
", mzxid: " << value.stats.mzxid <<
", numChildren: " << value.stats.numChildren() <<
", dataLength: " << value.stats.data_size <<
2021-03-16 11:44:43 +00:00
"}" << std::endl;
2022-04-06 07:43:48 +00:00
std::cout << "\tData: " << storage.container.getValue(key).getData() << std::endl;
2021-03-16 11:44:43 +00:00
2022-04-06 07:43:48 +00:00
for (const auto & child : value.getChildren())
2021-03-16 11:44:43 +00:00
{
if (key == "/")
2022-01-24 10:23:58 +00:00
keys.push(key + child.toString());
2021-03-16 11:44:43 +00:00
else
2022-01-24 10:23:58 +00:00
keys.push(key + "/" + child.toString());
2021-03-16 11:44:43 +00:00
}
}
std::cout << std::flush;
}
2024-11-26 14:57:42 +00:00
int mainEntryClickHouseKeeperDataDumper(int argc, char ** argv)
2021-03-16 11:44:43 +00:00
{
if (argc != 3)
{
std::cerr << "usage: " << argv[0] << " snapshotpath logpath" << std::endl;
return 3;
}
2024-09-19 11:51:02 +00:00
2021-03-16 11:44:43 +00:00
Poco::AutoPtr<Poco::ConsoleChannel> channel(new Poco::ConsoleChannel(std::cerr));
Poco::Logger::root().setChannel(channel);
Poco::Logger::root().setLevel("trace");
2024-09-19 11:51:02 +00:00
2024-01-31 12:20:56 +00:00
auto logger = getLogger("keeper-dumper");
2021-10-08 08:48:08 +00:00
ResponsesQueue queue(std::numeric_limits<size_t>::max());
2021-03-16 11:44:43 +00:00
SnapshotsQueue snapshots_queue{1};
CoordinationSettingsPtr settings = std::make_shared<CoordinationSettings>();
2024-02-14 11:11:45 +00:00
KeeperContextPtr keeper_context = std::make_shared<DB::KeeperContext>(true, settings);
2023-07-06 23:53:13 +00:00
keeper_context->setLogDisk(std::make_shared<DB::DiskLocal>("LogDisk", argv[2]));
2023-07-20 10:44:44 +00:00
keeper_context->setSnapshotDisk(std::make_shared<DB::DiskLocal>("SnapshotDisk", argv[1]));
2023-05-24 09:46:07 +00:00
auto state_machine = std::make_shared<KeeperStateMachine<DB::KeeperMemoryStorage>>(queue, snapshots_queue, keeper_context, nullptr);
2021-03-16 11:44:43 +00:00
state_machine->init();
size_t last_commited_index = state_machine->last_commit_index();
2021-03-23 07:28:14 +00:00
LOG_INFO(logger, "Last committed index: {}", last_commited_index);
2021-03-16 11:44:43 +00:00
2023-02-01 12:28:44 +00:00
DB::KeeperLogStore changelog(
2024-10-16 12:38:46 +00:00
LogFileSettings{.force_sync = true, .compress_logs = (*settings)[DB::CoordinationSetting::compress_logs], .rotate_interval = 10000000},
2023-08-23 08:59:41 +00:00
FlushSettings(),
keeper_context);
2021-03-16 11:44:43 +00:00
changelog.init(last_commited_index, 10000000000UL); /// collect all logs
if (changelog.size() == 0)
LOG_INFO(logger, "Changelog empty");
else
LOG_INFO(logger, "Last changelog entry {}", changelog.next_slot() - 1);
for (size_t i = last_commited_index + 1; i < changelog.next_slot(); ++i)
{
if (changelog.entry_at(i)->get_val_type() == nuraft::log_val_type::app_log)
2022-11-02 18:26:50 +00:00
{
state_machine->pre_commit(i, changelog.entry_at(i)->get_buf());
2021-03-16 11:44:43 +00:00
state_machine->commit(i, changelog.entry_at(i)->get_buf());
2022-11-02 18:26:50 +00:00
}
2021-03-16 11:44:43 +00:00
}
dumpMachine(state_machine);
return 0;
}
2024-05-12 22:26:18 +00:00
// NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange)