1 8 9 package com.sleepycat.je.tree; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.util.Iterator ; 14 15 import junit.framework.TestCase; 16 17 import com.sleepycat.je.CheckpointConfig; 18 import com.sleepycat.je.Cursor; 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.LockMode; 27 import com.sleepycat.je.OperationStatus; 28 import com.sleepycat.je.Transaction; 29 import com.sleepycat.je.config.EnvironmentParams; 30 import com.sleepycat.je.dbi.EnvironmentImpl; 31 import com.sleepycat.je.dbi.INList; 32 import com.sleepycat.je.log.FileManager; 33 import com.sleepycat.je.txn.Txn; 34 import com.sleepycat.je.util.TestUtils; 35 36 39 public class MemorySizeTest extends TestCase { 40 private static final boolean DEBUG = false; 41 42 private Environment env; 43 private File envHome; 44 private Database db; 45 46 public MemorySizeTest() 47 throws DatabaseException { 48 49 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 50 51 52 Key.DUMP_BINARY = true; 53 } 54 55 public void setUp() 56 throws IOException , DatabaseException { 57 58 IN.ACCUMULATED_LIMIT = 0; 59 Txn.ACCUMULATED_LIMIT = 0; 60 61 TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX); 62 63 67 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 68 envConfig.setConfigParam(EnvironmentParams.ENV_RUN_EVICTOR.getName(), 69 "false"); 70 envConfig.setConfigParam( 71 EnvironmentParams.ENV_RUN_INCOMPRESSOR.getName(), 72 "false"); 73 envConfig.setConfigParam( 74 EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(), 75 "false"); 76 envConfig.setConfigParam( 77 EnvironmentParams.ENV_RUN_CLEANER.getName(), 78 "false"); 79 80 81 DbInternal.setCheckpointUP(envConfig, false); 82 83 envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "4"); 84 envConfig.setAllowCreate(true); 85 envConfig.setTxnNoSync(Boolean.getBoolean(TestUtils.NO_SYNC)); 86 envConfig.setTransactional(true); 87 env = new Environment(envHome, envConfig); 88 } 89 90 public void tearDown() 91 throws IOException , DatabaseException { 92 93 if (env != null) { 94 try { 95 env.close(); 96 } catch (DatabaseException E) { 97 } 98 } 99 TestUtils.removeFiles("TearDown", envHome, 100 FileManager.JE_SUFFIX, true); 101 } 102 103 117 public void testMemSizeMaintenance() 118 throws Throwable { 119 120 EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env); 121 try { 122 initDb(); 123 124 125 insert((byte) 1, 10, (byte) 1, 100, true); 126 long newSize = TestUtils.validateNodeMemUsage(envImpl, true); 127 assertTrue(newSize > 0); 128 129 130 insert((byte) 2, 10, (byte) 2, 100, true); 131 insert((byte) 3, 10, (byte) 3, 100, true); 132 insert((byte) 4, 10, (byte) 4, 100, true); 133 long oldSize = newSize; 134 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 135 assertTrue(newSize > oldSize); 136 137 138 insert((byte) 5, 10, (byte) 5, 100, true); 139 insert((byte) 6, 10, (byte) 6, 100, true); 140 insert((byte) 7, 10, (byte) 7, 100, true); 141 oldSize = newSize; 142 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 143 assertTrue(newSize > oldSize); 144 145 146 modify((byte) 1, 10, (byte) 1, 1010, true); 147 modify((byte) 7, 10, (byte) 7, 1010, true); 148 oldSize = newSize; 149 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 150 assertTrue(newSize > oldSize); 151 152 153 delete((byte) 2, 10, true); 154 delete((byte) 6, 10, true); 155 oldSize = newSize; 156 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 157 assertTrue(newSize < oldSize); 158 159 160 compress(); 161 oldSize = newSize; 162 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 163 assertTrue(newSize < oldSize); 164 165 166 CheckpointConfig ckptConfig = new CheckpointConfig(); 167 ckptConfig.setForce(true); 168 env.checkpoint(ckptConfig); 169 oldSize = newSize; 170 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 171 assertEquals(oldSize, newSize); 172 173 174 evict(); 175 TestUtils.validateNodeMemUsage(envImpl, true); 176 oldSize = newSize; 177 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 178 assertTrue(newSize < oldSize); 179 180 181 insert((byte) 3, 10, (byte) 30, 200, true); 182 insert((byte) 3, 10, (byte) 31, 200, true); 183 insert((byte) 3, 10, (byte) 32, 200, true); 184 insert((byte) 3, 10, (byte) 33, 200, true); 185 oldSize = newSize; 186 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 187 assertTrue(newSize > oldSize); 188 189 190 insert((byte) 3, 10, (byte) 34, 200, true); 191 insert((byte) 3, 10, (byte) 35, 200, true); 192 oldSize = newSize; 193 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 194 assertTrue(newSize > oldSize); 195 196 197 checkCount(11); 198 oldSize = newSize; 199 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 200 assertTrue(newSize > oldSize); 201 202 203 modify((byte) 5, 10, (byte) 30, 1000, false); 204 oldSize = newSize; 205 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 206 assertTrue(newSize == oldSize); 207 208 209 delete((byte) 1, 10, false); 210 delete((byte) 7, 10, false); 211 oldSize = newSize; 212 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 213 214 215 delete((byte) 3, 10, (byte)34, 200, false); 216 oldSize = newSize; 217 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 218 219 220 insert((byte) 2, 10, (byte) 5, 100, false); 221 insert((byte) 6, 10, (byte) 6, 100, false); 222 insert((byte) 8, 10, (byte) 7, 100, false); 223 oldSize = newSize; 224 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 225 226 } catch (Throwable t) { 227 t.printStackTrace(); 228 throw t; 229 } finally { 230 if (db != null) { 231 db.close(); 232 } 233 234 if (env != null) { 235 env.close(); 236 } 237 238 239 } 240 } 241 242 250 public void testSlotReuseMaintenance() 251 throws Exception { 252 253 EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env); 254 try { 255 256 initDb(); 257 258 259 insert((byte) 1, 10, (byte) 1, 100, true); 260 insert((byte) 2, 10, (byte) 2, 100, true); 261 insert((byte) 3, 10, (byte) 3, 100, true); 262 long newSize = TestUtils.validateNodeMemUsage(envImpl, true); 263 264 265 delete((byte) 3, 10, true); 266 long oldSize = newSize; 267 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 268 assertTrue(newSize < oldSize); 269 270 271 insert((byte) 3, 10, (byte) 2, 400, true); 272 oldSize = newSize; 273 newSize = TestUtils.validateNodeMemUsage(envImpl, true); 274 assertTrue(newSize > oldSize); 275 } catch (Exception e) { 276 e.printStackTrace(); 277 throw e; 278 } finally { 279 if (db != null) { 280 db.close(); 281 } 282 283 if (env != null) { 284 env.close(); 285 } 286 } 287 } 288 289 290 private void initDb() 291 throws DatabaseException { 292 293 DatabaseConfig dbConfig = new DatabaseConfig(); 294 dbConfig.setAllowCreate(true); 295 dbConfig.setSortedDuplicates(true); 296 dbConfig.setTransactional(true); 297 db = env.openDatabase(null, "foo", dbConfig); 298 } 299 300 private void insert(byte keyVal, int keySize, 301 byte dataVal, int dataSize, 302 boolean commit) 303 throws DatabaseException { 304 305 Transaction txn = null; 306 if (!commit) { 307 txn = env.beginTransaction(null, null); 308 } 309 assertEquals(OperationStatus.SUCCESS, 310 db.put(null, getEntry(keyVal, keySize), 311 getEntry(dataVal, dataSize))); 312 if (!commit) { 313 txn.abort(); 314 } 315 } 316 317 private void modify(byte keyVal, int keySize, 318 byte dataVal, int dataSize, 319 boolean commit) 320 throws DatabaseException { 321 322 Transaction txn = null; 323 324 txn = env.beginTransaction(null, null); 325 Cursor cursor = db.openCursor(txn, null); 326 assertEquals(OperationStatus.SUCCESS, 327 cursor.getSearchKey(getEntry(keyVal, keySize), 328 new DatabaseEntry(), 329 LockMode.DEFAULT)); 330 assertEquals(OperationStatus.SUCCESS, 331 cursor.delete()); 332 assertEquals(OperationStatus.SUCCESS, 333 cursor.put(getEntry(keyVal, keySize), 334 getEntry(dataVal, dataSize))); 335 cursor.close(); 336 337 if (commit) { 338 txn.commit(); 339 } else { 340 txn.abort(); 341 } 342 } 343 344 private void delete(byte keyVal, int keySize, boolean commit) 345 throws DatabaseException { 346 347 Transaction txn = null; 348 if (!commit) { 349 txn = env.beginTransaction(null, null); 350 } 351 assertEquals(OperationStatus.SUCCESS, 352 db.delete(txn, getEntry(keyVal, keySize))); 353 if (!commit) { 354 txn.abort(); 355 } 356 } 357 private void delete(byte keyVal, int keySize, 358 byte dataVal, int dataSize, boolean commit) 359 throws DatabaseException { 360 361 Transaction txn = env.beginTransaction(null, null); 362 Cursor cursor = db.openCursor(txn, null); 363 assertEquals(OperationStatus.SUCCESS, 364 cursor.getSearchBoth(getEntry(keyVal, keySize), 365 getEntry(dataVal, dataSize), 366 LockMode.DEFAULT)); 367 assertEquals(OperationStatus.SUCCESS, cursor.delete()); 368 cursor.close(); 369 370 if (commit) { 371 txn.commit(); 372 } else { 373 txn.abort(); 374 } 375 } 376 377 381 private void compress() 382 throws DatabaseException { 383 384 INList inList = DbInternal.envGetEnvironmentImpl(env).getInMemoryINs(); 385 inList.latchMajor(); 386 try { 387 Iterator iter = inList.iterator(); 388 while (iter.hasNext()) { 389 IN in = (IN) iter.next(); 390 in.latch(); 391 if (in instanceof BIN) { 392 in.compress(null, true); 393 } 394 in.releaseLatch(); 395 } 396 } finally { 397 inList.releaseMajorLatch(); 398 } 399 } 400 401 405 private void evict() 406 throws DatabaseException { 407 408 INList inList = DbInternal.envGetEnvironmentImpl(env).getInMemoryINs(); 409 inList.latchMajor(); 410 try { 411 Iterator iter = inList.iterator(); 412 while (iter.hasNext()) { 413 IN in = (IN) iter.next(); 414 if (in instanceof BIN) { 415 BIN bin = (BIN) in; 416 bin.latch(); 417 assertTrue(bin.evictLNs() > 0); 418 bin.releaseLatch(); 419 } 420 } 421 } finally { 422 inList.releaseMajorLatch(); 423 } 424 } 425 426 427 private DatabaseEntry getEntry(byte val, int size) { 428 byte [] bArray = new byte[size]; 429 bArray[0] = val; 430 return new DatabaseEntry(bArray); 431 } 432 433 private void checkCount(int expectedCount) 434 throws DatabaseException { 435 436 Cursor cursor = db.openCursor(null, null); 437 int count = 0; 438 while (cursor.getNext(new DatabaseEntry(), new DatabaseEntry(), 439 LockMode.DEFAULT) == OperationStatus.SUCCESS) { 440 count++; 441 } 442 cursor.close(); 443 assertEquals(expectedCount, count); 444 } 445 446 private void dumpINList() 447 throws DatabaseException { 448 449 EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env); 450 INList inList = envImpl.getInMemoryINs(); 451 inList.latchMajor(); 452 try { 453 Iterator iter = inList.iterator(); 454 while (iter.hasNext()) { 455 IN in = (IN) iter.next(); 456 System.out.println("in nodeId=" + in.getNodeId()); 457 } 458 } finally { 459 inList.releaseMajorLatch(); 460 } 461 } 462 } 463 | Popular Tags |