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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
|
#include "dq_output_channel.h"
#include "dq_transport.h"
#include <ydb/library/yql/utils/yql_panic.h>
#include <util/generic/buffer.h>
#include <util/generic/size_literals.h>
#include <util/stream/buffer.h>
namespace NYql::NDq {
#define LOG(s) do { if (Y_UNLIKELY(LogFunc)) { LogFunc(TStringBuilder() << "channelId: " << ChannelId << ". " << s); } } while (0)
#ifndef NDEBUG
#define DLOG(s) LOG(s)
#else
#define DLOG(s)
#endif
namespace {
using namespace NKikimr;
const ui32 ChunkSizeHardLimit = 48 * 1024 * 1024; // 48 MB
bool IsSafeToEstimateValueSize(const NMiniKQL::TType* type) {
if (type->GetKind() == NMiniKQL::TType::EKind::Data) {
return true;
}
if (type->GetKind() == NMiniKQL::TType::EKind::Optional) {
const auto* optionalType = static_cast<const NMiniKQL::TOptionalType*>(type);
return IsSafeToEstimateValueSize(optionalType->GetItemType());
}
if (type->GetKind() == NMiniKQL::TType::EKind::Struct) {
const auto* structType = static_cast<const NMiniKQL::TStructType*>(type);
for (ui32 i = 0; i < structType->GetMembersCount(); ++i) {
if (!IsSafeToEstimateValueSize(structType->GetMemberType(i))) {
return false;
}
}
return true;
}
if (type->GetKind() == NMiniKQL::TType::EKind::Tuple) {
const auto* tupleType = static_cast<const NMiniKQL::TTupleType*>(type);
for (ui32 i = 0; i < tupleType->GetElementsCount(); ++i) {
if (!IsSafeToEstimateValueSize(tupleType->GetElementType(i))) {
return false;
}
}
return true;
}
if (type->GetKind() == NMiniKQL::TType::EKind::Variant) {
const auto* variantType = static_cast<const NMiniKQL::TVariantType*>(type);
for (ui32 i = 0; i < variantType->GetAlternativesCount(); ++i) {
if (!IsSafeToEstimateValueSize(variantType->GetAlternativeType(i))) {
return false;
}
}
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TDqOutputChannelOld : public IDqOutputChannel {
public:
TDqOutputChannelOld(ui64 channelId, NMiniKQL::TType* outputType, bool collectProfileStats,
const NMiniKQL::TTypeEnvironment& typeEnv, const NMiniKQL::THolderFactory& holderFactory, ui64 maxStoredBytes,
ui64 maxChunkBytes, NDqProto::EDataTransportVersion transportVersion, const TLogFunc& logFunc)
: ChannelId(channelId)
, OutputType(outputType)
, BasicStats(ChannelId)
, ProfileStats(collectProfileStats ? &BasicStats : nullptr)
, DataSerializer(typeEnv, holderFactory, transportVersion)
, MaxStoredBytes(maxStoredBytes)
, MaxChunkBytes(maxChunkBytes)
, LogFunc(logFunc) {}
ui64 GetChannelId() const override {
return ChannelId;
}
ui64 GetValuesCount(bool /* inMemoryOnly */) const override {
return Data.size();
}
[[nodiscard]]
bool IsFull() const override {
return Data.size() * EstimatedRowBytes >= MaxStoredBytes;
}
void Push(NUdf::TUnboxedValue&& value) override {
if (!BasicStats.FirstRowIn) {
BasicStats.FirstRowIn = TInstant::Now();
}
ui64 rowsInMemory = Data.size();
LOG("Push request, rows in memory: " << rowsInMemory << ", bytesInMemory: " << (rowsInMemory * EstimatedRowBytes)
<< ", finished: " << Finished);
YQL_ENSURE(!IsFull());
if (Finished) {
return;
}
if (!EstimatedRowBytes) {
EstimatedRowBytes = IsSafeToEstimateValueSize(OutputType)
? TDqDataSerializer::EstimateSize(value, OutputType)
: DataSerializer.CalcSerializedSize(value, OutputType);
LOG("EstimatedRowSize: " << EstimatedRowBytes << ", type: " << *OutputType
<< ", safe: " << IsSafeToEstimateValueSize(OutputType));
}
Data.emplace_back(std::move(value));
rowsInMemory++;
BasicStats.Bytes += EstimatedRowBytes;
BasicStats.RowsIn++;
if (Y_UNLIKELY(ProfileStats)) {
ProfileStats->MaxMemoryUsage = std::max(ProfileStats->MaxMemoryUsage, rowsInMemory * EstimatedRowBytes);
ProfileStats->MaxRowsInMemory = std::max(ProfileStats->MaxRowsInMemory, rowsInMemory);
}
}
void Push(NDqProto::TCheckpoint&& checkpoint) override {
YQL_ENSURE(!Checkpoint);
Checkpoint.ConstructInPlace(std::move(checkpoint));
}
[[nodiscard]]
bool Pop(NDqProto::TData& data, ui64 bytes) override {
LOG("Pop request, rows in memory: " << Data.size() << ", finished: " << Finished << ", requested: " << bytes
<< ", estimatedRowSize: " << EstimatedRowBytes);
if (!HasData()) {
if (Finished) {
data.SetTransportVersion(DataSerializer.GetTransportVersion());
data.SetRows(0);
data.ClearRaw();
}
return false;
}
bytes = std::min(bytes, MaxChunkBytes);
ui64 takeRows = std::max<ui64>(bytes / EstimatedRowBytes, 1);
takeRows = std::min<ui64>(takeRows, Data.size());
DLOG("About to take " << takeRows << " rows");
auto last = std::next(Data.begin(), takeRows);
if (Y_UNLIKELY(ProfileStats)) {
TInstant startTime = TInstant::Now();
data = DataSerializer.Serialize(Data.begin(), last, OutputType);
ProfileStats->SerializationTime += (TInstant::Now() - startTime);
} else {
data = DataSerializer.Serialize(Data.begin(), last, OutputType);
}
EstimatedRowBytes = EstimatedRowBytes * 0.6 + std::max<ui64>(data.GetRaw().Size() / takeRows, 1) * 0.4;
DLOG("Recalc estimated row size: " << EstimatedRowBytes);
while (data.GetRaw().size() >= ChunkSizeHardLimit && takeRows > 1) {
ui64 newTakeRows = std::max<ui64>(bytes / EstimatedRowBytes, 1);
newTakeRows = std::min<ui64>(newTakeRows, Data.size());
takeRows = newTakeRows < takeRows ? newTakeRows : takeRows / 2;
DLOG("Too big data, split it. Try to take " << takeRows);
NMiniKQL::TUnboxedValueVector values;
values.reserve(data.GetRows());
if (Y_UNLIKELY(ProfileStats)) {
TInstant startTime = TInstant::Now();
DataSerializer.Deserialize(data, OutputType, values);
ProfileStats->SerializationTime += (TInstant::Now() - startTime);
} else {
DataSerializer.Deserialize(data, OutputType, values);
}
for (ui32 i = 0; i < data.GetRows(); ++i) {
Data[i] = std::move(values[i]);
}
last = std::next(Data.begin(), takeRows);
if (Y_UNLIKELY(ProfileStats)) {
TInstant startTime = TInstant::Now();
data = DataSerializer.Serialize(Data.begin(), last, OutputType);
ProfileStats->SerializationTime += (TInstant::Now() - startTime);
} else {
data = DataSerializer.Serialize(Data.begin(), last, OutputType);
}
}
YQL_ENSURE(data.GetRaw().size() < ChunkSizeHardLimit);
BasicStats.Chunks++;
BasicStats.RowsOut += takeRows;
Data.erase(Data.begin(), last);
return true;
}
[[nodiscard]]
bool Pop(NDqProto::TCheckpoint& checkpoint) override {
if (!HasData() && Checkpoint) {
checkpoint = std::move(*Checkpoint);
Checkpoint = Nothing();
return true;
}
return false;
}
[[nodiscard]]
bool PopAll(NDqProto::TData& data) override {
if (Data.empty()) {
if (Finished) {
data.SetTransportVersion(DataSerializer.GetTransportVersion());
data.SetRows(0);
data.ClearRaw();
}
return false;
}
if (Y_UNLIKELY(ProfileStats)) {
TInstant startTime = TInstant::Now();
data = DataSerializer.Serialize(Data.begin(), Data.end(), OutputType);
ProfileStats->SerializationTime += (TInstant::Now() - startTime);
} else {
data = DataSerializer.Serialize(Data.begin(), Data.end(), OutputType);
}
BasicStats.Chunks++;
BasicStats.RowsOut += data.GetRows();
Data.clear();
return true;
}
bool PopAll(NKikimr::NMiniKQL::TUnboxedValueVector& data) override {
if (Data.empty()) {
return false;
}
data.reserve(data.size() + Data.size());
for (auto&& v : Data) {
data.emplace_back(std::move(v));
}
BasicStats.Chunks++;
BasicStats.RowsOut += data.size();
Data.clear();
return true;
}
void Finish() override {
LOG("Finish request");
Finished = true;
if (!BasicStats.FirstRowIn) {
BasicStats.FirstRowIn = TInstant::Now();
}
}
bool HasData() const override {
return !Data.empty();
}
bool IsFinished() const override {
return Finished && !HasData();
}
ui64 Drop() override { // Drop channel data because channel was finished. Leave checkpoint because checkpoints keep going through channel after finishing channel data transfer.
ui64 rows = Data.size();
Data.clear();
return rows;
}
NKikimr::NMiniKQL::TType* GetOutputType() const override {
return OutputType;
}
const TDqOutputChannelStats* GetStats() const override {
return &BasicStats;
}
void Terminate() override {
}
private:
const ui64 ChannelId;
NKikimr::NMiniKQL::TType* OutputType;
TDqOutputChannelStats BasicStats;
TDqOutputChannelStats* ProfileStats = nullptr;
TDqDataSerializer DataSerializer;
const ui64 MaxStoredBytes;
ui64 MaxChunkBytes;
TLogFunc LogFunc;
using TDataType = TDeque<NUdf::TUnboxedValue, NKikimr::NMiniKQL::TMKQLAllocator<NUdf::TUnboxedValue>>;
TDataType Data;
ui32 EstimatedRowBytes = 0;
bool Finished = false;
TMaybe<NDqProto::TCheckpoint> Checkpoint;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TDqOutputChannelNew : public IDqOutputChannel {
struct TSpilledBlob;
public:
TDqOutputChannelNew(ui64 channelId, NMiniKQL::TType* outputType, const NMiniKQL::TTypeEnvironment& typeEnv,
const NMiniKQL::THolderFactory& holderFactory, const TDqOutputChannelSettings& settings, const TLogFunc& log)
: ChannelId(channelId)
, OutputType(outputType)
, BasicStats(ChannelId)
, ProfileStats(settings.CollectProfileStats ? &BasicStats : nullptr)
, DataSerializer(typeEnv, holderFactory, settings.TransportVersion)
, Storage(settings.ChannelStorage)
, MaxStoredBytes(settings.MaxStoredBytes)
, MaxChunkBytes(settings.MaxChunkBytes)
, LogFunc(log)
{
if (Storage && 3 * MaxChunkBytes > MaxStoredBytes) {
MaxChunkBytes = Max(MaxStoredBytes / 3, 1_MB);
}
}
ui64 GetChannelId() const override {
return ChannelId;
}
ui64 GetValuesCount(bool inMemoryOnly) const override {
if (inMemoryOnly) {
return DataHead.size() + DataTail.size();
}
return DataHead.size() + DataTail.size() + SpilledRows;
}
[[nodiscard]]
bool IsFull() const override {
if (!Storage) {
if (MemoryUsed >= MaxStoredBytes) {
YQL_ENSURE(HasData());
return true;
}
return false;
}
return Storage->IsFull();
}
void Push(NUdf::TUnboxedValue&& value) override {
if (!BasicStats.FirstRowIn) {
BasicStats.FirstRowIn = TInstant::Now();
}
ui64 rowsInMemory = DataHead.size() + DataTail.size();
LOG("Push request, rows in memory: " << rowsInMemory << ", bytesInMemory: " << MemoryUsed
<< ", spilled rows: " << SpilledRows << ", spilled blobs: " << SpilledBlobs.size()
<< ", finished: " << Finished);
YQL_ENSURE(!IsFull());
if (Finished) {
return;
}
ui64 estimatedRowSize;
if (RowFixedSize.has_value()) {
estimatedRowSize = *RowFixedSize;
} else {
bool fixed;
estimatedRowSize = TDqDataSerializer::EstimateSize(value, OutputType, &fixed);
if (fixed) {
RowFixedSize.emplace(estimatedRowSize);
LOG("Raw has fixed size: " << estimatedRowSize);
}
}
DLOG("Row size: " << estimatedRowSize);
if (SpilledRows == 0) {
YQL_ENSURE(DataTail.empty() && SizeTail.empty());
DataHead.emplace_back(std::move(value));
SizeHead.emplace_back(estimatedRowSize);
} else {
DataTail.emplace_back(std::move(value));
SizeTail.emplace_back(estimatedRowSize);
}
rowsInMemory++;
MemoryUsed += estimatedRowSize;
ui64 spilledRows = 0;
ui64 spilledBytes = 0;
ui64 spilledBlobs = 0;
// TODO: add PushAndFinish call
// if processing is finished we don't have to spill data
if (MemoryUsed > MaxStoredBytes && Storage) {
LOG("Not enough memory to store rows in memory only, start spilling, rowsInMemory: " << rowsInMemory
<< ", memoryUsed: " << MemoryUsed << ", MaxStoredBytes: " << MaxStoredBytes);
TDataType* data = nullptr;
TDeque<ui64>* size = nullptr;
ui32 startIndex = std::numeric_limits<ui32>::max();
if (SpilledRows) {
// we have spilled rows already, so we can spill all DataTail
data = &DataTail;
size = &SizeTail;
startIndex = 0;
} else {
// spill from head
// but only if we have rows for at least 2 full chunks
ui32 chunks = 1;
ui64 chunkSize = 0;
startIndex = 0;
for (ui64 i = 0; i < SizeHead.size(); ++i) {
chunkSize += SizeHead.at(i);
if (chunkSize >= MaxChunkBytes && (i - startIndex) > 0) {
// LOG("one more chunk, size: " << chunkSize << ", start next chunk from " << i);
chunks++; // this row goes to the next chunk
chunkSize = 0;
if (chunks >= 3) {
data = &DataHead;
size = &SizeHead;
break;
}
startIndex = i;
}
}
}
// LOG("about to spill from index " << startIndex);
if (data) {
while (true) {
ui64 chunkSize = 0;
ui32 rowsInChunk = 0;
ui32 idx = startIndex + spilledRows;
while (idx < size->size()) {
ui64 rowSize = size->at(idx);
if (chunkSize == 0 || chunkSize + rowSize <= MaxChunkBytes) {
chunkSize += rowSize;
++rowsInChunk;
++idx;
} else {
break;
}
}
if (rowsInChunk > 0) {
// LOG("about to spill from " << (startIndex + spilledRows) << " to " << (startIndex + spilledRows + rowsInChunk));
TDataType::iterator first = std::next(data->begin(), startIndex + spilledRows);
TDataType::iterator last = std::next(data->begin(), startIndex + spilledRows + rowsInChunk);
NDqProto::TData protoBlob = DataSerializer.Serialize(first, last, OutputType);
TBuffer blob;
TBufferOutput out{blob};
protoBlob.SerializeToArcadiaStream(&out);
ui64 blobSize = blob.size();
ui64 blobId = NextBlobId++;
LOG("Spill blobId: " << blobId << ", size: " << blobSize << ", rows: " << rowsInChunk);
Storage->Put(blobId, std::move(blob));
SpilledBlobs.emplace_back(TSpilledBlob(blobId, chunkSize, blobSize, rowsInChunk));
SpilledRows += rowsInChunk;
MemoryUsed -= chunkSize;
spilledRows += rowsInChunk;
spilledBytes += blobSize;
spilledBlobs++;
} else {
break;
}
}
if (data == &DataHead) {
// move rows after spilled ones to the DataTail
YQL_ENSURE(DataTail.empty());
for (ui32 i = startIndex + spilledRows; i < data->size(); ++i) {
DataTail.emplace_back(std::move(DataHead[i]));
SizeTail.emplace_back(SizeHead[i]);
}
data->erase(std::next(data->begin(), startIndex), data->end());
size->erase(std::next(size->begin(), startIndex), size->end());
} else {
YQL_ENSURE(startIndex == 0);
data->erase(data->begin(), std::next(data->begin(), spilledRows));
size->erase(size->begin(), std::next(size->begin(), spilledRows));
}
} else {
// LOG("nothing to spill");
}
}
BasicStats.Bytes += estimatedRowSize;
BasicStats.RowsIn++;
if (Y_UNLIKELY(ProfileStats)) {
ProfileStats->MaxMemoryUsage = std::max(ProfileStats->MaxMemoryUsage, MemoryUsed);
ProfileStats->MaxRowsInMemory = std::max(ProfileStats->MaxRowsInMemory, DataHead.size() + DataTail.size());
if (spilledRows) {
ProfileStats->SpilledRows += spilledRows;
ProfileStats->SpilledBytes += spilledBytes;
ProfileStats->SpilledBlobs += spilledBlobs;
}
}
ValidateUsedMemory();
#if 0
TStringStream ss;
ss << "-- DUMP --" << Endl;
ss << " Head:" << Endl;
for (auto& v : DataHead) {
ss << " "; v.Dump(ss); ss << Endl;
}
ss << " Spilled: " << SpilledRows << Endl;
ss << " Tail:" << Endl;
for (auto& v : DataTail) {
ss << " "; v.Dump(ss); ss << Endl;
}
LOG(ss.Str());
#endif
}
void Push(NDqProto::TCheckpoint&& checkpoint) override {
YQL_ENSURE(!Checkpoint);
Checkpoint.ConstructInPlace(std::move(checkpoint));
}
[[nodiscard]]
bool Pop(NDqProto::TData& data, ui64 bytes) override {
LOG("Pop request, rows in memory: " << DataHead.size() << "/" << DataTail.size()
<< ", spilled rows: " << SpilledRows << ", spilled blobs: " << SpilledBlobs.size()
<< ", finished: " << Finished << ", requested: " << bytes << ", maxChunkBytes: " << MaxChunkBytes);
if (!HasData()) {
if (Finished) {
data.SetTransportVersion(DataSerializer.GetTransportVersion());
data.SetRows(0);
data.ClearRaw();
}
return false;
}
bytes = std::min(bytes, MaxChunkBytes);
if (DataHead.empty()) {
YQL_ENSURE(SpilledRows);
TSpilledBlob spilledBlob = SpilledBlobs.front();
LOG("Loading spilled blob. BlobId: " << spilledBlob.BlobId << ", rows: " << spilledBlob.Rows
<< ", bytes: " << spilledBlob.SerializedSize);
TBuffer blob;
if (!Storage->Get(spilledBlob.BlobId, blob)) {
LOG("BlobId " << spilledBlob.BlobId << " not ready yet");
// not loaded yet
return false;
}
YQL_ENSURE(blob.size() == spilledBlob.SerializedSize, "" << blob.size() << " != " << spilledBlob.SerializedSize);
NDqProto::TData protoBlob;
YQL_ENSURE(protoBlob.ParseFromArray(blob.Data(), blob.size()));
blob.Reset();
YQL_ENSURE(protoBlob.GetRows() == spilledBlob.Rows);
bool hasResult = false;
// LOG("bytes: " << bytes << ", loaded blob: " << spilledBlob.SerializedSize);
if (spilledBlob.SerializedSize <= bytes * 2) {
data.Swap(&protoBlob);
YQL_ENSURE(data.ByteSizeLong() <= ChunkSizeHardLimit);
hasResult = true;
// LOG("return loaded blob as-is");
} else {
NKikimr::NMiniKQL::TUnboxedValueVector buffer;
DataSerializer.Deserialize(protoBlob, OutputType, buffer);
for (ui32 i = 0; i < buffer.size(); ++i) {
SizeHead.emplace_back(RowFixedSize
? *RowFixedSize
: TDqDataSerializer::EstimateSize(buffer[i], OutputType));
DataHead.emplace_back(std::move(buffer[i]));
MemoryUsed += SizeHead.back();
}
}
SpilledRows -= spilledBlob.Rows;
SpilledBlobs.pop_front();
if (SpilledBlobs.empty()) {
// LOG("no more spilled blobs, move " << DataTail.size() << " tail rows to the head");
DataHead.insert(DataHead.end(), std::make_move_iterator(DataTail.begin()), std::make_move_iterator(DataTail.end()));
SizeHead.insert(SizeHead.end(), std::make_move_iterator(SizeTail.begin()), std::make_move_iterator(SizeTail.end()));
DataTail.clear();
SizeTail.clear();
}
if (hasResult) {
BasicStats.Chunks++;
BasicStats.RowsOut += data.GetRows();
ValidateUsedMemory();
return true;
}
}
ui32 takeRows = 0;
ui64 chunkSize = 0;
while (takeRows == 0 || (takeRows < SizeHead.size() && chunkSize + SizeHead[takeRows] <= bytes)) {
chunkSize += SizeHead[takeRows];
++takeRows;
}
DLOG("return rows: " << takeRows << ", size: " << chunkSize);
auto firstDataIt = DataHead.begin();
auto lastDataIt = std::next(firstDataIt, takeRows);
auto firstSizeIt = SizeHead.begin();
auto lastSizeIt = std::next(firstSizeIt, takeRows);
if (Y_UNLIKELY(ProfileStats)) {
TInstant startTime = TInstant::Now();
data = DataSerializer.Serialize(firstDataIt, lastDataIt, OutputType);
ProfileStats->SerializationTime += (TInstant::Now() - startTime);
} else {
data = DataSerializer.Serialize(firstDataIt, lastDataIt, OutputType);
}
YQL_ENSURE(data.ByteSizeLong() <= ChunkSizeHardLimit);
BasicStats.Chunks++;
BasicStats.RowsOut += takeRows;
DataHead.erase(firstDataIt, lastDataIt);
SizeHead.erase(firstSizeIt, lastSizeIt);
MemoryUsed -= chunkSize;
ValidateUsedMemory();
return true;
}
[[nodiscard]]
bool Pop(NDqProto::TCheckpoint& checkpoint) override {
if (!HasData() && Checkpoint) {
checkpoint = std::move(*Checkpoint);
Checkpoint = Nothing();
return true;
}
return false;
}
[[nodiscard]]
bool PopAll(NDqProto::TData& data) override {
YQL_ENSURE(!SpilledRows);
YQL_ENSURE(DataTail.empty());
if (DataHead.empty()) {
if (Finished) {
data.SetTransportVersion(DataSerializer.GetTransportVersion());
data.SetRows(0);
data.ClearRaw();
}
return false;
}
if (Y_UNLIKELY(ProfileStats)) {
TInstant startTime = TInstant::Now();
data = DataSerializer.Serialize(DataHead.begin(), DataHead.end(), OutputType);
ProfileStats->SerializationTime += (TInstant::Now() - startTime);
} else {
data = DataSerializer.Serialize(DataHead.begin(), DataHead.end(), OutputType);
}
BasicStats.Chunks++;
BasicStats.RowsOut += data.GetRows();
DataHead.clear();
SizeHead.clear();
MemoryUsed = 0;
return true;
}
bool PopAll(NKikimr::NMiniKQL::TUnboxedValueVector& data) override {
YQL_ENSURE(!SpilledRows);
YQL_ENSURE(DataTail.empty());
if (DataHead.empty()) {
return false;
}
data.reserve(data.size() + DataHead.size());
for (auto&& v : DataHead) {
data.emplace_back(std::move(v));
}
BasicStats.Chunks++;
BasicStats.RowsOut += data.size();
DataHead.clear();
SizeHead.clear();
MemoryUsed = 0;
return true;
}
void Finish() override {
DLOG("Finish request");
Finished = true;
if (!BasicStats.FirstRowIn) {
BasicStats.FirstRowIn = TInstant::Now();
}
}
bool HasData() const override {
return !DataHead.empty() || SpilledRows != 0 || !DataTail.empty();
}
bool IsFinished() const override {
return Finished && !HasData();
}
ui64 Drop() override { // Drop channel data because channel was finished. Leave checkpoint because checkpoints keep going through channel after finishing channel data transfer.
ui64 rows = DataHead.size() + SpilledRows + DataTail.size();
DataHead.clear();
SizeHead.clear();
DataTail.clear();
SizeTail.clear();
MemoryUsed = 0;
// todo: send remove request
SpilledRows = 0;
SpilledBlobs.clear();
return rows;
}
NKikimr::NMiniKQL::TType* GetOutputType() const override {
return OutputType;
}
const TDqOutputChannelStats* GetStats() const override {
return &BasicStats;
}
void Terminate() override {
if (Storage) {
Storage.Reset();
}
}
void ValidateUsedMemory() {
#ifndef NDEBUG
ui64 x = 0;
for (auto z : SizeHead) x += z;
for (auto z : SizeTail) x += z;
YQL_ENSURE(x == MemoryUsed, "" << x << " != " << MemoryUsed);
#endif
}
private:
const ui64 ChannelId;
NKikimr::NMiniKQL::TType* OutputType;
TDqOutputChannelStats BasicStats;
TDqOutputChannelStats* ProfileStats = nullptr;
TDqDataSerializer DataSerializer;
IDqChannelStorage::TPtr Storage;
const ui64 MaxStoredBytes;
ui64 MaxChunkBytes;
TLogFunc LogFunc;
std::optional<ui32> RowFixedSize;
struct TSpilledBlob {
ui64 BlobId;
ui64 InMemorySize;
ui64 SerializedSize;
ui32 Rows;
TSpilledBlob(ui64 blobId, ui64 inMemorySize, ui64 serializedSize, ui32 rows)
: BlobId(blobId), InMemorySize(inMemorySize), SerializedSize(serializedSize), Rows(rows) {}
};
// DataHead ( . SpilledBlobs (. DataTail)? )?
using TDataType = TDeque<NUdf::TUnboxedValue, NKikimr::NMiniKQL::TMKQLAllocator<NUdf::TUnboxedValue>>;
TDataType DataHead;
TDeque<ui64> SizeHead;
TDeque<TSpilledBlob> SpilledBlobs;
TDataType DataTail;
TDeque<ui64> SizeTail;
ui64 MemoryUsed = 0; // approx memory usage
ui64 NextBlobId = 1;
ui64 SpilledRows = 0;
bool Finished = false;
TMaybe<NDqProto::TCheckpoint> Checkpoint;
};
} // anonymous namespace
IDqOutputChannel::TPtr CreateDqOutputChannel(ui64 channelId, NKikimr::NMiniKQL::TType* outputType,
const NKikimr::NMiniKQL::TTypeEnvironment& typeEnv, const NKikimr::NMiniKQL::THolderFactory& holderFactory,
const TDqOutputChannelSettings& settings, const TLogFunc& logFunc)
{
if (settings.AllowGeneratorsInUnboxedValues) {
YQL_ENSURE(!settings.ChannelStorage);
return new TDqOutputChannelOld(channelId, outputType, settings.CollectProfileStats, typeEnv, holderFactory,
settings.MaxStoredBytes, settings.MaxChunkBytes, settings.TransportVersion, logFunc);
} else {
return new TDqOutputChannelNew(channelId, outputType, typeEnv, holderFactory, settings, logFunc);
}
}
} // namespace NYql::NDq
|