YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/tablet/tablet-harness.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 "yb/tablet/tablet-harness.h"
15
16
#include "yb/common/index.h"
17
#include "yb/common/partition.h"
18
19
#include "yb/consensus/log_anchor_registry.h"
20
21
#include "yb/server/logical_clock.h"
22
23
#include "yb/tablet/tablet.h"
24
#include "yb/tablet/tablet_metadata.h"
25
26
#include "yb/util/result.h"
27
28
namespace yb {
29
namespace tablet {
30
31
549
std::pair<PartitionSchema, Partition> CreateDefaultPartition(const Schema& schema) {
32
  // Create a default partition schema.
33
549
  PartitionSchema partition_schema;
34
549
  CHECK_OK(PartitionSchema::FromPB(PartitionSchemaPB(), schema, &partition_schema));
35
36
  // Create the tablet partitions.
37
549
  vector<Partition> partitions;
38
549
  CHECK_OK(partition_schema.CreatePartitions(vector<YBPartialRow>(), schema, &partitions));
39
549
  CHECK_EQ(1, partitions.size());
40
549
  return std::make_pair(partition_schema, partitions[0]);
41
549
}
42
43
74
CHECKED_STATUS TabletHarness::Create(bool first_time) {
44
74
  std::pair<PartitionSchema, Partition> partition(CreateDefaultPartition(schema_));
45
46
  // Build the Tablet
47
74
  fs_manager_.reset(new FsManager(options_.env, options_.root_dir, "tserver_test"));
48
74
  if (first_time) {
49
68
    RETURN_NOT_OK(fs_manager_->CreateInitialFileSystemLayout());
50
68
  }
51
74
  RETURN_NOT_OK(fs_manager_->CheckAndOpenFileSystemRoots());
52
53
74
  auto table_info = std::make_shared<TableInfo>(
54
74
      "YBTableTest", "test", "YBTableTest", options_.table_type, schema_, IndexMap(), boost::none,
55
74
      0 /* schema_version */, partition.first);
56
74
  auto metadata = VERIFY_RESULT(RaftGroupMetadata::LoadOrCreate(RaftGroupMetadataData {
57
74
    .fs_manager = fs_manager_.get(),
58
74
    .table_info = table_info,
59
74
    .raft_group_id = options_.tablet_id,
60
74
    .partition = partition.second,
61
74
    .tablet_data_state = TABLET_DATA_READY,
62
74
  }));
63
74
  if (options_.enable_metrics) {
64
74
    metrics_registry_.reset(new MetricRegistry());
65
74
  }
66
67
74
  clock_ = server::LogicalClock::CreateStartingAt(HybridTime::kInitial);
68
74
  tablet_ = std::make_shared<Tablet>(MakeTabletInitData(metadata));
69
74
  return Status::OK();
70
74
}
71
72
74
CHECKED_STATUS TabletHarness::Open() {
73
74
  RETURN_NOT_OK(tablet_->Open());
74
74
  tablet_->MarkFinishedBootstrapping();
75
74
  return tablet_->EnableCompactions(/* non_abortable_ops_pause */ nullptr);
76
74
}
77
78
6
Result<TabletPtr> TabletHarness::OpenTablet(const TabletId& tablet_id) {
79
6
  auto metadata = VERIFY_RESULT(RaftGroupMetadata::Load(fs_manager_.get(), tablet_id));
80
0
  auto tablet = std::make_shared<Tablet>(MakeTabletInitData(metadata));
81
6
  RETURN_NOT_OK(tablet->Open());
82
6
  tablet->MarkFinishedBootstrapping();
83
6
  RETURN_NOT_OK(tablet->EnableCompactions(/* non_abortable_ops_pause */ nullptr));
84
6
  return tablet;
85
6
}
86
87
80
TabletInitData TabletHarness::MakeTabletInitData(const RaftGroupMetadataPtr& metadata) {
88
80
  return TabletInitData {
89
80
    .metadata = metadata,
90
80
    .client_future = std::shared_future<client::YBClient*>(),
91
80
    .clock = clock_,
92
80
    .parent_mem_tracker = std::shared_ptr<MemTracker>(),
93
80
    .block_based_table_mem_tracker = std::shared_ptr<MemTracker>(),
94
80
    .metric_registry = metrics_registry_.get(),
95
80
    .log_anchor_registry = new log::LogAnchorRegistry(),
96
80
    .tablet_options = TabletOptions(),
97
80
    .log_prefix_suffix = std::string(),
98
80
    .transaction_participant_context = nullptr,
99
80
    .local_tablet_filter = client::LocalTabletFilter(),
100
80
    .transaction_coordinator_context = nullptr,
101
80
    .txns_enabled = TransactionsEnabled::kFalse,
102
80
    .is_sys_catalog = IsSysCatalogTablet::kFalse,
103
80
    .snapshot_coordinator = nullptr,
104
80
    .tablet_splitter = nullptr
105
80
  };
106
80
}
107
108
} // namespace tablet
109
} // namespace yb