forked from pingcap/tiflash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDTTool.h
More file actions
165 lines (146 loc) · 5.28 KB
/
DTTool.h
File metadata and controls
165 lines (146 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2023 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 <Common/TiFlashBuildInfo.h>
#include <Common/UnifiedLogFormatter.h>
#include <IO/Encryption/DataKeyManager.h>
#include <IO/Encryption/MockKeyManager.h>
#include <Interpreters/Context.h>
#include <Poco/ConsoleChannel.h>
#include <Poco/File.h>
#include <Poco/FormattingChannel.h>
#include <Poco/Logger.h>
#include <Poco/PatternFormatter.h>
#include <Server/CLIService.h>
#include <Server/IServer.h>
#include <Server/RaftConfigParser.h>
#include <Storages/KVStore/FFI/ProxyFFI.h>
#include <Storages/KVStore/TMTContext.h>
#include <daemon/BaseDaemon.h>
#include <pingcap/Config.h>
#include <ext/scope_guard.h>
#include <string>
#include <vector>
#define _TO_STRING(X) #X
#define TO_STRING(X) _TO_STRING(X)
using RaftStoreFFIFunc = void (*)(int argc, const char * const * argv, const DB::EngineStoreServerHelper *);
namespace DTTool
{
int mainEntryTiFlashDTTool(int argc, char ** argv);
}
namespace DTTool::Bench
{
int benchEntry(const std::vector<std::string> & opts);
}
namespace DTTool::Inspect
{
struct InspectArgs
{
bool check;
bool dump_columns;
bool dump_all_columns;
bool dump_minmax;
bool dump_merged_files;
size_t file_id;
std::string workdir;
std::vector<DB::ColumnID> col_ids;
};
int inspectEntry(const std::vector<std::string> & opts, RaftStoreFFIFunc ffi_function);
} // namespace DTTool::Inspect
namespace DTTool::Migrate
{
struct MigrateArgs
{
bool no_keep;
bool dry_mode;
size_t file_id;
size_t version;
size_t frame;
DB::ChecksumAlgo algorithm;
std::string workdir;
DB::CompressionMethod compression_method;
int compression_level;
};
int migrateEntry(const std::vector<std::string> & opts, RaftStoreFFIFunc ffi_function);
} // namespace DTTool::Migrate
namespace DTTool
{
namespace detail
{
using namespace DB;
class ImitativeEnv
{
DB::ContextPtr createImitativeContext(const std::string & workdir, bool encryption = false)
{
// set itself as global context
global_context = DB::Context::createGlobal(DB::Context::ApplicationType::LOCAL);
global_context->initializeTiFlashMetrics();
KeyManagerPtr key_manager = std::make_shared<MockKeyManager>(encryption);
global_context->initializeFileProvider(key_manager, encryption);
auto & settings = global_context->getSettingsRef();
global_context->initializeBackgroundPool(settings.background_pool_size.get());
global_context->initializeBlockableBackgroundPool(settings.background_pool_size.get());
// Theses global variables should be initialized by the following order
// 1. capacity
// 2. path pool
// 3. TMTContext
auto path = Poco::Path{workdir}.absolute().toString();
global_context->initializePathCapacityMetric(0, {path}, {}, {}, {});
global_context->setPathPool(
/*main_data_paths*/ {path},
/*latest_data_paths*/ {path},
/*kvstore_paths*/ Strings{},
global_context->getPathCapacity(),
global_context->getFileProvider());
TiFlashRaftConfig raft_config;
global_context->initializeGlobalPageIdAllocator();
global_context->initializeGlobalStoragePoolIfNeed(global_context->getPathPool());
raft_config.ignore_databases = {"default", "system"};
global_context->createTMTContext(raft_config, pingcap::ClusterConfig());
global_context->setDeltaIndexManager(1024 * 1024 * 100 /*100MB*/);
auto & path_pool = global_context->getPathPool();
global_context->getTMTContext().restore(path_pool);
return global_context;
}
static void setupLogger()
{
Poco::AutoPtr<Poco::ConsoleChannel> channel = new Poco::ConsoleChannel(std::cout);
// only enable colors when the output is a terminal
bool enable_colors = isatty(STDOUT_FILENO) && isatty(STDERR_FILENO);
Poco::AutoPtr<Poco::Formatter> formatter;
if (enable_colors)
formatter = new UnifiedLogFormatter<true>();
else
formatter = new UnifiedLogFormatter<false>();
Poco::AutoPtr<Poco::FormattingChannel> formatting_channel(new Poco::FormattingChannel(formatter, channel));
Poco::Logger::root().setChannel(formatting_channel);
Poco::Logger::root().setLevel("debug");
}
ContextPtr global_context{};
public:
explicit ImitativeEnv(const std::string & workdir, bool encryption = false)
{
setupLogger();
createImitativeContext(workdir, encryption);
}
~ImitativeEnv()
{
global_context->getTMTContext().setStatusTerminated();
global_context->shutdown();
global_context.reset();
}
ContextPtr getContext() { return global_context; }
};
} // namespace detail
} // namespace DTTool