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