1 8 9 package com.sleepycat.je; 10 11 import java.io.File ; 12 import java.io.RandomAccessFile ; 13 import java.nio.ByteBuffer ; 14 import java.nio.channels.FileChannel ; 15 16 import junit.framework.TestCase; 17 18 import com.sleepycat.je.DbInternal; 19 import com.sleepycat.je.config.EnvironmentParams; 20 import com.sleepycat.je.log.FileManager; 21 import com.sleepycat.je.util.TestUtils; 22 23 public class RunRecoveryFailureTest extends TestCase { 24 25 private Environment env; 26 private File envHome; 27 28 public RunRecoveryFailureTest() { 29 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 30 } 31 32 public void setUp() 33 throws Exception { 34 35 TestUtils.removeLogFiles("Setup", envHome, false); 36 openEnv(); 37 38 } 39 40 public void tearDown() 41 throws Exception { 42 43 47 try { 48 if (env != null) { 49 env.close(); 50 env = null; 51 } 52 } catch (RunRecoveryException e) { 53 54 return; 55 } catch (DatabaseException e) { 56 57 } 58 59 TestUtils.removeLogFiles("TearDown", envHome, false); 60 } 61 62 private void openEnv() 63 throws DatabaseException { 64 65 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 66 envConfig.setTransactional(true); 67 68 72 DbInternal.disableParameterValidation(envConfig); 73 envConfig.setConfigParam 74 (EnvironmentParams.NUM_LOG_BUFFERS.getName(), "2"); 75 envConfig.setConfigParam 76 (EnvironmentParams.LOG_MEM_SIZE.getName(), 77 EnvironmentParams.LOG_MEM_SIZE_MIN_STRING); 78 envConfig.setConfigParam 79 (EnvironmentParams.LOG_FILE_MAX.getName(), "1024"); 80 envConfig.setConfigParam 81 (EnvironmentParams.JE_LOGGING_LEVEL.getName(), "CONFIG"); 82 envConfig.setAllowCreate(true); 83 env = new Environment(envHome, envConfig); 84 } 85 86 90 public void testInvalidateEnvMidStream() 91 throws Throwable { 92 93 try { 94 95 96 Transaction txn = 97 env.beginTransaction(null, TransactionConfig.DEFAULT); 98 DatabaseConfig dbConfig = new DatabaseConfig(); 99 dbConfig.setTransactional(true); 100 dbConfig.setAllowCreate(true); 101 env.openDatabase(txn, "foo", dbConfig); 102 103 env.getEnvironmentImpl().getLogManager().flush(); 104 env.getEnvironmentImpl().getFileManager().clear(); 105 106 111 File file = new File (envHome, "00000001" + FileManager.JE_SUFFIX); 112 RandomAccessFile starterFile = new RandomAccessFile (file, "rw"); 113 FileChannel channel = starterFile.getChannel(); 114 long fileSize = channel.size(); 115 channel.position(FileManager.firstLogEntryOffset()); 116 ByteBuffer junkBuffer = ByteBuffer.allocate((int) fileSize); 117 int written = channel.write(junkBuffer, 118 FileManager.firstLogEntryOffset()); 119 assertTrue(written > 0); 120 starterFile.close(); 121 122 try { 123 txn.abort(); 124 fail("Should see a run recovery exception"); 125 } catch (RunRecoveryException e) { 126 } 127 128 try { 129 env.getDatabaseNames(); 130 fail("Should see a run recovery exception again"); 131 } catch (RunRecoveryException e) { 132 } 133 } catch (Throwable t) { 134 t.printStackTrace(); 135 throw t; 136 } 137 } 138 } 139 | Popular Tags |