blob: ec273a8b46ffd62dededf5f2f88cf1ecae011e55 [file] [log] [blame]
Austin Schuh6bb8a822021-03-31 23:04:39 -07001#include <sys/stat.h>
2
Austin Schuh315b96b2020-12-11 21:21:12 -08003#include "absl/strings/str_format.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -08004#include "aos/events/event_loop.h"
Tyler Chatowb7c6eba2021-07-28 14:43:23 -07005#include "aos/events/logging/log_reader.h"
Austin Schuhb06f03b2021-02-17 22:00:37 -08006#include "aos/events/logging/log_writer.h"
milind1f1dca32021-07-03 13:50:07 -07007#include "aos/events/logging/snappy_encoder.h"
Austin Schuh01b4c352020-09-21 23:09:39 -07008#include "aos/events/message_counter.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -08009#include "aos/events/ping_lib.h"
10#include "aos/events/pong_lib.h"
11#include "aos/events/simulated_event_loop.h"
Austin Schuh0de30f32020-12-06 12:44:28 -080012#include "aos/network/remote_message_generated.h"
Austin Schuh87dd3832021-01-01 23:07:31 -080013#include "aos/network/testing_time_converter.h"
Austin Schuh8d7e0bb2020-10-02 17:57:00 -070014#include "aos/network/timestamp_generated.h"
Austin Schuh373f1762021-06-02 21:07:09 -070015#include "aos/testing/path.h"
Austin Schuhc243b422020-10-11 15:35:08 -070016#include "aos/testing/tmpdir.h"
Austin Schuh2f8fd752020-09-01 22:38:28 -070017#include "aos/util/file.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080018#include "glog/logging.h"
Austin Schuh6f3babe2020-01-26 20:34:50 -080019#include "gmock/gmock.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080020#include "gtest/gtest.h"
21
Austin Schuh3bd4c402020-11-06 18:19:06 -080022#ifdef LZMA
23#include "aos/events/logging/lzma_encoder.h"
24#endif
25
Austin Schuhe309d2a2019-11-29 13:25:21 -080026namespace aos {
27namespace logger {
28namespace testing {
29
Austin Schuh373f1762021-06-02 21:07:09 -070030using aos::testing::ArtifactPath;
31
Austin Schuhe309d2a2019-11-29 13:25:21 -080032namespace chrono = std::chrono;
Austin Schuh0de30f32020-12-06 12:44:28 -080033using aos::message_bridge::RemoteMessage;
Austin Schuh01b4c352020-09-21 23:09:39 -070034using aos::testing::MessageCounter;
Austin Schuhe309d2a2019-11-29 13:25:21 -080035
Austin Schuhee4713b2021-03-21 19:25:17 -070036constexpr std::string_view kSingleConfigSha256(
Austin Schuhb8bca732021-07-30 22:32:00 -070037 "bbe1b563139273b23a5405eebc2f2740cefcda5f96681acd0a84b8ff9ab93ea4");
Austin Schuh8c399962020-12-25 21:51:45 -080038
Austin Schuhb06f03b2021-02-17 22:00:37 -080039std::vector<std::vector<std::string>> ToLogReaderVector(
40 const std::vector<LogFile> &log_files) {
41 std::vector<std::vector<std::string>> result;
42 for (const LogFile &log_file : log_files) {
43 for (const LogParts &log_parts : log_file.parts) {
44 std::vector<std::string> parts;
45 for (const std::string &part : log_parts.parts) {
46 parts.emplace_back(part);
47 }
48 result.emplace_back(std::move(parts));
49 }
50 }
51 return result;
52}
53
Austin Schuhe309d2a2019-11-29 13:25:21 -080054class LoggerTest : public ::testing::Test {
55 public:
56 LoggerTest()
Austin Schuh373f1762021-06-02 21:07:09 -070057 : config_(aos::configuration::ReadConfig(
58 ArtifactPath("aos/events/pingpong_config.json"))),
Austin Schuhe309d2a2019-11-29 13:25:21 -080059 event_loop_factory_(&config_.message()),
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080060 ping_event_loop_(event_loop_factory_.MakeEventLoop("ping")),
Austin Schuhe309d2a2019-11-29 13:25:21 -080061 ping_(ping_event_loop_.get()),
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080062 pong_event_loop_(event_loop_factory_.MakeEventLoop("pong")),
Austin Schuhe309d2a2019-11-29 13:25:21 -080063 pong_(pong_event_loop_.get()) {}
64
65 // Config and factory.
66 aos::FlatbufferDetachedBuffer<aos::Configuration> config_;
67 SimulatedEventLoopFactory event_loop_factory_;
68
69 // Event loop and app for Ping
70 std::unique_ptr<EventLoop> ping_event_loop_;
71 Ping ping_;
72
73 // Event loop and app for Pong
74 std::unique_ptr<EventLoop> pong_event_loop_;
75 Pong pong_;
76};
77
Brian Silverman1f345222020-09-24 21:14:48 -070078using LoggerDeathTest = LoggerTest;
79
Austin Schuhe309d2a2019-11-29 13:25:21 -080080// Tests that we can startup at all. This confirms that the channels are all in
81// the config.
82TEST_F(LoggerTest, Starts) {
Austin Schuhc243b422020-10-11 15:35:08 -070083 const ::std::string tmpdir = aos::testing::TestTmpDir();
Austin Schuh2f8fd752020-09-01 22:38:28 -070084 const ::std::string base_name = tmpdir + "/logfile";
Austin Schuh25b46712021-01-03 00:04:38 -080085 const ::std::string config =
Austin Schuhee4713b2021-03-21 19:25:17 -070086 absl::StrCat(base_name, kSingleConfigSha256, ".bfbs");
Austin Schuh2f8fd752020-09-01 22:38:28 -070087 const ::std::string logfile = base_name + ".part0.bfbs";
Austin Schuhe309d2a2019-11-29 13:25:21 -080088 // Remove it.
Austin Schuh25b46712021-01-03 00:04:38 -080089 unlink(config.c_str());
Austin Schuhe309d2a2019-11-29 13:25:21 -080090 unlink(logfile.c_str());
91
92 LOG(INFO) << "Logging data to " << logfile;
93
94 {
Austin Schuhe309d2a2019-11-29 13:25:21 -080095 std::unique_ptr<EventLoop> logger_event_loop =
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080096 event_loop_factory_.MakeEventLoop("logger");
Austin Schuhe309d2a2019-11-29 13:25:21 -080097
98 event_loop_factory_.RunFor(chrono::milliseconds(95));
99
Brian Silverman1f345222020-09-24 21:14:48 -0700100 Logger logger(logger_event_loop.get());
Austin Schuh8c399962020-12-25 21:51:45 -0800101 logger.set_separate_config(false);
Brian Silverman1f345222020-09-24 21:14:48 -0700102 logger.set_polling_period(std::chrono::milliseconds(100));
103 logger.StartLoggingLocalNamerOnRun(base_name);
Austin Schuhe309d2a2019-11-29 13:25:21 -0800104 event_loop_factory_.RunFor(chrono::milliseconds(20000));
105 }
106
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800107 // Even though it doesn't make any difference here, exercise the logic for
108 // passing in a separate config.
109 LogReader reader(logfile, &config_.message());
Austin Schuhe309d2a2019-11-29 13:25:21 -0800110
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800111 // Confirm that we can remap logged channels to point to new buses.
112 reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original");
Austin Schuhe309d2a2019-11-29 13:25:21 -0800113
Austin Schuh15649d62019-12-28 16:36:38 -0800114 // This sends out the fetched messages and advances time to the start of the
115 // log file.
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800116 reader.Register();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800117
Austin Schuh07676622021-01-21 18:59:17 -0800118 EXPECT_THAT(reader.LoggedNodes(), ::testing::ElementsAre(nullptr));
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800119
Austin Schuhe309d2a2019-11-29 13:25:21 -0800120 std::unique_ptr<EventLoop> test_event_loop =
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800121 reader.event_loop_factory()->MakeEventLoop("log_reader");
Austin Schuhe309d2a2019-11-29 13:25:21 -0800122
123 int ping_count = 10;
124 int pong_count = 10;
125
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800126 // Confirm that the ping value matches in the remapped channel location.
127 test_event_loop->MakeWatcher("/original/test",
Austin Schuhe309d2a2019-11-29 13:25:21 -0800128 [&ping_count](const examples::Ping &ping) {
129 EXPECT_EQ(ping.value(), ping_count + 1);
130 ++ping_count;
131 });
132 // Confirm that the ping and pong counts both match, and the value also
133 // matches.
134 test_event_loop->MakeWatcher(
135 "/test", [&pong_count, &ping_count](const examples::Pong &pong) {
136 EXPECT_EQ(pong.value(), pong_count + 1);
137 ++pong_count;
138 EXPECT_EQ(ping_count, pong_count);
139 });
140
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800141 reader.event_loop_factory()->RunFor(std::chrono::seconds(100));
Austin Schuhe309d2a2019-11-29 13:25:21 -0800142 EXPECT_EQ(ping_count, 2010);
Austin Schuhe309d2a2019-11-29 13:25:21 -0800143}
144
Brian Silverman1f345222020-09-24 21:14:48 -0700145// Tests calling StartLogging twice.
146TEST_F(LoggerDeathTest, ExtraStart) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800147 const ::std::string tmpdir = aos::testing::TestTmpDir();
Brian Silverman1f345222020-09-24 21:14:48 -0700148 const ::std::string base_name1 = tmpdir + "/logfile1";
Austin Schuh25b46712021-01-03 00:04:38 -0800149 const ::std::string config1 =
Austin Schuhee4713b2021-03-21 19:25:17 -0700150 absl::StrCat(base_name1, kSingleConfigSha256, ".bfbs");
Brian Silverman1f345222020-09-24 21:14:48 -0700151 const ::std::string logfile1 = base_name1 + ".part0.bfbs";
152 const ::std::string base_name2 = tmpdir + "/logfile2";
Austin Schuh25b46712021-01-03 00:04:38 -0800153 const ::std::string config2 =
Austin Schuhee4713b2021-03-21 19:25:17 -0700154 absl::StrCat(base_name2, kSingleConfigSha256, ".bfbs");
Brian Silverman1f345222020-09-24 21:14:48 -0700155 const ::std::string logfile2 = base_name2 + ".part0.bfbs";
156 unlink(logfile1.c_str());
Austin Schuh25b46712021-01-03 00:04:38 -0800157 unlink(config1.c_str());
Brian Silverman1f345222020-09-24 21:14:48 -0700158 unlink(logfile2.c_str());
Austin Schuh25b46712021-01-03 00:04:38 -0800159 unlink(config2.c_str());
Brian Silverman1f345222020-09-24 21:14:48 -0700160
161 LOG(INFO) << "Logging data to " << logfile1 << " then " << logfile2;
162
163 {
164 std::unique_ptr<EventLoop> logger_event_loop =
165 event_loop_factory_.MakeEventLoop("logger");
166
167 event_loop_factory_.RunFor(chrono::milliseconds(95));
168
169 Logger logger(logger_event_loop.get());
170 logger.set_polling_period(std::chrono::milliseconds(100));
milind1f1dca32021-07-03 13:50:07 -0700171 logger_event_loop->OnRun([base_name1, base_name2, &logger_event_loop,
172 &logger]() {
173 logger.StartLogging(std::make_unique<LocalLogNamer>(
174 base_name1, logger_event_loop.get(), logger_event_loop->node()));
175 EXPECT_DEATH(
Austin Schuh5b728b72021-06-16 14:57:15 -0700176 logger.StartLogging(std::make_unique<LocalLogNamer>(
milind1f1dca32021-07-03 13:50:07 -0700177 base_name2, logger_event_loop.get(), logger_event_loop->node())),
178 "Already logging");
179 });
Brian Silverman1f345222020-09-24 21:14:48 -0700180 event_loop_factory_.RunFor(chrono::milliseconds(20000));
181 }
182}
183
184// Tests calling StopLogging twice.
185TEST_F(LoggerDeathTest, ExtraStop) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800186 const ::std::string tmpdir = aos::testing::TestTmpDir();
Brian Silverman1f345222020-09-24 21:14:48 -0700187 const ::std::string base_name = tmpdir + "/logfile";
Austin Schuh25b46712021-01-03 00:04:38 -0800188 const ::std::string config =
Austin Schuhee4713b2021-03-21 19:25:17 -0700189 absl::StrCat(base_name, kSingleConfigSha256, ".bfbs");
Brian Silverman1f345222020-09-24 21:14:48 -0700190 const ::std::string logfile = base_name + ".part0.bfbs";
191 // Remove it.
Austin Schuh25b46712021-01-03 00:04:38 -0800192 unlink(config.c_str());
Brian Silverman1f345222020-09-24 21:14:48 -0700193 unlink(logfile.c_str());
194
195 LOG(INFO) << "Logging data to " << logfile;
196
197 {
198 std::unique_ptr<EventLoop> logger_event_loop =
199 event_loop_factory_.MakeEventLoop("logger");
200
201 event_loop_factory_.RunFor(chrono::milliseconds(95));
202
203 Logger logger(logger_event_loop.get());
Austin Schuh8c399962020-12-25 21:51:45 -0800204 logger.set_separate_config(false);
Brian Silverman1f345222020-09-24 21:14:48 -0700205 logger.set_polling_period(std::chrono::milliseconds(100));
206 logger_event_loop->OnRun([base_name, &logger_event_loop, &logger]() {
Austin Schuh5b728b72021-06-16 14:57:15 -0700207 logger.StartLogging(std::make_unique<LocalLogNamer>(
208 base_name, logger_event_loop.get(), logger_event_loop->node()));
Brian Silverman1f345222020-09-24 21:14:48 -0700209 logger.StopLogging(aos::monotonic_clock::min_time);
210 EXPECT_DEATH(logger.StopLogging(aos::monotonic_clock::min_time),
211 "Not logging right now");
212 });
213 event_loop_factory_.RunFor(chrono::milliseconds(20000));
214 }
215}
216
217// Tests that we can startup twice.
218TEST_F(LoggerTest, StartsTwice) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800219 const ::std::string tmpdir = aos::testing::TestTmpDir();
Brian Silverman1f345222020-09-24 21:14:48 -0700220 const ::std::string base_name1 = tmpdir + "/logfile1";
Austin Schuh25b46712021-01-03 00:04:38 -0800221 const ::std::string config1 =
Austin Schuhee4713b2021-03-21 19:25:17 -0700222 absl::StrCat(base_name1, kSingleConfigSha256, ".bfbs");
Brian Silverman1f345222020-09-24 21:14:48 -0700223 const ::std::string logfile1 = base_name1 + ".part0.bfbs";
224 const ::std::string base_name2 = tmpdir + "/logfile2";
Austin Schuh25b46712021-01-03 00:04:38 -0800225 const ::std::string config2 =
Austin Schuhee4713b2021-03-21 19:25:17 -0700226 absl::StrCat(base_name2, kSingleConfigSha256, ".bfbs");
Brian Silverman1f345222020-09-24 21:14:48 -0700227 const ::std::string logfile2 = base_name2 + ".part0.bfbs";
228 unlink(logfile1.c_str());
Austin Schuh25b46712021-01-03 00:04:38 -0800229 unlink(config1.c_str());
Brian Silverman1f345222020-09-24 21:14:48 -0700230 unlink(logfile2.c_str());
Austin Schuh25b46712021-01-03 00:04:38 -0800231 unlink(config2.c_str());
Brian Silverman1f345222020-09-24 21:14:48 -0700232
233 LOG(INFO) << "Logging data to " << logfile1 << " then " << logfile2;
234
235 {
236 std::unique_ptr<EventLoop> logger_event_loop =
237 event_loop_factory_.MakeEventLoop("logger");
238
239 event_loop_factory_.RunFor(chrono::milliseconds(95));
240
241 Logger logger(logger_event_loop.get());
Austin Schuh8c399962020-12-25 21:51:45 -0800242 logger.set_separate_config(false);
Brian Silverman1f345222020-09-24 21:14:48 -0700243 logger.set_polling_period(std::chrono::milliseconds(100));
Austin Schuh5b728b72021-06-16 14:57:15 -0700244 logger.StartLogging(std::make_unique<LocalLogNamer>(
245 base_name1, logger_event_loop.get(), logger_event_loop->node()));
Brian Silverman1f345222020-09-24 21:14:48 -0700246 event_loop_factory_.RunFor(chrono::milliseconds(10000));
247 logger.StopLogging(logger_event_loop->monotonic_now());
248 event_loop_factory_.RunFor(chrono::milliseconds(10000));
Austin Schuh5b728b72021-06-16 14:57:15 -0700249 logger.StartLogging(std::make_unique<LocalLogNamer>(
250 base_name2, logger_event_loop.get(), logger_event_loop->node()));
Brian Silverman1f345222020-09-24 21:14:48 -0700251 event_loop_factory_.RunFor(chrono::milliseconds(10000));
252 }
253
254 for (const auto &logfile :
255 {std::make_tuple(logfile1, 10), std::make_tuple(logfile2, 2010)}) {
256 SCOPED_TRACE(std::get<0>(logfile));
257 LogReader reader(std::get<0>(logfile));
258 reader.Register();
259
Austin Schuh07676622021-01-21 18:59:17 -0800260 EXPECT_THAT(reader.LoggedNodes(), ::testing::ElementsAre(nullptr));
Brian Silverman1f345222020-09-24 21:14:48 -0700261
262 std::unique_ptr<EventLoop> test_event_loop =
263 reader.event_loop_factory()->MakeEventLoop("log_reader");
264
265 int ping_count = std::get<1>(logfile);
266 int pong_count = std::get<1>(logfile);
267
268 // Confirm that the ping and pong counts both match, and the value also
269 // matches.
270 test_event_loop->MakeWatcher("/test",
271 [&ping_count](const examples::Ping &ping) {
272 EXPECT_EQ(ping.value(), ping_count + 1);
273 ++ping_count;
274 });
275 test_event_loop->MakeWatcher(
276 "/test", [&pong_count, &ping_count](const examples::Pong &pong) {
277 EXPECT_EQ(pong.value(), pong_count + 1);
278 ++pong_count;
279 EXPECT_EQ(ping_count, pong_count);
280 });
281
282 reader.event_loop_factory()->RunFor(std::chrono::seconds(100));
283 EXPECT_EQ(ping_count, std::get<1>(logfile) + 1000);
284 }
285}
286
Austin Schuhfa895892020-01-07 20:07:41 -0800287// Tests that we can read and write rotated log files.
288TEST_F(LoggerTest, RotatedLogFile) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800289 const ::std::string tmpdir = aos::testing::TestTmpDir();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700290 const ::std::string base_name = tmpdir + "/logfile";
Austin Schuh25b46712021-01-03 00:04:38 -0800291 const ::std::string config =
Austin Schuhee4713b2021-03-21 19:25:17 -0700292 absl::StrCat(base_name, kSingleConfigSha256, ".bfbs");
Austin Schuh2f8fd752020-09-01 22:38:28 -0700293 const ::std::string logfile0 = base_name + ".part0.bfbs";
294 const ::std::string logfile1 = base_name + ".part1.bfbs";
Austin Schuhfa895892020-01-07 20:07:41 -0800295 // Remove it.
Austin Schuh25b46712021-01-03 00:04:38 -0800296 unlink(config.c_str());
Austin Schuhfa895892020-01-07 20:07:41 -0800297 unlink(logfile0.c_str());
298 unlink(logfile1.c_str());
299
300 LOG(INFO) << "Logging data to " << logfile0 << " and " << logfile1;
301
302 {
Austin Schuhfa895892020-01-07 20:07:41 -0800303 std::unique_ptr<EventLoop> logger_event_loop =
304 event_loop_factory_.MakeEventLoop("logger");
305
306 event_loop_factory_.RunFor(chrono::milliseconds(95));
307
Brian Silverman1f345222020-09-24 21:14:48 -0700308 Logger logger(logger_event_loop.get());
Austin Schuh8c399962020-12-25 21:51:45 -0800309 logger.set_separate_config(false);
Brian Silverman1f345222020-09-24 21:14:48 -0700310 logger.set_polling_period(std::chrono::milliseconds(100));
311 logger.StartLoggingLocalNamerOnRun(base_name);
Austin Schuhfa895892020-01-07 20:07:41 -0800312 event_loop_factory_.RunFor(chrono::milliseconds(10000));
Austin Schuh2f8fd752020-09-01 22:38:28 -0700313 logger.Rotate();
Austin Schuhfa895892020-01-07 20:07:41 -0800314 event_loop_factory_.RunFor(chrono::milliseconds(10000));
315 }
316
Austin Schuh64fab802020-09-09 22:47:47 -0700317 {
318 // Confirm that the UUIDs match for both the parts and the logger, and the
319 // parts_index increments.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800320 std::vector<SizePrefixedFlatbufferVector<LogFileHeader>> log_header;
Austin Schuh64fab802020-09-09 22:47:47 -0700321 for (std::string_view f : {logfile0, logfile1}) {
Austin Schuh3bd4c402020-11-06 18:19:06 -0800322 log_header.emplace_back(ReadHeader(f).value());
Austin Schuh64fab802020-09-09 22:47:47 -0700323 }
324
Brian Silvermanae7c0332020-09-30 16:58:23 -0700325 EXPECT_EQ(log_header[0].message().log_event_uuid()->string_view(),
326 log_header[1].message().log_event_uuid()->string_view());
Austin Schuh64fab802020-09-09 22:47:47 -0700327 EXPECT_EQ(log_header[0].message().parts_uuid()->string_view(),
328 log_header[1].message().parts_uuid()->string_view());
329
330 EXPECT_EQ(log_header[0].message().parts_index(), 0);
331 EXPECT_EQ(log_header[1].message().parts_index(), 1);
332 }
333
Austin Schuhfa895892020-01-07 20:07:41 -0800334 // Even though it doesn't make any difference here, exercise the logic for
335 // passing in a separate config.
Austin Schuh287d43d2020-12-04 20:19:33 -0800336 LogReader reader(SortParts({logfile0, logfile1}), &config_.message());
Austin Schuhfa895892020-01-07 20:07:41 -0800337
338 // Confirm that we can remap logged channels to point to new buses.
339 reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original");
340
341 // This sends out the fetched messages and advances time to the start of the
342 // log file.
343 reader.Register();
344
Austin Schuh07676622021-01-21 18:59:17 -0800345 EXPECT_THAT(reader.LoggedNodes(), ::testing::ElementsAre(nullptr));
Austin Schuhfa895892020-01-07 20:07:41 -0800346
347 std::unique_ptr<EventLoop> test_event_loop =
348 reader.event_loop_factory()->MakeEventLoop("log_reader");
349
350 int ping_count = 10;
351 int pong_count = 10;
352
353 // Confirm that the ping value matches in the remapped channel location.
354 test_event_loop->MakeWatcher("/original/test",
355 [&ping_count](const examples::Ping &ping) {
356 EXPECT_EQ(ping.value(), ping_count + 1);
357 ++ping_count;
358 });
359 // Confirm that the ping and pong counts both match, and the value also
360 // matches.
361 test_event_loop->MakeWatcher(
362 "/test", [&pong_count, &ping_count](const examples::Pong &pong) {
363 EXPECT_EQ(pong.value(), pong_count + 1);
364 ++pong_count;
365 EXPECT_EQ(ping_count, pong_count);
366 });
367
368 reader.event_loop_factory()->RunFor(std::chrono::seconds(100));
369 EXPECT_EQ(ping_count, 2010);
370}
371
Austin Schuh4c4e0092019-12-22 16:18:03 -0800372// Tests that a large number of messages per second doesn't overwhelm writev.
373TEST_F(LoggerTest, ManyMessages) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800374 const ::std::string tmpdir = aos::testing::TestTmpDir();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700375 const ::std::string base_name = tmpdir + "/logfile";
Austin Schuh25b46712021-01-03 00:04:38 -0800376 const ::std::string config =
Austin Schuhee4713b2021-03-21 19:25:17 -0700377 absl::StrCat(base_name, kSingleConfigSha256, ".bfbs");
Austin Schuh2f8fd752020-09-01 22:38:28 -0700378 const ::std::string logfile = base_name + ".part0.bfbs";
Austin Schuh4c4e0092019-12-22 16:18:03 -0800379 // Remove the log file.
Austin Schuh25b46712021-01-03 00:04:38 -0800380 unlink(config.c_str());
Austin Schuh4c4e0092019-12-22 16:18:03 -0800381 unlink(logfile.c_str());
382
383 LOG(INFO) << "Logging data to " << logfile;
Austin Schuh4c3b9702020-08-30 11:34:55 -0700384 ping_.set_quiet(true);
Austin Schuh4c4e0092019-12-22 16:18:03 -0800385
386 {
Austin Schuh4c4e0092019-12-22 16:18:03 -0800387 std::unique_ptr<EventLoop> logger_event_loop =
388 event_loop_factory_.MakeEventLoop("logger");
389
390 std::unique_ptr<EventLoop> ping_spammer_event_loop =
391 event_loop_factory_.MakeEventLoop("ping_spammer");
392 aos::Sender<examples::Ping> ping_sender =
393 ping_spammer_event_loop->MakeSender<examples::Ping>("/test");
394
395 aos::TimerHandler *timer_handler =
396 ping_spammer_event_loop->AddTimer([&ping_sender]() {
397 aos::Sender<examples::Ping>::Builder builder =
398 ping_sender.MakeBuilder();
399 examples::Ping::Builder ping_builder =
400 builder.MakeBuilder<examples::Ping>();
Austin Schuhbfe6c572022-01-27 20:48:20 -0800401 CHECK_EQ(builder.Send(ping_builder.Finish()), RawSender::Error::kOk);
Austin Schuh4c4e0092019-12-22 16:18:03 -0800402 });
403
404 // 100 ms / 0.05 ms -> 2000 messages. Should be enough to crash it.
405 ping_spammer_event_loop->OnRun([&ping_spammer_event_loop, timer_handler]() {
406 timer_handler->Setup(ping_spammer_event_loop->monotonic_now(),
407 chrono::microseconds(50));
408 });
409
Brian Silverman1f345222020-09-24 21:14:48 -0700410 Logger logger(logger_event_loop.get());
Austin Schuh8c399962020-12-25 21:51:45 -0800411 logger.set_separate_config(false);
Brian Silverman1f345222020-09-24 21:14:48 -0700412 logger.set_polling_period(std::chrono::milliseconds(100));
413 logger.StartLoggingLocalNamerOnRun(base_name);
Austin Schuh4c4e0092019-12-22 16:18:03 -0800414
415 event_loop_factory_.RunFor(chrono::milliseconds(1000));
416 }
417}
418
James Kuszmaul890c2492022-04-06 14:59:31 -0700419// Tests that we can read a logfile that has channels which were sent too fast.
420TEST(SingleNodeLoggerNoFixtureTest, ReadTooFast) {
421 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
422 aos::configuration::ReadConfig(
423 ArtifactPath("aos/events/pingpong_config.json"));
424 SimulatedEventLoopFactory event_loop_factory(&config.message());
425 const ::std::string tmpdir = aos::testing::TestTmpDir();
426 const ::std::string base_name = tmpdir + "/logfile";
427 const ::std::string config_file =
428 absl::StrCat(base_name, kSingleConfigSha256, ".bfbs");
429 const ::std::string logfile = base_name + ".part0.bfbs";
430 // Remove the log file.
431 unlink(config_file.c_str());
432 unlink(logfile.c_str());
433
434 LOG(INFO) << "Logging data to " << logfile;
435
436 int sent_messages = 0;
437
438 {
439 std::unique_ptr<EventLoop> logger_event_loop =
440 event_loop_factory.MakeEventLoop("logger");
441
442 std::unique_ptr<EventLoop> ping_spammer_event_loop =
443 event_loop_factory.GetNodeEventLoopFactory(nullptr)->MakeEventLoop(
444 "ping_spammer", {NodeEventLoopFactory::CheckSentTooFast::kNo,
445 NodeEventLoopFactory::ExclusiveSenders::kNo});
446 aos::Sender<examples::Ping> ping_sender =
447 ping_spammer_event_loop->MakeSender<examples::Ping>("/test");
448
449 aos::TimerHandler *timer_handler =
450 ping_spammer_event_loop->AddTimer([&ping_sender, &sent_messages]() {
451 aos::Sender<examples::Ping>::Builder builder =
452 ping_sender.MakeBuilder();
453 examples::Ping::Builder ping_builder =
454 builder.MakeBuilder<examples::Ping>();
455 CHECK_EQ(builder.Send(ping_builder.Finish()), RawSender::Error::kOk);
456 ++sent_messages;
457 });
458
459 constexpr std::chrono::microseconds kSendPeriod{10};
460 const int max_legal_messages =
461 ping_sender.channel()->frequency() *
462 event_loop_factory.configuration()->channel_storage_duration() /
463 1000000000;
464
465 ping_spammer_event_loop->OnRun(
466 [&ping_spammer_event_loop, kSendPeriod, timer_handler]() {
467 timer_handler->Setup(
468 ping_spammer_event_loop->monotonic_now() + kSendPeriod / 2,
469 kSendPeriod);
470 });
471
472 Logger logger(logger_event_loop.get());
473 logger.set_separate_config(false);
474 logger.set_polling_period(std::chrono::milliseconds(100));
475 logger.StartLoggingLocalNamerOnRun(base_name);
476
477 event_loop_factory.RunFor(kSendPeriod * max_legal_messages * 2);
478 }
479
480 LogReader reader(logfile);
481
482 reader.Register();
483
484 std::unique_ptr<EventLoop> test_event_loop =
485 reader.event_loop_factory()->MakeEventLoop("log_reader");
486
487 int replay_count = 0;
488
489 test_event_loop->MakeWatcher(
490 "/test", [&replay_count](const examples::Ping &) { ++replay_count; });
491
492 reader.event_loop_factory()->Run();
493 EXPECT_EQ(replay_count, sent_messages);
494}
495
James Kuszmauldd0a5042021-10-28 23:38:04 -0700496struct CompressionParams {
497 std::string_view extension;
498 std::function<std::unique_ptr<DetachedBufferEncoder>()> encoder_factory;
499};
500
501std::ostream &operator<<(std::ostream &ostream,
502 const CompressionParams &params) {
503 ostream << "\"" << params.extension << "\"";
504 return ostream;
505}
506
507std::vector<CompressionParams> SupportedCompressionAlgorithms() {
508 return {{"", []() { return std::make_unique<DummyEncoder>(); }},
509 {SnappyDecoder::kExtension,
510 []() { return std::make_unique<SnappyEncoder>(); }},
511#ifdef LZMA
512 {LzmaDecoder::kExtension,
513 []() { return std::make_unique<LzmaEncoder>(3); }}
514#endif // LZMA
515 };
516}
517
Austin Schuh61e973f2021-02-21 21:43:56 -0800518// Parameters to run all the tests with.
James Kuszmauldd0a5042021-10-28 23:38:04 -0700519struct ConfigParams {
Austin Schuh61e973f2021-02-21 21:43:56 -0800520 // The config file to use.
521 std::string config;
522 // If true, the RemoteMessage channel should be shared between all the remote
523 // channels. If false, there will be 1 RemoteMessage channel per remote
524 // channel.
525 bool shared;
526 // sha256 of the config.
Austin Schuhcdd90272021-03-15 12:46:16 -0700527 std::string_view sha256;
Austin Schuh61e973f2021-02-21 21:43:56 -0800528};
Austin Schuh315b96b2020-12-11 21:21:12 -0800529
James Kuszmauldd0a5042021-10-28 23:38:04 -0700530std::ostream &operator<<(std::ostream &ostream, const ConfigParams &params) {
531 ostream << "{config: \"" << params.config << "\", shared: " << params.shared
532 << ", sha256: \"" << params.sha256 << "\"}";
533 return ostream;
534}
535
Austin Schuh3e20c692021-11-16 20:43:16 -0800536struct LoggerState {
537 static LoggerState MakeLogger(NodeEventLoopFactory *node,
538 SimulatedEventLoopFactory *factory,
539 CompressionParams params,
540 const Configuration *configuration = nullptr) {
541 if (configuration == nullptr) {
542 configuration = factory->configuration();
543 }
544 return {node->MakeEventLoop("logger"),
545 {},
546 configuration,
547 configuration::GetNode(configuration, node->node()),
548 nullptr,
549 params};
550 }
551
552 void StartLogger(std::string logfile_base) {
553 CHECK(!logfile_base.empty());
554
555 logger = std::make_unique<Logger>(event_loop.get(), configuration);
556 logger->set_polling_period(std::chrono::milliseconds(100));
557 logger->set_name(
558 absl::StrCat("name_prefix_", event_loop->node()->name()->str()));
Austin Schuhfa712682022-05-11 16:43:42 -0700559 logger->set_logger_sha1(
560 absl::StrCat("logger_sha1_", event_loop->node()->name()->str()));
561 logger->set_logger_version(
562 absl::StrCat("logger_version_", event_loop->node()->name()->str()));
Austin Schuh3e20c692021-11-16 20:43:16 -0800563 event_loop->OnRun([this, logfile_base]() {
564 std::unique_ptr<MultiNodeLogNamer> namer =
565 std::make_unique<MultiNodeLogNamer>(logfile_base, configuration,
566 event_loop.get(), node);
567 namer->set_extension(params.extension);
568 namer->set_encoder_factory(params.encoder_factory);
569 log_namer = namer.get();
570
571 logger->StartLogging(std::move(namer));
572 });
573 }
574
575 std::unique_ptr<EventLoop> event_loop;
576 std::unique_ptr<Logger> logger;
577 const Configuration *configuration;
578 const Node *node;
579 MultiNodeLogNamer *log_namer;
580 CompressionParams params;
581
582 void AppendAllFilenames(std::vector<std::string> *filenames) {
583 for (const std::string &file : log_namer->all_filenames()) {
584 const std::string_view separator =
585 log_namer->base_name().back() == '/' ? "" : "_";
586 filenames->emplace_back(
587 absl::StrCat(log_namer->base_name(), separator, file));
588 }
589 }
590
591 ~LoggerState() {
592 if (logger) {
Austin Schuh01f3b392022-01-25 20:03:09 -0800593 std::vector<std::string> filenames;
594 AppendAllFilenames(&filenames);
Austin Schuhbfe6c572022-01-27 20:48:20 -0800595 std::sort(filenames.begin(), filenames.end());
Austin Schuh01f3b392022-01-25 20:03:09 -0800596 for (const std::string &file : filenames) {
Austin Schuh3e20c692021-11-16 20:43:16 -0800597 LOG(INFO) << "Wrote to " << file;
Austin Schuh01f3b392022-01-25 20:03:09 -0800598 auto x = ReadHeader(file);
599 if (x) {
600 VLOG(1) << aos::FlatbufferToJson(x.value());
601 }
Austin Schuh3e20c692021-11-16 20:43:16 -0800602 }
603 }
604 }
605};
606
Austin Schuhe33c08d2022-02-03 18:15:21 -0800607std::vector<std::pair<std::vector<realtime_clock::time_point>,
608 std::vector<realtime_clock::time_point>>>
609ConfirmReadable(
610 const std::vector<std::string> &files,
611 realtime_clock::time_point start_time = realtime_clock::min_time,
612 realtime_clock::time_point end_time = realtime_clock::max_time) {
Austin Schuh3e20c692021-11-16 20:43:16 -0800613 {
614 LogReader reader(SortParts(files));
615
616 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
617 reader.Register(&log_reader_factory);
618
619 log_reader_factory.Run();
620
621 reader.Deregister();
622 }
623 {
Austin Schuhe33c08d2022-02-03 18:15:21 -0800624 std::vector<std::pair<std::vector<realtime_clock::time_point>,
625 std::vector<realtime_clock::time_point>>>
626 result;
Austin Schuh3e20c692021-11-16 20:43:16 -0800627 LogReader reader(SortParts(files));
628
Austin Schuhe33c08d2022-02-03 18:15:21 -0800629 reader.SetStartTime(start_time);
630 reader.SetEndTime(end_time);
631
Austin Schuh3e20c692021-11-16 20:43:16 -0800632 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
633 reader.RegisterWithoutStarting(&log_reader_factory);
Austin Schuhe33c08d2022-02-03 18:15:21 -0800634 result.resize(
635 configuration::NodesCount(log_reader_factory.configuration()));
Austin Schuh3e20c692021-11-16 20:43:16 -0800636 if (configuration::MultiNode(log_reader_factory.configuration())) {
Austin Schuhe33c08d2022-02-03 18:15:21 -0800637 size_t i = 0;
Austin Schuh3e20c692021-11-16 20:43:16 -0800638 for (const aos::Node *node :
639 *log_reader_factory.configuration()->nodes()) {
Austin Schuhe33c08d2022-02-03 18:15:21 -0800640 LOG(INFO) << "Registering start";
641 reader.OnStart(node, [node, &log_reader_factory, &result,
642 node_index = i]() {
Austin Schuh3e20c692021-11-16 20:43:16 -0800643 LOG(INFO) << "Starting " << node->name()->string_view();
Austin Schuhe33c08d2022-02-03 18:15:21 -0800644 result[node_index].first.push_back(
645 log_reader_factory.GetNodeEventLoopFactory(node)->realtime_now());
Austin Schuh3e20c692021-11-16 20:43:16 -0800646 });
Austin Schuhe33c08d2022-02-03 18:15:21 -0800647 reader.OnEnd(node, [node, &log_reader_factory, &result,
648 node_index = i]() {
649 LOG(INFO) << "Ending " << node->name()->string_view();
650 result[node_index].second.push_back(
651 log_reader_factory.GetNodeEventLoopFactory(node)->realtime_now());
652 });
653 ++i;
Austin Schuh3e20c692021-11-16 20:43:16 -0800654 }
Austin Schuhe33c08d2022-02-03 18:15:21 -0800655 } else {
656 reader.OnStart([&log_reader_factory, &result]() {
657 LOG(INFO) << "Starting";
658 result[0].first.push_back(
659 log_reader_factory.GetNodeEventLoopFactory(nullptr)
660 ->realtime_now());
661 });
662 reader.OnEnd([&log_reader_factory, &result]() {
663 LOG(INFO) << "Ending";
664 result[0].second.push_back(
665 log_reader_factory.GetNodeEventLoopFactory(nullptr)
666 ->realtime_now());
667 });
Austin Schuh3e20c692021-11-16 20:43:16 -0800668 }
669
670 log_reader_factory.Run();
671
672 reader.Deregister();
Austin Schuhe33c08d2022-02-03 18:15:21 -0800673
674 for (auto x : result) {
675 for (auto y : x.first) {
676 VLOG(1) << "Start " << y;
677 }
678 for (auto y : x.second) {
679 VLOG(1) << "End " << y;
680 }
681 }
682 return result;
Austin Schuh3e20c692021-11-16 20:43:16 -0800683 }
684}
685
James Kuszmauldd0a5042021-10-28 23:38:04 -0700686class MultinodeLoggerTest : public ::testing::TestWithParam<
687 std::tuple<ConfigParams, CompressionParams>> {
Austin Schuh15649d62019-12-28 16:36:38 -0800688 public:
689 MultinodeLoggerTest()
James Kuszmauldd0a5042021-10-28 23:38:04 -0700690 : config_(aos::configuration::ReadConfig(ArtifactPath(absl::StrCat(
691 "aos/events/logging/", std::get<0>(GetParam()).config)))),
Austin Schuh87dd3832021-01-01 23:07:31 -0800692 time_converter_(configuration::NodesCount(&config_.message())),
Austin Schuhac0771c2020-01-07 18:36:30 -0800693 event_loop_factory_(&config_.message()),
Austin Schuh58646e22021-08-23 23:51:46 -0700694 pi1_(event_loop_factory_.GetNodeEventLoopFactory("pi1")),
Austin Schuh87dd3832021-01-01 23:07:31 -0800695 pi1_index_(configuration::GetNodeIndex(
Austin Schuh58646e22021-08-23 23:51:46 -0700696 event_loop_factory_.configuration(), pi1_->node())),
697 pi2_(event_loop_factory_.GetNodeEventLoopFactory("pi2")),
Austin Schuh87dd3832021-01-01 23:07:31 -0800698 pi2_index_(configuration::GetNodeIndex(
Austin Schuh58646e22021-08-23 23:51:46 -0700699 event_loop_factory_.configuration(), pi2_->node())),
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800700 tmp_dir_(aos::testing::TestTmpDir()),
Austin Schuh8c399962020-12-25 21:51:45 -0800701 logfile_base1_(tmp_dir_ + "/multi_logfile1"),
702 logfile_base2_(tmp_dir_ + "/multi_logfile2"),
Austin Schuh61e973f2021-02-21 21:43:56 -0800703 pi1_reboot_logfiles_(MakePi1RebootLogfiles()),
Austin Schuh8c399962020-12-25 21:51:45 -0800704 logfiles_(MakeLogFiles(logfile_base1_, logfile_base2_)),
Austin Schuh61e973f2021-02-21 21:43:56 -0800705 pi1_single_direction_logfiles_(MakePi1SingleDirectionLogfiles()),
Austin Schuh58646e22021-08-23 23:51:46 -0700706 structured_logfiles_(StructureLogFiles()) {
James Kuszmauldd0a5042021-10-28 23:38:04 -0700707 LOG(INFO) << "Config " << std::get<0>(GetParam()).config;
Austin Schuh87dd3832021-01-01 23:07:31 -0800708 event_loop_factory_.SetTimeConverter(&time_converter_);
709
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700710 // Go through and remove the logfiles if they already exist.
Austin Schuh268586b2021-03-31 22:24:39 -0700711 for (const auto &file : logfiles_) {
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700712 unlink(file.c_str());
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800713 unlink((file + ".xz").c_str());
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700714 }
715
Austin Schuh268586b2021-03-31 22:24:39 -0700716 for (const auto &file :
Austin Schuh25b46712021-01-03 00:04:38 -0800717 MakeLogFiles(tmp_dir_ + "/relogged1", tmp_dir_ + "/relogged2")) {
Austin Schuh315b96b2020-12-11 21:21:12 -0800718 unlink(file.c_str());
719 }
720
Austin Schuh268586b2021-03-31 22:24:39 -0700721 for (const auto &file : pi1_reboot_logfiles_) {
Austin Schuh315b96b2020-12-11 21:21:12 -0800722 unlink(file.c_str());
723 }
724
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700725 LOG(INFO) << "Logging data to " << logfiles_[0] << ", " << logfiles_[1]
726 << " and " << logfiles_[2];
Austin Schuh58646e22021-08-23 23:51:46 -0700727
728 pi1_->OnStartup([this]() { pi1_->AlwaysStart<Ping>("ping"); });
729 pi2_->OnStartup([this]() { pi2_->AlwaysStart<Pong>("pong"); });
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700730 }
731
James Kuszmauldd0a5042021-10-28 23:38:04 -0700732 bool shared() const { return std::get<0>(GetParam()).shared; }
Austin Schuh61e973f2021-02-21 21:43:56 -0800733
734 std::vector<std::string> MakeLogFiles(std::string logfile_base1,
Austin Schuhe46492f2021-07-31 19:49:41 -0700735 std::string logfile_base2,
Austin Schuhbfe6c572022-01-27 20:48:20 -0800736 size_t pi1_data_count = 3,
737 size_t pi2_data_count = 3) {
Austin Schuh61e973f2021-02-21 21:43:56 -0800738 std::vector<std::string> result;
James Kuszmauldd0a5042021-10-28 23:38:04 -0700739 result.emplace_back(absl::StrCat(
740 logfile_base1, "_", std::get<0>(GetParam()).sha256, Extension()));
741 result.emplace_back(absl::StrCat(
742 logfile_base2, "_", std::get<0>(GetParam()).sha256, Extension()));
Austin Schuhe46492f2021-07-31 19:49:41 -0700743 for (size_t i = 0; i < pi1_data_count; ++i) {
744 result.emplace_back(
James Kuszmauldd0a5042021-10-28 23:38:04 -0700745 absl::StrCat(logfile_base1, "_pi1_data.part", i, Extension()));
Austin Schuhe46492f2021-07-31 19:49:41 -0700746 }
Austin Schuh61e973f2021-02-21 21:43:56 -0800747 result.emplace_back(logfile_base1 +
James Kuszmauldd0a5042021-10-28 23:38:04 -0700748 "_pi2_data/test/aos.examples.Pong.part0" + Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800749 result.emplace_back(logfile_base1 +
James Kuszmauldd0a5042021-10-28 23:38:04 -0700750 "_pi2_data/test/aos.examples.Pong.part1" + Extension());
Austin Schuhe46492f2021-07-31 19:49:41 -0700751 for (size_t i = 0; i < pi2_data_count; ++i) {
752 result.emplace_back(
James Kuszmauldd0a5042021-10-28 23:38:04 -0700753 absl::StrCat(logfile_base2, "_pi2_data.part", i, Extension()));
Austin Schuhe46492f2021-07-31 19:49:41 -0700754 }
James Kuszmauldd0a5042021-10-28 23:38:04 -0700755 result.emplace_back(logfile_base2 +
756 "_pi1_data/pi1/aos/aos.message_bridge.Timestamp.part0" +
757 Extension());
758 result.emplace_back(logfile_base2 +
759 "_pi1_data/pi1/aos/aos.message_bridge.Timestamp.part1" +
760 Extension());
761 result.emplace_back(logfile_base1 +
762 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part0" +
763 Extension());
764 result.emplace_back(logfile_base1 +
765 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part1" +
766 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800767 if (shared()) {
768 result.emplace_back(logfile_base1 +
769 "_timestamps/pi1/aos/remote_timestamps/pi2/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700770 "aos.message_bridge.RemoteMessage.part0" +
771 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800772 result.emplace_back(logfile_base1 +
773 "_timestamps/pi1/aos/remote_timestamps/pi2/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700774 "aos.message_bridge.RemoteMessage.part1" +
775 Extension());
Austin Schuhbfe6c572022-01-27 20:48:20 -0800776 result.emplace_back(logfile_base1 +
777 "_timestamps/pi1/aos/remote_timestamps/pi2/"
778 "aos.message_bridge.RemoteMessage.part2" +
779 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800780 result.emplace_back(logfile_base2 +
781 "_timestamps/pi2/aos/remote_timestamps/pi1/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700782 "aos.message_bridge.RemoteMessage.part0" +
783 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800784 result.emplace_back(logfile_base2 +
785 "_timestamps/pi2/aos/remote_timestamps/pi1/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700786 "aos.message_bridge.RemoteMessage.part1" +
787 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800788 } else {
789 result.emplace_back(logfile_base1 +
790 "_timestamps/pi1/aos/remote_timestamps/pi2/pi1/aos/"
791 "aos-message_bridge-Timestamp/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700792 "aos.message_bridge.RemoteMessage.part0" +
793 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800794 result.emplace_back(logfile_base1 +
795 "_timestamps/pi1/aos/remote_timestamps/pi2/pi1/aos/"
796 "aos-message_bridge-Timestamp/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700797 "aos.message_bridge.RemoteMessage.part1" +
798 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800799 result.emplace_back(logfile_base2 +
800 "_timestamps/pi2/aos/remote_timestamps/pi1/pi2/aos/"
801 "aos-message_bridge-Timestamp/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700802 "aos.message_bridge.RemoteMessage.part0" +
803 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800804 result.emplace_back(logfile_base2 +
805 "_timestamps/pi2/aos/remote_timestamps/pi1/pi2/aos/"
806 "aos-message_bridge-Timestamp/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700807 "aos.message_bridge.RemoteMessage.part1" +
808 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800809 result.emplace_back(logfile_base1 +
810 "_timestamps/pi1/aos/remote_timestamps/pi2/test/"
811 "aos-examples-Ping/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700812 "aos.message_bridge.RemoteMessage.part0" +
813 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800814 result.emplace_back(logfile_base1 +
815 "_timestamps/pi1/aos/remote_timestamps/pi2/test/"
816 "aos-examples-Ping/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700817 "aos.message_bridge.RemoteMessage.part1" +
818 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800819 }
820
821 return result;
822 }
823
824 std::vector<std::string> MakePi1RebootLogfiles() {
825 std::vector<std::string> result;
James Kuszmauldd0a5042021-10-28 23:38:04 -0700826 result.emplace_back(logfile_base1_ + "_pi1_data.part0" + Extension());
827 result.emplace_back(logfile_base1_ + "_pi1_data.part1" + Extension());
828 result.emplace_back(logfile_base1_ + "_pi1_data.part2" + Extension());
Austin Schuhbfe6c572022-01-27 20:48:20 -0800829 result.emplace_back(logfile_base1_ + "_pi1_data.part3" + Extension());
830 result.emplace_back(logfile_base1_ + "_pi1_data.part4" + Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800831 result.emplace_back(logfile_base1_ +
James Kuszmauldd0a5042021-10-28 23:38:04 -0700832 "_pi2_data/test/aos.examples.Pong.part0" + Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800833 result.emplace_back(logfile_base1_ +
James Kuszmauldd0a5042021-10-28 23:38:04 -0700834 "_pi2_data/test/aos.examples.Pong.part1" + Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800835 result.emplace_back(logfile_base1_ +
James Kuszmauldd0a5042021-10-28 23:38:04 -0700836 "_pi2_data/test/aos.examples.Pong.part2" + Extension());
Austin Schuh58646e22021-08-23 23:51:46 -0700837 result.emplace_back(logfile_base1_ +
James Kuszmauldd0a5042021-10-28 23:38:04 -0700838 "_pi2_data/test/aos.examples.Pong.part3" + Extension());
839 result.emplace_back(logfile_base1_ +
840 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part0" +
841 Extension());
842 result.emplace_back(logfile_base1_ +
843 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part1" +
844 Extension());
845 result.emplace_back(logfile_base1_ +
846 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part2" +
847 Extension());
848 result.emplace_back(logfile_base1_ +
849 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part3" +
850 Extension());
851 result.emplace_back(absl::StrCat(
852 logfile_base1_, "_", std::get<0>(GetParam()).sha256, Extension()));
Austin Schuh61e973f2021-02-21 21:43:56 -0800853 if (shared()) {
Austin Schuhbfe6c572022-01-27 20:48:20 -0800854 for (size_t i = 0; i < 6; ++i) {
855 result.emplace_back(
856 absl::StrCat(logfile_base1_,
857 "_timestamps/pi1/aos/remote_timestamps/pi2/"
858 "aos.message_bridge.RemoteMessage.part",
859 i, Extension()));
860 }
Austin Schuh61e973f2021-02-21 21:43:56 -0800861 } else {
862 result.emplace_back(logfile_base1_ +
863 "_timestamps/pi1/aos/remote_timestamps/pi2/pi1/aos/"
864 "aos-message_bridge-Timestamp/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700865 "aos.message_bridge.RemoteMessage.part0" +
866 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800867 result.emplace_back(logfile_base1_ +
868 "_timestamps/pi1/aos/remote_timestamps/pi2/pi1/aos/"
869 "aos-message_bridge-Timestamp/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700870 "aos.message_bridge.RemoteMessage.part1" +
871 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800872 result.emplace_back(logfile_base1_ +
873 "_timestamps/pi1/aos/remote_timestamps/pi2/pi1/aos/"
874 "aos-message_bridge-Timestamp/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700875 "aos.message_bridge.RemoteMessage.part2" +
876 Extension());
Austin Schuh58646e22021-08-23 23:51:46 -0700877 result.emplace_back(logfile_base1_ +
878 "_timestamps/pi1/aos/remote_timestamps/pi2/pi1/aos/"
879 "aos-message_bridge-Timestamp/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700880 "aos.message_bridge.RemoteMessage.part3" +
881 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800882
883 result.emplace_back(logfile_base1_ +
884 "_timestamps/pi1/aos/remote_timestamps/pi2/test/"
885 "aos-examples-Ping/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700886 "aos.message_bridge.RemoteMessage.part0" +
887 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800888 result.emplace_back(logfile_base1_ +
889 "_timestamps/pi1/aos/remote_timestamps/pi2/test/"
890 "aos-examples-Ping/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700891 "aos.message_bridge.RemoteMessage.part1" +
892 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800893 result.emplace_back(logfile_base1_ +
894 "_timestamps/pi1/aos/remote_timestamps/pi2/test/"
895 "aos-examples-Ping/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700896 "aos.message_bridge.RemoteMessage.part2" +
897 Extension());
Austin Schuh58646e22021-08-23 23:51:46 -0700898 result.emplace_back(logfile_base1_ +
899 "_timestamps/pi1/aos/remote_timestamps/pi2/test/"
900 "aos-examples-Ping/"
James Kuszmauldd0a5042021-10-28 23:38:04 -0700901 "aos.message_bridge.RemoteMessage.part3" +
902 Extension());
Austin Schuh61e973f2021-02-21 21:43:56 -0800903 }
904 return result;
905 }
906
907 std::vector<std::string> MakePi1SingleDirectionLogfiles() {
908 std::vector<std::string> result;
James Kuszmauldd0a5042021-10-28 23:38:04 -0700909 result.emplace_back(logfile_base1_ + "_pi1_data.part0" + Extension());
910 result.emplace_back(logfile_base1_ + "_pi1_data.part1" + Extension());
911 result.emplace_back(logfile_base1_ +
912 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part0" +
913 Extension());
914 result.emplace_back(absl::StrCat(
915 logfile_base1_, "_", std::get<0>(GetParam()).sha256, Extension()));
Austin Schuh510dc622021-08-06 18:47:30 -0700916 return result;
917 }
Austin Schuh61e973f2021-02-21 21:43:56 -0800918
Austin Schuh510dc622021-08-06 18:47:30 -0700919 std::vector<std::string> MakePi1DeadNodeLogfiles() {
920 std::vector<std::string> result;
James Kuszmauldd0a5042021-10-28 23:38:04 -0700921 result.emplace_back(logfile_base1_ + "_pi1_data.part0" + Extension());
922 result.emplace_back(absl::StrCat(
923 logfile_base1_, "_", std::get<0>(GetParam()).sha256, Extension()));
Austin Schuh61e973f2021-02-21 21:43:56 -0800924 return result;
925 }
926
927 std::vector<std::vector<std::string>> StructureLogFiles() {
928 std::vector<std::vector<std::string>> result{
Austin Schuhbfe6c572022-01-27 20:48:20 -0800929 std::vector<std::string>{logfiles_[2], logfiles_[3], logfiles_[4]},
930 std::vector<std::string>{logfiles_[5], logfiles_[6]},
931 std::vector<std::string>{logfiles_[7], logfiles_[8], logfiles_[9]},
Austin Schuh61e973f2021-02-21 21:43:56 -0800932 std::vector<std::string>{logfiles_[10], logfiles_[11]},
Austin Schuhbfe6c572022-01-27 20:48:20 -0800933 std::vector<std::string>{logfiles_[12], logfiles_[13]}};
Austin Schuh61e973f2021-02-21 21:43:56 -0800934
Austin Schuhbfe6c572022-01-27 20:48:20 -0800935 if (shared()) {
936 result.emplace_back(std::vector<std::string>{logfiles_[14], logfiles_[15],
937 logfiles_[16]});
938 result.emplace_back(
939 std::vector<std::string>{logfiles_[17], logfiles_[18]});
940 } else {
941 result.emplace_back(
942 std::vector<std::string>{logfiles_[14], logfiles_[15]});
Austin Schuh61e973f2021-02-21 21:43:56 -0800943 result.emplace_back(
Austin Schuhe46492f2021-07-31 19:49:41 -0700944 std::vector<std::string>{logfiles_[16], logfiles_[17]});
Austin Schuhbfe6c572022-01-27 20:48:20 -0800945 result.emplace_back(
946 std::vector<std::string>{logfiles_[18], logfiles_[19]});
Austin Schuh61e973f2021-02-21 21:43:56 -0800947 }
948
949 return result;
950 }
951
James Kuszmauldd0a5042021-10-28 23:38:04 -0700952 std::string Extension() {
953 return absl::StrCat(".bfbs", std::get<1>(GetParam()).extension);
954 }
955
Austin Schuh58646e22021-08-23 23:51:46 -0700956 LoggerState MakeLogger(NodeEventLoopFactory *node,
Austin Schuh5b728b72021-06-16 14:57:15 -0700957 SimulatedEventLoopFactory *factory = nullptr,
958 const Configuration *configuration = nullptr) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700959 if (factory == nullptr) {
960 factory = &event_loop_factory_;
961 }
Austin Schuh3e20c692021-11-16 20:43:16 -0800962 return LoggerState::MakeLogger(node, factory, std::get<1>(GetParam()),
963 configuration);
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700964 }
965
James Kuszmauldd0a5042021-10-28 23:38:04 -0700966 void StartLogger(LoggerState *logger, std::string logfile_base = "") {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700967 if (logfile_base.empty()) {
Austin Schuh8c399962020-12-25 21:51:45 -0800968 if (logger->event_loop->node()->name()->string_view() == "pi1") {
969 logfile_base = logfile_base1_;
970 } else {
971 logfile_base = logfile_base2_;
972 }
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700973 }
Austin Schuh3e20c692021-11-16 20:43:16 -0800974 logger->StartLogger(logfile_base);
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700975 }
Austin Schuh15649d62019-12-28 16:36:38 -0800976
Austin Schuh3bd4c402020-11-06 18:19:06 -0800977 void VerifyParts(const std::vector<LogFile> &sorted_parts,
978 const std::vector<std::string> &corrupted_parts = {}) {
979 EXPECT_EQ(sorted_parts.size(), 2u);
980
981 // Count up the number of UUIDs and make sure they are what we expect as a
982 // sanity check.
983 std::set<std::string> log_event_uuids;
984 std::set<std::string> parts_uuids;
985 std::set<std::string> both_uuids;
986
987 size_t missing_rt_count = 0;
988
989 std::vector<std::string> logger_nodes;
990 for (const LogFile &log_file : sorted_parts) {
991 EXPECT_FALSE(log_file.log_event_uuid.empty());
992 log_event_uuids.insert(log_file.log_event_uuid);
993 logger_nodes.emplace_back(log_file.logger_node);
994 both_uuids.insert(log_file.log_event_uuid);
Austin Schuh0ca51f32020-12-25 21:51:45 -0800995 EXPECT_TRUE(log_file.config);
996 EXPECT_EQ(log_file.name,
997 absl::StrCat("name_prefix_", log_file.logger_node));
Austin Schuhfa712682022-05-11 16:43:42 -0700998 EXPECT_EQ(log_file.logger_sha1,
999 absl::StrCat("logger_sha1_", log_file.logger_node));
1000 EXPECT_EQ(log_file.logger_version,
1001 absl::StrCat("logger_version_", log_file.logger_node));
Austin Schuh3bd4c402020-11-06 18:19:06 -08001002
1003 for (const LogParts &part : log_file.parts) {
1004 EXPECT_NE(part.monotonic_start_time, aos::monotonic_clock::min_time)
1005 << ": " << part;
1006 missing_rt_count +=
1007 part.realtime_start_time == aos::realtime_clock::min_time;
1008
1009 EXPECT_TRUE(log_event_uuids.find(part.log_event_uuid) !=
1010 log_event_uuids.end());
1011 EXPECT_NE(part.node, "");
Austin Schuh0ca51f32020-12-25 21:51:45 -08001012 EXPECT_TRUE(log_file.config);
Austin Schuh3bd4c402020-11-06 18:19:06 -08001013 parts_uuids.insert(part.parts_uuid);
1014 both_uuids.insert(part.parts_uuid);
1015 }
1016 }
1017
Austin Schuh61e973f2021-02-21 21:43:56 -08001018 // We won't have RT timestamps for 5 or 6 log files. We don't log the RT
1019 // start time on remote nodes because we don't know it and would be
1020 // guessing. And the log reader can actually do a better job. The number
1021 // depends on if we have the remote timestamps split across 2 files, or just
1022 // across 1, depending on if we are using a split or combined timestamp
1023 // channel config.
1024 EXPECT_EQ(missing_rt_count, shared() ? 5u : 6u);
Austin Schuh3bd4c402020-11-06 18:19:06 -08001025
1026 EXPECT_EQ(log_event_uuids.size(), 2u);
1027 EXPECT_EQ(parts_uuids.size(), ToLogReaderVector(sorted_parts).size());
1028 EXPECT_EQ(log_event_uuids.size() + parts_uuids.size(), both_uuids.size());
1029
1030 // Test that each list of parts is in order. Don't worry about the ordering
1031 // between part file lists though.
1032 // (inner vectors all need to be in order, but outer one doesn't matter).
Austin Schuhbfe6c572022-01-27 20:48:20 -08001033 ASSERT_THAT(ToLogReaderVector(sorted_parts),
Austin Schuh3bd4c402020-11-06 18:19:06 -08001034 ::testing::UnorderedElementsAreArray(structured_logfiles_));
1035
1036 EXPECT_THAT(logger_nodes, ::testing::UnorderedElementsAre("pi1", "pi2"));
1037
1038 EXPECT_NE(sorted_parts[0].realtime_start_time,
1039 aos::realtime_clock::min_time);
1040 EXPECT_NE(sorted_parts[1].realtime_start_time,
1041 aos::realtime_clock::min_time);
1042
1043 EXPECT_NE(sorted_parts[0].monotonic_start_time,
1044 aos::monotonic_clock::min_time);
1045 EXPECT_NE(sorted_parts[1].monotonic_start_time,
1046 aos::monotonic_clock::min_time);
1047
1048 EXPECT_THAT(sorted_parts[0].corrupted, ::testing::Eq(corrupted_parts));
1049 EXPECT_THAT(sorted_parts[1].corrupted, ::testing::Eq(corrupted_parts));
1050 }
1051
1052 void AddExtension(std::string_view extension) {
1053 std::transform(logfiles_.begin(), logfiles_.end(), logfiles_.begin(),
1054 [extension](const std::string &in) {
1055 return absl::StrCat(in, extension);
1056 });
1057
1058 std::transform(structured_logfiles_.begin(), structured_logfiles_.end(),
1059 structured_logfiles_.begin(),
1060 [extension](std::vector<std::string> in) {
1061 std::transform(in.begin(), in.end(), in.begin(),
1062 [extension](const std::string &in_str) {
1063 return absl::StrCat(in_str, extension);
1064 });
1065 return in;
1066 });
1067 }
1068
Austin Schuh15649d62019-12-28 16:36:38 -08001069 // Config and factory.
1070 aos::FlatbufferDetachedBuffer<aos::Configuration> config_;
Austin Schuh87dd3832021-01-01 23:07:31 -08001071 message_bridge::TestingTimeConverter time_converter_;
Austin Schuh15649d62019-12-28 16:36:38 -08001072 SimulatedEventLoopFactory event_loop_factory_;
1073
Austin Schuh58646e22021-08-23 23:51:46 -07001074 NodeEventLoopFactory *const pi1_;
Austin Schuh87dd3832021-01-01 23:07:31 -08001075 const size_t pi1_index_;
Austin Schuh58646e22021-08-23 23:51:46 -07001076 NodeEventLoopFactory *const pi2_;
Austin Schuh87dd3832021-01-01 23:07:31 -08001077 const size_t pi2_index_;
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001078
1079 std::string tmp_dir_;
Austin Schuh8c399962020-12-25 21:51:45 -08001080 std::string logfile_base1_;
1081 std::string logfile_base2_;
Austin Schuh315b96b2020-12-11 21:21:12 -08001082 std::vector<std::string> pi1_reboot_logfiles_;
Austin Schuh2f8fd752020-09-01 22:38:28 -07001083 std::vector<std::string> logfiles_;
Austin Schuhc9049732020-12-21 22:27:15 -08001084 std::vector<std::string> pi1_single_direction_logfiles_;
Austin Schuh2f8fd752020-09-01 22:38:28 -07001085
1086 std::vector<std::vector<std::string>> structured_logfiles_;
Austin Schuh15649d62019-12-28 16:36:38 -08001087};
1088
Austin Schuh391e3172020-09-01 22:48:18 -07001089// Counts the number of messages on a channel. Returns (channel name, channel
1090// type, count) for every message matching matcher()
1091std::vector<std::tuple<std::string, std::string, int>> CountChannelsMatching(
Austin Schuh25b46712021-01-03 00:04:38 -08001092 std::shared_ptr<const aos::Configuration> config, std::string_view filename,
Tyler Chatowb7c6eba2021-07-28 14:43:23 -07001093 std::function<bool(const UnpackedMessageHeader *)> matcher) {
Austin Schuh6f3babe2020-01-26 20:34:50 -08001094 MessageReader message_reader(filename);
Austin Schuh8c399962020-12-25 21:51:45 -08001095 std::vector<int> counts(config->channels()->size(), 0);
Austin Schuh15649d62019-12-28 16:36:38 -08001096
Austin Schuh6f3babe2020-01-26 20:34:50 -08001097 while (true) {
Tyler Chatowb7c6eba2021-07-28 14:43:23 -07001098 std::shared_ptr<UnpackedMessageHeader> msg = message_reader.ReadMessage();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001099 if (!msg) {
1100 break;
1101 }
1102
Tyler Chatowb7c6eba2021-07-28 14:43:23 -07001103 if (matcher(msg.get())) {
1104 counts[msg->channel_index]++;
Austin Schuh6f3babe2020-01-26 20:34:50 -08001105 }
1106 }
1107
Austin Schuh391e3172020-09-01 22:48:18 -07001108 std::vector<std::tuple<std::string, std::string, int>> result;
Austin Schuh6f3babe2020-01-26 20:34:50 -08001109 int channel = 0;
1110 for (size_t i = 0; i < counts.size(); ++i) {
1111 if (counts[i] != 0) {
Austin Schuh8c399962020-12-25 21:51:45 -08001112 const Channel *channel = config->channels()->Get(i);
Austin Schuh391e3172020-09-01 22:48:18 -07001113 result.push_back(std::make_tuple(channel->name()->str(),
1114 channel->type()->str(), counts[i]));
Austin Schuh6f3babe2020-01-26 20:34:50 -08001115 }
1116 ++channel;
1117 }
1118
1119 return result;
1120}
1121
1122// Counts the number of messages (channel, count) for all data messages.
Austin Schuh391e3172020-09-01 22:48:18 -07001123std::vector<std::tuple<std::string, std::string, int>> CountChannelsData(
Austin Schuh8c399962020-12-25 21:51:45 -08001124 std::shared_ptr<const aos::Configuration> config,
Austin Schuh391e3172020-09-01 22:48:18 -07001125 std::string_view filename) {
Tyler Chatowb7c6eba2021-07-28 14:43:23 -07001126 return CountChannelsMatching(
1127 config, filename, [](const UnpackedMessageHeader *msg) {
1128 if (msg->span.data() != nullptr) {
1129 CHECK(!msg->monotonic_remote_time.has_value());
1130 CHECK(!msg->realtime_remote_time.has_value());
1131 CHECK(!msg->remote_queue_index.has_value());
1132 return true;
1133 }
1134 return false;
1135 });
Austin Schuh6f3babe2020-01-26 20:34:50 -08001136}
1137
1138// Counts the number of messages (channel, count) for all timestamp messages.
Austin Schuh391e3172020-09-01 22:48:18 -07001139std::vector<std::tuple<std::string, std::string, int>> CountChannelsTimestamp(
Austin Schuh8c399962020-12-25 21:51:45 -08001140 std::shared_ptr<const aos::Configuration> config,
Austin Schuh6f3babe2020-01-26 20:34:50 -08001141 std::string_view filename) {
Tyler Chatowb7c6eba2021-07-28 14:43:23 -07001142 return CountChannelsMatching(
1143 config, filename, [](const UnpackedMessageHeader *msg) {
1144 if (msg->span.data() == nullptr) {
1145 CHECK(msg->monotonic_remote_time.has_value());
1146 CHECK(msg->realtime_remote_time.has_value());
1147 CHECK(msg->remote_queue_index.has_value());
1148 return true;
1149 }
1150 return false;
1151 });
Austin Schuh6f3babe2020-01-26 20:34:50 -08001152}
1153
Austin Schuhcde938c2020-02-02 17:30:07 -08001154// Tests that we can write and read simple multi-node log files.
Austin Schuh61e973f2021-02-21 21:43:56 -08001155TEST_P(MultinodeLoggerTest, SimpleMultiNode) {
Austin Schuhbfe6c572022-01-27 20:48:20 -08001156 std::vector<std::string> actual_filenames;
Austin Schuh87dd3832021-01-01 23:07:31 -08001157 time_converter_.StartEqual();
Austin Schuhbfe6c572022-01-27 20:48:20 -08001158
Austin Schuh15649d62019-12-28 16:36:38 -08001159 {
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001160 LoggerState pi1_logger = MakeLogger(pi1_);
1161 LoggerState pi2_logger = MakeLogger(pi2_);
Austin Schuh15649d62019-12-28 16:36:38 -08001162
1163 event_loop_factory_.RunFor(chrono::milliseconds(95));
1164
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001165 StartLogger(&pi1_logger);
1166 StartLogger(&pi2_logger);
Austin Schuhcde938c2020-02-02 17:30:07 -08001167
Austin Schuh15649d62019-12-28 16:36:38 -08001168 event_loop_factory_.RunFor(chrono::milliseconds(20000));
Austin Schuhbfe6c572022-01-27 20:48:20 -08001169 pi1_logger.AppendAllFilenames(&actual_filenames);
1170 pi2_logger.AppendAllFilenames(&actual_filenames);
Austin Schuh15649d62019-12-28 16:36:38 -08001171 }
1172
Austin Schuhbfe6c572022-01-27 20:48:20 -08001173 ASSERT_THAT(actual_filenames,
1174 ::testing::UnorderedElementsAreArray(logfiles_));
1175
Austin Schuh6f3babe2020-01-26 20:34:50 -08001176 {
Austin Schuh64fab802020-09-09 22:47:47 -07001177 std::set<std::string> logfile_uuids;
1178 std::set<std::string> parts_uuids;
1179 // Confirm that we have the expected number of UUIDs for both the logfile
1180 // UUIDs and parts UUIDs.
Austin Schuhadd6eb32020-11-09 21:24:26 -08001181 std::vector<SizePrefixedFlatbufferVector<LogFileHeader>> log_header;
Austin Schuh64fab802020-09-09 22:47:47 -07001182 for (std::string_view f : logfiles_) {
Austin Schuh3bd4c402020-11-06 18:19:06 -08001183 log_header.emplace_back(ReadHeader(f).value());
Austin Schuh8c399962020-12-25 21:51:45 -08001184 if (!log_header.back().message().has_configuration()) {
1185 logfile_uuids.insert(
1186 log_header.back().message().log_event_uuid()->str());
1187 parts_uuids.insert(log_header.back().message().parts_uuid()->str());
1188 }
Austin Schuh64fab802020-09-09 22:47:47 -07001189 }
Austin Schuh15649d62019-12-28 16:36:38 -08001190
Austin Schuh64fab802020-09-09 22:47:47 -07001191 EXPECT_EQ(logfile_uuids.size(), 2u);
Austin Schuh61e973f2021-02-21 21:43:56 -08001192 if (shared()) {
1193 EXPECT_EQ(parts_uuids.size(), 7u);
1194 } else {
1195 EXPECT_EQ(parts_uuids.size(), 8u);
1196 }
Austin Schuh64fab802020-09-09 22:47:47 -07001197
1198 // And confirm everything is on the correct node.
Austin Schuh61e973f2021-02-21 21:43:56 -08001199 EXPECT_EQ(log_header[2].message().node()->name()->string_view(), "pi1");
Austin Schuhe46492f2021-07-31 19:49:41 -07001200 EXPECT_EQ(log_header[3].message().node()->name()->string_view(), "pi1");
Austin Schuhbfe6c572022-01-27 20:48:20 -08001201 EXPECT_EQ(log_header[4].message().node()->name()->string_view(), "pi1");
1202
Austin Schuh64fab802020-09-09 22:47:47 -07001203 EXPECT_EQ(log_header[5].message().node()->name()->string_view(), "pi2");
Austin Schuhe46492f2021-07-31 19:49:41 -07001204 EXPECT_EQ(log_header[6].message().node()->name()->string_view(), "pi2");
Austin Schuhbfe6c572022-01-27 20:48:20 -08001205
Austin Schuhe46492f2021-07-31 19:49:41 -07001206 EXPECT_EQ(log_header[7].message().node()->name()->string_view(), "pi2");
Austin Schuhbfe6c572022-01-27 20:48:20 -08001207 EXPECT_EQ(log_header[8].message().node()->name()->string_view(), "pi2");
1208 EXPECT_EQ(log_header[9].message().node()->name()->string_view(), "pi2");
1209
1210 EXPECT_EQ(log_header[10].message().node()->name()->string_view(), "pi1");
1211 EXPECT_EQ(log_header[11].message().node()->name()->string_view(), "pi1");
1212
Austin Schuhe46492f2021-07-31 19:49:41 -07001213 EXPECT_EQ(log_header[12].message().node()->name()->string_view(), "pi2");
1214 EXPECT_EQ(log_header[13].message().node()->name()->string_view(), "pi2");
Austin Schuhbfe6c572022-01-27 20:48:20 -08001215
1216 if (shared()) {
1217 EXPECT_EQ(log_header[14].message().node()->name()->string_view(), "pi2");
1218 EXPECT_EQ(log_header[15].message().node()->name()->string_view(), "pi2");
Austin Schuhe46492f2021-07-31 19:49:41 -07001219 EXPECT_EQ(log_header[16].message().node()->name()->string_view(), "pi2");
Austin Schuhbfe6c572022-01-27 20:48:20 -08001220
1221 EXPECT_EQ(log_header[17].message().node()->name()->string_view(), "pi1");
1222 EXPECT_EQ(log_header[18].message().node()->name()->string_view(), "pi1");
1223 } else {
1224 EXPECT_EQ(log_header[14].message().node()->name()->string_view(), "pi2");
1225 EXPECT_EQ(log_header[15].message().node()->name()->string_view(), "pi2");
1226
1227 EXPECT_EQ(log_header[16].message().node()->name()->string_view(), "pi1");
1228 EXPECT_EQ(log_header[17].message().node()->name()->string_view(), "pi1");
1229
1230 EXPECT_EQ(log_header[18].message().node()->name()->string_view(), "pi2");
1231 EXPECT_EQ(log_header[19].message().node()->name()->string_view(), "pi2");
Austin Schuh61e973f2021-02-21 21:43:56 -08001232 }
Austin Schuh64fab802020-09-09 22:47:47 -07001233
1234 // And the parts index matches.
Austin Schuh61e973f2021-02-21 21:43:56 -08001235 EXPECT_EQ(log_header[2].message().parts_index(), 0);
Austin Schuhe46492f2021-07-31 19:49:41 -07001236 EXPECT_EQ(log_header[3].message().parts_index(), 1);
Austin Schuhbfe6c572022-01-27 20:48:20 -08001237 EXPECT_EQ(log_header[4].message().parts_index(), 2);
1238
1239 EXPECT_EQ(log_header[5].message().parts_index(), 0);
1240 EXPECT_EQ(log_header[6].message().parts_index(), 1);
1241
1242 EXPECT_EQ(log_header[7].message().parts_index(), 0);
1243 EXPECT_EQ(log_header[8].message().parts_index(), 1);
1244 EXPECT_EQ(log_header[9].message().parts_index(), 2);
1245
Austin Schuh64fab802020-09-09 22:47:47 -07001246 EXPECT_EQ(log_header[10].message().parts_index(), 0);
1247 EXPECT_EQ(log_header[11].message().parts_index(), 1);
Austin Schuhbfe6c572022-01-27 20:48:20 -08001248
Austin Schuh61e973f2021-02-21 21:43:56 -08001249 EXPECT_EQ(log_header[12].message().parts_index(), 0);
1250 EXPECT_EQ(log_header[13].message().parts_index(), 1);
Austin Schuhbfe6c572022-01-27 20:48:20 -08001251
1252 if (shared()) {
1253 EXPECT_EQ(log_header[14].message().parts_index(), 0);
1254 EXPECT_EQ(log_header[15].message().parts_index(), 1);
1255 EXPECT_EQ(log_header[16].message().parts_index(), 2);
1256
1257 EXPECT_EQ(log_header[17].message().parts_index(), 0);
1258 EXPECT_EQ(log_header[18].message().parts_index(), 1);
1259 } else {
1260 EXPECT_EQ(log_header[14].message().parts_index(), 0);
1261 EXPECT_EQ(log_header[15].message().parts_index(), 1);
1262
Austin Schuhe46492f2021-07-31 19:49:41 -07001263 EXPECT_EQ(log_header[16].message().parts_index(), 0);
1264 EXPECT_EQ(log_header[17].message().parts_index(), 1);
Austin Schuhbfe6c572022-01-27 20:48:20 -08001265
1266 EXPECT_EQ(log_header[18].message().parts_index(), 0);
1267 EXPECT_EQ(log_header[19].message().parts_index(), 1);
Austin Schuh61e973f2021-02-21 21:43:56 -08001268 }
Austin Schuh64fab802020-09-09 22:47:47 -07001269 }
1270
Austin Schuh8c399962020-12-25 21:51:45 -08001271 const std::vector<LogFile> sorted_log_files = SortParts(logfiles_);
Austin Schuh64fab802020-09-09 22:47:47 -07001272 {
Austin Schuh391e3172020-09-01 22:48:18 -07001273 using ::testing::UnorderedElementsAre;
Austin Schuh8c399962020-12-25 21:51:45 -08001274 std::shared_ptr<const aos::Configuration> config =
1275 sorted_log_files[0].config;
Austin Schuh391e3172020-09-01 22:48:18 -07001276
Austin Schuh6f3babe2020-01-26 20:34:50 -08001277 // Timing reports, pings
Austin Schuh3e20c692021-11-16 20:43:16 -08001278 EXPECT_THAT(CountChannelsData(config, logfiles_[2]),
1279 UnorderedElementsAre(
1280 std::make_tuple("/pi1/aos",
1281 "aos.message_bridge.ServerStatistics", 1),
1282 std::make_tuple("/test", "aos.examples.Ping", 1)))
Austin Schuhe46492f2021-07-31 19:49:41 -07001283 << " : " << logfiles_[2];
1284 EXPECT_THAT(
1285 CountChannelsData(config, logfiles_[3]),
1286 UnorderedElementsAre(
Austin Schuhbfe6c572022-01-27 20:48:20 -08001287 std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 1),
1288 std::make_tuple("/pi1/aos", "aos.message_bridge.ClientStatistics",
1289 1)))
1290 << " : " << logfiles_[3];
1291 EXPECT_THAT(
1292 CountChannelsData(config, logfiles_[4]),
1293 UnorderedElementsAre(
1294 std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 199),
James Kuszmaul4f106fb2021-01-05 20:53:02 -08001295 std::make_tuple("/pi1/aos", "aos.message_bridge.ServerStatistics",
Austin Schuhe46492f2021-07-31 19:49:41 -07001296 20),
James Kuszmaul4f106fb2021-01-05 20:53:02 -08001297 std::make_tuple("/pi1/aos", "aos.message_bridge.ClientStatistics",
Austin Schuhbfe6c572022-01-27 20:48:20 -08001298 199),
Austin Schuh2f8fd752020-09-01 22:38:28 -07001299 std::make_tuple("/pi1/aos", "aos.timing.Report", 40),
Austin Schuhe46492f2021-07-31 19:49:41 -07001300 std::make_tuple("/test", "aos.examples.Ping", 2000)))
Austin Schuhbfe6c572022-01-27 20:48:20 -08001301 << " : " << logfiles_[4];
Austin Schuh6f3babe2020-01-26 20:34:50 -08001302 // Timestamps for pong
Austin Schuhe46492f2021-07-31 19:49:41 -07001303 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[2]),
1304 UnorderedElementsAre())
1305 << " : " << logfiles_[2];
Austin Schuh2f8fd752020-09-01 22:38:28 -07001306 EXPECT_THAT(
Austin Schuhe46492f2021-07-31 19:49:41 -07001307 CountChannelsTimestamp(config, logfiles_[3]),
Austin Schuhbfe6c572022-01-27 20:48:20 -08001308 UnorderedElementsAre(std::make_tuple("/test", "aos.examples.Pong", 1)))
Austin Schuhe46492f2021-07-31 19:49:41 -07001309 << " : " << logfiles_[3];
Austin Schuhbfe6c572022-01-27 20:48:20 -08001310 EXPECT_THAT(
1311 CountChannelsTimestamp(config, logfiles_[4]),
1312 UnorderedElementsAre(
1313 std::make_tuple("/test", "aos.examples.Pong", 2000),
1314 std::make_tuple("/pi2/aos", "aos.message_bridge.Timestamp", 200)))
1315 << " : " << logfiles_[4];
Austin Schuh6f3babe2020-01-26 20:34:50 -08001316
1317 // Pong data.
Austin Schuh20ac95d2020-12-05 17:24:19 -08001318 EXPECT_THAT(
Austin Schuhbfe6c572022-01-27 20:48:20 -08001319 CountChannelsData(config, logfiles_[5]),
Austin Schuh20ac95d2020-12-05 17:24:19 -08001320 UnorderedElementsAre(std::make_tuple("/test", "aos.examples.Pong", 91)))
Austin Schuhbfe6c572022-01-27 20:48:20 -08001321 << " : " << logfiles_[5];
1322 EXPECT_THAT(CountChannelsData(config, logfiles_[6]),
Austin Schuh2f8fd752020-09-01 22:38:28 -07001323 UnorderedElementsAre(
Austin Schuh20ac95d2020-12-05 17:24:19 -08001324 std::make_tuple("/test", "aos.examples.Pong", 1910)))
Austin Schuhbfe6c572022-01-27 20:48:20 -08001325 << " : " << logfiles_[6];
Austin Schuh391e3172020-09-01 22:48:18 -07001326
Austin Schuh6f3babe2020-01-26 20:34:50 -08001327 // No timestamps
Austin Schuhe46492f2021-07-31 19:49:41 -07001328 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[5]),
1329 UnorderedElementsAre())
1330 << " : " << logfiles_[5];
Austin Schuhbfe6c572022-01-27 20:48:20 -08001331 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[6]),
1332 UnorderedElementsAre())
1333 << " : " << logfiles_[6];
Austin Schuh6f3babe2020-01-26 20:34:50 -08001334
1335 // Timing reports and pongs.
Austin Schuhbfe6c572022-01-27 20:48:20 -08001336 EXPECT_THAT(CountChannelsData(config, logfiles_[7]),
Austin Schuhe46492f2021-07-31 19:49:41 -07001337 UnorderedElementsAre(std::make_tuple(
1338 "/pi2/aos", "aos.message_bridge.ServerStatistics", 1)))
Austin Schuhbfe6c572022-01-27 20:48:20 -08001339 << " : " << logfiles_[7];
Austin Schuh2f8fd752020-09-01 22:38:28 -07001340 EXPECT_THAT(
Austin Schuhbfe6c572022-01-27 20:48:20 -08001341 CountChannelsData(config, logfiles_[8]),
1342 UnorderedElementsAre(std::make_tuple("/test", "aos.examples.Pong", 1)))
1343 << " : " << logfiles_[8];
1344 EXPECT_THAT(
1345 CountChannelsData(config, logfiles_[9]),
Austin Schuh2f8fd752020-09-01 22:38:28 -07001346 UnorderedElementsAre(
1347 std::make_tuple("/pi2/aos", "aos.message_bridge.Timestamp", 200),
James Kuszmaul4f106fb2021-01-05 20:53:02 -08001348 std::make_tuple("/pi2/aos", "aos.message_bridge.ServerStatistics",
Austin Schuhe46492f2021-07-31 19:49:41 -07001349 20),
James Kuszmaul4f106fb2021-01-05 20:53:02 -08001350 std::make_tuple("/pi2/aos", "aos.message_bridge.ClientStatistics",
1351 200),
Austin Schuh2f8fd752020-09-01 22:38:28 -07001352 std::make_tuple("/pi2/aos", "aos.timing.Report", 40),
Austin Schuhbfe6c572022-01-27 20:48:20 -08001353 std::make_tuple("/test", "aos.examples.Pong", 2000)))
1354 << " : " << logfiles_[9];
Austin Schuh61e973f2021-02-21 21:43:56 -08001355 // And ping timestamps.
Austin Schuhbfe6c572022-01-27 20:48:20 -08001356 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[7]),
Austin Schuh61e973f2021-02-21 21:43:56 -08001357 UnorderedElementsAre())
Austin Schuh61e973f2021-02-21 21:43:56 -08001358 << " : " << logfiles_[7];
Austin Schuhbfe6c572022-01-27 20:48:20 -08001359 EXPECT_THAT(
1360 CountChannelsTimestamp(config, logfiles_[8]),
1361 UnorderedElementsAre(std::make_tuple("/test", "aos.examples.Ping", 1)))
1362 << " : " << logfiles_[8];
1363 EXPECT_THAT(
1364 CountChannelsTimestamp(config, logfiles_[9]),
1365 UnorderedElementsAre(
1366 std::make_tuple("/test", "aos.examples.Ping", 2000),
1367 std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 200)))
1368 << " : " << logfiles_[9];
Austin Schuhe46492f2021-07-31 19:49:41 -07001369
1370 // And then test that the remotely logged timestamp data files only have
1371 // timestamps in them.
Austin Schuhe46492f2021-07-31 19:49:41 -07001372 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[10]),
1373 UnorderedElementsAre())
1374 << " : " << logfiles_[10];
1375 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[11]),
1376 UnorderedElementsAre())
1377 << " : " << logfiles_[11];
Austin Schuhbfe6c572022-01-27 20:48:20 -08001378 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[12]),
1379 UnorderedElementsAre())
1380 << " : " << logfiles_[12];
1381 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[13]),
1382 UnorderedElementsAre())
1383 << " : " << logfiles_[13];
Austin Schuh2f8fd752020-09-01 22:38:28 -07001384
Austin Schuh8c399962020-12-25 21:51:45 -08001385 EXPECT_THAT(CountChannelsData(config, logfiles_[10]),
Austin Schuhe46492f2021-07-31 19:49:41 -07001386 UnorderedElementsAre(std::make_tuple(
Austin Schuhbfe6c572022-01-27 20:48:20 -08001387 "/pi1/aos", "aos.message_bridge.Timestamp", 9)))
Austin Schuh20ac95d2020-12-05 17:24:19 -08001388 << " : " << logfiles_[10];
Austin Schuh8c399962020-12-25 21:51:45 -08001389 EXPECT_THAT(CountChannelsData(config, logfiles_[11]),
Austin Schuhe46492f2021-07-31 19:49:41 -07001390 UnorderedElementsAre(std::make_tuple(
Austin Schuhbfe6c572022-01-27 20:48:20 -08001391 "/pi1/aos", "aos.message_bridge.Timestamp", 191)))
Austin Schuh20ac95d2020-12-05 17:24:19 -08001392 << " : " << logfiles_[11];
Austin Schuhe46492f2021-07-31 19:49:41 -07001393
Austin Schuh61e973f2021-02-21 21:43:56 -08001394 EXPECT_THAT(CountChannelsData(config, logfiles_[12]),
Austin Schuhbfe6c572022-01-27 20:48:20 -08001395 UnorderedElementsAre(std::make_tuple(
1396 "/pi2/aos", "aos.message_bridge.Timestamp", 9)))
Austin Schuh61e973f2021-02-21 21:43:56 -08001397 << " : " << logfiles_[12];
1398 EXPECT_THAT(CountChannelsData(config, logfiles_[13]),
Austin Schuhbfe6c572022-01-27 20:48:20 -08001399 UnorderedElementsAre(std::make_tuple(
1400 "/pi2/aos", "aos.message_bridge.Timestamp", 191)))
Austin Schuh61e973f2021-02-21 21:43:56 -08001401 << " : " << logfiles_[13];
Austin Schuhbfe6c572022-01-27 20:48:20 -08001402
1403 // Timestamps from pi2 on pi1, and the other way.
1404 if (shared()) {
1405 EXPECT_THAT(CountChannelsData(config, logfiles_[14]),
1406 UnorderedElementsAre())
1407 << " : " << logfiles_[14];
1408 EXPECT_THAT(CountChannelsData(config, logfiles_[15]),
1409 UnorderedElementsAre())
1410 << " : " << logfiles_[15];
Austin Schuhe46492f2021-07-31 19:49:41 -07001411 EXPECT_THAT(CountChannelsData(config, logfiles_[16]),
Austin Schuh61e973f2021-02-21 21:43:56 -08001412 UnorderedElementsAre())
Austin Schuhe46492f2021-07-31 19:49:41 -07001413 << " : " << logfiles_[16];
1414 EXPECT_THAT(CountChannelsData(config, logfiles_[17]),
Austin Schuh61e973f2021-02-21 21:43:56 -08001415 UnorderedElementsAre())
Austin Schuhe46492f2021-07-31 19:49:41 -07001416 << " : " << logfiles_[17];
Austin Schuhbfe6c572022-01-27 20:48:20 -08001417 EXPECT_THAT(CountChannelsData(config, logfiles_[18]),
1418 UnorderedElementsAre())
1419 << " : " << logfiles_[18];
Austin Schuh61e973f2021-02-21 21:43:56 -08001420
Austin Schuhbfe6c572022-01-27 20:48:20 -08001421 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[14]),
1422 UnorderedElementsAre(
1423 std::make_tuple("/test", "aos.examples.Ping", 1)))
1424 << " : " << logfiles_[14];
Austin Schuh61e973f2021-02-21 21:43:56 -08001425 EXPECT_THAT(
Austin Schuhbfe6c572022-01-27 20:48:20 -08001426 CountChannelsTimestamp(config, logfiles_[15]),
Austin Schuh61e973f2021-02-21 21:43:56 -08001427 UnorderedElementsAre(
1428 std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 9),
Austin Schuhbfe6c572022-01-27 20:48:20 -08001429 std::make_tuple("/test", "aos.examples.Ping", 90)))
1430 << " : " << logfiles_[15];
Austin Schuh61e973f2021-02-21 21:43:56 -08001431 EXPECT_THAT(
Austin Schuhbfe6c572022-01-27 20:48:20 -08001432 CountChannelsTimestamp(config, logfiles_[16]),
Austin Schuh61e973f2021-02-21 21:43:56 -08001433 UnorderedElementsAre(
1434 std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 191),
1435 std::make_tuple("/test", "aos.examples.Ping", 1910)))
Austin Schuhe46492f2021-07-31 19:49:41 -07001436 << " : " << logfiles_[16];
1437 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[17]),
Austin Schuhbfe6c572022-01-27 20:48:20 -08001438 UnorderedElementsAre(std::make_tuple(
1439 "/pi2/aos", "aos.message_bridge.Timestamp", 9)))
1440 << " : " << logfiles_[17];
1441 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[18]),
1442 UnorderedElementsAre(std::make_tuple(
1443 "/pi2/aos", "aos.message_bridge.Timestamp", 191)))
1444 << " : " << logfiles_[18];
1445 } else {
1446 EXPECT_THAT(CountChannelsData(config, logfiles_[14]),
1447 UnorderedElementsAre())
1448 << " : " << logfiles_[14];
1449 EXPECT_THAT(CountChannelsData(config, logfiles_[15]),
1450 UnorderedElementsAre())
1451 << " : " << logfiles_[15];
1452 EXPECT_THAT(CountChannelsData(config, logfiles_[16]),
1453 UnorderedElementsAre())
1454 << " : " << logfiles_[16];
1455 EXPECT_THAT(CountChannelsData(config, logfiles_[17]),
1456 UnorderedElementsAre())
1457 << " : " << logfiles_[17];
1458 EXPECT_THAT(CountChannelsData(config, logfiles_[18]),
1459 UnorderedElementsAre())
1460 << " : " << logfiles_[18];
1461 EXPECT_THAT(CountChannelsData(config, logfiles_[19]),
1462 UnorderedElementsAre())
1463 << " : " << logfiles_[19];
1464
1465 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[14]),
1466 UnorderedElementsAre(std::make_tuple(
1467 "/pi1/aos", "aos.message_bridge.Timestamp", 9)))
1468 << " : " << logfiles_[14];
1469 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[15]),
1470 UnorderedElementsAre(std::make_tuple(
1471 "/pi1/aos", "aos.message_bridge.Timestamp", 191)))
1472 << " : " << logfiles_[15];
1473 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[16]),
1474 UnorderedElementsAre(std::make_tuple(
1475 "/pi2/aos", "aos.message_bridge.Timestamp", 9)))
1476 << " : " << logfiles_[16];
1477 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[17]),
1478 UnorderedElementsAre(std::make_tuple(
1479 "/pi2/aos", "aos.message_bridge.Timestamp", 191)))
1480 << " : " << logfiles_[17];
1481 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[18]),
1482 UnorderedElementsAre(
1483 std::make_tuple("/test", "aos.examples.Ping", 91)))
1484 << " : " << logfiles_[18];
1485 EXPECT_THAT(CountChannelsTimestamp(config, logfiles_[19]),
Austin Schuh61e973f2021-02-21 21:43:56 -08001486 UnorderedElementsAre(
1487 std::make_tuple("/test", "aos.examples.Ping", 1910)))
Austin Schuhbfe6c572022-01-27 20:48:20 -08001488 << " : " << logfiles_[19];
Austin Schuh61e973f2021-02-21 21:43:56 -08001489 }
Austin Schuh6f3babe2020-01-26 20:34:50 -08001490 }
1491
Austin Schuh8c399962020-12-25 21:51:45 -08001492 LogReader reader(sorted_log_files);
Austin Schuh6f3babe2020-01-26 20:34:50 -08001493
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001494 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
Austin Schuh6331ef92020-01-07 18:28:09 -08001495 log_reader_factory.set_send_delay(chrono::microseconds(0));
Austin Schuh15649d62019-12-28 16:36:38 -08001496
1497 // This sends out the fetched messages and advances time to the start of the
1498 // log file.
Austin Schuh6331ef92020-01-07 18:28:09 -08001499 reader.Register(&log_reader_factory);
James Kuszmaul84ff3e52020-01-03 19:48:53 -08001500
Austin Schuhac0771c2020-01-07 18:36:30 -08001501 const Node *pi1 =
1502 configuration::GetNode(log_reader_factory.configuration(), "pi1");
Austin Schuh6f3babe2020-01-26 20:34:50 -08001503 const Node *pi2 =
1504 configuration::GetNode(log_reader_factory.configuration(), "pi2");
Austin Schuhac0771c2020-01-07 18:36:30 -08001505
Austin Schuh2f8fd752020-09-01 22:38:28 -07001506 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi1) << " pi1";
1507 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi2) << " pi2";
1508 LOG(INFO) << "now pi1 "
1509 << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now();
1510 LOG(INFO) << "now pi2 "
1511 << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now();
1512
Austin Schuh07676622021-01-21 18:59:17 -08001513 EXPECT_THAT(reader.LoggedNodes(),
1514 ::testing::ElementsAre(
1515 configuration::GetNode(reader.logged_configuration(), pi1),
1516 configuration::GetNode(reader.logged_configuration(), pi2)));
James Kuszmaul84ff3e52020-01-03 19:48:53 -08001517
1518 reader.event_loop_factory()->set_send_delay(chrono::microseconds(0));
Austin Schuh15649d62019-12-28 16:36:38 -08001519
Austin Schuh6f3babe2020-01-26 20:34:50 -08001520 std::unique_ptr<EventLoop> pi1_event_loop =
Austin Schuhac0771c2020-01-07 18:36:30 -08001521 log_reader_factory.MakeEventLoop("test", pi1);
Austin Schuh6f3babe2020-01-26 20:34:50 -08001522 std::unique_ptr<EventLoop> pi2_event_loop =
1523 log_reader_factory.MakeEventLoop("test", pi2);
Austin Schuh15649d62019-12-28 16:36:38 -08001524
Austin Schuh6f3babe2020-01-26 20:34:50 -08001525 int pi1_ping_count = 10;
1526 int pi2_ping_count = 10;
1527 int pi1_pong_count = 10;
1528 int pi2_pong_count = 10;
Austin Schuh15649d62019-12-28 16:36:38 -08001529
1530 // Confirm that the ping value matches.
Austin Schuh6f3babe2020-01-26 20:34:50 -08001531 pi1_event_loop->MakeWatcher(
1532 "/test", [&pi1_ping_count, &pi1_event_loop](const examples::Ping &ping) {
Austin Schuh2f8fd752020-09-01 22:38:28 -07001533 VLOG(1) << "Pi1 ping " << FlatbufferToJson(&ping) << " at "
Austin Schuh6f3babe2020-01-26 20:34:50 -08001534 << pi1_event_loop->context().monotonic_remote_time << " -> "
1535 << pi1_event_loop->context().monotonic_event_time;
1536 EXPECT_EQ(ping.value(), pi1_ping_count + 1);
1537 EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time,
1538 pi1_ping_count * chrono::milliseconds(10) +
1539 monotonic_clock::epoch());
1540 EXPECT_EQ(pi1_event_loop->context().realtime_remote_time,
1541 pi1_ping_count * chrono::milliseconds(10) +
1542 realtime_clock::epoch());
1543 EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time,
1544 pi1_event_loop->context().monotonic_event_time);
1545 EXPECT_EQ(pi1_event_loop->context().realtime_remote_time,
1546 pi1_event_loop->context().realtime_event_time);
Austin Schuh15649d62019-12-28 16:36:38 -08001547
Austin Schuh6f3babe2020-01-26 20:34:50 -08001548 ++pi1_ping_count;
1549 });
1550 pi2_event_loop->MakeWatcher(
1551 "/test", [&pi2_ping_count, &pi2_event_loop](const examples::Ping &ping) {
Austin Schuh2f8fd752020-09-01 22:38:28 -07001552 VLOG(1) << "Pi2 ping " << FlatbufferToJson(&ping) << " at "
Austin Schuh6f3babe2020-01-26 20:34:50 -08001553 << pi2_event_loop->context().monotonic_remote_time << " -> "
1554 << pi2_event_loop->context().monotonic_event_time;
1555 EXPECT_EQ(ping.value(), pi2_ping_count + 1);
1556
1557 EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time,
1558 pi2_ping_count * chrono::milliseconds(10) +
1559 monotonic_clock::epoch());
1560 EXPECT_EQ(pi2_event_loop->context().realtime_remote_time,
1561 pi2_ping_count * chrono::milliseconds(10) +
1562 realtime_clock::epoch());
1563 EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time +
1564 chrono::microseconds(150),
1565 pi2_event_loop->context().monotonic_event_time);
1566 EXPECT_EQ(pi2_event_loop->context().realtime_remote_time +
1567 chrono::microseconds(150),
1568 pi2_event_loop->context().realtime_event_time);
1569 ++pi2_ping_count;
Austin Schuh15649d62019-12-28 16:36:38 -08001570 });
1571
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001572 constexpr ssize_t kQueueIndexOffset = -9;
Austin Schuh6f3babe2020-01-26 20:34:50 -08001573 // Confirm that the ping and pong counts both match, and the value also
1574 // matches.
1575 pi1_event_loop->MakeWatcher(
1576 "/test", [&pi1_event_loop, &pi1_ping_count,
1577 &pi1_pong_count](const examples::Pong &pong) {
1578 VLOG(1) << "Pi1 pong " << FlatbufferToJson(&pong) << " at "
1579 << pi1_event_loop->context().monotonic_remote_time << " -> "
1580 << pi1_event_loop->context().monotonic_event_time;
1581
1582 EXPECT_EQ(pi1_event_loop->context().remote_queue_index,
1583 pi1_pong_count + kQueueIndexOffset);
1584 EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time,
1585 chrono::microseconds(200) +
1586 pi1_pong_count * chrono::milliseconds(10) +
1587 monotonic_clock::epoch());
1588 EXPECT_EQ(pi1_event_loop->context().realtime_remote_time,
1589 chrono::microseconds(200) +
1590 pi1_pong_count * chrono::milliseconds(10) +
1591 realtime_clock::epoch());
1592
1593 EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time +
1594 chrono::microseconds(150),
1595 pi1_event_loop->context().monotonic_event_time);
1596 EXPECT_EQ(pi1_event_loop->context().realtime_remote_time +
1597 chrono::microseconds(150),
1598 pi1_event_loop->context().realtime_event_time);
1599
1600 EXPECT_EQ(pong.value(), pi1_pong_count + 1);
1601 ++pi1_pong_count;
1602 EXPECT_EQ(pi1_ping_count, pi1_pong_count);
1603 });
1604 pi2_event_loop->MakeWatcher(
1605 "/test", [&pi2_event_loop, &pi2_ping_count,
1606 &pi2_pong_count](const examples::Pong &pong) {
1607 VLOG(1) << "Pi2 pong " << FlatbufferToJson(&pong) << " at "
1608 << pi2_event_loop->context().monotonic_remote_time << " -> "
1609 << pi2_event_loop->context().monotonic_event_time;
1610
1611 EXPECT_EQ(pi2_event_loop->context().remote_queue_index,
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001612 pi2_pong_count + kQueueIndexOffset);
Austin Schuh6f3babe2020-01-26 20:34:50 -08001613
1614 EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time,
1615 chrono::microseconds(200) +
1616 pi2_pong_count * chrono::milliseconds(10) +
1617 monotonic_clock::epoch());
1618 EXPECT_EQ(pi2_event_loop->context().realtime_remote_time,
1619 chrono::microseconds(200) +
1620 pi2_pong_count * chrono::milliseconds(10) +
1621 realtime_clock::epoch());
1622
1623 EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time,
1624 pi2_event_loop->context().monotonic_event_time);
1625 EXPECT_EQ(pi2_event_loop->context().realtime_remote_time,
1626 pi2_event_loop->context().realtime_event_time);
1627
1628 EXPECT_EQ(pong.value(), pi2_pong_count + 1);
1629 ++pi2_pong_count;
1630 EXPECT_EQ(pi2_ping_count, pi2_pong_count);
1631 });
1632
1633 log_reader_factory.Run();
1634 EXPECT_EQ(pi1_ping_count, 2010);
1635 EXPECT_EQ(pi2_ping_count, 2010);
1636 EXPECT_EQ(pi1_pong_count, 2010);
1637 EXPECT_EQ(pi2_pong_count, 2010);
Austin Schuh6331ef92020-01-07 18:28:09 -08001638
1639 reader.Deregister();
Austin Schuh15649d62019-12-28 16:36:38 -08001640}
1641
James Kuszmaul46d82582020-05-09 19:50:09 -07001642typedef MultinodeLoggerTest MultinodeLoggerDeathTest;
1643
1644// Test that if we feed the replay with a mismatched node list that we die on
1645// the LogReader constructor.
Austin Schuh61e973f2021-02-21 21:43:56 -08001646TEST_P(MultinodeLoggerDeathTest, MultiNodeBadReplayConfig) {
Austin Schuh87dd3832021-01-01 23:07:31 -08001647 time_converter_.StartEqual();
James Kuszmaul46d82582020-05-09 19:50:09 -07001648 {
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001649 LoggerState pi1_logger = MakeLogger(pi1_);
1650 LoggerState pi2_logger = MakeLogger(pi2_);
James Kuszmaul46d82582020-05-09 19:50:09 -07001651
1652 event_loop_factory_.RunFor(chrono::milliseconds(95));
1653
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001654 StartLogger(&pi1_logger);
1655 StartLogger(&pi2_logger);
James Kuszmaul46d82582020-05-09 19:50:09 -07001656
James Kuszmaul46d82582020-05-09 19:50:09 -07001657 event_loop_factory_.RunFor(chrono::milliseconds(20000));
1658 }
1659
1660 // Test that, if we add an additional node to the replay config that the
1661 // logger complains about the mismatch in number of nodes.
1662 FlatbufferDetachedBuffer<Configuration> extra_nodes_config =
1663 configuration::MergeWithConfig(&config_.message(), R"({
1664 "nodes": [
1665 {
1666 "name": "extra-node"
1667 }
1668 ]
1669 }
1670 )");
1671
Austin Schuh287d43d2020-12-04 20:19:33 -08001672 const std::vector<LogFile> sorted_parts = SortParts(logfiles_);
1673 EXPECT_DEATH(LogReader(sorted_parts, &extra_nodes_config.message()),
James Kuszmaul46d82582020-05-09 19:50:09 -07001674 "Log file and replay config need to have matching nodes lists.");
James Kuszmaul46d82582020-05-09 19:50:09 -07001675}
1676
Austin Schuhcde938c2020-02-02 17:30:07 -08001677// Tests that we can read log files where they don't start at the same monotonic
1678// time.
Austin Schuh61e973f2021-02-21 21:43:56 -08001679TEST_P(MultinodeLoggerTest, StaggeredStart) {
Austin Schuh87dd3832021-01-01 23:07:31 -08001680 time_converter_.StartEqual();
Austin Schuhbfe6c572022-01-27 20:48:20 -08001681 std::vector<std::string> actual_filenames;
1682
Austin Schuhcde938c2020-02-02 17:30:07 -08001683 {
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001684 LoggerState pi1_logger = MakeLogger(pi1_);
1685 LoggerState pi2_logger = MakeLogger(pi2_);
Austin Schuhcde938c2020-02-02 17:30:07 -08001686
1687 event_loop_factory_.RunFor(chrono::milliseconds(95));
1688
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001689 StartLogger(&pi1_logger);
Austin Schuhcde938c2020-02-02 17:30:07 -08001690
1691 event_loop_factory_.RunFor(chrono::milliseconds(200));
1692
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001693 StartLogger(&pi2_logger);
1694
Austin Schuhcde938c2020-02-02 17:30:07 -08001695 event_loop_factory_.RunFor(chrono::milliseconds(20000));
Austin Schuhbfe6c572022-01-27 20:48:20 -08001696 pi1_logger.AppendAllFilenames(&actual_filenames);
1697 pi2_logger.AppendAllFilenames(&actual_filenames);
Austin Schuhcde938c2020-02-02 17:30:07 -08001698 }
1699
Austin Schuhe46492f2021-07-31 19:49:41 -07001700 // Since we delay starting pi2, it already knows about all the timestamps so
1701 // we don't end up with extra parts.
Austin Schuhbfe6c572022-01-27 20:48:20 -08001702 LogReader reader(SortParts(actual_filenames));
Austin Schuhcde938c2020-02-02 17:30:07 -08001703
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001704 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
Austin Schuhcde938c2020-02-02 17:30:07 -08001705 log_reader_factory.set_send_delay(chrono::microseconds(0));
1706
1707 // This sends out the fetched messages and advances time to the start of the
1708 // log file.
1709 reader.Register(&log_reader_factory);
1710
1711 const Node *pi1 =
1712 configuration::GetNode(log_reader_factory.configuration(), "pi1");
1713 const Node *pi2 =
1714 configuration::GetNode(log_reader_factory.configuration(), "pi2");
1715
Austin Schuh07676622021-01-21 18:59:17 -08001716 EXPECT_THAT(reader.LoggedNodes(),
1717 ::testing::ElementsAre(
1718 configuration::GetNode(reader.logged_configuration(), pi1),
1719 configuration::GetNode(reader.logged_configuration(), pi2)));
Austin Schuhcde938c2020-02-02 17:30:07 -08001720
1721 reader.event_loop_factory()->set_send_delay(chrono::microseconds(0));
1722
1723 std::unique_ptr<EventLoop> pi1_event_loop =
1724 log_reader_factory.MakeEventLoop("test", pi1);
1725 std::unique_ptr<EventLoop> pi2_event_loop =
1726 log_reader_factory.MakeEventLoop("test", pi2);
1727
1728 int pi1_ping_count = 30;
1729 int pi2_ping_count = 30;
1730 int pi1_pong_count = 30;
1731 int pi2_pong_count = 30;
1732
1733 // Confirm that the ping value matches.
1734 pi1_event_loop->MakeWatcher(
1735 "/test", [&pi1_ping_count, &pi1_event_loop](const examples::Ping &ping) {
1736 VLOG(1) << "Pi1 ping " << FlatbufferToJson(&ping)
1737 << pi1_event_loop->context().monotonic_remote_time << " -> "
1738 << pi1_event_loop->context().monotonic_event_time;
1739 EXPECT_EQ(ping.value(), pi1_ping_count + 1);
1740
1741 ++pi1_ping_count;
1742 });
1743 pi2_event_loop->MakeWatcher(
1744 "/test", [&pi2_ping_count, &pi2_event_loop](const examples::Ping &ping) {
1745 VLOG(1) << "Pi2 ping " << FlatbufferToJson(&ping)
1746 << pi2_event_loop->context().monotonic_remote_time << " -> "
1747 << pi2_event_loop->context().monotonic_event_time;
1748 EXPECT_EQ(ping.value(), pi2_ping_count + 1);
1749
1750 ++pi2_ping_count;
1751 });
1752
1753 // Confirm that the ping and pong counts both match, and the value also
1754 // matches.
1755 pi1_event_loop->MakeWatcher(
1756 "/test", [&pi1_event_loop, &pi1_ping_count,
1757 &pi1_pong_count](const examples::Pong &pong) {
1758 VLOG(1) << "Pi1 pong " << FlatbufferToJson(&pong) << " at "
1759 << pi1_event_loop->context().monotonic_remote_time << " -> "
1760 << pi1_event_loop->context().monotonic_event_time;
1761
1762 EXPECT_EQ(pong.value(), pi1_pong_count + 1);
1763 ++pi1_pong_count;
1764 EXPECT_EQ(pi1_ping_count, pi1_pong_count);
1765 });
1766 pi2_event_loop->MakeWatcher(
1767 "/test", [&pi2_event_loop, &pi2_ping_count,
1768 &pi2_pong_count](const examples::Pong &pong) {
1769 VLOG(1) << "Pi2 pong " << FlatbufferToJson(&pong) << " at "
1770 << pi2_event_loop->context().monotonic_remote_time << " -> "
1771 << pi2_event_loop->context().monotonic_event_time;
1772
1773 EXPECT_EQ(pong.value(), pi2_pong_count + 1);
1774 ++pi2_pong_count;
1775 EXPECT_EQ(pi2_ping_count, pi2_pong_count);
1776 });
1777
1778 log_reader_factory.Run();
1779 EXPECT_EQ(pi1_ping_count, 2030);
1780 EXPECT_EQ(pi2_ping_count, 2030);
1781 EXPECT_EQ(pi1_pong_count, 2030);
1782 EXPECT_EQ(pi2_pong_count, 2030);
1783
1784 reader.Deregister();
1785}
Austin Schuh6f3babe2020-01-26 20:34:50 -08001786
Austin Schuh8bd96322020-02-13 21:18:22 -08001787// Tests that we can read log files where the monotonic clocks drift and don't
1788// match correctly. While we are here, also test that different ending times
1789// also is readable.
Austin Schuh61e973f2021-02-21 21:43:56 -08001790TEST_P(MultinodeLoggerTest, MismatchedClocks) {
Austin Schuh87dd3832021-01-01 23:07:31 -08001791 // TODO(austin): Negate...
1792 const chrono::nanoseconds initial_pi2_offset = chrono::seconds(1000);
1793
Austin Schuh66168842021-08-17 19:42:21 -07001794 time_converter_.AddMonotonic(
1795 {BootTimestamp::epoch(), BootTimestamp::epoch() + initial_pi2_offset});
Austin Schuh87dd3832021-01-01 23:07:31 -08001796 // Wait for 95 ms, (~0.1 seconds - 1/2 of the ping/pong period), and set the
1797 // skew to be 200 uS/s
1798 const chrono::nanoseconds startup_sleep1 = time_converter_.AddMonotonic(
1799 {chrono::milliseconds(95),
1800 chrono::milliseconds(95) - chrono::nanoseconds(200) * 95});
1801 // Run another 200 ms to have one logger start first.
1802 const chrono::nanoseconds startup_sleep2 = time_converter_.AddMonotonic(
1803 {chrono::milliseconds(200), chrono::milliseconds(200)});
1804 // Slew one way then the other at the same 200 uS/S slew rate. Make sure we
1805 // go far enough to cause problems if this isn't accounted for.
1806 const chrono::nanoseconds logger_run1 = time_converter_.AddMonotonic(
1807 {chrono::milliseconds(20000),
1808 chrono::milliseconds(20000) - chrono::nanoseconds(200) * 20000});
1809 const chrono::nanoseconds logger_run2 = time_converter_.AddMonotonic(
1810 {chrono::milliseconds(40000),
1811 chrono::milliseconds(40000) + chrono::nanoseconds(200) * 40000});
1812 const chrono::nanoseconds logger_run3 = time_converter_.AddMonotonic(
1813 {chrono::milliseconds(400), chrono::milliseconds(400)});
1814
Austin Schuhcde938c2020-02-02 17:30:07 -08001815 {
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001816 LoggerState pi2_logger = MakeLogger(pi2_);
1817
Austin Schuh58646e22021-08-23 23:51:46 -07001818 LOG(INFO) << "pi2 times: " << pi2_->monotonic_now() << " "
1819 << pi2_->realtime_now() << " distributed "
1820 << pi2_->ToDistributedClock(pi2_->monotonic_now());
Austin Schuhcde938c2020-02-02 17:30:07 -08001821
Austin Schuh58646e22021-08-23 23:51:46 -07001822 LOG(INFO) << "pi2_ times: " << pi2_->monotonic_now() << " "
1823 << pi2_->realtime_now() << " distributed "
1824 << pi2_->ToDistributedClock(pi2_->monotonic_now());
Austin Schuhcde938c2020-02-02 17:30:07 -08001825
Austin Schuh87dd3832021-01-01 23:07:31 -08001826 event_loop_factory_.RunFor(startup_sleep1);
Austin Schuhcde938c2020-02-02 17:30:07 -08001827
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001828 StartLogger(&pi2_logger);
Austin Schuhcde938c2020-02-02 17:30:07 -08001829
Austin Schuh87dd3832021-01-01 23:07:31 -08001830 event_loop_factory_.RunFor(startup_sleep2);
Austin Schuhcde938c2020-02-02 17:30:07 -08001831
Austin Schuh8bd96322020-02-13 21:18:22 -08001832 {
1833 // Run pi1's logger for only part of the time.
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001834 LoggerState pi1_logger = MakeLogger(pi1_);
Austin Schuh8bd96322020-02-13 21:18:22 -08001835
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001836 StartLogger(&pi1_logger);
Austin Schuh87dd3832021-01-01 23:07:31 -08001837 event_loop_factory_.RunFor(logger_run1);
Austin Schuh8bd96322020-02-13 21:18:22 -08001838
Austin Schuh87dd3832021-01-01 23:07:31 -08001839 // Make sure we slewed time far enough so that the difference is greater
1840 // than the network delay. This confirms that if we sort incorrectly, it
1841 // would show in the results.
1842 EXPECT_LT(
Austin Schuh58646e22021-08-23 23:51:46 -07001843 (pi2_->monotonic_now() - pi1_->monotonic_now()) - initial_pi2_offset,
Austin Schuh87dd3832021-01-01 23:07:31 -08001844 -event_loop_factory_.send_delay() -
1845 event_loop_factory_.network_delay());
Austin Schuh8bd96322020-02-13 21:18:22 -08001846
Austin Schuh87dd3832021-01-01 23:07:31 -08001847 event_loop_factory_.RunFor(logger_run2);
Austin Schuh8bd96322020-02-13 21:18:22 -08001848
Austin Schuh87dd3832021-01-01 23:07:31 -08001849 // And now check that we went far enough the other way to make sure we
1850 // cover both problems.
1851 EXPECT_GT(
Austin Schuh58646e22021-08-23 23:51:46 -07001852 (pi2_->monotonic_now() - pi1_->monotonic_now()) - initial_pi2_offset,
Austin Schuh87dd3832021-01-01 23:07:31 -08001853 event_loop_factory_.send_delay() +
1854 event_loop_factory_.network_delay());
Austin Schuh8bd96322020-02-13 21:18:22 -08001855 }
1856
1857 // And log a bit more on pi2.
Austin Schuh87dd3832021-01-01 23:07:31 -08001858 event_loop_factory_.RunFor(logger_run3);
Austin Schuhcde938c2020-02-02 17:30:07 -08001859 }
1860
Austin Schuh72211ae2021-08-05 14:02:30 -07001861 LogReader reader(
1862 SortParts(MakeLogFiles(logfile_base1_, logfile_base2_, 3, 2)));
Austin Schuhcde938c2020-02-02 17:30:07 -08001863
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001864 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
Austin Schuhcde938c2020-02-02 17:30:07 -08001865 log_reader_factory.set_send_delay(chrono::microseconds(0));
1866
Austin Schuhcde938c2020-02-02 17:30:07 -08001867 const Node *pi1 =
1868 configuration::GetNode(log_reader_factory.configuration(), "pi1");
1869 const Node *pi2 =
1870 configuration::GetNode(log_reader_factory.configuration(), "pi2");
1871
Austin Schuh2f8fd752020-09-01 22:38:28 -07001872 // This sends out the fetched messages and advances time to the start of the
1873 // log file.
1874 reader.Register(&log_reader_factory);
1875
1876 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi1) << " pi1";
1877 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi2) << " pi2";
1878 LOG(INFO) << "now pi1 "
1879 << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now();
1880 LOG(INFO) << "now pi2 "
1881 << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now();
1882
Austin Schuhcde938c2020-02-02 17:30:07 -08001883 LOG(INFO) << "Done registering (pi1) "
Austin Schuh391e3172020-09-01 22:48:18 -07001884 << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now()
1885 << " "
Austin Schuhcde938c2020-02-02 17:30:07 -08001886 << log_reader_factory.GetNodeEventLoopFactory(pi1)->realtime_now();
1887 LOG(INFO) << "Done registering (pi2) "
Austin Schuh391e3172020-09-01 22:48:18 -07001888 << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now()
1889 << " "
Austin Schuhcde938c2020-02-02 17:30:07 -08001890 << log_reader_factory.GetNodeEventLoopFactory(pi2)->realtime_now();
1891
Austin Schuh07676622021-01-21 18:59:17 -08001892 EXPECT_THAT(reader.LoggedNodes(),
1893 ::testing::ElementsAre(
1894 configuration::GetNode(reader.logged_configuration(), pi1),
1895 configuration::GetNode(reader.logged_configuration(), pi2)));
Austin Schuhcde938c2020-02-02 17:30:07 -08001896
1897 reader.event_loop_factory()->set_send_delay(chrono::microseconds(0));
1898
1899 std::unique_ptr<EventLoop> pi1_event_loop =
1900 log_reader_factory.MakeEventLoop("test", pi1);
1901 std::unique_ptr<EventLoop> pi2_event_loop =
1902 log_reader_factory.MakeEventLoop("test", pi2);
1903
1904 int pi1_ping_count = 30;
1905 int pi2_ping_count = 30;
1906 int pi1_pong_count = 30;
1907 int pi2_pong_count = 30;
1908
1909 // Confirm that the ping value matches.
1910 pi1_event_loop->MakeWatcher(
1911 "/test", [&pi1_ping_count, &pi1_event_loop](const examples::Ping &ping) {
1912 VLOG(1) << "Pi1 ping " << FlatbufferToJson(&ping)
1913 << pi1_event_loop->context().monotonic_remote_time << " -> "
1914 << pi1_event_loop->context().monotonic_event_time;
1915 EXPECT_EQ(ping.value(), pi1_ping_count + 1);
1916
1917 ++pi1_ping_count;
1918 });
1919 pi2_event_loop->MakeWatcher(
1920 "/test", [&pi2_ping_count, &pi2_event_loop](const examples::Ping &ping) {
1921 VLOG(1) << "Pi2 ping " << FlatbufferToJson(&ping)
1922 << pi2_event_loop->context().monotonic_remote_time << " -> "
1923 << pi2_event_loop->context().monotonic_event_time;
1924 EXPECT_EQ(ping.value(), pi2_ping_count + 1);
1925
1926 ++pi2_ping_count;
1927 });
1928
1929 // Confirm that the ping and pong counts both match, and the value also
1930 // matches.
1931 pi1_event_loop->MakeWatcher(
1932 "/test", [&pi1_event_loop, &pi1_ping_count,
1933 &pi1_pong_count](const examples::Pong &pong) {
1934 VLOG(1) << "Pi1 pong " << FlatbufferToJson(&pong) << " at "
1935 << pi1_event_loop->context().monotonic_remote_time << " -> "
1936 << pi1_event_loop->context().monotonic_event_time;
1937
1938 EXPECT_EQ(pong.value(), pi1_pong_count + 1);
1939 ++pi1_pong_count;
1940 EXPECT_EQ(pi1_ping_count, pi1_pong_count);
1941 });
1942 pi2_event_loop->MakeWatcher(
1943 "/test", [&pi2_event_loop, &pi2_ping_count,
1944 &pi2_pong_count](const examples::Pong &pong) {
1945 VLOG(1) << "Pi2 pong " << FlatbufferToJson(&pong) << " at "
1946 << pi2_event_loop->context().monotonic_remote_time << " -> "
1947 << pi2_event_loop->context().monotonic_event_time;
1948
1949 EXPECT_EQ(pong.value(), pi2_pong_count + 1);
1950 ++pi2_pong_count;
1951 EXPECT_EQ(pi2_ping_count, pi2_pong_count);
1952 });
1953
1954 log_reader_factory.Run();
Austin Schuh8bd96322020-02-13 21:18:22 -08001955 EXPECT_EQ(pi1_ping_count, 6030);
1956 EXPECT_EQ(pi2_ping_count, 6030);
1957 EXPECT_EQ(pi1_pong_count, 6030);
1958 EXPECT_EQ(pi2_pong_count, 6030);
Austin Schuhcde938c2020-02-02 17:30:07 -08001959
1960 reader.Deregister();
1961}
1962
Austin Schuh5212cad2020-09-09 23:12:09 -07001963// Tests that we can sort a bunch of parts into the pre-determined sorted parts.
Austin Schuh61e973f2021-02-21 21:43:56 -08001964TEST_P(MultinodeLoggerTest, SortParts) {
Austin Schuh87dd3832021-01-01 23:07:31 -08001965 time_converter_.StartEqual();
Austin Schuh5212cad2020-09-09 23:12:09 -07001966 // Make a bunch of parts.
1967 {
1968 LoggerState pi1_logger = MakeLogger(pi1_);
1969 LoggerState pi2_logger = MakeLogger(pi2_);
1970
1971 event_loop_factory_.RunFor(chrono::milliseconds(95));
1972
1973 StartLogger(&pi1_logger);
1974 StartLogger(&pi2_logger);
1975
1976 event_loop_factory_.RunFor(chrono::milliseconds(2000));
1977 }
1978
Austin Schuh11d43732020-09-21 17:28:30 -07001979 const std::vector<LogFile> sorted_parts = SortParts(logfiles_);
Austin Schuh3bd4c402020-11-06 18:19:06 -08001980 VerifyParts(sorted_parts);
1981}
Austin Schuh11d43732020-09-21 17:28:30 -07001982
Austin Schuh3bd4c402020-11-06 18:19:06 -08001983// Tests that we can sort a bunch of parts with an empty part. We should ignore
1984// it and remove it from the sorted list.
Austin Schuh61e973f2021-02-21 21:43:56 -08001985TEST_P(MultinodeLoggerTest, SortEmptyParts) {
Austin Schuh87dd3832021-01-01 23:07:31 -08001986 time_converter_.StartEqual();
Austin Schuh3bd4c402020-11-06 18:19:06 -08001987 // Make a bunch of parts.
1988 {
1989 LoggerState pi1_logger = MakeLogger(pi1_);
1990 LoggerState pi2_logger = MakeLogger(pi2_);
Austin Schuh11d43732020-09-21 17:28:30 -07001991
Austin Schuh3bd4c402020-11-06 18:19:06 -08001992 event_loop_factory_.RunFor(chrono::milliseconds(95));
Austin Schuh11d43732020-09-21 17:28:30 -07001993
Austin Schuh3bd4c402020-11-06 18:19:06 -08001994 StartLogger(&pi1_logger);
1995 StartLogger(&pi2_logger);
Austin Schuh11d43732020-09-21 17:28:30 -07001996
Austin Schuh3bd4c402020-11-06 18:19:06 -08001997 event_loop_factory_.RunFor(chrono::milliseconds(2000));
Austin Schuh11d43732020-09-21 17:28:30 -07001998 }
1999
Austin Schuh3bd4c402020-11-06 18:19:06 -08002000 // TODO(austin): Should we flip out if the file can't open?
James Kuszmauldd0a5042021-10-28 23:38:04 -07002001 const std::string kEmptyFile("foobarinvalidfiledoesnotexist" + Extension());
Austin Schuh11d43732020-09-21 17:28:30 -07002002
Austin Schuh3bd4c402020-11-06 18:19:06 -08002003 aos::util::WriteStringToFileOrDie(kEmptyFile, "");
2004 logfiles_.emplace_back(kEmptyFile);
Austin Schuh5212cad2020-09-09 23:12:09 -07002005
Austin Schuh3bd4c402020-11-06 18:19:06 -08002006 const std::vector<LogFile> sorted_parts = SortParts(logfiles_);
2007 VerifyParts(sorted_parts, {kEmptyFile});
Austin Schuh5212cad2020-09-09 23:12:09 -07002008}
2009
James Kuszmauldd0a5042021-10-28 23:38:04 -07002010// Tests that we can sort a bunch of parts with the end missing off a
Austin Schuh3bd4c402020-11-06 18:19:06 -08002011// file. We should use the part we can read.
James Kuszmauldd0a5042021-10-28 23:38:04 -07002012TEST_P(MultinodeLoggerTest, SortTruncatedParts) {
Austin Schuhbfe6c572022-01-27 20:48:20 -08002013 std::vector<std::string> actual_filenames;
Austin Schuh87dd3832021-01-01 23:07:31 -08002014 time_converter_.StartEqual();
Austin Schuh3bd4c402020-11-06 18:19:06 -08002015 // Make a bunch of parts.
2016 {
2017 LoggerState pi1_logger = MakeLogger(pi1_);
2018 LoggerState pi2_logger = MakeLogger(pi2_);
2019
2020 event_loop_factory_.RunFor(chrono::milliseconds(95));
2021
James Kuszmauldd0a5042021-10-28 23:38:04 -07002022 StartLogger(&pi1_logger);
2023 StartLogger(&pi2_logger);
Austin Schuh3bd4c402020-11-06 18:19:06 -08002024
2025 event_loop_factory_.RunFor(chrono::milliseconds(2000));
Austin Schuhbfe6c572022-01-27 20:48:20 -08002026
2027 pi1_logger.AppendAllFilenames(&actual_filenames);
2028 pi2_logger.AppendAllFilenames(&actual_filenames);
Austin Schuh3bd4c402020-11-06 18:19:06 -08002029 }
2030
Austin Schuhbfe6c572022-01-27 20:48:20 -08002031 ASSERT_THAT(actual_filenames,
2032 ::testing::UnorderedElementsAreArray(logfiles_));
2033
Austin Schuh3bd4c402020-11-06 18:19:06 -08002034 // Strip off the end of one of the files. Pick one with a lot of data.
James Kuszmauldd0a5042021-10-28 23:38:04 -07002035 // For snappy, needs to have enough data to be >1 chunk of compressed data so
2036 // that we don't corrupt the entire log part.
Austin Schuh3bd4c402020-11-06 18:19:06 -08002037 ::std::string compressed_contents =
Austin Schuhbfe6c572022-01-27 20:48:20 -08002038 aos::util::ReadFileToStringOrDie(logfiles_[4]);
Austin Schuh3bd4c402020-11-06 18:19:06 -08002039
2040 aos::util::WriteStringToFileOrDie(
Austin Schuhbfe6c572022-01-27 20:48:20 -08002041 logfiles_[4],
Austin Schuh3bd4c402020-11-06 18:19:06 -08002042 compressed_contents.substr(0, compressed_contents.size() - 100));
2043
2044 const std::vector<LogFile> sorted_parts = SortParts(logfiles_);
2045 VerifyParts(sorted_parts);
2046}
Austin Schuh3bd4c402020-11-06 18:19:06 -08002047
Austin Schuh01b4c352020-09-21 23:09:39 -07002048// Tests that if we remap a remapped channel, it shows up correctly.
Austin Schuh61e973f2021-02-21 21:43:56 -08002049TEST_P(MultinodeLoggerTest, RemapLoggedChannel) {
Austin Schuh87dd3832021-01-01 23:07:31 -08002050 time_converter_.StartEqual();
Austin Schuh01b4c352020-09-21 23:09:39 -07002051 {
2052 LoggerState pi1_logger = MakeLogger(pi1_);
2053 LoggerState pi2_logger = MakeLogger(pi2_);
2054
2055 event_loop_factory_.RunFor(chrono::milliseconds(95));
2056
2057 StartLogger(&pi1_logger);
2058 StartLogger(&pi2_logger);
2059
2060 event_loop_factory_.RunFor(chrono::milliseconds(20000));
2061 }
2062
Austin Schuh287d43d2020-12-04 20:19:33 -08002063 LogReader reader(SortParts(logfiles_));
Austin Schuh01b4c352020-09-21 23:09:39 -07002064
2065 // Remap just on pi1.
2066 reader.RemapLoggedChannel<aos::timing::Report>(
2067 "/aos", configuration::GetNode(reader.configuration(), "pi1"));
2068
2069 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
2070 log_reader_factory.set_send_delay(chrono::microseconds(0));
2071
Austin Schuh1c227352021-09-17 12:53:54 -07002072 std::vector<const Channel *> remapped_channels = reader.RemappedChannels();
2073 ASSERT_EQ(remapped_channels.size(), 1u);
2074 EXPECT_EQ(remapped_channels[0]->name()->string_view(), "/original/pi1/aos");
2075 EXPECT_EQ(remapped_channels[0]->type()->string_view(), "aos.timing.Report");
2076
Austin Schuh01b4c352020-09-21 23:09:39 -07002077 reader.Register(&log_reader_factory);
2078
2079 const Node *pi1 =
2080 configuration::GetNode(log_reader_factory.configuration(), "pi1");
2081 const Node *pi2 =
2082 configuration::GetNode(log_reader_factory.configuration(), "pi2");
2083
2084 // Confirm we can read the data on the remapped channel, just for pi1. Nothing
2085 // else should have moved.
2086 std::unique_ptr<EventLoop> pi1_event_loop =
2087 log_reader_factory.MakeEventLoop("test", pi1);
2088 pi1_event_loop->SkipTimingReport();
2089 std::unique_ptr<EventLoop> full_pi1_event_loop =
2090 log_reader_factory.MakeEventLoop("test", pi1);
2091 full_pi1_event_loop->SkipTimingReport();
2092 std::unique_ptr<EventLoop> pi2_event_loop =
2093 log_reader_factory.MakeEventLoop("test", pi2);
2094 pi2_event_loop->SkipTimingReport();
2095
2096 MessageCounter<aos::timing::Report> pi1_timing_report(pi1_event_loop.get(),
2097 "/aos");
2098 MessageCounter<aos::timing::Report> full_pi1_timing_report(
2099 full_pi1_event_loop.get(), "/pi1/aos");
2100 MessageCounter<aos::timing::Report> pi1_original_timing_report(
2101 pi1_event_loop.get(), "/original/aos");
2102 MessageCounter<aos::timing::Report> full_pi1_original_timing_report(
2103 full_pi1_event_loop.get(), "/original/pi1/aos");
2104 MessageCounter<aos::timing::Report> pi2_timing_report(pi2_event_loop.get(),
2105 "/aos");
2106
2107 log_reader_factory.Run();
2108
2109 EXPECT_EQ(pi1_timing_report.count(), 0u);
2110 EXPECT_EQ(full_pi1_timing_report.count(), 0u);
2111 EXPECT_NE(pi1_original_timing_report.count(), 0u);
2112 EXPECT_NE(full_pi1_original_timing_report.count(), 0u);
2113 EXPECT_NE(pi2_timing_report.count(), 0u);
2114
2115 reader.Deregister();
2116}
2117
Austin Schuh006a9f52021-04-07 16:24:18 -07002118// Tests that we can remap a forwarded channel as well.
2119TEST_P(MultinodeLoggerTest, RemapForwardedLoggedChannel) {
2120 time_converter_.StartEqual();
2121 {
2122 LoggerState pi1_logger = MakeLogger(pi1_);
2123 LoggerState pi2_logger = MakeLogger(pi2_);
2124
2125 event_loop_factory_.RunFor(chrono::milliseconds(95));
2126
2127 StartLogger(&pi1_logger);
2128 StartLogger(&pi2_logger);
2129
2130 event_loop_factory_.RunFor(chrono::milliseconds(20000));
2131 }
2132
2133 LogReader reader(SortParts(logfiles_));
2134
2135 reader.RemapLoggedChannel<examples::Ping>("/test");
2136
2137 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
2138 log_reader_factory.set_send_delay(chrono::microseconds(0));
2139
2140 reader.Register(&log_reader_factory);
2141
2142 const Node *pi1 =
2143 configuration::GetNode(log_reader_factory.configuration(), "pi1");
2144 const Node *pi2 =
2145 configuration::GetNode(log_reader_factory.configuration(), "pi2");
2146
2147 // Confirm we can read the data on the remapped channel, just for pi1. Nothing
2148 // else should have moved.
2149 std::unique_ptr<EventLoop> pi1_event_loop =
2150 log_reader_factory.MakeEventLoop("test", pi1);
2151 pi1_event_loop->SkipTimingReport();
2152 std::unique_ptr<EventLoop> full_pi1_event_loop =
2153 log_reader_factory.MakeEventLoop("test", pi1);
2154 full_pi1_event_loop->SkipTimingReport();
2155 std::unique_ptr<EventLoop> pi2_event_loop =
2156 log_reader_factory.MakeEventLoop("test", pi2);
2157 pi2_event_loop->SkipTimingReport();
2158
2159 MessageCounter<examples::Ping> pi1_ping(pi1_event_loop.get(), "/test");
2160 MessageCounter<examples::Ping> pi2_ping(pi2_event_loop.get(), "/test");
2161 MessageCounter<examples::Ping> pi1_original_ping(pi1_event_loop.get(),
2162 "/original/test");
2163 MessageCounter<examples::Ping> pi2_original_ping(pi2_event_loop.get(),
2164 "/original/test");
2165
2166 std::unique_ptr<MessageCounter<message_bridge::RemoteMessage>>
2167 pi1_original_ping_timestamp;
2168 std::unique_ptr<MessageCounter<message_bridge::RemoteMessage>>
2169 pi1_ping_timestamp;
2170 if (!shared()) {
2171 pi1_original_ping_timestamp =
2172 std::make_unique<MessageCounter<message_bridge::RemoteMessage>>(
2173 pi1_event_loop.get(),
2174 "/pi1/aos/remote_timestamps/pi2/original/test/aos-examples-Ping");
2175 pi1_ping_timestamp =
2176 std::make_unique<MessageCounter<message_bridge::RemoteMessage>>(
2177 pi1_event_loop.get(),
2178 "/pi1/aos/remote_timestamps/pi2/test/aos-examples-Ping");
2179 }
2180
2181 log_reader_factory.Run();
2182
2183 EXPECT_EQ(pi1_ping.count(), 0u);
2184 EXPECT_EQ(pi2_ping.count(), 0u);
2185 EXPECT_NE(pi1_original_ping.count(), 0u);
2186 EXPECT_NE(pi2_original_ping.count(), 0u);
2187 if (!shared()) {
2188 EXPECT_NE(pi1_original_ping_timestamp->count(), 0u);
2189 EXPECT_EQ(pi1_ping_timestamp->count(), 0u);
2190 }
2191
2192 reader.Deregister();
2193}
2194
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002195// Tests that we properly recreate forwarded timestamps when replaying a log.
2196// This should be enough that we can then re-run the logger and get a valid log
2197// back.
Austin Schuh61e973f2021-02-21 21:43:56 -08002198TEST_P(MultinodeLoggerTest, MessageHeader) {
Austin Schuh87dd3832021-01-01 23:07:31 -08002199 time_converter_.StartEqual();
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002200 {
2201 LoggerState pi1_logger = MakeLogger(pi1_);
2202 LoggerState pi2_logger = MakeLogger(pi2_);
2203
2204 event_loop_factory_.RunFor(chrono::milliseconds(95));
2205
2206 StartLogger(&pi1_logger);
2207 StartLogger(&pi2_logger);
2208
2209 event_loop_factory_.RunFor(chrono::milliseconds(20000));
2210 }
2211
Austin Schuh287d43d2020-12-04 20:19:33 -08002212 LogReader reader(SortParts(logfiles_));
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002213
2214 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
2215 log_reader_factory.set_send_delay(chrono::microseconds(0));
2216
2217 // This sends out the fetched messages and advances time to the start of the
2218 // log file.
2219 reader.Register(&log_reader_factory);
2220
2221 const Node *pi1 =
2222 configuration::GetNode(log_reader_factory.configuration(), "pi1");
2223 const Node *pi2 =
2224 configuration::GetNode(log_reader_factory.configuration(), "pi2");
2225
2226 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi1) << " pi1";
2227 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi2) << " pi2";
2228 LOG(INFO) << "now pi1 "
2229 << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now();
2230 LOG(INFO) << "now pi2 "
2231 << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now();
2232
Austin Schuh07676622021-01-21 18:59:17 -08002233 EXPECT_THAT(reader.LoggedNodes(),
2234 ::testing::ElementsAre(
2235 configuration::GetNode(reader.logged_configuration(), pi1),
2236 configuration::GetNode(reader.logged_configuration(), pi2)));
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002237
2238 reader.event_loop_factory()->set_send_delay(chrono::microseconds(0));
2239
2240 std::unique_ptr<EventLoop> pi1_event_loop =
2241 log_reader_factory.MakeEventLoop("test", pi1);
2242 std::unique_ptr<EventLoop> pi2_event_loop =
2243 log_reader_factory.MakeEventLoop("test", pi2);
2244
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002245 aos::Fetcher<message_bridge::Timestamp> pi1_timestamp_on_pi1_fetcher =
2246 pi1_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi1/aos");
2247 aos::Fetcher<message_bridge::Timestamp> pi1_timestamp_on_pi2_fetcher =
2248 pi2_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi1/aos");
2249
2250 aos::Fetcher<examples::Ping> ping_on_pi1_fetcher =
2251 pi1_event_loop->MakeFetcher<examples::Ping>("/test");
2252 aos::Fetcher<examples::Ping> ping_on_pi2_fetcher =
2253 pi2_event_loop->MakeFetcher<examples::Ping>("/test");
2254
2255 aos::Fetcher<message_bridge::Timestamp> pi2_timestamp_on_pi2_fetcher =
2256 pi2_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi2/aos");
2257 aos::Fetcher<message_bridge::Timestamp> pi2_timestamp_on_pi1_fetcher =
2258 pi1_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi2/aos");
2259
2260 aos::Fetcher<examples::Pong> pong_on_pi2_fetcher =
2261 pi2_event_loop->MakeFetcher<examples::Pong>("/test");
2262 aos::Fetcher<examples::Pong> pong_on_pi1_fetcher =
2263 pi1_event_loop->MakeFetcher<examples::Pong>("/test");
2264
2265 const size_t pi1_timestamp_channel = configuration::ChannelIndex(
2266 pi1_event_loop->configuration(), pi1_timestamp_on_pi1_fetcher.channel());
2267 const size_t ping_timestamp_channel = configuration::ChannelIndex(
2268 pi2_event_loop->configuration(), ping_on_pi2_fetcher.channel());
2269
2270 const size_t pi2_timestamp_channel = configuration::ChannelIndex(
2271 pi2_event_loop->configuration(), pi2_timestamp_on_pi2_fetcher.channel());
2272 const size_t pong_timestamp_channel = configuration::ChannelIndex(
2273 pi1_event_loop->configuration(), pong_on_pi1_fetcher.channel());
2274
Austin Schuh969cd602021-01-03 00:09:45 -08002275 const chrono::nanoseconds network_delay = event_loop_factory_.network_delay();
Austin Schuh816e5d62021-01-05 23:42:20 -08002276 const chrono::nanoseconds send_delay = event_loop_factory_.send_delay();
Austin Schuh969cd602021-01-03 00:09:45 -08002277
Austin Schuh61e973f2021-02-21 21:43:56 -08002278 for (std::pair<int, std::string> channel :
2279 shared()
2280 ? std::vector<
2281 std::pair<int, std::string>>{{-1,
2282 "/aos/remote_timestamps/pi2"}}
2283 : std::vector<std::pair<int, std::string>>{
2284 {pi1_timestamp_channel,
2285 "/aos/remote_timestamps/pi2/pi1/aos/"
2286 "aos-message_bridge-Timestamp"},
2287 {ping_timestamp_channel,
2288 "/aos/remote_timestamps/pi2/test/aos-examples-Ping"}}) {
2289 pi1_event_loop->MakeWatcher(
2290 channel.second,
2291 [&pi1_event_loop, &pi2_event_loop, pi1_timestamp_channel,
2292 ping_timestamp_channel, &pi1_timestamp_on_pi1_fetcher,
2293 &pi1_timestamp_on_pi2_fetcher, &ping_on_pi1_fetcher,
2294 &ping_on_pi2_fetcher, network_delay, send_delay,
2295 channel_index = channel.first](const RemoteMessage &header) {
2296 const aos::monotonic_clock::time_point header_monotonic_sent_time(
2297 chrono::nanoseconds(header.monotonic_sent_time()));
2298 const aos::realtime_clock::time_point header_realtime_sent_time(
2299 chrono::nanoseconds(header.realtime_sent_time()));
2300 const aos::monotonic_clock::time_point header_monotonic_remote_time(
2301 chrono::nanoseconds(header.monotonic_remote_time()));
2302 const aos::realtime_clock::time_point header_realtime_remote_time(
2303 chrono::nanoseconds(header.realtime_remote_time()));
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002304
Austin Schuh61e973f2021-02-21 21:43:56 -08002305 if (channel_index != -1) {
2306 ASSERT_EQ(channel_index, header.channel_index());
2307 }
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002308
Austin Schuh61e973f2021-02-21 21:43:56 -08002309 const Context *pi1_context = nullptr;
2310 const Context *pi2_context = nullptr;
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002311
Austin Schuh61e973f2021-02-21 21:43:56 -08002312 if (header.channel_index() == pi1_timestamp_channel) {
2313 ASSERT_TRUE(pi1_timestamp_on_pi1_fetcher.FetchNext());
2314 ASSERT_TRUE(pi1_timestamp_on_pi2_fetcher.FetchNext());
2315 pi1_context = &pi1_timestamp_on_pi1_fetcher.context();
2316 pi2_context = &pi1_timestamp_on_pi2_fetcher.context();
2317 } else if (header.channel_index() == ping_timestamp_channel) {
2318 ASSERT_TRUE(ping_on_pi1_fetcher.FetchNext());
2319 ASSERT_TRUE(ping_on_pi2_fetcher.FetchNext());
2320 pi1_context = &ping_on_pi1_fetcher.context();
2321 pi2_context = &ping_on_pi2_fetcher.context();
2322 } else {
2323 LOG(FATAL) << "Unknown channel " << FlatbufferToJson(&header) << " "
2324 << configuration::CleanedChannelToString(
2325 pi1_event_loop->configuration()->channels()->Get(
2326 header.channel_index()));
2327 }
Austin Schuh315b96b2020-12-11 21:21:12 -08002328
Austin Schuh61e973f2021-02-21 21:43:56 -08002329 ASSERT_TRUE(header.has_boot_uuid());
Austin Schuhcdd90272021-03-15 12:46:16 -07002330 EXPECT_EQ(UUID::FromVector(header.boot_uuid()),
2331 pi2_event_loop->boot_uuid());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002332
Austin Schuh61e973f2021-02-21 21:43:56 -08002333 EXPECT_EQ(pi1_context->queue_index, header.remote_queue_index());
2334 EXPECT_EQ(pi2_context->remote_queue_index,
2335 header.remote_queue_index());
2336 EXPECT_EQ(pi2_context->queue_index, header.queue_index());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002337
Austin Schuh61e973f2021-02-21 21:43:56 -08002338 EXPECT_EQ(pi2_context->monotonic_event_time,
2339 header_monotonic_sent_time);
2340 EXPECT_EQ(pi2_context->realtime_event_time,
2341 header_realtime_sent_time);
2342 EXPECT_EQ(pi2_context->realtime_remote_time,
2343 header_realtime_remote_time);
2344 EXPECT_EQ(pi2_context->monotonic_remote_time,
2345 header_monotonic_remote_time);
Austin Schuh969cd602021-01-03 00:09:45 -08002346
Austin Schuh61e973f2021-02-21 21:43:56 -08002347 EXPECT_EQ(pi1_context->realtime_event_time,
2348 header_realtime_remote_time);
2349 EXPECT_EQ(pi1_context->monotonic_event_time,
2350 header_monotonic_remote_time);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002351
Austin Schuh61e973f2021-02-21 21:43:56 -08002352 // Time estimation isn't perfect, but we know the clocks were
2353 // identical when logged, so we know when this should have come back.
2354 // Confirm we got it when we expected.
2355 EXPECT_EQ(pi1_event_loop->context().monotonic_event_time,
2356 pi1_context->monotonic_event_time + 2 * network_delay +
2357 send_delay);
2358 });
2359 }
2360 for (std::pair<int, std::string> channel :
2361 shared()
2362 ? std::vector<
2363 std::pair<int, std::string>>{{-1,
2364 "/aos/remote_timestamps/pi1"}}
2365 : std::vector<std::pair<int, std::string>>{
2366 {pi2_timestamp_channel,
2367 "/aos/remote_timestamps/pi1/pi2/aos/"
2368 "aos-message_bridge-Timestamp"}}) {
2369 pi2_event_loop->MakeWatcher(
2370 channel.second,
2371 [&pi2_event_loop, &pi1_event_loop, pi2_timestamp_channel,
2372 pong_timestamp_channel, &pi2_timestamp_on_pi2_fetcher,
2373 &pi2_timestamp_on_pi1_fetcher, &pong_on_pi2_fetcher,
2374 &pong_on_pi1_fetcher, network_delay, send_delay,
2375 channel_index = channel.first](const RemoteMessage &header) {
2376 const aos::monotonic_clock::time_point header_monotonic_sent_time(
2377 chrono::nanoseconds(header.monotonic_sent_time()));
2378 const aos::realtime_clock::time_point header_realtime_sent_time(
2379 chrono::nanoseconds(header.realtime_sent_time()));
2380 const aos::monotonic_clock::time_point header_monotonic_remote_time(
2381 chrono::nanoseconds(header.monotonic_remote_time()));
2382 const aos::realtime_clock::time_point header_realtime_remote_time(
2383 chrono::nanoseconds(header.realtime_remote_time()));
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002384
Austin Schuh61e973f2021-02-21 21:43:56 -08002385 if (channel_index != -1) {
2386 ASSERT_EQ(channel_index, header.channel_index());
2387 }
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002388
Austin Schuh61e973f2021-02-21 21:43:56 -08002389 const Context *pi2_context = nullptr;
2390 const Context *pi1_context = nullptr;
Austin Schuh315b96b2020-12-11 21:21:12 -08002391
Austin Schuh61e973f2021-02-21 21:43:56 -08002392 if (header.channel_index() == pi2_timestamp_channel) {
2393 ASSERT_TRUE(pi2_timestamp_on_pi2_fetcher.FetchNext());
2394 ASSERT_TRUE(pi2_timestamp_on_pi1_fetcher.FetchNext());
2395 pi2_context = &pi2_timestamp_on_pi2_fetcher.context();
2396 pi1_context = &pi2_timestamp_on_pi1_fetcher.context();
2397 } else if (header.channel_index() == pong_timestamp_channel) {
2398 ASSERT_TRUE(pong_on_pi2_fetcher.FetchNext());
2399 ASSERT_TRUE(pong_on_pi1_fetcher.FetchNext());
2400 pi2_context = &pong_on_pi2_fetcher.context();
2401 pi1_context = &pong_on_pi1_fetcher.context();
2402 } else {
2403 LOG(FATAL) << "Unknown channel " << FlatbufferToJson(&header) << " "
2404 << configuration::CleanedChannelToString(
2405 pi2_event_loop->configuration()->channels()->Get(
2406 header.channel_index()));
2407 }
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002408
Austin Schuh61e973f2021-02-21 21:43:56 -08002409 ASSERT_TRUE(header.has_boot_uuid());
Austin Schuhcdd90272021-03-15 12:46:16 -07002410 EXPECT_EQ(UUID::FromVector(header.boot_uuid()),
2411 pi1_event_loop->boot_uuid());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002412
Austin Schuh61e973f2021-02-21 21:43:56 -08002413 EXPECT_EQ(pi2_context->queue_index, header.remote_queue_index());
2414 EXPECT_EQ(pi1_context->remote_queue_index,
2415 header.remote_queue_index());
2416 EXPECT_EQ(pi1_context->queue_index, header.queue_index());
Austin Schuh969cd602021-01-03 00:09:45 -08002417
Austin Schuh61e973f2021-02-21 21:43:56 -08002418 EXPECT_EQ(pi1_context->monotonic_event_time,
2419 header_monotonic_sent_time);
2420 EXPECT_EQ(pi1_context->realtime_event_time,
2421 header_realtime_sent_time);
2422 EXPECT_EQ(pi1_context->realtime_remote_time,
2423 header_realtime_remote_time);
2424 EXPECT_EQ(pi1_context->monotonic_remote_time,
2425 header_monotonic_remote_time);
2426
2427 EXPECT_EQ(pi2_context->realtime_event_time,
2428 header_realtime_remote_time);
2429 EXPECT_EQ(pi2_context->monotonic_event_time,
2430 header_monotonic_remote_time);
2431
2432 // Time estimation isn't perfect, but we know the clocks were
2433 // identical when logged, so we know when this should have come back.
2434 // Confirm we got it when we expected.
2435 EXPECT_EQ(pi2_event_loop->context().monotonic_event_time,
2436 pi2_context->monotonic_event_time + 2 * network_delay +
2437 send_delay);
2438 });
2439 }
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002440
2441 // And confirm we can re-create a log again, while checking the contents.
2442 {
2443 LoggerState pi1_logger = MakeLogger(
Austin Schuh58646e22021-08-23 23:51:46 -07002444 log_reader_factory.GetNodeEventLoopFactory("pi1"), &log_reader_factory);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002445 LoggerState pi2_logger = MakeLogger(
Austin Schuh58646e22021-08-23 23:51:46 -07002446 log_reader_factory.GetNodeEventLoopFactory("pi2"), &log_reader_factory);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002447
Austin Schuh25b46712021-01-03 00:04:38 -08002448 StartLogger(&pi1_logger, tmp_dir_ + "/relogged1");
2449 StartLogger(&pi2_logger, tmp_dir_ + "/relogged2");
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002450
2451 log_reader_factory.Run();
2452 }
2453
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002454 reader.Deregister();
James Kuszmaul4f106fb2021-01-05 20:53:02 -08002455
2456 // And verify that we can run the LogReader over the relogged files without
2457 // hitting any fatal errors.
2458 {
2459 LogReader relogged_reader(SortParts(
2460 MakeLogFiles(tmp_dir_ + "/relogged1", tmp_dir_ + "/relogged2")));
2461 relogged_reader.Register();
2462
2463 relogged_reader.event_loop_factory()->Run();
2464 }
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07002465}
2466
Austin Schuh315b96b2020-12-11 21:21:12 -08002467// Tests that we properly populate and extract the logger_start time by setting
2468// up a clock difference between 2 nodes and looking at the resulting parts.
Austin Schuh61e973f2021-02-21 21:43:56 -08002469TEST_P(MultinodeLoggerTest, LoggerStartTime) {
Austin Schuhf5f99f32022-02-07 20:05:37 -08002470 std::vector<std::string> actual_filenames;
Austin Schuh87dd3832021-01-01 23:07:31 -08002471 time_converter_.AddMonotonic(
Austin Schuh3e20c692021-11-16 20:43:16 -08002472 {BootTimestamp::epoch(), BootTimestamp::epoch() + chrono::seconds(1000)});
Austin Schuh315b96b2020-12-11 21:21:12 -08002473 {
2474 LoggerState pi1_logger = MakeLogger(pi1_);
2475 LoggerState pi2_logger = MakeLogger(pi2_);
2476
Austin Schuh315b96b2020-12-11 21:21:12 -08002477 StartLogger(&pi1_logger);
2478 StartLogger(&pi2_logger);
2479
2480 event_loop_factory_.RunFor(chrono::milliseconds(10000));
Austin Schuhf5f99f32022-02-07 20:05:37 -08002481
2482 pi1_logger.AppendAllFilenames(&actual_filenames);
2483 pi2_logger.AppendAllFilenames(&actual_filenames);
Austin Schuh315b96b2020-12-11 21:21:12 -08002484 }
2485
Austin Schuhf5f99f32022-02-07 20:05:37 -08002486 ASSERT_THAT(actual_filenames,
2487 ::testing::UnorderedElementsAreArray(logfiles_));
2488
Austin Schuh315b96b2020-12-11 21:21:12 -08002489 for (const LogFile &log_file : SortParts(logfiles_)) {
2490 for (const LogParts &log_part : log_file.parts) {
2491 if (log_part.node == log_file.logger_node) {
2492 EXPECT_EQ(log_part.logger_monotonic_start_time,
2493 aos::monotonic_clock::min_time);
2494 EXPECT_EQ(log_part.logger_realtime_start_time,
2495 aos::realtime_clock::min_time);
2496 } else {
2497 const chrono::seconds offset = log_file.logger_node == "pi1"
2498 ? -chrono::seconds(1000)
2499 : chrono::seconds(1000);
2500 EXPECT_EQ(log_part.logger_monotonic_start_time,
2501 log_part.monotonic_start_time + offset);
2502 EXPECT_EQ(log_part.logger_realtime_start_time,
2503 log_file.realtime_start_time +
2504 (log_part.logger_monotonic_start_time -
2505 log_file.monotonic_start_time));
2506 }
2507 }
2508 }
2509}
2510
Austin Schuh6bb8a822021-03-31 23:04:39 -07002511// Test that renaming the base, renames the folder.
Austin Schuh9f2a74b2021-12-08 12:10:50 -08002512TEST_P(MultinodeLoggerTest, LoggerRenameFolder) {
Austin Schuh9733ae52021-07-30 18:25:52 -07002513 util::UnlinkRecursive(tmp_dir_ + "/renamefolder");
2514 util::UnlinkRecursive(tmp_dir_ + "/new-good");
Austin Schuh6bb8a822021-03-31 23:04:39 -07002515 time_converter_.AddMonotonic(
Austin Schuh3e20c692021-11-16 20:43:16 -08002516 {BootTimestamp::epoch(), BootTimestamp::epoch() + chrono::seconds(1000)});
Austin Schuh6bb8a822021-03-31 23:04:39 -07002517 logfile_base1_ = tmp_dir_ + "/renamefolder/multi_logfile1";
2518 logfile_base2_ = tmp_dir_ + "/renamefolder/multi_logfile2";
2519 logfiles_ = MakeLogFiles(logfile_base1_, logfile_base2_);
2520 LoggerState pi1_logger = MakeLogger(pi1_);
2521 LoggerState pi2_logger = MakeLogger(pi2_);
2522
2523 StartLogger(&pi1_logger);
2524 StartLogger(&pi2_logger);
2525
2526 event_loop_factory_.RunFor(chrono::milliseconds(10000));
2527 logfile_base1_ = tmp_dir_ + "/new-good/multi_logfile1";
2528 logfile_base2_ = tmp_dir_ + "/new-good/multi_logfile2";
2529 logfiles_ = MakeLogFiles(logfile_base1_, logfile_base2_);
2530 ASSERT_TRUE(pi1_logger.logger->RenameLogBase(logfile_base1_));
2531 ASSERT_TRUE(pi2_logger.logger->RenameLogBase(logfile_base2_));
2532 for (auto &file : logfiles_) {
2533 struct stat s;
2534 EXPECT_EQ(0, stat(file.c_str(), &s));
2535 }
2536}
2537
2538// Test that renaming the file base dies.
2539TEST_P(MultinodeLoggerDeathTest, LoggerRenameFile) {
2540 time_converter_.AddMonotonic(
Austin Schuh58646e22021-08-23 23:51:46 -07002541 {BootTimestamp::epoch(), BootTimestamp::epoch() + chrono::seconds(1000)});
Austin Schuh9733ae52021-07-30 18:25:52 -07002542 util::UnlinkRecursive(tmp_dir_ + "/renamefile");
Austin Schuh6bb8a822021-03-31 23:04:39 -07002543 logfile_base1_ = tmp_dir_ + "/renamefile/multi_logfile1";
2544 logfile_base2_ = tmp_dir_ + "/renamefile/multi_logfile2";
2545 logfiles_ = MakeLogFiles(logfile_base1_, logfile_base2_);
2546 LoggerState pi1_logger = MakeLogger(pi1_);
2547 StartLogger(&pi1_logger);
2548 event_loop_factory_.RunFor(chrono::milliseconds(10000));
2549 logfile_base1_ = tmp_dir_ + "/new-renamefile/new_multi_logfile1";
2550 EXPECT_DEATH({ pi1_logger.logger->RenameLogBase(logfile_base1_); },
2551 "Rename of file base from");
2552}
2553
Austin Schuh8bd96322020-02-13 21:18:22 -08002554// TODO(austin): We can write a test which recreates a logfile and confirms that
2555// we get it back. That is the ultimate test.
2556
Austin Schuh315b96b2020-12-11 21:21:12 -08002557// Tests that we properly recreate forwarded timestamps when replaying a log.
2558// This should be enough that we can then re-run the logger and get a valid log
2559// back.
Austin Schuh58646e22021-08-23 23:51:46 -07002560TEST_P(MultinodeLoggerTest, RemoteReboot) {
Austin Schuhbfe6c572022-01-27 20:48:20 -08002561 std::vector<std::string> actual_filenames;
2562
Austin Schuh58646e22021-08-23 23:51:46 -07002563 const UUID pi1_boot0 = UUID::Random();
2564 const UUID pi2_boot0 = UUID::Random();
2565 const UUID pi2_boot1 = UUID::Random();
Austin Schuh315b96b2020-12-11 21:21:12 -08002566 {
Austin Schuh58646e22021-08-23 23:51:46 -07002567 CHECK_EQ(pi1_index_, 0u);
2568 CHECK_EQ(pi2_index_, 1u);
2569
2570 time_converter_.set_boot_uuid(pi1_index_, 0, pi1_boot0);
2571 time_converter_.set_boot_uuid(pi2_index_, 0, pi2_boot0);
2572 time_converter_.set_boot_uuid(pi2_index_, 1, pi2_boot1);
2573
2574 time_converter_.AddNextTimestamp(
2575 distributed_clock::epoch(),
2576 {BootTimestamp::epoch(), BootTimestamp::epoch()});
2577 const chrono::nanoseconds reboot_time = chrono::milliseconds(10100);
2578 time_converter_.AddNextTimestamp(
2579 distributed_clock::epoch() + reboot_time,
2580 {BootTimestamp::epoch() + reboot_time,
2581 BootTimestamp{
2582 .boot = 1,
2583 .time = monotonic_clock::epoch() + chrono::milliseconds(1323)}});
2584 }
2585
2586 {
Austin Schuh315b96b2020-12-11 21:21:12 -08002587 LoggerState pi1_logger = MakeLogger(pi1_);
2588
2589 event_loop_factory_.RunFor(chrono::milliseconds(95));
Austin Schuh58646e22021-08-23 23:51:46 -07002590 EXPECT_EQ(event_loop_factory_.GetNodeEventLoopFactory("pi1")->boot_uuid(),
2591 pi1_boot0);
2592 EXPECT_EQ(event_loop_factory_.GetNodeEventLoopFactory("pi2")->boot_uuid(),
2593 pi2_boot0);
Austin Schuh315b96b2020-12-11 21:21:12 -08002594
2595 StartLogger(&pi1_logger);
2596
2597 event_loop_factory_.RunFor(chrono::milliseconds(10000));
2598
Austin Schuh58646e22021-08-23 23:51:46 -07002599 VLOG(1) << "Reboot now!";
Austin Schuh315b96b2020-12-11 21:21:12 -08002600
2601 event_loop_factory_.RunFor(chrono::milliseconds(20000));
Austin Schuh58646e22021-08-23 23:51:46 -07002602 EXPECT_EQ(event_loop_factory_.GetNodeEventLoopFactory("pi1")->boot_uuid(),
2603 pi1_boot0);
2604 EXPECT_EQ(event_loop_factory_.GetNodeEventLoopFactory("pi2")->boot_uuid(),
2605 pi2_boot1);
Austin Schuhbfe6c572022-01-27 20:48:20 -08002606
2607 pi1_logger.AppendAllFilenames(&actual_filenames);
Austin Schuh315b96b2020-12-11 21:21:12 -08002608 }
2609
Austin Schuhbfe6c572022-01-27 20:48:20 -08002610 std::sort(actual_filenames.begin(), actual_filenames.end());
2611 std::sort(pi1_reboot_logfiles_.begin(), pi1_reboot_logfiles_.end());
2612 ASSERT_THAT(actual_filenames,
2613 ::testing::UnorderedElementsAreArray(pi1_reboot_logfiles_));
2614
Austin Schuh72211ae2021-08-05 14:02:30 -07002615 // Confirm that our new oldest timestamps properly update as we reboot and
2616 // rotate.
2617 for (const std::string &file : pi1_reboot_logfiles_) {
2618 std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> log_header =
2619 ReadHeader(file);
2620 CHECK(log_header);
2621 if (log_header->message().has_configuration()) {
2622 continue;
2623 }
2624
Austin Schuh58646e22021-08-23 23:51:46 -07002625 const monotonic_clock::time_point monotonic_start_time =
2626 monotonic_clock::time_point(
2627 chrono::nanoseconds(log_header->message().monotonic_start_time()));
2628 const UUID source_node_boot_uuid = UUID::FromString(
2629 log_header->message().source_node_boot_uuid()->string_view());
2630
Austin Schuh72211ae2021-08-05 14:02:30 -07002631 if (log_header->message().node()->name()->string_view() != "pi1") {
Austin Schuh01f3b392022-01-25 20:03:09 -08002632 // The remote message channel should rotate later and have more parts.
2633 // This only is true on the log files with shared remote messages.
2634 //
2635 // TODO(austin): I'm not the most thrilled with this test pattern... It
2636 // feels brittle in a different way.
Austin Schuhbfe6c572022-01-27 20:48:20 -08002637 if (file.find("aos.message_bridge.RemoteMessage") == std::string::npos ||
2638 !shared()) {
Austin Schuh01f3b392022-01-25 20:03:09 -08002639 switch (log_header->message().parts_index()) {
2640 case 0:
2641 EXPECT_EQ(source_node_boot_uuid, pi2_boot0);
2642 EXPECT_EQ(monotonic_start_time, monotonic_clock::min_time);
2643 break;
2644 case 1:
2645 EXPECT_EQ(source_node_boot_uuid, pi2_boot0);
2646 ASSERT_EQ(monotonic_start_time,
2647 monotonic_clock::epoch() + chrono::seconds(1));
2648 break;
2649 case 2:
2650 EXPECT_EQ(source_node_boot_uuid, pi2_boot1);
2651 EXPECT_EQ(monotonic_start_time, monotonic_clock::min_time) << file;
2652 break;
2653 case 3:
2654 EXPECT_EQ(source_node_boot_uuid, pi2_boot1);
2655 ASSERT_EQ(monotonic_start_time, monotonic_clock::epoch() +
2656 chrono::nanoseconds(2322999462))
2657 << " on " << file;
2658 break;
2659 default:
2660 FAIL();
2661 break;
2662 }
2663 } else {
2664 switch (log_header->message().parts_index()) {
2665 case 0:
Austin Schuhbfe6c572022-01-27 20:48:20 -08002666 case 1:
Austin Schuh01f3b392022-01-25 20:03:09 -08002667 EXPECT_EQ(source_node_boot_uuid, pi2_boot0);
2668 EXPECT_EQ(monotonic_start_time, monotonic_clock::min_time);
2669 break;
Austin Schuhbfe6c572022-01-27 20:48:20 -08002670 case 2:
Austin Schuh01f3b392022-01-25 20:03:09 -08002671 EXPECT_EQ(source_node_boot_uuid, pi2_boot0);
2672 ASSERT_EQ(monotonic_start_time,
2673 monotonic_clock::epoch() + chrono::seconds(1));
2674 break;
Austin Schuh01f3b392022-01-25 20:03:09 -08002675 case 3:
Austin Schuhbfe6c572022-01-27 20:48:20 -08002676 case 4:
Austin Schuh01f3b392022-01-25 20:03:09 -08002677 EXPECT_EQ(source_node_boot_uuid, pi2_boot1);
2678 EXPECT_EQ(monotonic_start_time, monotonic_clock::min_time) << file;
2679 break;
Austin Schuhbfe6c572022-01-27 20:48:20 -08002680 case 5:
Austin Schuh01f3b392022-01-25 20:03:09 -08002681 EXPECT_EQ(source_node_boot_uuid, pi2_boot1);
2682 ASSERT_EQ(monotonic_start_time, monotonic_clock::epoch() +
2683 chrono::nanoseconds(2322999462))
2684 << " on " << file;
2685 break;
2686 default:
2687 FAIL();
2688 break;
2689 }
Austin Schuh58646e22021-08-23 23:51:46 -07002690 }
Austin Schuh72211ae2021-08-05 14:02:30 -07002691 continue;
2692 }
2693 SCOPED_TRACE(file);
2694 SCOPED_TRACE(aos::FlatbufferToJson(
2695 *log_header, {.multi_line = true, .max_vector_size = 100}));
2696 ASSERT_TRUE(log_header->message().has_oldest_remote_monotonic_timestamps());
2697 ASSERT_EQ(
2698 log_header->message().oldest_remote_monotonic_timestamps()->size(), 2u);
2699 EXPECT_EQ(
2700 log_header->message().oldest_remote_monotonic_timestamps()->Get(0),
2701 monotonic_clock::max_time.time_since_epoch().count());
2702 ASSERT_TRUE(log_header->message().has_oldest_local_monotonic_timestamps());
2703 ASSERT_EQ(log_header->message().oldest_local_monotonic_timestamps()->size(),
2704 2u);
2705 EXPECT_EQ(log_header->message().oldest_local_monotonic_timestamps()->Get(0),
2706 monotonic_clock::max_time.time_since_epoch().count());
2707 ASSERT_TRUE(log_header->message()
2708 .has_oldest_remote_unreliable_monotonic_timestamps());
2709 ASSERT_EQ(log_header->message()
2710 .oldest_remote_unreliable_monotonic_timestamps()
2711 ->size(),
2712 2u);
2713 EXPECT_EQ(log_header->message()
2714 .oldest_remote_unreliable_monotonic_timestamps()
2715 ->Get(0),
2716 monotonic_clock::max_time.time_since_epoch().count());
2717 ASSERT_TRUE(log_header->message()
2718 .has_oldest_local_unreliable_monotonic_timestamps());
2719 ASSERT_EQ(log_header->message()
2720 .oldest_local_unreliable_monotonic_timestamps()
2721 ->size(),
2722 2u);
2723 EXPECT_EQ(log_header->message()
2724 .oldest_local_unreliable_monotonic_timestamps()
2725 ->Get(0),
2726 monotonic_clock::max_time.time_since_epoch().count());
2727
2728 const monotonic_clock::time_point oldest_remote_monotonic_timestamps =
2729 monotonic_clock::time_point(chrono::nanoseconds(
2730 log_header->message().oldest_remote_monotonic_timestamps()->Get(
2731 1)));
2732 const monotonic_clock::time_point oldest_local_monotonic_timestamps =
2733 monotonic_clock::time_point(chrono::nanoseconds(
2734 log_header->message().oldest_local_monotonic_timestamps()->Get(1)));
2735 const monotonic_clock::time_point
2736 oldest_remote_unreliable_monotonic_timestamps =
2737 monotonic_clock::time_point(chrono::nanoseconds(
2738 log_header->message()
2739 .oldest_remote_unreliable_monotonic_timestamps()
2740 ->Get(1)));
2741 const monotonic_clock::time_point
2742 oldest_local_unreliable_monotonic_timestamps =
2743 monotonic_clock::time_point(chrono::nanoseconds(
2744 log_header->message()
2745 .oldest_local_unreliable_monotonic_timestamps()
2746 ->Get(1)));
Austin Schuhbfe6c572022-01-27 20:48:20 -08002747 const monotonic_clock::time_point
2748 oldest_remote_reliable_monotonic_timestamps =
2749 monotonic_clock::time_point(chrono::nanoseconds(
2750 log_header->message()
2751 .oldest_remote_reliable_monotonic_timestamps()
2752 ->Get(1)));
2753 const monotonic_clock::time_point
2754 oldest_local_reliable_monotonic_timestamps =
2755 monotonic_clock::time_point(chrono::nanoseconds(
2756 log_header->message()
2757 .oldest_local_reliable_monotonic_timestamps()
2758 ->Get(1)));
Austin Schuhf5f99f32022-02-07 20:05:37 -08002759 const monotonic_clock::time_point
2760 oldest_logger_remote_unreliable_monotonic_timestamps =
2761 monotonic_clock::time_point(chrono::nanoseconds(
2762 log_header->message()
2763 .oldest_logger_remote_unreliable_monotonic_timestamps()
2764 ->Get(0)));
2765 const monotonic_clock::time_point
2766 oldest_logger_local_unreliable_monotonic_timestamps =
2767 monotonic_clock::time_point(chrono::nanoseconds(
2768 log_header->message()
2769 .oldest_logger_local_unreliable_monotonic_timestamps()
2770 ->Get(0)));
2771 EXPECT_EQ(oldest_logger_remote_unreliable_monotonic_timestamps,
2772 monotonic_clock::max_time);
2773 EXPECT_EQ(oldest_logger_local_unreliable_monotonic_timestamps,
2774 monotonic_clock::max_time);
Austin Schuh72211ae2021-08-05 14:02:30 -07002775 switch (log_header->message().parts_index()) {
2776 case 0:
2777 EXPECT_EQ(oldest_remote_monotonic_timestamps,
2778 monotonic_clock::max_time);
2779 EXPECT_EQ(oldest_local_monotonic_timestamps, monotonic_clock::max_time);
2780 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
2781 monotonic_clock::max_time);
2782 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
2783 monotonic_clock::max_time);
Austin Schuhbfe6c572022-01-27 20:48:20 -08002784 EXPECT_EQ(oldest_remote_reliable_monotonic_timestamps,
2785 monotonic_clock::max_time);
2786 EXPECT_EQ(oldest_local_reliable_monotonic_timestamps,
2787 monotonic_clock::max_time);
Austin Schuh72211ae2021-08-05 14:02:30 -07002788 break;
2789 case 1:
2790 EXPECT_EQ(oldest_remote_monotonic_timestamps,
2791 monotonic_clock::time_point(chrono::microseconds(90200)));
2792 EXPECT_EQ(oldest_local_monotonic_timestamps,
2793 monotonic_clock::time_point(chrono::microseconds(90350)));
2794 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
2795 monotonic_clock::time_point(chrono::microseconds(90200)));
2796 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
2797 monotonic_clock::time_point(chrono::microseconds(90350)));
Austin Schuhbfe6c572022-01-27 20:48:20 -08002798 EXPECT_EQ(oldest_remote_reliable_monotonic_timestamps,
2799 monotonic_clock::max_time);
2800 EXPECT_EQ(oldest_local_reliable_monotonic_timestamps,
2801 monotonic_clock::max_time);
Austin Schuh72211ae2021-08-05 14:02:30 -07002802 break;
2803 case 2:
2804 EXPECT_EQ(oldest_remote_monotonic_timestamps,
Austin Schuhbfe6c572022-01-27 20:48:20 -08002805 monotonic_clock::time_point(chrono::microseconds(90200)))
2806 << file;
2807 EXPECT_EQ(oldest_local_monotonic_timestamps,
2808 monotonic_clock::time_point(chrono::microseconds(90350)))
2809 << file;
2810 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
2811 monotonic_clock::time_point(chrono::microseconds(90200)))
2812 << file;
2813 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
2814 monotonic_clock::time_point(chrono::microseconds(90350)))
2815 << file;
2816 EXPECT_EQ(oldest_remote_reliable_monotonic_timestamps,
2817 monotonic_clock::time_point(chrono::microseconds(100000)))
2818 << file;
2819 EXPECT_EQ(oldest_local_reliable_monotonic_timestamps,
2820 monotonic_clock::time_point(chrono::microseconds(100150)))
2821 << file;
2822 break;
2823 case 3:
2824 EXPECT_EQ(oldest_remote_monotonic_timestamps,
Austin Schuh58646e22021-08-23 23:51:46 -07002825 monotonic_clock::time_point(chrono::milliseconds(1323) +
2826 chrono::microseconds(200)));
Austin Schuh72211ae2021-08-05 14:02:30 -07002827 EXPECT_EQ(oldest_local_monotonic_timestamps,
Austin Schuh58646e22021-08-23 23:51:46 -07002828 monotonic_clock::time_point(chrono::microseconds(10100350)));
Austin Schuh72211ae2021-08-05 14:02:30 -07002829 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
Austin Schuh58646e22021-08-23 23:51:46 -07002830 monotonic_clock::time_point(chrono::milliseconds(1323) +
2831 chrono::microseconds(200)));
Austin Schuh72211ae2021-08-05 14:02:30 -07002832 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
2833 monotonic_clock::time_point(chrono::microseconds(10100350)));
Austin Schuhbfe6c572022-01-27 20:48:20 -08002834 EXPECT_EQ(oldest_remote_reliable_monotonic_timestamps,
2835 monotonic_clock::max_time)
2836 << file;
2837 EXPECT_EQ(oldest_local_reliable_monotonic_timestamps,
2838 monotonic_clock::max_time)
2839 << file;
2840 break;
2841 case 4:
2842 EXPECT_EQ(oldest_remote_monotonic_timestamps,
2843 monotonic_clock::time_point(chrono::milliseconds(1323) +
2844 chrono::microseconds(200)));
2845 EXPECT_EQ(oldest_local_monotonic_timestamps,
2846 monotonic_clock::time_point(chrono::microseconds(10100350)));
2847 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
2848 monotonic_clock::time_point(chrono::milliseconds(1323) +
2849 chrono::microseconds(200)));
2850 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
2851 monotonic_clock::time_point(chrono::microseconds(10100350)));
2852 EXPECT_EQ(oldest_remote_reliable_monotonic_timestamps,
2853 monotonic_clock::time_point(chrono::microseconds(1423000)))
2854 << file;
2855 EXPECT_EQ(oldest_local_reliable_monotonic_timestamps,
2856 monotonic_clock::time_point(chrono::microseconds(10200150)))
2857 << file;
Austin Schuh72211ae2021-08-05 14:02:30 -07002858 break;
2859 default:
2860 FAIL();
2861 break;
2862 }
2863 }
2864
Austin Schuh315b96b2020-12-11 21:21:12 -08002865 // Confirm that we refuse to replay logs with missing boot uuids.
Austin Schuh58646e22021-08-23 23:51:46 -07002866 {
2867 LogReader reader(SortParts(pi1_reboot_logfiles_));
Austin Schuh315b96b2020-12-11 21:21:12 -08002868
Austin Schuh58646e22021-08-23 23:51:46 -07002869 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
2870 log_reader_factory.set_send_delay(chrono::microseconds(0));
Austin Schuh315b96b2020-12-11 21:21:12 -08002871
Austin Schuh58646e22021-08-23 23:51:46 -07002872 // This sends out the fetched messages and advances time to the start of
2873 // the log file.
2874 reader.Register(&log_reader_factory);
2875
2876 log_reader_factory.Run();
2877
2878 reader.Deregister();
2879 }
Austin Schuh315b96b2020-12-11 21:21:12 -08002880}
2881
Austin Schuh5e14d842022-01-21 12:02:15 -08002882// Tests that we can sort a log which only has timestamps from the remote
2883// because the local message_bridge_client failed to connect.
2884TEST_P(MultinodeLoggerTest, RemoteRebootOnlyTimestamps) {
2885 const UUID pi1_boot0 = UUID::Random();
2886 const UUID pi2_boot0 = UUID::Random();
2887 const UUID pi2_boot1 = UUID::Random();
2888 {
2889 CHECK_EQ(pi1_index_, 0u);
2890 CHECK_EQ(pi2_index_, 1u);
2891
2892 time_converter_.set_boot_uuid(pi1_index_, 0, pi1_boot0);
2893 time_converter_.set_boot_uuid(pi2_index_, 0, pi2_boot0);
2894 time_converter_.set_boot_uuid(pi2_index_, 1, pi2_boot1);
2895
2896 time_converter_.AddNextTimestamp(
2897 distributed_clock::epoch(),
2898 {BootTimestamp::epoch(), BootTimestamp::epoch()});
2899 const chrono::nanoseconds reboot_time = chrono::milliseconds(10100);
2900 time_converter_.AddNextTimestamp(
2901 distributed_clock::epoch() + reboot_time,
2902 {BootTimestamp::epoch() + reboot_time,
2903 BootTimestamp{
2904 .boot = 1,
2905 .time = monotonic_clock::epoch() + chrono::milliseconds(1323)}});
2906 }
2907 pi2_->Disconnect(pi1_->node());
2908
2909 std::vector<std::string> filenames;
2910 {
2911 LoggerState pi1_logger = MakeLogger(pi1_);
2912
2913 event_loop_factory_.RunFor(chrono::milliseconds(95));
2914 EXPECT_EQ(event_loop_factory_.GetNodeEventLoopFactory("pi1")->boot_uuid(),
2915 pi1_boot0);
2916 EXPECT_EQ(event_loop_factory_.GetNodeEventLoopFactory("pi2")->boot_uuid(),
2917 pi2_boot0);
2918
2919 StartLogger(&pi1_logger);
2920
2921 event_loop_factory_.RunFor(chrono::milliseconds(10000));
2922
2923 VLOG(1) << "Reboot now!";
2924
2925 event_loop_factory_.RunFor(chrono::milliseconds(20000));
2926 EXPECT_EQ(event_loop_factory_.GetNodeEventLoopFactory("pi1")->boot_uuid(),
2927 pi1_boot0);
2928 EXPECT_EQ(event_loop_factory_.GetNodeEventLoopFactory("pi2")->boot_uuid(),
2929 pi2_boot1);
2930 pi1_logger.AppendAllFilenames(&filenames);
2931 }
2932
Austin Schuhbfe6c572022-01-27 20:48:20 -08002933 std::sort(filenames.begin(), filenames.end());
2934
Austin Schuh5e14d842022-01-21 12:02:15 -08002935 // Confirm that our new oldest timestamps properly update as we reboot and
2936 // rotate.
2937 size_t timestamp_file_count = 0;
2938 for (const std::string &file : filenames) {
2939 std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> log_header =
2940 ReadHeader(file);
2941 CHECK(log_header);
2942
2943 if (log_header->message().has_configuration()) {
2944 continue;
2945 }
2946
2947 const monotonic_clock::time_point monotonic_start_time =
2948 monotonic_clock::time_point(
2949 chrono::nanoseconds(log_header->message().monotonic_start_time()));
2950 const UUID source_node_boot_uuid = UUID::FromString(
2951 log_header->message().source_node_boot_uuid()->string_view());
2952
2953 ASSERT_TRUE(log_header->message().has_oldest_remote_monotonic_timestamps());
2954 ASSERT_EQ(
2955 log_header->message().oldest_remote_monotonic_timestamps()->size(), 2u);
2956 ASSERT_TRUE(log_header->message().has_oldest_local_monotonic_timestamps());
2957 ASSERT_EQ(log_header->message().oldest_local_monotonic_timestamps()->size(),
2958 2u);
2959 ASSERT_TRUE(log_header->message()
2960 .has_oldest_remote_unreliable_monotonic_timestamps());
2961 ASSERT_EQ(log_header->message()
2962 .oldest_remote_unreliable_monotonic_timestamps()
2963 ->size(),
2964 2u);
2965 ASSERT_TRUE(log_header->message()
2966 .has_oldest_local_unreliable_monotonic_timestamps());
2967 ASSERT_EQ(log_header->message()
2968 .oldest_local_unreliable_monotonic_timestamps()
2969 ->size(),
2970 2u);
Austin Schuhf5f99f32022-02-07 20:05:37 -08002971 ASSERT_TRUE(log_header->message()
2972 .has_oldest_remote_reliable_monotonic_timestamps());
2973 ASSERT_EQ(log_header->message()
2974 .oldest_remote_reliable_monotonic_timestamps()
2975 ->size(),
2976 2u);
Austin Schuh4b8b45b2022-04-13 17:05:44 -07002977 ASSERT_TRUE(
2978 log_header->message().has_oldest_local_reliable_monotonic_timestamps());
Austin Schuhf5f99f32022-02-07 20:05:37 -08002979 ASSERT_EQ(log_header->message()
2980 .oldest_local_reliable_monotonic_timestamps()
2981 ->size(),
2982 2u);
2983
2984 ASSERT_TRUE(
2985 log_header->message()
2986 .has_oldest_logger_remote_unreliable_monotonic_timestamps());
2987 ASSERT_EQ(log_header->message()
2988 .oldest_logger_remote_unreliable_monotonic_timestamps()
2989 ->size(),
2990 2u);
2991 ASSERT_TRUE(log_header->message()
2992 .has_oldest_logger_local_unreliable_monotonic_timestamps());
2993 ASSERT_EQ(log_header->message()
2994 .oldest_logger_local_unreliable_monotonic_timestamps()
2995 ->size(),
2996 2u);
Austin Schuh5e14d842022-01-21 12:02:15 -08002997
2998 if (log_header->message().node()->name()->string_view() != "pi1") {
2999 ASSERT_TRUE(file.find("aos.message_bridge.RemoteMessage") !=
3000 std::string::npos);
3001
3002 const std::optional<SizePrefixedFlatbufferVector<MessageHeader>> msg =
3003 ReadNthMessage(file, 0);
3004 CHECK(msg);
3005
3006 EXPECT_TRUE(msg->message().has_monotonic_sent_time());
3007 EXPECT_TRUE(msg->message().has_monotonic_remote_time());
3008
3009 const monotonic_clock::time_point
3010 expected_oldest_local_monotonic_timestamps(
3011 chrono::nanoseconds(msg->message().monotonic_sent_time()));
3012 const monotonic_clock::time_point
3013 expected_oldest_remote_monotonic_timestamps(
3014 chrono::nanoseconds(msg->message().monotonic_remote_time()));
Austin Schuhf5f99f32022-02-07 20:05:37 -08003015 const monotonic_clock::time_point
3016 expected_oldest_timestamp_monotonic_timestamps(
3017 chrono::nanoseconds(msg->message().monotonic_timestamp_time()));
Austin Schuh5e14d842022-01-21 12:02:15 -08003018
3019 EXPECT_NE(expected_oldest_local_monotonic_timestamps,
3020 monotonic_clock::min_time);
3021 EXPECT_NE(expected_oldest_remote_monotonic_timestamps,
3022 monotonic_clock::min_time);
Austin Schuhf5f99f32022-02-07 20:05:37 -08003023 EXPECT_NE(expected_oldest_timestamp_monotonic_timestamps,
3024 monotonic_clock::min_time);
Austin Schuh5e14d842022-01-21 12:02:15 -08003025
3026 ++timestamp_file_count;
3027 // Since the log file is from the perspective of the other node,
3028 const monotonic_clock::time_point oldest_remote_monotonic_timestamps =
3029 monotonic_clock::time_point(chrono::nanoseconds(
3030 log_header->message().oldest_remote_monotonic_timestamps()->Get(
3031 0)));
3032 const monotonic_clock::time_point oldest_local_monotonic_timestamps =
3033 monotonic_clock::time_point(chrono::nanoseconds(
3034 log_header->message().oldest_local_monotonic_timestamps()->Get(
3035 0)));
3036 const monotonic_clock::time_point
3037 oldest_remote_unreliable_monotonic_timestamps =
3038 monotonic_clock::time_point(chrono::nanoseconds(
3039 log_header->message()
3040 .oldest_remote_unreliable_monotonic_timestamps()
3041 ->Get(0)));
3042 const monotonic_clock::time_point
3043 oldest_local_unreliable_monotonic_timestamps =
3044 monotonic_clock::time_point(chrono::nanoseconds(
3045 log_header->message()
3046 .oldest_local_unreliable_monotonic_timestamps()
3047 ->Get(0)));
Austin Schuhbfe6c572022-01-27 20:48:20 -08003048 const monotonic_clock::time_point
3049 oldest_remote_reliable_monotonic_timestamps =
3050 monotonic_clock::time_point(chrono::nanoseconds(
3051 log_header->message()
3052 .oldest_remote_reliable_monotonic_timestamps()
3053 ->Get(0)));
3054 const monotonic_clock::time_point
3055 oldest_local_reliable_monotonic_timestamps =
3056 monotonic_clock::time_point(chrono::nanoseconds(
3057 log_header->message()
3058 .oldest_local_reliable_monotonic_timestamps()
3059 ->Get(0)));
Austin Schuhf5f99f32022-02-07 20:05:37 -08003060 const monotonic_clock::time_point
3061 oldest_logger_remote_unreliable_monotonic_timestamps =
3062 monotonic_clock::time_point(chrono::nanoseconds(
3063 log_header->message()
3064 .oldest_logger_remote_unreliable_monotonic_timestamps()
3065 ->Get(1)));
3066 const monotonic_clock::time_point
3067 oldest_logger_local_unreliable_monotonic_timestamps =
3068 monotonic_clock::time_point(chrono::nanoseconds(
3069 log_header->message()
3070 .oldest_logger_local_unreliable_monotonic_timestamps()
3071 ->Get(1)));
Austin Schuh5e14d842022-01-21 12:02:15 -08003072
Austin Schuh01f3b392022-01-25 20:03:09 -08003073 const Channel *channel =
3074 event_loop_factory_.configuration()->channels()->Get(
3075 msg->message().channel_index());
3076 const Connection *connection = configuration::ConnectionToNode(
3077 channel, configuration::GetNode(
3078 event_loop_factory_.configuration(),
3079 log_header->message().node()->name()->string_view()));
3080
3081 const bool reliable = connection->time_to_live() == 0;
3082
Austin Schuhf5f99f32022-02-07 20:05:37 -08003083 SCOPED_TRACE(file);
3084 SCOPED_TRACE(aos::FlatbufferToJson(
3085 *log_header, {.multi_line = true, .max_vector_size = 100}));
3086
Austin Schuh01f3b392022-01-25 20:03:09 -08003087 if (shared()) {
3088 // Confirm that the oldest timestamps match what we expect. Based on
3089 // what we are doing, we know that the oldest time is the first
3090 // message's time.
3091 //
3092 // This makes the test robust to both the split and combined config
3093 // tests.
3094 switch (log_header->message().parts_index()) {
3095 case 0:
Austin Schuh01f3b392022-01-25 20:03:09 -08003096 EXPECT_EQ(oldest_remote_monotonic_timestamps,
3097 expected_oldest_remote_monotonic_timestamps);
3098 EXPECT_EQ(oldest_local_monotonic_timestamps,
3099 expected_oldest_local_monotonic_timestamps);
Austin Schuhf5f99f32022-02-07 20:05:37 -08003100 EXPECT_EQ(oldest_logger_remote_unreliable_monotonic_timestamps,
Austin Schuh4b8b45b2022-04-13 17:05:44 -07003101 expected_oldest_local_monotonic_timestamps)
3102 << file;
Austin Schuhf5f99f32022-02-07 20:05:37 -08003103 EXPECT_EQ(oldest_logger_local_unreliable_monotonic_timestamps,
Austin Schuh4b8b45b2022-04-13 17:05:44 -07003104 expected_oldest_timestamp_monotonic_timestamps)
3105 << file;
Austin Schuhf5f99f32022-02-07 20:05:37 -08003106
Austin Schuh01f3b392022-01-25 20:03:09 -08003107 if (reliable) {
Austin Schuhbfe6c572022-01-27 20:48:20 -08003108 EXPECT_EQ(oldest_remote_reliable_monotonic_timestamps,
3109 expected_oldest_remote_monotonic_timestamps);
3110 EXPECT_EQ(oldest_local_reliable_monotonic_timestamps,
3111 expected_oldest_local_monotonic_timestamps);
Austin Schuh01f3b392022-01-25 20:03:09 -08003112 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
3113 monotonic_clock::max_time);
3114 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
3115 monotonic_clock::max_time);
3116 } else {
Austin Schuhbfe6c572022-01-27 20:48:20 -08003117 EXPECT_EQ(oldest_remote_reliable_monotonic_timestamps,
3118 monotonic_clock::max_time);
3119 EXPECT_EQ(oldest_local_reliable_monotonic_timestamps,
3120 monotonic_clock::max_time);
3121 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
3122 expected_oldest_remote_monotonic_timestamps);
3123 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
3124 expected_oldest_local_monotonic_timestamps);
3125 }
3126 break;
3127 case 1:
3128 EXPECT_EQ(oldest_remote_monotonic_timestamps,
3129 monotonic_clock::epoch() + chrono::nanoseconds(90000000));
3130 EXPECT_EQ(oldest_local_monotonic_timestamps,
3131 monotonic_clock::epoch() + chrono::nanoseconds(90150000));
Austin Schuhf5f99f32022-02-07 20:05:37 -08003132 EXPECT_EQ(oldest_logger_remote_unreliable_monotonic_timestamps,
3133 monotonic_clock::epoch() + chrono::nanoseconds(90150000));
3134 EXPECT_EQ(oldest_logger_local_unreliable_monotonic_timestamps,
3135 monotonic_clock::epoch() + chrono::nanoseconds(90250000));
Austin Schuhbfe6c572022-01-27 20:48:20 -08003136 if (reliable) {
3137 EXPECT_EQ(oldest_remote_reliable_monotonic_timestamps,
3138 expected_oldest_remote_monotonic_timestamps);
3139 EXPECT_EQ(oldest_local_reliable_monotonic_timestamps,
3140 expected_oldest_local_monotonic_timestamps);
3141 EXPECT_EQ(
3142 oldest_remote_unreliable_monotonic_timestamps,
3143 monotonic_clock::epoch() + chrono::nanoseconds(90000000));
3144 EXPECT_EQ(
3145 oldest_local_unreliable_monotonic_timestamps,
3146 monotonic_clock::epoch() + chrono::nanoseconds(90150000));
3147 } else {
3148 EXPECT_EQ(oldest_remote_reliable_monotonic_timestamps,
3149 monotonic_clock::max_time);
3150 EXPECT_EQ(oldest_local_reliable_monotonic_timestamps,
3151 monotonic_clock::max_time);
Austin Schuh01f3b392022-01-25 20:03:09 -08003152 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
3153 expected_oldest_remote_monotonic_timestamps);
3154 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
3155 expected_oldest_local_monotonic_timestamps);
3156 }
3157 break;
3158 case 2:
Austin Schuhbfe6c572022-01-27 20:48:20 -08003159 EXPECT_EQ(
3160 oldest_remote_monotonic_timestamps,
3161 monotonic_clock::epoch() + chrono::nanoseconds(10000000000));
3162 EXPECT_EQ(
3163 oldest_local_monotonic_timestamps,
3164 monotonic_clock::epoch() + chrono::nanoseconds(1323100000));
Austin Schuhf5f99f32022-02-07 20:05:37 -08003165 EXPECT_EQ(oldest_logger_remote_unreliable_monotonic_timestamps,
Austin Schuh4b8b45b2022-04-13 17:05:44 -07003166 expected_oldest_local_monotonic_timestamps)
3167 << file;
Austin Schuhf5f99f32022-02-07 20:05:37 -08003168 EXPECT_EQ(oldest_logger_local_unreliable_monotonic_timestamps,
Austin Schuh4b8b45b2022-04-13 17:05:44 -07003169 expected_oldest_timestamp_monotonic_timestamps)
3170 << file;
Austin Schuhbfe6c572022-01-27 20:48:20 -08003171 if (reliable) {
3172 EXPECT_EQ(oldest_remote_reliable_monotonic_timestamps,
3173 expected_oldest_remote_monotonic_timestamps);
3174 EXPECT_EQ(oldest_local_reliable_monotonic_timestamps,
3175 expected_oldest_local_monotonic_timestamps);
3176 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
3177 monotonic_clock::max_time);
3178 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
3179 monotonic_clock::max_time);
3180 } else {
3181 EXPECT_EQ(oldest_remote_reliable_monotonic_timestamps,
3182 monotonic_clock::max_time);
3183 EXPECT_EQ(oldest_local_reliable_monotonic_timestamps,
3184 monotonic_clock::max_time);
3185 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
3186 expected_oldest_remote_monotonic_timestamps);
3187 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
3188 expected_oldest_local_monotonic_timestamps);
3189 }
3190 break;
3191
3192 case 3:
Austin Schuh01f3b392022-01-25 20:03:09 -08003193 EXPECT_EQ(
3194 oldest_remote_monotonic_timestamps,
3195 monotonic_clock::epoch() + chrono::nanoseconds(10000000000));
3196 EXPECT_EQ(
3197 oldest_local_monotonic_timestamps,
3198 monotonic_clock::epoch() + chrono::nanoseconds(1323100000));
3199 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
3200 expected_oldest_remote_monotonic_timestamps);
3201 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
3202 expected_oldest_local_monotonic_timestamps);
Austin Schuhf5f99f32022-02-07 20:05:37 -08003203 EXPECT_EQ(
3204 oldest_logger_remote_unreliable_monotonic_timestamps,
3205 monotonic_clock::epoch() + chrono::nanoseconds(1323100000));
3206 EXPECT_EQ(
3207 oldest_logger_local_unreliable_monotonic_timestamps,
3208 monotonic_clock::epoch() + chrono::nanoseconds(10100200000));
Austin Schuh01f3b392022-01-25 20:03:09 -08003209 break;
3210 default:
3211 FAIL();
3212 break;
3213 }
3214
3215 switch (log_header->message().parts_index()) {
3216 case 0:
3217 EXPECT_EQ(source_node_boot_uuid, pi2_boot0);
3218 EXPECT_EQ(monotonic_start_time, monotonic_clock::min_time);
3219 break;
3220 case 1:
Austin Schuhbfe6c572022-01-27 20:48:20 -08003221 EXPECT_EQ(source_node_boot_uuid, pi2_boot0);
Austin Schuh01f3b392022-01-25 20:03:09 -08003222 EXPECT_EQ(monotonic_start_time, monotonic_clock::min_time);
3223 break;
3224 case 2:
Austin Schuhbfe6c572022-01-27 20:48:20 -08003225 EXPECT_EQ(source_node_boot_uuid, pi2_boot1);
3226 EXPECT_EQ(monotonic_start_time, monotonic_clock::min_time);
3227 break;
3228 case 3:
Austin Schuh01f3b392022-01-25 20:03:09 -08003229 if (shared()) {
3230 EXPECT_EQ(source_node_boot_uuid, pi2_boot1);
3231 EXPECT_EQ(monotonic_start_time, monotonic_clock::min_time);
Austin Schuh01f3b392022-01-25 20:03:09 -08003232 break;
3233 }
3234 [[fallthrough]];
3235 default:
3236 FAIL();
3237 break;
3238 }
3239 } else {
3240 switch (log_header->message().parts_index()) {
3241 case 0:
Austin Schuhf5f99f32022-02-07 20:05:37 -08003242 if (reliable) {
3243 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
3244 monotonic_clock::max_time);
3245 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
3246 monotonic_clock::max_time);
3247 EXPECT_EQ(
3248 oldest_logger_remote_unreliable_monotonic_timestamps,
3249 monotonic_clock::epoch() + chrono::nanoseconds(100150000))
3250 << file;
3251 EXPECT_EQ(
3252 oldest_logger_local_unreliable_monotonic_timestamps,
3253 monotonic_clock::epoch() + chrono::nanoseconds(100250000))
3254 << file;
3255 } else {
3256 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
3257 expected_oldest_remote_monotonic_timestamps);
3258 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
3259 expected_oldest_local_monotonic_timestamps);
3260 EXPECT_EQ(
3261 oldest_logger_remote_unreliable_monotonic_timestamps,
3262 monotonic_clock::epoch() + chrono::nanoseconds(90150000))
3263 << file;
3264 EXPECT_EQ(
3265 oldest_logger_local_unreliable_monotonic_timestamps,
3266 monotonic_clock::epoch() + chrono::nanoseconds(90250000))
3267 << file;
3268 }
3269 break;
Austin Schuh01f3b392022-01-25 20:03:09 -08003270 case 1:
3271 if (reliable) {
3272 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
3273 monotonic_clock::max_time);
3274 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
3275 monotonic_clock::max_time);
Austin Schuhf5f99f32022-02-07 20:05:37 -08003276 EXPECT_EQ(
3277 oldest_logger_remote_unreliable_monotonic_timestamps,
3278 monotonic_clock::epoch() + chrono::nanoseconds(1323100000));
3279 EXPECT_EQ(
3280 oldest_logger_local_unreliable_monotonic_timestamps,
3281 monotonic_clock::epoch() + chrono::nanoseconds(10100200000));
Austin Schuh01f3b392022-01-25 20:03:09 -08003282 } else {
3283 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
3284 expected_oldest_remote_monotonic_timestamps);
3285 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
3286 expected_oldest_local_monotonic_timestamps);
Austin Schuhf5f99f32022-02-07 20:05:37 -08003287 EXPECT_EQ(
3288 oldest_logger_remote_unreliable_monotonic_timestamps,
3289 monotonic_clock::epoch() + chrono::nanoseconds(1323150000));
3290 EXPECT_EQ(
3291 oldest_logger_local_unreliable_monotonic_timestamps,
3292 monotonic_clock::epoch() + chrono::nanoseconds(10100250000));
Austin Schuh01f3b392022-01-25 20:03:09 -08003293 }
3294 break;
3295 default:
3296 FAIL();
3297 break;
3298 }
3299
3300 switch (log_header->message().parts_index()) {
3301 case 0:
3302 EXPECT_EQ(source_node_boot_uuid, pi2_boot0);
3303 EXPECT_EQ(monotonic_start_time, monotonic_clock::min_time);
3304 break;
3305 case 1:
3306 EXPECT_EQ(source_node_boot_uuid, pi2_boot1);
3307 EXPECT_EQ(monotonic_start_time, monotonic_clock::min_time);
3308 break;
3309 default:
3310 FAIL();
3311 break;
3312 }
Austin Schuh5e14d842022-01-21 12:02:15 -08003313 }
3314
3315 continue;
3316 }
3317 EXPECT_EQ(
3318 log_header->message().oldest_remote_monotonic_timestamps()->Get(0),
3319 monotonic_clock::max_time.time_since_epoch().count());
3320 EXPECT_EQ(log_header->message().oldest_local_monotonic_timestamps()->Get(0),
3321 monotonic_clock::max_time.time_since_epoch().count());
3322 EXPECT_EQ(log_header->message()
3323 .oldest_remote_unreliable_monotonic_timestamps()
3324 ->Get(0),
3325 monotonic_clock::max_time.time_since_epoch().count());
3326 EXPECT_EQ(log_header->message()
3327 .oldest_local_unreliable_monotonic_timestamps()
3328 ->Get(0),
3329 monotonic_clock::max_time.time_since_epoch().count());
3330
3331 const monotonic_clock::time_point oldest_remote_monotonic_timestamps =
3332 monotonic_clock::time_point(chrono::nanoseconds(
3333 log_header->message().oldest_remote_monotonic_timestamps()->Get(
3334 1)));
3335 const monotonic_clock::time_point oldest_local_monotonic_timestamps =
3336 monotonic_clock::time_point(chrono::nanoseconds(
3337 log_header->message().oldest_local_monotonic_timestamps()->Get(1)));
3338 const monotonic_clock::time_point
3339 oldest_remote_unreliable_monotonic_timestamps =
3340 monotonic_clock::time_point(chrono::nanoseconds(
3341 log_header->message()
3342 .oldest_remote_unreliable_monotonic_timestamps()
3343 ->Get(1)));
3344 const monotonic_clock::time_point
3345 oldest_local_unreliable_monotonic_timestamps =
3346 monotonic_clock::time_point(chrono::nanoseconds(
3347 log_header->message()
3348 .oldest_local_unreliable_monotonic_timestamps()
3349 ->Get(1)));
3350 switch (log_header->message().parts_index()) {
3351 case 0:
3352 EXPECT_EQ(oldest_remote_monotonic_timestamps,
3353 monotonic_clock::max_time);
3354 EXPECT_EQ(oldest_local_monotonic_timestamps, monotonic_clock::max_time);
3355 EXPECT_EQ(oldest_remote_unreliable_monotonic_timestamps,
3356 monotonic_clock::max_time);
3357 EXPECT_EQ(oldest_local_unreliable_monotonic_timestamps,
3358 monotonic_clock::max_time);
3359 break;
3360 default:
3361 FAIL();
3362 break;
3363 }
3364 }
3365
Austin Schuh01f3b392022-01-25 20:03:09 -08003366 if (shared()) {
Austin Schuhbfe6c572022-01-27 20:48:20 -08003367 EXPECT_EQ(timestamp_file_count, 4u);
Austin Schuh01f3b392022-01-25 20:03:09 -08003368 } else {
3369 EXPECT_EQ(timestamp_file_count, 4u);
3370 }
Austin Schuh5e14d842022-01-21 12:02:15 -08003371
Austin Schuhe2373e22022-01-21 12:25:17 -08003372 // Confirm that we can actually sort the resulting log and read it.
3373 {
3374 LogReader reader(SortParts(filenames));
3375
3376 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
3377 log_reader_factory.set_send_delay(chrono::microseconds(0));
3378
3379 // This sends out the fetched messages and advances time to the start of
3380 // the log file.
3381 reader.Register(&log_reader_factory);
3382
3383 log_reader_factory.Run();
3384
3385 reader.Deregister();
3386 }
Austin Schuh5e14d842022-01-21 12:02:15 -08003387}
3388
Austin Schuhc9049732020-12-21 22:27:15 -08003389// Tests that we properly handle one direction of message_bridge being
3390// unavailable.
Austin Schuh61e973f2021-02-21 21:43:56 -08003391TEST_P(MultinodeLoggerTest, OneDirectionWithNegativeSlope) {
Austin Schuh58646e22021-08-23 23:51:46 -07003392 pi1_->Disconnect(pi2_->node());
Austin Schuh87dd3832021-01-01 23:07:31 -08003393 time_converter_.AddMonotonic(
Austin Schuh3e20c692021-11-16 20:43:16 -08003394 {BootTimestamp::epoch(), BootTimestamp::epoch() + chrono::seconds(1000)});
Austin Schuh87dd3832021-01-01 23:07:31 -08003395
3396 time_converter_.AddMonotonic(
3397 {chrono::milliseconds(10000),
3398 chrono::milliseconds(10000) - chrono::milliseconds(1)});
Austin Schuhc9049732020-12-21 22:27:15 -08003399 {
3400 LoggerState pi1_logger = MakeLogger(pi1_);
3401
3402 event_loop_factory_.RunFor(chrono::milliseconds(95));
3403
3404 StartLogger(&pi1_logger);
3405
3406 event_loop_factory_.RunFor(chrono::milliseconds(10000));
3407 }
3408
3409 // Confirm that we can parse the result. LogReader has enough internal CHECKs
3410 // to confirm the right thing happened.
3411 ConfirmReadable(pi1_single_direction_logfiles_);
3412}
3413
3414// Tests that we properly handle one direction of message_bridge being
3415// unavailable.
Austin Schuh61e973f2021-02-21 21:43:56 -08003416TEST_P(MultinodeLoggerTest, OneDirectionWithPositiveSlope) {
Austin Schuh58646e22021-08-23 23:51:46 -07003417 pi1_->Disconnect(pi2_->node());
Austin Schuh87dd3832021-01-01 23:07:31 -08003418 time_converter_.AddMonotonic(
Austin Schuh3e20c692021-11-16 20:43:16 -08003419 {BootTimestamp::epoch(), BootTimestamp::epoch() + chrono::seconds(500)});
Austin Schuh87dd3832021-01-01 23:07:31 -08003420
3421 time_converter_.AddMonotonic(
3422 {chrono::milliseconds(10000),
3423 chrono::milliseconds(10000) + chrono::milliseconds(1)});
3424 {
3425 LoggerState pi1_logger = MakeLogger(pi1_);
3426
3427 event_loop_factory_.RunFor(chrono::milliseconds(95));
3428
3429 StartLogger(&pi1_logger);
3430
3431 event_loop_factory_.RunFor(chrono::milliseconds(10000));
3432 }
3433
3434 // Confirm that we can parse the result. LogReader has enough internal CHECKs
3435 // to confirm the right thing happened.
3436 ConfirmReadable(pi1_single_direction_logfiles_);
3437}
3438
Austin Schuhe9f00232021-09-16 18:04:23 -07003439// Tests that we explode if someone passes in a part file twice with a better
3440// error than an out of order error.
3441TEST_P(MultinodeLoggerTest, DuplicateLogFiles) {
3442 time_converter_.AddMonotonic(
Austin Schuh3e20c692021-11-16 20:43:16 -08003443 {BootTimestamp::epoch(), BootTimestamp::epoch() + chrono::seconds(1000)});
Austin Schuhe9f00232021-09-16 18:04:23 -07003444 {
3445 LoggerState pi1_logger = MakeLogger(pi1_);
3446
3447 event_loop_factory_.RunFor(chrono::milliseconds(95));
3448
3449 StartLogger(&pi1_logger);
3450
3451 event_loop_factory_.RunFor(chrono::milliseconds(10000));
3452 }
3453
3454 std::vector<std::string> duplicates;
3455 for (const std::string &f : pi1_single_direction_logfiles_) {
3456 duplicates.emplace_back(f);
3457 duplicates.emplace_back(f);
3458 }
3459 EXPECT_DEATH({ SortParts(duplicates); }, "Found duplicate parts in");
3460}
3461
Austin Schuh87dd3832021-01-01 23:07:31 -08003462// Tests that we properly handle a dead node. Do this by just disconnecting it
3463// and only using one nodes of logs.
Austin Schuh61e973f2021-02-21 21:43:56 -08003464TEST_P(MultinodeLoggerTest, DeadNode) {
Austin Schuh58646e22021-08-23 23:51:46 -07003465 pi1_->Disconnect(pi2_->node());
3466 pi2_->Disconnect(pi1_->node());
Austin Schuh87dd3832021-01-01 23:07:31 -08003467 time_converter_.AddMonotonic(
Austin Schuh3e20c692021-11-16 20:43:16 -08003468 {BootTimestamp::epoch(), BootTimestamp::epoch() + chrono::seconds(1000)});
Austin Schuhc9049732020-12-21 22:27:15 -08003469 {
3470 LoggerState pi1_logger = MakeLogger(pi1_);
3471
3472 event_loop_factory_.RunFor(chrono::milliseconds(95));
3473
3474 StartLogger(&pi1_logger);
3475
3476 event_loop_factory_.RunFor(chrono::milliseconds(10000));
3477 }
3478
3479 // Confirm that we can parse the result. LogReader has enough internal CHECKs
3480 // to confirm the right thing happened.
Austin Schuh510dc622021-08-06 18:47:30 -07003481 ConfirmReadable(MakePi1DeadNodeLogfiles());
Austin Schuhc9049732020-12-21 22:27:15 -08003482}
3483
Austin Schuhcdd90272021-03-15 12:46:16 -07003484constexpr std::string_view kCombinedConfigSha1(
Sarah Newman45a64df2022-04-11 19:33:46 -07003485 "158a244107a7dc637fc5934ac161cb9e6c26195930fd8f82bb351c3ad7cce349");
Austin Schuhcdd90272021-03-15 12:46:16 -07003486constexpr std::string_view kSplitConfigSha1(
Sarah Newman45a64df2022-04-11 19:33:46 -07003487 "c73aa7913a9e116ee0a793d8280fac170b7eeea8e7350f45c6ac5bfc4ab018e1");
Austin Schuhcdd90272021-03-15 12:46:16 -07003488
James Kuszmaulf4bf9fe2021-05-10 22:58:24 -07003489INSTANTIATE_TEST_SUITE_P(
Austin Schuh61e973f2021-02-21 21:43:56 -08003490 All, MultinodeLoggerTest,
James Kuszmauldd0a5042021-10-28 23:38:04 -07003491 ::testing::Combine(::testing::Values(
3492 ConfigParams{
3493 "multinode_pingpong_combined_config.json", true,
3494 kCombinedConfigSha1},
3495 ConfigParams{"multinode_pingpong_split_config.json",
3496 false, kSplitConfigSha1}),
3497 ::testing::ValuesIn(SupportedCompressionAlgorithms())));
Austin Schuh61e973f2021-02-21 21:43:56 -08003498
James Kuszmaulf4bf9fe2021-05-10 22:58:24 -07003499INSTANTIATE_TEST_SUITE_P(
Austin Schuh61e973f2021-02-21 21:43:56 -08003500 All, MultinodeLoggerDeathTest,
James Kuszmauldd0a5042021-10-28 23:38:04 -07003501 ::testing::Combine(::testing::Values(
3502 ConfigParams{
3503 "multinode_pingpong_combined_config.json", true,
3504 kCombinedConfigSha1},
3505 ConfigParams{"multinode_pingpong_split_config.json",
3506 false, kSplitConfigSha1}),
3507 ::testing::ValuesIn(SupportedCompressionAlgorithms())));
Austin Schuh61e973f2021-02-21 21:43:56 -08003508
Austin Schuh5b728b72021-06-16 14:57:15 -07003509// Tests that we can relog with a different config. This makes most sense when
3510// you are trying to edit a log and want to use channel renaming + the original
3511// config in the new log.
3512TEST_P(MultinodeLoggerTest, LogDifferentConfig) {
3513 time_converter_.StartEqual();
3514 {
3515 LoggerState pi1_logger = MakeLogger(pi1_);
3516 LoggerState pi2_logger = MakeLogger(pi2_);
3517
3518 event_loop_factory_.RunFor(chrono::milliseconds(95));
3519
3520 StartLogger(&pi1_logger);
3521 StartLogger(&pi2_logger);
3522
3523 event_loop_factory_.RunFor(chrono::milliseconds(20000));
3524 }
3525
3526 LogReader reader(SortParts(logfiles_));
3527 reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original");
3528
3529 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
3530 log_reader_factory.set_send_delay(chrono::microseconds(0));
3531
3532 // This sends out the fetched messages and advances time to the start of the
3533 // log file.
3534 reader.Register(&log_reader_factory);
3535
3536 const Node *pi1 =
3537 configuration::GetNode(log_reader_factory.configuration(), "pi1");
3538 const Node *pi2 =
3539 configuration::GetNode(log_reader_factory.configuration(), "pi2");
3540
3541 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi1) << " pi1";
3542 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi2) << " pi2";
3543 LOG(INFO) << "now pi1 "
3544 << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now();
3545 LOG(INFO) << "now pi2 "
3546 << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now();
3547
3548 EXPECT_THAT(reader.LoggedNodes(),
3549 ::testing::ElementsAre(
3550 configuration::GetNode(reader.logged_configuration(), pi1),
3551 configuration::GetNode(reader.logged_configuration(), pi2)));
3552
3553 reader.event_loop_factory()->set_send_delay(chrono::microseconds(0));
3554
3555 // And confirm we can re-create a log again, while checking the contents.
3556 std::vector<std::string> log_files;
3557 {
3558 LoggerState pi1_logger =
Austin Schuh58646e22021-08-23 23:51:46 -07003559 MakeLogger(log_reader_factory.GetNodeEventLoopFactory("pi1"),
Austin Schuh5b728b72021-06-16 14:57:15 -07003560 &log_reader_factory, reader.logged_configuration());
3561 LoggerState pi2_logger =
Austin Schuh58646e22021-08-23 23:51:46 -07003562 MakeLogger(log_reader_factory.GetNodeEventLoopFactory("pi2"),
Austin Schuh5b728b72021-06-16 14:57:15 -07003563 &log_reader_factory, reader.logged_configuration());
3564
Austin Schuh3e20c692021-11-16 20:43:16 -08003565 pi1_logger.StartLogger(tmp_dir_ + "/relogged1");
3566 pi2_logger.StartLogger(tmp_dir_ + "/relogged2");
Austin Schuh5b728b72021-06-16 14:57:15 -07003567
3568 log_reader_factory.Run();
3569
3570 for (auto &x : pi1_logger.log_namer->all_filenames()) {
3571 log_files.emplace_back(absl::StrCat(tmp_dir_, "/relogged1_", x));
3572 }
3573 for (auto &x : pi2_logger.log_namer->all_filenames()) {
3574 log_files.emplace_back(absl::StrCat(tmp_dir_, "/relogged2_", x));
3575 }
3576 }
3577
3578 reader.Deregister();
3579
3580 // And verify that we can run the LogReader over the relogged files without
3581 // hitting any fatal errors.
3582 {
3583 LogReader relogged_reader(SortParts(log_files));
3584 relogged_reader.Register();
3585
3586 relogged_reader.event_loop_factory()->Run();
3587 }
3588}
Austin Schuha04efed2021-01-24 18:04:20 -08003589
Austin Schuh3e20c692021-11-16 20:43:16 -08003590// Tests that we properly replay a log where the start time for a node is before
3591// any data on the node. This can happen if the logger starts before data is
3592// published. While the scenario below is a bit convoluted, we have seen logs
3593// like this generated out in the wild.
3594TEST(MultinodeRebootLoggerTest, StartTimeBeforeData) {
3595 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
3596 aos::configuration::ReadConfig(ArtifactPath(
3597 "aos/events/logging/multinode_pingpong_split3_config.json"));
3598 message_bridge::TestingTimeConverter time_converter(
3599 configuration::NodesCount(&config.message()));
3600 SimulatedEventLoopFactory event_loop_factory(&config.message());
3601 event_loop_factory.SetTimeConverter(&time_converter);
3602 NodeEventLoopFactory *const pi1 =
3603 event_loop_factory.GetNodeEventLoopFactory("pi1");
3604 const size_t pi1_index = configuration::GetNodeIndex(
3605 event_loop_factory.configuration(), pi1->node());
3606 NodeEventLoopFactory *const pi2 =
3607 event_loop_factory.GetNodeEventLoopFactory("pi2");
3608 const size_t pi2_index = configuration::GetNodeIndex(
3609 event_loop_factory.configuration(), pi2->node());
3610 NodeEventLoopFactory *const pi3 =
3611 event_loop_factory.GetNodeEventLoopFactory("pi3");
3612 const size_t pi3_index = configuration::GetNodeIndex(
3613 event_loop_factory.configuration(), pi3->node());
3614
3615 const std::string kLogfile1_1 =
3616 aos::testing::TestTmpDir() + "/multi_logfile1/";
3617 const std::string kLogfile2_1 =
3618 aos::testing::TestTmpDir() + "/multi_logfile2.1/";
3619 const std::string kLogfile2_2 =
3620 aos::testing::TestTmpDir() + "/multi_logfile2.2/";
3621 const std::string kLogfile3_1 =
3622 aos::testing::TestTmpDir() + "/multi_logfile3/";
3623 util::UnlinkRecursive(kLogfile1_1);
3624 util::UnlinkRecursive(kLogfile2_1);
3625 util::UnlinkRecursive(kLogfile2_2);
3626 util::UnlinkRecursive(kLogfile3_1);
3627 const UUID pi1_boot0 = UUID::Random();
3628 const UUID pi2_boot0 = UUID::Random();
3629 const UUID pi2_boot1 = UUID::Random();
3630 const UUID pi3_boot0 = UUID::Random();
3631 {
3632 CHECK_EQ(pi1_index, 0u);
3633 CHECK_EQ(pi2_index, 1u);
3634 CHECK_EQ(pi3_index, 2u);
3635
3636 time_converter.set_boot_uuid(pi1_index, 0, pi1_boot0);
3637 time_converter.set_boot_uuid(pi2_index, 0, pi2_boot0);
3638 time_converter.set_boot_uuid(pi2_index, 1, pi2_boot1);
3639 time_converter.set_boot_uuid(pi3_index, 0, pi3_boot0);
3640
3641 time_converter.AddNextTimestamp(
3642 distributed_clock::epoch(),
3643 {BootTimestamp::epoch(), BootTimestamp::epoch(),
3644 BootTimestamp::epoch()});
3645 const chrono::nanoseconds reboot_time = chrono::milliseconds(20000);
3646 time_converter.AddNextTimestamp(
3647 distributed_clock::epoch() + reboot_time,
3648 {BootTimestamp::epoch() + reboot_time,
3649 BootTimestamp{
3650 .boot = 1,
3651 .time = monotonic_clock::epoch() + chrono::milliseconds(1323)},
3652 BootTimestamp::epoch() + reboot_time});
3653 }
3654
3655 // Make everything perfectly quiet.
3656 event_loop_factory.SkipTimingReport();
3657 event_loop_factory.DisableStatistics();
3658
3659 std::vector<std::string> filenames;
3660 {
3661 LoggerState pi1_logger = LoggerState::MakeLogger(
3662 pi1, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
3663 LoggerState pi3_logger = LoggerState::MakeLogger(
3664 pi3, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
3665 {
3666 // And now start the logger.
3667 LoggerState pi2_logger = LoggerState::MakeLogger(
3668 pi2, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
3669
3670 event_loop_factory.RunFor(chrono::milliseconds(1000));
3671
3672 pi1_logger.StartLogger(kLogfile1_1);
3673 pi3_logger.StartLogger(kLogfile3_1);
3674 pi2_logger.StartLogger(kLogfile2_1);
3675
3676 event_loop_factory.RunFor(chrono::milliseconds(10000));
3677
3678 // Now that we've got a start time in the past, turn on data.
3679 event_loop_factory.EnableStatistics();
3680 std::unique_ptr<aos::EventLoop> ping_event_loop =
3681 pi1->MakeEventLoop("ping");
3682 Ping ping(ping_event_loop.get());
3683
3684 pi2->AlwaysStart<Pong>("pong");
3685
3686 event_loop_factory.RunFor(chrono::milliseconds(3000));
3687
3688 pi2_logger.AppendAllFilenames(&filenames);
3689
3690 // Stop logging on pi2 before rebooting and completely shut off all
3691 // messages on pi2.
3692 pi2->DisableStatistics();
3693 pi1->Disconnect(pi2->node());
3694 pi2->Disconnect(pi1->node());
3695 }
3696 event_loop_factory.RunFor(chrono::milliseconds(7000));
3697 // pi2 now reboots.
3698 {
3699 event_loop_factory.RunFor(chrono::milliseconds(1000));
3700
3701 // Start logging again on pi2 after it is up.
3702 LoggerState pi2_logger = LoggerState::MakeLogger(
3703 pi2, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
3704 pi2_logger.StartLogger(kLogfile2_2);
3705
3706 event_loop_factory.RunFor(chrono::milliseconds(10000));
3707 // And, now that we have a start time in the log, turn data back on.
3708 pi2->EnableStatistics();
3709 pi1->Connect(pi2->node());
3710 pi2->Connect(pi1->node());
3711
3712 pi2->AlwaysStart<Pong>("pong");
3713 std::unique_ptr<aos::EventLoop> ping_event_loop =
3714 pi1->MakeEventLoop("ping");
3715 Ping ping(ping_event_loop.get());
3716
3717 event_loop_factory.RunFor(chrono::milliseconds(3000));
3718
3719 pi2_logger.AppendAllFilenames(&filenames);
3720 }
3721
3722 pi1_logger.AppendAllFilenames(&filenames);
3723 pi3_logger.AppendAllFilenames(&filenames);
3724 }
3725
3726 // Confirm that we can parse the result. LogReader has enough internal CHECKs
3727 // to confirm the right thing happened.
3728 const std::vector<LogFile> sorted_parts = SortParts(filenames);
Austin Schuhe33c08d2022-02-03 18:15:21 -08003729 auto result = ConfirmReadable(filenames);
3730 EXPECT_THAT(result[0].first, ::testing::ElementsAre(realtime_clock::epoch() +
3731 chrono::seconds(1)));
3732 EXPECT_THAT(result[0].second,
3733 ::testing::ElementsAre(realtime_clock::epoch() +
3734 chrono::microseconds(34990350)));
3735
3736 EXPECT_THAT(result[1].first,
3737 ::testing::ElementsAre(
3738 realtime_clock::epoch() + chrono::seconds(1),
3739 realtime_clock::epoch() + chrono::microseconds(3323000)));
3740 EXPECT_THAT(result[1].second,
3741 ::testing::ElementsAre(
3742 realtime_clock::epoch() + chrono::microseconds(13990200),
3743 realtime_clock::epoch() + chrono::microseconds(16313200)));
3744
3745 EXPECT_THAT(result[2].first, ::testing::ElementsAre(realtime_clock::epoch() +
3746 chrono::seconds(1)));
3747 EXPECT_THAT(result[2].second,
3748 ::testing::ElementsAre(realtime_clock::epoch() +
3749 chrono::microseconds(34900150)));
Austin Schuh3e20c692021-11-16 20:43:16 -08003750}
3751
Austin Schuh5dd22842021-11-17 16:09:39 -08003752// Tests that local data before remote data after reboot is properly replayed.
3753// We only trigger a reboot in the timestamp interpolation function when solving
3754// the timestamp problem when we actually have a point in the function. This
3755// originally only happened when a point passes the noncausal filter. At the
3756// start of time for the second boot, if we aren't careful, we will have
3757// messages which need to be published at times before the boot. This happens
3758// when a local message is in the log before a forwarded message, so there is no
3759// point in the interpolation function. This delays the reboot. So, we need to
3760// recreate that situation and make sure it doesn't come back.
Austin Schuhbfe6c572022-01-27 20:48:20 -08003761TEST(MultinodeRebootLoggerTest,
3762 LocalMessageBeforeRemoteBeforeStartAfterReboot) {
Austin Schuh5dd22842021-11-17 16:09:39 -08003763 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
3764 aos::configuration::ReadConfig(ArtifactPath(
3765 "aos/events/logging/multinode_pingpong_split3_config.json"));
3766 message_bridge::TestingTimeConverter time_converter(
3767 configuration::NodesCount(&config.message()));
3768 SimulatedEventLoopFactory event_loop_factory(&config.message());
3769 event_loop_factory.SetTimeConverter(&time_converter);
3770 NodeEventLoopFactory *const pi1 =
3771 event_loop_factory.GetNodeEventLoopFactory("pi1");
3772 const size_t pi1_index = configuration::GetNodeIndex(
3773 event_loop_factory.configuration(), pi1->node());
3774 NodeEventLoopFactory *const pi2 =
3775 event_loop_factory.GetNodeEventLoopFactory("pi2");
3776 const size_t pi2_index = configuration::GetNodeIndex(
3777 event_loop_factory.configuration(), pi2->node());
3778 NodeEventLoopFactory *const pi3 =
3779 event_loop_factory.GetNodeEventLoopFactory("pi3");
3780 const size_t pi3_index = configuration::GetNodeIndex(
3781 event_loop_factory.configuration(), pi3->node());
3782
3783 const std::string kLogfile1_1 =
3784 aos::testing::TestTmpDir() + "/multi_logfile1/";
3785 const std::string kLogfile2_1 =
3786 aos::testing::TestTmpDir() + "/multi_logfile2.1/";
3787 const std::string kLogfile2_2 =
3788 aos::testing::TestTmpDir() + "/multi_logfile2.2/";
3789 const std::string kLogfile3_1 =
3790 aos::testing::TestTmpDir() + "/multi_logfile3/";
3791 util::UnlinkRecursive(kLogfile1_1);
3792 util::UnlinkRecursive(kLogfile2_1);
3793 util::UnlinkRecursive(kLogfile2_2);
3794 util::UnlinkRecursive(kLogfile3_1);
3795 const UUID pi1_boot0 = UUID::Random();
3796 const UUID pi2_boot0 = UUID::Random();
3797 const UUID pi2_boot1 = UUID::Random();
3798 const UUID pi3_boot0 = UUID::Random();
3799 {
3800 CHECK_EQ(pi1_index, 0u);
3801 CHECK_EQ(pi2_index, 1u);
3802 CHECK_EQ(pi3_index, 2u);
3803
3804 time_converter.set_boot_uuid(pi1_index, 0, pi1_boot0);
3805 time_converter.set_boot_uuid(pi2_index, 0, pi2_boot0);
3806 time_converter.set_boot_uuid(pi2_index, 1, pi2_boot1);
3807 time_converter.set_boot_uuid(pi3_index, 0, pi3_boot0);
3808
3809 time_converter.AddNextTimestamp(
3810 distributed_clock::epoch(),
3811 {BootTimestamp::epoch(), BootTimestamp::epoch(),
3812 BootTimestamp::epoch()});
3813 const chrono::nanoseconds reboot_time = chrono::milliseconds(5000);
3814 time_converter.AddNextTimestamp(
3815 distributed_clock::epoch() + reboot_time,
3816 {BootTimestamp::epoch() + reboot_time,
Austin Schuhbfe6c572022-01-27 20:48:20 -08003817 BootTimestamp{.boot = 1,
3818 .time = monotonic_clock::epoch() + reboot_time +
3819 chrono::seconds(100)},
Austin Schuh5dd22842021-11-17 16:09:39 -08003820 BootTimestamp::epoch() + reboot_time});
3821 }
3822
3823 std::vector<std::string> filenames;
3824 {
3825 LoggerState pi1_logger = LoggerState::MakeLogger(
3826 pi1, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
3827 LoggerState pi3_logger = LoggerState::MakeLogger(
3828 pi3, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
3829 {
3830 // And now start the logger.
3831 LoggerState pi2_logger = LoggerState::MakeLogger(
3832 pi2, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
3833
3834 pi1_logger.StartLogger(kLogfile1_1);
3835 pi3_logger.StartLogger(kLogfile3_1);
3836 pi2_logger.StartLogger(kLogfile2_1);
3837
3838 event_loop_factory.RunFor(chrono::milliseconds(1005));
3839
3840 // Now that we've got a start time in the past, turn on data.
3841 std::unique_ptr<aos::EventLoop> ping_event_loop =
3842 pi1->MakeEventLoop("ping");
3843 Ping ping(ping_event_loop.get());
3844
3845 pi2->AlwaysStart<Pong>("pong");
3846
3847 event_loop_factory.RunFor(chrono::milliseconds(3000));
3848
3849 pi2_logger.AppendAllFilenames(&filenames);
3850
3851 // Disable any remote messages on pi2.
3852 pi1->Disconnect(pi2->node());
3853 pi2->Disconnect(pi1->node());
3854 }
3855 event_loop_factory.RunFor(chrono::milliseconds(995));
3856 // pi2 now reboots at 5 seconds.
3857 {
3858 event_loop_factory.RunFor(chrono::milliseconds(1000));
3859
3860 // Make local stuff happen before we start logging and connect the remote.
3861 pi2->AlwaysStart<Pong>("pong");
3862 std::unique_ptr<aos::EventLoop> ping_event_loop =
3863 pi1->MakeEventLoop("ping");
3864 Ping ping(ping_event_loop.get());
3865 event_loop_factory.RunFor(chrono::milliseconds(1005));
3866
3867 // Start logging again on pi2 after it is up.
3868 LoggerState pi2_logger = LoggerState::MakeLogger(
3869 pi2, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
3870 pi2_logger.StartLogger(kLogfile2_2);
3871
3872 // And allow remote messages now that we have some local ones.
3873 pi1->Connect(pi2->node());
3874 pi2->Connect(pi1->node());
3875
3876 event_loop_factory.RunFor(chrono::milliseconds(1000));
3877
3878 event_loop_factory.RunFor(chrono::milliseconds(3000));
3879
3880 pi2_logger.AppendAllFilenames(&filenames);
3881 }
3882
3883 pi1_logger.AppendAllFilenames(&filenames);
3884 pi3_logger.AppendAllFilenames(&filenames);
3885 }
3886
3887 // Confirm that we can parse the result. LogReader has enough internal CHECKs
3888 // to confirm the right thing happened.
3889 const std::vector<LogFile> sorted_parts = SortParts(filenames);
Austin Schuhe33c08d2022-02-03 18:15:21 -08003890 auto result = ConfirmReadable(filenames);
3891
3892 EXPECT_THAT(result[0].first, ::testing::ElementsAre(realtime_clock::epoch()));
3893 EXPECT_THAT(result[0].second,
3894 ::testing::ElementsAre(realtime_clock::epoch() +
3895 chrono::microseconds(11000350)));
3896
3897 EXPECT_THAT(result[1].first,
3898 ::testing::ElementsAre(
3899 realtime_clock::epoch(),
3900 realtime_clock::epoch() + chrono::microseconds(107005000)));
3901 EXPECT_THAT(result[1].second,
3902 ::testing::ElementsAre(
3903 realtime_clock::epoch() + chrono::microseconds(4000150),
3904 realtime_clock::epoch() + chrono::microseconds(111000200)));
3905
3906 EXPECT_THAT(result[2].first, ::testing::ElementsAre(realtime_clock::epoch()));
3907 EXPECT_THAT(result[2].second,
3908 ::testing::ElementsAre(realtime_clock::epoch() +
3909 chrono::microseconds(11000150)));
3910
3911 auto start_stop_result = ConfirmReadable(
3912 filenames, realtime_clock::epoch() + chrono::milliseconds(2000),
3913 realtime_clock::epoch() + chrono::milliseconds(3000));
3914
Austin Schuh4b8b45b2022-04-13 17:05:44 -07003915 EXPECT_THAT(
3916 start_stop_result[0].first,
3917 ::testing::ElementsAre(realtime_clock::epoch() + chrono::seconds(2)));
3918 EXPECT_THAT(
3919 start_stop_result[0].second,
3920 ::testing::ElementsAre(realtime_clock::epoch() + chrono::seconds(3)));
3921 EXPECT_THAT(
3922 start_stop_result[1].first,
3923 ::testing::ElementsAre(realtime_clock::epoch() + chrono::seconds(2)));
3924 EXPECT_THAT(
3925 start_stop_result[1].second,
3926 ::testing::ElementsAre(realtime_clock::epoch() + chrono::seconds(3)));
3927 EXPECT_THAT(
3928 start_stop_result[2].first,
3929 ::testing::ElementsAre(realtime_clock::epoch() + chrono::seconds(2)));
3930 EXPECT_THAT(
3931 start_stop_result[2].second,
3932 ::testing::ElementsAre(realtime_clock::epoch() + chrono::seconds(3)));
Austin Schuh5dd22842021-11-17 16:09:39 -08003933}
3934
Austin Schuhe33c08d2022-02-03 18:15:21 -08003935// Tests that setting the start and stop flags across a reboot works as
3936// expected.
3937TEST(MultinodeRebootLoggerTest, RebootStartStopTimes) {
3938 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
3939 aos::configuration::ReadConfig(ArtifactPath(
3940 "aos/events/logging/multinode_pingpong_split3_config.json"));
3941 message_bridge::TestingTimeConverter time_converter(
3942 configuration::NodesCount(&config.message()));
3943 SimulatedEventLoopFactory event_loop_factory(&config.message());
3944 event_loop_factory.SetTimeConverter(&time_converter);
3945 NodeEventLoopFactory *const pi1 =
3946 event_loop_factory.GetNodeEventLoopFactory("pi1");
3947 const size_t pi1_index = configuration::GetNodeIndex(
3948 event_loop_factory.configuration(), pi1->node());
3949 NodeEventLoopFactory *const pi2 =
3950 event_loop_factory.GetNodeEventLoopFactory("pi2");
3951 const size_t pi2_index = configuration::GetNodeIndex(
3952 event_loop_factory.configuration(), pi2->node());
3953 NodeEventLoopFactory *const pi3 =
3954 event_loop_factory.GetNodeEventLoopFactory("pi3");
3955 const size_t pi3_index = configuration::GetNodeIndex(
3956 event_loop_factory.configuration(), pi3->node());
3957
3958 const std::string kLogfile1_1 =
3959 aos::testing::TestTmpDir() + "/multi_logfile1/";
3960 const std::string kLogfile2_1 =
3961 aos::testing::TestTmpDir() + "/multi_logfile2.1/";
3962 const std::string kLogfile2_2 =
3963 aos::testing::TestTmpDir() + "/multi_logfile2.2/";
3964 const std::string kLogfile3_1 =
3965 aos::testing::TestTmpDir() + "/multi_logfile3/";
3966 util::UnlinkRecursive(kLogfile1_1);
3967 util::UnlinkRecursive(kLogfile2_1);
3968 util::UnlinkRecursive(kLogfile2_2);
3969 util::UnlinkRecursive(kLogfile3_1);
3970 {
3971 CHECK_EQ(pi1_index, 0u);
3972 CHECK_EQ(pi2_index, 1u);
3973 CHECK_EQ(pi3_index, 2u);
3974
3975 time_converter.AddNextTimestamp(
3976 distributed_clock::epoch(),
3977 {BootTimestamp::epoch(), BootTimestamp::epoch(),
3978 BootTimestamp::epoch()});
3979 const chrono::nanoseconds reboot_time = chrono::milliseconds(5000);
3980 time_converter.AddNextTimestamp(
3981 distributed_clock::epoch() + reboot_time,
3982 {BootTimestamp::epoch() + reboot_time,
3983 BootTimestamp{.boot = 1,
3984 .time = monotonic_clock::epoch() + reboot_time},
3985 BootTimestamp::epoch() + reboot_time});
3986 }
3987
3988 std::vector<std::string> filenames;
3989 {
3990 LoggerState pi1_logger = LoggerState::MakeLogger(
3991 pi1, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
3992 LoggerState pi3_logger = LoggerState::MakeLogger(
3993 pi3, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
3994 {
3995 // And now start the logger.
3996 LoggerState pi2_logger = LoggerState::MakeLogger(
3997 pi2, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
3998
3999 pi1_logger.StartLogger(kLogfile1_1);
4000 pi3_logger.StartLogger(kLogfile3_1);
4001 pi2_logger.StartLogger(kLogfile2_1);
4002
4003 event_loop_factory.RunFor(chrono::milliseconds(1005));
4004
4005 // Now that we've got a start time in the past, turn on data.
4006 std::unique_ptr<aos::EventLoop> ping_event_loop =
4007 pi1->MakeEventLoop("ping");
4008 Ping ping(ping_event_loop.get());
4009
4010 pi2->AlwaysStart<Pong>("pong");
4011
4012 event_loop_factory.RunFor(chrono::milliseconds(3000));
4013
4014 pi2_logger.AppendAllFilenames(&filenames);
4015 }
4016 event_loop_factory.RunFor(chrono::milliseconds(995));
4017 // pi2 now reboots at 5 seconds.
4018 {
4019 event_loop_factory.RunFor(chrono::milliseconds(1000));
4020
4021 // Make local stuff happen before we start logging and connect the remote.
4022 pi2->AlwaysStart<Pong>("pong");
4023 std::unique_ptr<aos::EventLoop> ping_event_loop =
4024 pi1->MakeEventLoop("ping");
4025 Ping ping(ping_event_loop.get());
4026 event_loop_factory.RunFor(chrono::milliseconds(5));
4027
4028 // Start logging again on pi2 after it is up.
4029 LoggerState pi2_logger = LoggerState::MakeLogger(
4030 pi2, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
4031 pi2_logger.StartLogger(kLogfile2_2);
4032
4033 event_loop_factory.RunFor(chrono::milliseconds(5000));
4034
4035 pi2_logger.AppendAllFilenames(&filenames);
4036 }
4037
4038 pi1_logger.AppendAllFilenames(&filenames);
4039 pi3_logger.AppendAllFilenames(&filenames);
4040 }
4041
4042 const std::vector<LogFile> sorted_parts = SortParts(filenames);
4043 auto result = ConfirmReadable(filenames);
4044
4045 EXPECT_THAT(result[0].first, ::testing::ElementsAre(realtime_clock::epoch()));
4046 EXPECT_THAT(result[0].second,
4047 ::testing::ElementsAre(realtime_clock::epoch() +
4048 chrono::microseconds(11000350)));
4049
4050 EXPECT_THAT(result[1].first,
4051 ::testing::ElementsAre(
4052 realtime_clock::epoch(),
4053 realtime_clock::epoch() + chrono::microseconds(6005000)));
4054 EXPECT_THAT(result[1].second,
4055 ::testing::ElementsAre(
4056 realtime_clock::epoch() + chrono::microseconds(4900150),
4057 realtime_clock::epoch() + chrono::microseconds(11000200)));
4058
4059 EXPECT_THAT(result[2].first, ::testing::ElementsAre(realtime_clock::epoch()));
4060 EXPECT_THAT(result[2].second,
4061 ::testing::ElementsAre(realtime_clock::epoch() +
4062 chrono::microseconds(11000150)));
4063
4064 // Confirm we observed the correct start and stop times. We should see the
4065 // reboot here.
4066 auto start_stop_result = ConfirmReadable(
4067 filenames, realtime_clock::epoch() + chrono::milliseconds(2000),
4068 realtime_clock::epoch() + chrono::milliseconds(8000));
4069
4070 EXPECT_THAT(
4071 start_stop_result[0].first,
4072 ::testing::ElementsAre(realtime_clock::epoch() + chrono::seconds(2)));
4073 EXPECT_THAT(
4074 start_stop_result[0].second,
4075 ::testing::ElementsAre(realtime_clock::epoch() + chrono::seconds(8)));
4076 EXPECT_THAT(start_stop_result[1].first,
4077 ::testing::ElementsAre(
4078 realtime_clock::epoch() + chrono::seconds(2),
4079 realtime_clock::epoch() + chrono::microseconds(6005000)));
4080 EXPECT_THAT(start_stop_result[1].second,
4081 ::testing::ElementsAre(
4082 realtime_clock::epoch() + chrono::microseconds(4900150),
4083 realtime_clock::epoch() + chrono::seconds(8)));
4084 EXPECT_THAT(
4085 start_stop_result[2].first,
4086 ::testing::ElementsAre(realtime_clock::epoch() + chrono::seconds(2)));
4087 EXPECT_THAT(
4088 start_stop_result[2].second,
4089 ::testing::ElementsAre(realtime_clock::epoch() + chrono::seconds(8)));
4090}
Austin Schuh5dd22842021-11-17 16:09:39 -08004091
Austin Schuh5c770fa2022-03-11 06:57:22 -08004092// Tests that we properly handle one direction being down.
4093TEST(MissingDirectionTest, OneDirection) {
4094 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
4095 aos::configuration::ReadConfig(ArtifactPath(
4096 "aos/events/logging/multinode_pingpong_split4_config.json"));
4097 message_bridge::TestingTimeConverter time_converter(
4098 configuration::NodesCount(&config.message()));
4099 SimulatedEventLoopFactory event_loop_factory(&config.message());
4100 event_loop_factory.SetTimeConverter(&time_converter);
4101
4102 NodeEventLoopFactory *const pi1 =
4103 event_loop_factory.GetNodeEventLoopFactory("pi1");
4104 const size_t pi1_index = configuration::GetNodeIndex(
4105 event_loop_factory.configuration(), pi1->node());
4106 NodeEventLoopFactory *const pi2 =
4107 event_loop_factory.GetNodeEventLoopFactory("pi2");
4108 const size_t pi2_index = configuration::GetNodeIndex(
4109 event_loop_factory.configuration(), pi2->node());
4110 std::vector<std::string> filenames;
4111
4112 {
4113 CHECK_EQ(pi1_index, 0u);
4114 CHECK_EQ(pi2_index, 1u);
4115
4116 time_converter.AddNextTimestamp(
4117 distributed_clock::epoch(),
4118 {BootTimestamp::epoch(), BootTimestamp::epoch()});
4119
4120 const chrono::nanoseconds reboot_time = chrono::milliseconds(5000);
4121 time_converter.AddNextTimestamp(
4122 distributed_clock::epoch() + reboot_time,
Austin Schuh4b8b45b2022-04-13 17:05:44 -07004123 {BootTimestamp{.boot = 1, .time = monotonic_clock::epoch()},
Austin Schuh5c770fa2022-03-11 06:57:22 -08004124 BootTimestamp::epoch() + reboot_time});
4125 }
4126
4127 const std::string kLogfile2_1 =
4128 aos::testing::TestTmpDir() + "/multi_logfile2.1/";
4129 const std::string kLogfile1_1 =
4130 aos::testing::TestTmpDir() + "/multi_logfile1.1/";
4131 util::UnlinkRecursive(kLogfile2_1);
4132 util::UnlinkRecursive(kLogfile1_1);
4133
4134 pi2->Disconnect(pi1->node());
4135
4136 pi1->AlwaysStart<Ping>("ping");
4137 pi2->AlwaysStart<Pong>("pong");
4138
4139 {
4140 LoggerState pi2_logger = LoggerState::MakeLogger(
4141 pi2, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
4142
4143 event_loop_factory.RunFor(chrono::milliseconds(95));
4144
4145 pi2_logger.StartLogger(kLogfile2_1);
4146
4147 event_loop_factory.RunFor(chrono::milliseconds(6000));
4148
4149 pi2->Connect(pi1->node());
4150
4151 LoggerState pi1_logger = LoggerState::MakeLogger(
4152 pi1, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
4153 pi1_logger.StartLogger(kLogfile1_1);
4154
4155 event_loop_factory.RunFor(chrono::milliseconds(5000));
4156 pi1_logger.AppendAllFilenames(&filenames);
4157 pi2_logger.AppendAllFilenames(&filenames);
4158 }
4159
4160 const std::vector<LogFile> sorted_parts = SortParts(filenames);
4161 ConfirmReadable(filenames);
4162}
4163
Austin Schuhc1ee1b62022-03-22 17:09:52 -07004164// Tests that we properly handle only one direction ever existing after a
4165// reboot.
4166TEST(MissingDirectionTest, OneDirectionAfterReboot) {
4167 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
4168 aos::configuration::ReadConfig(ArtifactPath(
4169 "aos/events/logging/multinode_pingpong_split4_config.json"));
4170 message_bridge::TestingTimeConverter time_converter(
4171 configuration::NodesCount(&config.message()));
4172 SimulatedEventLoopFactory event_loop_factory(&config.message());
4173 event_loop_factory.SetTimeConverter(&time_converter);
4174
4175 NodeEventLoopFactory *const pi1 =
4176 event_loop_factory.GetNodeEventLoopFactory("pi1");
4177 const size_t pi1_index = configuration::GetNodeIndex(
4178 event_loop_factory.configuration(), pi1->node());
4179 NodeEventLoopFactory *const pi2 =
4180 event_loop_factory.GetNodeEventLoopFactory("pi2");
4181 const size_t pi2_index = configuration::GetNodeIndex(
4182 event_loop_factory.configuration(), pi2->node());
4183 std::vector<std::string> filenames;
4184
4185 {
4186 CHECK_EQ(pi1_index, 0u);
4187 CHECK_EQ(pi2_index, 1u);
4188
4189 time_converter.AddNextTimestamp(
4190 distributed_clock::epoch(),
4191 {BootTimestamp::epoch(), BootTimestamp::epoch()});
4192
4193 const chrono::nanoseconds reboot_time = chrono::milliseconds(5000);
4194 time_converter.AddNextTimestamp(
4195 distributed_clock::epoch() + reboot_time,
Austin Schuh4b8b45b2022-04-13 17:05:44 -07004196 {BootTimestamp{.boot = 1, .time = monotonic_clock::epoch()},
Austin Schuhc1ee1b62022-03-22 17:09:52 -07004197 BootTimestamp::epoch() + reboot_time});
4198 }
4199
4200 const std::string kLogfile2_1 =
4201 aos::testing::TestTmpDir() + "/multi_logfile2.1/";
4202 util::UnlinkRecursive(kLogfile2_1);
4203
Austin Schuh4b8b45b2022-04-13 17:05:44 -07004204 pi1->AlwaysStart<Ping>("ping");
4205
4206 // Pi1 sends to pi2. Reboot pi1, but don't let pi2 connect to pi1. This
4207 // makes it such that we will only get timestamps from pi1 -> pi2 on the
4208 // second boot.
4209 {
4210 LoggerState pi2_logger = LoggerState::MakeLogger(
4211 pi2, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
4212
4213 event_loop_factory.RunFor(chrono::milliseconds(95));
4214
4215 pi2_logger.StartLogger(kLogfile2_1);
4216
4217 event_loop_factory.RunFor(chrono::milliseconds(4000));
4218
4219 pi2->Disconnect(pi1->node());
4220
4221 event_loop_factory.RunFor(chrono::milliseconds(1000));
4222 pi1->AlwaysStart<Ping>("ping");
4223
4224 event_loop_factory.RunFor(chrono::milliseconds(5000));
4225 pi2_logger.AppendAllFilenames(&filenames);
4226 }
4227
4228 const std::vector<LogFile> sorted_parts = SortParts(filenames);
4229 ConfirmReadable(filenames);
4230}
4231
4232// Tests that we properly handle only one direction ever existing after a reboot
4233// with only reliable data.
4234TEST(MissingDirectionTest, OneDirectionAfterRebootReliable) {
4235 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
4236 aos::configuration::ReadConfig(ArtifactPath(
4237 "aos/events/logging/multinode_pingpong_split4_reliable_config.json"));
4238 message_bridge::TestingTimeConverter time_converter(
4239 configuration::NodesCount(&config.message()));
4240 SimulatedEventLoopFactory event_loop_factory(&config.message());
4241 event_loop_factory.SetTimeConverter(&time_converter);
4242
4243 NodeEventLoopFactory *const pi1 =
4244 event_loop_factory.GetNodeEventLoopFactory("pi1");
4245 const size_t pi1_index = configuration::GetNodeIndex(
4246 event_loop_factory.configuration(), pi1->node());
4247 NodeEventLoopFactory *const pi2 =
4248 event_loop_factory.GetNodeEventLoopFactory("pi2");
4249 const size_t pi2_index = configuration::GetNodeIndex(
4250 event_loop_factory.configuration(), pi2->node());
4251 std::vector<std::string> filenames;
4252
4253 {
4254 CHECK_EQ(pi1_index, 0u);
4255 CHECK_EQ(pi2_index, 1u);
4256
4257 time_converter.AddNextTimestamp(
4258 distributed_clock::epoch(),
4259 {BootTimestamp::epoch(), BootTimestamp::epoch()});
4260
4261 const chrono::nanoseconds reboot_time = chrono::milliseconds(5000);
4262 time_converter.AddNextTimestamp(
4263 distributed_clock::epoch() + reboot_time,
4264 {BootTimestamp{.boot = 1, .time = monotonic_clock::epoch()},
4265 BootTimestamp::epoch() + reboot_time});
4266 }
4267
4268 const std::string kLogfile2_1 =
4269 aos::testing::TestTmpDir() + "/multi_logfile2.1/";
4270 util::UnlinkRecursive(kLogfile2_1);
Austin Schuhc1ee1b62022-03-22 17:09:52 -07004271
4272 pi1->AlwaysStart<Ping>("ping");
4273
4274 // Pi1 sends to pi2. Reboot pi1, but don't let pi2 connect to pi1. This
4275 // makes it such that we will only get timestamps from pi1 -> pi2 on the
4276 // second boot.
4277 {
4278 LoggerState pi2_logger = LoggerState::MakeLogger(
4279 pi2, &event_loop_factory, SupportedCompressionAlgorithms()[0]);
4280
4281 event_loop_factory.RunFor(chrono::milliseconds(95));
4282
4283 pi2_logger.StartLogger(kLogfile2_1);
4284
4285 event_loop_factory.RunFor(chrono::milliseconds(4000));
4286
4287 pi2->Disconnect(pi1->node());
4288
4289 event_loop_factory.RunFor(chrono::milliseconds(1000));
4290 pi1->AlwaysStart<Ping>("ping");
4291
4292 event_loop_factory.RunFor(chrono::milliseconds(5000));
4293 pi2_logger.AppendAllFilenames(&filenames);
4294 }
4295
4296 const std::vector<LogFile> sorted_parts = SortParts(filenames);
4297 ConfirmReadable(filenames);
4298}
4299
Austin Schuhe309d2a2019-11-29 13:25:21 -08004300} // namespace testing
4301} // namespace logger
4302} // namespace aos