YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/tablet/tablet_data_integrity-test.cc
Line
Count
Source (jump to first uncovered line)
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
14
#include <gtest/gtest.h>
15
16
#include "yb/common/ql_expr.h"
17
18
#include "yb/gutil/stl_util.h"
19
#include "yb/gutil/strings/join.h"
20
21
#include "yb/tablet/local_tablet_writer.h"
22
#include "yb/tablet/tablet-test-base.h"
23
#include "yb/tablet/tablet.h"
24
#include "yb/tablet/tablet_metadata.h"
25
26
#include "yb/util/path_util.h"
27
#include "yb/util/slice.h"
28
#include "yb/util/test_macros.h"
29
30
namespace yb {
31
namespace tablet {
32
33
class TabletDataIntegrityTest : public TabletTestBase<IntKeyTestSetup<INT32>> {
34
  typedef TabletTestBase<IntKeyTestSetup<INT32>> superclass;
35
 public:
36
3
  void SetUp() override {
37
3
    superclass::SetUp();
38
39
3
    const auto& tablet = this->tablet().get();
40
3
    LocalTabletWriter writer(tablet);
41
3
    ASSERT_OK(this->InsertTestRow(&writer, 12345, 0));
42
3
    ASSERT_OK(tablet->Flush(FlushMode::kSync));
43
3
  }
44
45
 protected:
46
2
  Result<std::string> GetFirstSstFilePath() {
47
2
    const auto& tablet = this->tablet().get();
48
2
    auto dir = tablet->metadata()->rocksdb_dir();
49
2
    auto list = VERIFY_RESULT(tablet->metadata()->fs_manager()->ListDir(dir));
50
2
    if (std::find(list.begin(), list.end(), "CURRENT") == list.end()) {
51
0
      return STATUS(NotFound, "No rocksdb files found at tablet directory");
52
0
    }
53
54
8
    for (const auto& file : list) {
55
8
      if (file.find(".sst") != std::string::npos) {
56
2
        return JoinPathSegments(dir, file);
57
2
      }
58
8
    }
59
60
0
    return STATUS(NotFound, "No sst files found in rocksdb directory");
61
2
  }
62
};
63
64
1
TEST_F(TabletDataIntegrityTest, TestNoCorruption) {
65
1
  const auto& tablet = this->tablet().get();
66
1
  ASSERT_OK(tablet->VerifyDataIntegrity());
67
1
}
68
69
1
TEST_F(TabletDataIntegrityTest, TestDeletedFile) {
70
1
  const auto& tablet = this->tablet().get();
71
72
1
  auto sst_path = ASSERT_RESULT(GetFirstSstFilePath());
73
1
  ASSERT_OK(env_->DeleteFile(sst_path));
74
75
1
  Status s = tablet->VerifyDataIntegrity();
76
1
  ASSERT_TRUE(s.IsCorruption());
77
1
  ASSERT_STR_CONTAINS(s.message().ToBuffer(), "No such file");
78
1
}
79
80
1
TEST_F(TabletDataIntegrityTest, TestFileTruncate) {
81
1
  const auto& tablet = this->tablet().get();
82
83
1
  auto sst_path = ASSERT_RESULT(GetFirstSstFilePath());
84
1
  faststring data;
85
1
  ASSERT_OK(ReadFileToString(env_.get(), sst_path, &data));
86
1
  data.resize(1);
87
1
  ASSERT_OK(WriteStringToFile(env_.get(), Slice(data), sst_path));
88
89
1
  Status s = tablet->VerifyDataIntegrity();
90
1
  ASSERT_TRUE(s.IsCorruption());
91
1
  ASSERT_STR_CONTAINS(s.message().ToBuffer(), "file size mismatch");
92
1
}
93
94
// Skipping as we currently don't have any block checks in place.
95
// TODO: enable this test once we add those. (See issue #7904)
96
0
TEST_F(TabletDataIntegrityTest, DISABLED_TestFileGarbageOverwrite) {
97
0
  const auto& tablet = this->tablet().get();
98
99
0
  auto sst_path = ASSERT_RESULT(GetFirstSstFilePath());
100
0
  faststring data;
101
0
  ASSERT_OK(ReadFileToString(env_.get(), sst_path, &data));
102
103
0
  faststring garbage;
104
0
  garbage.resize(data.size());
105
0
  ASSERT_OK(WriteStringToFile(env_.get(), Slice(garbage), sst_path));
106
107
0
  Status s = tablet->VerifyDataIntegrity();
108
0
  ASSERT_TRUE(s.IsCorruption());
109
0
  ASSERT_STR_CONTAINS(s.message().ToBuffer(), "bad block contents");
110
0
}
111
112
} // namespace tablet
113
} // namespace yb