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
|
/*
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: BinDeltaTest.java,v 1.50.2.2 2010/01/04 15:30:48 cwl Exp $
*/
package com.sleepycat.je.tree;
import java.io.File;
import java.io.IOException;
import junit.framework.TestCase;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.DbInternal;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;
import com.sleepycat.je.config.EnvironmentParams;
import com.sleepycat.je.dbi.CursorImpl;
import com.sleepycat.je.log.LogManager;
import com.sleepycat.je.log.FileManager;
import com.sleepycat.je.log.entry.LogEntry;
import com.sleepycat.je.tree.Key.DumpType;
import com.sleepycat.je.txn.BasicLocker;
import com.sleepycat.je.txn.Locker;
import com.sleepycat.je.util.TestUtils;
import com.sleepycat.je.utilint.DbLsn;
/**
* Exercise the delta based BIN logging.
*/
public class BinDeltaTest extends TestCase {
private static final String DB_NAME = "test";
private static final boolean DEBUG = false;
private Environment env;
private File envHome;
private Database db;
private LogManager logManager;
public BinDeltaTest() throws DatabaseException {
envHome = new File(System.getProperty(TestUtils.DEST_DIR));
/* Print keys as numbers */
Key.DUMP_TYPE = DumpType.BINARY;
}
public void setUp() throws IOException, DatabaseException {
TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
/*
* Properties for creating an environment. Disable the evictor for
* this test, use larger BINS.
*/
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setTransactional(true);
envConfig.setConfigParam
(EnvironmentParams.ENV_RUN_EVICTOR.getName(), "true");
envConfig.setConfigParam
(EnvironmentParams.NODE_MAX.getName(), "50");
envConfig.setConfigParam
(EnvironmentParams.BIN_DELTA_PERCENT.getName(), "50");
envConfig.setAllowCreate(true);
env = new Environment(envHome, envConfig);
logManager = DbInternal.envGetEnvironmentImpl(env).getLogManager();
}
public void tearDown() throws IOException, DatabaseException {
if (env != null) {
try {
env.close();
} catch (DatabaseException E) {
}
}
TestUtils.removeFiles("TearDown", envHome,
FileManager.JE_SUFFIX, true);
}
/**
* Create a db, fill with numRecords, return the first BIN.
* @param numRecords
*/
private BIN initDb(int start, int end)
throws DatabaseException {
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(true);
dbConfig.setAllowCreate(true);
db = env.openDatabase(null, DB_NAME, dbConfig);
addRecords(start, end);
/* Now reach into the tree and get the first BIN */
Locker txn = BasicLocker.
createBasicLocker(DbInternal.envGetEnvironmentImpl(env));
CursorImpl internalCursor =
new CursorImpl(DbInternal.dbGetDatabaseImpl(db), txn);
assertTrue(internalCursor.positionFirstOrLast(true, null));
BIN firstBIN = internalCursor.getBIN();
firstBIN.releaseLatch();
internalCursor.close();
txn.operationEnd();
return firstBIN;
}
/**
* Modify the data, just to dirty the BIN.
*/
private void modifyRecords(int start, int end, int increment)
throws DatabaseException {
Transaction txn = env.beginTransaction(null, null);
Cursor cursor = db.openCursor(txn, null);
DatabaseEntry searchKey = new DatabaseEntry();
DatabaseEntry foundData = new DatabaseEntry();
DatabaseEntry newData = new DatabaseEntry();
for (int i = start; i <= end; i++) {
searchKey.setData(TestUtils.getTestArray(i));
assertEquals(OperationStatus.SUCCESS,
cursor.getSearchKey(searchKey, foundData,
LockMode.DEFAULT));
newData.setData(TestUtils.getTestArray(i+increment));
cursor.putCurrent(newData);
}
cursor.close();
txn.commit();
}
/*
* Add the specified records.
*/
private void addRecords(int start, int end)
throws DatabaseException {
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
for (int i = start; i < end; i++) {
byte[] keyData = TestUtils.getTestArray(i);
byte[] dataData = TestUtils.byteArrayCopy(keyData);
key.setData(keyData);
data.setData(dataData);
db.put(null, key, data);
}
}
/**
* Simple test, delta a BIN several times, reconstruct.
*/
public void testSimple()
throws Throwable {
try {
/* Create a db, insert records value 10 - 30, get the first BIN */
BIN bin = initDb(10, 30);
/* Log a full version. */
bin.latch();
long fullLsn = bin.log
(logManager, true, false, false, false, null);
bin.releaseLatch();
assertTrue(fullLsn != DbLsn.NULL_LSN);
if (DEBUG) {
System.out.println("Start");
System.out.println(bin.dumpString(0, true));
}
/* Modify some of the data, add data so the BIN is changed. */
modifyRecords(11,13,10);
addRecords(1,3);
logAndCheck(bin);
/* Modify more of the data, so the BIN is changed. */
modifyRecords(14,15,10);
logAndCheck(bin);
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
db.close();
}
}
/**
* Test that a delta is correctly generated when there are entries
* that have been aborted and rolled back.
*
* The case we're trying to test, (that was in error before)
* - a record is deleted
* - a full version of BIN x is written to the log, reflecting that
* deletion.
* - the deleting txn is aborted, so the record is restored. Now the
* BIN has an entry where the child LSN is less than the last full
* BIN version LSN.
* - generate a delta, make sure that the restoration of the record is
* present.
*/
public void testUndo()
throws Throwable {
try {
/* Create a db, insert records value 10 - 30, get the first BIN */
BIN bin = initDb(10, 30);
/* Delete the first record, then abort the delete. */
Transaction txn = env.beginTransaction(null, null);
Cursor cursor = db.openCursor(txn, null);
DatabaseEntry firstKey = new DatabaseEntry();
DatabaseEntry foundData = new DatabaseEntry();
OperationStatus status = cursor.getFirst(firstKey, foundData,
LockMode.DEFAULT);
assertEquals(OperationStatus.SUCCESS, status);
status = cursor.delete();
assertEquals(OperationStatus.SUCCESS, status);
cursor.close();
/* Log a full version. This will reflect the delete. */
bin.latch();
long fullLsn = bin.log
(logManager, true, false, false, false, null);
bin.releaseLatch();
assertTrue(fullLsn != DbLsn.NULL_LSN);
/*
* Roll back the deletion. Now the full version of the LSN is out
* of date.
*/
txn.abort();
/*
* Make sure a delta reflect the abort, even though the abort
* returns an older LSN back into the BIN.
*/
logAndCheck(bin);
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
db.close();
}
}
/* Check if full is logged when percent > max */
/* Check that max deltas works. */
/* check knownDelete. */
/**
* Log the targetBIN, then read it back from the log and make sure
* the recreated BIN matches the in memory BIN.
*/
private void logAndCheck(BIN targetBIN)
throws DatabaseException {
/*
* Log it as a delta. If the logging was done as a delta, this method
* returns null, so we expect null
*/
assertTrue(targetBIN.log
(logManager, true, false, false, false, null) ==
DbLsn.NULL_LSN);
/* Read the delta back. */
LogEntry partial =
logManager.getLogEntry(targetBIN.getLastDeltaVersion());
/* Make sure that this is was a delta entry. */
assertTrue(partial.getMainItem() instanceof BINDelta);
BINDelta delta = (BINDelta) partial.getMainItem();
/* Compare to the current version. */
BIN createdBIN =
delta.reconstituteBIN(DbInternal.envGetEnvironmentImpl(env));
if (DEBUG) {
System.out.println("created");
System.out.println(createdBIN.dumpString(0, true));
}
assertEquals(targetBIN.getClass().getName(),
createdBIN.getClass().getName());
assertEquals(targetBIN.getNEntries(), createdBIN.getNEntries());
for (int i = 0; i < createdBIN.getNEntries(); i++) {
assertEquals("LSN " + i, targetBIN.getLsn(i),
createdBIN.getLsn(i));
}
assertEquals(true, createdBIN.getDirty());
assertEquals(true, targetBIN.getDirty());
}
}
|