forked from microsoft/cpp_client_telemetry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryStorageTests.cpp
365 lines (306 loc) · 11.4 KB
/
MemoryStorageTests.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
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
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
#include "common/Common.hpp"
#include "utils/Utils.hpp"
#include "offline/MemoryStorage.hpp"
#include "config/RuntimeConfig_Default.hpp"
#include "NullObjects.hpp"
#include "common/MockIOfflineStorageObserver.hpp"
#include <stdio.h>
#include <regex>
#include "pal/PAL.hpp"
#include <set>
#include <memory>
#include <thread>
#include <atomic>
using namespace testing;
using namespace MAT;
/// <summary>
/// Test observer. Currently the callbacks mechanism is not implemented at MemoryStorage abstraction level.
/// </summary>
/// <seealso cref="IOfflineStorageObserver" />
class TestObserver : public IOfflineStorageObserver
{
virtual void OnStorageOpened(std::string const & type) override
{
UNREFERENCED_PARAMETER(type);
}
virtual void OnStorageFailed(std::string const & reason) override
{
UNREFERENCED_PARAMETER(reason);
}
virtual void OnStorageOpenFailed(std::string const & reason) override
{
UNREFERENCED_PARAMETER(reason);
}
virtual void OnStorageTrimmed(std::map<std::string, size_t> const & numRecords) override
{
UNREFERENCED_PARAMETER(numRecords);
}
virtual void OnStorageRecordsDropped(std::map<std::string, size_t> const & numRecords) override
{
UNREFERENCED_PARAMETER(numRecords);
}
virtual void OnStorageRecordsRejected(std::map<std::string, size_t> const & numRecords) override
{
UNREFERENCED_PARAMETER(numRecords);
}
virtual void OnStorageRecordsSaved(size_t numRecords) override
{
UNREFERENCED_PARAMETER(numRecords);
}
};
/// <summary>
/// No-op NULL object pattern log manager
/// </summary>
NullLogManager testLogManager;
/// <summary>
/// Default test configuration
/// </summary>
RuntimeConfig_Default testConfig(testLogManager.GetLogConfiguration());
/// <summary>
/// Storage observer
/// </summary>
TestObserver testObserver;
// Run the list of all supported latencies
std::set<EventLatency> latencies =
{
EventLatency_Normal,
EventLatency_CostDeferred,
EventLatency_RealTime,
EventLatency_Max
};
constexpr size_t num_iterations = 10000;
/// <summary>
/// Adds events of various latencies to storage
/// </summary>
/// <param name="storage">The storage.</param>
/// <returns>Database size</returns>
size_t addEvents(MemoryStorage& storage)
{
// Add events
size_t total_db_size = 0;
for (size_t i = 0; i < num_iterations; i++)
{
for (const EventLatency &lat : latencies)
{
StorageRecord record{ PAL::generateUuidString(), "token", lat, EventPersistence_Critical, INT64_MIN + 1, { 5, 4, 3, 2, 1 }, 77, INT64_MAX - 1 };
total_db_size += record.blob.size() + sizeof(record);
storage.StoreRecord(record);
}
}
return total_db_size;
}
/// <summary>
/// Initialize, generate some records, save and retrieve these records back.
/// </summary>
/// <param name="">The .</param>
/// <param name="">The .</param>
/// <returns></returns>
TEST(MemoryStorageTests, StoreAndGetRecords)
{
MemoryStorage storage(testLogManager, testConfig);
// Observer callbacks are not currently implemented for this storage type
storage.Initialize(testObserver);
// Initial DB is empty
EXPECT_THAT(storage.GetSize(), 0);
// Check that EventLatency_Off doesn't get saved to ram queue
StorageRecord record{ PAL::generateUuidString(), "token", EventLatency_Off, EventPersistence_Critical, INT64_MIN + 1, { 5, 4, 3, 2, 1 }, 77, INT64_MAX - 1 };
EXPECT_THAT(storage.StoreRecord(record), false);
EXPECT_THAT(storage.GetSize(), 0);
// Read records in batches by latency
{
size_t total_db_size = addEvents(storage);
// Verify that the DB is the right size
EXPECT_THAT(storage.GetSize(), total_db_size);
// Verify the contents
size_t total_records = num_iterations * latencies.size();
for (auto latency : { EventLatency_Max , EventLatency_RealTime , EventLatency_CostDeferred , EventLatency_Normal })
{
auto records = storage.GetRecords(false, latency, 0);
EXPECT_THAT(records.size(), num_iterations);
EXPECT_THAT(records.size(), storage.LastReadRecordCount());
total_records -= records.size();
}
EXPECT_THAT(storage.GetSize(), 0);
EXPECT_THAT(total_records, 0);
}
// Read all records at once in one batch
{
size_t total_db_size = addEvents(storage);
// Verify that the DB is the right size
EXPECT_THAT(storage.GetSize(), total_db_size);
// Verify the contents
size_t total_records = num_iterations * latencies.size();
auto records = storage.GetRecords(false, EventLatency_Normal, 0);
EXPECT_THAT(records.size(), total_records);
EXPECT_THAT(records.size(), storage.LastReadRecordCount());
EXPECT_THAT(storage.GetSize(), 0);
}
// Verify that the ram queue is empty after read
EXPECT_THAT(storage.GetSize(), 0);
}
TEST(MemoryStorageTests, GetRecordsDeletesRecords)
{
MemoryStorage storage(testLogManager, testConfig);
std::vector<StorageRecordId> ids;
// Add some events to storage
auto total_db_size = addEvents(storage);
EXPECT_THAT(storage.GetSize(), total_db_size);
// Retrieve those into records
auto records = storage.GetRecords();
// Storage size is "zero" because all records are fetched
EXPECT_THAT(storage.GetSize(), 0);
EXPECT_THAT(storage.GetRecordCount(), 0);
EXPECT_THAT(storage.GetReservedCount(), 0);
EXPECT_THAT(records.size(), num_iterations * 4); // 4 latencies
}
TEST(MemoryStorageTests, DeleteAllRecords)
{
MemoryStorage storage(testLogManager, testConfig);
std::vector<StorageRecordId> ids;
// Add some events to storage
auto total_db_size = addEvents(storage);
EXPECT_THAT(storage.GetSize(), total_db_size);
// Retrieve those into records
storage.DeleteAllRecords();
// Storage size is "zero" because all records are fetched
EXPECT_THAT(storage.GetSize(), 0);
EXPECT_THAT(storage.GetRecordCount(), 0);
EXPECT_THAT(storage.GetReservedCount(), 0);
}
TEST(MemoryStorageTests, ReleaseRecords)
{
MemoryStorage storage(testLogManager, testConfig);
std::vector<StorageRecordId> ids;
HttpHeaders headers;
bool fromMemory = true;
// Add some events to storage
auto total_db_size = addEvents(storage);
EXPECT_THAT(storage.GetSize(), total_db_size);
auto total_records = storage.GetRecordCount();
// Retrieve those into records
std::vector<StorageRecord> records;
auto consumer = [&records](StorageRecord&& record) -> bool {
records.push_back(std::move(record));
return true; // want more
};
storage.GetAndReserveRecords(consumer, 1500);
// Storage size is "zero" because all records are currently reserved (in-flight)
EXPECT_THAT(storage.GetSize(), 0);
EXPECT_THAT(storage.GetRecordCount(), 0);
EXPECT_THAT(storage.GetReservedCount(), total_records);
EXPECT_THAT(records.size(), total_records);
// Track IDs of all reserved records
for (auto &record : records)
{
ids.push_back(record.id);
}
// Restore all reserved records
storage.ReleaseRecords(ids, false, headers, fromMemory);
EXPECT_THAT(storage.GetSize(), total_db_size);
EXPECT_THAT(storage.GetRecordCount(), total_records);
EXPECT_THAT(storage.GetReservedCount(), 0);
}
TEST(MemoryStorageTests, GetAndReserveSome)
{
MemoryStorage storage(testLogManager, testConfig);
storage.Initialize(testObserver);
addEvents(storage);
auto totalCount = storage.GetRecordCount();
constexpr size_t howMany = 32;
std::vector<StorageRecord> someRecords;
#if defined(__clang__)
#pragma clang diagnostic push // This appears to be a detection bug with constexpr variables in Clang9
#pragma clang diagnostic ignored "-Wunused-lambda-capture" // error : lambda capture 'howMany' is not required to be captured for this use[-Werror, -Wunused - lambda - capture]
#endif
storage.GetAndReserveRecords(
[&someRecords, howMany] (StorageRecord && record)->bool
{
if (someRecords.size() >= howMany) {
return false;
}
someRecords.emplace_back(std::move(record));
return true;
},
EventLatency_Normal
);
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
EXPECT_EQ(howMany, someRecords.size());
EXPECT_EQ(howMany, storage.LastReadRecordCount());
EXPECT_EQ(totalCount - howMany, storage.GetRecordCount());
}
// This method is not implemented for RAM storage
TEST(MemoryStorageTests, StoreSetting)
{
MemoryStorage storage(testLogManager, testConfig);
bool result = storage.StoreSetting("not_implemented", "not_implemented");
EXPECT_THAT(result, false);
}
// This method is not implemented for RAM storage
TEST(MemoryStorageTests, GetSetting)
{
MemoryStorage storage(testLogManager, testConfig);
auto result = storage.GetSetting("not_implemented");
EXPECT_THAT(result.empty(), true);
}
// This method is not implemented for RAM storage
TEST(MemoryStorageTests, ResizeDb)
{
MemoryStorage storage(testLogManager, testConfig);
EXPECT_THAT(storage.ResizeDb(), true);
}
constexpr size_t MAX_STRESS_THREADS = 20;
TEST(MemoryStorageTests, MultiThreadPerfTest)
{
MemoryStorage storage(testLogManager, testConfig);
std::vector<std::thread> workers;
std::atomic<size_t> threadCount(0);
// Add / Remove some events from several threads
for (size_t i = 0; i < MAX_STRESS_THREADS; i++) {
workers.push_back(std::thread([&storage, &threadCount]()
{
threadCount++;
// Add some events
addEvents(storage);
// Retrieve them
auto records = storage.GetRecords();
// Acquire IDs
std::vector<StorageRecordId> ids;
for (const auto &record : records)
ids.push_back(record.id);
// Release records
HttpHeaders headers;
bool inMemory = true;
storage.DeleteRecords(ids, headers, inMemory);
}));
}
// The issue here is that clang-libc++ Mac OS X runtime can schedule
// the main thread to continue running BEFORE propagating the spawned
// thread(s) into joinable state, i.e. the thread is created, but not
// assigned a native handle yet. What that in essence means that the
// logic that waits for completion of all workers may run into race
// condition with one of threads not being in joinable state YET...
// Means rather than waiting for completion - we actually skip a thread
// that is about to get scheduled!! Solution is to use the threadCount
// to yield until each thread is guaranteed spawned, assigned a native
// handle, after that we can iterate and wait for completion.
while (threadCount.load() != MAX_STRESS_THREADS)
{
std::this_thread::yield();
}
// Wait for completion of all worker threads
std::for_each(workers.begin(), workers.end(), [](std::thread &t)
{
if (t.joinable())
t.join();
});
// Once we're done - the storage has to be empty
EXPECT_THAT(storage.GetRecordCount(), 0);
EXPECT_THAT(storage.GetReservedCount(), 0);
EXPECT_THAT(storage.GetSize(), 0);
}