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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Copyright (C) 2016 Red Hat Inc.
*
* Author: J. Eric Ivancich <ivancich@redhat.com>
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version
* 2.1, as published by the Free Software Foundation. See file
* COPYING.
*/
#include <chrono>
#include <mutex>
#include <functional>
#include <iostream>
#include "dmclock_client.h"
#include "dmclock_util.h"
#include "gtest/gtest.h"
namespace dmc = crimson::dmclock;
namespace crimson {
namespace dmclock {
/*
* Allows us to test the code provided with the mutex provided locked.
*/
static void test_locked(std::mutex& mtx, std::function<void()> code) {
std::lock_guard<std::mutex> l(mtx);
code();
}
TEST(dmclock_client, server_erase) {
using ServerId = int;
// using ClientId = int;
ServerId server = 101;
// ClientId client = 3;
// dmc::PhaseType resp_params = dmc::PhaseType::reservation;
dmc::ServiceTracker<ServerId> st(std::chrono::seconds(2),
std::chrono::seconds(3));
auto lock_st = [&](std::function<void()> code) {
test_locked(st.data_mtx, code);
};
/* The timeline should be as follows:
*
* 0 seconds : request created
*
* 1 seconds : map is size 1
*
* 2 seconds : clean notes first mark; +2 is base for further calcs
*
* 4 seconds : clean does nothing except makes another mark
*
* 5 seconds : when we're secheduled to erase (+2 + 3)
*
* 5 seconds : since the clean job hasn't run yet, map still size 1
*
* 6 seconds : clean erases server
*
* 7 seconds : verified server is gone (map size 0)
*/
lock_st([&] () {
EXPECT_EQ(0u, st.server_map.size()) <<
"server map initially has size 0";
});
std::this_thread::sleep_for(std::chrono::seconds(1));
// call for side effects
(void) st.get_req_params(server);
lock_st([&] () {
EXPECT_EQ(1u, st.server_map.size()) <<
"server map has size 1 after first request";
});
std::this_thread::sleep_for(std::chrono::seconds(4));
lock_st([&] () {
EXPECT_EQ(1u, st.server_map.size()) <<
"server map has size 1 just before erase";
});
std::this_thread::sleep_for(std::chrono::seconds(2));
lock_st([&] () {
EXPECT_EQ(0u, st.server_map.size()) <<
"server map has size 0 just after erase";
});
} // TEST
TEST(dmclock_client, delta_rho_values_borrowing_tracker) {
using ServerId = int;
// using ClientId = int;
ServerId server1 = 101;
ServerId server2 = 7;
// ClientId client = 3;
// RespParams<ServerId> resp_params(server, dmc::PhaseType::reservation);
dmc::ServiceTracker<ServerId,dmc::BorrowingTracker> st(std::chrono::seconds(2),
std::chrono::seconds(3));
auto rp1 = st.get_req_params(server1);
EXPECT_EQ(1u, rp1.delta) <<
"delta should be 1 with no intervening responses by" <<
"other servers";
EXPECT_EQ(1u, rp1.rho) <<
"rho should be 1 with no intervening reservation responses by" <<
"other servers";
auto rp2 = st.get_req_params(server1);
EXPECT_EQ(1u, rp2.delta) <<
"delta should be 1 with no intervening responses by" <<
"other servers";
EXPECT_EQ(1u, rp2.rho) <<
"rho should be 1 with no intervening reservation responses by" <<
"other servers";
// RESPONSE
st.track_resp(server1, dmc::PhaseType::priority, 1u);
auto rp3 = st.get_req_params(server1);
EXPECT_EQ(1u, rp3.delta) <<
"delta should be 1 with no intervening responses by" <<
"other servers";
EXPECT_EQ(1u, rp3.rho) <<
"rho should be 1 with no intervening reservation responses by" <<
"other servers";
// RESPONSE
st.track_resp(server2, dmc::PhaseType::priority, 1u);
auto rp4 = st.get_req_params(server1);
EXPECT_EQ(1u, rp4.delta) <<
"delta should be 2 with one intervening priority response by " <<
"another server";
EXPECT_EQ(1u, rp4.rho) <<
"rho should be 1 with one intervening priority responses by " <<
"another server";
auto rp5 = st.get_req_params(server1);
EXPECT_EQ(1u, rp5.delta) <<
"delta should be 1 with no intervening responses by" <<
"other servers";
EXPECT_EQ(1u, rp5.rho) <<
"rho should be 1 with no intervening reservation responses by" <<
"other servers";
// RESPONSE
st.track_resp(server2, dmc::PhaseType::reservation, 1u);
auto rp6 = st.get_req_params(server1);
EXPECT_EQ(1u, rp6.delta) <<
"delta should be 2 with one intervening reservation response by " <<
"another server";
EXPECT_EQ(1u, rp6.rho) <<
"rho should be 2 with one intervening reservation responses by " <<
"another server";
st.track_resp(server2, dmc::PhaseType::reservation, 1u);
st.track_resp(server1, dmc::PhaseType::priority, 1u);
st.track_resp(server2, dmc::PhaseType::priority, 1u);
st.track_resp(server2, dmc::PhaseType::reservation, 1u);
st.track_resp(server1, dmc::PhaseType::reservation, 1u);
st.track_resp(server1, dmc::PhaseType::priority, 1u);
st.track_resp(server2, dmc::PhaseType::priority, 1u);
auto rp7 = st.get_req_params(server1);
EXPECT_EQ(5u, rp7.delta) <<
"delta should be 5 with four intervening responses by " <<
"another server";
EXPECT_EQ(1u, rp7.rho) <<
"rho should be 1 with two intervening reservation responses by " <<
"another server";
auto rp7b = st.get_req_params(server2);
EXPECT_EQ(9u, rp7b.delta) <<
"delta should be 9 with three intervening responses by " <<
"another server";
EXPECT_EQ(4u, rp7b.rho) <<
"rho should be 4 with one intervening reservation responses by " <<
"another server";
auto rp8 = st.get_req_params(server1);
EXPECT_EQ(1u, rp8.delta) <<
"delta should be 1 with no intervening responses by " <<
"another server";
EXPECT_EQ(1u, rp8.rho) <<
"rho should be 1 with no intervening reservation responses by " <<
"another server";
auto rp8b = st.get_req_params(server2);
EXPECT_EQ(1u, rp8b.delta) <<
"delta should be 1 with no intervening responses by " <<
"another server";
EXPECT_EQ(1u, rp8b.rho) <<
"rho should be 1 with no intervening reservation responses by " <<
"another server";
} // TEST
// NB: the BorrowingTracker has not been fully tested and the
// expected values below have not yet been compared with the
// theoretically correct values.
TEST(dmclock_client, delta_rho_values_orig_tracker) {
using ServerId = int;
ServerId server1 = 101;
ServerId server2 = 7;
dmc::ServiceTracker<ServerId,OrigTracker>
st(std::chrono::seconds(2), std::chrono::seconds(3));
auto rp1 = st.get_req_params(server1);
EXPECT_EQ(1u, rp1.delta);
EXPECT_EQ(1u, rp1.rho);
auto rp2 = st.get_req_params(server1);
EXPECT_EQ(0u, rp2.delta);
EXPECT_EQ(0u, rp2.rho);
st.track_resp(server1, dmc::PhaseType::priority, 1u);
auto rp3 = st.get_req_params(server1);
EXPECT_EQ(0u, rp3.delta);
EXPECT_EQ(0u, rp3.rho);
st.track_resp(server2, dmc::PhaseType::priority, 1u);
auto rp4 = st.get_req_params(server1);
EXPECT_EQ(1u, rp4.delta);
EXPECT_EQ(0u, rp4.rho);
auto rp5 = st.get_req_params(server1);
EXPECT_EQ(0u, rp5.delta);
EXPECT_EQ(0u, rp5.rho);
st.track_resp(server2, dmc::PhaseType::reservation, 1u);
auto rp6 = st.get_req_params(server1);
EXPECT_EQ(1u, rp6.delta);
EXPECT_EQ(1u, rp6.rho);
// auto rp6_b = st.get_req_params(server2);
st.track_resp(server2, dmc::PhaseType::reservation, 1u);
st.track_resp(server1, dmc::PhaseType::priority, 1u);
st.track_resp(server2, dmc::PhaseType::priority, 1u);
st.track_resp(server2, dmc::PhaseType::reservation, 1u);
st.track_resp(server1, dmc::PhaseType::reservation, 1u);
st.track_resp(server1, dmc::PhaseType::priority, 1u);
st.track_resp(server2, dmc::PhaseType::priority, 1u);
auto rp7 = st.get_req_params(server1);
EXPECT_EQ(4u, rp7.delta);
EXPECT_EQ(2u, rp7.rho);
auto rp7b = st.get_req_params(server2);
EXPECT_EQ(3u, rp7b.delta);
EXPECT_EQ(1u, rp7b.rho);
auto rp8 = st.get_req_params(server1);
EXPECT_EQ(0u, rp8.delta);
EXPECT_EQ(0u, rp8.rho);
auto rp8b = st.get_req_params(server2);
EXPECT_EQ(0u, rp8b.delta);
EXPECT_EQ(0u, rp8b.rho);
} // TEST
} // namespace dmclock
} // namespace crimson
|