1 8 9 package com.sleepycat.je.evictor; 10 11 import java.io.File ; 12 import java.util.ArrayList ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 import junit.framework.TestCase; 17 18 import com.sleepycat.bind.tuple.IntegerBinding; 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.EnvironmentStats; 27 import com.sleepycat.je.StatsConfig; 28 import com.sleepycat.je.config.EnvironmentParams; 29 import com.sleepycat.je.dbi.DbTree; 30 import com.sleepycat.je.dbi.EnvironmentImpl; 31 import com.sleepycat.je.dbi.INList; 32 import com.sleepycat.je.dbi.MemoryBudget; 33 import com.sleepycat.je.log.FileManager; 34 import com.sleepycat.je.tree.IN; 35 import com.sleepycat.je.util.TestUtils; 36 37 public class EvictSelectionTest extends TestCase { 38 private static boolean DEBUG = false; 39 private static String DB_NAME = "EvictionSelectionTestDb"; 40 private File envHome; 41 private int scanSize = 5; 42 private Environment env; 43 private EnvironmentImpl envImpl; 44 45 public EvictSelectionTest() { 46 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 47 } 48 49 public void setUp() 50 throws Exception { 51 52 TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX); 53 } 54 55 public void tearDown() 56 throws Exception { 57 58 TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX); 59 } 60 61 62 public void testEvictPass() 63 throws Throwable { 64 65 66 try { 67 initialize(true); 68 69 EnvironmentStats stats = new EnvironmentStats(); 70 StatsConfig statsConfig = new StatsConfig(); 71 statsConfig.setClear(true); 72 73 77 int startingNumINs = envImpl.getInMemoryINs().getSize(); 78 assertTrue((startingNumINs % scanSize) != 0); 79 80 Evictor evictor = envImpl.getEvictor(); 81 evictor.loadStats(statsConfig, stats); 82 83 90 for (int batch = 1;; batch += 1) { 91 92 List expectedCandidates = new ArrayList (); 93 int expectedNScanned = getExpectedCandidates 94 (envImpl, evictor, expectedCandidates); 95 96 evictor.evictBatch("test", false, 1); 97 98 evictor.loadStats(statsConfig, stats); 99 assertEquals(1, stats.getNEvictPasses()); 100 assertEquals(expectedNScanned, stats.getNNodesScanned()); 101 102 List candidates = evictor.evictProfile.getCandidates(); 103 assertEquals(expectedCandidates, candidates); 104 105 106 if (expectedCandidates.isEmpty()) { 107 break; 108 } 109 } 110 111 env.close(); 112 } catch (Throwable t) { 113 t.printStackTrace(); 114 throw (t); 115 } 116 } 117 118 122 public void testEmptyINList() 123 throws Throwable { 124 125 126 try { 127 initialize(true); 128 129 env.close(); 130 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 131 envConfig.setCacheSize(MemoryBudget.MIN_MAX_MEMORY_SIZE); 132 env = new Environment(envHome, envConfig); 133 env.close(); 134 } catch (Throwable t) { 135 t.printStackTrace(); 136 throw (t); 137 } 138 } 139 140 143 private void initialize(boolean makeDatabase) 144 throws DatabaseException { 145 146 147 148 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 149 envConfig.setAllowCreate(true); 150 envConfig.setConfigParam(EnvironmentParams. 151 ENV_RUN_EVICTOR.getName(), 152 "false"); 153 envConfig.setConfigParam(EnvironmentParams. 154 ENV_RUN_CLEANER.getName(), 155 "false"); 156 envConfig.setConfigParam(EnvironmentParams. 157 ENV_RUN_CHECKPOINTER.getName(), 158 "false"); 159 envConfig.setConfigParam(EnvironmentParams. 160 ENV_RUN_INCOMPRESSOR.getName(), 161 "false"); 162 envConfig.setConfigParam(EnvironmentParams. 163 NODE_MAX.getName(), "4"); 164 envConfig.setConfigParam(EnvironmentParams. 165 EVICTOR_NODES_PER_SCAN.getName(), "5"); 166 if (DEBUG) { 167 envConfig.setConfigParam(EnvironmentParams. 168 JE_LOGGING_CONSOLE.getName(), "true"); 169 envConfig.setConfigParam(EnvironmentParams. 170 JE_LOGGING_LEVEL_EVICTOR.getName(), 171 "SEVERE"); 172 } 173 env = new Environment(envHome, envConfig); 174 envImpl = DbInternal.envGetEnvironmentImpl(env); 175 176 if (makeDatabase) { 177 178 179 DatabaseConfig dbConfig = new DatabaseConfig(); 180 dbConfig.setAllowCreate(true); 181 Database db = env.openDatabase(null, "foo", dbConfig); 182 183 184 185 DatabaseEntry keyAndData = new DatabaseEntry(); 186 for (int i = 0; i < 110; i++) { 187 IntegerBinding.intToEntry(i, keyAndData); 188 db.put(null, keyAndData, keyAndData); 189 } 190 191 db.close(); 192 } 193 } 194 195 199 private int getExpectedCandidates(EnvironmentImpl envImpl, 200 Evictor evictor, 201 List expected) 202 throws DatabaseException { 203 204 boolean evictByLruOnly = envImpl.getConfigManager().getBoolean 205 (EnvironmentParams.EVICTOR_LRU_ONLY); 206 207 INList inList = envImpl.getInMemoryINs(); 208 inList.latchMajor(); 209 210 IN nextNode = evictor.getNextNode(); 211 if (nextNode == null) { 212 nextNode = (IN) inList.first(); 213 } 214 Iterator inIter = inList.tailSet(nextNode).iterator(); 215 216 long targetGeneration = Long.MAX_VALUE; 217 int targetLevel = Integer.MAX_VALUE; 218 boolean targetDirty = true; 219 IN target = null; 220 221 boolean wrapped = false; 222 int nScanned = 0; 223 int nCandidates = 0; 224 while (nCandidates < scanSize) { 225 226 if (!inIter.hasNext()) { 227 if (wrapped) { 228 break; 229 } else { 230 inIter = inList.tailSet(inList.first()).iterator(); 231 wrapped = true; 232 } 233 } 234 235 IN in = (IN) inIter.next(); 236 nScanned += 1; 237 238 if (in.getDatabase() == null || in.getDatabase().isDeleted()) { 239 continue; 240 } 241 242 if (in.getDatabase().getId().equals(DbTree.ID_DB_ID)) { 243 continue; 244 } 245 246 int evictType = in.getEvictionType(); 247 if (evictType == IN.MAY_NOT_EVICT) { 248 continue; 249 } 250 251 if (evictByLruOnly) { 252 if (in.getGeneration() < targetGeneration) { 253 targetGeneration = in.getGeneration(); 254 target = in; 255 } 256 } else { 257 int level = evictor.normalizeLevel(in, evictType); 258 if (targetLevel != level) { 259 if (targetLevel > level) { 260 targetLevel = level; 261 targetDirty = in.getDirty(); 262 targetGeneration = in.getGeneration(); 263 target = in; 264 } 265 } else if (targetDirty != in.getDirty()) { 266 if (targetDirty) { 267 targetDirty = false; 268 targetGeneration = in.getGeneration(); 269 target = in; 270 } 271 } else { 272 if (targetGeneration > in.getGeneration()) { 273 targetGeneration = in.getGeneration(); 274 target = in; 275 } 276 } 277 } 278 279 nCandidates++; 280 } 281 282 inList.releaseMajorLatch(); 283 284 expected.clear(); 285 if (target != null) { 286 expected.add(new Long (target.getNodeId())); 287 } 288 return nScanned; 289 } 290 } 291 | Popular Tags |