blob: c53671135f3c518a1c4e8c5ecad53eed99fbb4d6 [file] [log] [blame]
James Kuszmaul38735e82019-12-07 16:42:06 -08001#include "aos/events/logging/logger.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -08002
3#include "aos/events/event_loop.h"
Austin Schuh01b4c352020-09-21 23:09:39 -07004#include "aos/events/message_counter.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -08005#include "aos/events/ping_lib.h"
6#include "aos/events/pong_lib.h"
7#include "aos/events/simulated_event_loop.h"
Austin Schuh0de30f32020-12-06 12:44:28 -08008#include "aos/network/remote_message_generated.h"
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07009#include "aos/network/timestamp_generated.h"
Austin Schuhc243b422020-10-11 15:35:08 -070010#include "aos/testing/tmpdir.h"
Austin Schuh2f8fd752020-09-01 22:38:28 -070011#include "aos/util/file.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080012#include "glog/logging.h"
Austin Schuh6f3babe2020-01-26 20:34:50 -080013#include "gmock/gmock.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080014#include "gtest/gtest.h"
15
Austin Schuh3bd4c402020-11-06 18:19:06 -080016#ifdef LZMA
17#include "aos/events/logging/lzma_encoder.h"
18#endif
19
Austin Schuhe309d2a2019-11-29 13:25:21 -080020namespace aos {
21namespace logger {
22namespace testing {
23
24namespace chrono = std::chrono;
Austin Schuh0de30f32020-12-06 12:44:28 -080025using aos::message_bridge::RemoteMessage;
Austin Schuh01b4c352020-09-21 23:09:39 -070026using aos::testing::MessageCounter;
Austin Schuhe309d2a2019-11-29 13:25:21 -080027
28class LoggerTest : public ::testing::Test {
29 public:
30 LoggerTest()
31 : config_(
32 aos::configuration::ReadConfig("aos/events/pingpong_config.json")),
33 event_loop_factory_(&config_.message()),
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080034 ping_event_loop_(event_loop_factory_.MakeEventLoop("ping")),
Austin Schuhe309d2a2019-11-29 13:25:21 -080035 ping_(ping_event_loop_.get()),
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080036 pong_event_loop_(event_loop_factory_.MakeEventLoop("pong")),
Austin Schuhe309d2a2019-11-29 13:25:21 -080037 pong_(pong_event_loop_.get()) {}
38
39 // Config and factory.
40 aos::FlatbufferDetachedBuffer<aos::Configuration> config_;
41 SimulatedEventLoopFactory event_loop_factory_;
42
43 // Event loop and app for Ping
44 std::unique_ptr<EventLoop> ping_event_loop_;
45 Ping ping_;
46
47 // Event loop and app for Pong
48 std::unique_ptr<EventLoop> pong_event_loop_;
49 Pong pong_;
50};
51
Brian Silverman1f345222020-09-24 21:14:48 -070052using LoggerDeathTest = LoggerTest;
53
Austin Schuhe309d2a2019-11-29 13:25:21 -080054// Tests that we can startup at all. This confirms that the channels are all in
55// the config.
56TEST_F(LoggerTest, Starts) {
Austin Schuhc243b422020-10-11 15:35:08 -070057 const ::std::string tmpdir = aos::testing::TestTmpDir();
Austin Schuh2f8fd752020-09-01 22:38:28 -070058 const ::std::string base_name = tmpdir + "/logfile";
59 const ::std::string logfile = base_name + ".part0.bfbs";
Austin Schuhe309d2a2019-11-29 13:25:21 -080060 // Remove it.
61 unlink(logfile.c_str());
62
63 LOG(INFO) << "Logging data to " << logfile;
64
65 {
Austin Schuhe309d2a2019-11-29 13:25:21 -080066 std::unique_ptr<EventLoop> logger_event_loop =
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080067 event_loop_factory_.MakeEventLoop("logger");
Austin Schuhe309d2a2019-11-29 13:25:21 -080068
69 event_loop_factory_.RunFor(chrono::milliseconds(95));
70
Brian Silverman1f345222020-09-24 21:14:48 -070071 Logger logger(logger_event_loop.get());
72 logger.set_polling_period(std::chrono::milliseconds(100));
73 logger.StartLoggingLocalNamerOnRun(base_name);
Austin Schuhe309d2a2019-11-29 13:25:21 -080074 event_loop_factory_.RunFor(chrono::milliseconds(20000));
75 }
76
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080077 // Even though it doesn't make any difference here, exercise the logic for
78 // passing in a separate config.
79 LogReader reader(logfile, &config_.message());
Austin Schuhe309d2a2019-11-29 13:25:21 -080080
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080081 // Confirm that we can remap logged channels to point to new buses.
82 reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original");
Austin Schuhe309d2a2019-11-29 13:25:21 -080083
Austin Schuh15649d62019-12-28 16:36:38 -080084 // This sends out the fetched messages and advances time to the start of the
85 // log file.
James Kuszmaul84ff3e52020-01-03 19:48:53 -080086 reader.Register();
Austin Schuhe309d2a2019-11-29 13:25:21 -080087
Austin Schuh6f3babe2020-01-26 20:34:50 -080088 EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(nullptr));
James Kuszmaul84ff3e52020-01-03 19:48:53 -080089
Austin Schuhe309d2a2019-11-29 13:25:21 -080090 std::unique_ptr<EventLoop> test_event_loop =
James Kuszmaul84ff3e52020-01-03 19:48:53 -080091 reader.event_loop_factory()->MakeEventLoop("log_reader");
Austin Schuhe309d2a2019-11-29 13:25:21 -080092
93 int ping_count = 10;
94 int pong_count = 10;
95
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080096 // Confirm that the ping value matches in the remapped channel location.
97 test_event_loop->MakeWatcher("/original/test",
Austin Schuhe309d2a2019-11-29 13:25:21 -080098 [&ping_count](const examples::Ping &ping) {
99 EXPECT_EQ(ping.value(), ping_count + 1);
100 ++ping_count;
101 });
102 // Confirm that the ping and pong counts both match, and the value also
103 // matches.
104 test_event_loop->MakeWatcher(
105 "/test", [&pong_count, &ping_count](const examples::Pong &pong) {
106 EXPECT_EQ(pong.value(), pong_count + 1);
107 ++pong_count;
108 EXPECT_EQ(ping_count, pong_count);
109 });
110
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800111 reader.event_loop_factory()->RunFor(std::chrono::seconds(100));
Austin Schuhe309d2a2019-11-29 13:25:21 -0800112 EXPECT_EQ(ping_count, 2010);
Austin Schuhe309d2a2019-11-29 13:25:21 -0800113}
114
Brian Silverman1f345222020-09-24 21:14:48 -0700115// Tests calling StartLogging twice.
116TEST_F(LoggerDeathTest, ExtraStart) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800117 const ::std::string tmpdir = aos::testing::TestTmpDir();
Brian Silverman1f345222020-09-24 21:14:48 -0700118 const ::std::string base_name1 = tmpdir + "/logfile1";
119 const ::std::string logfile1 = base_name1 + ".part0.bfbs";
120 const ::std::string base_name2 = tmpdir + "/logfile2";
121 const ::std::string logfile2 = base_name2 + ".part0.bfbs";
122 unlink(logfile1.c_str());
123 unlink(logfile2.c_str());
124
125 LOG(INFO) << "Logging data to " << logfile1 << " then " << logfile2;
126
127 {
128 std::unique_ptr<EventLoop> logger_event_loop =
129 event_loop_factory_.MakeEventLoop("logger");
130
131 event_loop_factory_.RunFor(chrono::milliseconds(95));
132
133 Logger logger(logger_event_loop.get());
134 logger.set_polling_period(std::chrono::milliseconds(100));
135 logger_event_loop->OnRun(
136 [base_name1, base_name2, &logger_event_loop, &logger]() {
137 logger.StartLogging(std::make_unique<LocalLogNamer>(
138 base_name1, logger_event_loop->node()));
139 EXPECT_DEATH(logger.StartLogging(std::make_unique<LocalLogNamer>(
140 base_name2, logger_event_loop->node())),
141 "Already logging");
142 });
143 event_loop_factory_.RunFor(chrono::milliseconds(20000));
144 }
145}
146
147// Tests calling StopLogging twice.
148TEST_F(LoggerDeathTest, ExtraStop) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800149 const ::std::string tmpdir = aos::testing::TestTmpDir();
Brian Silverman1f345222020-09-24 21:14:48 -0700150 const ::std::string base_name = tmpdir + "/logfile";
151 const ::std::string logfile = base_name + ".part0.bfbs";
152 // Remove it.
153 unlink(logfile.c_str());
154
155 LOG(INFO) << "Logging data to " << logfile;
156
157 {
158 std::unique_ptr<EventLoop> logger_event_loop =
159 event_loop_factory_.MakeEventLoop("logger");
160
161 event_loop_factory_.RunFor(chrono::milliseconds(95));
162
163 Logger logger(logger_event_loop.get());
164 logger.set_polling_period(std::chrono::milliseconds(100));
165 logger_event_loop->OnRun([base_name, &logger_event_loop, &logger]() {
166 logger.StartLogging(std::make_unique<LocalLogNamer>(
167 base_name, logger_event_loop->node()));
168 logger.StopLogging(aos::monotonic_clock::min_time);
169 EXPECT_DEATH(logger.StopLogging(aos::monotonic_clock::min_time),
170 "Not logging right now");
171 });
172 event_loop_factory_.RunFor(chrono::milliseconds(20000));
173 }
174}
175
176// Tests that we can startup twice.
177TEST_F(LoggerTest, StartsTwice) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800178 const ::std::string tmpdir = aos::testing::TestTmpDir();
Brian Silverman1f345222020-09-24 21:14:48 -0700179 const ::std::string base_name1 = tmpdir + "/logfile1";
180 const ::std::string logfile1 = base_name1 + ".part0.bfbs";
181 const ::std::string base_name2 = tmpdir + "/logfile2";
182 const ::std::string logfile2 = base_name2 + ".part0.bfbs";
183 unlink(logfile1.c_str());
184 unlink(logfile2.c_str());
185
186 LOG(INFO) << "Logging data to " << logfile1 << " then " << logfile2;
187
188 {
189 std::unique_ptr<EventLoop> logger_event_loop =
190 event_loop_factory_.MakeEventLoop("logger");
191
192 event_loop_factory_.RunFor(chrono::milliseconds(95));
193
194 Logger logger(logger_event_loop.get());
195 logger.set_polling_period(std::chrono::milliseconds(100));
196 logger.StartLogging(
197 std::make_unique<LocalLogNamer>(base_name1, logger_event_loop->node()));
198 event_loop_factory_.RunFor(chrono::milliseconds(10000));
199 logger.StopLogging(logger_event_loop->monotonic_now());
200 event_loop_factory_.RunFor(chrono::milliseconds(10000));
201 logger.StartLogging(
202 std::make_unique<LocalLogNamer>(base_name2, logger_event_loop->node()));
203 event_loop_factory_.RunFor(chrono::milliseconds(10000));
204 }
205
206 for (const auto &logfile :
207 {std::make_tuple(logfile1, 10), std::make_tuple(logfile2, 2010)}) {
208 SCOPED_TRACE(std::get<0>(logfile));
209 LogReader reader(std::get<0>(logfile));
210 reader.Register();
211
212 EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(nullptr));
213
214 std::unique_ptr<EventLoop> test_event_loop =
215 reader.event_loop_factory()->MakeEventLoop("log_reader");
216
217 int ping_count = std::get<1>(logfile);
218 int pong_count = std::get<1>(logfile);
219
220 // Confirm that the ping and pong counts both match, and the value also
221 // matches.
222 test_event_loop->MakeWatcher("/test",
223 [&ping_count](const examples::Ping &ping) {
224 EXPECT_EQ(ping.value(), ping_count + 1);
225 ++ping_count;
226 });
227 test_event_loop->MakeWatcher(
228 "/test", [&pong_count, &ping_count](const examples::Pong &pong) {
229 EXPECT_EQ(pong.value(), pong_count + 1);
230 ++pong_count;
231 EXPECT_EQ(ping_count, pong_count);
232 });
233
234 reader.event_loop_factory()->RunFor(std::chrono::seconds(100));
235 EXPECT_EQ(ping_count, std::get<1>(logfile) + 1000);
236 }
237}
238
Austin Schuhfa895892020-01-07 20:07:41 -0800239// Tests that we can read and write rotated log files.
240TEST_F(LoggerTest, RotatedLogFile) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800241 const ::std::string tmpdir = aos::testing::TestTmpDir();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700242 const ::std::string base_name = tmpdir + "/logfile";
243 const ::std::string logfile0 = base_name + ".part0.bfbs";
244 const ::std::string logfile1 = base_name + ".part1.bfbs";
Austin Schuhfa895892020-01-07 20:07:41 -0800245 // Remove it.
246 unlink(logfile0.c_str());
247 unlink(logfile1.c_str());
248
249 LOG(INFO) << "Logging data to " << logfile0 << " and " << logfile1;
250
251 {
Austin Schuhfa895892020-01-07 20:07:41 -0800252 std::unique_ptr<EventLoop> logger_event_loop =
253 event_loop_factory_.MakeEventLoop("logger");
254
255 event_loop_factory_.RunFor(chrono::milliseconds(95));
256
Brian Silverman1f345222020-09-24 21:14:48 -0700257 Logger logger(logger_event_loop.get());
258 logger.set_polling_period(std::chrono::milliseconds(100));
259 logger.StartLoggingLocalNamerOnRun(base_name);
Austin Schuhfa895892020-01-07 20:07:41 -0800260 event_loop_factory_.RunFor(chrono::milliseconds(10000));
Austin Schuh2f8fd752020-09-01 22:38:28 -0700261 logger.Rotate();
Austin Schuhfa895892020-01-07 20:07:41 -0800262 event_loop_factory_.RunFor(chrono::milliseconds(10000));
263 }
264
Austin Schuh64fab802020-09-09 22:47:47 -0700265 {
266 // Confirm that the UUIDs match for both the parts and the logger, and the
267 // parts_index increments.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800268 std::vector<SizePrefixedFlatbufferVector<LogFileHeader>> log_header;
Austin Schuh64fab802020-09-09 22:47:47 -0700269 for (std::string_view f : {logfile0, logfile1}) {
Austin Schuh3bd4c402020-11-06 18:19:06 -0800270 log_header.emplace_back(ReadHeader(f).value());
Austin Schuh64fab802020-09-09 22:47:47 -0700271 }
272
Brian Silvermanae7c0332020-09-30 16:58:23 -0700273 EXPECT_EQ(log_header[0].message().log_event_uuid()->string_view(),
274 log_header[1].message().log_event_uuid()->string_view());
Austin Schuh64fab802020-09-09 22:47:47 -0700275 EXPECT_EQ(log_header[0].message().parts_uuid()->string_view(),
276 log_header[1].message().parts_uuid()->string_view());
277
278 EXPECT_EQ(log_header[0].message().parts_index(), 0);
279 EXPECT_EQ(log_header[1].message().parts_index(), 1);
280 }
281
Austin Schuhfa895892020-01-07 20:07:41 -0800282 // Even though it doesn't make any difference here, exercise the logic for
283 // passing in a separate config.
Austin Schuh287d43d2020-12-04 20:19:33 -0800284 LogReader reader(SortParts({logfile0, logfile1}), &config_.message());
Austin Schuhfa895892020-01-07 20:07:41 -0800285
286 // Confirm that we can remap logged channels to point to new buses.
287 reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original");
288
289 // This sends out the fetched messages and advances time to the start of the
290 // log file.
291 reader.Register();
292
Austin Schuh6f3babe2020-01-26 20:34:50 -0800293 EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(nullptr));
Austin Schuhfa895892020-01-07 20:07:41 -0800294
295 std::unique_ptr<EventLoop> test_event_loop =
296 reader.event_loop_factory()->MakeEventLoop("log_reader");
297
298 int ping_count = 10;
299 int pong_count = 10;
300
301 // Confirm that the ping value matches in the remapped channel location.
302 test_event_loop->MakeWatcher("/original/test",
303 [&ping_count](const examples::Ping &ping) {
304 EXPECT_EQ(ping.value(), ping_count + 1);
305 ++ping_count;
306 });
307 // Confirm that the ping and pong counts both match, and the value also
308 // matches.
309 test_event_loop->MakeWatcher(
310 "/test", [&pong_count, &ping_count](const examples::Pong &pong) {
311 EXPECT_EQ(pong.value(), pong_count + 1);
312 ++pong_count;
313 EXPECT_EQ(ping_count, pong_count);
314 });
315
316 reader.event_loop_factory()->RunFor(std::chrono::seconds(100));
317 EXPECT_EQ(ping_count, 2010);
318}
319
Austin Schuh4c4e0092019-12-22 16:18:03 -0800320// Tests that a large number of messages per second doesn't overwhelm writev.
321TEST_F(LoggerTest, ManyMessages) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800322 const ::std::string tmpdir = aos::testing::TestTmpDir();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700323 const ::std::string base_name = tmpdir + "/logfile";
324 const ::std::string logfile = base_name + ".part0.bfbs";
Austin Schuh4c4e0092019-12-22 16:18:03 -0800325 // Remove the log file.
326 unlink(logfile.c_str());
327
328 LOG(INFO) << "Logging data to " << logfile;
Austin Schuh4c3b9702020-08-30 11:34:55 -0700329 ping_.set_quiet(true);
Austin Schuh4c4e0092019-12-22 16:18:03 -0800330
331 {
Austin Schuh4c4e0092019-12-22 16:18:03 -0800332 std::unique_ptr<EventLoop> logger_event_loop =
333 event_loop_factory_.MakeEventLoop("logger");
334
335 std::unique_ptr<EventLoop> ping_spammer_event_loop =
336 event_loop_factory_.MakeEventLoop("ping_spammer");
337 aos::Sender<examples::Ping> ping_sender =
338 ping_spammer_event_loop->MakeSender<examples::Ping>("/test");
339
340 aos::TimerHandler *timer_handler =
341 ping_spammer_event_loop->AddTimer([&ping_sender]() {
342 aos::Sender<examples::Ping>::Builder builder =
343 ping_sender.MakeBuilder();
344 examples::Ping::Builder ping_builder =
345 builder.MakeBuilder<examples::Ping>();
346 CHECK(builder.Send(ping_builder.Finish()));
347 });
348
349 // 100 ms / 0.05 ms -> 2000 messages. Should be enough to crash it.
350 ping_spammer_event_loop->OnRun([&ping_spammer_event_loop, timer_handler]() {
351 timer_handler->Setup(ping_spammer_event_loop->monotonic_now(),
352 chrono::microseconds(50));
353 });
354
Brian Silverman1f345222020-09-24 21:14:48 -0700355 Logger logger(logger_event_loop.get());
356 logger.set_polling_period(std::chrono::milliseconds(100));
357 logger.StartLoggingLocalNamerOnRun(base_name);
Austin Schuh4c4e0092019-12-22 16:18:03 -0800358
359 event_loop_factory_.RunFor(chrono::milliseconds(1000));
360 }
361}
362
Austin Schuh15649d62019-12-28 16:36:38 -0800363class MultinodeLoggerTest : public ::testing::Test {
364 public:
365 MultinodeLoggerTest()
366 : config_(aos::configuration::ReadConfig(
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800367 "aos/events/logging/multinode_pingpong_config.json")),
Austin Schuhac0771c2020-01-07 18:36:30 -0800368 event_loop_factory_(&config_.message()),
Austin Schuhcde938c2020-02-02 17:30:07 -0800369 pi1_(
370 configuration::GetNode(event_loop_factory_.configuration(), "pi1")),
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700371 pi2_(
372 configuration::GetNode(event_loop_factory_.configuration(), "pi2")),
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800373 tmp_dir_(aos::testing::TestTmpDir()),
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700374 logfile_base_(tmp_dir_ + "/multi_logfile"),
Austin Schuh2f8fd752020-09-01 22:38:28 -0700375 logfiles_(
376 {logfile_base_ + "_pi1_data.part0.bfbs",
377 logfile_base_ + "_pi2_data/test/aos.examples.Pong.part0.bfbs",
378 logfile_base_ + "_pi2_data/test/aos.examples.Pong.part1.bfbs",
379 logfile_base_ + "_pi2_data.part0.bfbs",
380 logfile_base_ + "_timestamps/pi1/aos/remote_timestamps/pi2/"
Austin Schuh0de30f32020-12-06 12:44:28 -0800381 "aos.message_bridge.RemoteMessage.part0.bfbs",
Austin Schuh2f8fd752020-09-01 22:38:28 -0700382 logfile_base_ + "_timestamps/pi1/aos/remote_timestamps/pi2/"
Austin Schuh0de30f32020-12-06 12:44:28 -0800383 "aos.message_bridge.RemoteMessage.part1.bfbs",
Austin Schuh2f8fd752020-09-01 22:38:28 -0700384 logfile_base_ + "_timestamps/pi2/aos/remote_timestamps/pi1/"
Austin Schuh0de30f32020-12-06 12:44:28 -0800385 "aos.message_bridge.RemoteMessage.part0.bfbs",
Austin Schuh2f8fd752020-09-01 22:38:28 -0700386 logfile_base_ + "_timestamps/pi2/aos/remote_timestamps/pi1/"
Austin Schuh0de30f32020-12-06 12:44:28 -0800387 "aos.message_bridge.RemoteMessage.part1.bfbs",
Austin Schuh2f8fd752020-09-01 22:38:28 -0700388 logfile_base_ +
389 "_pi1_data/pi1/aos/aos.message_bridge.Timestamp.part0.bfbs",
390 logfile_base_ +
391 "_pi1_data/pi1/aos/aos.message_bridge.Timestamp.part1.bfbs",
392 logfile_base_ +
393 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part0.bfbs",
394 logfile_base_ +
395 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part1.bfbs"}),
396 structured_logfiles_{
397 std::vector<std::string>{logfiles_[0]},
398 std::vector<std::string>{logfiles_[1], logfiles_[2]},
399 std::vector<std::string>{logfiles_[3]},
400 std::vector<std::string>{logfiles_[4], logfiles_[5]},
401 std::vector<std::string>{logfiles_[6], logfiles_[7]},
402 std::vector<std::string>{logfiles_[8], logfiles_[9]},
403 std::vector<std::string>{logfiles_[10], logfiles_[11]}},
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700404 ping_event_loop_(event_loop_factory_.MakeEventLoop("ping", pi1_)),
405 ping_(ping_event_loop_.get()),
406 pong_event_loop_(event_loop_factory_.MakeEventLoop("pong", pi2_)),
407 pong_(pong_event_loop_.get()) {
408 // Go through and remove the logfiles if they already exist.
409 for (const auto file : logfiles_) {
410 unlink(file.c_str());
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800411 unlink((file + ".xz").c_str());
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700412 }
413
414 LOG(INFO) << "Logging data to " << logfiles_[0] << ", " << logfiles_[1]
415 << " and " << logfiles_[2];
416 }
417
418 struct LoggerState {
419 std::unique_ptr<EventLoop> event_loop;
420 std::unique_ptr<Logger> logger;
421 };
422
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700423 LoggerState MakeLogger(const Node *node,
424 SimulatedEventLoopFactory *factory = nullptr) {
425 if (factory == nullptr) {
426 factory = &event_loop_factory_;
427 }
428 return {factory->MakeEventLoop("logger", node), {}};
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700429 }
430
Austin Schuh3bd4c402020-11-06 18:19:06 -0800431 void StartLogger(LoggerState *logger, std::string logfile_base = "",
432 bool compress = false) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700433 if (logfile_base.empty()) {
434 logfile_base = logfile_base_;
435 }
436
Brian Silverman1f345222020-09-24 21:14:48 -0700437 logger->logger = std::make_unique<Logger>(logger->event_loop.get());
438 logger->logger->set_polling_period(std::chrono::milliseconds(100));
Austin Schuh3bd4c402020-11-06 18:19:06 -0800439 logger->event_loop->OnRun([logger, logfile_base, compress]() {
440 std::unique_ptr<MultiNodeLogNamer> namer =
441 std::make_unique<MultiNodeLogNamer>(
442 logfile_base, logger->event_loop->configuration(),
443 logger->event_loop->node());
444 if (compress) {
445#ifdef LZMA
446 namer->set_extension(".xz");
447 namer->set_encoder_factory(
448 []() { return std::make_unique<aos::logger::LzmaEncoder>(3); });
449#else
450 LOG(FATAL) << "Compression unsupported";
451#endif
452 }
453
454 logger->logger->StartLogging(std::move(namer));
Brian Silverman1f345222020-09-24 21:14:48 -0700455 });
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700456 }
Austin Schuh15649d62019-12-28 16:36:38 -0800457
Austin Schuh3bd4c402020-11-06 18:19:06 -0800458 void VerifyParts(const std::vector<LogFile> &sorted_parts,
459 const std::vector<std::string> &corrupted_parts = {}) {
460 EXPECT_EQ(sorted_parts.size(), 2u);
461
462 // Count up the number of UUIDs and make sure they are what we expect as a
463 // sanity check.
464 std::set<std::string> log_event_uuids;
465 std::set<std::string> parts_uuids;
466 std::set<std::string> both_uuids;
467
468 size_t missing_rt_count = 0;
469
470 std::vector<std::string> logger_nodes;
471 for (const LogFile &log_file : sorted_parts) {
472 EXPECT_FALSE(log_file.log_event_uuid.empty());
473 log_event_uuids.insert(log_file.log_event_uuid);
474 logger_nodes.emplace_back(log_file.logger_node);
475 both_uuids.insert(log_file.log_event_uuid);
476
477 for (const LogParts &part : log_file.parts) {
478 EXPECT_NE(part.monotonic_start_time, aos::monotonic_clock::min_time)
479 << ": " << part;
480 missing_rt_count +=
481 part.realtime_start_time == aos::realtime_clock::min_time;
482
483 EXPECT_TRUE(log_event_uuids.find(part.log_event_uuid) !=
484 log_event_uuids.end());
485 EXPECT_NE(part.node, "");
486 parts_uuids.insert(part.parts_uuid);
487 both_uuids.insert(part.parts_uuid);
488 }
489 }
490
491 // We won't have RT timestamps for 5 log files. We don't log the RT start
492 // time on remote nodes because we don't know it and would be guessing. And
493 // the log reader can actually do a better job.
494 EXPECT_EQ(missing_rt_count, 5u);
495
496 EXPECT_EQ(log_event_uuids.size(), 2u);
497 EXPECT_EQ(parts_uuids.size(), ToLogReaderVector(sorted_parts).size());
498 EXPECT_EQ(log_event_uuids.size() + parts_uuids.size(), both_uuids.size());
499
500 // Test that each list of parts is in order. Don't worry about the ordering
501 // between part file lists though.
502 // (inner vectors all need to be in order, but outer one doesn't matter).
503 EXPECT_THAT(ToLogReaderVector(sorted_parts),
504 ::testing::UnorderedElementsAreArray(structured_logfiles_));
505
506 EXPECT_THAT(logger_nodes, ::testing::UnorderedElementsAre("pi1", "pi2"));
507
508 EXPECT_NE(sorted_parts[0].realtime_start_time,
509 aos::realtime_clock::min_time);
510 EXPECT_NE(sorted_parts[1].realtime_start_time,
511 aos::realtime_clock::min_time);
512
513 EXPECT_NE(sorted_parts[0].monotonic_start_time,
514 aos::monotonic_clock::min_time);
515 EXPECT_NE(sorted_parts[1].monotonic_start_time,
516 aos::monotonic_clock::min_time);
517
518 EXPECT_THAT(sorted_parts[0].corrupted, ::testing::Eq(corrupted_parts));
519 EXPECT_THAT(sorted_parts[1].corrupted, ::testing::Eq(corrupted_parts));
520 }
521
522 void AddExtension(std::string_view extension) {
523 std::transform(logfiles_.begin(), logfiles_.end(), logfiles_.begin(),
524 [extension](const std::string &in) {
525 return absl::StrCat(in, extension);
526 });
527
528 std::transform(structured_logfiles_.begin(), structured_logfiles_.end(),
529 structured_logfiles_.begin(),
530 [extension](std::vector<std::string> in) {
531 std::transform(in.begin(), in.end(), in.begin(),
532 [extension](const std::string &in_str) {
533 return absl::StrCat(in_str, extension);
534 });
535 return in;
536 });
537 }
538
Austin Schuh15649d62019-12-28 16:36:38 -0800539 // Config and factory.
540 aos::FlatbufferDetachedBuffer<aos::Configuration> config_;
541 SimulatedEventLoopFactory event_loop_factory_;
542
Austin Schuhcde938c2020-02-02 17:30:07 -0800543 const Node *pi1_;
544 const Node *pi2_;
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700545
546 std::string tmp_dir_;
547 std::string logfile_base_;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700548 std::vector<std::string> logfiles_;
549
550 std::vector<std::vector<std::string>> structured_logfiles_;
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700551
552 std::unique_ptr<EventLoop> ping_event_loop_;
553 Ping ping_;
554 std::unique_ptr<EventLoop> pong_event_loop_;
555 Pong pong_;
Austin Schuh15649d62019-12-28 16:36:38 -0800556};
557
Austin Schuh391e3172020-09-01 22:48:18 -0700558// Counts the number of messages on a channel. Returns (channel name, channel
559// type, count) for every message matching matcher()
560std::vector<std::tuple<std::string, std::string, int>> CountChannelsMatching(
Austin Schuh6f3babe2020-01-26 20:34:50 -0800561 std::string_view filename,
562 std::function<bool(const MessageHeader *)> matcher) {
563 MessageReader message_reader(filename);
564 std::vector<int> counts(
565 message_reader.log_file_header()->configuration()->channels()->size(), 0);
Austin Schuh15649d62019-12-28 16:36:38 -0800566
Austin Schuh6f3babe2020-01-26 20:34:50 -0800567 while (true) {
Austin Schuhadd6eb32020-11-09 21:24:26 -0800568 std::optional<SizePrefixedFlatbufferVector<MessageHeader>> msg =
Austin Schuh6f3babe2020-01-26 20:34:50 -0800569 message_reader.ReadMessage();
570 if (!msg) {
571 break;
572 }
573
574 if (matcher(&msg.value().message())) {
575 counts[msg.value().message().channel_index()]++;
576 }
577 }
578
Austin Schuh391e3172020-09-01 22:48:18 -0700579 std::vector<std::tuple<std::string, std::string, int>> result;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800580 int channel = 0;
581 for (size_t i = 0; i < counts.size(); ++i) {
582 if (counts[i] != 0) {
Austin Schuh391e3172020-09-01 22:48:18 -0700583 const Channel *channel =
584 message_reader.log_file_header()->configuration()->channels()->Get(i);
585 result.push_back(std::make_tuple(channel->name()->str(),
586 channel->type()->str(), counts[i]));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800587 }
588 ++channel;
589 }
590
591 return result;
592}
593
594// Counts the number of messages (channel, count) for all data messages.
Austin Schuh391e3172020-09-01 22:48:18 -0700595std::vector<std::tuple<std::string, std::string, int>> CountChannelsData(
596 std::string_view filename) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800597 return CountChannelsMatching(filename, [](const MessageHeader *msg) {
598 if (msg->has_data()) {
599 CHECK(!msg->has_monotonic_remote_time());
600 CHECK(!msg->has_realtime_remote_time());
601 CHECK(!msg->has_remote_queue_index());
602 return true;
603 }
604 return false;
605 });
606}
607
608// Counts the number of messages (channel, count) for all timestamp messages.
Austin Schuh391e3172020-09-01 22:48:18 -0700609std::vector<std::tuple<std::string, std::string, int>> CountChannelsTimestamp(
Austin Schuh6f3babe2020-01-26 20:34:50 -0800610 std::string_view filename) {
611 return CountChannelsMatching(filename, [](const MessageHeader *msg) {
612 if (!msg->has_data()) {
613 CHECK(msg->has_monotonic_remote_time());
614 CHECK(msg->has_realtime_remote_time());
615 CHECK(msg->has_remote_queue_index());
616 return true;
617 }
618 return false;
619 });
620}
621
Austin Schuhcde938c2020-02-02 17:30:07 -0800622// Tests that we can write and read simple multi-node log files.
623TEST_F(MultinodeLoggerTest, SimpleMultiNode) {
Austin Schuh15649d62019-12-28 16:36:38 -0800624 {
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700625 LoggerState pi1_logger = MakeLogger(pi1_);
626 LoggerState pi2_logger = MakeLogger(pi2_);
Austin Schuh15649d62019-12-28 16:36:38 -0800627
628 event_loop_factory_.RunFor(chrono::milliseconds(95));
629
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700630 StartLogger(&pi1_logger);
631 StartLogger(&pi2_logger);
Austin Schuhcde938c2020-02-02 17:30:07 -0800632
Austin Schuh15649d62019-12-28 16:36:38 -0800633 event_loop_factory_.RunFor(chrono::milliseconds(20000));
634 }
635
Austin Schuh6f3babe2020-01-26 20:34:50 -0800636 {
Austin Schuh64fab802020-09-09 22:47:47 -0700637 std::set<std::string> logfile_uuids;
638 std::set<std::string> parts_uuids;
639 // Confirm that we have the expected number of UUIDs for both the logfile
640 // UUIDs and parts UUIDs.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800641 std::vector<SizePrefixedFlatbufferVector<LogFileHeader>> log_header;
Austin Schuh64fab802020-09-09 22:47:47 -0700642 for (std::string_view f : logfiles_) {
Austin Schuh3bd4c402020-11-06 18:19:06 -0800643 log_header.emplace_back(ReadHeader(f).value());
Brian Silvermanae7c0332020-09-30 16:58:23 -0700644 logfile_uuids.insert(log_header.back().message().log_event_uuid()->str());
Austin Schuh64fab802020-09-09 22:47:47 -0700645 parts_uuids.insert(log_header.back().message().parts_uuid()->str());
646 }
Austin Schuh15649d62019-12-28 16:36:38 -0800647
Austin Schuh64fab802020-09-09 22:47:47 -0700648 EXPECT_EQ(logfile_uuids.size(), 2u);
649 EXPECT_EQ(parts_uuids.size(), 7u);
650
651 // And confirm everything is on the correct node.
652 EXPECT_EQ(log_header[0].message().node()->name()->string_view(), "pi1");
653 EXPECT_EQ(log_header[1].message().node()->name()->string_view(), "pi2");
654 EXPECT_EQ(log_header[2].message().node()->name()->string_view(), "pi2");
655 EXPECT_EQ(log_header[3].message().node()->name()->string_view(), "pi2");
656 EXPECT_EQ(log_header[4].message().node()->name()->string_view(), "pi2");
657 EXPECT_EQ(log_header[5].message().node()->name()->string_view(), "pi2");
658 EXPECT_EQ(log_header[6].message().node()->name()->string_view(), "pi1");
659 EXPECT_EQ(log_header[7].message().node()->name()->string_view(), "pi1");
660 EXPECT_EQ(log_header[8].message().node()->name()->string_view(), "pi1");
661 EXPECT_EQ(log_header[9].message().node()->name()->string_view(), "pi1");
662 EXPECT_EQ(log_header[10].message().node()->name()->string_view(), "pi2");
663 EXPECT_EQ(log_header[11].message().node()->name()->string_view(), "pi2");
664
665 // And the parts index matches.
666 EXPECT_EQ(log_header[0].message().parts_index(), 0);
667 EXPECT_EQ(log_header[1].message().parts_index(), 0);
668 EXPECT_EQ(log_header[2].message().parts_index(), 1);
669 EXPECT_EQ(log_header[3].message().parts_index(), 0);
670 EXPECT_EQ(log_header[4].message().parts_index(), 0);
671 EXPECT_EQ(log_header[5].message().parts_index(), 1);
672 EXPECT_EQ(log_header[6].message().parts_index(), 0);
673 EXPECT_EQ(log_header[7].message().parts_index(), 1);
674 EXPECT_EQ(log_header[8].message().parts_index(), 0);
675 EXPECT_EQ(log_header[9].message().parts_index(), 1);
676 EXPECT_EQ(log_header[10].message().parts_index(), 0);
677 EXPECT_EQ(log_header[11].message().parts_index(), 1);
678 }
679
680 {
Austin Schuh391e3172020-09-01 22:48:18 -0700681 using ::testing::UnorderedElementsAre;
682
Austin Schuh6f3babe2020-01-26 20:34:50 -0800683 // Timing reports, pings
Austin Schuh2f8fd752020-09-01 22:38:28 -0700684 EXPECT_THAT(
685 CountChannelsData(logfiles_[0]),
686 UnorderedElementsAre(
687 std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 200),
688 std::make_tuple("/pi1/aos", "aos.timing.Report", 40),
689 std::make_tuple("/test", "aos.examples.Ping", 2001)));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800690 // Timestamps for pong
Austin Schuh2f8fd752020-09-01 22:38:28 -0700691 EXPECT_THAT(
692 CountChannelsTimestamp(logfiles_[0]),
693 UnorderedElementsAre(
694 std::make_tuple("/test", "aos.examples.Pong", 2001),
695 std::make_tuple("/pi2/aos", "aos.message_bridge.Timestamp", 200)));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800696
697 // Pong data.
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700698 EXPECT_THAT(CountChannelsData(logfiles_[1]),
Austin Schuh391e3172020-09-01 22:48:18 -0700699 UnorderedElementsAre(
Austin Schuh2f8fd752020-09-01 22:38:28 -0700700 std::make_tuple("/test", "aos.examples.Pong", 101)));
701 EXPECT_THAT(CountChannelsData(logfiles_[2]),
702 UnorderedElementsAre(
703 std::make_tuple("/test", "aos.examples.Pong", 1900)));
Austin Schuh391e3172020-09-01 22:48:18 -0700704
Austin Schuh6f3babe2020-01-26 20:34:50 -0800705 // No timestamps
Austin Schuh391e3172020-09-01 22:48:18 -0700706 EXPECT_THAT(CountChannelsTimestamp(logfiles_[1]), UnorderedElementsAre());
Austin Schuh2f8fd752020-09-01 22:38:28 -0700707 EXPECT_THAT(CountChannelsTimestamp(logfiles_[2]), UnorderedElementsAre());
Austin Schuh6f3babe2020-01-26 20:34:50 -0800708
709 // Timing reports and pongs.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700710 EXPECT_THAT(
711 CountChannelsData(logfiles_[3]),
712 UnorderedElementsAre(
713 std::make_tuple("/pi2/aos", "aos.message_bridge.Timestamp", 200),
714 std::make_tuple("/pi2/aos", "aos.timing.Report", 40),
715 std::make_tuple("/test", "aos.examples.Pong", 2001)));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800716 // And ping timestamps.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700717 EXPECT_THAT(
718 CountChannelsTimestamp(logfiles_[3]),
719 UnorderedElementsAre(
720 std::make_tuple("/test", "aos.examples.Ping", 2001),
721 std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 200)));
722
723 // Timestamps from pi2 on pi1, and the other way.
724 EXPECT_THAT(CountChannelsData(logfiles_[4]), UnorderedElementsAre());
725 EXPECT_THAT(CountChannelsData(logfiles_[5]), UnorderedElementsAre());
726 EXPECT_THAT(CountChannelsData(logfiles_[6]), UnorderedElementsAre());
727 EXPECT_THAT(CountChannelsData(logfiles_[7]), UnorderedElementsAre());
728 EXPECT_THAT(
729 CountChannelsTimestamp(logfiles_[4]),
730 UnorderedElementsAre(
731 std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 10),
732 std::make_tuple("/test", "aos.examples.Ping", 101)));
733 EXPECT_THAT(
734 CountChannelsTimestamp(logfiles_[5]),
735 UnorderedElementsAre(
736 std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 190),
737 std::make_tuple("/test", "aos.examples.Ping", 1900)));
738 EXPECT_THAT(CountChannelsTimestamp(logfiles_[6]),
739 UnorderedElementsAre(std::make_tuple(
740 "/pi2/aos", "aos.message_bridge.Timestamp", 10)));
741 EXPECT_THAT(CountChannelsTimestamp(logfiles_[7]),
742 UnorderedElementsAre(std::make_tuple(
743 "/pi2/aos", "aos.message_bridge.Timestamp", 190)));
744
745 // And then test that the remotely logged timestamp data files only have
746 // timestamps in them.
747 EXPECT_THAT(CountChannelsTimestamp(logfiles_[8]), UnorderedElementsAre());
748 EXPECT_THAT(CountChannelsTimestamp(logfiles_[9]), UnorderedElementsAre());
749 EXPECT_THAT(CountChannelsTimestamp(logfiles_[10]), UnorderedElementsAre());
750 EXPECT_THAT(CountChannelsTimestamp(logfiles_[11]), UnorderedElementsAre());
751
752 EXPECT_THAT(CountChannelsData(logfiles_[8]),
753 UnorderedElementsAre(std::make_tuple(
754 "/pi1/aos", "aos.message_bridge.Timestamp", 10)));
755 EXPECT_THAT(CountChannelsData(logfiles_[9]),
756 UnorderedElementsAre(std::make_tuple(
757 "/pi1/aos", "aos.message_bridge.Timestamp", 190)));
758
759 EXPECT_THAT(CountChannelsData(logfiles_[10]),
760 UnorderedElementsAre(std::make_tuple(
761 "/pi2/aos", "aos.message_bridge.Timestamp", 10)));
762 EXPECT_THAT(CountChannelsData(logfiles_[11]),
763 UnorderedElementsAre(std::make_tuple(
764 "/pi2/aos", "aos.message_bridge.Timestamp", 190)));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800765 }
766
Austin Schuh287d43d2020-12-04 20:19:33 -0800767 LogReader reader(SortParts(logfiles_));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800768
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700769 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
Austin Schuh6331ef92020-01-07 18:28:09 -0800770 log_reader_factory.set_send_delay(chrono::microseconds(0));
Austin Schuh15649d62019-12-28 16:36:38 -0800771
772 // This sends out the fetched messages and advances time to the start of the
773 // log file.
Austin Schuh6331ef92020-01-07 18:28:09 -0800774 reader.Register(&log_reader_factory);
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800775
Austin Schuhac0771c2020-01-07 18:36:30 -0800776 const Node *pi1 =
777 configuration::GetNode(log_reader_factory.configuration(), "pi1");
Austin Schuh6f3babe2020-01-26 20:34:50 -0800778 const Node *pi2 =
779 configuration::GetNode(log_reader_factory.configuration(), "pi2");
Austin Schuhac0771c2020-01-07 18:36:30 -0800780
Austin Schuh2f8fd752020-09-01 22:38:28 -0700781 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi1) << " pi1";
782 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi2) << " pi2";
783 LOG(INFO) << "now pi1 "
784 << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now();
785 LOG(INFO) << "now pi2 "
786 << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now();
787
Austin Schuh6f3babe2020-01-26 20:34:50 -0800788 EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(pi1, pi2));
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800789
790 reader.event_loop_factory()->set_send_delay(chrono::microseconds(0));
Austin Schuh15649d62019-12-28 16:36:38 -0800791
Austin Schuh6f3babe2020-01-26 20:34:50 -0800792 std::unique_ptr<EventLoop> pi1_event_loop =
Austin Schuhac0771c2020-01-07 18:36:30 -0800793 log_reader_factory.MakeEventLoop("test", pi1);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800794 std::unique_ptr<EventLoop> pi2_event_loop =
795 log_reader_factory.MakeEventLoop("test", pi2);
Austin Schuh15649d62019-12-28 16:36:38 -0800796
Austin Schuh6f3babe2020-01-26 20:34:50 -0800797 int pi1_ping_count = 10;
798 int pi2_ping_count = 10;
799 int pi1_pong_count = 10;
800 int pi2_pong_count = 10;
Austin Schuh15649d62019-12-28 16:36:38 -0800801
802 // Confirm that the ping value matches.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800803 pi1_event_loop->MakeWatcher(
804 "/test", [&pi1_ping_count, &pi1_event_loop](const examples::Ping &ping) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700805 VLOG(1) << "Pi1 ping " << FlatbufferToJson(&ping) << " at "
Austin Schuh6f3babe2020-01-26 20:34:50 -0800806 << pi1_event_loop->context().monotonic_remote_time << " -> "
807 << pi1_event_loop->context().monotonic_event_time;
808 EXPECT_EQ(ping.value(), pi1_ping_count + 1);
809 EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time,
810 pi1_ping_count * chrono::milliseconds(10) +
811 monotonic_clock::epoch());
812 EXPECT_EQ(pi1_event_loop->context().realtime_remote_time,
813 pi1_ping_count * chrono::milliseconds(10) +
814 realtime_clock::epoch());
815 EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time,
816 pi1_event_loop->context().monotonic_event_time);
817 EXPECT_EQ(pi1_event_loop->context().realtime_remote_time,
818 pi1_event_loop->context().realtime_event_time);
Austin Schuh15649d62019-12-28 16:36:38 -0800819
Austin Schuh6f3babe2020-01-26 20:34:50 -0800820 ++pi1_ping_count;
821 });
822 pi2_event_loop->MakeWatcher(
823 "/test", [&pi2_ping_count, &pi2_event_loop](const examples::Ping &ping) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700824 VLOG(1) << "Pi2 ping " << FlatbufferToJson(&ping) << " at "
Austin Schuh6f3babe2020-01-26 20:34:50 -0800825 << pi2_event_loop->context().monotonic_remote_time << " -> "
826 << pi2_event_loop->context().monotonic_event_time;
827 EXPECT_EQ(ping.value(), pi2_ping_count + 1);
828
829 EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time,
830 pi2_ping_count * chrono::milliseconds(10) +
831 monotonic_clock::epoch());
832 EXPECT_EQ(pi2_event_loop->context().realtime_remote_time,
833 pi2_ping_count * chrono::milliseconds(10) +
834 realtime_clock::epoch());
835 EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time +
836 chrono::microseconds(150),
837 pi2_event_loop->context().monotonic_event_time);
838 EXPECT_EQ(pi2_event_loop->context().realtime_remote_time +
839 chrono::microseconds(150),
840 pi2_event_loop->context().realtime_event_time);
841 ++pi2_ping_count;
Austin Schuh15649d62019-12-28 16:36:38 -0800842 });
843
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700844 constexpr ssize_t kQueueIndexOffset = -9;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800845 // Confirm that the ping and pong counts both match, and the value also
846 // matches.
847 pi1_event_loop->MakeWatcher(
848 "/test", [&pi1_event_loop, &pi1_ping_count,
849 &pi1_pong_count](const examples::Pong &pong) {
850 VLOG(1) << "Pi1 pong " << FlatbufferToJson(&pong) << " at "
851 << pi1_event_loop->context().monotonic_remote_time << " -> "
852 << pi1_event_loop->context().monotonic_event_time;
853
854 EXPECT_EQ(pi1_event_loop->context().remote_queue_index,
855 pi1_pong_count + kQueueIndexOffset);
856 EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time,
857 chrono::microseconds(200) +
858 pi1_pong_count * chrono::milliseconds(10) +
859 monotonic_clock::epoch());
860 EXPECT_EQ(pi1_event_loop->context().realtime_remote_time,
861 chrono::microseconds(200) +
862 pi1_pong_count * chrono::milliseconds(10) +
863 realtime_clock::epoch());
864
865 EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time +
866 chrono::microseconds(150),
867 pi1_event_loop->context().monotonic_event_time);
868 EXPECT_EQ(pi1_event_loop->context().realtime_remote_time +
869 chrono::microseconds(150),
870 pi1_event_loop->context().realtime_event_time);
871
872 EXPECT_EQ(pong.value(), pi1_pong_count + 1);
873 ++pi1_pong_count;
874 EXPECT_EQ(pi1_ping_count, pi1_pong_count);
875 });
876 pi2_event_loop->MakeWatcher(
877 "/test", [&pi2_event_loop, &pi2_ping_count,
878 &pi2_pong_count](const examples::Pong &pong) {
879 VLOG(1) << "Pi2 pong " << FlatbufferToJson(&pong) << " at "
880 << pi2_event_loop->context().monotonic_remote_time << " -> "
881 << pi2_event_loop->context().monotonic_event_time;
882
883 EXPECT_EQ(pi2_event_loop->context().remote_queue_index,
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700884 pi2_pong_count + kQueueIndexOffset);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800885
886 EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time,
887 chrono::microseconds(200) +
888 pi2_pong_count * chrono::milliseconds(10) +
889 monotonic_clock::epoch());
890 EXPECT_EQ(pi2_event_loop->context().realtime_remote_time,
891 chrono::microseconds(200) +
892 pi2_pong_count * chrono::milliseconds(10) +
893 realtime_clock::epoch());
894
895 EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time,
896 pi2_event_loop->context().monotonic_event_time);
897 EXPECT_EQ(pi2_event_loop->context().realtime_remote_time,
898 pi2_event_loop->context().realtime_event_time);
899
900 EXPECT_EQ(pong.value(), pi2_pong_count + 1);
901 ++pi2_pong_count;
902 EXPECT_EQ(pi2_ping_count, pi2_pong_count);
903 });
904
905 log_reader_factory.Run();
906 EXPECT_EQ(pi1_ping_count, 2010);
907 EXPECT_EQ(pi2_ping_count, 2010);
908 EXPECT_EQ(pi1_pong_count, 2010);
909 EXPECT_EQ(pi2_pong_count, 2010);
Austin Schuh6331ef92020-01-07 18:28:09 -0800910
911 reader.Deregister();
Austin Schuh15649d62019-12-28 16:36:38 -0800912}
913
James Kuszmaul46d82582020-05-09 19:50:09 -0700914typedef MultinodeLoggerTest MultinodeLoggerDeathTest;
915
916// Test that if we feed the replay with a mismatched node list that we die on
917// the LogReader constructor.
918TEST_F(MultinodeLoggerDeathTest, MultiNodeBadReplayConfig) {
James Kuszmaul46d82582020-05-09 19:50:09 -0700919 {
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700920 LoggerState pi1_logger = MakeLogger(pi1_);
921 LoggerState pi2_logger = MakeLogger(pi2_);
James Kuszmaul46d82582020-05-09 19:50:09 -0700922
923 event_loop_factory_.RunFor(chrono::milliseconds(95));
924
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700925 StartLogger(&pi1_logger);
926 StartLogger(&pi2_logger);
James Kuszmaul46d82582020-05-09 19:50:09 -0700927
James Kuszmaul46d82582020-05-09 19:50:09 -0700928 event_loop_factory_.RunFor(chrono::milliseconds(20000));
929 }
930
931 // Test that, if we add an additional node to the replay config that the
932 // logger complains about the mismatch in number of nodes.
933 FlatbufferDetachedBuffer<Configuration> extra_nodes_config =
934 configuration::MergeWithConfig(&config_.message(), R"({
935 "nodes": [
936 {
937 "name": "extra-node"
938 }
939 ]
940 }
941 )");
942
Austin Schuh287d43d2020-12-04 20:19:33 -0800943 const std::vector<LogFile> sorted_parts = SortParts(logfiles_);
944 EXPECT_DEATH(LogReader(sorted_parts, &extra_nodes_config.message()),
James Kuszmaul46d82582020-05-09 19:50:09 -0700945 "Log file and replay config need to have matching nodes lists.");
James Kuszmaul46d82582020-05-09 19:50:09 -0700946}
947
Austin Schuhcde938c2020-02-02 17:30:07 -0800948// Tests that we can read log files where they don't start at the same monotonic
949// time.
950TEST_F(MultinodeLoggerTest, StaggeredStart) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800951 {
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700952 LoggerState pi1_logger = MakeLogger(pi1_);
953 LoggerState pi2_logger = MakeLogger(pi2_);
Austin Schuhcde938c2020-02-02 17:30:07 -0800954
955 event_loop_factory_.RunFor(chrono::milliseconds(95));
956
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700957 StartLogger(&pi1_logger);
Austin Schuhcde938c2020-02-02 17:30:07 -0800958
959 event_loop_factory_.RunFor(chrono::milliseconds(200));
960
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700961 StartLogger(&pi2_logger);
962
Austin Schuhcde938c2020-02-02 17:30:07 -0800963 event_loop_factory_.RunFor(chrono::milliseconds(20000));
964 }
965
Austin Schuh287d43d2020-12-04 20:19:33 -0800966 LogReader reader(SortParts(logfiles_));
Austin Schuhcde938c2020-02-02 17:30:07 -0800967
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700968 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
Austin Schuhcde938c2020-02-02 17:30:07 -0800969 log_reader_factory.set_send_delay(chrono::microseconds(0));
970
971 // This sends out the fetched messages and advances time to the start of the
972 // log file.
973 reader.Register(&log_reader_factory);
974
975 const Node *pi1 =
976 configuration::GetNode(log_reader_factory.configuration(), "pi1");
977 const Node *pi2 =
978 configuration::GetNode(log_reader_factory.configuration(), "pi2");
979
980 EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(pi1, pi2));
981
982 reader.event_loop_factory()->set_send_delay(chrono::microseconds(0));
983
984 std::unique_ptr<EventLoop> pi1_event_loop =
985 log_reader_factory.MakeEventLoop("test", pi1);
986 std::unique_ptr<EventLoop> pi2_event_loop =
987 log_reader_factory.MakeEventLoop("test", pi2);
988
989 int pi1_ping_count = 30;
990 int pi2_ping_count = 30;
991 int pi1_pong_count = 30;
992 int pi2_pong_count = 30;
993
994 // Confirm that the ping value matches.
995 pi1_event_loop->MakeWatcher(
996 "/test", [&pi1_ping_count, &pi1_event_loop](const examples::Ping &ping) {
997 VLOG(1) << "Pi1 ping " << FlatbufferToJson(&ping)
998 << pi1_event_loop->context().monotonic_remote_time << " -> "
999 << pi1_event_loop->context().monotonic_event_time;
1000 EXPECT_EQ(ping.value(), pi1_ping_count + 1);
1001
1002 ++pi1_ping_count;
1003 });
1004 pi2_event_loop->MakeWatcher(
1005 "/test", [&pi2_ping_count, &pi2_event_loop](const examples::Ping &ping) {
1006 VLOG(1) << "Pi2 ping " << FlatbufferToJson(&ping)
1007 << pi2_event_loop->context().monotonic_remote_time << " -> "
1008 << pi2_event_loop->context().monotonic_event_time;
1009 EXPECT_EQ(ping.value(), pi2_ping_count + 1);
1010
1011 ++pi2_ping_count;
1012 });
1013
1014 // Confirm that the ping and pong counts both match, and the value also
1015 // matches.
1016 pi1_event_loop->MakeWatcher(
1017 "/test", [&pi1_event_loop, &pi1_ping_count,
1018 &pi1_pong_count](const examples::Pong &pong) {
1019 VLOG(1) << "Pi1 pong " << FlatbufferToJson(&pong) << " at "
1020 << pi1_event_loop->context().monotonic_remote_time << " -> "
1021 << pi1_event_loop->context().monotonic_event_time;
1022
1023 EXPECT_EQ(pong.value(), pi1_pong_count + 1);
1024 ++pi1_pong_count;
1025 EXPECT_EQ(pi1_ping_count, pi1_pong_count);
1026 });
1027 pi2_event_loop->MakeWatcher(
1028 "/test", [&pi2_event_loop, &pi2_ping_count,
1029 &pi2_pong_count](const examples::Pong &pong) {
1030 VLOG(1) << "Pi2 pong " << FlatbufferToJson(&pong) << " at "
1031 << pi2_event_loop->context().monotonic_remote_time << " -> "
1032 << pi2_event_loop->context().monotonic_event_time;
1033
1034 EXPECT_EQ(pong.value(), pi2_pong_count + 1);
1035 ++pi2_pong_count;
1036 EXPECT_EQ(pi2_ping_count, pi2_pong_count);
1037 });
1038
1039 log_reader_factory.Run();
1040 EXPECT_EQ(pi1_ping_count, 2030);
1041 EXPECT_EQ(pi2_ping_count, 2030);
1042 EXPECT_EQ(pi1_pong_count, 2030);
1043 EXPECT_EQ(pi2_pong_count, 2030);
1044
1045 reader.Deregister();
1046}
Austin Schuh6f3babe2020-01-26 20:34:50 -08001047
Austin Schuh8bd96322020-02-13 21:18:22 -08001048// Tests that we can read log files where the monotonic clocks drift and don't
1049// match correctly. While we are here, also test that different ending times
1050// also is readable.
1051TEST_F(MultinodeLoggerTest, MismatchedClocks) {
Austin Schuhcde938c2020-02-02 17:30:07 -08001052 {
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001053 LoggerState pi2_logger = MakeLogger(pi2_);
1054
Austin Schuh8bd96322020-02-13 21:18:22 -08001055 NodeEventLoopFactory *pi2 =
1056 event_loop_factory_.GetNodeEventLoopFactory(pi2_);
1057 LOG(INFO) << "pi2 times: " << pi2->monotonic_now() << " "
1058 << pi2->realtime_now() << " distributed "
1059 << pi2->ToDistributedClock(pi2->monotonic_now());
Austin Schuhcde938c2020-02-02 17:30:07 -08001060
Austin Schuh8bd96322020-02-13 21:18:22 -08001061 const chrono::nanoseconds initial_pi2_offset = -chrono::seconds(1000);
1062 chrono::nanoseconds pi2_offset = initial_pi2_offset;
Austin Schuhcde938c2020-02-02 17:30:07 -08001063
Austin Schuhbe69cf32020-08-27 11:38:33 -07001064 pi2->SetDistributedOffset(-pi2_offset, 1.0);
Austin Schuh8bd96322020-02-13 21:18:22 -08001065 LOG(INFO) << "pi2 times: " << pi2->monotonic_now() << " "
1066 << pi2->realtime_now() << " distributed "
1067 << pi2->ToDistributedClock(pi2->monotonic_now());
Austin Schuhcde938c2020-02-02 17:30:07 -08001068
Austin Schuh8bd96322020-02-13 21:18:22 -08001069 for (int i = 0; i < 95; ++i) {
1070 pi2_offset += chrono::nanoseconds(200);
Austin Schuhbe69cf32020-08-27 11:38:33 -07001071 pi2->SetDistributedOffset(-pi2_offset, 1.0);
Austin Schuh8bd96322020-02-13 21:18:22 -08001072 event_loop_factory_.RunFor(chrono::milliseconds(1));
1073 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001074
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001075 StartLogger(&pi2_logger);
Austin Schuhcde938c2020-02-02 17:30:07 -08001076
Austin Schuh8bd96322020-02-13 21:18:22 -08001077 event_loop_factory_.RunFor(chrono::milliseconds(200));
Austin Schuhcde938c2020-02-02 17:30:07 -08001078
Austin Schuh8bd96322020-02-13 21:18:22 -08001079 {
1080 // Run pi1's logger for only part of the time.
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001081 LoggerState pi1_logger = MakeLogger(pi1_);
Austin Schuh8bd96322020-02-13 21:18:22 -08001082
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001083 StartLogger(&pi1_logger);
Austin Schuh8bd96322020-02-13 21:18:22 -08001084
1085 for (int i = 0; i < 20000; ++i) {
1086 pi2_offset += chrono::nanoseconds(200);
Austin Schuhbe69cf32020-08-27 11:38:33 -07001087 pi2->SetDistributedOffset(-pi2_offset, 1.0);
Austin Schuh8bd96322020-02-13 21:18:22 -08001088 event_loop_factory_.RunFor(chrono::milliseconds(1));
1089 }
1090
1091 EXPECT_GT(pi2_offset - initial_pi2_offset,
1092 event_loop_factory_.send_delay() +
1093 event_loop_factory_.network_delay());
1094
1095 for (int i = 0; i < 40000; ++i) {
1096 pi2_offset -= chrono::nanoseconds(200);
Austin Schuhbe69cf32020-08-27 11:38:33 -07001097 pi2->SetDistributedOffset(-pi2_offset, 1.0);
Austin Schuh8bd96322020-02-13 21:18:22 -08001098 event_loop_factory_.RunFor(chrono::milliseconds(1));
1099 }
1100 }
1101
1102 // And log a bit more on pi2.
1103 event_loop_factory_.RunFor(chrono::milliseconds(400));
Austin Schuhcde938c2020-02-02 17:30:07 -08001104 }
1105
Austin Schuh287d43d2020-12-04 20:19:33 -08001106 LogReader reader(SortParts(logfiles_));
Austin Schuhcde938c2020-02-02 17:30:07 -08001107
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001108 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
Austin Schuhcde938c2020-02-02 17:30:07 -08001109 log_reader_factory.set_send_delay(chrono::microseconds(0));
1110
Austin Schuhcde938c2020-02-02 17:30:07 -08001111 const Node *pi1 =
1112 configuration::GetNode(log_reader_factory.configuration(), "pi1");
1113 const Node *pi2 =
1114 configuration::GetNode(log_reader_factory.configuration(), "pi2");
1115
Austin Schuh2f8fd752020-09-01 22:38:28 -07001116 // This sends out the fetched messages and advances time to the start of the
1117 // log file.
1118 reader.Register(&log_reader_factory);
1119
1120 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi1) << " pi1";
1121 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi2) << " pi2";
1122 LOG(INFO) << "now pi1 "
1123 << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now();
1124 LOG(INFO) << "now pi2 "
1125 << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now();
1126
Austin Schuhcde938c2020-02-02 17:30:07 -08001127 LOG(INFO) << "Done registering (pi1) "
Austin Schuh391e3172020-09-01 22:48:18 -07001128 << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now()
1129 << " "
Austin Schuhcde938c2020-02-02 17:30:07 -08001130 << log_reader_factory.GetNodeEventLoopFactory(pi1)->realtime_now();
1131 LOG(INFO) << "Done registering (pi2) "
Austin Schuh391e3172020-09-01 22:48:18 -07001132 << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now()
1133 << " "
Austin Schuhcde938c2020-02-02 17:30:07 -08001134 << log_reader_factory.GetNodeEventLoopFactory(pi2)->realtime_now();
1135
1136 EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(pi1, pi2));
1137
1138 reader.event_loop_factory()->set_send_delay(chrono::microseconds(0));
1139
1140 std::unique_ptr<EventLoop> pi1_event_loop =
1141 log_reader_factory.MakeEventLoop("test", pi1);
1142 std::unique_ptr<EventLoop> pi2_event_loop =
1143 log_reader_factory.MakeEventLoop("test", pi2);
1144
1145 int pi1_ping_count = 30;
1146 int pi2_ping_count = 30;
1147 int pi1_pong_count = 30;
1148 int pi2_pong_count = 30;
1149
1150 // Confirm that the ping value matches.
1151 pi1_event_loop->MakeWatcher(
1152 "/test", [&pi1_ping_count, &pi1_event_loop](const examples::Ping &ping) {
1153 VLOG(1) << "Pi1 ping " << FlatbufferToJson(&ping)
1154 << pi1_event_loop->context().monotonic_remote_time << " -> "
1155 << pi1_event_loop->context().monotonic_event_time;
1156 EXPECT_EQ(ping.value(), pi1_ping_count + 1);
1157
1158 ++pi1_ping_count;
1159 });
1160 pi2_event_loop->MakeWatcher(
1161 "/test", [&pi2_ping_count, &pi2_event_loop](const examples::Ping &ping) {
1162 VLOG(1) << "Pi2 ping " << FlatbufferToJson(&ping)
1163 << pi2_event_loop->context().monotonic_remote_time << " -> "
1164 << pi2_event_loop->context().monotonic_event_time;
1165 EXPECT_EQ(ping.value(), pi2_ping_count + 1);
1166
1167 ++pi2_ping_count;
1168 });
1169
1170 // Confirm that the ping and pong counts both match, and the value also
1171 // matches.
1172 pi1_event_loop->MakeWatcher(
1173 "/test", [&pi1_event_loop, &pi1_ping_count,
1174 &pi1_pong_count](const examples::Pong &pong) {
1175 VLOG(1) << "Pi1 pong " << FlatbufferToJson(&pong) << " at "
1176 << pi1_event_loop->context().monotonic_remote_time << " -> "
1177 << pi1_event_loop->context().monotonic_event_time;
1178
1179 EXPECT_EQ(pong.value(), pi1_pong_count + 1);
1180 ++pi1_pong_count;
1181 EXPECT_EQ(pi1_ping_count, pi1_pong_count);
1182 });
1183 pi2_event_loop->MakeWatcher(
1184 "/test", [&pi2_event_loop, &pi2_ping_count,
1185 &pi2_pong_count](const examples::Pong &pong) {
1186 VLOG(1) << "Pi2 pong " << FlatbufferToJson(&pong) << " at "
1187 << pi2_event_loop->context().monotonic_remote_time << " -> "
1188 << pi2_event_loop->context().monotonic_event_time;
1189
1190 EXPECT_EQ(pong.value(), pi2_pong_count + 1);
1191 ++pi2_pong_count;
1192 EXPECT_EQ(pi2_ping_count, pi2_pong_count);
1193 });
1194
1195 log_reader_factory.Run();
Austin Schuh8bd96322020-02-13 21:18:22 -08001196 EXPECT_EQ(pi1_ping_count, 6030);
1197 EXPECT_EQ(pi2_ping_count, 6030);
1198 EXPECT_EQ(pi1_pong_count, 6030);
1199 EXPECT_EQ(pi2_pong_count, 6030);
Austin Schuhcde938c2020-02-02 17:30:07 -08001200
1201 reader.Deregister();
1202}
1203
Austin Schuh5212cad2020-09-09 23:12:09 -07001204// Tests that we can sort a bunch of parts into the pre-determined sorted parts.
1205TEST_F(MultinodeLoggerTest, SortParts) {
1206 // Make a bunch of parts.
1207 {
1208 LoggerState pi1_logger = MakeLogger(pi1_);
1209 LoggerState pi2_logger = MakeLogger(pi2_);
1210
1211 event_loop_factory_.RunFor(chrono::milliseconds(95));
1212
1213 StartLogger(&pi1_logger);
1214 StartLogger(&pi2_logger);
1215
1216 event_loop_factory_.RunFor(chrono::milliseconds(2000));
1217 }
1218
Austin Schuh11d43732020-09-21 17:28:30 -07001219 const std::vector<LogFile> sorted_parts = SortParts(logfiles_);
Austin Schuh3bd4c402020-11-06 18:19:06 -08001220 VerifyParts(sorted_parts);
1221}
Austin Schuh11d43732020-09-21 17:28:30 -07001222
Austin Schuh3bd4c402020-11-06 18:19:06 -08001223// Tests that we can sort a bunch of parts with an empty part. We should ignore
1224// it and remove it from the sorted list.
1225TEST_F(MultinodeLoggerTest, SortEmptyParts) {
1226 // Make a bunch of parts.
1227 {
1228 LoggerState pi1_logger = MakeLogger(pi1_);
1229 LoggerState pi2_logger = MakeLogger(pi2_);
Austin Schuh11d43732020-09-21 17:28:30 -07001230
Austin Schuh3bd4c402020-11-06 18:19:06 -08001231 event_loop_factory_.RunFor(chrono::milliseconds(95));
Austin Schuh11d43732020-09-21 17:28:30 -07001232
Austin Schuh3bd4c402020-11-06 18:19:06 -08001233 StartLogger(&pi1_logger);
1234 StartLogger(&pi2_logger);
Austin Schuh11d43732020-09-21 17:28:30 -07001235
Austin Schuh3bd4c402020-11-06 18:19:06 -08001236 event_loop_factory_.RunFor(chrono::milliseconds(2000));
Austin Schuh11d43732020-09-21 17:28:30 -07001237 }
1238
Austin Schuh3bd4c402020-11-06 18:19:06 -08001239 // TODO(austin): Should we flip out if the file can't open?
1240 const std::string kEmptyFile("foobarinvalidfiledoesnotexist.bfbs");
Austin Schuh11d43732020-09-21 17:28:30 -07001241
Austin Schuh3bd4c402020-11-06 18:19:06 -08001242 aos::util::WriteStringToFileOrDie(kEmptyFile, "");
1243 logfiles_.emplace_back(kEmptyFile);
Austin Schuh5212cad2020-09-09 23:12:09 -07001244
Austin Schuh3bd4c402020-11-06 18:19:06 -08001245 const std::vector<LogFile> sorted_parts = SortParts(logfiles_);
1246 VerifyParts(sorted_parts, {kEmptyFile});
Austin Schuh5212cad2020-09-09 23:12:09 -07001247}
1248
Austin Schuh3bd4c402020-11-06 18:19:06 -08001249#ifdef LZMA
1250// Tests that we can sort a bunch of parts with an empty .xz file in there. The
1251// empty file should be ignored.
1252TEST_F(MultinodeLoggerTest, SortEmptyCompressedParts) {
1253 // Make a bunch of parts.
1254 {
1255 LoggerState pi1_logger = MakeLogger(pi1_);
1256 LoggerState pi2_logger = MakeLogger(pi2_);
1257
1258 event_loop_factory_.RunFor(chrono::milliseconds(95));
1259
1260 StartLogger(&pi1_logger, "", true);
1261 StartLogger(&pi2_logger, "", true);
1262
1263 event_loop_factory_.RunFor(chrono::milliseconds(2000));
1264 }
1265
1266 // TODO(austin): Should we flip out if the file can't open?
1267 const std::string kEmptyFile("foobarinvalidfiledoesnotexist.bfbs.xz");
1268
1269 AddExtension(".xz");
1270
1271 aos::util::WriteStringToFileOrDie(kEmptyFile, "");
1272 logfiles_.emplace_back(kEmptyFile);
1273
1274 const std::vector<LogFile> sorted_parts = SortParts(logfiles_);
1275 VerifyParts(sorted_parts, {kEmptyFile});
1276}
1277
1278// Tests that we can sort a bunch of parts with the end missing off a compressed
1279// file. We should use the part we can read.
1280TEST_F(MultinodeLoggerTest, SortTruncatedCompressedParts) {
1281 // Make a bunch of parts.
1282 {
1283 LoggerState pi1_logger = MakeLogger(pi1_);
1284 LoggerState pi2_logger = MakeLogger(pi2_);
1285
1286 event_loop_factory_.RunFor(chrono::milliseconds(95));
1287
1288 StartLogger(&pi1_logger, "", true);
1289 StartLogger(&pi2_logger, "", true);
1290
1291 event_loop_factory_.RunFor(chrono::milliseconds(2000));
1292 }
1293
1294 // Append everything with .xz.
1295 AddExtension(".xz");
1296
1297 // Strip off the end of one of the files. Pick one with a lot of data.
1298 ::std::string compressed_contents =
1299 aos::util::ReadFileToStringOrDie(logfiles_[0]);
1300
1301 aos::util::WriteStringToFileOrDie(
1302 logfiles_[0],
1303 compressed_contents.substr(0, compressed_contents.size() - 100));
1304
1305 const std::vector<LogFile> sorted_parts = SortParts(logfiles_);
1306 VerifyParts(sorted_parts);
1307}
1308#endif
1309
Austin Schuh01b4c352020-09-21 23:09:39 -07001310// Tests that if we remap a remapped channel, it shows up correctly.
1311TEST_F(MultinodeLoggerTest, RemapLoggedChannel) {
1312 {
1313 LoggerState pi1_logger = MakeLogger(pi1_);
1314 LoggerState pi2_logger = MakeLogger(pi2_);
1315
1316 event_loop_factory_.RunFor(chrono::milliseconds(95));
1317
1318 StartLogger(&pi1_logger);
1319 StartLogger(&pi2_logger);
1320
1321 event_loop_factory_.RunFor(chrono::milliseconds(20000));
1322 }
1323
Austin Schuh287d43d2020-12-04 20:19:33 -08001324 LogReader reader(SortParts(logfiles_));
Austin Schuh01b4c352020-09-21 23:09:39 -07001325
1326 // Remap just on pi1.
1327 reader.RemapLoggedChannel<aos::timing::Report>(
1328 "/aos", configuration::GetNode(reader.configuration(), "pi1"));
1329
1330 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
1331 log_reader_factory.set_send_delay(chrono::microseconds(0));
1332
1333 reader.Register(&log_reader_factory);
1334
1335 const Node *pi1 =
1336 configuration::GetNode(log_reader_factory.configuration(), "pi1");
1337 const Node *pi2 =
1338 configuration::GetNode(log_reader_factory.configuration(), "pi2");
1339
1340 // Confirm we can read the data on the remapped channel, just for pi1. Nothing
1341 // else should have moved.
1342 std::unique_ptr<EventLoop> pi1_event_loop =
1343 log_reader_factory.MakeEventLoop("test", pi1);
1344 pi1_event_loop->SkipTimingReport();
1345 std::unique_ptr<EventLoop> full_pi1_event_loop =
1346 log_reader_factory.MakeEventLoop("test", pi1);
1347 full_pi1_event_loop->SkipTimingReport();
1348 std::unique_ptr<EventLoop> pi2_event_loop =
1349 log_reader_factory.MakeEventLoop("test", pi2);
1350 pi2_event_loop->SkipTimingReport();
1351
1352 MessageCounter<aos::timing::Report> pi1_timing_report(pi1_event_loop.get(),
1353 "/aos");
1354 MessageCounter<aos::timing::Report> full_pi1_timing_report(
1355 full_pi1_event_loop.get(), "/pi1/aos");
1356 MessageCounter<aos::timing::Report> pi1_original_timing_report(
1357 pi1_event_loop.get(), "/original/aos");
1358 MessageCounter<aos::timing::Report> full_pi1_original_timing_report(
1359 full_pi1_event_loop.get(), "/original/pi1/aos");
1360 MessageCounter<aos::timing::Report> pi2_timing_report(pi2_event_loop.get(),
1361 "/aos");
1362
1363 log_reader_factory.Run();
1364
1365 EXPECT_EQ(pi1_timing_report.count(), 0u);
1366 EXPECT_EQ(full_pi1_timing_report.count(), 0u);
1367 EXPECT_NE(pi1_original_timing_report.count(), 0u);
1368 EXPECT_NE(full_pi1_original_timing_report.count(), 0u);
1369 EXPECT_NE(pi2_timing_report.count(), 0u);
1370
1371 reader.Deregister();
1372}
1373
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001374// Tests that we properly recreate forwarded timestamps when replaying a log.
1375// This should be enough that we can then re-run the logger and get a valid log
1376// back.
1377TEST_F(MultinodeLoggerTest, MessageHeader) {
1378 {
1379 LoggerState pi1_logger = MakeLogger(pi1_);
1380 LoggerState pi2_logger = MakeLogger(pi2_);
1381
1382 event_loop_factory_.RunFor(chrono::milliseconds(95));
1383
1384 StartLogger(&pi1_logger);
1385 StartLogger(&pi2_logger);
1386
1387 event_loop_factory_.RunFor(chrono::milliseconds(20000));
1388 }
1389
Austin Schuh287d43d2020-12-04 20:19:33 -08001390 LogReader reader(SortParts(logfiles_));
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001391
1392 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
1393 log_reader_factory.set_send_delay(chrono::microseconds(0));
1394
1395 // This sends out the fetched messages and advances time to the start of the
1396 // log file.
1397 reader.Register(&log_reader_factory);
1398
1399 const Node *pi1 =
1400 configuration::GetNode(log_reader_factory.configuration(), "pi1");
1401 const Node *pi2 =
1402 configuration::GetNode(log_reader_factory.configuration(), "pi2");
1403
1404 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi1) << " pi1";
1405 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi2) << " pi2";
1406 LOG(INFO) << "now pi1 "
1407 << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now();
1408 LOG(INFO) << "now pi2 "
1409 << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now();
1410
1411 EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(pi1, pi2));
1412
1413 reader.event_loop_factory()->set_send_delay(chrono::microseconds(0));
1414
1415 std::unique_ptr<EventLoop> pi1_event_loop =
1416 log_reader_factory.MakeEventLoop("test", pi1);
1417 std::unique_ptr<EventLoop> pi2_event_loop =
1418 log_reader_factory.MakeEventLoop("test", pi2);
1419
Austin Schuh0de30f32020-12-06 12:44:28 -08001420 MessageCounter<RemoteMessage> pi1_original_message_header_counter(
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001421 pi1_event_loop.get(), "/original/aos/remote_timestamps/pi2");
Austin Schuh0de30f32020-12-06 12:44:28 -08001422 MessageCounter<RemoteMessage> pi2_original_message_header_counter(
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001423 pi2_event_loop.get(), "/original/aos/remote_timestamps/pi1");
1424
1425 aos::Fetcher<message_bridge::Timestamp> pi1_timestamp_on_pi1_fetcher =
1426 pi1_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi1/aos");
1427 aos::Fetcher<message_bridge::Timestamp> pi1_timestamp_on_pi2_fetcher =
1428 pi2_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi1/aos");
1429
1430 aos::Fetcher<examples::Ping> ping_on_pi1_fetcher =
1431 pi1_event_loop->MakeFetcher<examples::Ping>("/test");
1432 aos::Fetcher<examples::Ping> ping_on_pi2_fetcher =
1433 pi2_event_loop->MakeFetcher<examples::Ping>("/test");
1434
1435 aos::Fetcher<message_bridge::Timestamp> pi2_timestamp_on_pi2_fetcher =
1436 pi2_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi2/aos");
1437 aos::Fetcher<message_bridge::Timestamp> pi2_timestamp_on_pi1_fetcher =
1438 pi1_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi2/aos");
1439
1440 aos::Fetcher<examples::Pong> pong_on_pi2_fetcher =
1441 pi2_event_loop->MakeFetcher<examples::Pong>("/test");
1442 aos::Fetcher<examples::Pong> pong_on_pi1_fetcher =
1443 pi1_event_loop->MakeFetcher<examples::Pong>("/test");
1444
1445 const size_t pi1_timestamp_channel = configuration::ChannelIndex(
1446 pi1_event_loop->configuration(), pi1_timestamp_on_pi1_fetcher.channel());
1447 const size_t ping_timestamp_channel = configuration::ChannelIndex(
1448 pi2_event_loop->configuration(), ping_on_pi2_fetcher.channel());
1449
1450 const size_t pi2_timestamp_channel = configuration::ChannelIndex(
1451 pi2_event_loop->configuration(), pi2_timestamp_on_pi2_fetcher.channel());
1452 const size_t pong_timestamp_channel = configuration::ChannelIndex(
1453 pi1_event_loop->configuration(), pong_on_pi1_fetcher.channel());
1454
1455 pi1_event_loop->MakeWatcher(
1456 "/aos/remote_timestamps/pi2",
1457 [&pi1_event_loop, pi1_timestamp_channel, ping_timestamp_channel,
1458 &pi1_timestamp_on_pi1_fetcher, &pi1_timestamp_on_pi2_fetcher,
1459 &ping_on_pi1_fetcher,
Austin Schuh0de30f32020-12-06 12:44:28 -08001460 &ping_on_pi2_fetcher](const RemoteMessage &header) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001461 const aos::monotonic_clock::time_point header_monotonic_sent_time(
1462 chrono::nanoseconds(header.monotonic_sent_time()));
1463 const aos::realtime_clock::time_point header_realtime_sent_time(
1464 chrono::nanoseconds(header.realtime_sent_time()));
1465 const aos::monotonic_clock::time_point header_monotonic_remote_time(
1466 chrono::nanoseconds(header.monotonic_remote_time()));
1467 const aos::realtime_clock::time_point header_realtime_remote_time(
1468 chrono::nanoseconds(header.realtime_remote_time()));
1469
1470 const Context *pi1_context = nullptr;
1471 const Context *pi2_context = nullptr;
1472
1473 if (header.channel_index() == pi1_timestamp_channel) {
1474 ASSERT_TRUE(pi1_timestamp_on_pi1_fetcher.FetchNext());
1475 ASSERT_TRUE(pi1_timestamp_on_pi2_fetcher.FetchNext());
1476 pi1_context = &pi1_timestamp_on_pi1_fetcher.context();
1477 pi2_context = &pi1_timestamp_on_pi2_fetcher.context();
1478 } else if (header.channel_index() == ping_timestamp_channel) {
1479 ASSERT_TRUE(ping_on_pi1_fetcher.FetchNext());
1480 ASSERT_TRUE(ping_on_pi2_fetcher.FetchNext());
1481 pi1_context = &ping_on_pi1_fetcher.context();
1482 pi2_context = &ping_on_pi2_fetcher.context();
1483 } else {
1484 LOG(FATAL) << "Unknown channel " << FlatbufferToJson(&header) << " "
1485 << configuration::CleanedChannelToString(
1486 pi1_event_loop->configuration()->channels()->Get(
1487 header.channel_index()));
1488 }
1489
1490 EXPECT_EQ(pi1_context->queue_index, header.remote_queue_index());
1491 EXPECT_EQ(pi2_context->remote_queue_index, header.remote_queue_index());
1492 EXPECT_EQ(pi2_context->queue_index, header.queue_index());
1493
1494 EXPECT_EQ(pi2_context->monotonic_event_time,
1495 header_monotonic_sent_time);
1496 EXPECT_EQ(pi2_context->realtime_event_time, header_realtime_sent_time);
1497 EXPECT_EQ(pi2_context->realtime_remote_time,
1498 header_realtime_remote_time);
1499 EXPECT_EQ(pi2_context->monotonic_remote_time,
1500 header_monotonic_remote_time);
1501
1502 EXPECT_EQ(pi1_context->realtime_event_time,
1503 header_realtime_remote_time);
1504 EXPECT_EQ(pi1_context->monotonic_event_time,
1505 header_monotonic_remote_time);
1506 });
1507 pi2_event_loop->MakeWatcher(
1508 "/aos/remote_timestamps/pi1",
1509 [&pi2_event_loop, pi2_timestamp_channel, pong_timestamp_channel,
1510 &pi2_timestamp_on_pi2_fetcher, &pi2_timestamp_on_pi1_fetcher,
1511 &pong_on_pi2_fetcher,
Austin Schuh0de30f32020-12-06 12:44:28 -08001512 &pong_on_pi1_fetcher](const RemoteMessage &header) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001513 const aos::monotonic_clock::time_point header_monotonic_sent_time(
1514 chrono::nanoseconds(header.monotonic_sent_time()));
1515 const aos::realtime_clock::time_point header_realtime_sent_time(
1516 chrono::nanoseconds(header.realtime_sent_time()));
1517 const aos::monotonic_clock::time_point header_monotonic_remote_time(
1518 chrono::nanoseconds(header.monotonic_remote_time()));
1519 const aos::realtime_clock::time_point header_realtime_remote_time(
1520 chrono::nanoseconds(header.realtime_remote_time()));
1521
1522 const Context *pi2_context = nullptr;
1523 const Context *pi1_context = nullptr;
1524
1525 if (header.channel_index() == pi2_timestamp_channel) {
1526 ASSERT_TRUE(pi2_timestamp_on_pi2_fetcher.FetchNext());
1527 ASSERT_TRUE(pi2_timestamp_on_pi1_fetcher.FetchNext());
1528 pi2_context = &pi2_timestamp_on_pi2_fetcher.context();
1529 pi1_context = &pi2_timestamp_on_pi1_fetcher.context();
1530 } else if (header.channel_index() == pong_timestamp_channel) {
1531 ASSERT_TRUE(pong_on_pi2_fetcher.FetchNext());
1532 ASSERT_TRUE(pong_on_pi1_fetcher.FetchNext());
1533 pi2_context = &pong_on_pi2_fetcher.context();
1534 pi1_context = &pong_on_pi1_fetcher.context();
1535 } else {
1536 LOG(FATAL) << "Unknown channel " << FlatbufferToJson(&header) << " "
1537 << configuration::CleanedChannelToString(
1538 pi2_event_loop->configuration()->channels()->Get(
1539 header.channel_index()));
1540 }
1541
1542 EXPECT_EQ(pi2_context->queue_index, header.remote_queue_index());
1543 EXPECT_EQ(pi1_context->remote_queue_index, header.remote_queue_index());
1544 EXPECT_EQ(pi1_context->queue_index, header.queue_index());
1545
1546 EXPECT_EQ(pi1_context->monotonic_event_time,
1547 header_monotonic_sent_time);
1548 EXPECT_EQ(pi1_context->realtime_event_time, header_realtime_sent_time);
1549 EXPECT_EQ(pi1_context->realtime_remote_time,
1550 header_realtime_remote_time);
1551 EXPECT_EQ(pi1_context->monotonic_remote_time,
1552 header_monotonic_remote_time);
1553
1554 EXPECT_EQ(pi2_context->realtime_event_time,
1555 header_realtime_remote_time);
1556 EXPECT_EQ(pi2_context->monotonic_event_time,
1557 header_monotonic_remote_time);
1558 });
1559
1560 // And confirm we can re-create a log again, while checking the contents.
1561 {
1562 LoggerState pi1_logger = MakeLogger(
1563 configuration::GetNode(log_reader_factory.configuration(), pi1_),
1564 &log_reader_factory);
1565 LoggerState pi2_logger = MakeLogger(
1566 configuration::GetNode(log_reader_factory.configuration(), pi2_),
1567 &log_reader_factory);
1568
1569 StartLogger(&pi1_logger, "relogged");
1570 StartLogger(&pi2_logger, "relogged");
1571
1572 log_reader_factory.Run();
1573 }
1574
1575 EXPECT_EQ(pi2_original_message_header_counter.count(), 0u);
1576 EXPECT_EQ(pi1_original_message_header_counter.count(), 0u);
1577
1578 reader.Deregister();
1579}
1580
Austin Schuh8bd96322020-02-13 21:18:22 -08001581// TODO(austin): We can write a test which recreates a logfile and confirms that
1582// we get it back. That is the ultimate test.
1583
Austin Schuhe309d2a2019-11-29 13:25:21 -08001584} // namespace testing
1585} // namespace logger
1586} // namespace aos