forked from ydb-platform/nbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_ut_stress.cpp
295 lines (240 loc) · 8.4 KB
/
server_ut_stress.cpp
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
#include "server.h"
#include "vhost_test.h"
#include <cloud/blockstore/libs/diagnostics/server_stats_test.h>
#include <cloud/blockstore/libs/service/device_handler.h>
#include <cloud/blockstore/libs/service/storage.h>
#include <cloud/storage/core/libs/common/error.h>
#include <cloud/storage/core/libs/diagnostics/logging.h>
#include <library/cpp/testing/unittest/registar.h>
#include <util/thread/factory.h>
#include <util/thread/lfqueue.h>
#include <atomic>
#include <random>
namespace NCloud::NBlockStore::NVhost {
using namespace NThreading;
namespace {
////////////////////////////////////////////////////////////////////////////////
class TScopedTasks
{
private:
using TThread = THolder<IThreadFactory::IThread>;
TVector<TThread> Workers;
std::atomic_flag ShouldStart = false;
public:
void Start()
{
ShouldStart.test_and_set();
}
void Stop()
{
for (auto& w: Workers) {
w->Join();
}
}
template <typename F>
void Add(F f)
{
Workers.push_back(SystemThreadFactory()->Run(
[this, f = std::move(f)]() {
while (!ShouldStart.test()) {}
f();
}
));
}
};
////////////////////////////////////////////////////////////////////////////////
class TStressStorage final
: public IStorage
{
private:
TLockFreeQueue<TPromise<NProto::TError>> Requests;
public:
NThreading::TFuture<NProto::TZeroBlocksResponse> ZeroBlocks(
TCallContextPtr callContext,
std::shared_ptr<NProto::TZeroBlocksRequest> request) override
{
Y_UNUSED(callContext);
Y_UNUSED(request);
return RegisterRequest<NProto::TZeroBlocksResponse>();
}
NThreading::TFuture<NProto::TWriteBlocksLocalResponse> WriteBlocksLocal(
TCallContextPtr callContext,
std::shared_ptr<NProto::TWriteBlocksLocalRequest> request) override
{
Y_UNUSED(callContext);
Y_UNUSED(request);
return RegisterRequest<NProto::TWriteBlocksLocalResponse>();
}
NThreading::TFuture<NProto::TReadBlocksLocalResponse> ReadBlocksLocal(
TCallContextPtr callContext,
std::shared_ptr<NProto::TReadBlocksLocalRequest> request) override
{
Y_UNUSED(callContext);
Y_UNUSED(request);
return RegisterRequest<NProto::TReadBlocksLocalResponse>();
}
NThreading::TFuture<NProto::TError> EraseDevice(
NProto::EDeviceEraseMethod method) override
{
Y_UNUSED(method);
return NThreading::MakeFuture(NProto::TError());
}
TStorageBuffer AllocateBuffer(size_t bytesCount) override
{
Y_UNUSED(bytesCount);
return nullptr;
}
void ReportIOError() override
{}
bool CompleteRequest(NProto::TError error)
{
TPromise<NProto::TError> promise;
if (Requests.Dequeue(&promise)) {
promise.SetValue(std::move(error));
return true;
}
return false;
}
private:
template<typename T>
TFuture<T> RegisterRequest()
{
auto promise = NewPromise<NProto::TError>();
auto future = promise.GetFuture();
Requests.Enqueue(std::move(promise));
return future.Apply([] (const auto& f) {
T response;
*response.MutableError() = f.GetValue();
return response;
});
}
};
////////////////////////////////////////////////////////////////////////////////
std::mt19937_64 CreateRandomEngine()
{
std::random_device rd;
std::array<ui32, std::mt19937_64::state_size> randomData;
std::generate(std::begin(randomData), std::end(randomData), std::ref(rd));
std::seed_seq seeds(std::begin(randomData), std::end(randomData));
return std::mt19937_64(seeds);
}
void SendRandomRequest(ITestVhostDevice& device)
{
thread_local auto eng = CreateRandomEngine();
std::uniform_int_distribution<ui64> dist1(0, 1);
EBlockStoreRequest type = dist1(eng) == 0
? EBlockStoreRequest::WriteBlocks
: EBlockStoreRequest::ReadBlocks;
std::uniform_int_distribution<ui64> dist2(0, 7999);
ui64 from = dist2(eng) * 512;
std::uniform_int_distribution<ui64> dist3(1, 4);
ui64 length = dist3(eng) * 1024;
TString buffer(length, '0');
auto sglist = TSgList{ TBlockDataRef(buffer.data(), buffer.size()) };
auto future = device.SendTestRequest(type, from, length, std::move(sglist));
future.Apply([holder = std::move(buffer)] (const auto& f) {
Y_UNUSED(holder);
return f.GetValue();
});
}
NProto::TError GetRandomError()
{
thread_local auto eng = CreateRandomEngine();
std::uniform_int_distribution<ui64> dist(0, 2);
auto code = dist(eng);
return MakeError(code == 0 ? S_OK : (code == 1 ? E_CANCELLED : E_FAIL));
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
Y_UNIT_TEST_SUITE(TServerStressTest)
{
Y_UNIT_TEST(Stress)
{
size_t threadsCount = 2;
size_t endpointCount = 4;
size_t consumerCount = 16;
size_t providerCount = 4;
TString socketPath = "./socket";
std::atomic<int> inflight = 0;
std::atomic<int> completed = 0;
auto serverStats = std::make_shared<TTestServerStats>();
serverStats->RequestStartedHandler = [&] (
TLog&, TMetricRequest&, TCallContext&, const TString&)
{
inflight.fetch_add(1);
};
serverStats->RequestCompletedHandler = [&] (
TLog&, TMetricRequest&, TCallContext&, const NProto::TError&)
{
inflight.fetch_sub(1);
completed.fetch_add(1);
};
auto queueFactory = std::make_shared<TTestVhostQueueFactory>();
auto server = CreateServer(
CreateLoggingService("console"),
std::move(serverStats),
queueFactory,
CreateDefaultDeviceHandlerFactory(),
TServerConfig{.ThreadsCount = threadsCount},
TVhostCallbacks());
server->Start();
Sleep(TDuration::MilliSeconds(300));
UNIT_ASSERT(queueFactory->Queues.size() == threadsCount);
TVector<std::shared_ptr<TStressStorage>> Storages;
TVector<std::shared_ptr<ITestVhostDevice>> Devices;
for (size_t i = 0; i < endpointCount; ++i) {
auto storage = std::make_shared<TStressStorage>();
Storages.push_back(storage);
auto future = server->StartEndpoint(
socketPath + ToString(i + 1),
std::move(storage),
TStorageOptions{
.DiskId = "disk" + ToString(i + 1),
.BlockSize = 4096,
.BlocksCount = 1024 * 1024,
.VhostQueuesCount = 2,
.UnalignedRequestsDisabled = false,
});
const auto& error = future.GetValue(TDuration::Seconds(5));
UNIT_ASSERT_C(!HasError(error), error);
}
for (auto& queue: queueFactory->Queues) {
auto devices = queue->GetDevices();
Devices.insert(Devices.end(), devices.begin(), devices.end());
}
TScopedTasks tasks;
std::atomic_flag shouldStop(false);
// Providers
for (ui32 i = 0; i < providerCount; ++i) {
tasks.Add([&, index = i] {
auto device = Devices[index % Devices.size()];
while (!shouldStop.test()) {
if (inflight.load() < 256) {
SendRandomRequest(*device);
}
};
});
}
// Consumers
for (ui32 i = 0; i < consumerCount; ++i) {
tasks.Add([&, index = i] {
auto storage = Storages[index % Storages.size()];
while (completed.load() < 5000) {
storage->CompleteRequest(GetRandomError());
}
shouldStop.test_and_set();
});
}
tasks.Start();
tasks.Stop();
for (size_t i = 0; i < endpointCount / 2; ++i) {
auto future = server->StopEndpoint(socketPath + ToString(i + 1));
const auto& error = future.GetValue(TDuration::Seconds(30));
UNIT_ASSERT_C(!HasError(error), error);
}
server->Stop();
UNIT_ASSERT_VALUES_EQUAL(0, inflight.load());
Cerr << "Amount of completed requests: " << completed.load() << Endl;
}
}
} // namespace NCloud::NBlockStore::NVhost