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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: LogBufferPoolTest.java,v 1.66.2.2 2010/01/04 15:30:44 cwl Exp $
*/
package com.sleepycat.je.log;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import junit.framework.TestCase;
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.config.EnvironmentParams;
import com.sleepycat.je.dbi.EnvironmentImpl;
import com.sleepycat.je.dbi.MemoryBudget;
import com.sleepycat.je.utilint.DbLsn;
import com.sleepycat.je.util.TestUtils;
public class LogBufferPoolTest extends TestCase {
Environment env;
Database db;
EnvironmentImpl environment;
FileManager fileManager;
File envHome;
LogBufferPool bufPool;
public LogBufferPoolTest() {
super();
envHome = new File(System.getProperty(TestUtils.DEST_DIR));
}
protected void setUp()
throws Exception {
/* Remove files to start with a clean slate. */
TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
}
protected void tearDown()
throws Exception {
bufPool = null;
if (fileManager != null) {
fileManager.clear();
fileManager.close();
}
TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
}
/**
* Make sure that we'll add more buffers as needed.
*/
public void testGrowBuffers()
throws Throwable {
try {
setupEnv(true);
/*
* Each buffer can only hold 2 items. Put enough test items in to
* get seven buffers.
*/
List<Long> lsns = new ArrayList<Long>();
for (int i = 0; i < 14; i++) {
long lsn = insertData(bufPool, (byte)(i + 1));
lsns.add(new Long(lsn));
}
/*
* Check that the bufPool knows where each LSN lives and that the
* fetched buffer does hold this item.
*/
LogBuffer logBuf;
ByteBuffer b;
for (int i = 0; i < 14; i++) {
/*
* For each test LSN, ask the bufpool for the logbuffer that
* houses it.
*/
long testLsn = DbLsn.longToLsn(lsns.get(i));
logBuf = bufPool.getReadBuffer(testLsn);
assertNotNull(logBuf);
/* Here's the expected data. */
byte[] expected = new byte[10];
Arrays.fill(expected, (byte)(i+1));
/* Here's the data in the log buffer. */
byte[] logData = new byte[10];
b = logBuf.getDataBuffer();
long firstLsnInBuf = logBuf.getFirstLsn();
b.position((int) (DbLsn.getFileOffset(testLsn) -
DbLsn.getFileOffset(firstLsnInBuf)));
logBuf.getDataBuffer().get(logData);
/* They'd better be equal. */
assertTrue(Arrays.equals(logData, expected));
logBuf.release();
}
/*
* This LSN shouldn't be in the buffers, it's less than any
* buffered item.
*/
assertNull(bufPool.getReadBuffer(DbLsn.makeLsn(0,10)));
/*
* This LSN is illegal to ask for, it's greater than any registered
* LSN.
*/
assertNull("LSN too big",
bufPool.getReadBuffer(DbLsn.makeLsn(10, 141)));
} catch (Throwable t) {
t.printStackTrace();
throw t;
}
}
/**
* Helper to insert fake data.
* @return LSN registered for this fake data
*/
private long insertData(LogBufferPool bufPool,
byte value)
throws IOException, DatabaseException {
byte[] data = new byte[10];
Arrays.fill(data, value);
boolean flippedFile = fileManager.bumpLsn(data.length);
LogBuffer logBuf = bufPool.getWriteBuffer(data.length, flippedFile);
logBuf.getDataBuffer().put(data);
long lsn = fileManager.getLastUsedLsn();
bufPool.writeCompleted(fileManager.getLastUsedLsn(), false);
return lsn;
}
/**
* Test buffer flushes.
*/
public void testBufferFlush()
throws Throwable {
try {
setupEnv(false);
assertFalse("There should be no files", fileManager.filesExist());
fileManager.VERIFY_CHECKSUMS = false;
/*
* Each buffer can only hold 2 items. Put enough test items in to
* get five buffers.
*/
/* CWL: What is lsnList used for? */
List<Long> lsnList = new ArrayList<Long>();
for (int i = 0; i < 9; i++) {
long lsn = insertData(bufPool, (byte)(i+1));
lsnList.add(new Long(lsn));
}
bufPool.writeBufferToFile(0);
fileManager.syncLogEnd();
/* We should see two files exist. */
String[] fileNames =
fileManager.listFiles(FileManager.JE_SUFFIXES);
assertEquals("Should be 2 files", 2, fileNames.length);
/* Read the files. */
if (false) {
ByteBuffer dataBuffer = ByteBuffer.allocate(100);
FileHandle file0 = fileManager.getFileHandle(0L);
RandomAccessFile file = file0.getFile();
FileChannel channel = file.getChannel();
int bytesRead = channel.read(dataBuffer,
FileManager.firstLogEntryOffset());
dataBuffer.flip();
assertEquals("Check bytes read", 50, bytesRead);
assertEquals("Check size of file", 50, dataBuffer.limit());
file.close();
FileHandle file1 = fileManager.getFileHandle(1L);
file = file1.getFile();
channel = file.getChannel();
bytesRead = channel.read(dataBuffer,
FileManager.firstLogEntryOffset());
dataBuffer.flip();
assertEquals("Check bytes read", 40, bytesRead);
assertEquals("Check size of file", 40, dataBuffer.limit());
file0.release();
file1.release();
}
} catch (Throwable e) {
e.printStackTrace();
throw e;
}
}
public void testTemporaryBuffers()
throws Exception {
final int KEY_SIZE = 10;
final int DATA_SIZE = 1000000;
tempBufferInitEnvInternal
("0", MemoryBudget.MIN_MAX_MEMORY_SIZE_STRING);
DatabaseEntry key = new DatabaseEntry(new byte[KEY_SIZE]);
DatabaseEntry data = new DatabaseEntry(new byte[DATA_SIZE]);
db.put(null, key, data);
db.close();
env.close();
}
private void tempBufferInitEnvInternal(String buffSize, String cacheSize)
throws DatabaseException {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
if (!buffSize.equals("0")) {
envConfig.setConfigParam("je.log.totalBufferBytes", buffSize);
}
if (!cacheSize.equals("0")) {
envConfig.setConfigParam("je.maxMemory", cacheSize);
}
env = new Environment(envHome, envConfig);
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
dbConfig.setSortedDuplicates(true);
dbConfig.setTransactional(true);
db = env.openDatabase(null, "InsertAndDelete", dbConfig);
}
private void setupEnv(boolean inMemory)
throws Exception {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
DbInternal.disableParameterValidation(envConfig);
envConfig.setConfigParam(EnvironmentParams.LOG_MEM_SIZE.getName(),
EnvironmentParams.LOG_MEM_SIZE_MIN_STRING);
envConfig.setConfigParam
(EnvironmentParams.LOG_FILE_MAX.getName(), "90");
envConfig.setConfigParam
(EnvironmentParams.NUM_LOG_BUFFERS.getName(), "2");
envConfig.setAllowCreate(true);
if (inMemory) {
/* Make the bufPool grow some buffers. Disable writing. */
envConfig.setConfigParam
(EnvironmentParams.LOG_MEMORY_ONLY.getName(), "true");
}
environment = new EnvironmentImpl(envHome,
envConfig,
null /*sharedCacheEnv*/,
false /*replicationIntended*/);
/* Make a standalone file manager for this test. */
environment.close();
environment.open(); /* Just sets state to OPEN. */
fileManager = new FileManager(environment, envHome, false);
bufPool = new LogBufferPool(fileManager, environment);
/*
* Remove any files after the environment is created again! We want to
* remove the files made by recovery, so we can test the file manager
* in controlled cases.
*/
TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
}
}
|