YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/ent/src/yb/master/catalog_entity_info.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
#include "yb/master/catalog_entity_info.h"
14
15
#include "yb/common/wire_protocol.h"
16
17
#include "yb/master/cdc_rpc_tasks.h"
18
19
#include "yb/util/result.h"
20
#include "yb/util/shared_lock.h"
21
#include "yb/util/trace.h"
22
23
using std::set;
24
25
namespace yb {
26
namespace master {
27
28
// ================================================================================================
29
// CDCStreamInfo
30
// ================================================================================================
31
32
126
const google::protobuf::RepeatedPtrField<std::string>& CDCStreamInfo::table_id() const {
33
126
  return LockForRead()->pb.table_id();
34
126
}
35
36
44
const NamespaceId& CDCStreamInfo::namespace_id() const {
37
44
  return LockForRead()->pb.namespace_id();
38
44
}
39
40
310
std::string CDCStreamInfo::ToString() const {
41
310
  auto l = LockForRead();
42
310
  if (l->pb.has_namespace_id()) {
43
306
    return Format(
44
306
        "$0 [namespace=$1] {metadata=$2} ", id(), l->pb.namespace_id(), l->pb.ShortDebugString());
45
306
  }
46
4
  return Format("$0 [table=$1] {metadata=$2} ", id(), l->pb.table_id(0), l->pb.ShortDebugString());
47
310
}
48
49
// ================================================================================================
50
// UniverseReplicationInfo
51
// ================================================================================================
52
53
Result<std::shared_ptr<CDCRpcTasks>> UniverseReplicationInfo::GetOrCreateCDCRpcTasks(
54
2
    google::protobuf::RepeatedPtrField<HostPortPB> producer_masters) {
55
2
  std::vector<HostPort> hp;
56
2
  HostPortsFromPBs(producer_masters, &hp);
57
2
  std::string master_addrs = HostPort::ToCommaSeparatedString(hp);
58
59
2
  std::lock_guard<decltype(lock_)> l(lock_);
60
2
  if (cdc_rpc_tasks_ != nullptr) {
61
    // Master Addresses changed, update YBClient with new retry logic.
62
0
    if (master_addrs_ != master_addrs) {
63
0
      if (cdc_rpc_tasks_->UpdateMasters(master_addrs).ok()) {
64
0
        master_addrs_ = master_addrs;
65
0
      }
66
0
    }
67
0
    return cdc_rpc_tasks_;
68
0
  }
69
70
2
  auto result = CDCRpcTasks::CreateWithMasterAddrs(producer_id_, master_addrs);
71
2
  if (result.ok()) {
72
0
    cdc_rpc_tasks_ = *result;
73
0
    master_addrs_ = master_addrs;
74
0
  }
75
2
  return result;
76
2
}
77
78
2
std::string UniverseReplicationInfo::ToString() const {
79
2
  auto l = LockForRead();
80
2
  return strings::Substitute("$0 [data=$1] ", id(), l->pb.ShortDebugString());
81
2
}
82
83
0
void UniverseReplicationInfo::SetSetupUniverseReplicationErrorStatus(const Status& status) {
84
0
  std::lock_guard<decltype(lock_)> l(lock_);
85
0
  setup_universe_replication_error_ = status;
86
0
}
87
88
0
Status UniverseReplicationInfo::GetSetupUniverseReplicationErrorStatus() const {
89
0
  SharedLock<decltype(lock_)> l(lock_);
90
0
  return setup_universe_replication_error_;
91
0
}
92
93
////////////////////////////////////////////////////////////
94
// SnapshotInfo
95
////////////////////////////////////////////////////////////
96
97
0
SnapshotInfo::SnapshotInfo(SnapshotId id) : snapshot_id_(std::move(id)) {}
98
99
0
SysSnapshotEntryPB::State SnapshotInfo::state() const {
100
0
  return LockForRead()->state();
101
0
}
102
103
0
const std::string& SnapshotInfo::state_name() const {
104
0
  return LockForRead()->state_name();
105
0
}
106
107
0
std::string SnapshotInfo::ToString() const {
108
0
  return YB_CLASS_TO_STRING(snapshot_id);
109
0
}
110
111
0
bool SnapshotInfo::IsCreateInProgress() const {
112
0
  return LockForRead()->is_creating();
113
0
}
114
115
0
bool SnapshotInfo::IsRestoreInProgress() const {
116
0
  return LockForRead()->is_restoring();
117
0
}
118
119
0
bool SnapshotInfo::IsDeleteInProgress() const {
120
0
  return LockForRead()->is_deleting();
121
0
}
122
123
void SnapshotInfo::AddEntries(
124
0
    const TableDescription& table_description, std::unordered_set<NamespaceId>* added_namespaces) {
125
0
  SysSnapshotEntryPB& pb = mutable_metadata()->mutable_dirty()->pb;
126
0
  AddEntries(
127
0
      table_description, pb.mutable_entries(), pb.mutable_tablet_snapshots(), added_namespaces);
128
0
}
129
130
void SnapshotInfo::AddEntries(
131
    const TableDescription& table_description,
132
    google::protobuf::RepeatedPtrField<SysRowEntry>* out,
133
    google::protobuf::RepeatedPtrField<SysSnapshotEntryPB::TabletSnapshotPB>* tablet_infos,
134
30
    std::unordered_set<NamespaceId>* added_namespaces) {
135
  // Note: SysSnapshotEntryPB includes PBs for stored (1) namespaces (2) tables (3) tablets.
136
  // Add namespace entry.
137
30
  if (added_namespaces->emplace(table_description.namespace_info->id()).second) {
138
2
    TRACE("Locking namespace");
139
2
    AddInfoEntry(table_description.namespace_info.get(), out);
140
2
  }
141
142
  // Add table entry.
143
30
  {
144
30
    TRACE("Locking table");
145
30
    AddInfoEntry(table_description.table_info.get(), out);
146
30
  }
147
148
  // Add tablet entries.
149
80
  for (const scoped_refptr<TabletInfo>& tablet : table_description.tablet_infos) {
150
80
    SysSnapshotEntryPB::TabletSnapshotPB* const tablet_info =
151
80
        tablet_infos ? 
tablet_infos->Add()0
: nullptr;
152
153
80
    TRACE("Locking tablet");
154
80
    auto l = AddInfoEntry(tablet.get(), out);
155
156
80
    if (tablet_info) {
157
0
      tablet_info->set_id(tablet->id());
158
0
      tablet_info->set_state(SysSnapshotEntryPB::CREATING);
159
0
    }
160
80
  }
161
30
}
162
163
} // namespace master
164
} // namespace yb