1 8 9 package com.sleepycat.je.cleaner; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 14 import junit.framework.TestCase; 15 16 import com.sleepycat.bind.tuple.TupleBase; 17 import com.sleepycat.bind.tuple.TupleOutput; 18 import com.sleepycat.je.CheckpointConfig; 19 import com.sleepycat.je.Database; 20 import com.sleepycat.je.DatabaseConfig; 21 import com.sleepycat.je.DatabaseEntry; 22 import com.sleepycat.je.DatabaseException; 23 import com.sleepycat.je.DbInternal; 24 import com.sleepycat.je.Environment; 25 import com.sleepycat.je.EnvironmentConfig; 26 import com.sleepycat.je.config.EnvironmentParams; 27 import com.sleepycat.je.dbi.EnvironmentImpl; 28 import com.sleepycat.je.latch.LatchSupport; 29 import com.sleepycat.je.log.FileManager; 30 import com.sleepycat.je.util.TestUtils; 31 import com.sleepycat.je.utilint.TestHook; 32 33 public class BackgroundIOTest extends TestCase { 34 35 private static CheckpointConfig forceConfig; 36 static { 37 forceConfig = new CheckpointConfig(); 38 forceConfig.setForce(true); 39 } 40 41 private File envHome; 42 private Environment env; 43 private int nSleeps; 44 45 public BackgroundIOTest() { 46 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 47 } 48 49 public void setUp() 50 throws IOException , DatabaseException { 51 52 TestUtils.removeLogFiles("Setup", envHome, false); 53 TestUtils.removeFiles("Setup", envHome, FileManager.DEL_SUFFIX); 54 } 55 56 public void tearDown() 57 throws IOException , DatabaseException { 58 59 if (env != null) { 60 try { 61 env.close(); 62 } catch (Throwable e) { 63 System.out.println("tearDown: " + e); 64 } 65 env = null; 66 } 67 68 TestUtils.removeLogFiles("TearDown", envHome, true); 70 TestUtils.removeFiles("TearDown", envHome, FileManager.DEL_SUFFIX); 71 } 73 74 public void testBackgroundIO1() 75 throws DatabaseException, InterruptedException { 76 77 doTest(10, 10, 226, 246); 78 } 79 80 public void testBackgroundIO2() 81 throws DatabaseException, InterruptedException { 82 83 doTest(10, 5, 365, 385); 84 } 85 86 public void testBackgroundIO3() 87 throws DatabaseException, InterruptedException { 88 89 doTest(5, 10, 324, 344); 90 } 91 92 public void testBackgroundIO4() 93 throws DatabaseException, InterruptedException { 94 95 doTest(5, 5, 463, 483); 96 } 97 98 private void doTest(int readLimit, 99 int writeLimit, 100 int minSleeps, 101 int maxSleeps) 102 throws DatabaseException, InterruptedException { 103 104 final int fileSize = 1000000; 105 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 106 envConfig.setAllowCreate(true); 107 envConfig.setConfigParam 108 (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false"); 109 envConfig.setConfigParam 110 (EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(), "false"); 111 envConfig.setConfigParam 112 (EnvironmentParams.ENV_RUN_INCOMPRESSOR.getName(), "false"); 113 envConfig.setConfigParam 114 (EnvironmentParams.LOG_BUFFER_MAX_SIZE.getName(), 115 Integer.toString(1024)); 116 envConfig.setConfigParam 117 (EnvironmentParams.LOG_FILE_MAX.getName(), 118 Integer.toString(fileSize)); 119 envConfig.setConfigParam 120 (EnvironmentParams.CLEANER_MIN_UTILIZATION.getName(), "60"); 121 envConfig.setConfigParam 123 (EnvironmentParams.ENV_BACKGROUND_READ_LIMIT.getName(), 124 String.valueOf(readLimit)); 125 envConfig.setConfigParam 126 (EnvironmentParams.ENV_BACKGROUND_WRITE_LIMIT.getName(), 127 String.valueOf(writeLimit)); 128 env = new Environment(envHome, envConfig); 130 131 EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env); 132 envImpl.setBackgroundSleepHook(new TestHook() { 133 public void doHook() { 134 nSleeps += 1; 135 assertEquals(0, LatchSupport.countLatchesHeld()); 136 } 137 public Object getHookValue() { 138 throw new UnsupportedOperationException (); 139 } 140 public void doIOHook() throws IOException { 141 throw new UnsupportedOperationException (); 142 } 143 }); 144 145 DatabaseConfig dbConfig = new DatabaseConfig(); 146 dbConfig.setAllowCreate(true); 147 dbConfig.setExclusiveCreate(true); 148 Database db = env.openDatabase(null, "BackgroundIO", dbConfig); 149 150 final int nFiles = 3; 151 final int keySize = 20; 152 final int dataSize = 10; 153 final int recSize = keySize + dataSize + 35 ; 154 final int nRecords = nFiles * (fileSize / recSize); 155 156 161 DatabaseEntry key = new DatabaseEntry(); 162 DatabaseEntry data = new DatabaseEntry(new byte[dataSize]); 163 for (int i = 0; i <= nRecords; i += 2) { 164 setKey(key, i, keySize); 165 db.put(null, key, data); 166 } 167 for (int i = 1; i <= nRecords; i += 2) { 168 setKey(key, i, keySize); 169 db.put(null, key, data); 170 } 171 172 173 env.checkpoint(forceConfig); 174 175 176 for (int i = 0; i <= nRecords; i += 1) { 177 setKey(key, i, keySize); 178 db.delete(null, key); 179 } 180 181 182 env.checkpoint(forceConfig); 183 env.cleanLog(); 184 env.checkpoint(forceConfig); 185 186 db.close(); 187 env.close(); 188 env = null; 189 190 String msg; 191 msg = "readLimit=" + readLimit + 192 " writeLimit=" + writeLimit + 193 " minSleeps=" + minSleeps + 194 " maxSleeps=" + maxSleeps + 195 " actualSleeps=" + nSleeps; 196 198 assertTrue(msg, nSleeps >= minSleeps && nSleeps <= maxSleeps); 200 } 202 203 206 private void setKey(DatabaseEntry entry, int val, int len) { 207 TupleOutput out = new TupleOutput(); 208 out.writeInt(val); 209 for (int i = 0; i < len - 4; i += 1) { 210 out.writeByte(0); 211 } 212 TupleBase.outputToEntry(out, entry); 213 } 214 } 215 | Popular Tags |