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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: EvictSelectionTest.java,v 1.22.2.3 2010/01/04 15:30:43 cwl Exp $
*/
package com.sleepycat.je.evictor;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import junit.framework.TestCase;
import com.sleepycat.bind.tuple.IntegerBinding;
import com.sleepycat.je.Cursor;
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.EnvironmentMutableConfig;
import com.sleepycat.je.EnvironmentStats;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.StatsConfig;
import com.sleepycat.je.config.EnvironmentParams;
import com.sleepycat.je.dbi.EnvironmentImpl;
import com.sleepycat.je.dbi.INList;
import com.sleepycat.je.dbi.MemoryBudget;
import com.sleepycat.je.log.FileManager;
import com.sleepycat.je.tree.IN;
import com.sleepycat.je.util.TestUtils;
public class EvictSelectionTest extends TestCase {
private static boolean DEBUG = false;
private static String DB_NAME = "EvictionSelectionTestDb";
private File envHome;
private int scanSize = 5;
private Environment env;
private EnvironmentImpl envImpl;
public EvictSelectionTest() {
envHome = new File(System.getProperty(TestUtils.DEST_DIR));
}
public void setUp()
throws Exception {
TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
}
public void tearDown()
throws Exception {
try {
if (env != null) {
env.close();
}
} catch (Throwable e) {
System.out.println("tearDown: " + e);
}
env = null;
envImpl = null;
try {
TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
} catch (Throwable e) {
System.out.println("tearDown: " + e);
}
}
public void testEvictPass()
throws Throwable {
/* Create an environment, database, and insert some data. */
initialize(true);
/* The SharedEvictor is not testable using getExpectedCandidates. */
if (env.getConfig().getSharedCache()) {
env.close();
env = null;
return;
}
EnvironmentStats stats = new EnvironmentStats();
StatsConfig statsConfig = new StatsConfig();
statsConfig.setClear(true);
/*
* Set up the test w/a number of INs that doesn't divide evenly
* into scan sets.
*/
int startingNumINs = envImpl.getInMemoryINs().getSize();
assertTrue((startingNumINs % scanSize) != 0);
Evictor evictor = envImpl.getEvictor();
/* Evict once to initialize the scan iterator. */
evictor.evictBatch("test", false, 1);
evictor.loadStats(statsConfig, stats);
/*
* Test evictBatch, where each batch only evicts one node because
* we are passing one byte for the currentRequiredEvictBytes
* parameter. To predict the evicted nodes when more than one
* target is selected, we would have to simulate eviction and
* maintain a parallel IN tree, which is too complex.
*/
for (int batch = 1;; batch += 1) {
List<Long> expectedCandidates = new ArrayList<Long>();
int expectedNScanned = getExpectedCandidates
(envImpl, evictor, expectedCandidates);
evictor.evictBatch("test", false, 1);
evictor.loadStats(statsConfig, stats);
assertEquals(1, stats.getNEvictPasses());
assertEquals(expectedNScanned, stats.getNNodesScanned());
List<Long> candidates = evictor.evictProfile.getCandidates();
assertEquals(expectedCandidates, candidates);
/* Stop when no more nodes are evictable. */
if (expectedCandidates.isEmpty()) {
break;
}
}
env.close();
env = null;
}
/*
* We might call evict on an empty INList if the cache is set very low
* at recovery time.
*/
public void testEmptyINList()
throws Throwable {
/* Create an environment, database, and insert some data. */
initialize(true);
env.close();
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setCacheSize(MemoryBudget.MIN_MAX_MEMORY_SIZE);
env = new Environment(envHome, envConfig);
env.close();
env = null;
}
/*
* Create an environment, database, and insert some data.
*/
private void initialize(boolean makeDatabase)
throws DatabaseException {
/* Environment */
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setAllowCreate(true);
envConfig.setConfigParam(EnvironmentParams.
ENV_RUN_EVICTOR.getName(),
"false");
envConfig.setConfigParam(EnvironmentParams.
ENV_RUN_CLEANER.getName(),
"false");
envConfig.setConfigParam(EnvironmentParams.
ENV_RUN_CHECKPOINTER.getName(),
"false");
envConfig.setConfigParam(EnvironmentParams.
ENV_RUN_INCOMPRESSOR.getName(),
"false");
envConfig.setConfigParam(EnvironmentParams.
NODE_MAX.getName(), "4");
envConfig.setConfigParam(EnvironmentParams.
EVICTOR_NODES_PER_SCAN.getName(), "5");
if (DEBUG) {
envConfig.setConfigParam(EnvironmentParams.
JE_LOGGING_CONSOLE.getName(), "true");
envConfig.setConfigParam(EnvironmentParams.
JE_LOGGING_LEVEL_EVICTOR.getName(),
"SEVERE");
}
env = new Environment(envHome, envConfig);
envImpl = DbInternal.envGetEnvironmentImpl(env);
if (makeDatabase) {
/* Database */
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
Database db = env.openDatabase(null, "foo", dbConfig);
/* Insert enough keys to get an odd number of nodes */
DatabaseEntry keyAndData = new DatabaseEntry();
for (int i = 0; i < 110; i++) {
IntegerBinding.intToEntry(i, keyAndData);
db.put(null, keyAndData, keyAndData);
}
db.close();
}
}
/**
* Returns the number of INs selected (examined) and fills the expected
* list with the selected targets. Currently only one target is selected.
*/
private int getExpectedCandidates(EnvironmentImpl envImpl,
Evictor evictor,
List<Long> expected)
throws DatabaseException {
if (!envImpl.getMemoryBudget().isTreeUsageAboveMinimum()) {
return 0;
}
boolean evictByLruOnly = envImpl.getConfigManager().getBoolean
(EnvironmentParams.EVICTOR_LRU_ONLY);
INList inList = envImpl.getInMemoryINs();
Iterator<IN> inIter = evictor.getScanIterator();
IN firstScanned = null;
boolean firstWrapped = false;
long targetGeneration = Long.MAX_VALUE;
int targetLevel = Integer.MAX_VALUE;
boolean targetDirty = true;
IN target = null;
boolean wrapped = false;
int nIterated = 0;
int maxNodesToIterate = evictor.getMaxINsPerBatch();
int nCandidates = 0;
/* Simulate the eviction alorithm. */
while (nIterated < maxNodesToIterate && nCandidates < scanSize) {
if (!inIter.hasNext()) {
inIter = inList.iterator();
wrapped = true;
}
IN in = inIter.next();
nIterated += 1;
if (firstScanned == null) {
firstScanned = in;
firstWrapped = wrapped;
}
if (in.getDatabase() == null || in.getDatabase().isDeleted()) {
continue;
}
int evictType = in.getEvictionType();
if (evictType == IN.MAY_NOT_EVICT) {
continue;
}
if (evictByLruOnly) {
if (in.getGeneration() < targetGeneration) {
targetGeneration = in.getGeneration();
target = in;
}
} else {
int level = evictor.normalizeLevel(in, evictType);
if (targetLevel != level) {
if (targetLevel > level) {
targetLevel = level;
targetDirty = in.getDirty();
targetGeneration = in.getGeneration();
target = in;
}
} else if (targetDirty != in.getDirty()) {
if (targetDirty) {
targetDirty = false;
targetGeneration = in.getGeneration();
target = in;
}
} else {
if (targetGeneration > in.getGeneration()) {
targetGeneration = in.getGeneration();
target = in;
}
}
}
nCandidates++;
}
/*
* Restore the Evictor's iterator position to just before the
* firstScanned IN. There is no way to clone an iterator and we can't
* create a tailSet iterator because the map is unsorted.
*/
int prevPosition = 0;
if (firstWrapped) {
for (IN in : inList) {
prevPosition += 1;
}
} else {
boolean firstScannedFound = false;
for (IN in : inList) {
if (in == firstScanned) {
firstScannedFound = true;
break;
} else {
prevPosition += 1;
}
}
assertTrue(firstScannedFound);
}
inIter = inList.iterator();
while (prevPosition > 0) {
inIter.next();
prevPosition -= 1;
}
evictor.setScanIterator(inIter);
/* Return the expected IN. */
expected.clear();
if (target != null) {
expected.add(new Long(target.getNodeId()));
}
return nIterated;
}
/**
* Tests a fix for an eviction bug that could cause an OOME in a read-only
* environment. [#17590]
*
* Before the bug fix, a dirty IN prevented eviction from working if the
* dirty IN is returned by Evictor.selectIN repeatedly, only to be rejected
* by Evictor.evictIN because it is dirty. A dirty IN was considered as a
* target and sometimes selected by selectIN as a way to avoid an infinite
* loop when all INs are dirty. This is unnecessary, since a condition was
* added to cause the selectIN loop to terminate when all INs in the INList
* have been iterated. Now, with the fix, a dirty IN in a read-only
* environment is never considered as a target or returned by selectIN.
*
* The OOME was reproduced with a simple test that uses a cursor to iterate
* through 100k records, each 100k in size, in a read-only enviroment with
* a 16m heap. However, reproducing the problem in a fast-running unit
* test is very difficult. Instead, since the code change only impacts a
* read-only environment, this unit test only ensures that the fix does not
* cause an infinte loop when all nodes are dirty.
*/
public void testReadOnlyAllDirty()
throws Throwable {
/* Create an environment, database, and insert some data. */
initialize(true /*makeDatabase*/);
env.close();
env = null;
envImpl = null;
/* Open the environment read-only. */
final EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setReadOnly(true);
env = new Environment(envHome, envConfig);
envImpl = DbInternal.envGetEnvironmentImpl(env);
/* Load everything into cache. */
{
final DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setReadOnly(true);
final Database db = env.openDatabase(null, "foo", dbConfig);
final Cursor cursor = db.openCursor(null, null);
final DatabaseEntry key = new DatabaseEntry();
final DatabaseEntry data = new DatabaseEntry();
OperationStatus status = cursor.getFirst(key, data, null);
while (status == OperationStatus.SUCCESS) {
status = cursor.getNext(key, data, null);
}
cursor.close();
db.close();
}
/* Artificially make all nodes dirty in a read-only environment. */
for (IN in : envImpl.getInMemoryINs()) {
in.setDirty(true);
}
/*
* Force an eviction. No nodes will be selected for an eviction,
* because all nodes are dirty. If the (nIterated < maxNodesToIterate)
* condition is removed from the selectIN loop, an infinite loop will
* occur.
*/
final EnvironmentMutableConfig mutableConfig = env.getMutableConfig();
mutableConfig.setCacheSize(MemoryBudget.MIN_MAX_MEMORY_SIZE);
env.setMutableConfig(mutableConfig);
final StatsConfig clearStats = new StatsConfig();
clearStats.setClear(true);
EnvironmentStats stats = env.getStats(clearStats);
env.evictMemory();
stats = env.getStats(clearStats);
assertEquals(0, stats.getNNodesSelected());
env.close();
env = null;
envImpl = null;
}
}
|