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
|
#include "file_reader.h"
#include "transaction.h"
#include "transaction_pinger.h"
#include <yt/cpp/mapreduce/common/helpers.h>
#include <yt/cpp/mapreduce/common/retry_lib.h>
#include <yt/cpp/mapreduce/common/wait_proxy.h>
#include <yt/cpp/mapreduce/interface/config.h>
#include <yt/cpp/mapreduce/interface/tvm.h>
#include <yt/cpp/mapreduce/interface/logging/yt_log.h>
#include <yt/cpp/mapreduce/io/helpers.h>
#include <yt/cpp/mapreduce/http/helpers.h>
#include <yt/cpp/mapreduce/http/http.h>
#include <yt/cpp/mapreduce/http/http_client.h>
#include <yt/cpp/mapreduce/http/retry_request.h>
#include <yt/cpp/mapreduce/raw_client/raw_requests.h>
namespace NYT {
namespace NDetail {
using ::ToString;
////////////////////////////////////////////////////////////////////////////////
static TMaybe<ui64> GetEndOffset(const TFileReaderOptions& options) {
if (options.Length_) {
return options.Offset_.GetOrElse(0) + *options.Length_;
} else {
return Nothing();
}
}
////////////////////////////////////////////////////////////////////////////////
TStreamReaderBase::TStreamReaderBase(
IClientRetryPolicyPtr clientRetryPolicy,
ITransactionPingerPtr transactionPinger,
const TClientContext& context,
const TTransactionId& transactionId)
: Context_(context)
, ClientRetryPolicy_(std::move(clientRetryPolicy))
, ReadTransaction_(MakeHolder<TPingableTransaction>(
ClientRetryPolicy_,
context,
transactionId,
transactionPinger->GetChildTxPinger(),
TStartTransactionOptions()))
{ }
TStreamReaderBase::~TStreamReaderBase() = default;
TYPath TStreamReaderBase::Snapshot(const TYPath& path)
{
return NYT::Snapshot(ClientRetryPolicy_, Context_, ReadTransaction_->GetId(), path);
}
TString TStreamReaderBase::GetActiveRequestId() const
{
if (Response_) {
return Response_->GetRequestId();;
} else {
return "<no-active-request>";
}
}
size_t TStreamReaderBase::DoRead(void* buf, size_t len)
{
const int retryCount = Context_.Config->ReadRetryCount;
for (int attempt = 1; attempt <= retryCount; ++attempt) {
try {
if (!Input_) {
Response_ = Request(Context_, ReadTransaction_->GetId(), CurrentOffset_);
Input_ = Response_->GetResponseStream();
}
if (len == 0) {
return 0;
}
const size_t read = Input_->Read(buf, len);
CurrentOffset_ += read;
return read;
} catch (TErrorResponse& e) {
YT_LOG_ERROR("RSP %v - failed: %v (attempt %v of %v)",
GetActiveRequestId(),
e.what(),
attempt,
retryCount);
if (!IsRetriable(e) || attempt == retryCount) {
throw;
}
NDetail::TWaitProxy::Get()->Sleep(GetBackoffDuration(e, Context_.Config));
} catch (std::exception& e) {
YT_LOG_ERROR("RSP %v - failed: %v (attempt %v of %v)",
GetActiveRequestId(),
e.what(),
attempt,
retryCount);
// Invalidate connection.
Response_.reset();
if (attempt == retryCount) {
throw;
}
NDetail::TWaitProxy::Get()->Sleep(GetBackoffDuration(e, Context_.Config));
}
Input_ = nullptr;
}
Y_UNREACHABLE(); // we should either return or throw from loop above
}
////////////////////////////////////////////////////////////////////////////////
TFileReader::TFileReader(
const TRichYPath& path,
IClientRetryPolicyPtr clientRetryPolicy,
ITransactionPingerPtr transactionPinger,
const TClientContext& context,
const TTransactionId& transactionId,
const TFileReaderOptions& options)
: TStreamReaderBase(std::move(clientRetryPolicy), std::move(transactionPinger), context, transactionId)
, FileReaderOptions_(options)
, Path_(path)
, StartOffset_(FileReaderOptions_.Offset_.GetOrElse(0))
, EndOffset_(GetEndOffset(FileReaderOptions_))
{
Path_.Path_ = TStreamReaderBase::Snapshot(Path_.Path_);
}
NHttpClient::IHttpResponsePtr TFileReader::Request(const TClientContext& context, const TTransactionId& transactionId, ui64 readBytes)
{
const ui64 currentOffset = StartOffset_ + readBytes;
TString hostName = GetProxyForHeavyRequest(context);
THttpHeader header("GET", GetReadFileCommand(context.Config->ApiVersion));
if (context.ServiceTicketAuth) {
header.SetServiceTicket(context.ServiceTicketAuth->Ptr->IssueServiceTicket());
} else {
header.SetToken(context.Token);
}
if (context.ImpersonationUser) {
header.SetImpersonationUser(*context.ImpersonationUser);
}
UpdateHeaderForProxyIfNeed(hostName, context, header);
header.AddTransactionId(transactionId);
header.SetOutputFormat(TMaybe<TFormat>()); // Binary format
if (EndOffset_) {
Y_ABORT_UNLESS(*EndOffset_ >= currentOffset);
FileReaderOptions_.Length(*EndOffset_ - currentOffset);
}
FileReaderOptions_.Offset(currentOffset);
header.MergeParameters(FormIORequestParameters(Path_, FileReaderOptions_));
header.SetResponseCompression(ToString(context.Config->AcceptEncoding));
auto requestId = CreateGuidAsString();
NHttpClient::IHttpResponsePtr response;
try {
response = context.HttpClient->Request(GetFullUrl(hostName, context, header), requestId, header);
} catch (const std::exception& ex) {
LogRequestError(requestId, header, ex.what(), "");
throw;
}
YT_LOG_DEBUG("RSP %v - file stream",
requestId);
return response;
}
////////////////////////////////////////////////////////////////////////////////
TBlobTableReader::TBlobTableReader(
const TYPath& path,
const TKey& key,
IClientRetryPolicyPtr retryPolicy,
ITransactionPingerPtr transactionPinger,
const TClientContext& context,
const TTransactionId& transactionId,
const TBlobTableReaderOptions& options)
: TStreamReaderBase(std::move(retryPolicy), std::move(transactionPinger), context, transactionId)
, Key_(key)
, Options_(options)
{
Path_ = TStreamReaderBase::Snapshot(path);
}
NHttpClient::IHttpResponsePtr TBlobTableReader::Request(const TClientContext& context, const TTransactionId& transactionId, ui64 readBytes)
{
TString hostName = GetProxyForHeavyRequest(context);
THttpHeader header("GET", "read_blob_table");
if (context.ServiceTicketAuth) {
header.SetServiceTicket(context.ServiceTicketAuth->Ptr->IssueServiceTicket());
} else {
header.SetToken(context.Token);
}
if (context.ImpersonationUser) {
header.SetImpersonationUser(*context.ImpersonationUser);
}
UpdateHeaderForProxyIfNeed(hostName, context, header);
header.AddTransactionId(transactionId);
header.SetOutputFormat(TMaybe<TFormat>()); // Binary format
const ui64 currentOffset = Options_.Offset_ + readBytes;
const i64 startPartIndex = currentOffset / Options_.PartSize_;
const ui64 skipBytes = currentOffset - Options_.PartSize_ * startPartIndex;
auto lowerLimitKey = Key_;
lowerLimitKey.Parts_.push_back(startPartIndex);
auto upperLimitKey = Key_;
upperLimitKey.Parts_.push_back(std::numeric_limits<i64>::max());
TNode params = PathToParamNode(TRichYPath(Path_).AddRange(TReadRange()
.LowerLimit(TReadLimit().Key(lowerLimitKey))
.UpperLimit(TReadLimit().Key(upperLimitKey))));
params["start_part_index"] = TNode(startPartIndex);
params["offset"] = skipBytes;
if (Options_.PartIndexColumnName_) {
params["part_index_column_name"] = *Options_.PartIndexColumnName_;
}
if (Options_.DataColumnName_) {
params["data_column_name"] = *Options_.DataColumnName_;
}
params["part_size"] = Options_.PartSize_;
header.MergeParameters(params);
header.SetResponseCompression(ToString(context.Config->AcceptEncoding));
auto requestId = CreateGuidAsString();
NHttpClient::IHttpResponsePtr response;
try {
response = context.HttpClient->Request(GetFullUrl(hostName, context, header), requestId, header);
} catch (const std::exception& ex) {
LogRequestError(requestId, header, ex.what(), "");
throw;
}
YT_LOG_DEBUG("RSP %v - blob table stream",
requestId);
return response;
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NDetail
} // namespace NYT
|