File: INFileReaderTest.java

package info (click to toggle)
libdb-je-java 3.3.98-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,052 kB
  • sloc: java: 153,077; xml: 2,034; makefile: 3
file content (458 lines) | stat: -rw-r--r-- 15,715 bytes parent folder | download | duplicates (3)
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002,2010 Oracle.  All rights reserved.
 *
 * $Id: INFileReaderTest.java,v 1.87.2.2 2010/01/04 15:30:44 cwl Exp $
 */

package com.sleepycat.je.log;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import junit.framework.TestCase;

import com.sleepycat.je.CacheMode;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.DbInternal;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.cleaner.RecoveryUtilizationTracker;
import com.sleepycat.je.config.EnvironmentParams;
import com.sleepycat.je.dbi.DbConfigManager;
import com.sleepycat.je.dbi.EnvironmentImpl;
import com.sleepycat.je.log.entry.SingleItemEntry;
import com.sleepycat.je.tree.BIN;
import com.sleepycat.je.tree.ChildReference;
import com.sleepycat.je.tree.IN;
import com.sleepycat.je.tree.INDeleteInfo;
import com.sleepycat.je.tree.Key;
import com.sleepycat.je.tree.Key.DumpType;
import com.sleepycat.je.tree.LN;
import com.sleepycat.je.util.TestUtils;
import com.sleepycat.je.utilint.DbLsn;
import com.sleepycat.je.utilint.Tracer;

/**
 *
 */
public class INFileReaderTest extends TestCase {

    static private final boolean DEBUG = false;

    private File envHome;
    private Environment env;
    /*
     * Need a handle onto the true environment in order to create
     * a reader
     */
    private EnvironmentImpl envImpl;
    private Database db;
    private long maxNodeId;
    private List<CheckInfo> checkList;

    public INFileReaderTest() {
        super();
        envHome = new File(System.getProperty(TestUtils.DEST_DIR));
        Key.DUMP_TYPE = DumpType.BINARY;
    }

    public void setUp()
	throws IOException, DatabaseException {

        /*
         * Note that we use the official Environment class to make the
         * environment, so that everything is set up, but we then go a
         * backdoor route to get to the underlying EnvironmentImpl class
         * so that we don't require that the Environment.getDbEnvironment
         * method be unnecessarily public.
         */
        TestUtils.removeLogFiles("Setup", envHome, false);

        EnvironmentConfig envConfig = TestUtils.initEnvConfig();
        envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6");
        envConfig.setConfigParam
	    (EnvironmentParams.BIN_DELTA_PERCENT.getName(), "75");
        envConfig.setAllowCreate(true);

        /* Disable noisy UtilizationProfile database creation. */
        DbInternal.setCreateUP(envConfig, false);
        /* Don't checkpoint utilization info for this test. */
        DbInternal.setCheckpointUP(envConfig, false);
        /* Don't run the cleaner without a UtilizationProfile. */
        envConfig.setConfigParam
	    (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false");

        env = new Environment(envHome, envConfig);

        envImpl =DbInternal.envGetEnvironmentImpl(env);

    }

    public void tearDown()
	throws IOException, DatabaseException {

        envImpl = null;
        env.close();
        TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
    }

    /**
     * Test no log file
     */
    public void testNoFile()
	throws IOException, DatabaseException {

        /* Make a log file with a valid header, but no data. */
        INFileReader reader = new INFileReader
            (envImpl, 1000, DbLsn.NULL_LSN, DbLsn.NULL_LSN, false, false,
             DbLsn.NULL_LSN, DbLsn.NULL_LSN, null);
        reader.addTargetType(LogEntryType.LOG_IN);
        reader.addTargetType(LogEntryType.LOG_BIN);
        reader.addTargetType(LogEntryType.LOG_IN_DELETE_INFO);

        int count = 0;
        while (reader.readNextEntry()) {
            count += 1;
        }
        assertEquals("Empty file should not have entries", 0, count);
    }

    /**
     * Run with an empty file
     */
    public void testEmpty()
	throws IOException, DatabaseException {

        /* Make a log file with a valid header, but no data. */
        FileManager fileManager = envImpl.getFileManager();
        fileManager.bumpLsn(1000000);
        FileManagerTestUtils.createLogFile(fileManager, envImpl, 10000);
        fileManager.clear();

        INFileReader reader = new INFileReader
            (envImpl, 1000, DbLsn.NULL_LSN, DbLsn.NULL_LSN, false, false,
             DbLsn.NULL_LSN, DbLsn.NULL_LSN, null);
        reader.addTargetType(LogEntryType.LOG_IN);
        reader.addTargetType(LogEntryType.LOG_BIN);
        reader.addTargetType(LogEntryType.LOG_IN_DELETE_INFO);

        int count = 0;
        while (reader.readNextEntry()) {
            count += 1;
        }
        assertEquals("Empty file should not have entries", 0, count);
    }

    /**
     * Run with defaults, read whole log
     */
    public void testBasic()
	throws IOException, DatabaseException {

        DbConfigManager cm = envImpl.getConfigManager();
        doTest(50,
               cm.getInt(EnvironmentParams.LOG_ITERATOR_READ_SIZE),
               0,
               false);
    }

    /**
     * Run with very small buffers and track node ids
     */
    public void testTracking()
	throws IOException, DatabaseException {

        doTest(50, // num iterations
               10, // tiny buffer
               0, // start lsn index
               true); // track node ids
    }

    /**
     * Start in the middle of the file
     */
    public void testMiddleStart()
	throws IOException, DatabaseException {

        doTest(50, 100, 40, true);
    }

    private void doTest(int numIters,
                        int bufferSize,
                        int startLsnIndex,
                        boolean trackNodeIds)
        throws IOException, DatabaseException {

        /* Fill up a fake log file. */
        createLogFile(numIters);

        /* Decide where to start. */
        long startLsn = DbLsn.NULL_LSN;
        int checkIndex = 0;
        if (startLsnIndex >= 0) {
            startLsn = checkList.get(startLsnIndex).lsn;
            checkIndex = startLsnIndex;
        }

        /* Use an empty utilization map for testing tracking. */
        RecoveryUtilizationTracker tracker = trackNodeIds ?
            (new RecoveryUtilizationTracker(envImpl)) : null;

        INFileReader reader =
            new INFileReader(envImpl, bufferSize, startLsn, DbLsn.NULL_LSN,
                             trackNodeIds, false, DbLsn.NULL_LSN,
                             DbLsn.NULL_LSN, tracker);
        reader.addTargetType(LogEntryType.LOG_IN);
        reader.addTargetType(LogEntryType.LOG_BIN);
        reader.addTargetType(LogEntryType.LOG_BIN_DELTA);
        reader.addTargetType(LogEntryType.LOG_IN_DELETE_INFO);

        /* Read. */
        checkLogFile(reader, checkIndex, trackNodeIds);
    }

    /**
     * Write a logfile of entries, then read the end
     */
    private void createLogFile(int numIters)
	throws IOException, DatabaseException {

        /*
         * Create a log file full of INs, INDeleteInfo, BINDeltas and
         * Debug Records
         */
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setAllowCreate(true);
        db = env.openDatabase(null, "foo", dbConfig);
        LogManager logManager = envImpl.getLogManager();
        maxNodeId = 0;

        checkList = new ArrayList<CheckInfo>();

        for (int i = 0; i < numIters; i++) {
            /* Add a debug record. */
            Tracer rec = new Tracer("Hello there, rec " + (i + 1));
            rec.log(logManager);

            /* Create, log, and save an IN. */
            byte[] data = new byte[i + 1];
            Arrays.fill(data, (byte) (i + 1));

            byte[] key = new byte[i + 1];
            Arrays.fill(key, (byte) (i + 1));

            IN in = new IN(DbInternal.dbGetDatabaseImpl(db), key, 5, 10);
	    in.latch(CacheMode.UNCHANGED);
            long lsn = in.log(logManager);
	    in.releaseLatch();
            checkList.add(new CheckInfo(lsn, in));

            if (DEBUG) {
                System.out.println("LSN " + i + " = " + lsn);
                System.out.println("IN " + i + " = " + in.getNodeId());
            }

            /* Add other types of INs. */
            BIN bin = new BIN(DbInternal.dbGetDatabaseImpl(db), key, 2, 1);
	    bin.latch(CacheMode.UNCHANGED);
            lsn = bin.log(logManager);
            checkList.add(new CheckInfo(lsn, bin));

            /* Add provisional entries, which should get ignored. */
            lsn = bin.log(logManager,
        	          false, // allowDeltas,
        	          true,  // isProvisional,
        	          false, // proactiveMigration,
        	          false, // backgroundIO
        	          in);

	    bin.releaseLatch();

            /* Add a LN, to stress the node tracking. */
            LN ln = new LN(data, envImpl, false /*replicated*/);
            lsn = ln.log(envImpl,
                         DbInternal.dbGetDatabaseImpl(db),
                         key, DbLsn.NULL_LSN, null, false,
                         ReplicationContext.NO_REPLICATE);

            /*
	     * Add an IN delete entry, it should get picked up by the reader.
	     */
            INDeleteInfo info =
                new INDeleteInfo(i, key, DbInternal.
				 dbGetDatabaseImpl(db).getId());
            lsn = logManager.log
                (new SingleItemEntry(LogEntryType.LOG_IN_DELETE_INFO, info),
                 ReplicationContext.NO_REPLICATE);
            checkList.add(new CheckInfo(lsn, info));

            /*
             * Add an BINDelta. Generate it by making the first, full version
             * provisional so the test doesn't pick it up, and then log a
             * delta.
             */
            BIN binDeltaBin =
		new BIN(DbInternal.dbGetDatabaseImpl(db), key, 10, 1);
            maxNodeId = binDeltaBin.getNodeId();
            binDeltaBin.latch();
            ChildReference newEntry =
                new ChildReference(null, key, DbLsn.makeLsn(0, 0));
            assertTrue(binDeltaBin.insertEntry(newEntry));

            lsn = binDeltaBin.log(logManager,
        	            	  false, // allowDeltas,
        	          	  true,  // isProvisional,
        	          	  false, // proactiveMigration,
                                  false, // backgroundIO
        	          	  in);   // parent

            /* Modify the bin with one entry so there can be a delta. */

            byte[] keyBuf2 = new byte[2];
            Arrays.fill(keyBuf2, (byte) (i + 2));
            ChildReference newEntry2 =
                new ChildReference(null, keyBuf2,
                                   DbLsn.makeLsn(100, 101));
            assertTrue(binDeltaBin.insertEntry(newEntry2));

            assertTrue(binDeltaBin.log(logManager,
        	                       true, // allowDeltas
        	                       false, // isProvisional
        	                       false, // proactiveMigration,
                                       false, // backgroundIO
        	                       in) ==
		       DbLsn.NULL_LSN);
            lsn = binDeltaBin.getLastDeltaVersion();
            if (DEBUG) {
                System.out.println("delta =" + binDeltaBin.getNodeId() +
                                   " at LSN " + lsn);
            }
            checkList.add(new CheckInfo(lsn, binDeltaBin));

            /*
             * Reset the generation to 0 so this version of the BIN, which gets
             * saved for unit test comparison, will compare to the version read
             * from the log, which is initialized to 0.
             */
            binDeltaBin.setGeneration(0);
            binDeltaBin.releaseLatch();
        }

        /* Flush the log, files. */
        logManager.flush();
        envImpl.getFileManager().clear();
    }

    private void checkLogFile(INFileReader reader,
                              int checkIndex,
                              boolean checkMaxNodeId)
        throws IOException, DatabaseException {

        try {
            /* Read all the INs. */
            int i = checkIndex;

            while (reader.readNextEntry()) {
                if (DEBUG) {
                    System.out.println("i = "
                                       + i
                                       + " reader.isDeleteInfo="
                                       + reader.isDeleteInfo()
                                       + " LSN = "
                                       + reader.getLastLsn());
                }

                CheckInfo check = checkList.get(i);

                if (reader.isDeleteInfo()) {
                    assertEquals(check.info.getDeletedNodeId(),
                                 reader.getDeletedNodeId());
                    assertTrue(Arrays.equals(check.info.getDeletedIdKey(),
                                             reader.getDeletedIdKey()));
                    assertTrue(check.info.getDatabaseId().equals
                               (reader.getDatabaseId()));

                } else {

                    /*
		     * When comparing the check data against the data from the
		     * log, make the dirty bits match so that they compare
		     * equal.
                     */
                    IN inFromLog = reader.getIN();
		    inFromLog.latch(CacheMode.UNCHANGED);
                    inFromLog.setDirty(true);
		    inFromLog.releaseLatch();
                    IN testIN = check.in;
		    testIN.latch(CacheMode.UNCHANGED);
                    testIN.setDirty(true);
		    testIN.releaseLatch();

                    /*
                     * Only check the INs we created in the test. (The others
                     * are from the map db.
                     */
                    if (reader.getDatabaseId().
			equals(DbInternal.dbGetDatabaseImpl(db).getId())) {
                        // The IN should match
                        String inFromLogString = inFromLog.toString();
                        String testINString = testIN.toString();
                        if (DEBUG) {
                            System.out.println("testIN=" + testINString);
                            System.out.println("inFromLog=" + inFromLogString);
                        }

                        assertEquals("IN "
                                     + inFromLog.getNodeId()
                                     + " at index "
                                     + i
                                     + " should match.\nTestIN=" +
                                     testIN +
                                     "\nLogIN=" +
                                     inFromLog,
                                     testINString,
                                     inFromLogString);
                    }
                }
                /* The LSN should match. */
                assertEquals
		    ("LSN " + i + " should match",
		     check.lsn,
		     reader.getLastLsn());

                i++;
            }
            assertEquals(i, checkList.size());
            if (checkMaxNodeId) {
                assertEquals(maxNodeId, reader.getMaxNodeId());
            }
        } finally {
            db.close();
        }
    }

    private class CheckInfo {
        long lsn;
        IN in;
        INDeleteInfo info;

        CheckInfo(long lsn, IN in) {
            this.lsn = lsn;
            this.in = in;
            this.info = null;
        }

        CheckInfo(long lsn, INDeleteInfo info) {
            this.lsn = lsn;
            this.in = null;
            this.info = info;
        }
    }
}