aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/blobstorage/dsproxy/dsproxy_blackboard.cpp
blob: a10c48ec0b83efa296c5105f6c09b81d10f48099 (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
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
#include "dsproxy_blackboard.h"

namespace NKikimr {

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TBlobState
//

void TBlobState::TState::AddResponseData(ui32 fullSize, ui32 shift, TString &data) {
    // Add the data to the Data buffer
    Y_VERIFY(data.size());
    Y_VERIFY(shift + data.size() <= fullSize);
    Data.Write(shift, data.data(), data.size());
    // Mark the interval as present in the Data buffer
    Here.Add(shift, shift + data.size());
}

void TBlobState::TState::AddPartToPut(TString &data) {
    Y_VERIFY(data.size());
    Data.SetMonolith(data);
    Here.Assign(0, data.size());
}


void TBlobState::Init(const TLogoBlobID &id, const TBlobStorageGroupInfo &info) {
    Id = id;
    Parts.resize(info.Type.TotalPartCount());
    ui32 blobSubgroupSize = info.Type.BlobSubgroupSize();
    Disks.resize(blobSubgroupSize);
    TBlobStorageGroupInfo::TServiceIds vdisksSvc;
    TBlobStorageGroupInfo::TVDiskIds vdisksId;
    const ui32 hash = Id.Hash();
    info.PickSubgroup(hash, &vdisksId, &vdisksSvc);
    for (ui32 i = 0; i < blobSubgroupSize; ++i) {
        Disks[i].OrderNumber = info.GetOrderNumber(vdisksId[i]);
        Disks[i].DiskParts.resize(info.Type.TotalPartCount());
    }
    IsChanged = true; 
}

void TBlobState::AddNeeded(ui64 begin, ui64 size) {
    Y_VERIFY(bool(Id));
    Whole.Needed.Add(begin, begin + size);
    Whole.NotHere.Add(begin, begin + size);
    IsChanged = true; 
}

void TBlobState::AddPartToPut(ui32 partIdx, TString &partData) {
    Y_VERIFY(bool(Id));
    Y_VERIFY(partIdx < Parts.size());
    Parts[partIdx].AddPartToPut(partData);
    IsChanged = true; 
}

void TBlobState::MarkBlobReadyToPut(ui8 blobIdx) { 
    Y_VERIFY(WholeSituation == ESituation::Unknown || WholeSituation == ESituation::Present);
    WholeSituation = ESituation::Present;
    BlobIdx = blobIdx; 
    IsChanged = true; 
}

bool TBlobState::Restore(const TBlobStorageGroupInfo &info) {
    TIntervalVec<i32> fullBlobInterval(0, Id.BlobSize());
    if (fullBlobInterval.IsSubsetOf(Whole.Here)) {
        return true;
    }

    const ui32 parts = info.Type.TotalPartCount();
    ui32 partsPresent = 0;
    for (ui32 i = 0; i < parts; ++i) {
        if (const ui32 partSize = info.Type.PartSize(TLogoBlobID(Id, i + 1))) {
            if (TIntervalVec<i32>(0, partSize).IsSubsetOf(Parts[i].Here)) {
                ++partsPresent;
            }
        }
    }
    if (partsPresent < info.Type.MinimalRestorablePartCount()) {
        return false;
    }

    TDataPartSet partSet;
    partSet.Parts.resize(parts);
    for (ui32 i = 0; i < parts; ++i) {
        if (const ui32 partSize = info.Type.PartSize(TLogoBlobID(Id, i + 1))) {
            if (TIntervalVec<i32>(0, partSize).IsSubsetOf(Parts[i].Here)) {
                partSet.PartsMask |= (1 << i);
                TString tmp = TString::Uninitialized(partSize);
                Parts[i].Data.Read(0, const_cast<char*>(tmp.data()), partSize);
                partSet.Parts[i].ReferenceTo(tmp);
            }
        }
    }
    partSet.FullDataSize = Id.BlobSize();

    TString whole;
    info.Type.RestoreData((TErasureType::ECrcMode)Id.CrcMode(), partSet, whole, false, true, false);
    Whole.Data.Write(0, whole.data(), Id.BlobSize());
    Whole.Here.Add(fullBlobInterval);
    Whole.NotHere.Subtract(fullBlobInterval);
    return true;
}

void TBlobState::AddResponseData(const TBlobStorageGroupInfo &info, const TLogoBlobID &id, ui32 orderNumber,
        ui32 shift, TString &data) {
    // Add actual data to Parts
    Y_VERIFY(id.PartId() != 0);
    ui32 partIdx = id.PartId() - 1;
    Y_VERIFY(partIdx < Parts.size());
    const ui32 partSize = info.Type.PartSize(id);
    if (partSize) {
        Parts[partIdx].AddResponseData(partSize, shift, data);
    }
    IsChanged = true; 
    // Mark part as present for the disk
    bool isFound = false;
    for (ui32 diskIdx = 0; diskIdx < Disks.size(); ++diskIdx) {
        TDisk &disk = Disks[diskIdx];
        if (disk.OrderNumber == orderNumber) {
            isFound = true;
            Y_VERIFY(partIdx < disk.DiskParts.size());
            TDiskPart &diskPart = disk.DiskParts[partIdx];
            //Cerr << Endl << "present diskIdx# " << diskIdx << " partIdx# " << partIdx << Endl << Endl;
            diskPart.Situation = ESituation::Present;
            if (partSize) {
                TIntervalVec<i32> responseInterval(shift, shift + data.size());
                diskPart.Requested.Subtract(responseInterval);
            }
            break;
        }
    }
    Y_VERIFY(isFound);
}

void TBlobState::AddNoDataResponse(const TBlobStorageGroupInfo &info, const TLogoBlobID &id, ui32 orderNumber) {
    Y_UNUSED(info);
    Y_VERIFY(id.PartId() != 0);
    ui32 partIdx = id.PartId() - 1;
    IsChanged = true; 
    // Mark part as absent for the disk
    bool isFound = false;
    for (ui32 diskIdx = 0; diskIdx < Disks.size(); ++diskIdx) {
        TDisk &disk = Disks[diskIdx];
        if (disk.OrderNumber == orderNumber) {
            isFound = true;
            Y_VERIFY(partIdx < disk.DiskParts.size());
            TDiskPart &diskPart = disk.DiskParts[partIdx];
            //Cerr << Endl << "absent diskIdx# " << diskIdx << " partIdx# " << partIdx << Endl << Endl;
            diskPart.Situation = ESituation::Absent;
            diskPart.Requested.Clear();
            break;
        }
    }
    Y_VERIFY(isFound);
}

void TBlobState::AddPutOkResponse(const TBlobStorageGroupInfo &info, const TLogoBlobID &id, ui32 orderNumber) {
    Y_UNUSED(info);
    Y_VERIFY(id.PartId() != 0);
    ui32 partIdx = id.PartId() - 1;
    IsChanged = true; 
    // Mark part as put ok for the disk
    bool isFound = false;
    for (ui32 diskIdx = 0; diskIdx < Disks.size(); ++diskIdx) {
        TDisk &disk = Disks[diskIdx];
        if (disk.OrderNumber == orderNumber) {
            isFound = true;
            Y_VERIFY(partIdx < disk.DiskParts.size());
            TDiskPart &diskPart = disk.DiskParts[partIdx];
            //Cerr << Endl << "put ok diskIdx# " << diskIdx << " partIdx# " << partIdx << Endl << Endl;
            diskPart.Situation = ESituation::Present;
            break;
        }
    }
    Y_VERIFY(isFound);
}

void TBlobState::AddErrorResponse(const TBlobStorageGroupInfo &info, const TLogoBlobID &id, ui32 orderNumber) {
    Y_UNUSED(info);
    Y_VERIFY(id.PartId() != 0);
    ui32 partIdx = id.PartId() - 1;
    IsChanged = true; 
    // Mark part as error for the disk
    bool isFound = false;
    for (ui32 diskIdx = 0; diskIdx < Disks.size(); ++diskIdx) {
        TDisk &disk = Disks[diskIdx];
        if (disk.OrderNumber == orderNumber) {
            isFound = true;
            Y_VERIFY(partIdx < disk.DiskParts.size());
            TDiskPart &diskPart = disk.DiskParts[partIdx];
            //Cerr << Endl << "error diskIdx# " << diskIdx << " partIdx# " << partIdx << Endl << Endl;
            diskPart.Situation = ESituation::Error;
            diskPart.Requested.Clear();
            break;
        }
    }
    Y_VERIFY(isFound);
}

void TBlobState::AddNotYetResponse(const TBlobStorageGroupInfo &info, const TLogoBlobID &id, ui32 orderNumber) {
    Y_UNUSED(info);
    Y_VERIFY(id.PartId() != 0);
    ui32 partIdx = id.PartId() - 1;
    IsChanged = true; 
    // Mark part as error for the disk
    bool isFound = false;
    for (ui32 diskIdx = 0; diskIdx < Disks.size(); ++diskIdx) {
        TDisk &disk = Disks[diskIdx];
        if (disk.OrderNumber == orderNumber) {
            isFound = true;
            Y_VERIFY(partIdx < disk.DiskParts.size());
            TDiskPart &diskPart = disk.DiskParts[partIdx];
            //Cerr << Endl << "error diskIdx# " << diskIdx << " partIdx# " << partIdx << Endl << Endl;
            diskPart.Situation = ESituation::Lost;
            diskPart.Requested.Clear();
            break;
        }
    }
    Y_VERIFY(isFound);
}

ui64 TBlobState::GetPredictedDelayNs(const TBlobStorageGroupInfo &info, TGroupQueues &groupQueues,
        ui32 diskIdxInSubring, NKikimrBlobStorage::EVDiskQueueId queueId) const {
    Y_UNUSED(info);
    return groupQueues.GetPredictedDelayNsByOrderNumber(Disks[diskIdxInSubring].OrderNumber, queueId);
}

void TBlobState::GetWorstPredictedDelaysNs(const TBlobStorageGroupInfo &info, TGroupQueues &groupQueues,
        NKikimrBlobStorage::EVDiskQueueId queueId,
        ui64 *outWorstNs, ui64 *outNextToWorstNs, i32 *outWorstSubgroupIdx) const {
    *outWorstSubgroupIdx = -1;
    *outWorstNs = 0;
    *outNextToWorstNs = 0;
    for (ui32 diskIdx = 0; diskIdx < Disks.size(); ++diskIdx) {
        ui64 predictedNs = GetPredictedDelayNs(info, groupQueues, diskIdx, queueId);
        if (predictedNs > *outWorstNs) {
            *outNextToWorstNs = *outWorstNs;
            *outWorstNs = predictedNs;
            *outWorstSubgroupIdx = diskIdx;
        } else if (predictedNs > *outNextToWorstNs) {
            *outNextToWorstNs = predictedNs;
        }
    }
}

TString TBlobState::ToString() const {
    TStringStream str;
    str << "{Id# " << Id.ToString();
    str << Endl;
    str << " Whole# " << Whole.ToString();
    str << Endl;
    str << " WholeSituation# " << SituationToString(WholeSituation);
    str << Endl;
    for (ui32 i = 0; i < Parts.size(); ++i) {
    str << Endl;
        str << " Parts[" << i << "]# " << Parts[i].ToString();
    str << Endl;
    }
    for (ui32 i = 0; i < Disks.size(); ++i) {
    str << Endl;
        str << " Disks[" << i << "]# " << Disks[i].ToString();
    str << Endl;
    }
    str << " BlobIdx# " << (ui32)BlobIdx; 
    str << Endl; 
    str << "}";
    return str.Str();
}

TString TBlobState::SituationToString(ESituation situation) {
    switch (situation) {
        case ESituation::Unknown:
            return "ESituation::Unknown";
        case ESituation::Error:
            return "ESituation::Error";
        case ESituation::Absent:
            return "ESituation::Absent";
        case ESituation::Lost:
            return "ESituation::Lost";
        case ESituation::Present:
            return "ESituation::Present";
        case ESituation::Sent:
            return "ESituation::Sent";
    }
    Y_VERIFY(false, "Unexpected situation# %" PRIu64, ui64(situation));
    return "";
}

TString TBlobState::TDisk::ToString() const {
    TStringStream str;
    str << "{OrderNumber# " << OrderNumber;
    str << " IsSlow# " << IsSlow;
    for (ui32 i = 0; i < DiskParts.size(); ++i) {
    str << Endl;
        str << " DiskParts[" << i << "]# " << DiskParts[i].ToString();
    }
    str << "}";
    return str.Str();
}

TString TBlobState::TDiskPart::ToString() const {
    TStringStream str;
    str << "{Requested# " << Requested.ToString();
    str << " Situation# " << SituationToString(Situation);
    str << "}";
    return str.Str();
}

TString TBlobState::TState::ToString() const {
    TStringStream str;
    str << "{Data# " << Data.Print();
    str << " Here# " << Here.ToString();
    str << "}";
    return str.Str();
}

TString TBlobState::TWholeState::ToString() const {
    TStringStream str;
    str << "{Data# " << Data.Print();
    str << " Here# " << Here.ToString();
    str << " Needed# " << Needed.ToString();
    str << " NotHere# " << NotHere.ToString();
    str << "}";
    return str.Str();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TGroupDiskRequests
//

TGroupDiskRequests::TGroupDiskRequests(ui32 disks) {
    DiskRequestsForOrderNumber.resize(disks);
}

void TGroupDiskRequests::AddGet(const ui32 diskOrderNumber, const TLogoBlobID &id, const TIntervalSet<i32> &intervalSet) {
    Y_VERIFY(diskOrderNumber < DiskRequestsForOrderNumber.size());
    auto &requestsToSend = DiskRequestsForOrderNumber[diskOrderNumber].GetsToSend;
    for (auto pair: intervalSet) {
        requestsToSend.emplace_back(id, pair.first, pair.second - pair.first);
    }
}

void TGroupDiskRequests::AddGet(const ui32 diskOrderNumber, const TLogoBlobID &id, const ui32 shift,
        const ui32 size) {
    Y_VERIFY(diskOrderNumber < DiskRequestsForOrderNumber.size());
    DiskRequestsForOrderNumber[diskOrderNumber].GetsToSend.emplace_back(id, shift, size);
}

void TGroupDiskRequests::AddPut(const ui32 diskOrderNumber, const TLogoBlobID &id, TString buffer,
        TDiskPutRequest::EPutReason putReason, bool isHandoff, ui8 blobIdx) { 
    Y_VERIFY(diskOrderNumber < DiskRequestsForOrderNumber.size());
    DiskRequestsForOrderNumber[diskOrderNumber].PutsToSend.emplace_back(id, buffer, putReason, isHandoff, blobIdx); 
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TBlackboard
//

void TBlackboard::AddNeeded(const TLogoBlobID &id, ui32 inShift, ui32 inSize) {
    Y_VERIFY(bool(id));
    Y_VERIFY(id.PartId() == 0);
    Y_VERIFY(id.BlobSize() != 0);
    ui64 fullSize = id.BlobSize();
    ui64 shift = Min(ui64(inShift), fullSize);
    ui64 maxSize = fullSize - shift;
    ui64 size = (inSize ? Min(ui64(inSize), maxSize) : maxSize);
    //Cerr << "size " << size << " shift " << shift << Endl;
    if (size > 0) {
        TBlobState &state = BlobStates[id];; 
        if (!bool(state.Id)) {
            state.Init(id, *Info);
        }
        state.AddNeeded(shift, size);
    } else {
        TStringStream str;
        str << "It is impossible to read 0 bytes! Do not send such requests.";
        str << " Id# " << id.ToString() << " inShift# " << inShift << " inSize# " << inSize;
        Y_VERIFY(false, "%s", str.Str().c_str());
    }
}

void TBlackboard::AddPartToPut(const TLogoBlobID &id, ui32 partIdx, TString &partData) {
    Y_VERIFY(bool(id));
    Y_VERIFY(id.PartId() == 0);
    Y_VERIFY(id.BlobSize() != 0);
    TBlobState &state = BlobStates[id];
    if (!bool(state.Id)) {
        state.Init(id, *Info);
    }
    state.AddPartToPut(partIdx, partData);
}

void TBlackboard::MarkBlobReadyToPut(const TLogoBlobID &id, ui8 blobIdx) { 
    Y_VERIFY(bool(id));
    Y_VERIFY(id.PartId() == 0);
    Y_VERIFY(id.BlobSize() != 0);
    TBlobState &state = BlobStates[id];; 
    if (!bool(state.Id)) {
        state.Init(id, *Info);
    }
    state.MarkBlobReadyToPut(blobIdx); 
}

void TBlackboard::MoveBlobStateToDone(const TLogoBlobID &id) { 
    Y_VERIFY(bool(id)); 
    Y_VERIFY(id.PartId() == 0); 
    Y_VERIFY(id.BlobSize() != 0); 
    auto it = BlobStates.find(id); 
    if (it == BlobStates.end()) { 
        auto doneIt = DoneBlobStates.find(id); 
        const char *errorMsg = doneIt == DoneBlobStates.end() ? 
                "This blobId is not in BlobStates or in DoneBlobStates" : 
                "This blobId is already in DoneBlobStates"; 
        Y_VERIFY_S(false, errorMsg << " BlobId# " << id << " Blackboard# " << ToString()); 
    } else { 
        if (!it->second.IsDone) { 
            DoneCount++; 
            it->second.IsDone = true; 
        } 
        auto node = BlobStates.extract(it); 
        DoneBlobStates.insert(std::move(node)); 
    } 
} 
 
void TBlackboard::AddPutOkResponse(const TLogoBlobID &id, ui32 orderNumber) {
    Y_VERIFY(bool(id));
    Y_VERIFY(id.PartId() != 0);
    TBlobState &state = GetState(id); 
    state.AddPutOkResponse(*Info, id, orderNumber);
}

void TBlackboard::AddResponseData(const TLogoBlobID &id, ui32 orderNumber, ui32 shift, TString &data) {
    Y_VERIFY(bool(id));
    Y_VERIFY(id.PartId() != 0);
    TBlobState &state = GetState(id); 
    state.AddResponseData(*Info, id, orderNumber, shift, data);
}

void TBlackboard::AddNoDataResponse(const TLogoBlobID &id, ui32 orderNumber) {
    Y_VERIFY(bool(id));
    Y_VERIFY(id.PartId() != 0);
    TBlobState &state = GetState(id); 
    state.AddNoDataResponse(*Info, id, orderNumber);
}

void TBlackboard::AddNotYetResponse(const TLogoBlobID &id, ui32 orderNumber) {
    Y_VERIFY(bool(id));
    Y_VERIFY(id.PartId() != 0);
    TBlobState &state = GetState(id); 
    state.AddNotYetResponse(*Info, id, orderNumber);
}

void TBlackboard::AddErrorResponse(const TLogoBlobID &id, ui32 orderNumber) {
    Y_VERIFY(bool(id));
    Y_VERIFY(id.PartId() != 0);
    TBlobState &state = GetState(id); 
    state.AddErrorResponse(*Info, id, orderNumber);
}

EStrategyOutcome TBlackboard::RunStrategy(TLogContext &logCtx, const IStrategy& s, TBatchedVec<TBlobStates::value_type*> *finished) {
    IStrategy& temp = const_cast<IStrategy&>(s); // better UX
    Y_VERIFY(BlobStates.size()); 
    EStrategyOutcome outcome = EStrategyOutcome::IN_PROGRESS; 
    for (auto it = BlobStates.begin(); it != BlobStates.end(); ++it) {
        auto& blob = it->second;
        if (!blob.IsChanged) { 
            continue; 
        } 
        blob.IsChanged = false; 
        // recalculate blob outcome if it is not yet determined
        switch (auto res = temp.Process(logCtx, blob, *Info, *this, GroupDiskRequests)) {
            case EStrategyOutcome::IN_PROGRESS:
                if (blob.IsDone) { 
                    DoneCount--; 
                    blob.IsDone = false; 
                } 
                outcome = EStrategyOutcome::IN_PROGRESS;
                break;

            case EStrategyOutcome::ERROR:
                if (IsAllRequestsTogether) {
                    return res;
                } else {
                    blob.Status = NKikimrProto::ERROR;
                    if (finished) {
                        finished->push_back(&*it);
                    }
                    if (outcome.ErrorReason) { 
                        outcome.ErrorReason += " && ";
                        outcome.ErrorReason += res.ErrorReason;
                    } else { 
                        outcome.ErrorReason = res.ErrorReason; 
                    }
                }
                if (!blob.IsDone) { 
                    DoneCount++; 
                    blob.IsDone = true; 
                } 
                break;

            case EStrategyOutcome::DONE:
                if (!IsAllRequestsTogether) {
                    blob.Status = NKikimrProto::OK;
                    if (finished) {
                        finished->push_back(&*it);
                    }
                }
                if (!blob.IsDone) { 
                    DoneCount++; 
                    blob.IsDone = true; 
                } 
                break;
        } 
    }
    if (DoneCount == (BlobStates.size() + DoneBlobStates.size())) { 
        outcome = EStrategyOutcome::DONE; 
    } 
    return outcome;
}

TBlobState& TBlackboard::GetState(const TLogoBlobID &id) { 
    Y_VERIFY(bool(id));
    TLogoBlobID fullId = id.FullID(); 
    auto it = BlobStates.find(fullId); 
    if (it == BlobStates.end()) { 
        it = DoneBlobStates.find(fullId); 
        Y_VERIFY_S(it != DoneBlobStates.end(), "The blob was not found in BlobStates and DoneBlobStates" 
                << " blobId# " << fullId 
                << " BlackBoard# " << ToString()); 
    } 
    TBlobState &state = it->second; 
    return state;
}

ssize_t TBlackboard::AddPartMap(const TLogoBlobID &id, ui32 diskOrderNumber, ui32 requestIndex) {
    Y_VERIFY(id);
    TBlobState &state = GetState(id); 
    ssize_t ret = state.PartMap.size();
    state.PartMap.emplace_back(TEvBlobStorage::TEvGetResult::TPartMapItem{
            diskOrderNumber,
            id.PartId(),
            requestIndex,
            Max<ui32>(),
            {},
        });
    return ret;
}

void TBlackboard::ReportPartMapStatus(const TLogoBlobID &id, ssize_t partMapIndex, ui32 responseIndex, NKikimrProto::EReplyStatus status) {
    Y_VERIFY(id);
    Y_VERIFY(partMapIndex >= 0);
    TBlobState &state = GetState(id); 
    Y_VERIFY(static_cast<size_t>(partMapIndex) < state.PartMap.size());
    TEvBlobStorage::TEvGetResult::TPartMapItem &item = state.PartMap[partMapIndex];
    Y_VERIFY(item.ResponseIndex == responseIndex || item.ResponseIndex == Max<ui32>());
    item.ResponseIndex = responseIndex;
    item.Status.emplace_back(id.PartId(), status);
}

void TBlackboard::GetWorstPredictedDelaysNs(const TBlobStorageGroupInfo &info, TGroupQueues &groupQueues,
        NKikimrBlobStorage::EVDiskQueueId queueId,
        ui64 *outWorstNs, ui64 *outNextToWorstNs, i32 *outWorstOrderNumber) const {
    *outWorstOrderNumber = -1;
    *outWorstNs = 0;
    *outNextToWorstNs = 0;
    ui32 totalVDisks = info.GetTotalVDisksNum();
    for (ui32 orderNumber = 0; orderNumber < totalVDisks; ++orderNumber) {
        ui64 predictedNs = groupQueues.GetPredictedDelayNsByOrderNumber(orderNumber, queueId);
        if (predictedNs > *outWorstNs) {
            *outNextToWorstNs = *outWorstNs;
            *outWorstNs = predictedNs;
            *outWorstOrderNumber = orderNumber;
        } else if (predictedNs > *outNextToWorstNs) {
            *outNextToWorstNs = predictedNs;
        }
    }
}

TString TBlackboard::ToString() const {
    TStringStream str;
    str << "{BlobStates size# " << BlobStates.size();
    str << Endl;
    str << " Data# {"; 
    str << Endl;
    for (auto it = BlobStates.begin(); it != BlobStates.end(); ++it) {
        str << "{id# " << it->first.ToString() << " state# {" << it->second.ToString() << "}}";
        str << Endl;
    }
    str << "}";
    str << Endl;
    str << " DoneBlobStates size # " << DoneBlobStates.size(); 
    str << Endl; 
    str << " DoneData# {"; 
    for (auto &kv : DoneBlobStates) { 
        str << "{id# " << kv.first.ToString() << " state# {" << kv.second.ToString() << "}}"; 
        str << Endl; 
    } 
    str << "}"; 
    str << Endl; 
    // ...
    str << " PutHandleClass# " << EPutHandleClass_Name(PutHandleClass);
    str << Endl;
    str << " GetHandleClass# " << EGetHandleClass_Name(GetHandleClass);
    str << Endl;
    str << "}";
    return str.Str();
}


}//NKikimr