1 8 9 package com.sleepycat.je.recovery; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.io.RandomAccessFile ; 14 import java.util.Hashtable ; 15 import java.util.List ; 16 17 import com.sleepycat.je.Database; 18 import com.sleepycat.je.DatabaseConfig; 19 import com.sleepycat.je.DatabaseEntry; 20 import com.sleepycat.je.DbInternal; 21 import com.sleepycat.je.Environment; 22 import com.sleepycat.je.EnvironmentConfig; 23 import com.sleepycat.je.Transaction; 24 import com.sleepycat.je.config.EnvironmentParams; 25 import com.sleepycat.je.dbi.EnvironmentImpl; 26 import com.sleepycat.je.log.FileManager; 27 import com.sleepycat.je.tree.LN; 28 import com.sleepycat.je.tree.Node; 29 import com.sleepycat.je.util.StringDbt; 30 import com.sleepycat.je.util.TestUtils; 31 32 public class RecoveryEdgeTest extends RecoveryTestBase { 33 public void testNoLogFiles() 34 throws Throwable { 35 36 37 EnvironmentImpl env = null; 38 try { 39 EnvironmentConfig noFileConfig = TestUtils.initEnvConfig(); 40 41 DbInternal.setCheckpointUP(noFileConfig, false); 42 noFileConfig.setConfigParam(EnvironmentParams.LOG_MEMORY_ONLY.getName(), 43 "true"); 44 noFileConfig.setTransactional(true); 45 noFileConfig.setAllowCreate(true); 46 env = new EnvironmentImpl(envHome, noFileConfig); 47 List dbList = env.getDbMapTree().getDbNames(); 48 assertEquals("no dbs exist", 0, dbList.size()); 49 50 51 env.close(); 52 env = new EnvironmentImpl(envHome, noFileConfig); 53 dbList = env.getDbMapTree().getDbNames(); 54 assertEquals("no dbs exist", 0, dbList.size()); 55 } catch (Throwable t) { 56 t.printStackTrace(); 57 throw t; 58 } finally { 59 if (env != null) 60 env.close(); 61 } 62 } 63 64 67 public void testDbId() 68 throws Throwable { 69 70 Transaction createTxn = null; 71 try { 72 73 77 EnvironmentConfig createConfig = TestUtils.initEnvConfig(); 78 createConfig.setTransactional(true); 79 createConfig.setAllowCreate(true); 80 createConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6"); 81 env = new Environment(envHome, createConfig); 82 83 int numStartDbs = 1; 84 createTxn = env.beginTransaction(null, null); 85 86 87 DatabaseConfig dbConfig = new DatabaseConfig(); 88 dbConfig.setTransactional(true); 89 dbConfig.setAllowCreate(true); 90 for (int i = 0; i < numStartDbs; i++) { 91 Database anotherDb = env.openDatabase(createTxn, "foo" + i, 92 dbConfig); 93 assertEquals((i+3), 94 DbInternal.dbGetDatabaseImpl(anotherDb).getId().getId()); 95 anotherDb.close(); 96 } 97 createTxn.commit(); 98 env.close(); 99 100 104 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 105 envConfig.setTransactional(true); 106 createTxn = null; 107 for (int i = numStartDbs; i < numStartDbs + 3; i++) { 108 env = new Environment(envHome, envConfig); 109 110 createTxn = env.beginTransaction(null, null); 111 Database anotherDb = env.openDatabase(createTxn, "foo" + i, 112 dbConfig); 113 assertEquals(i+3, 114 DbInternal.dbGetDatabaseImpl(anotherDb).getId().getId()); 115 anotherDb.close(); 116 createTxn.commit(); 117 env.close(); 118 } 119 } catch (Throwable t) { 120 if (createTxn != null) { 121 createTxn.abort(); 122 } 123 t.printStackTrace(); 124 throw t; 125 } 126 } 127 128 131 public void testNodeId() 132 throws Throwable { 133 134 try { 135 136 createEnvAndDbs(1024, true, NUM_DBS); 137 Hashtable expectedData = new Hashtable (); 138 Transaction txn = env.beginTransaction(null, null); 139 insertData(txn, 0, 4, expectedData, 1, true, NUM_DBS); 140 txn.commit(); 141 142 143 long maxSeenNodeId = Node.getLastId(); 144 145 146 closeEnv(); 147 EnvironmentConfig recoveryConfig = TestUtils.initEnvConfig(); 148 recoveryConfig.setConfigParam( 149 EnvironmentParams.NODE_MAX.getName(), "6"); 150 recoveryConfig.setConfigParam( 151 EnvironmentParams.ENV_RUN_CLEANER.getName(), 152 "false"); 153 154 DbInternal.setCheckpointUP(recoveryConfig, false); 155 env = new Environment(envHome, recoveryConfig); 156 LN ln = new LN(new byte[0]); 157 158 159 assertTrue(maxSeenNodeId + 1 < ln.getNodeId()); 160 maxSeenNodeId = Node.getLastId(); 161 162 168 env.close(); 169 env = new Environment(envHome, recoveryConfig); 170 ln = new LN(new byte[0]); 171 175 assertTrue(maxSeenNodeId+1 <= ln.getNodeId()); 176 177 } catch (Throwable t) { 178 t.printStackTrace(); 179 throw t; 180 } 181 } 182 183 186 public void testTxnId() 187 throws Throwable { 188 189 try { 190 191 createEnvAndDbs(1024, true, NUM_DBS); 192 Hashtable expectedData = new Hashtable (); 193 194 195 Transaction txn = env.beginTransaction(null, null); 196 insertData(txn, 0, 4, expectedData, 1, true, NUM_DBS); 197 txn.commit(); 198 env.checkpoint(forceConfig); 199 txn = env.beginTransaction(null, null); 200 insertData(txn, 5, 6, expectedData, 1, false, NUM_DBS); 201 202 203 long maxTxnId = txn.getId(); 204 txn.abort(); 205 206 207 closeEnv(); 208 209 EnvironmentConfig recoveryConfig = TestUtils.initEnvConfig(); 210 recoveryConfig.setConfigParam (EnvironmentParams.ENV_RUN_CLEANER.getName(), 211 "false"); 212 recoveryConfig.setTransactional(true); 213 env = new Environment(envHome, recoveryConfig); 214 215 220 txn = env.beginTransaction(null, null); 221 createDbs(txn, NUM_DBS); 222 assertTrue(maxTxnId < txn.getId()); 223 assertTrue((txn.getId() - maxTxnId) < 11); 224 225 229 insertData(txn, 7, 8, expectedData, 1, false, NUM_DBS); 230 long secondMaxTxnId = txn.getId(); 231 txn.abort(); 232 233 237 closeEnv(); 238 env = new Environment(envHome, recoveryConfig); 239 txn = env.beginTransaction(null, null); 240 assertTrue(secondMaxTxnId < txn.getId()); 241 assertTrue((txn.getId() - secondMaxTxnId) < 10); 242 txn.abort(); 243 } catch (Throwable t) { 244 t.printStackTrace(); 245 throw t; 246 } 247 } 248 249 253 public void testNonTxnalDb () 254 throws Throwable { 255 256 createEnv(1024, false); 257 try { 258 262 DatabaseConfig dbConfig = new DatabaseConfig(); 263 dbConfig.setAllowCreate(true); 264 Database dbA = env.openDatabase(null, "NotTxnal", dbConfig); 265 266 DatabaseEntry key = new StringDbt("foo"); 267 DatabaseEntry data = new StringDbt("bar"); 268 dbA.put(null, key, data); 269 270 273 dbA.close(); 274 env.close(); 275 createEnv(1024, false); 276 277 dbA = env.openDatabase(null, "NotTxnal", null); 278 dbA.close(); 279 280 284 dbConfig.setTransactional(true); 285 Database dbB = env.openDatabase(null, "Txnal", dbConfig); 286 dbB.close(); 287 dbB = env.openDatabase(null, "Txnal", null); 288 dbB.put(null, key, data); 289 dbB.close(); 290 env.close(); 291 292 296 297 createEnv(1024, false); 298 List dbNames = env.getDatabaseNames(); 299 assertEquals(2, dbNames.size()); 300 assertEquals("Txnal", dbNames.get(1)); 301 assertEquals("NotTxnal", dbNames.get(0)); 302 303 } catch (Throwable t) { 304 t.printStackTrace(); 305 throw t; 306 } finally { 307 env.close(); 308 } 309 } 310 311 314 public void testBadChecksum() 315 throws Throwable { 316 317 try { 318 319 createEnvAndDbs(2048, false, 1); 320 Hashtable expectedData = new Hashtable (); 321 322 323 Transaction txn = env.beginTransaction(null, null); 324 insertData(txn, 0, 4, expectedData, 1, true, 1); 325 txn.commit(); 326 env.checkpoint(forceConfig); 327 328 txn = env.beginTransaction(null, null); 329 insertData(txn, 5, 6, expectedData, 1, true, 1); 330 txn.commit(); 331 332 txn = env.beginTransaction(null, null); 333 insertData(txn, 7, 8, expectedData, 1, false, 1); 334 335 336 closeEnv(); 337 338 339 writeBadStuffInLastFile(); 340 341 recoverAndVerify(expectedData, 1); 342 } catch (Throwable t) { 343 t.printStackTrace(); 344 throw t; 345 } 346 } 347 348 354 public void testBadChecksumReadOnlyReadPastLastFile() 355 throws Throwable { 356 357 try { 358 359 createEnvAndDbs(500, false, 1); 360 Hashtable expectedData = new Hashtable (); 361 362 363 Transaction txn = env.beginTransaction(null, null); 364 insertData(txn, 0, 4, expectedData, 1, true, 1); 365 txn.commit(); 366 env.checkpoint(forceConfig); 367 368 372 String [] suffixes = new String [] {FileManager.JE_SUFFIX}; 373 String [] fileList = FileManager.listFiles(envHome, suffixes); 374 int startingNumFiles = fileList.length; 375 376 377 txn = env.beginTransaction(null, null); 378 insertData(txn, 7, 50, expectedData, 1, false, 1); 379 380 381 closeEnv(); 382 383 384 fileList = FileManager.listFiles(envHome, suffixes); 385 assertTrue(fileList.length > startingNumFiles); 386 387 388 writeBadStuffInLastFile(); 389 390 recoverROAndVerify(expectedData, 1); 391 } catch (Throwable t) { 392 t.printStackTrace(); 393 throw t; 394 } 395 } 396 397 private void writeBadStuffInLastFile() 398 throws IOException { 399 400 String [] files = 401 FileManager.listFiles(envHome, 402 new String [] {FileManager.JE_SUFFIX}); 403 File lastFile = new File (envHome, files[files.length - 1]); 404 RandomAccessFile rw = new RandomAccessFile (lastFile, "rw"); 405 406 rw.seek(rw.length()-10); 407 rw.writeBytes("000000"); 408 rw.close(); 409 } 410 } 411 | Popular Tags |