Skip to content

Commit 082a5d8

Browse files
committed
add snapshot manager ut
1 parent 38adb3e commit 082a5d8

File tree

1 file changed

+225
-0
lines changed

1 file changed

+225
-0
lines changed
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#include <gen_cpp/AgentService_types.h>
19+
#include <gtest/gtest-message.h>
20+
#include <gtest/gtest-test-part.h>
21+
22+
#include <memory>
23+
#include <string>
24+
#include <vector>
25+
26+
#include "common/status.h"
27+
#include "gtest/gtest_pred_impl.h"
28+
#include "io/fs/local_file_system.h"
29+
#include "olap/data_dir.h"
30+
#include "olap/olap_common.h"
31+
#include "olap/olap_define.h"
32+
#include "olap/options.h"
33+
#include "olap/snapshot_manager.h"
34+
#include "olap/storage_engine.h"
35+
#include "olap/tablet.h"
36+
#include "olap/tablet_manager.h"
37+
#include "olap/tablet_meta.h"
38+
#include "runtime/exec_env.h"
39+
#include "util/uid_util.h"
40+
41+
namespace doris {
42+
class SnapshotManagerTest : public testing::Test {
43+
public:
44+
virtual void SetUp() {
45+
_engine_data_path = "./be/test/olap/test_data/converter_test_data/tmp";
46+
auto st = io::global_local_filesystem()->delete_directory(_engine_data_path);
47+
ASSERT_TRUE(st.ok()) << st;
48+
st = io::global_local_filesystem()->create_directory(_engine_data_path);
49+
ASSERT_TRUE(st.ok()) << st;
50+
EXPECT_TRUE(
51+
io::global_local_filesystem()->create_directory(_engine_data_path + "/meta").ok());
52+
53+
config::tablet_map_shard_size = 1;
54+
config::txn_map_shard_size = 1;
55+
config::txn_shard_size = 1;
56+
EngineOptions options;
57+
options.backend_uid = UniqueId::gen_uid();
58+
auto engine = std::make_unique<StorageEngine>(options);
59+
ExecEnv::GetInstance()->set_storage_engine(std::move(engine));
60+
_engine = &ExecEnv::GetInstance()->storage_engine().to_local();
61+
_data_dir = new DataDir(*_engine, _engine_data_path, 1000000000);
62+
static_cast<void>(_data_dir->init());
63+
}
64+
65+
virtual void TearDown() {
66+
SAFE_DELETE(_data_dir);
67+
EXPECT_TRUE(io::global_local_filesystem()->delete_directory(_engine_data_path).ok());
68+
ExecEnv::GetInstance()->set_storage_engine(nullptr);
69+
_engine = nullptr;
70+
}
71+
protected:
72+
StorageEngine* _engine = nullptr;
73+
DataDir* _data_dir = nullptr;
74+
std::string _engine_data_path;
75+
};
76+
77+
TEST_F(SnapshotManagerTest, TestMakeSnapshotInvalidParameters) {
78+
TSnapshotRequest request;
79+
request.tablet_id = 10001;
80+
request.schema_hash = 12345;
81+
82+
std::string* null_snapshot_path = nullptr;
83+
bool allow_incremental_clone = false;
84+
85+
Status status = _engine->snapshot_mgr()->make_snapshot(request, null_snapshot_path, &allow_incremental_clone);
86+
EXPECT_FALSE(status.ok());
87+
EXPECT_EQ(status.code(), ErrorCode::INVALID_ARGUMENT);
88+
}
89+
90+
TEST_F(SnapshotManagerTest, TestReleaseSnapshotInvalidPath) {
91+
std::string invalid_path = "/invalid/snapshot/path";
92+
93+
Status status = _engine->snapshot_mgr()->release_snapshot(invalid_path);
94+
EXPECT_FALSE(status.ok());
95+
EXPECT_EQ(status.code(), ErrorCode::CE_CMD_PARAMS_ERROR);
96+
}
97+
98+
TEST_F(SnapshotManagerTest, TestConvertRowsetIdsInvalidDir) {
99+
std::string non_existent_dir = "/non/existent/dir";
100+
int64_t tablet_id = 10003;
101+
int64_t replica_id = 1;
102+
int64_t table_id = 1000;
103+
int64_t partition_id = 100;
104+
int32_t schema_hash = 54321;
105+
106+
auto result = _engine->snapshot_mgr()->convert_rowset_ids(non_existent_dir, tablet_id, replica_id,
107+
table_id, partition_id, schema_hash);
108+
EXPECT_FALSE(result.has_value());
109+
EXPECT_EQ(result.error().code(), ErrorCode::DIR_NOT_EXIST);
110+
}
111+
112+
TEST_F(SnapshotManagerTest, TestConvertRowsetIdsNormal) {
113+
std::string clone_dir = _engine_data_path + "/clone_dir_local";
114+
EXPECT_TRUE(io::global_local_filesystem()->create_directory(clone_dir).ok());
115+
116+
TabletMetaPB tablet_meta_pb;
117+
tablet_meta_pb.set_tablet_id(10006);
118+
tablet_meta_pb.set_schema_hash(12346);
119+
tablet_meta_pb.set_replica_id(1);
120+
tablet_meta_pb.set_table_id(1000);
121+
tablet_meta_pb.set_partition_id(100);
122+
123+
TabletSchemaPB* schema_pb = tablet_meta_pb.mutable_schema();
124+
schema_pb->set_num_short_key_columns(1);
125+
schema_pb->set_num_rows_per_row_block(1024);
126+
schema_pb->set_compress_kind(COMPRESS_LZ4);
127+
128+
// column 0
129+
ColumnPB* column = schema_pb->add_column();
130+
column->set_unique_id(0);
131+
column->set_name("k1");
132+
column->set_type("INT");
133+
column->set_is_key(true);
134+
column->set_is_nullable(false);
135+
column->set_length(4);
136+
column->set_index_length(4);
137+
column->set_precision(0);
138+
column->set_frac(0);
139+
140+
// column 1
141+
column = schema_pb->add_column();
142+
column->set_unique_id(1);
143+
column->set_name("v1");
144+
column->set_type("INT");
145+
column->set_is_key(false);
146+
column->set_is_nullable(true);
147+
column->set_length(4);
148+
column->set_index_length(4);
149+
column->set_precision(0);
150+
column->set_frac(0);
151+
152+
// index 0
153+
TabletIndexPB* index_pb = schema_pb->add_index();
154+
index_pb->set_index_id(1001);
155+
index_pb->set_index_name("test_index");
156+
index_pb->set_index_type(IndexType::BITMAP);
157+
index_pb->add_col_unique_id(0);
158+
159+
// rowset meta 0
160+
RowsetMetaPB* rowset_meta = tablet_meta_pb.add_rs_metas();
161+
rowset_meta->set_rowset_id(10001);
162+
rowset_meta->set_rowset_id_v2("02000000000000010001");
163+
rowset_meta->set_tablet_id(10006);
164+
rowset_meta->set_tablet_schema_hash(12346);
165+
rowset_meta->set_rowset_type(BETA_ROWSET);
166+
rowset_meta->set_rowset_state(VISIBLE);
167+
rowset_meta->set_start_version(0);
168+
rowset_meta->set_end_version(1);
169+
rowset_meta->set_num_rows(100);
170+
rowset_meta->set_total_disk_size(1024);
171+
rowset_meta->set_data_disk_size(1000);
172+
rowset_meta->set_index_disk_size(24);
173+
rowset_meta->set_empty(false);
174+
175+
// new ids
176+
int64_t tablet_id = 20006;
177+
int64_t replica_id = 2;
178+
int64_t table_id = 2000;
179+
int64_t partition_id = 200;
180+
int32_t schema_hash = 65432;
181+
182+
// save the meta file
183+
std::string meta_file = clone_dir + "/" + std::to_string(tablet_id) + ".hdr";
184+
EXPECT_TRUE(TabletMeta::save(meta_file, tablet_meta_pb).ok());
185+
186+
auto result = _engine->snapshot_mgr()->convert_rowset_ids(clone_dir, tablet_id, replica_id,
187+
table_id, partition_id, schema_hash);
188+
EXPECT_TRUE(result.has_value());
189+
190+
TabletMetaPB converted_meta_pb;
191+
EXPECT_TRUE(TabletMeta::load_from_file(meta_file, &converted_meta_pb).ok());
192+
193+
EXPECT_EQ(converted_meta_pb.tablet_id(), tablet_id);
194+
EXPECT_EQ(converted_meta_pb.schema_hash(), schema_hash);
195+
EXPECT_EQ(converted_meta_pb.replica_id(), replica_id);
196+
EXPECT_EQ(converted_meta_pb.table_id(), table_id);
197+
EXPECT_EQ(converted_meta_pb.partition_id(), partition_id);
198+
199+
// verify schema
200+
EXPECT_EQ(converted_meta_pb.schema().num_short_key_columns(), 1);
201+
EXPECT_EQ(converted_meta_pb.schema().column_size(), 2);
202+
203+
// verify index
204+
EXPECT_EQ(converted_meta_pb.schema().index_size(), 1);
205+
const TabletIndexPB& converted_index = converted_meta_pb.schema().index(0);
206+
EXPECT_EQ(converted_index.index_id(), 1001);
207+
EXPECT_EQ(converted_index.index_name(), "test_index");
208+
EXPECT_EQ(converted_index.index_type(), IndexType::BITMAP);
209+
EXPECT_EQ(converted_index.col_unique_id_size(), 1);
210+
EXPECT_EQ(converted_index.col_unique_id(0), 0);
211+
212+
// verify rowset meta
213+
EXPECT_EQ(converted_meta_pb.rs_metas_size(), 1);
214+
const RowsetMetaPB& converted_rowset_meta = converted_meta_pb.rs_metas(0);
215+
EXPECT_NE(converted_rowset_meta.rowset_id(), 10001);
216+
EXPECT_NE(converted_rowset_meta.rowset_id_v2(), "02000000000000010001");
217+
EXPECT_EQ(converted_rowset_meta.tablet_id(), tablet_id);
218+
EXPECT_EQ(converted_rowset_meta.rowset_type(), BETA_ROWSET);
219+
EXPECT_EQ(converted_rowset_meta.rowset_state(), VISIBLE);
220+
EXPECT_EQ(converted_rowset_meta.start_version(), 0);
221+
EXPECT_EQ(converted_rowset_meta.end_version(), 1);
222+
EXPECT_EQ(converted_rowset_meta.num_rows(), 100);
223+
EXPECT_EQ(converted_rowset_meta.total_disk_size(), 1024);
224+
}
225+
}

0 commit comments

Comments
 (0)