1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
#include "table_mount_cache_detail.h"
#include "config.h"
#include <yt/yt/core/yson/string.h>
#include <yt/yt/core/ytree/convert.h>
#include <yt/yt/core/profiling/timing.h>
#include <library/cpp/yt/misc/hash.h>
namespace NYT::NTabletClient {
using namespace NConcurrency;
////////////////////////////////////////////////////////////////////////////////
static constexpr auto TabletCacheSweepPeriod = TDuration::Seconds(60);
///////////////////////////////////////////////////////////////////////////////
TTabletInfoCache::TTabletInfoCache(NLogging::TLogger logger)
: Logger(std::move(logger))
{ }
TTabletInfoPtr TTabletInfoCache::Find(TTabletId tabletId)
{
SweepExpiredEntries();
auto guard = ReaderGuard(MapLock_);
ProcessNextGCQueueEntry();
auto it = Map_.find(tabletId);
return it != Map_.end() ? it->second.Lock() : nullptr;
}
TTabletInfoPtr TTabletInfoCache::Insert(const TTabletInfoPtr& tabletInfo)
{
SweepExpiredEntries();
auto guard = WriterGuard(MapLock_);
ProcessNextGCQueueEntry();
typename decltype(Map_)::insert_ctx context;
auto it = Map_.find(tabletInfo->TabletId, context);
if (it != Map_.end()) {
if (auto existingTabletInfo = it->second.Lock()) {
if (tabletInfo->MountRevision < existingTabletInfo->MountRevision) {
THROW_ERROR_EXCEPTION(
EErrorCode::InvalidMountRevision,
"Tablet mount revision %x is outdated",
tabletInfo->MountRevision)
<< TErrorAttribute("tablet_id", tabletInfo->TabletId);
}
for (const auto& owner : existingTabletInfo->Owners) {
if (!owner.IsExpired()) {
tabletInfo->Owners.push_back(owner);
}
}
}
it->second = MakeWeak(tabletInfo);
} else {
Map_.emplace_direct(context, tabletInfo->TabletId, tabletInfo);
guard.Release();
auto gcGuard = Guard(GCLock_);
GCQueue_.push(tabletInfo->TabletId);
}
return tabletInfo;
}
void TTabletInfoCache::Clear()
{
decltype(Map_) other;
{
auto guard = WriterGuard(MapLock_);
other = std::move(Map_);
}
}
void TTabletInfoCache::SweepExpiredEntries()
{
auto now = NProfiling::GetCpuInstant();
auto deadline = ExpiredEntriesSweepDeadline_.load(std::memory_order::relaxed);
if (now < deadline) {
return;
}
if (!ExpiredEntriesSweepDeadline_.compare_exchange_strong(deadline, now + NProfiling::DurationToCpuDuration(TabletCacheSweepPeriod))) {
return;
}
decltype(ExpiredTabletIds_) expiredTabletIds;
{
auto gcGuard = Guard(GCLock_);
expiredTabletIds = std::move(ExpiredTabletIds_);
}
if (!expiredTabletIds.empty()) {
YT_LOG_DEBUG("Start sweeping expired tablet info (ExpiredTabletCount: %v)",
expiredTabletIds.size());
for (auto id : expiredTabletIds) {
auto guard = WriterGuard(MapLock_);
if (auto it = Map_.find(id); it) {
if (it->second.IsExpired()) {
Map_.erase(it);
continue;
}
guard.Release();
auto gcGuard = Guard(GCLock_);
GCQueue_.push(id);
}
}
YT_LOG_DEBUG("Finish sweeping expired tablet info");
}
}
void TTabletInfoCache::ProcessNextGCQueueEntry()
{
VERIFY_SPINLOCK_AFFINITY(MapLock_);
auto gcGuard = Guard(GCLock_);
if (!GCQueue_.empty()) {
const auto& id = GCQueue_.front();
if (auto it = Map_.find(id); it) {
if (it->second.IsExpired()) {
ExpiredTabletIds_.push_back(id);
} else {
GCQueue_.push(id);
}
}
GCQueue_.pop();
}
}
////////////////////////////////////////////////////////////////////////////////
TTableMountCacheBase::TTableMountCacheBase(
TTableMountCacheConfigPtr config,
NLogging::TLogger logger,
NProfiling::TProfiler profiler)
: TAsyncExpiringCache(
config,
logger.WithTag("Cache: TableMount"),
profiler)
, Logger(std::move(logger))
, TabletInfoCache_(Logger)
, Config_(std::move(config))
{ }
TFuture<TTableMountInfoPtr> TTableMountCacheBase::GetTableInfo(const NYPath::TYPath& path)
{
auto [future, requestInitialized] = TAsyncExpiringCache::GetExtended(path);
bool shouldThrow = false;
if (!requestInitialized && !future.IsSet()) {
auto guard = ReaderGuard(SpinLock_);
shouldThrow = Config_->RejectIfEntryIsRequestedButNotReady;
}
if (shouldThrow) {
// COMPAT(babenko): replace with TransientFailure error code.
THROW_ERROR_EXCEPTION(NRpc::EErrorCode::Unavailable,
"Mount info is unavailable, please try again")
<< TError(NTabletClient::EErrorCode::TableMountInfoNotReady,
"Table mount info is not ready, but has already been requested")
<< TErrorAttribute("path", path);
}
return future;
}
TTabletInfoPtr TTableMountCacheBase::FindTabletInfo(TTabletId tabletId)
{
return TabletInfoCache_.Find(tabletId);
}
void TTableMountCacheBase::InvalidateTablet(TTabletInfoPtr tabletInfo)
{
for (const auto& weakOwner : tabletInfo->Owners) {
if (auto owner = weakOwner.Lock()) {
InvalidateTable(owner);
}
}
}
std::pair<std::optional<TErrorCode>, TTabletInfoPtr> TTableMountCacheBase::InvalidateOnError(
const TError& error,
bool forceRetry)
{
static const std::vector<TErrorCode> retriableCodes = {
NTabletClient::EErrorCode::NoSuchTablet,
NTabletClient::EErrorCode::TabletNotMounted,
NTabletClient::EErrorCode::InvalidMountRevision,
NYTree::EErrorCode::ResolveError
};
if (!error.IsOK()) {
for (auto code : retriableCodes) {
if (auto retriableError = error.FindMatching(code)) {
auto tabletId = retriableError->Attributes().Find<TTabletId>("tablet_id");
if (!tabletId) {
continue;
}
auto isTabletUnmounted = retriableError->Attributes().Get<bool>("is_tablet_unmounted", false);
auto tabletInfo = FindTabletInfo(*tabletId);
if (tabletInfo) {
YT_LOG_DEBUG(error,
"Invalidating tablet in table mount cache "
"(TabletId: %v, CellId: %v, MountRevision: %x, IsTabletUnmounted: %v, Owners: %v)",
tabletInfo->TabletId,
tabletInfo->CellId,
tabletInfo->MountRevision,
isTabletUnmounted,
MakeFormattableView(tabletInfo->Owners, [] (auto* builder, const auto& weakOwner) {
if (auto owner = weakOwner.Lock()) {
FormatValue(builder, owner->Path, TStringBuf());
} else {
builder->AppendString(TStringBuf("<expired>"));
}
}));
InvalidateTablet(tabletInfo);
}
std::optional<TErrorCode> retryableErrorCode = code;
if (code == NTabletClient::EErrorCode::TabletNotMounted &&
isTabletUnmounted &&
!forceRetry)
{
retryableErrorCode = std::nullopt;
}
return std::make_pair(retryableErrorCode, tabletInfo);
}
}
}
return std::make_pair(std::nullopt, nullptr);
}
void TTableMountCacheBase::Clear()
{
TAsyncExpiringCache::Clear();
TabletInfoCache_.Clear();
YT_LOG_DEBUG("Table mount info cache cleared");
}
void TTableMountCacheBase::Reconfigure(TTableMountCacheConfigPtr config)
{
TAsyncExpiringCache::Reconfigure(config);
{
auto guard = WriterGuard(SpinLock_);
Config_ = config;
}
YT_LOG_DEBUG("Table mount info cache reconfigured (NewConfig: %v)",
NYson::ConvertToYsonString(config, NYson::EYsonFormat::Text).AsStringBuf());
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT::NTabletClient
|