aboutsummaryrefslogtreecommitdiffstats
path: root/logserver/src/test/java/com/yahoo/logserver/handlers/archive/ArchiverHandlerTestCase.java
blob: 7c17f259442c97bcd16d52227145bcfaaf29d1e7 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.logserver.handlers.archive;

import com.yahoo.log.InvalidLogFormatException;
import com.yahoo.log.LogMessage;
import com.yahoo.plugin.SystemPropertyConfig;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @author Bjorn Borud
 */
public class ArchiverHandlerTestCase {

    private static final String[] mStrings = {
        "1095159244.095000\thost\t1/2\tservice\tcomponent\tinfo\tpayload1",
        "1095206399.000000\thost\t1/2\tservice\tcomponent\tinfo\tpayload2",
        "1095206400.000000\thost\t1/2\tservice\tcomponent\tinfo\tpayload3",
        "1095206401.000000\thost\t1/2\tservice\tcomponent\tinfo\tpayload4",
    };

    private static final LogMessage[] msg = new LogMessage[mStrings.length];

    static {
        try {
            for (int i = 0; i < mStrings.length; i++) {
                msg[i] = LogMessage.parseNativeFormat(mStrings[i]);
            }
        } catch (InvalidLogFormatException e) {
            throw new RuntimeException(e);
        }

        // mute the logging
        Logger.getLogger(ArchiverHandler.class.getName()).setLevel(Level.WARNING);
        Logger.getLogger(LogWriter.class.getName()).setLevel(Level.WARNING);

    }


    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    /**
     * Make sure that rollover for dateHash() occurs at midnight, so that
     * points in time belonging to the same day are in the interval
     * [00:00:00.000, 23:59:59.999].
     */
    @Test
    public void testDateHash() throws IOException {
        File tmpDir = temporaryFolder.newFolder();

        ArchiverHandler a = new ArchiverHandler(tmpDir.getAbsolutePath(),
                                                1024, "gzip");
        long now = 1095159244095L;
        long midnight = 1095206400000L;
        assertEquals(2004091410, a.dateHash(now));
        assertEquals(2004091423, a.dateHash(midnight - 1));
        assertEquals(2004091500, a.dateHash(midnight));
        assertEquals(2004091500, a.dateHash(midnight + 1));
        a.close();
    }

    /**
     * Test that the ArchiverHandler creates correct filenames.
     */
    @Test
    public void testFilenameCreation() throws IOException {
        File tmpDir = temporaryFolder.newFolder();
        try {
            ArchiverHandler a = new ArchiverHandler(tmpDir.getAbsolutePath(),
                                                    1024, "gzip");
            LogMessage msg1 = LogMessage.parseNativeFormat("1139322725\thost\t1/1\tservice\tcomponent\tinfo\tpayload");
            LogMessage msg2 = LogMessage.parseNativeFormat("1161172200\thost\t1/1\tservice\tcomponent\tinfo\tpayload");
            assertEquals(tmpDir.getAbsolutePath() + "/2006/02/07/14", a.getPrefix(msg1));
            assertEquals(tmpDir.getAbsolutePath() + "/2006/10/18/11", a.getPrefix(msg2));
            assertEquals(a.getPrefix(msg1).length(), a.getPrefix(msg2).length());
            a.close();
        } catch (InvalidLogFormatException e) {
            fail(e.toString());
        }
    }

    /**
     * Log some messages to the handler and verify that they are
     * written.
     */
    @Test
    public void testLogging() throws java.io.IOException, InvalidLogFormatException {
        File tmpDir = temporaryFolder.newFolder();

        ArchiverHandler a = new ArchiverHandler(tmpDir.getAbsolutePath(),
                                                1024, "gzip");

        for (int i = 0; i < msg.length; i++) {
            a.handle(msg[i]);
        }
        a.close();


        // make sure all the log files are there, that all log entries
        // are accounted for and that they match what we logged.
        int messageCount = 0;

        // map of files we've already inspected.  this we need to ensure
        // that we are not inspecting the same files over and over again
        // so the counts get messed up
        Set<String> inspectedFiles = new HashSet<String>();

        for (int i = 0; i < msg.length; i++) {
            String name = a.getPrefix(msg[i]) + "-0";

            // have we inspected this file before?
            if (! inspectedFiles.add(name)) {
                continue;
            }


            File f = new File(name);
            assertTrue(f.exists());

            BufferedReader br = new BufferedReader(new FileReader(f));
            for (String line = br.readLine();
                 line != null;
                 line = br.readLine()) {
                // primitive check if the messages match
                boolean foundMatch = false;
                for (int k = 0; k < mStrings.length; k++) {
                    if (mStrings[k].equals(line)) {
                        foundMatch = true;
                        break;
                    }
                }
                assertTrue(foundMatch);

                // try to instantiate messages to ensure that they
                // are parseable
                @SuppressWarnings("unused")
                    LogMessage m = LogMessage.parseNativeFormat(line);
                messageCount++;
            }
            br.close();
        }

        // verify that the number of log messages written equals
        // the number of log messages we have in our test
        assertEquals(mStrings.length, messageCount);
    }

    /**
     * Make sure that the file is rotated after N bytes
     */
    @Test
    public void testRotation() throws IOException {
        File tmpDir = temporaryFolder.newFolder();

        ArchiverHandler a = new ArchiverHandler(tmpDir.getAbsolutePath(),
                                                msg[1].toString().length() + 1,
                                                "gzip");
        // log the same message 4 times
        for (int i = 0; i < 4; i++) {
            a.handle(msg[1]);
        }
        a.close();

        // we should now have 3 files
        String prefix = a.getPrefix(msg[1]);
        int msgCount = 0;
        for (int i = 0; i < 3; i++) {
            File f = new File(prefix + "-" + i);
            assertTrue(f.exists());

            // ensure there's the same log message in all files
            BufferedReader br = new BufferedReader(new FileReader(f));
            for (String line = br.readLine();
                 line != null;
                 line = br.readLine()) {
                assertTrue(msg[1].toString().equals((line + "\n")));
                msgCount++;
            }
            br.close();
        }
        assertEquals(4, msgCount);

        // make sure we have no more than 3 files
        assertTrue(! (new File(prefix + "-3").exists()));



    }

    @Test
    public void testCacheEldestEntry() throws IOException {
        LogWriterLRUCache cache = new LogWriterLRUCache(5, (float) 0.75);
        String d = "target/tmp/logarchive";
        FilesArchived archive = new FilesArchived(new File(d), "gzip");
        for (int i = 0; i < cache.maxEntries + 10; i++) {
            cache.put(i, new LogWriter(d+"/2018/12/31/17", 5, archive));
        }
        assertEquals(cache.size(), cache.maxEntries);
    }

    @Test
    public void testArchiverPlugin() {
        ArchiverPlugin ap = new ArchiverPlugin();
        try {
            ap.shutdownPlugin();
            fail("Shutdown before init didn't throw.");
        } catch (Exception e) {
        }
        ap.initPlugin(new SystemPropertyConfig("test"));
        try {
            ap.initPlugin(new SystemPropertyConfig("test"));
            fail("Multiple init didn't throw.");
        } catch (Exception e) {
        }
        ap.shutdownPlugin();
    }

}