Skip to content

Commit c25360f

Browse files
authored
Rework BlockDevice tests (#10107)
1 parent 99bbf73 commit c25360f

File tree

3 files changed

+54
-79
lines changed

3 files changed

+54
-79
lines changed

ydb/core/blobstorage/pdisk/blobstorage_pdisk_blockdevice_ut.cpp

Lines changed: 38 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99

1010
#include <ydb/core/control/immediate_control_board_wrapper.h>
1111

12+
#include <library/cpp/deprecated/atomic/atomic.h>
1213
#include <library/cpp/testing/unittest/registar.h>
1314
#include <util/folder/dirut.h>
1415
#include <util/folder/tempdir.h>
15-
#include <library/cpp/deprecated/atomic/atomic.h>
16-
#include <util/system/condvar.h>
16+
#include <util/random/entropy.h>
1717
#include <util/string/printf.h>
18+
#include <util/system/condvar.h>
1819
#include <util/system/file.h>
1920
#include <util/system/sanitizers.h>
2021

2122
namespace NKikimr {
2223

24+
using namespace NPDisk;
25+
2326
class TWriter : public NPDisk::TCompletionAction {
2427
NPDisk::IBlockDevice &Device;
2528
NPDisk::TBuffer *Buffer;
@@ -158,8 +161,6 @@ TString CreateFile(const char *baseDir, ui32 dataSize) {
158161
return path;
159162
}
160163

161-
constexpr TDuration TIMEOUT = NSan::PlainOrUnderSanitizer(TDuration::Seconds(120), TDuration::Seconds(360));
162-
163164
void WaitForValue(TAtomic *counter, TDuration maxDuration, TAtomicBase expectedValue) {
164165
TInstant finishTime = TInstant::Now() + maxDuration;
165166
while (TInstant::Now() < finishTime) {
@@ -171,30 +172,37 @@ void WaitForValue(TAtomic *counter, TDuration maxDuration, TAtomicBase expectedV
171172
}
172173
}
173174
}
175+
UNIT_FAIL("deadline exceeded");
174176
}
175177

176-
void RunWriteTestWithSectorMap(NPDisk::NSectorMap::EDiskMode diskMode, ui32 diskSize, ui32 bufferSize, bool sequential = true) {
178+
void RunWriteTestWithSectorMap(NSectorMap::EDiskMode diskMode, ui64 diskSize, ui32 bufferSize) {
177179
const TIntrusivePtr<::NMonitoring::TDynamicCounters> counters = new ::NMonitoring::TDynamicCounters;
178180
THolder<TPDiskMon> mon(new TPDiskMon(counters, 0, nullptr));
179181

180182
TActorSystemCreator creator;
181-
TIntrusivePtr<NPDisk::TSectorMap> sectorMap = new NPDisk::TSectorMap(diskSize, diskMode);
182-
THolder<NPDisk::IBlockDevice> device(NPDisk::CreateRealBlockDeviceWithDefaults(
183-
/* path can be empty when sector map is used */ "", *mon, NPDisk::TDeviceMode::None, sectorMap, creator.GetActorSystem()));
184-
185-
NPDisk::TAlignedData data(bufferSize);
186-
memset(data.Get(), 0, data.Size());
187-
188-
TAtomic completedWrites = 0;
189-
190-
ui32 offsetIncrement = sequential ? bufferSize : 2 * bufferSize;
191-
TAtomic expectedWrites = diskSize / (offsetIncrement);
192-
193-
for (ui64 offset = 0; offset < diskSize; offset += offsetIncrement) {
194-
device->PwriteAsync(data.Get(), data.Size(), offset, new TCompletionWorkerWithCounter(completedWrites), NPDisk::TReqId(NPDisk::TReqId::Test1, 0), {});
183+
TIntrusivePtr<TSectorMap> sectorMap;
184+
TTempDir tempDir;
185+
TString path;
186+
if (diskMode == NSectorMap::EDiskMode::DM_COUNT) {
187+
path = CreateFile(tempDir().c_str(), diskSize);
188+
} else {
189+
/* path can be empty when sector map is used */
190+
sectorMap = new TSectorMap(diskSize, diskMode);
191+
}
192+
THolder<IBlockDevice> device(CreateRealBlockDeviceWithDefaults(path, *mon, TDeviceMode::None, sectorMap, creator.GetActorSystem()));
193+
194+
TAlignedData writeData(bufferSize);
195+
TAlignedData readData(bufferSize);
196+
TSimpleTimer t;
197+
EntropyPool().Read(writeData.Get(), writeData.Size());
198+
Ctest << bufferSize / t.Get().SecondsFloat() / 1e9 << " GB/s" << Endl;
199+
200+
for (ui64 offset : {ui64(0), NSectorMap::SECTOR_SIZE, 7 * NSectorMap::SECTOR_SIZE, diskSize - bufferSize}) {
201+
device->PwriteSync(writeData.Get(), writeData.Size(), offset, NPDisk::TReqId(NPDisk::TReqId::Test1, 0), nullptr);
202+
NSan::Poison(readData.Get(), readData.Size());
203+
device->PreadSync(readData.Get(), readData.Size(), offset, NPDisk::TReqId(NPDisk::TReqId::Test1, 0), nullptr);
204+
UNIT_ASSERT(writeData == readData);
195205
}
196-
197-
WaitForValue(&completedWrites, TIMEOUT, expectedWrites);
198206

199207
device.Destroy();
200208
}
@@ -219,59 +227,16 @@ Y_UNIT_TEST_SUITE(TBlockDeviceTest) {
219227
device->PwriteSync(data.Get(), data.Size(), 0, {}, nullptr);
220228

221229
device.Destroy();
222-
Ctest << "Done" << Endl;
223-
}
224-
225-
Y_UNIT_TEST(TestWriteWithNoneSectorMap2GbDisk8MbBuffer) {
226-
ui32 diskSize = 2 * 1024 * 1024 * 1024u;
227-
ui32 bufferSize = 8 * 1024 * 1024u;
228-
RunWriteTestWithSectorMap(NPDisk::NSectorMap::DM_NONE, diskSize, bufferSize);
229-
Ctest << "Done" << Endl;
230-
}
231-
232-
Y_UNIT_TEST(TestWriteWithNoneSectorMap2GbDisk8MbBufferNonSequential) {
233-
ui32 diskSize = 2 * 1024 * 1024 * 1024u;
234-
ui32 bufferSize = 8 * 1024 * 1024u;
235-
bool sequential = false;
236-
RunWriteTestWithSectorMap(NPDisk::NSectorMap::DM_NONE, diskSize, bufferSize, sequential);
237-
Ctest << "Done" << Endl;
238-
}
239-
240-
Y_UNIT_TEST(TestWriteWithNoneSectorMap2GbDisk32KbBuffer) {
241-
ui32 diskSize = 2 * 1024 * 1024 * 1024u;
242-
ui32 bufferSize = 32 * 1024u;
243-
RunWriteTestWithSectorMap(NPDisk::NSectorMap::DM_NONE, diskSize, bufferSize);
244-
Ctest << "Done" << Endl;
245230
}
246231

247-
Y_UNIT_TEST(TestWriteWithNoneSectorMap2GbDisk32KbBufferNonSequential) {
248-
ui32 diskSize = 2 * 1024 * 1024 * 1024u;
249-
ui32 bufferSize = 32 * 1024u;
250-
bool sequential = false;
251-
RunWriteTestWithSectorMap(NPDisk::NSectorMap::DM_NONE, diskSize, bufferSize, sequential);
252-
Ctest << "Done" << Endl;
253-
}
254-
255-
Y_UNIT_TEST(TestWriteWithHddSectorMap2GbDisk8MbBuffer) {
256-
ui32 diskSize = 2 * 1024 * 1024 * 1024u;
257-
ui32 bufferSize = 8 * 1024 * 1024u;
258-
RunWriteTestWithSectorMap(NPDisk::NSectorMap::DM_HDD, diskSize, bufferSize);
259-
Ctest << "Done" << Endl;
260-
}
261-
262-
Y_UNIT_TEST(TestWriteWithHddSectorMap2GbDisk8MbBufferNonSequential) {
263-
ui32 diskSize = 2 * 1024 * 1024 * 1024u;
264-
ui32 bufferSize = 8 * 1024 * 1024u;
265-
bool sequential = false;
266-
RunWriteTestWithSectorMap(NPDisk::NSectorMap::DM_HDD, diskSize, bufferSize, sequential);
267-
Ctest << "Done" << Endl;
268-
}
269-
270-
Y_UNIT_TEST(TestWriteWithHddSectorMap2GbDisk32KbBuffer) {
271-
ui32 diskSize = 2 * 1024 * 1024 * 1024u;
272-
ui32 bufferSize = 32 * 1024u;
273-
RunWriteTestWithSectorMap(NPDisk::NSectorMap::DM_HDD, diskSize, bufferSize);
274-
Ctest << "Done" << Endl;
232+
Y_UNIT_TEST(TestWriteSectorMapAllTypes) {
233+
ui64 diskSize = 2_GB;
234+
// DM_COUNT stands for real file
235+
for (ui32 type = NPDisk::NSectorMap::DM_NONE; type <= NPDisk::NSectorMap::DM_COUNT; ++type) {
236+
for (ui32 bufferSize : {256_KB, 4_MB}) {
237+
RunWriteTestWithSectorMap((NPDisk::NSectorMap::EDiskMode)type, diskSize, bufferSize);
238+
}
239+
}
275240
}
276241

277242
Y_UNIT_TEST(WriteReadRestart) {
@@ -319,7 +284,7 @@ Y_UNIT_TEST_SUITE(TBlockDeviceTest) {
319284
}
320285
}
321286

322-
Cerr << AtomicGet(counter) << Endl;
287+
Ctest << AtomicGet(counter) << Endl;
323288
device.Destroy();
324289
UNIT_ASSERT(AtomicGet(counter) == totalRequests);
325290
}

ydb/library/pdisk_io/buffers.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ struct TAlignedData {
6666
spdkState->Free(AlignedBuffer);
6767
}
6868
}
69+
70+
friend bool operator==(const TAlignedData& lhs, const TAlignedData& rhs) {
71+
if (lhs.Size() != rhs.Size())
72+
return false;
73+
74+
if (memcmp(lhs.Get(), rhs.Get(), rhs.Size()) != 0)
75+
return false;
76+
77+
return true;
78+
}
6979
};
7080

7181
//
@@ -174,4 +184,3 @@ TBufferPool *CreateBufferPool(ui64 size, ui32 bufferCount, bool UseHugePages, TB
174184

175185
} // NPDisk
176186
} // NKikimr
177-

ydb/library/pdisk_io/sector_map.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ class TSectorMap : public TThrRefBase {
196196

197197
std::atomic<ui64> AllocatedBytes;
198198

199+
private:
200+
THashMap<ui64, TString> Map;
201+
NSectorMap::EDiskMode DiskMode = NSectorMap::DM_NONE;
202+
THolder<NSectorMap::TSectorOperationThrottler> SectorOperationThrottler;
203+
204+
public:
199205
TSectorMap(ui64 deviceSize = 0, NSectorMap::EDiskMode diskMode = NSectorMap::DM_NONE)
200206
: DeviceSize(deviceSize)
201207
, IsLocked(false)
@@ -345,11 +351,6 @@ class TSectorMap : public TThrRefBase {
345351
}
346352
return nullptr;
347353
}
348-
349-
private:
350-
THashMap<ui64, TString> Map;
351-
NSectorMap::EDiskMode DiskMode = NSectorMap::DM_NONE;
352-
THolder<NSectorMap::TSectorOperationThrottler> SectorOperationThrottler;
353354
};
354355

355356
} // NPDisk

0 commit comments

Comments
 (0)