YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/ent/src/yb/tserver/backup_service-test.cc
Line
Count
Source
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
13
#include "yb/common/wire_protocol.h"
14
#include "yb/common/wire_protocol-test-util.h"
15
16
#include "yb/rpc/messenger.h"
17
#include "yb/rpc/rpc_controller.h"
18
19
#include "yb/tablet/tablet.h"
20
#include "yb/tablet/tablet_metadata.h"
21
#include "yb/tablet/tablet_peer.h"
22
#include "yb/tablet/tablet_snapshots.h"
23
24
#include "yb/tserver/backup.proxy.h"
25
#include "yb/tserver/mini_tablet_server.h"
26
#include "yb/tserver/tablet_server.h"
27
#include "yb/tserver/tablet_server-test-base.h"
28
#include "yb/tserver/ts_tablet_manager.h"
29
#include "yb/tserver/tserver.pb.h"
30
#include "yb/tserver/tserver_service.proxy.h"
31
32
namespace yb {
33
namespace tserver {
34
35
using std::string;
36
37
using yb::rpc::RpcController;
38
using yb::tablet::TabletPeer;
39
using yb::tablet::Tablet;
40
41
class BackupServiceTest : public TabletServerTestBase {
42
 public:
43
2
  BackupServiceTest() : TabletServerTestBase(TableType::YQL_TABLE_TYPE) {}
44
45
 protected:
46
2
  void SetUp() override {
47
2
    TabletServerTestBase::SetUp();
48
2
    StartTabletServer();
49
50
2
    backup_proxy_.reset(
51
2
        new TabletServerBackupServiceProxy(
52
2
            proxy_cache_.get(), HostPort::FromBoundEndpoint(mini_server_->bound_rpc_addr())));
53
2
  }
54
55
  std::unique_ptr<TabletServerBackupServiceProxy> backup_proxy_;
56
};
57
58
1
TEST_F(BackupServiceTest, TestCreateTabletSnapshot) {
59
  // Verify that the tablet exists.
60
1
  std::shared_ptr<TabletPeer> tablet;
61
1
  ASSERT_TRUE(mini_server_->server()->tablet_manager()->LookupTablet(kTabletId, &tablet));
62
1
  FsManager* const fs = tablet->tablet_metadata()->fs_manager();
63
64
1
  const string snapshot_id = "00000000000000000000000000000000";
65
1
  const string rocksdb_dir = tablet->tablet_metadata()->rocksdb_dir();
66
1
  const string top_snapshots_dir = tablet->tablet_metadata()->snapshots_dir();
67
1
  const string snapshot_dir = JoinPathSegments(top_snapshots_dir, snapshot_id);
68
69
1
  TabletSnapshotOpRequestPB req;
70
1
  TabletSnapshotOpResponsePB resp;
71
72
1
  req.set_operation(TabletSnapshotOpRequestPB::CREATE_ON_TABLET);
73
1
  req.set_dest_uuid(mini_server_->server()->fs_manager()->uuid());
74
1
  req.set_snapshot_id(snapshot_id);
75
76
  // Test empty tablet list - expected error.
77
  // Send the call.
78
1
  {
79
1
    RpcController rpc;
80
1
    SCOPED_TRACE(req.DebugString());
81
1
    ASSERT_OK(backup_proxy_->TabletSnapshotOp(req, &resp, &rpc));
82
1
    ASSERT_NOK(StatusFromPB(resp.error().status()));
83
1
  }
84
85
1
  req.add_tablet_id(kTabletId);
86
87
1
  ASSERT_TRUE(fs->Exists(rocksdb_dir));
88
1
  ASSERT_TRUE(fs->Exists(top_snapshots_dir));
89
90
  // Send the call.
91
1
  {
92
1
    RpcController rpc;
93
1
    SCOPED_TRACE(req.DebugString());
94
1
    ASSERT_OK(backup_proxy_->TabletSnapshotOp(req, &resp, &rpc));
95
1
    SCOPED_TRACE(resp.DebugString());
96
1
    ASSERT_FALSE(resp.has_error());
97
1
  }
98
99
1
  ASSERT_TRUE(fs->Exists(rocksdb_dir));
100
1
  ASSERT_TRUE(fs->Exists(top_snapshots_dir));
101
1
  ASSERT_TRUE(fs->Exists(snapshot_dir));
102
  // Check existence of snapshot files:
103
1
  ASSERT_TRUE(fs->Exists(JoinPathSegments(snapshot_dir, "CURRENT")));
104
1
  ASSERT_TRUE(fs->Exists(JoinPathSegments(snapshot_dir, "MANIFEST-000001")));
105
1
}
106
107
1
TEST_F(BackupServiceTest, TestSnapshotData) {
108
  // Verify that the tablet exists.
109
1
  std::shared_ptr<TabletPeer> tablet;
110
1
  ASSERT_TRUE(mini_server_->server()->tablet_manager()->LookupTablet(kTabletId, &tablet));
111
1
  tablet.reset();
112
113
1
  WriteRequestPB write_req;
114
1
  WriteResponsePB write_resp;
115
1
  write_req.set_tablet_id(kTabletId);
116
117
  // Send an actual row insert.
118
1
  {
119
1
    AddTestRowInsert(1, 11, "key1", &write_req);
120
121
1
    RpcController rpc;
122
1
    SCOPED_TRACE(write_req.DebugString());
123
1
    ASSERT_OK(proxy_->Write(write_req, &write_resp, &rpc));
124
1
    SCOPED_TRACE(write_resp.DebugString());
125
1
    ASSERT_FALSE(write_resp.has_error());
126
1
  }
127
128
1
  VerifyRows(schema_, { KeyValue(1, 11) });
129
130
1
  const string snapshot_id = "00000000000000000000000000000000";
131
132
1
  TabletSnapshotOpRequestPB req;
133
1
  TabletSnapshotOpResponsePB resp;
134
135
  // Send the create snapshot request.
136
1
  req.set_operation(TabletSnapshotOpRequestPB::CREATE_ON_TABLET);
137
1
  req.set_dest_uuid(mini_server_->server()->fs_manager()->uuid());
138
1
  req.set_snapshot_id(snapshot_id);
139
1
  req.add_tablet_id(kTabletId);
140
1
  {
141
1
    RpcController rpc;
142
1
    SCOPED_TRACE(req.DebugString());
143
1
    ASSERT_OK(backup_proxy_->TabletSnapshotOp(req, &resp, &rpc));
144
1
    SCOPED_TRACE(resp.DebugString());
145
1
    ASSERT_FALSE(resp.has_error());
146
1
  }
147
148
1
  SleepFor(MonoDelta::FromMilliseconds(500));
149
1
  LOG(INFO) << "CREATED SNAPSHOT. UPDATING THE TABLET DATA..";
150
151
  // Send the second row.
152
1
  {
153
1
    AddTestRowInsert(2, 22, "key1", &write_req);
154
155
1
    RpcController rpc;
156
1
    SCOPED_TRACE(write_req.DebugString());
157
1
    ASSERT_OK(proxy_->Write(write_req, &write_resp, &rpc));
158
1
    SCOPED_TRACE(write_resp.DebugString());
159
1
    ASSERT_FALSE(write_resp.has_error());
160
1
  }
161
162
1
  VerifyRows(schema_, { KeyValue(1, 11), KeyValue(2, 22) });
163
164
  // Send the restore snapshot request.
165
1
  req.set_operation(TabletSnapshotOpRequestPB::RESTORE_ON_TABLET);
166
1
  {
167
1
    RpcController rpc;
168
1
    SCOPED_TRACE(req.DebugString());
169
1
    ASSERT_OK(backup_proxy_->TabletSnapshotOp(req, &resp, &rpc));
170
1
    SCOPED_TRACE(resp.DebugString());
171
1
    ASSERT_FALSE(resp.has_error());
172
1
  }
173
174
1
  SleepFor(MonoDelta::FromMilliseconds(500));
175
1
  LOG(INFO) << "RESTORED SNAPSHOT. CHECKING THE TABLET DATA..";
176
177
  // Expected the first row only from the snapshot.
178
  // FIXME: Current implementation of VerifyRows() can fail due to RocksDB reset in
179
  //        RestoreSnapshot(). VerifyRows() must be fixed.
180
  // VerifyRows(schema_, { KeyValue(1, 11) });
181
182
1
  LOG(INFO) << "THE TABLET DATA IS VALID. Test TestSnapshotData finished.";
183
1
}
184
185
} // namespace tserver
186
} // namespace yb