aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/io/node_table_reader.cpp
blob: bc3da75ee6bae45eb93f1a576cd4169fb2369d11 (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
#include "node_table_reader.h"

#include <yt/cpp/mapreduce/common/node_builder.h>
#include <yt/cpp/mapreduce/common/wait_proxy.h>

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

#include <library/cpp/yson/parser.h>

namespace NYT {

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

class TRowBuilder
    : public ::NYson::TYsonConsumerBase
{
public:
    explicit TRowBuilder(TMaybe<TRowElement>* resultRow);

    void OnStringScalar(TStringBuf value) override;
    void OnInt64Scalar(i64 value) override;
    void OnUint64Scalar(ui64 value) override;
    void OnDoubleScalar(double value) override;
    void OnBooleanScalar(bool value) override;
    void OnBeginList() override;
    void OnEntity() override;
    void OnListItem() override;
    void OnEndList() override;
    void OnBeginMap() override;
    void OnKeyedItem(TStringBuf key) override;
    void OnEndMap() override;
    void OnBeginAttributes() override;
    void OnEndAttributes() override;

    void Finalize();

private:
    THolder<TNodeBuilder> Builder_;
    TRowElement Row_;
    int Depth_ = 0;
    bool Started_ = false;
    TMaybe<TRowElement>* ResultRow_;

    void SaveResultRow();
};

TRowBuilder::TRowBuilder(TMaybe<TRowElement>* resultRow)
    : ResultRow_(resultRow)
{ }

void TRowBuilder::OnStringScalar(TStringBuf value)
{
    Row_.Size += sizeof(TNode) + sizeof(TString) + value.size();
    Builder_->OnStringScalar(value);
}

void TRowBuilder::OnInt64Scalar(i64 value)
{
    Row_.Size += sizeof(TNode);
    Builder_->OnInt64Scalar(value);
}

void TRowBuilder::OnUint64Scalar(ui64 value)
{
    Row_.Size += sizeof(TNode);
    Builder_->OnUint64Scalar(value);
}

void TRowBuilder::OnDoubleScalar(double value)
{
    Row_.Size += sizeof(TNode);
    Builder_->OnDoubleScalar(value);
}

void TRowBuilder::OnBooleanScalar(bool value)
{
    Row_.Size += sizeof(TNode);
    Builder_->OnBooleanScalar(value);
}

void TRowBuilder::OnBeginList()
{
    ++Depth_;
    Builder_->OnBeginList();
}

void TRowBuilder::OnEntity()
{
    Row_.Size += sizeof(TNode);
    Builder_->OnEntity();
}

void TRowBuilder::OnListItem()
{
    if (Depth_ == 0) {
        SaveResultRow();
    } else {
        Builder_->OnListItem();
    }
}

void TRowBuilder::OnEndList()
{
    --Depth_;
    Builder_->OnEndList();
}

void TRowBuilder::OnBeginMap()
{
    ++Depth_;
    Builder_->OnBeginMap();
}

void TRowBuilder::OnKeyedItem(TStringBuf key)
{
    Row_.Size += sizeof(TString) + key.size();
    Builder_->OnKeyedItem(key);
}

void TRowBuilder::OnEndMap()
{
    --Depth_;
    Builder_->OnEndMap();
}

void TRowBuilder::OnBeginAttributes()
{
    ++Depth_;
    Builder_->OnBeginAttributes();
}

void TRowBuilder::OnEndAttributes()
{
    --Depth_;
    Builder_->OnEndAttributes();
}

void TRowBuilder::SaveResultRow()
{
    if (!Started_) {
        Started_ = true;
    } else {
        *ResultRow_ = std::move(Row_);
    }
    Row_.Reset();
    Builder_.Reset(new TNodeBuilder(&Row_.Node));
}

void TRowBuilder::Finalize()
{
    if (Started_) {
        *ResultRow_ = std::move(Row_);
    }
}

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

TNodeTableReader::TNodeTableReader(::TIntrusivePtr<TRawTableReader> input)
    : Input_(std::move(input))
{
    PrepareParsing();
    Next();
}

TNodeTableReader::~TNodeTableReader()
{
}

void TNodeTableReader::ParseListFragmentItem() {
    if (!Parser_->Parse()) {
        Builder_->Finalize();
        IsLast_ = true;
    }
}

const TNode& TNodeTableReader::GetRow() const
{
    CheckValidity();
    if (!Row_) {
        ythrow yexception() << "Row is moved";
    }
    return Row_->Node;
}

void TNodeTableReader::MoveRow(TNode* result)
{
    CheckValidity();
    if (!Row_) {
        ythrow yexception() << "Row is moved";
    }
    *result = std::move(Row_->Node);
    Row_.Clear();
}

bool TNodeTableReader::IsValid() const
{
    return Valid_;
}

void TNodeTableReader::Next()
{
    try {
        NextImpl();
    } catch (const std::exception& ex) {
        YT_LOG_ERROR("TNodeTableReader::Next failed: %v", ex.what());
        throw;
    }
}

void TNodeTableReader::NextImpl()
{
    CheckValidity();

    if (RowIndex_) {
        ++*RowIndex_;
    }

    // At the begin of stream parser doesn't return a finished row.
    ParseFirstListFragmentItem();

    while (true) {
        if (IsLast_) {
            Finished_ = true;
            Valid_ = false;
            break;
        }

        try {
            ParseListFragmentItem();
        } catch (std::exception& ex) {
            NeedParseFirst_ = true;
            OnStreamError(std::current_exception(), ex.what());
            ParseFirstListFragmentItem();
            continue;
        }

        Row_ = std::move(*NextRow_);
        if (!Row_) {
            throw yexception() << "No row in NextRow_";
        }

        // We successfully parsed one more row from the stream,
        // so reset retry count to their initial value.
        Input_.ResetRetries();

        if (!Row_->Node.IsNull()) {
            AtStart_ = false;
            break;
        }

        for (auto& entry : Row_->Node.GetAttributes().AsMap()) {
            if (entry.first == "key_switch") {
                if (!AtStart_) {
                    Valid_ = false;
                }
            } else if (entry.first == "table_index") {
                TableIndex_ = static_cast<ui32>(entry.second.AsInt64());
            } else if (entry.first == "row_index") {
                RowIndex_ = static_cast<ui64>(entry.second.AsInt64());
            } else if (entry.first == "range_index") {
                RangeIndex_ = static_cast<ui32>(entry.second.AsInt64());
            } else if (entry.first == "tablet_index") {
                TabletIndex_ = entry.second.AsInt64();
            } else if (entry.first == "end_of_stream") {
                IsEndOfStream_ = true;
            }
        }

        if (!Valid_) {
            break;
        }
    }
}

void TNodeTableReader::ParseFirstListFragmentItem()
{
    while (NeedParseFirst_) {
        try {
            ParseListFragmentItem();
            NeedParseFirst_ = false;
            break;
        } catch (std::exception& ex) {
            OnStreamError(std::current_exception(), ex.what());
        }
    }
}

ui32 TNodeTableReader::GetTableIndex() const
{
    CheckValidity();
    return TableIndex_;
}

ui32 TNodeTableReader::GetRangeIndex() const
{
    CheckValidity();
    return RangeIndex_.GetOrElse(0) + RangeIndexShift_;
}

ui64 TNodeTableReader::GetRowIndex() const
{
    CheckValidity();
    return RowIndex_.GetOrElse(0UL);
}

i64 TNodeTableReader::GetTabletIndex() const
{
    CheckValidity();
    return TabletIndex_.GetOrElse(0L);
}

void TNodeTableReader::NextKey()
{
    while (Valid_) {
        Next();
    }

    if (Finished_) {
        return;
    }

    Valid_ = true;

    if (RowIndex_) {
        --*RowIndex_;
    }
}

TMaybe<size_t> TNodeTableReader::GetReadByteCount() const
{
    return Input_.GetReadByteCount();
}

bool TNodeTableReader::IsEndOfStream() const
{
    return IsEndOfStream_;
}

bool TNodeTableReader::IsRawReaderExhausted() const
{
    return Finished_;
}

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

void TNodeTableReader::PrepareParsing()
{
    NextRow_.Clear();
    Builder_.Reset(new TRowBuilder(&NextRow_));
    Parser_.Reset(new ::NYson::TYsonListParser(Builder_.Get(), &Input_));
}

void TNodeTableReader::OnStreamError(std::exception_ptr exception, TString error)
{
    YT_LOG_ERROR("Read error (RangeIndex: %v, RowIndex: %v, Error: %v)", RangeIndex_, RowIndex_, error);
    Exception_ = exception;
    if (Input_.Retry(RangeIndex_, RowIndex_, exception)) {
        if (RangeIndex_) {
            RangeIndexShift_ += *RangeIndex_;
        }
        RowIndex_.Clear();
        RangeIndex_.Clear();
        PrepareParsing();
    } else {
        std::rethrow_exception(Exception_);
    }
}

void TNodeTableReader::CheckValidity() const
{
    if (!Valid_) {
        ythrow yexception() << "Iterator is not valid";
    }
}

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

} // namespace NYT