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

#include <util/generic/ptr.h>
#include <util/generic/yexception.h>
#include <util/generic/vector.h>
#include <util/generic/set.h>
#include <util/generic/maybe.h>
#include <util/memory/blob.h>
#include <util/stream/length.h>
#include <util/stream/mem.h>

#include "eventlog_int.h"
#include "eventlog.h"
#include "common.h"

class IInputStream;

static const ui64 MAX_REQUEST_DURATION = 60'000'000;
static const ui64 MIN_START_TIME = MAX_REQUEST_DURATION;
static const ui64 MAX_END_TIME = ((ui64)-1) - MAX_REQUEST_DURATION;

class TEventFilter: public TSet<TEventClass>, public TSimpleRefCount<TEventFilter> {
public:
    TEventFilter(bool enableEvents)
        : Enable_(enableEvents)
    {
    }

    void AddEventClass(TEventClass cls) {
        insert(cls);
    }

    bool EventAllowed(TEventClass cls) const {
        bool found = (find(cls) != end());

        return Enable_ == found;
    }

private:
    bool Enable_;
};

using TEventStream = TPacketInputStream<TConstEventPtr>;

struct TFrameHeader {
    // Reads header from the stream. The caller must make sure that the
    // sync data is present just befor the stream position.
    explicit TFrameHeader(IInputStream& in);

    ui64 StartTime() const {
        return Framehdr.StartTimestamp;
    }

    ui64 EndTime() const {
        return Framehdr.EndTimestamp;
    }

    ui32 FrameId() const {
        return Basehdr.FrameId;
    }

    ui64 Duration() const {
        return EndTime() - StartTime();
    }

    TEventLogFormat ContentFormat() const {
        return Basehdr.Format & 0xffffff;
    }

    TEventLogFormat LogFormat() const {
        return Basehdr.Format >> 24;
    }

    ui64 FrameLength() const {
        return Basehdr.Length - sizeof(TCompressedFrameHeader2);
    }

    // Length including the header
    ui64 FullLength() const {
        return sizeof(*this) + FrameLength();
    }

    TCompressedFrameBaseHeader Basehdr;
    TCompressedFrameHeader2 Framehdr;
};

struct TFrameLoadError: public yexception {
    explicit TFrameLoadError(size_t skipAfter)
        : SkipAfter(skipAfter)
    {}

    size_t SkipAfter;
};

class TFrame : public TFrameHeader {
public:
    // Reads the frame after the header has been read.
    TFrame(IInputStream& in, TFrameHeader header, IEventFactory*);

    TString GetRawFrame() const;
    TString GetCompressedFrame() const;

    ui64 Address() const { return Address_; }

private:
    const TConstEventPtr& GetEvent(size_t index) const {
        return EventsCache_[index];
    }

    void ClearEventsCache() const;

    THolder<TLengthLimitedInput> Limiter_;
    mutable TVector<TConstEventPtr> EventsCache_;

    IEventFactory* Fac_;
    ui64 Address_;

    friend class TFrameDecoder;
    friend class TFrameStreamer;

private:
    class TIterator: TEventStream {
    public:
        TIterator(const TFrame& frame, TIntrusiveConstPtr<TEventFilter> filter);
        ~TIterator() override = default;

        bool Avail() const override {
            return Index_ < Size_;
        }

        TConstEventPtr operator*() const override;
        bool Next() override;

    private:
        void SkipToValidEvent();

        const TFrame& Frame_;
        size_t Size_;
        TIntrusiveConstPtr<TEventFilter> Filter_;
        size_t Index_;
    };

public:
    TFrame::TIterator GetIterator(TIntrusiveConstPtr<TEventFilter> eventFilter = nullptr) const;
};

// If `in` is derived from TCountingInput, Frame's address will
// be set accorting to the in->Counter(). Otherwise it will be zeroO
TMaybe<TFrame> FindNextFrame(IInputStream* in, IEventFactory*);

using TFrameStream = TPacketInputStream<const TFrame&>;

class IFrameFilter: public TSimpleRefCount<IFrameFilter> {
public:
    IFrameFilter() {
    }

    virtual ~IFrameFilter() = default;

    virtual bool FrameAllowed(const TFrame& frame) const = 0;
};

using IFrameFilterRef = TIntrusivePtr<IFrameFilter>;

class TDurationFrameFilter: public IFrameFilter {
public:
    TDurationFrameFilter(ui64 minFrameDuration, ui64 maxFrameDuration = Max<ui64>())
        : MinDuration_(minFrameDuration)
        , MaxDuration_(maxFrameDuration)
    {
    }

    bool FrameAllowed(const TFrame& frame) const override {
        return frame.Duration() >= MinDuration_ && frame.Duration() <= MaxDuration_;
    }

private:
    const ui64 MinDuration_;
    const ui64 MaxDuration_;
};

class TFrameIdFrameFilter: public IFrameFilter {
public:
    TFrameIdFrameFilter(ui32 frameId)
        : FrameId_(frameId)
    {
    }

    bool FrameAllowed(const TFrame& frame) const override {
        return frame.FrameId() == FrameId_;
    }

private:
    const ui32 FrameId_;
};

class TContainsEventFrameFilter: public IFrameFilter {
public:
    TContainsEventFrameFilter(const TString& args, const IEventFactory* fac);

    bool FrameAllowed(const TFrame& frame) const override;

private:
    struct TMatchGroup {
        TEventClass EventID;
        TString FieldName;
        TString ValueToMatch;
    };

    TVector<TMatchGroup> MatchGroups;
};

void SplitWithEscaping(TVector<TStringBuf>& tokens, const TStringBuf& stringToSplit, const TStringBuf& externalCharacterSet);

TString UnescapeCharacters(const TStringBuf& stringToUnescape, const TStringBuf& characterSet);

TString GetEventFieldAsString(const NProtoBuf::Message* message, const google::protobuf::FieldDescriptor* fieldDescriptor, const google::protobuf::Reflection* reflection);

class TFrameStreamer: public TFrameStream {
public:
    TFrameStreamer(IInputStream&, IEventFactory* fac, IFrameFilterRef ff = nullptr);
    TFrameStreamer(
            const TString& fileName,
            ui64 startTime,
            ui64 endTime,
            ui64 maxRequestDuration,
            IEventFactory* fac,
            IFrameFilterRef ff = nullptr);
    ~TFrameStreamer() override;

    bool Avail() const override;
    const TFrame& operator*() const override;
    bool Next() override;

private:
    bool DoNext();
    bool AllowedTimeRange(const TFrame& frame) const;

    bool AllowedFrame(const TFrame& frame) const {
        return AllowedTimeRange(frame) && (!FrameFilter_ || FrameFilter_->FrameAllowed(frame));
    }

    void SkipToAllowedFrame() {
        if (Frame_) {
            while (!AllowedFrame(*Frame_) && DoNext()) {
                //do nothing
            }
        }
    }

    TBlob File_;
    TMemoryInput MemoryIn_;
    TCountingInput In_;
    THolder<IInputStream> Stream_;
    ui64 StartTime_ = 0;
    ui64 EndTime_ = 0;
    ui64 CutoffTime_ = 0;
    TMaybe<TFrame> Frame_;
    IFrameFilterRef FrameFilter_;
    IEventFactory* EventFactory_;
};

class TFrameDecoder: TEventStream {
public:
    TFrameDecoder(const TFrame&, const TEventFilter* const filter, bool strict = false, bool withRawData = false);
    ~TFrameDecoder() override;

    bool Avail() const override;

    TConstEventPtr operator*() const override;
    bool Next() override;

    const TStringBuf GetRawEvent() const;

private:
    TFrameDecoder(const TFrameDecoder&);
    void operator=(const TFrameDecoder&);

    inline bool HaveData() const {
        return Event_ != nullptr;
    }

    void Decode();

private:
    const TFrame& Frame_;
    THolder<IInputStream> Decompressor_;
    THolder<TLengthLimitedInput> Limiter_;
    TEventPtr Event_;
    const TEventFilter* const Flt_;
    IEventFactory* Fac_;
    THolder<TEvent> EndOfFrame_;
    bool Strict_;
    TBuffer UncompressedData_;
    TStringBuf RawEventData_;
    bool WithRawData_;
};

class TEventStreamer: public TEventStream {
public:
    TEventStreamer(TFrameStream&, ui64 start, ui64 end, bool strongOrdering, TIntrusivePtr<TEventFilter> filter, bool losslessStrongOrdering = false);
    ~TEventStreamer() override;

    bool Avail() const override;
    TConstEventPtr operator*() const override;
    bool Next() override;

private:
    class TEventBuffer: public TEventStream {
    public:
        void SetCheckpoint();
        void Rollback();
        void Reorder(bool strongOrdering);
        void Append(TConstEventPtr event, bool strongOrdering);

        bool Avail() const override;
        TConstEventPtr operator*() const override;
        bool Next() override;

    private:
        TVector<TConstEventPtr> Buffer_;
        size_t BufLen_ = 0;
        ui64 LastTimestamp_ = 0;
    };

private:
    struct TInvalidEventTimestamps: public yexception {
    };

    bool LoadMoreEvents();
    void TransferEvents(const TFrame&);

private:
    TFrameStream& Frames_;
    TEventBuffer Events_;

    ui64 Start_, End_;
    ui64 MaxEndTimestamp_;
    ui64 Frontier_;
    bool StrongOrdering_;
    bool LosslessStrongOrdering_;
    TIntrusivePtr<TEventFilter> EventFilter_;
};