aboutsummaryrefslogtreecommitdiffstats
path: root/vespalog/src/test/bufferedlogtest.cpp
blob: 3add70b3d4e3ce136132321a81d2e9e078cfc919 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/log/bufferedlogger.h>
#include <vespa/log/internal.h>

#include "bufferedlogtest.logger1.h"
#include "bufferedlogtest.logger2.h"

#include <fstream>
#include <iostream>
#include <unistd.h>
#include <cstdlib>

LOG_SETUP("bufferedlogtest");

using namespace std::literals::chrono_literals;


/** Test timer returning just a given time. Used in tests to fake time. */
struct TestTimer : public ns_log::Timer {
    uint64_t & _time;
    TestTimer(uint64_t & timeVar) : _time(timeVar) { }
    ns_log::system_time getTimestamp() const noexcept override {
        return ns_log::system_time(std::chrono::microseconds(_time));
    }
};

std::string readFile(const std::string& file) {
    std::ostringstream ost;
    std::ifstream is(file.c_str());
    std::string line;
    while (std::getline(is, line)) {
        std::string::size_type pos = line.find('\t');
        if (pos == std::string::npos) continue;
        std::string::size_type pos2 = line.find('\t', pos + 1);
        if (pos2 == std::string::npos) continue;
        std::string result = line.substr(0, pos)
                           + "\tlocalhost"
                           + line.substr(pos2);
        ost << result << "\n";
    }
    return ost.str();
}

void spamLog1(uint64_t& time, int diff) {
    for (int i=0; i<100; ++i) {
        time += diff;
        LOGBT(warning, "Failed to send to node 4",
                       "Failed to send message to node 4: NOT CONNECTED");
        time += diff;
        LOGBT(warning, "Failed to send to node 4",
                       "Failed to send message to node 4: NOT_READY");
        time += diff;
        LOGBT(warning, "Failed to send to node 4",
                       "Failed to send message to node 4: BAAAH");
        time += diff;
        LOGBT(warning, "Failed to send to node 4",
                       "Failed to send message to node 4: RPC FAILURE");
        time += diff;
        LOGBT(warning, "Failed to send to node 4",
                       "Failed to send message to node 4: COSMIC RADIATION");
        time += diff;
        LOGBT(warning, "Failed to send to node 4",
                       "Failed to send message to node 4: ITS SATURDAY");
        time += diff;
        LOGBT(warning, "Failed to send to node 4",
                       "Failed to send message to node 4: Yeah, Right!!");
        time += diff;
        LOGBT(error,   "Failed to send to node 4",
                       "Failed to send message to node 4: NOT CONNECTED");
        time += diff;
        LOGBT(spam,    "Failed to send to node 4",
                       "Failed to send message to node 4: NOT CONNECTED");
        time += diff;
        LOGBT(warning, "Failed to send to node 4",
                       "Failed to send message to node 4: NOT CONNECTED");
        time += diff * 10;
        LOGBT(warning, "Failed to send to node 3",
                       "Failed to send message to node 3: NOT CONNECTED");
    }
}

void spamLog2(uint64_t& time, int diff) {
    for (int i=0; i<100; ++i) {
        time += diff;
        std::ostringstream ost;
        ost << "Message " << i;
        LOGBT(info, ost.str(), "%s", ost.str().c_str());
    }
}

void testThatEntriesWithHighCountIsKept(const std::string& file, uint64_t& timer)
{
    std::cerr << "testThatEntriesWithHighCountIsKept ...\n";
    timer = 10 * 1000000 + 4;

    LOGBM(info, "Starting up, using logfile %s", file.c_str());
    timer = 100 * 1000000 + 4;
    LOGBT(warning, "Token", "message");

    spamLog1(timer, 1);
    spamLog2(timer, 1);
    spamLog1(timer, 1);

    LOGB_FLUSH();

    std::string result(readFile(file));
    std::string expected(readFile("bufferedlogtest.highcountkept.log"));

    if (result != expected) {
        std::cerr << "Failed testThatEntriesWithHighCountIsKept\n";
        [[maybe_unused]] int systemResult =
        system(("diff -u " + file + " bufferedlogtest.highcountkept.log")
                .c_str());
        std::_Exit(EXIT_FAILURE);
    }
    unlink(file.c_str());
}

void testThatEntriesWithHighCountsAreEventuallyRemoved(
        const std::string& file, uint64_t& timer)
{
    std::cerr << "testThatEntriesWithHighCountsAreEventuallyRemoved ...\n";
        // Same as above, just increase timer more between each log entry.
        // Should eventually throw out the entries with high count
    timer = 10 * 1000000 + 4;
        // Make sure we don't remove due to age.
    ns_log::BufferedLogger::instance().setMaxEntryAge(1000000);
        // Let each count, count for 5 seconds.
    ns_log::BufferedLogger::instance().setCountFactor(5);

    LOGBM(info, "Starting up, using logfile %s", file.c_str());
    timer = 100 * 1000000 + 4;
    LOGBT(warning, "Token", "message");

    spamLog1(timer, 1);
    spamLog2(timer, 10 * 1000000);
    spamLog1(timer, 1);

    LOGB_FLUSH();

    std::string result(readFile(file));
    std::string expected(readFile("bufferedlogtest.highcountexpire.log"));

    if (result != expected) {
        std::cerr << "Failed "
                  << "testThatEntriesWithHighCountsAreEventuallyRemoved\n";
        [[maybe_unused]] int systemResult =
        system(("diff -u " + file + " bufferedlogtest.highcountexpire.log")
                .c_str());
        std::_Exit(EXIT_FAILURE);
    }
    unlink(file.c_str());
}

void testThatEntriesExpire(
        const std::string& file, uint64_t& timer)
{
    std::cerr << "testThatEntriesExpire ...\n";
        // Test that we don't keep entries longer than max age
    timer = 10 * 1000000 + 4;
        // Time out after 120 seconds
    ns_log::BufferedLogger::instance().setMaxEntryAge(120);
        // Let counts count much, so they expire due to time instead
    ns_log::BufferedLogger::instance().setCountFactor(100000);

    LOGBM(info, "Starting up, using logfile %s", file.c_str());
    timer = 100 * 1000000 + 4;
    LOGBT(warning, "Token", "message");

    spamLog1(timer, 1);
    spamLog2(timer, 10 * 1000000);
    spamLog1(timer, 1);

    LOGB_FLUSH();

    std::string result(readFile(file));
    std::string expected(readFile("bufferedlogtest.expire.log"));

    if (result != expected) {
        std::cerr << "Failed "
                  << "testThatEntriesExpire\n";
        [[maybe_unused]] int systemResult =
        system(("diff -u " + file + " bufferedlogtest.expire.log").c_str());
        std::_Exit(EXIT_FAILURE);
    }
    unlink(file.c_str());
}

// Spam 10+ different entries lots of times, to fill cache with high count
// entries
void spamLog3(uint64_t& time, int diff) {
    for (int i=0; i<100; ++i) {
        time += diff;
        LOGBT(warning, "Failed to talk to node 1",
                       "Failed to send message to node 0x1: NOT CONNECTED");
        time += diff;
        LOGBT(warning, "Failed to talk to node 2",
                       "Failed to send message to node 0x2: NOT_READY");
        time += diff;
        LOGBT(warning, "Failed to talk to node 3",
                       "Failed to send message to node 0x3: BAAAH");
        time += diff;
        LOGBT(warning, "Failed to talk to node 4",
                       "Failed to send message to node 0x4: RPC FAILURE");
        time += diff;
        LOGBT(warning, "Failed to talk to node 5",
                       "Failed to send message to node 0x5: COSMIC RADIATION");
        time += diff;
        LOGBT(warning, "Failed to talk to node 6",
                       "Failed to send message to node 0x6: ITS SATURDAY");
        time += diff;
        LOGBT(warning, "Failed to talk to node 7",
                       "Failed to send message to node 0x7: Yeah, Right!!");
        time += diff;
        LOGBT(error,   "Failed to talk to node 8",
                       "Failed to send message to node 0x8: NOT CONNECTED");
        time += diff;
        LOGBT(info,    "Failed to talk to node 9",
                       "Failed to send message to node 0x9: NOT CONNECTED");
        time += diff;
        LOGBT(warning, "Failed to talk to node 10",
                       "Failed to send message to node 0xa: NOT CONNECTED");
    }
}

void testThatHighCountEntriesDontStarveOthers(
        const std::string& file, uint64_t& timer)
{
    std::cerr << "testThatHighCountEntriesDontStarveOthers ...\n";
    timer = 10 * 1000000 + 4;
        // Long time out, we don't want to rely on timeout to prevent starvation
    ns_log::BufferedLogger::instance().setMaxEntryAge(12000000);
        // Let counts count much, so they score high
    ns_log::BufferedLogger::instance().setCountFactor(100000);

    LOGBM(info, "Starting up, using logfile %s", file.c_str());
    timer = 100 * 1000000;
    LOGBT(warning, "Token", "message");

    spamLog3(timer, 1);
    spamLog1(timer, 1);

    LOGB_FLUSH();

    std::string result(readFile(file));
    std::string expected(readFile("bufferedlogtest.nostarve.log"));

    if (result != expected) {
        std::cerr << "Failed "
                  << "testThatHighCountEntriesDontStarveOthers\n";
        [[maybe_unused]] int systemResult =
        system(("diff -u " + file + " bufferedlogtest.nostarve.log").c_str());
        std::_Exit(EXIT_FAILURE);
    }
    unlink(file.c_str());
}

void testNoTokenMatchAcrossComponents(const std::string& file,
                                      uint64_t& timer)
{
    std::cerr << "testNoTokenMatchAcrossComponents ...\n";

    LOGBP(info, "Starting up, using logfile %s", file.c_str());

    timer = 200 * 1000000;
    for (uint32_t i=0; i<100; ++i) {
        std::ostringstream ost;
        ost << "Message " << i;
        logWithLogger1("Message", ost.str());
        timer += 1;
        logWithLogger2("Message", ost.str());
        timer += 1;
    }

    LOGB_FLUSH();

    std::string result(readFile(file));
    std::string expected(readFile("bufferedlogtest.tokenacrossloggers.log"));

    if (result != expected) {
        std::cerr << "Failed "
                  << "testNoTokenMatchAcrossComponents\n";
        [[maybe_unused]] int systemResult =
        system(("diff -u " + file
                    + " bufferedlogtest.tokenacrossloggers.log").c_str());
        std::_Exit(EXIT_FAILURE);
    }
    unlink(file.c_str());
}

void testLogLocationAsToken(const std::string& file, uint64_t& timer)
{
    std::cerr << "testLogLocationAsToken ...\n";
    LOGBP(info, "Starting up, using logfile %s", file.c_str());

    timer = 200 * 1000000;
    for (uint32_t i=0; i<100; ++i) {
        LOGBP(info, "Message %i", i);
        timer += 1;
        LOGBP(info, "Message %i", i);
        timer += 1;
    }

    LOGB_FLUSH();

    std::string result(readFile(file));
    std::string expected(readFile("bufferedlogtest.locationastoken.log"));

    if (result != expected) {
        std::cerr << "Failed "
                  << "testLogLocationAsToken\n";
        [[maybe_unused]] int systemResult =
        system(("diff -u " + file
                    + " bufferedlogtest.locationastoken.log").c_str());
        std::_Exit(EXIT_FAILURE);
    }
    unlink(file.c_str());
}

void testLogMessageAsToken(const std::string& file, uint64_t& timer)
{
    std::cerr << "testLogMessageAsToken ...\n";
    LOGBM(info, "Starting up, using logfile %s", file.c_str());

    timer = 200 * 1000000;
    for (uint32_t i=0; i<100; ++i) {
        LOGBM(info, "Message %i", i % 10);
        timer += 1;
        LOGBM(info, "Message %i", i % 10);
        timer += 1;
    }

    LOGB_FLUSH();

    std::string result(readFile(file));
    std::string expected(readFile("bufferedlogtest.messageastoken.log"));

    if (result != expected) {
        std::cerr << "Failed "
                  << "testLogMessageAsToken\n";
        [[maybe_unused]] int systemResult =
        system(("diff -u " + file
                    + " bufferedlogtest.messageastoken.log").c_str());
        std::_Exit(EXIT_FAILURE);
    }
    unlink(file.c_str());
}

void testNonBufferedLoggerTriggersBufferedLogTrim(const std::string& file,
                                                  uint64_t& timer)
{
    std::cerr << "testNonBufferedLoggerTriggersBufferedLogTrim ...\n";
        // Write a lot of buffered log.
    LOGBM(info, "Starting up, using logfile %s", file.c_str());

    timer = 200 * 1000000;
    for (uint32_t i=0; i<100; ++i) {
        LOGBP(info, "Message %i", i);
        timer += 1;
    }

        // Advance time to time where we should have flushed long ago.
    timer = 100000 * 1000000ull;
    LOG(info, "This should cause buffered log flush");

        // So flushing should have happened before this time.
    timer = 200000 * 1000000ull;
    LOGB_FLUSH();

    std::string result(readFile(file));
    std::string expected(readFile("bufferedlogtest.trimcache.log"));

    if (result != expected) {
        std::cerr << "Failed "
                  << "testNonBufferedLoggerTriggersBufferedLogTrim\n";
        [[maybe_unused]] int systemResult =
        system(("diff -u " + file
                    + " bufferedlogtest.trimcache.log").c_str());
        std::_Exit(EXIT_FAILURE);
    }
    unlink(file.c_str());

}

void reset(uint64_t& timer) {
    timer = 0;
    ns_log::BufferedLogger::instance().setMaxEntryAge(300);
    ns_log::BufferedLogger::instance().setCountFactor(5);
}

int
main(int argc, char **argv)
{
    if (argc != 2) {
        std::cerr << "bufferedlogtest must be called with one argument\n";
        return EXIT_FAILURE;
    }
    ns_log::Logger::fakePid = true;
    ns_log::BufferedLogger::instance().setMaxCacheSize(10);
    uint64_t timer;
    ns_log_logger.setTimer(std::make_unique<TestTimer>(timer));
    ns_log::BufferedLogger::instance().setTimer(std::make_unique<TestTimer>(timer));

    reset(timer);
    testThatEntriesWithHighCountIsKept(argv[1], timer);
    reset(timer);
    testThatEntriesWithHighCountsAreEventuallyRemoved(argv[1], timer);
    reset(timer);
    testThatEntriesExpire(argv[1], timer);
    reset(timer);
    testThatHighCountEntriesDontStarveOthers(argv[1], timer);
    reset(timer);
    testNoTokenMatchAcrossComponents(argv[1], timer);
    reset(timer);
    testLogLocationAsToken(argv[1], timer);
    reset(timer);
    testLogMessageAsToken(argv[1], timer);
    reset(timer);
    testNonBufferedLoggerTriggersBufferedLogTrim(argv[1], timer);

    return EXIT_SUCCESS;
}