aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/library/pdisk_io/aio_map.cpp
blob: 689d5bf8e5baa031f529691bace4850de411d476 (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
#include "aio.h" 
 
#include <ydb/core/blobstorage/pdisk/blobstorage_pdisk_util_countedqueueoneone.h>
 
#include <util/random/random.h> 
#include <util/thread/pool.h> 
 
namespace NKikimr { 
namespace NPDisk { 
 
struct TAsyncIoOperationMap : IObjectInQueue, IAsyncIoOperation { 
    TSectorMap &SectorMap; 
    TCountedQueueOneOne<IAsyncIoOperation*, 4 << 10> &CompleteQueue; 
    void *Cookie; 
    void *Data = nullptr; 
    ui64 Offset = 0; 
    ui64 Size = 0; 
    EType Type = IAsyncIoOperation::EType::PRead; 
    TReqId ReqId; 
    ICallback *Callback = nullptr; 
    NWilson::TTraceId TraceId; 
 
    TInstant Deadline; 
 
    TAsyncIoOperationMap(TSectorMap &sectorMap, 
            TCountedQueueOneOne<IAsyncIoOperation*, 4 << 10> &completeQueue, 
            void *cookie, TReqId reqId, NWilson::TTraceId *traceId) 
        : SectorMap(sectorMap) 
        , CompleteQueue(completeQueue) 
        , Cookie(cookie) 
        , ReqId(reqId) 
        , TraceId(traceId ? std::move(*traceId) : NWilson::TTraceId()) 
    {} 
 
    ~TAsyncIoOperationMap() override { 
    } 
 
    void* GetCookie() override { 
        return Cookie; 
    } 
 
    NWilson::TTraceId *GetTraceIdPtr() override { 
        return &TraceId; 
    } 
 
    void* GetData() override { 
        return Data; 
    } 
 
    ui64 GetOffset() override { 
        return Offset; 
    }; 
 
    ui64 GetSize() override { 
        return Size; 
    }; 
 
    EType GetType() override { 
        return Type; 
    }; 
 
    TReqId GetReqId() override { 
        return ReqId; 
    } 
 
    void Process(void*) override { 
        switch (Type) { 
            case IAsyncIoOperation::EType::PRead: 
                { 
                    SectorMap.Read((ui8*)Data, Size, Offset); 
                    break; 
                } 
            case IAsyncIoOperation::EType::PWrite: 
                { 
                    SectorMap.Write((ui8*)Data, Size, Offset); 
                    break; 
                } 
            default: 
                Y_FAIL_S("Unexpected op type# " << (i64)Type); 
        } 
        CompleteQueue.Push(this); 
    } 
 
    void SetCallback(ICallback *callback) override { 
        Callback = callback; 
    } 
 
    void ExecCallback(TAsyncIoOperationResult *result) override { 
        Callback->Exec(result); 
    } 
}; 
 
class TRandomWaitThreadPool : public IThreadPool { 
    TCountedQueueOneOne<TAsyncIoOperationMap*, 4 << 10> IncomingQueue; 
    TMultiMap<TInstant, TAsyncIoOperationMap*> WaitQueue; 
 
    TThread WorkThread; 
    std::atomic<bool> StopFlag; 
    std::pair<TDuration, TDuration> WaitParams; 
 
    ///////    Thread working part    ///// 
    static void *Proc(void* that) { 
        static_cast<TRandomWaitThreadPool*>(that)->Work(); 
        return nullptr; 
    } 
 
    void Work() { 
        bool receivedNullFromIncomingQueue = false; 
        while (true) { 
            TInstant now = TInstant::Now(); 
            TAtomicBase size = IncomingQueue.GetWaitingSize(); 
            for (TAtomicBase idx = 0; idx < size; ++idx) { 
                TAsyncIoOperationMap *op = IncomingQueue.Pop(); 
                if (op) { 
                    if (op->Deadline <= now) { 
                        op->Process(nullptr); 
                    } else { 
                        WaitQueue.emplace(op->Deadline, op); 
                    } 
                } else { 
                    receivedNullFromIncomingQueue = true; 
                } 
            } 
            if (StopFlag.load()) { 
                Cleanup(receivedNullFromIncomingQueue); 
                return; 
            } 
            now = TInstant::Now(); 
            auto it = WaitQueue.begin(); 
            while (it != WaitQueue.end() && it->first <= now) { 
                TAsyncIoOperationMap *op = it->second; 
                op->Process(nullptr); 
                auto curr = it; 
                ++it; 
                WaitQueue.erase(curr); 
            } 
            TDuration wait = TDuration::Max(); 
            if (WaitQueue) { 
                Y_VERIFY(WaitQueue.begin()->first > now); 
                wait = WaitQueue.begin()->first - now; 
            } 
            IncomingQueue.ProducedWait(wait); 
        } 
    } 
 
    void Cleanup(bool receiveNull) { 
        for (auto& op : WaitQueue) { 
            delete op.second; 
        } 
        WaitQueue.clear(); 
        while (!receiveNull) { 
            TAtomicBase size = IncomingQueue.GetWaitingSize(); 
            for (TAtomicBase idx = 0; idx < size; ++idx) { 
                TAsyncIoOperationMap *op = IncomingQueue.Pop(); 
                if (op) { 
                    delete op; 
                } else { 
                    receiveNull = true; 
                } 
            } 
        } 
    } 
 
    ///////    Intefrace   ///// 
    bool Add(IObjectInQueue *obj) override { 
        if (StopFlag.load()) { 
            return false; 
        } 
        auto op = static_cast<TAsyncIoOperationMap*>(obj); 
        op->Deadline = TInstant::Now() + WaitParams.first 
            + TDuration::MicroSeconds(RandomNumber<ui32>(WaitParams.second.MicroSeconds())); 
        IncomingQueue.Push(op); 
        return true; 
    } 
 
    size_t Size() const noexcept override { 
        return 0; // Size of thread pool, meaningless for that class 
    } 
 
    void Start(size_t, size_t) override { 
    } 
 
    void Stop() noexcept override { 
        Y_VERIFY(!StopFlag.load()); 
        StopFlag.store(true); 
        IncomingQueue.Push(nullptr); 
        WorkThread.Join(); 
    } 
 
 
public: 
    TRandomWaitThreadPool(const std::pair<TDuration, TDuration>& waitParams) 
        : WorkThread(TThread::TParams(Proc, this)) 
        , StopFlag(false) 
        , WaitParams(waitParams) 
    { 
        WorkThread.Start(); 
    } 
 
    ~TRandomWaitThreadPool(){ 
    } 
}; 
 
class TAsyncIoContextMap : public IAsyncIoContext { 
    TAutoPtr<IThreadPool> Queue; 
    TIntrusivePtr<TSectorMap> SectorMap; 
    TCountedQueueOneOne<IAsyncIoOperation*, 4 << 10> CompleteQueue; 
    ui64 MaxEvents = 0; 
    int LastErrno = 0; 
 
    TPDiskDebugInfo PDiskInfo; 
public: 
 
    TAsyncIoContextMap(const TString &path, ui32 pDiskId, TIntrusivePtr<TSectorMap> sectorMap) 
        : SectorMap(sectorMap) 
        , PDiskInfo(path, pDiskId, "map") 
    {} 
 
    ~TAsyncIoContextMap() { 
    } 
 
    void InitializeMonitoring(TPDiskMon &mon) override { 
        Y_UNUSED(mon); 
    } 
 
    IAsyncIoOperation* CreateAsyncIoOperation(void* cookie, TReqId reqId, NWilson::TTraceId *traceId) override { 
        IAsyncIoOperation *operation = new TAsyncIoOperationMap(*SectorMap, CompleteQueue, cookie, reqId, traceId); 
        return operation; 
    } 
 
    void DestroyAsyncIoOperation(IAsyncIoOperation *operation) override { 
        delete operation; 
    } 
 
    EIoResult Destroy() override { 
        Queue->Stop(); 
        SectorMap->Unlock(); 
 
        return EIoResult::Ok; 
    } 
 
    i64 GetEvents(ui64 minEvents, ui64 maxEvents, TAsyncIoOperationResult *events, TDuration timeout) override { 
        ui64 outputIdx = 0; 
        TInstant startTime = TInstant::Now(); 
        TInstant deadline = startTime + timeout; 
        while (true) { 
            TAtomicBase size = CompleteQueue.GetWaitingSize(); 
            if (size > 0) { 
                for (TAtomicBase idx = 0; idx < size; ++idx) { 
                    TAsyncIoOperationMap *op = static_cast<TAsyncIoOperationMap*>(CompleteQueue.Pop()); 
                    events[outputIdx].Operation = op; 
                    events[outputIdx].Result = (RandomNumber<double>() < 
                            SectorMap->ImitateIoErrorProbability.load()) 
                        ? EIoResult::FakeError 
                        : EIoResult::Ok; 
                    if (op->GetType() == IAsyncIoOperation::EType::PRead && 
                            RandomNumber<double>() < SectorMap->ImitateReadIoErrorProbability.load()) { 
                        events[outputIdx].Result = EIoResult::FakeError; 
                    } 
                    events[outputIdx].Operation->ExecCallback(&events[outputIdx]); 
                    ++outputIdx; 
                    if (outputIdx == maxEvents) { 
                        return outputIdx; 
                    } 
                } 
            } else { 
                if (outputIdx >= minEvents) { 
                    return outputIdx; 
                } 
                if (!timeout.NanoSeconds()) { 
                    CompleteQueue.ProducedWaitI(); 
                } else { 
                    TInstant now = TInstant::Now(); 
                    if (now > deadline) { 
                        return outputIdx; 
                    } 
                    TDuration remainingTime = deadline - now; 
                    bool isOk = CompleteQueue.ProducedWait(remainingTime); 
                    if (!isOk) { 
                        return outputIdx; 
                    } 
                } 
            } 
        } 
    } 
 
    void PrepareImpl(IAsyncIoOperation *op, void *data, size_t size, size_t offset, 
            IAsyncIoOperation::EType type) { 
        TAsyncIoOperationMap *operation = static_cast<TAsyncIoOperationMap*>(op); 
        operation->Data = data; 
        operation->Size = size; 
        operation->Offset = offset; 
        operation->Type = type; 
    } 
 
    void PreparePRead(IAsyncIoOperation *op, void *destination, size_t size, size_t offset) override { 
        PrepareImpl(op, destination, size, offset, IAsyncIoOperation::EType::PRead); 
    } 
 
    void PreparePWrite(IAsyncIoOperation *op, const void *source, size_t size, size_t offset) override { 
        PrepareImpl(op, const_cast<void*>(source), size, offset, IAsyncIoOperation::EType::PWrite); 
    } 
 
    void PreparePTrim(IAsyncIoOperation *op, size_t size, size_t offset) override { 
        PrepareImpl(op, nullptr, size, offset, IAsyncIoOperation::EType::PTrim); 
    } 
 
    bool DoTrim(IAsyncIoOperation *op) override { 
        Sleep(TDuration::MilliSeconds(40)); 
 
        SectorMap->Trim(op->GetSize(), op->GetOffset()); 
        return true; 
    } 
 
    EIoResult Setup(ui64 maxEvents, bool doLock) override { 
        if (doLock) { 
            bool isLocked = SectorMap->Lock(); 
            if (!isLocked) { 
                return EIoResult::FileOpenError; 
            } 
        } 
        MaxEvents = maxEvents; 
        if (SectorMap->ImitateRandomWait) { 
            Queue = new TRandomWaitThreadPool(*SectorMap->ImitateRandomWait); 
        } else { 
            Queue = CreateThreadPool(1, MaxEvents); 
        } 
        return EIoResult::Ok; 
    } 
 
    EIoResult Submit(IAsyncIoOperation *op, ICallback *callback) override { 
        op->SetCallback(callback); 
        TAsyncIoOperationMap *operation = static_cast<TAsyncIoOperationMap*>(op); 
        bool isOk = Queue->Add(operation); 
        return isOk ? EIoResult::Ok : EIoResult::TryAgain; 
    } 
 
    void SetActorSystem(TActorSystem* /*actorSystem*/) override 
    {} 
 
    TString GetPDiskInfo() override { 
        return PDiskInfo.Str(); 
    } 
 
    int GetLastErrno() override { 
        return LastErrno; 
    } 
 
    TFileHandle *GetFileHandle() override { 
        return nullptr; 
    } 
}; 
 
std::unique_ptr<IAsyncIoContext> CreateAsyncIoContextMap(const TString &path, ui32 pDiskId, TIntrusivePtr<TSectorMap> sectorMap) { 
    return std::make_unique<TAsyncIoContextMap>(path, pDiskId, sectorMap); 
} 
 
} // NPDisk 
} // NKikimr