aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/http/http_client.cpp
blob: 7e9d761c3cfb56d4d44a6543ae19d7f7cdcac560 (plain) (blame)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#include "http_client.h"

#include "abortable_http_response.h"
#include "core.h"
#include "helpers.h"
#include "http.h"

#include <yt/cpp/mapreduce/interface/config.h>

#include <yt/cpp/mapreduce/interface/logging/yt_log.h>

#include <yt/yt/core/concurrency/thread_pool_poller.h>

#include <yt/yt/core/http/client.h>
#include <yt/yt/core/http/config.h>
#include <yt/yt/core/http/http.h>

#include <yt/yt/core/https/client.h>
#include <yt/yt/core/https/config.h>

#include <library/cpp/yson/node/node_io.h>

namespace NYT::NHttpClient {

namespace {

TString CreateHost(TStringBuf host, TStringBuf port)
{
    if (!port.empty()) {
        return Format("%v:%v", host, port);
    }

    return TString(host);
}

TMaybe<TErrorResponse> GetErrorResponse(const TString& hostName, const TString& requestId, const NHttp::IResponsePtr& response)
{
    auto httpCode = response->GetStatusCode();
    if (httpCode == NHttp::EStatusCode::OK || httpCode == NHttp::EStatusCode::Accepted) {
        return {};
    }

    TErrorResponse errorResponse(static_cast<int>(httpCode), requestId);

    auto logAndSetError = [&] (const TString& rawError) {
        YT_LOG_ERROR("RSP %v - HTTP %v - %v",
            requestId,
            httpCode,
            rawError.data());
        errorResponse.SetRawError(rawError);
    };

    switch (httpCode) {
        case NHttp::EStatusCode::TooManyRequests:
            logAndSetError("request rate limit exceeded");
            break;

        case NHttp::EStatusCode::InternalServerError:
            logAndSetError("internal error in proxy " + hostName);
            break;

        default: {
            TStringStream httpHeaders;
            httpHeaders << "HTTP headers (";
            for (const auto& [headerName, headerValue] : response->GetHeaders()->Dump()) {
                httpHeaders << headerName << ": " << headerValue << "; ";
            }
            httpHeaders << ")";

            auto errorString = Sprintf("RSP %s - HTTP %d - %s",
                requestId.data(),
                static_cast<int>(httpCode),
                httpHeaders.Str().data());

            YT_LOG_ERROR("%v",
                errorString.data());

            if (auto errorHeader = response->GetHeaders()->Find("X-YT-Error")) {
                errorResponse.ParseFromJsonError(*errorHeader);
                if (errorResponse.IsOk()) {
                    return Nothing();
                }
                return errorResponse;
            }

            errorResponse.SetRawError(
                    errorString + " - X-YT-Error is missing in headers");
            break;
        }
    }

    return errorResponse;
}

void CheckErrorResponse(const TString& hostName, const TString& requestId, const NHttp::IResponsePtr& response)
{
    auto errorResponse = GetErrorResponse(hostName, requestId, response);
    if (errorResponse) {
        throw *errorResponse;
    }
}

} // namespace

////////////////////////////////////////////////////////////////////////////////

class TDefaultHttpResponse
    : public IHttpResponse
{
public:
    TDefaultHttpResponse(std::unique_ptr<THttpRequest> request)
        : Request_(std::move(request))
    { }

    int GetStatusCode() override
    {
        return Request_->GetHttpCode();
    }

    IInputStream* GetResponseStream() override
    {
        return Request_->GetResponseStream();
    }

    TString GetResponse() override
    {
        return Request_->GetResponse();
    }

    TString GetRequestId() const override
    {
        return Request_->GetRequestId();
    }

private:
    std::unique_ptr<THttpRequest> Request_;
};

class TDefaultHttpRequest
    : public IHttpRequest
{
public:
    TDefaultHttpRequest(std::unique_ptr<THttpRequest> request, IOutputStream* stream)
        : Request_(std::move(request))
        , Stream_(stream)
    { }

    IOutputStream* GetStream() override
    {
        return Stream_;
    }

    IHttpResponsePtr Finish() override
    {
        Request_->FinishRequest();
        return std::make_unique<TDefaultHttpResponse>(std::move(Request_));
    }

private:
    std::unique_ptr<THttpRequest> Request_;
    IOutputStream* Stream_;
};

class TDefaultHttpClient
    : public IHttpClient
{
public:
    IHttpResponsePtr Request(const TString& url, const TString& requestId, const THttpConfig& config, const THttpHeader& header, TMaybe<TStringBuf> body) override
    {
        auto urlRef = NHttp::ParseUrl(url);
        auto host = CreateHost(urlRef.Host, urlRef.PortStr);

        auto request = std::make_unique<THttpRequest>(requestId, host, header, config.SocketTimeout);

        request->SmallRequest(body);
        return std::make_unique<TDefaultHttpResponse>(std::move(request));
    }

    IHttpRequestPtr StartRequest(const TString& url, const TString& requestId, const THttpConfig& config, const THttpHeader& header) override
    {
        auto urlRef = NHttp::ParseUrl(url);
        auto host = CreateHost(urlRef.Host, urlRef.PortStr);

        auto request = std::make_unique<THttpRequest>(requestId, host, header, config.SocketTimeout);

        auto stream = request->StartRequest();
        return std::make_unique<TDefaultHttpRequest>(std::move(request), stream);
    }
};

////////////////////////////////////////////////////////////////////////////////

struct TCoreRequestContext
{
    TString HostName;
    TString Url;
    TString RequestId;
    bool LogResponse;
    TInstant StartTime;
    TString LoggedAttributes;
};

class TCoreHttpResponse
    : public IHttpResponse
{
public:
    TCoreHttpResponse(
        TCoreRequestContext context,
        NHttp::IResponsePtr response)
        : Context_(std::move(context))
        , Response_(std::move(response))
    { }

    int GetStatusCode() override
    {
        return static_cast<int>(Response_->GetStatusCode());
    }

    IInputStream* GetResponseStream() override
    {
        if (!Stream_) {
            auto stream = std::make_unique<TWrappedStream>(
                NConcurrency::CreateSyncAdapter(NConcurrency::CreateCopyingAdapter(Response_), NConcurrency::EWaitForStrategy::WaitFor),
                Response_,
                Context_.RequestId);
            CheckErrorResponse(Context_.HostName, Context_.RequestId, Response_);

            if (TConfig::Get()->UseAbortableResponse) {
                Y_ABORT_UNLESS(!Context_.Url.empty());
                Stream_ = std::make_unique<TAbortableCoreHttpResponse>(std::move(stream), Context_.Url);
            } else {
                Stream_ = std::move(stream);
            }
        }

        return Stream_.get();
    }

    TString GetResponse() override
    {
        auto result = GetResponseStream()->ReadAll();

        TStringStream loggedAttributes;
        loggedAttributes
            << "Time: " << TInstant::Now() - Context_.StartTime << "; "
            << "HostName: " << Context_.HostName << "; "
            << Context_.LoggedAttributes;

        if (Context_.LogResponse) {
            constexpr auto sizeLimit = 1 << 7;
            YT_LOG_DEBUG("RSP %v - received response (Response: '%v'; %v)",
                Context_.RequestId,
                TruncateForLogs(result, sizeLimit),
                loggedAttributes.Str());
        } else {
            YT_LOG_DEBUG("RSP %v - received response of %v bytes (%v)",
                Context_.RequestId,
                result.size(),
                loggedAttributes.Str());
        }
        return result;
    }

    TString GetRequestId() const override
    {
        return Context_.RequestId;
    }

private:
    class TWrappedStream
        : public IInputStream
    {
    public:
        TWrappedStream(std::unique_ptr<IInputStream> underlying, NHttp::IResponsePtr response, TString requestId)
            : Underlying_(std::move(underlying))
            , Response_(std::move(response))
            , RequestId_(std::move(requestId))
        { }

    protected:
        size_t DoRead(void* buf, size_t len) override
        {
            size_t read = Underlying_->Read(buf, len);

            if (read == 0 && len != 0) {
                CheckTrailers(Response_->GetTrailers());
            }
            return read;
        }

        size_t DoSkip(size_t len) override
        {
            size_t skipped = Underlying_->Skip(len);
            if (skipped == 0 && len != 0) {
                CheckTrailers(Response_->GetTrailers());
            }
            return skipped;
        }

    private:
        void CheckTrailers(const NHttp::THeadersPtr& trailers)
        {
            if (auto errorResponse = ParseError(trailers)) {
                errorResponse->SetIsFromTrailers(true);
                YT_LOG_ERROR("RSP %v - %v",
                    RequestId_,
                    errorResponse.GetRef().what());
                ythrow errorResponse.GetRef();
            }
        }

        TMaybe<TErrorResponse> ParseError(const NHttp::THeadersPtr& headers)
        {
            if (auto errorHeader = headers->Find("X-YT-Error")) {
                TErrorResponse errorResponse(static_cast<int>(Response_->GetStatusCode()), RequestId_);
                errorResponse.ParseFromJsonError(*errorHeader);
                if (errorResponse.IsOk()) {
                    return Nothing();
                }
                return errorResponse;
            }
            return Nothing();
        }

    private:
        std::unique_ptr<IInputStream> Underlying_;
        NHttp::IResponsePtr Response_;
        TString RequestId_;
    };

private:
    TCoreRequestContext Context_;
    NHttp::IResponsePtr Response_;
    std::unique_ptr<IInputStream> Stream_;
};

class TCoreHttpRequest
    : public IHttpRequest
{
public:
    TCoreHttpRequest(TCoreRequestContext context, NHttp::IActiveRequestPtr activeRequest)
        : Context_(std::move(context))
        , ActiveRequest_(std::move(activeRequest))
        , Stream_(NConcurrency::CreateBufferedSyncAdapter(ActiveRequest_->GetRequestStream()))
        , WrappedStream_(this, Stream_.get())
    { }

    IOutputStream* GetStream() override
    {
        return &WrappedStream_;
    }

    IHttpResponsePtr Finish() override
    {
        WrappedStream_.Flush();
        auto response = ActiveRequest_->Finish().Get().ValueOrThrow();
        return std::make_unique<TCoreHttpResponse>(std::move(Context_), std::move(response));
    }

    IHttpResponsePtr FinishWithError()
    {
        auto response = ActiveRequest_->GetResponse();
        return std::make_unique<TCoreHttpResponse>(std::move(Context_), std::move(response));
    }

private:
    class TWrappedStream
        : public IOutputStream
    {
    public:
        TWrappedStream(TCoreHttpRequest* httpRequest, IOutputStream* underlying)
            : HttpRequest_(httpRequest)
            , Underlying_(underlying)
        { }

    private:
        void DoWrite(const void* buf, size_t len) override
        {
            WrapWriteFunc([&] {
                Underlying_->Write(buf, len);
            });
        }

        void DoWriteV(const TPart* parts, size_t count) override
        {
            WrapWriteFunc([&] {
                Underlying_->Write(parts, count);
            });
        }

        void DoWriteC(char ch) override
        {
            WrapWriteFunc([&] {
                Underlying_->Write(ch);
            });
        }

        void DoFlush() override
        {
            WrapWriteFunc([&] {
                Underlying_->Flush();
            });
        }

        void DoFinish() override
        {
            WrapWriteFunc([&] {
                Underlying_->Finish();
            });
        }

        void WrapWriteFunc(std::function<void()> func)
        {
            CheckErrorState();
            try {
                func();
            } catch (const std::exception&) {
                HandleWriteException();
            }
        }

        // In many cases http proxy stops reading request and resets connection
        // if error has happend. This function tries to read error response
        // in such cases.
        void HandleWriteException() {
            Y_ABORT_UNLESS(WriteError_ == nullptr);
            WriteError_ = std::current_exception();
            Y_ABORT_UNLESS(WriteError_ != nullptr);
            try {
                HttpRequest_->FinishWithError()->GetResponseStream();
            } catch (const TErrorResponse &) {
                throw;
            } catch (...) {
            }
            std::rethrow_exception(WriteError_);
        }

        void CheckErrorState()
        {
            if (WriteError_) {
                std::rethrow_exception(WriteError_);
            }
        }

    private:
        TCoreHttpRequest* const HttpRequest_;
        IOutputStream* Underlying_;
        std::exception_ptr WriteError_;
    };

private:
    TCoreRequestContext Context_;
    NHttp::IActiveRequestPtr ActiveRequest_;
    std::unique_ptr<IOutputStream> Stream_;
    TWrappedStream WrappedStream_;
};

class TCoreHttpClient
    : public IHttpClient
{
public:
    TCoreHttpClient(bool useTLS, const TConfigPtr& config)
        : Poller_(NConcurrency::CreateThreadPoolPoller(1, "http_poller"))  // TODO(nadya73): YT-18363: move threads count to config
    {
        if (useTLS) {
            auto httpsConfig = NYT::New<NYT::NHttps::TClientConfig>();
            httpsConfig->MaxIdleConnections = config->ConnectionPoolSize;
            Client_ = NHttps::CreateClient(httpsConfig, Poller_);
        } else {
            auto httpConfig = NYT::New<NYT::NHttp::TClientConfig>();
            httpConfig->MaxIdleConnections = config->ConnectionPoolSize;
            Client_ = NHttp::CreateClient(httpConfig, Poller_);
        }
    }

    IHttpResponsePtr Request(const TString& url, const TString& requestId, const THttpConfig& /*config*/, const THttpHeader& header, TMaybe<TStringBuf> body) override
    {
        TCoreRequestContext context = CreateContext(url, requestId, header);

        // TODO(nadya73): YT-18363: pass socket timeouts from THttpConfig

        NHttp::IResponsePtr response;

        auto logRequest = [&](bool includeParameters) {
            LogRequest(header, url, includeParameters, requestId, context.HostName);
            context.LoggedAttributes = GetLoggedAttributes(header, url, includeParameters, 128);
        };

        if (!body && (header.GetMethod() == "PUT" || header.GetMethod() == "POST")) {
            const auto& parameters = header.GetParameters();
            auto parametersStr = NodeToYsonString(parameters);

            bool includeParameters = false;
            auto headers = header.GetHeader(context.HostName, requestId, includeParameters).Get();

            logRequest(includeParameters);

            auto activeRequest = StartRequestImpl(header.GetMethod(), url, headers);

            activeRequest->GetRequestStream()->Write(TSharedRef::FromString(parametersStr)).Get().ThrowOnError();
            response = activeRequest->Finish().Get().ValueOrThrow();
        } else {
            auto bodyRef = TSharedRef::FromString(TString(body ? *body : ""));
            bool includeParameters = true;
            auto headers = header.GetHeader(context.HostName, requestId, includeParameters).Get();

            logRequest(includeParameters);

            if (header.GetMethod() == "GET") {
                response = RequestImpl(header.GetMethod(), url, headers, bodyRef);
            } else {
                auto activeRequest = StartRequestImpl(header.GetMethod(), url, headers);

                auto request = std::make_unique<TCoreHttpRequest>(std::move(context), std::move(activeRequest));
                if (body) {
                    request->GetStream()->Write(*body);
                }
                return request->Finish();
            }
        }

        return std::make_unique<TCoreHttpResponse>(std::move(context), std::move(response));
    }

    IHttpRequestPtr StartRequest(const TString& url, const TString& requestId, const THttpConfig& /*config*/, const THttpHeader& header) override
    {
        TCoreRequestContext context = CreateContext(url, requestId, header);

        LogRequest(header, url, true, requestId, context.HostName);
        context.LoggedAttributes = GetLoggedAttributes(header, url, true, 128);

        auto headers = header.GetHeader(context.HostName, requestId, true).Get();
        auto activeRequest = StartRequestImpl(header.GetMethod(), url, headers);

        return std::make_unique<TCoreHttpRequest>(std::move(context), std::move(activeRequest));
    }

private:
    TCoreRequestContext CreateContext(const TString& url, const TString& requestId, const THttpHeader& header)
    {
        TCoreRequestContext context;
        context.Url = url;
        context.RequestId = requestId;

        auto urlRef = NHttp::ParseUrl(url);
        context.HostName = CreateHost(urlRef.Host, urlRef.PortStr);

        context.LogResponse = false;
        auto outputFormat = header.GetOutputFormat();
        if (outputFormat && outputFormat->IsTextYson()) {
            context.LogResponse = true;
        }
        context.StartTime = TInstant::Now();
        return context;
    }

    NHttp::IResponsePtr RequestImpl(const TString& method, const TString& url, const NHttp::THeadersPtr& headers, const TSharedRef& body)
    {
        if (method == "GET") {
            return Client_->Get(url, headers).Get().ValueOrThrow();
        } else if (method == "POST") {
            return Client_->Post(url, body, headers).Get().ValueOrThrow();
        } else if (method == "PUT") {
            return Client_->Put(url, body, headers).Get().ValueOrThrow();
        } else {
            YT_LOG_FATAL("Unsupported http method (Method: %v, Url: %v)",
                method,
                url);
        }
    }

    NHttp::IActiveRequestPtr StartRequestImpl(const TString& method, const TString& url, const NHttp::THeadersPtr& headers)
    {
        if (method == "POST") {
            return Client_->StartPost(url, headers).Get().ValueOrThrow();
        } else if (method == "PUT") {
            return Client_->StartPut(url, headers).Get().ValueOrThrow();
        } else {
            YT_LOG_FATAL("Unsupported http method (Method: %v, Url: %v)",
                method,
                url);
        }
    }

    NConcurrency::IThreadPoolPollerPtr Poller_;
    NHttp::IClientPtr Client_;
};

////////////////////////////////////////////////////////////////////////////////

IHttpClientPtr CreateDefaultHttpClient()
{
    return std::make_shared<TDefaultHttpClient>();
}

IHttpClientPtr CreateCoreHttpClient(bool useTLS, const TConfigPtr& config)
{
    return std::make_shared<TCoreHttpClient>(useTLS, config);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT::NHttpClient