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
|
#include "yql_yt_native_folders.h"
#include <yt/cpp/mapreduce/interface/error_codes.h>
#include <yql/essentials/utils/log/log.h>
#include <yql/essentials/providers/common/proto/gateways_config.pb.h>
#include <yt/yql/providers/yt/gateway/lib/yt_helpers.h>
namespace NYql::NNative {
using namespace NYT;
using namespace NCommon;
using namespace NKikimr;
using namespace NKikimr::NMiniKQL;
using namespace NNodes;
using namespace NThreading;
TString GetTypeFromAttributes(const NYT::TNode& attributes, bool getDocumentType) {
if (!attributes.HasKey("type")) {
return "unknown";
}
const auto type = attributes["type"].AsString();
if (getDocumentType && "document" == type) {
if (!attributes.HasKey("_yql_type")) {
return "unknown";
}
return attributes["_yql_type"].AsString();
} else {
return type;
}
}
TString GetTypeFromNode(const NYT::TNode& node, bool getDocumentType) {
if (!node.HasAttributes()) {
return "unknown";
}
return GetTypeFromAttributes(node.GetAttributes(), getDocumentType);
}
TMaybe<TVector<IYtGateway::TBatchFolderResult::TFolderItem>> MaybeGetFolderFromCache(TTransactionCache::TEntry::TPtr entry, TStringBuf cacheKey) {
TVector<IYtGateway::TBatchFolderResult::TFolderItem> items;
with_lock(entry->Lock_) {
const auto listPtr = entry->FolderCache.FindPtr(cacheKey);
if (listPtr) {
YQL_CLOG(INFO, ProviderYt) << "Found folder in cache with key ('" << cacheKey << "') with " << listPtr->size() << " items";
for (auto& el : *listPtr) {
IYtGateway::TBatchFolderResult::TFolderItem item;
std::tie(item.Type, item.Path, item.Attributes) = el;
items.emplace_back(std::move(item));
}
return items;
}
}
return {};
}
TMaybe<TFileLinkPtr> MaybeGetFilePtrFromCache(TTransactionCache::TEntry::TPtr entry, const IYtGateway::TBatchFolderOptions::TFolderPrefixAttrs& folder) {
const auto cacheKey = std::accumulate(folder.AttrKeys.begin(), folder.AttrKeys.end(), folder.Prefix,
[] (TString&& str, const TString& arg) {
return str + "&" + arg;
});
with_lock(entry->Lock_) {
const auto filePtr = entry->FolderFilePtrCache.FindPtr(cacheKey);
if (filePtr) {
return filePtr->Get();
}
}
return {};
}
TAttributeFilter MakeAttrFilter(const TSet<TString>& attributes, bool isResolvingLink) {
NYT::TAttributeFilter filter;
for (const auto& attr : attributes) {
filter.AddAttribute(attr);
}
if (!isResolvingLink) {
filter.AddAttribute("target_path");
filter.AddAttribute("broken");
}
filter.AddAttribute("type");
return filter;
}
IYtGateway::TBatchFolderResult::TFolderItem MakeFolderItem(const NYT::TNode& node, const TString& path) {
IYtGateway::TBatchFolderResult::TFolderItem item;
item.Attributes = NYT::TNode::CreateMap();
for (const auto& attr: node.GetAttributes().AsMap()) {
if (attr.first == "type") {
continue;
}
item.Attributes[attr.first] = attr.second;
}
item.Type = GetTypeFromNode(node, false);
item.Path = path.StartsWith(NYT::TConfig::Get()->Prefix)
? path.substr(NYT::TConfig::Get()->Prefix.size())
: path;
return item;
}
const TTransactionCache::TEntry::TFolderCache::value_type& StoreResInCache(const TTransactionCache::TEntry::TPtr& entry, TVector<IYtGateway::TBatchFolderResult::TFolderItem>&& items, const TString& cacheKey) {
std::vector<std::tuple<TString, TString, NYT::TNode>> cache;
for (const auto& item : items) {
cache.emplace_back(std::move(item.Type), std::move(item.Path), std::move(item.Attributes));
}
with_lock(entry->Lock_) {
const auto [it, _] = entry->FolderCache.insert_or_assign(cacheKey, std::move(cache));
return *it;
}
}
TFileLinkPtr SaveItemsToTempFile(const TExecContext<IYtGateway::TBatchFolderOptions>::TPtr& execCtx, const TVector<IYtGateway::TFolderResult::TFolderItem>& folderItems) {
const TString file = execCtx->FileStorage_->GetTemp() / GetGuidAsString(execCtx->Session_->RandomProvider_->GenGuid());
YQL_CLOG(INFO, ProviderYt) << "Folder limit exceeded. Writing items to file " << file;
auto out = MakeHolder<TOFStream>(file);
for (auto& item: folderItems) {
::SaveMany(out.Get(), item.Type, item.Path, item.Attributes);
}
::SaveSize(out.Get(), 0);
out.Destroy();
return CreateFakeFileLink(file, "", true);
}
IYtGateway::TBatchFolderResult ExecResolveLinks(const TExecContext<IYtGateway::TResolveOptions>::TPtr& execCtx) {
try {
auto batchGet = execCtx->GetEntry()->Tx->CreateBatchRequest();
TVector<TFuture<IYtGateway::TBatchFolderResult::TFolderItem>> batchRes;
for (const auto& [item, reqAttributes]: execCtx->Options_.Items()) {
if (item.Type != "link") {
batchRes.push_back(MakeFuture<IYtGateway::TBatchFolderResult::TFolderItem>(std::move(item)));
continue;
}
if (item.Attributes["broken"].AsBool()) {
continue;
}
const auto& targetPath = item.Attributes["target_path"].AsString();
const auto& path = item.Path;
const auto attrFilter = MakeAttrFilter(reqAttributes, /* isResolvingLink */ true);
batchRes.push_back(
batchGet->Get(targetPath, TGetOptions().AttributeFilter(attrFilter))
.Apply([path, pos = execCtx->Options_.Pos()] (const auto& f) {
try {
const auto linkNode = f.GetValue();
return MakeFolderItem(linkNode, path);
} catch (const NYT::TErrorResponse& e) {
return MakeFolderItem(NYT::TNode::CreateMap(), path);
}
})
);
}
IYtGateway::TBatchFolderResult res;
res.SetSuccess();
if (batchRes.empty()) {
YQL_CLOG(INFO, ProviderYt) << "Batch get result is empty";
return res;
}
res.Items.reserve(batchRes.size());
batchGet->ExecuteBatch();
WaitAll(batchRes).Wait();
for (auto& f : batchRes) {
res.Items.push_back(f.ExtractValue());
}
return res;
}
catch (...) {
return ResultFromCurrentException<IYtGateway::TBatchFolderResult>(execCtx->Options_.Pos());
}
}
IYtGateway::TBatchFolderResult ExecGetFolder(const TExecContext<IYtGateway::TBatchFolderOptions>::TPtr& execCtx) {
const auto entry = execCtx->GetOrCreateEntry();
auto batchList = entry->Tx->CreateBatchRequest();
TVector<TFuture<TVector<IYtGateway::TBatchFolderResult::TFolderItem>>> batchRes;
IYtGateway::TBatchFolderResult folderResult;
folderResult.SetSuccess();
for (const auto& folder : execCtx->Options_.Folders()) {
const auto cacheKey = std::accumulate(folder.AttrKeys.begin(), folder.AttrKeys.end(), folder.Prefix,
[] (TString&& str, const TString& arg) {
return str + "&" + arg;
});
YQL_CLOG(INFO, ProviderYt) << "Executing list command with prefix: " << folder.Prefix << " , cacheKey = " << cacheKey;
auto maybeCached = MaybeGetFolderFromCache(entry, cacheKey);
if (maybeCached) {
batchRes.push_back(MakeFuture<TVector<IYtGateway::TBatchFolderResult::TFolderItem>>(std::move(*maybeCached)));
continue;
}
const auto attrFilter = MakeAttrFilter(folder.AttrKeys, /* isResolvingLink */ false);
batchRes.push_back(
batchList->List(folder.Prefix, TListOptions().AttributeFilter(attrFilter))
.Apply([&folder, cacheKey = std::move(cacheKey), &entry] (const TFuture<NYT::TNode::TListType>& f)
-> TFuture<TVector<IYtGateway::TBatchFolderResult::TFolderItem>> {
TVector<IYtGateway::TBatchFolderResult::TFolderItem> folderItems;
try {
auto nodeList = f.GetValue();
folderItems.reserve(nodeList.size());
for (const auto& node : nodeList) {
TStringBuilder path;
if (!folder.Prefix.empty()) {
path << folder.Prefix << "/";
}
path << node.AsString();
folderItems.push_back(MakeFolderItem(node, path));
}
StoreResInCache(entry, std::move(folderItems), cacheKey);
return MakeFuture(std::move(folderItems));
}
catch (const NYT::TErrorResponse& e) {
if (e.GetError().ContainsErrorCode(NYT::NClusterErrorCodes::NYTree::ResolveError)) {
// Return empty list on missing path
YQL_CLOG(INFO, ProviderYt) << "Storing empty folder in cache with key ('" << cacheKey << "')";
StoreResInCache(entry, {}, cacheKey);
return MakeFuture<TVector<IYtGateway::TBatchFolderResult::TFolderItem>>({});
}
return MakeErrorFuture<TVector<IYtGateway::TBatchFolderResult::TFolderItem>>(std::current_exception());
}
catch (...) {
return MakeErrorFuture<TVector<IYtGateway::TBatchFolderResult::TFolderItem>>(std::current_exception());
}
})
);
}
TExecuteBatchOptions batchOptions;
if (batchRes.size() > 1) {
const size_t concurrency = execCtx->Options_.Config()->BatchListFolderConcurrency
.Get().GetOrElse(DEFAULT_BATCH_LIST_FOLDER_CONCURRENCY);
batchOptions.Concurrency(concurrency);
}
try {
batchList->ExecuteBatch(batchOptions);
WaitExceptionOrAll(batchRes).Wait();
for (auto& res : batchRes) {
const auto items = res.ExtractValue();
folderResult.Items.reserve(folderResult.Items.size() + items.size());
for (const auto& item : items) {
folderResult.Items.push_back(std::move(item));
}
}
}
catch (...) {
return ResultFromCurrentException<IYtGateway::TBatchFolderResult>(execCtx->Options_.Pos());
}
return folderResult;
}
} // NYql::NNative
|