aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/client/yt_poller.h
blob: 4f4e9eb7abeb4e64a6cf84968bb42edd31caa1d3 (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
#pragma once

#include <yt/cpp/mapreduce/common/fwd.h>

#include <yt/cpp/mapreduce/http/context.h>
#include <yt/cpp/mapreduce/http/requests.h>

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

#include <util/generic/list.h>
#include <util/system/mutex.h>
#include <util/system/thread.h>
#include <util/system/condvar.h>

namespace NYT {
namespace NDetail {

namespace NRawClient {
    class TRawBatchRequest;
}

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

class IYtPollerItem
    : public TThrRefBase
{
public:
    enum EStatus {
        PollContinue,
        PollBreak,
    };

public:
    virtual ~IYtPollerItem() = default;

    virtual void PrepareRequest(NRawClient::TRawBatchRequest* batchRequest) = 0;

    // Should return PollContinue if poller should continue polling this item.
    // Should return PollBreak if poller should stop polling this item.
    virtual EStatus OnRequestExecuted() = 0;

    virtual void OnItemDiscarded() = 0;

};
using IYtPollerItemPtr = ::TIntrusivePtr<IYtPollerItem>;

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

class TYtPoller
    : public TThrRefBase
{
public:
    TYtPoller(TClientContext context, const IClientRetryPolicyPtr& retryPolicy);
    ~TYtPoller();

    void Watch(IYtPollerItemPtr item);

    void Stop();

private:
    void DiscardQueuedItems();

    void WatchLoop();
    static void* WatchLoopProc(void*);

private:
    struct TItem;

    const TClientContext Context_;
    const IClientRetryPolicyPtr ClientRetryPolicy_;


    TList<IYtPollerItemPtr> InProgress_;
    TList<IYtPollerItemPtr> Pending_;

    TThread WaiterThread_;
    TMutex Lock_;
    TCondVar HasData_;

    bool IsRunning_ = true;
};

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

} // namespace NDetail
} // namespace NYT