YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/tablet/tablet-metadata-test.cc
Line
Count
Source
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
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#include <cstddef>
34
35
#include <glog/logging.h>
36
37
#include "yb/common/ql_protocol_util.h"
38
#include "yb/common/schema.h"
39
#include "yb/common/wire_protocol-test-util.h"
40
41
#include "yb/fs/fs_manager.h"
42
43
#include "yb/gutil/ref_counted.h"
44
45
#include "yb/tablet/local_tablet_writer.h"
46
#include "yb/tablet/operations/snapshot_operation.h"
47
#include "yb/tablet/tablet-test-util.h"
48
#include "yb/tablet/tablet.h"
49
#include "yb/tablet/tablet_metadata.h"
50
#include "yb/tablet/tablet_snapshots.h"
51
52
#include "yb/util/opid.h"
53
#include "yb/util/status_log.h"
54
55
namespace yb {
56
namespace tablet {
57
58
class TestRaftGroupMetadata : public YBTabletTest {
59
 public:
60
  TestRaftGroupMetadata()
61
2
      : YBTabletTest(GetSimpleTestSchema()) {
62
2
  }
63
64
2
  void SetUp() override {
65
2
    YBTabletTest::SetUp();
66
2
    writer_.reset(new LocalTabletWriter(harness_->tablet().get()));
67
2
  }
68
69
  void BuildPartialRow(int key, int intval, const char* strval,
70
                       QLWriteRequestPB* req);
71
72
 protected:
73
  std::unique_ptr<LocalTabletWriter> writer_;
74
};
75
76
void TestRaftGroupMetadata::BuildPartialRow(int key, int intval, const char* strval,
77
4
                                         QLWriteRequestPB* req) {
78
4
  req->Clear();
79
4
  QLAddInt32HashValue(req, key);
80
4
  QLAddInt32ColumnValue(req, kFirstColumnId + 1, intval);
81
4
  QLAddStringColumnValue(req, kFirstColumnId + 2, strval);
82
4
}
83
84
// Test that loading & storing the superblock results in an equivalent file.
85
1
TEST_F(TestRaftGroupMetadata, TestLoadFromSuperBlock) {
86
  // Write some data to the tablet and flush.
87
1
  QLWriteRequestPB req;
88
1
  BuildPartialRow(0, 0, "foo", &req);
89
1
  ASSERT_OK(writer_->Write(&req));
90
1
  ASSERT_OK(harness_->tablet()->Flush(tablet::FlushMode::kSync));
91
92
  // Create one more row. Write and flush.
93
1
  BuildPartialRow(1, 1, "bar", &req);
94
1
  ASSERT_OK(writer_->Write(&req));
95
1
  ASSERT_OK(harness_->tablet()->Flush(tablet::FlushMode::kSync));
96
97
  // Shut down the tablet.
98
1
  harness_->tablet()->StartShutdown();
99
1
  harness_->tablet()->CompleteShutdown();
100
101
1
  RaftGroupMetadata* meta = harness_->tablet()->metadata();
102
103
  // Dump the superblock to a PB. Save the PB to the side.
104
1
  RaftGroupReplicaSuperBlockPB superblock_pb_1;
105
1
  meta->ToSuperBlock(&superblock_pb_1);
106
107
  // Load the superblock PB back into the RaftGroupMetadata.
108
1
  ASSERT_OK(meta->ReplaceSuperBlock(superblock_pb_1));
109
110
  // Dump the tablet metadata to a superblock PB again, and save it.
111
1
  RaftGroupReplicaSuperBlockPB superblock_pb_2;
112
1
  meta->ToSuperBlock(&superblock_pb_2);
113
114
  // Compare the 2 dumped superblock PBs.
115
2
  ASSERT_EQ(superblock_pb_1.SerializeAsString(),
116
2
            superblock_pb_2.SerializeAsString())
117
2
    << superblock_pb_1.DebugString()
118
2
    << superblock_pb_2.DebugString();
119
120
1
  LOG(INFO) << "Superblocks match:\n"
121
1
            << superblock_pb_1.DebugString();
122
1
}
123
124
1
TEST_F(TestRaftGroupMetadata, TestDeleteTabletDataClearsDisk) {
125
1
  auto tablet = harness_->tablet();
126
127
  // Write some data to the tablet and flush.
128
1
  QLWriteRequestPB req;
129
1
  BuildPartialRow(0, 0, "foo", &req);
130
1
  ASSERT_OK(writer_->Write(&req));
131
1
  ASSERT_OK(tablet->Flush(tablet::FlushMode::kSync));
132
133
  // Create one more row. Write and flush.
134
1
  BuildPartialRow(1, 1, "bar", &req);
135
1
  ASSERT_OK(writer_->Write(&req));
136
1
  ASSERT_OK(tablet->Flush(tablet::FlushMode::kSync));
137
138
1
  const string snapshotId = "0123456789ABCDEF0123456789ABCDEF";
139
1
  tserver::TabletSnapshotOpRequestPB request;
140
1
  request.set_snapshot_id(snapshotId);
141
1
  tablet::SnapshotOperation operation(tablet.get(), &request);
142
1
  operation.set_hybrid_time(tablet->clock()->Now());
143
1
  operation.set_op_id(OpId(-1, 2));
144
1
  ASSERT_OK(tablet->snapshots().Create(&operation));
145
146
1
  ASSERT_TRUE(env_->DirExists(tablet->metadata()->rocksdb_dir()));
147
1
  ASSERT_TRUE(env_->DirExists(tablet->metadata()->intents_rocksdb_dir()));
148
1
  ASSERT_TRUE(env_->DirExists(tablet->metadata()->snapshots_dir()));
149
150
1
  CHECK_OK(tablet->metadata()->DeleteTabletData(
151
1
    TabletDataState::TABLET_DATA_DELETED, operation.op_id()));
152
153
1
  ASSERT_FALSE(env_->DirExists(tablet->metadata()->rocksdb_dir()));
154
1
  ASSERT_FALSE(env_->DirExists(tablet->metadata()->intents_rocksdb_dir()));
155
1
  ASSERT_FALSE(env_->DirExists(tablet->metadata()->snapshots_dir()));
156
1
}
157
158
} // namespace tablet
159
} // namespace yb