-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathldb_test.cc
165 lines (130 loc) · 4.47 KB
/
ldb_test.cc
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 (c) YugaByte, 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 <gtest/gtest.h>
#include "yb/client/client.h"
#include "yb/client/schema.h"
#include "yb/client/session.h"
#include "yb/client/table.h"
#include "yb/client/table_creator.h"
#include "yb/client/yb_op.h"
#include "yb/integration-tests/mini_cluster.h"
#include "yb/integration-tests/yb_mini_cluster_test_base.h"
#include "yb/tablet/tablet_metadata.h"
#include "yb/tablet/tablet_peer.h"
#include "yb/tools/data_gen_util.h"
#include "yb/util/path_util.h"
#include "yb/util/random.h"
#include "yb/util/random_util.h"
#include "yb/util/result.h"
#include "yb/util/status.h"
#include "yb/util/subprocess.h"
#include "yb/util/test_util.h"
using std::string;
using std::vector;
using namespace std::literals;
namespace yb {
namespace tools {
using client::YBClient;
using client::YBSchema;
using client::YBSchemaBuilder;
using client::YBTableName;
using client::YBTable;
static const char* const kTabletUtilToolName = "ldb";
static const char* const kNamespace = "ldb_test_namespace";
static const char* const kTableName = "my_table";
static constexpr int32_t kNumTablets = 1;
static constexpr int32_t kNumTabletServers = 1;
class YBTabletUtilTest : public YBMiniClusterTestBase<MiniCluster> {
public:
YBTabletUtilTest() : random_(0) {
}
void SetUp() override {
YBMiniClusterTestBase::SetUp();
MiniClusterOptions opts;
opts.num_tablet_servers = kNumTabletServers;
cluster_.reset(new MiniCluster(opts));
ASSERT_OK(cluster_->Start());
YBSchema schema;
YBSchemaBuilder b;
b.AddColumn("k")->Type(DataType::INT64)->NotNull()->HashPrimaryKey();
ASSERT_OK(b.Build(&schema));
client_ = ASSERT_RESULT(cluster_->CreateClient());
// Create the namespace.
ASSERT_OK(client_->CreateNamespace(kNamespace));
// Create the table.
const YBTableName table_name(YQL_DATABASE_CQL, kNamespace, kTableName);
ASSERT_OK(client_
->NewTableCreator()
->table_name(table_name)
.table_type(client::YBTableType::YQL_TABLE_TYPE)
.schema(&schema)
.num_tablets(kNumTablets)
.wait(true)
.Create());
ASSERT_OK(client_->OpenTable(table_name, &table_));
}
void DoTearDown() override {
client_.reset();
cluster_->Shutdown();
}
protected:
Status WriteData() {
auto session = client_->NewSession(5s);
std::shared_ptr<client::YBqlWriteOp> insert(table_->NewQLWrite());
auto req = insert->mutable_request();
GenerateDataForRow(table_->schema(), 17 /* record_id */, &random_, req);
session->Apply(insert);
RETURN_NOT_OK(session->TEST_Flush());
return Status::OK();
}
Result<string> GetTabletDbPath() {
for (const auto& peer : cluster_->GetTabletPeers(0)) {
if (peer->TEST_table_type() == TableType::YQL_TABLE_TYPE) {
return peer->tablet_metadata()->rocksdb_dir();
}
}
return STATUS(IllegalState, "Did not find tablet peer with YCQL table");
}
std::unique_ptr<YBClient> client_;
std::shared_ptr<YBTable> table_;
Random random_;
};
TEST_F(YBTabletUtilTest, VerifySingleKeyIsFound) {
string output;
ASSERT_OK(WriteData());
ASSERT_OK(cluster_->FlushTablets(tablet::FlushMode::kSync, tablet::FlushFlags::kAllDbs));
string db_path = ASSERT_RESULT(GetTabletDbPath());
vector<string> argv = {
GetToolPath(kTabletUtilToolName),
"dump",
"--compression_type=snappy",
"--db=" + db_path
};
ASSERT_OK(Subprocess::Call(argv, &output));
ASSERT_NE(output.find("Keys in range: 1"), string::npos);
}
TEST_F(YBTabletUtilTest, DumpManifestFile) {
string output;
ASSERT_OK(WriteData());
ASSERT_OK(cluster_->FlushTablets(tablet::FlushMode::kSync, tablet::FlushFlags::kAllDbs));
string db_path = ASSERT_RESULT(GetTabletDbPath());
vector<string> argv = {
GetToolPath(kTabletUtilToolName),
"manifest_dump",
"--db=" + db_path
};
// Make sure LDB is not crashing
ASSERT_OK(Subprocess::Call(argv, &output));
}
} // namespace tools
} // namespace yb