aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/eventlog/threaded_eventlog.h
blob: 52382b856d184d66f689101a31c981a8eb997c5b (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
#pragma once

#include "eventlog.h"

#include <util/generic/string.h>
#include <util/thread/pool.h>

class TThreadedEventLog: public TEventLogWithSlave {
public:
    class TWrapper;
    using TOverflowCallback = std::function<void(TWrapper& wrapper)>;

    enum class EDegradationResult {
        ShouldWrite,
        ShouldDrop,
    };
    using TDegradationCallback = std::function<EDegradationResult(float fillFactor)>;

public:
    TThreadedEventLog(
        IEventLog& parentLog,
        size_t threadCount,
        size_t queueSize,
        TOverflowCallback cb,
        TDegradationCallback degradationCallback = {})
        : TEventLogWithSlave(parentLog)
        , LogSaver(TThreadPoolParams().SetThreadName("ThreadedEventLog"))
        , ThreadCount(threadCount)
        , QueueSize(queueSize)
        , OverflowCallback(std::move(cb))
        , DegradationCallback(std::move(degradationCallback))
    {
        Init();
    }

    TThreadedEventLog(
        const TEventLogPtr& parentLog,
        size_t threadCount,
        size_t queueSize,
        TOverflowCallback cb,
        TDegradationCallback degradationCallback = {})
        : TEventLogWithSlave(parentLog)
        , LogSaver(TThreadPoolParams().SetThreadName("ThreadedEventLog"))
        , ThreadCount(threadCount)
        , QueueSize(queueSize)
        , OverflowCallback(std::move(cb))
        , DegradationCallback(std::move(degradationCallback))
    {
        Init();
    }

    TThreadedEventLog(IEventLog& parentLog)
        : TThreadedEventLog(parentLog, 1, 0, TOverflowCallback())
    {
    }

    TThreadedEventLog(const TEventLogPtr& parentLog)
        : TThreadedEventLog(parentLog, 1, 0, TOverflowCallback())
    {
    }

    ~TThreadedEventLog() override {
        try {
            LogSaver.Stop();
        } catch (...) {
        }
    }

    void ReopenLog() override {
        TEventLogWithSlave::ReopenLog();
    }

    void CloseLog() override {
        LogSaver.Stop();
        TEventLogWithSlave::CloseLog();
    }

    void WriteFrame(TBuffer& buffer,
                    TEventTimestamp startTimestamp,
                    TEventTimestamp endTimestamp,
                    TWriteFrameCallbackPtr writeFrameCallback = nullptr,
                    TLogRecord::TMetaFlags metaFlags = {}) override {
        float fillFactor = 0.0f;
        if (Y_LIKELY(LogSaver.GetMaxQueueSize() > 0)) {
            fillFactor = static_cast<float>(LogSaver.Size()) / LogSaver.GetMaxQueueSize();
        }

        EDegradationResult status = EDegradationResult::ShouldWrite;
        if (DegradationCallback) {
            status = DegradationCallback(fillFactor);
        }
        if (Y_UNLIKELY(status == EDegradationResult::ShouldDrop)) {
            return;
        }

        THolder<TWrapper> wrapped;
        wrapped.Reset(new TWrapper(buffer, startTimestamp, endTimestamp, Slave(), writeFrameCallback, std::move(metaFlags)));

        if (LogSaver.Add(wrapped.Get())) {
            Y_UNUSED(wrapped.Release());
        } else if (OverflowCallback) {
            OverflowCallback(*wrapped);
        }
    }

private:
    void Init() {
        LogSaver.Start(ThreadCount, QueueSize);
    }

public:
    class TWrapper: public IObjectInQueue {
    public:
        TWrapper(TBuffer& buffer,
                 TEventTimestamp startTimestamp,
                 TEventTimestamp endTimestamp,
                 IEventLog& slave,
                 TWriteFrameCallbackPtr writeFrameCallback = nullptr,
                 TLogRecord::TMetaFlags metaFlags = {})
            : StartTimestamp(startTimestamp)
            , EndTimestamp(endTimestamp)
            , Slave(&slave)
            , WriteFrameCallback(writeFrameCallback)
            , MetaFlags(std::move(metaFlags))
        {
            Buffer.Swap(buffer);
        }

        void Process(void*) override {
            THolder<TWrapper> holder(this);

            WriteFrame();
        }

        void WriteFrame() {
            Slave->WriteFrame(Buffer, StartTimestamp, EndTimestamp, WriteFrameCallback, std::move(MetaFlags));
        }

    private:
        TBuffer Buffer;
        TEventTimestamp StartTimestamp;
        TEventTimestamp EndTimestamp;
        IEventLog* Slave;
        TWriteFrameCallbackPtr WriteFrameCallback;
        TLogRecord::TMetaFlags MetaFlags;
    };

private:
    TThreadPool LogSaver;
    const size_t ThreadCount;
    const size_t QueueSize;
    const TOverflowCallback OverflowCallback;
    const TDegradationCallback DegradationCallback;
};