1 8 9 package com.sleepycat.je.log; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.io.RandomAccessFile ; 14 import java.nio.ByteBuffer ; 15 import java.nio.channels.FileChannel ; 16 import java.util.ArrayList ; 17 import java.util.Arrays ; 18 import java.util.List ; 19 20 import junit.framework.TestCase; 21 22 import com.sleepycat.je.Database; 23 import com.sleepycat.je.DatabaseConfig; 24 import com.sleepycat.je.DatabaseEntry; 25 import com.sleepycat.je.DatabaseException; 26 import com.sleepycat.je.DbInternal; 27 import com.sleepycat.je.Environment; 28 import com.sleepycat.je.EnvironmentConfig; 29 import com.sleepycat.je.config.EnvironmentParams; 30 import com.sleepycat.je.dbi.EnvironmentImpl; 31 import com.sleepycat.je.dbi.MemoryBudget; 32 import com.sleepycat.je.utilint.DbLsn; 33 import com.sleepycat.je.util.TestUtils; 34 35 public class LogBufferPoolTest extends TestCase { 36 37 Environment env; 38 Database db; 39 EnvironmentImpl environment; 40 FileManager fileManager; 41 File envHome; 42 LogBufferPool bufPool; 43 44 public LogBufferPoolTest() { 45 super(); 46 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 47 } 48 49 protected void setUp() 50 throws Exception { 51 52 53 TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX); 54 } 55 56 protected void tearDown() 57 throws Exception { 58 59 bufPool = null; 60 if (fileManager != null) { 61 fileManager.clear(); 62 fileManager.close(); 63 } 64 TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX); 65 } 66 67 70 public void testGrowBuffers() 71 throws Throwable { 72 73 try { 74 75 setupEnv(true); 76 77 81 List lsns = new ArrayList (); 82 for (int i = 0; i < 14; i++) { 83 long lsn = insertData(bufPool, (byte)(i + 1)); 84 lsns.add(new Long (lsn)); 85 } 86 87 91 LogBuffer logBuf; 92 ByteBuffer b; 93 for (int i = 0; i < 14; i++) { 94 95 99 long testLsn = DbLsn.longToLsn((Long ) lsns.get(i)); 100 logBuf = bufPool.getReadBuffer(testLsn); 101 assertNotNull(logBuf); 102 103 104 byte[] expected = new byte[10]; 105 Arrays.fill(expected, (byte)(i+1)); 106 107 108 byte[] logData = new byte[10]; 109 b = logBuf.getDataBuffer(); 110 long firstLsnInBuf = logBuf.getFirstLsn(); 111 b.position((int) (DbLsn.getFileOffset(testLsn) - 112 DbLsn.getFileOffset(firstLsnInBuf))); 113 logBuf.getDataBuffer().get(logData); 114 115 116 assertTrue(Arrays.equals(logData, expected)); 117 logBuf.release(); 118 } 119 120 124 assertNull(bufPool.getReadBuffer(DbLsn.makeLsn(0,10))); 125 126 130 assertNull("LSN too big", 131 bufPool.getReadBuffer(DbLsn.makeLsn(10, 141))); 132 } catch (Throwable t) { 133 t.printStackTrace(); 134 throw t; 135 } 136 } 137 138 142 private long insertData(LogBufferPool bufPool, 143 byte value) 144 throws IOException , DatabaseException { 145 146 byte[] data = new byte[10]; 147 Arrays.fill(data, value); 148 boolean flippedFile = fileManager.bumpLsn(data.length); 149 LogBuffer logBuf = bufPool.getWriteBuffer(data.length, flippedFile); 150 logBuf.getDataBuffer().put(data); 151 long lsn = fileManager.getLastUsedLsn(); 152 bufPool.writeCompleted(fileManager.getLastUsedLsn(), false); 153 return lsn; 154 } 155 156 159 public void testBufferFlush() 160 throws Throwable { 161 162 try { 163 setupEnv(false); 164 assertFalse("There should be no files", fileManager.filesExist()); 165 166 170 171 List lsnList = new ArrayList (); 172 for (int i = 0; i < 9; i++) { 173 long lsn = insertData(bufPool, (byte)(i+1)); 174 lsnList.add(new Long (lsn)); 175 } 176 bufPool.writeBufferToFile(0); 177 fileManager.syncLogEnd(); 178 179 180 String [] fileNames = 181 fileManager.listFiles(FileManager.JE_SUFFIXES); 182 assertEquals("Should be 2 files", 2, fileNames.length); 183 184 185 if (false) { 186 ByteBuffer dataBuffer = ByteBuffer.allocate(100); 187 FileHandle file0 = fileManager.getFileHandle(0L); 188 RandomAccessFile file = file0.getFile(); 189 FileChannel channel = file.getChannel(); 190 int bytesRead = channel.read(dataBuffer, 191 FileManager.firstLogEntryOffset()); 192 dataBuffer.flip(); 193 assertEquals("Check bytes read", 50, bytesRead); 194 assertEquals("Check size of file", 50, dataBuffer.limit()); 195 file.close(); 196 FileHandle file1 = fileManager.getFileHandle(1L); 197 file = file1.getFile(); 198 channel = file.getChannel(); 199 bytesRead = channel.read(dataBuffer, 200 FileManager.firstLogEntryOffset()); 201 dataBuffer.flip(); 202 assertEquals("Check bytes read", 40, bytesRead); 203 assertEquals("Check size of file", 40, dataBuffer.limit()); 204 file0.release(); 205 file1.release(); 206 } 207 } catch (Throwable e) { 208 e.printStackTrace(); 209 throw e; 210 } 211 } 212 213 public void testTemporaryBuffers() 214 throws Exception { 215 216 final int KEY_SIZE = 10; 217 final int DATA_SIZE = 1000000; 218 219 tempBufferInitEnvInternal 220 ("0", MemoryBudget.MIN_MAX_MEMORY_SIZE_STRING); 221 DatabaseEntry key = new DatabaseEntry(new byte[KEY_SIZE]); 222 DatabaseEntry data = new DatabaseEntry(new byte[DATA_SIZE]); 223 db.put(null, key, data); 224 db.close(); 225 env.close(); 226 } 227 228 private void tempBufferInitEnvInternal(String buffSize, String cacheSize) 229 throws DatabaseException { 230 231 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 232 envConfig.setTransactional(true); 233 envConfig.setAllowCreate(true); 234 if (!buffSize.equals("0")) { 235 envConfig.setConfigParam("je.log.totalBufferBytes", buffSize); 236 } 237 238 if (!cacheSize.equals("0")) { 239 envConfig.setConfigParam("je.maxMemory", cacheSize); 240 } 241 env = new Environment(envHome, envConfig); 242 243 DatabaseConfig dbConfig = new DatabaseConfig(); 244 dbConfig.setAllowCreate(true); 245 dbConfig.setSortedDuplicates(true); 246 dbConfig.setTransactional(true); 247 db = env.openDatabase(null, "InsertAndDelete", dbConfig); 248 } 249 250 private void setupEnv(boolean inMemory) 251 throws Exception { 252 253 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 254 255 DbInternal.disableParameterValidation(envConfig); 256 envConfig.setConfigParam(EnvironmentParams.LOG_MEM_SIZE.getName(), 257 EnvironmentParams.LOG_MEM_SIZE_MIN_STRING); 258 envConfig.setConfigParam 259 (EnvironmentParams.LOG_FILE_MAX.getName(), "90"); 260 envConfig.setConfigParam 261 (EnvironmentParams.NUM_LOG_BUFFERS.getName(), "2"); 262 envConfig.setAllowCreate(true); 263 if (inMemory) { 264 265 envConfig.setConfigParam 266 (EnvironmentParams.LOG_MEMORY_ONLY.getName(), "true"); 267 } 268 environment = new EnvironmentImpl(envHome, envConfig); 269 270 271 environment.close(); 272 environment.open(); 273 fileManager = new FileManager(environment, envHome, false); 274 bufPool = new LogBufferPool(fileManager, environment); 275 276 281 TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX); 282 } 283 } 284 | Popular Tags |