artdaq  v3_11_00
PreferSameHost_policy_t.cc
1 #define BOOST_TEST_MODULE PreferSameHost_policy_t
2 #include <boost/test/unit_test.hpp>
3 
4 #include "artdaq/RoutingPolicies/makeRoutingManagerPolicy.hh"
5 #include "fhiclcpp/ParameterSet.h"
6 #include "artdaq-utilities/Plugins/MakeParameterSet.hh"
7 
8 BOOST_AUTO_TEST_SUITE(PreferSameHost_policy_t)
9 
10 BOOST_AUTO_TEST_CASE(Simple)
11 {
12  fhicl::ParameterSet ps = artdaq::make_pset("");
13 
14  auto psh = artdaq::makeRoutingManagerPolicy("PreferSameHost", ps);
15 
16 
17  psh->Reset();
18  psh->AddReceiverToken(1, 1);
19  psh->AddReceiverToken(3, 1);
20  psh->AddReceiverToken(2, 1);
21  psh->AddReceiverToken(4, 1);
22  psh->AddReceiverToken(2, 1);
23  BOOST_REQUIRE_EQUAL(psh->GetReceiverCount(), 4);
24  auto secondTable = psh->GetCurrentTable();
25  BOOST_REQUIRE_EQUAL(secondTable.size(), 4);
26  BOOST_REQUIRE_EQUAL(secondTable[0].destination_rank, 1);
27  BOOST_REQUIRE_EQUAL(secondTable[1].destination_rank, 2);
28  BOOST_REQUIRE_EQUAL(secondTable[2].destination_rank, 3);
29  BOOST_REQUIRE_EQUAL(secondTable[3].destination_rank, 4);
30  BOOST_REQUIRE_EQUAL(secondTable[0].sequence_id, 1);
31  BOOST_REQUIRE_EQUAL(secondTable[1].sequence_id, 2);
32  BOOST_REQUIRE_EQUAL(secondTable[2].sequence_id, 3);
33  BOOST_REQUIRE_EQUAL(secondTable[3].sequence_id, 4);
34 
35  psh->AddReceiverToken(1, 0);
36 
37  auto thirdTable = psh->GetCurrentTable();
38  BOOST_REQUIRE_EQUAL(thirdTable.size(), 0);
39 
40  psh->AddReceiverToken(1, 2);
41  psh->AddReceiverToken(2, 1);
42  psh->AddReceiverToken(3, 1);
43  psh->AddReceiverToken(4, 2);
44  auto fourthTable = psh->GetCurrentTable();
45  BOOST_REQUIRE_EQUAL(fourthTable.size(), 4);
46  BOOST_REQUIRE_EQUAL(fourthTable[0].destination_rank, 1);
47 
48  psh->AddReceiverToken(3, 1);
49  auto fifthTable = psh->GetCurrentTable();
50  BOOST_REQUIRE_EQUAL(fifthTable.size(), 4);
51  BOOST_REQUIRE_EQUAL(fifthTable[0].destination_rank, 1);
52 }
53 
54 BOOST_AUTO_TEST_CASE(MinimumParticipants)
55 {
56  fhicl::ParameterSet ps = artdaq::make_pset("minimum_participants: 2");
57 
58  auto psh = artdaq::makeRoutingManagerPolicy("PreferSameHost", ps);
59 
60 
61  psh->Reset();
62  psh->AddReceiverToken(1, 1);
63  psh->AddReceiverToken(3, 1);
64  psh->AddReceiverToken(2, 1);
65  psh->AddReceiverToken(4, 1);
66  psh->AddReceiverToken(2, 1);
67  BOOST_REQUIRE_EQUAL(psh->GetReceiverCount(), 4);
68  auto secondTable = psh->GetCurrentTable();
69  BOOST_REQUIRE_EQUAL(secondTable.size(), 4);
70  BOOST_REQUIRE_EQUAL(secondTable[0].destination_rank, 1);
71  BOOST_REQUIRE_EQUAL(secondTable[1].destination_rank, 2);
72  BOOST_REQUIRE_EQUAL(secondTable[2].destination_rank, 3);
73  BOOST_REQUIRE_EQUAL(secondTable[3].destination_rank, 4);
74  BOOST_REQUIRE_EQUAL(secondTable[0].sequence_id, 1);
75  BOOST_REQUIRE_EQUAL(secondTable[1].sequence_id, 2);
76  BOOST_REQUIRE_EQUAL(secondTable[2].sequence_id, 3);
77  BOOST_REQUIRE_EQUAL(secondTable[3].sequence_id, 4);
78 
79  psh->AddReceiverToken(1, 0);
80 
81  auto thirdTable = psh->GetCurrentTable();
82  BOOST_REQUIRE_EQUAL(thirdTable.size(), 0);
83 
84  psh->AddReceiverToken(1, 1);
85  auto fourthTable = psh->GetCurrentTable();
86  BOOST_REQUIRE_EQUAL(fourthTable.size(), 2);
87 
88  BOOST_REQUIRE_EQUAL(fourthTable[0].destination_rank, 1);
89  BOOST_REQUIRE_EQUAL(fourthTable[1].destination_rank, 2);
90  BOOST_REQUIRE_EQUAL(fourthTable[0].sequence_id, 5);
91  BOOST_REQUIRE_EQUAL(fourthTable[1].sequence_id, 6);
92 
93  psh->AddReceiverToken(1, 2);
94  psh->AddReceiverToken(2, 2);
95  psh->AddReceiverToken(3, 1);
96  psh->AddReceiverToken(4, 2);
97  auto fifthTable = psh->GetCurrentTable();
98  BOOST_REQUIRE_EQUAL(fifthTable.size(), 7);
99  BOOST_REQUIRE_EQUAL(fifthTable[0].destination_rank, 1);
100 
101  psh->AddReceiverToken(3, 1);
102  auto sixthTable = psh->GetCurrentTable();
103  BOOST_REQUIRE_EQUAL(sixthTable.size(), 0);
104 }
105 
106 // RECENT CHANGE IN BEHAVIOR! Since the number of receivers is now dynamic as tokens are added, RoundRobin_policy will ALWAYS wait for at least minimum_participants to be available when it is positive!
107 BOOST_AUTO_TEST_CASE(LargeMinimumParticipants)
108 {
109  TLOG(TLVL_INFO) << "PreferSameHost_policy_t Test Case LargeMinimumParticipants BEGIN";
110  fhicl::ParameterSet ps = artdaq::make_pset("minimum_participants: 5");
111 
112  auto rr = artdaq::makeRoutingManagerPolicy("PreferSameHost", ps);
113 
114  rr->Reset();
115  rr->AddReceiverToken(1, 1);
116  rr->AddReceiverToken(3, 1);
117  rr->AddReceiverToken(2, 1);
118  rr->AddReceiverToken(3, 1);
119  rr->AddReceiverToken(2, 1);
120  BOOST_REQUIRE_EQUAL(rr->GetReceiverCount(), 3);
121  auto firstTable = rr->GetCurrentTable();
122  BOOST_REQUIRE_EQUAL(firstTable.size(), 0);
123 
124  rr->AddReceiverToken(5, 1);
125  rr->AddReceiverToken(6, 1);
126 
127  auto secondTable = rr->GetCurrentTable();
128  BOOST_REQUIRE_EQUAL(secondTable.size(), 5);
129  BOOST_REQUIRE_EQUAL(secondTable[0].destination_rank, 1);
130  BOOST_REQUIRE_EQUAL(secondTable[1].destination_rank, 2);
131  BOOST_REQUIRE_EQUAL(secondTable[2].destination_rank, 3);
132  BOOST_REQUIRE_EQUAL(secondTable[3].destination_rank, 5);
133  BOOST_REQUIRE_EQUAL(secondTable[4].destination_rank, 6);
134 
135  rr->AddReceiverToken(1, 1);
136  auto thirdTable = rr->GetCurrentTable();
137  BOOST_REQUIRE_EQUAL(thirdTable.size(), 0);
138 
139  TLOG(TLVL_INFO) << "PreferSameHost_policy_t Test Case LargeMinimumParticipants END";
140 }
141 
142 BOOST_AUTO_TEST_CASE(ManyMissingParticipants)
143 {
144  fhicl::ParameterSet ps = artdaq::make_pset("minimum_participants: -5");
145 
146  auto psh = artdaq::makeRoutingManagerPolicy("PreferSameHost", ps);
147 
148 
149  psh->Reset();
150  psh->AddReceiverToken(1, 1);
151  psh->AddReceiverToken(3, 1);
152  psh->AddReceiverToken(2, 1);
153  psh->AddReceiverToken(3, 1);
154  psh->AddReceiverToken(2, 1);
155  BOOST_REQUIRE_EQUAL(psh->GetReceiverCount(), 3);
156  auto secondTable = psh->GetCurrentTable();
157  BOOST_REQUIRE_EQUAL(secondTable.size(), 5);
158  BOOST_REQUIRE_EQUAL(secondTable[0].destination_rank, 1);
159 
160  psh->AddReceiverToken(1, 1);
161  auto thirdTable = psh->GetCurrentTable();
162  BOOST_REQUIRE_EQUAL(thirdTable.size(), 1);
163 }
164 
165 BOOST_AUTO_TEST_CASE(DataFlowMode)
166 {
167  fhicl::ParameterSet ps = artdaq::make_pset(
168  "routing_manager_mode: DataFlow \
169  host_map: [{rank: 1 host: \"testHost1\"}, \
170  {rank: 2 host: \"testHost1\"}, \
171  {rank: 3 host: \"testHost2\"}, \
172  {rank: 4 host: \"testHost1\"}, \
173  {rank: 5 host: \"testHost2\"}, \
174  {rank: 6 host: \"testHost3\"}]"
175  );
176 
177  auto psh = artdaq::makeRoutingManagerPolicy("PreferSameHost", ps);
178 
179 
180  psh->Reset();
181  psh->AddReceiverToken(1, 1);
182  psh->AddReceiverToken(3, 1);
183  psh->AddReceiverToken(2, 1);
184  psh->AddReceiverToken(3, 1);
185  psh->AddReceiverToken(2, 1);
186  BOOST_REQUIRE_EQUAL(psh->GetReceiverCount(), 3);
187  auto route = psh->GetRouteForSequenceID(1, 4);
188  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
189  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
190 
191  // Multiple hits for the same sequence ID are allowed, and should receive different information
192  route = psh->GetRouteForSequenceID(1, 5);
193  BOOST_REQUIRE_EQUAL(route.destination_rank, 3);
194  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
195 
196  // Except, that the same sequence ID from the same host should always get the same info
197  route = psh->GetRouteForSequenceID(1, 5);
198  BOOST_REQUIRE_EQUAL(route.destination_rank, 3);
199  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
200 
201  route = psh->GetRouteForSequenceID(2, 4);
202  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
203  BOOST_REQUIRE_EQUAL(route.sequence_id, 2);
204 
205  psh->AddReceiverToken(1, 1);
206  route = psh->GetRouteForSequenceID(2, 5);
207  BOOST_REQUIRE_EQUAL(route.destination_rank, 3);
208  BOOST_REQUIRE_EQUAL(route.sequence_id, 2);
209 
210  // Out-of-order sequence IDs are allowed
211  route = psh->GetRouteForSequenceID(1, 6);
212  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
213  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
214 
215  // Arbitrary sequence IDs are allowed
216  route = psh->GetRouteForSequenceID(10343, 4);
217  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
218  BOOST_REQUIRE_EQUAL(route.sequence_id, 10343);
219 }
220 
221 BOOST_AUTO_TEST_CASE(RequestBasedEventBuilding)
222 {
223  fhicl::ParameterSet ps = artdaq::make_pset(
224  "routing_manager_mode: RequestBasedEventBuilding routing_cache_size: 2 \
225  host_map: [{rank: 1 host: \"testHost1\"}, \
226  {rank: 2 host: \"testHost1\"}, \
227  {rank: 3 host: \"testHost2\"}, \
228  {rank: 4 host: \"testHost1\"}, \
229  {rank: 5 host: \"testHost2\"}, \
230  {rank: 6 host: \"testHost3\"}]"
231  );
232 
233  auto psh = artdaq::makeRoutingManagerPolicy("PreferSameHost", ps);
234 
235 
236  psh->Reset();
237  psh->AddReceiverToken(1, 1);
238  psh->AddReceiverToken(3, 1);
239  psh->AddReceiverToken(2, 1);
240  psh->AddReceiverToken(3, 1);
241  psh->AddReceiverToken(2, 1);
242  BOOST_REQUIRE_EQUAL(psh->GetReceiverCount(), 3);
243 
244  auto route = psh->GetRouteForSequenceID(1, 4);
245  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
246  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
247 
248  // Multiple hits for the same sequence ID should receive the same routing
249  route = psh->GetRouteForSequenceID(1, 5);
250  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
251  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
252 
253  // Only events which have started routing should be in the table
254  auto firstTable = psh->GetCurrentTable();
255  BOOST_REQUIRE_EQUAL(firstTable.size(), 1);
256  BOOST_REQUIRE_EQUAL(firstTable[0].destination_rank, 2);
257 
258  // Arbitrary Sequence IDs are allowed
259  route = psh->GetRouteForSequenceID(12343, 4);
260  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
261  BOOST_REQUIRE_EQUAL(route.sequence_id, 12343);
262 
263  // Out-of-order Sequence IDs are allowed
264  route = psh->GetRouteForSequenceID(4, 5);
265  BOOST_REQUIRE_EQUAL(route.destination_rank, 3);
266  BOOST_REQUIRE_EQUAL(route.sequence_id, 4);
267 
268  // Requests that arrive late still get the same info
269  route = psh->GetRouteForSequenceID(1, 6);
270  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
271  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
272 
273  // Check that things behave when tokens are exhausted...
274  route = psh->GetRouteForSequenceID(10, 6);
275  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
276  BOOST_REQUIRE_EQUAL(route.sequence_id, 10);
277  route = psh->GetRouteForSequenceID(11, 6);
278  BOOST_REQUIRE_EQUAL(route.destination_rank, 3);
279  BOOST_REQUIRE_EQUAL(route.sequence_id, 11);
280 
281  route = psh->GetRouteForSequenceID(50, 4);
282  BOOST_REQUIRE_EQUAL(route.destination_rank, -1);
283 
284  psh->AddReceiverToken(1, 1);
285  route = psh->GetRouteForSequenceID(50, 4);
286  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
287  BOOST_REQUIRE_EQUAL(route.sequence_id, 50);
288 
289  route = psh->GetRouteForSequenceID(50, 5);
290  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
291  BOOST_REQUIRE_EQUAL(route.sequence_id, 50);
292 
293  // Routing cache is sorted by sequence ID
294  auto secondTable = psh->GetCurrentTable();
295  BOOST_REQUIRE_EQUAL(secondTable.size(), 5);
296  BOOST_REQUIRE_EQUAL(secondTable[0].destination_rank, 3);
297  BOOST_REQUIRE_EQUAL(secondTable[0].sequence_id, 4);
298  BOOST_REQUIRE_EQUAL(secondTable[1].destination_rank, 2);
299  BOOST_REQUIRE_EQUAL(secondTable[1].sequence_id, 10);
300  BOOST_REQUIRE_EQUAL(secondTable[2].destination_rank, 3);
301  BOOST_REQUIRE_EQUAL(secondTable[2].sequence_id, 11);
302  BOOST_REQUIRE_EQUAL(secondTable[3].destination_rank, 1);
303  BOOST_REQUIRE_EQUAL(secondTable[3].sequence_id, 50);
304  BOOST_REQUIRE_EQUAL(secondTable[4].destination_rank, 1);
305  BOOST_REQUIRE_EQUAL(secondTable[4].sequence_id, 12343);
306 
307  // Since the routing cache has been set to 2, only the highest two events routed are here, as the cache is checked when generating tables
308  BOOST_REQUIRE_EQUAL(psh->GetCacheSize(), 2);
309  auto thirdTable = psh->GetCurrentTable();
310  BOOST_REQUIRE_EQUAL(thirdTable.size(), 0);
311 
312  BOOST_REQUIRE(psh->CacheHasRoute(50));
313  BOOST_REQUIRE(!psh->CacheHasRoute(4));
314  route = psh->GetRouteForSequenceID(50, 6);
315  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
316  BOOST_REQUIRE_EQUAL(route.sequence_id, 50);
317 
318  TLOG(TLVL_INFO) << "RoundRobin_policy_t Test Case RequestBasedEventBuilding END";
319 }
320 
321 BOOST_AUTO_TEST_SUITE_END()
std::shared_ptr< RoutingManagerPolicy > makeRoutingManagerPolicy(std::string const &policy_plugin_spec, fhicl::ParameterSet const &ps)
Load a RoutingManagerPolicy plugin.