1 8 9 package com.sleepycat.je.util; 10 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.FileOutputStream ; 14 import java.io.FilenameFilter ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.OutputStream ; 18 import java.text.NumberFormat ; 19 import java.util.Iterator ; 20 import java.util.Random ; 21 22 import junit.framework.TestCase; 23 24 import com.sleepycat.je.Cursor; 25 import com.sleepycat.je.Database; 26 import com.sleepycat.je.DatabaseException; 27 import com.sleepycat.je.DbInternal; 28 import com.sleepycat.je.DbTestProxy; 29 import com.sleepycat.je.Environment; 30 import com.sleepycat.je.EnvironmentConfig; 31 import com.sleepycat.je.StatsConfig; 32 import com.sleepycat.je.dbi.CursorImpl; 33 import com.sleepycat.je.dbi.EnvironmentImpl; 34 import com.sleepycat.je.dbi.INList; 35 import com.sleepycat.je.latch.LatchSupport; 36 import com.sleepycat.je.log.FileManager; 37 import com.sleepycat.je.tree.BIN; 38 import com.sleepycat.je.tree.ChildReference; 39 import com.sleepycat.je.tree.IN; 40 import com.sleepycat.je.tree.SearchResult; 41 import com.sleepycat.je.tree.Tree; 42 import com.sleepycat.je.tree.WithRootLatched; 43 44 public class TestUtils { 45 public static String DEST_DIR = "testdestdir"; 46 public static String NO_SYNC = "txnnosync"; 47 public static String LONG_TEST = "longtest"; 48 49 public static final String LOG_FILE_NAME = "00000000.jdb"; 50 51 public static final StatsConfig FAST_STATS; 52 53 static { 54 FAST_STATS = new StatsConfig(); 55 FAST_STATS.setFast(true); 56 } 57 58 private static final boolean DEBUG = true; 59 private static Random rnd = new Random (); 60 61 public void debugMsg(String message) { 62 63 if (DEBUG) { 64 System.out.println 65 (Thread.currentThread().toString() + " " + message); 66 } 67 } 68 69 static public void setRandomSeed(int seed) { 70 71 rnd = new Random (seed); 72 } 73 74 static public void generateRandomAlphaBytes(byte[] bytes) { 75 76 byte[] aAndZ = "AZ".getBytes(); 77 int range = aAndZ[1] - aAndZ[0] + 1; 78 79 for (int i = 0; i < bytes.length; i++) { 80 bytes[i] = (byte) (rnd.nextInt(range) + aAndZ[0]); 81 } 82 } 83 84 static public void checkLatchCount() { 85 TestCase.assertTrue(LatchSupport.countLatchesHeld() == 0); 86 } 87 88 static public void printLatchCount(String msg) { 89 System.out.println(msg + " : " + LatchSupport.countLatchesHeld()); 90 } 91 92 static public void printLatches(String msg) { 93 System.out.println(msg + " : "); 94 LatchSupport.dumpLatchesHeld(); 95 } 96 97 102 static public int alphaKey(int i) { 103 104 int ret = 0; 105 for (int j = 0; j < 4; j++) { 106 byte b = (byte) (i % 26); 107 ret <<= 8; 108 ret |= (b + 65); 109 i /= 26; 110 } 111 112 return ret; 113 } 114 115 118 static public void putUnsignedInt(byte[] buf, long value) { 119 120 int i = 0; 121 buf[i++] = (byte) (value >>> 0); 122 buf[i++] = (byte) (value >>> 8); 123 buf[i++] = (byte) (value >>> 16); 124 buf[i] = (byte) (value >>> 24); 125 } 126 127 132 private static boolean removeDisabled() { 133 134 String doRemove = System.getProperty("removeLogFiles"); 135 return ((doRemove != null) && doRemove.equalsIgnoreCase("false")); 136 } 137 138 144 public static void removeLogFiles(String msg, 145 File envFile, 146 boolean checkRemove) 147 throws IOException { 148 149 removeFiles(msg, envFile, FileManager.JE_SUFFIX, checkRemove); 150 } 151 152 158 public static void removeFiles(String msg, 159 File envFile, 160 String suffix) 161 throws IOException { 162 163 removeFiles(msg, envFile, suffix, false); 164 } 165 166 174 public static void removeFiles(String msg, 175 File envFile, 176 String suffix, 177 boolean checkRemove) 178 throws IOException { 179 180 if (checkRemove && removeDisabled()) { 181 return; 182 } 183 184 String [] suffixes = new String [] { suffix }; 185 String [] names = FileManager.listFiles(envFile, suffixes); 186 187 188 for (int i = 0; i < names.length; i++) { 189 File oldFile = new File (envFile, names[i]); 190 boolean done = oldFile.delete(); 191 assert done : 192 msg + " couldn't delete " + names[i] + " out of " + 193 names[names.length - 1]; 194 oldFile = null; 195 } 196 } 197 198 205 public static void removeFiles(File envFile, FilenameFilter filter) 206 throws IOException { 207 208 if (removeDisabled()) { 209 return; 210 } 211 212 File [] targetFiles = envFile.listFiles(filter); 213 214 for (int i = 0; i < targetFiles.length; i++) { 216 boolean done = targetFiles[i].delete(); 217 if (!done) { 218 System.out.println 219 ("Warning, couldn't delete " 220 + targetFiles[i] 221 + " out of " 222 + targetFiles[targetFiles.length - 1]); 223 } 224 } 225 } 226 227 230 public static void copyFiles(File fromDir, File toDir) 231 throws IOException { 232 233 String [] names = fromDir.list(); 234 if (names != null) { 235 for (int i = 0; i < names.length; i += 1) { 236 File fromFile = new File (fromDir, names[i]); 237 if (fromFile.isDirectory()) { 238 continue; 239 } 240 File toFile = new File (toDir, names[i]); 241 int len = (int) fromFile.length(); 242 byte[] data = new byte[len]; 243 FileInputStream fis = null; 244 FileOutputStream fos = null; 245 try { 246 fis = new FileInputStream (fromFile); 247 fos = new FileOutputStream (toFile); 248 fis.read(data); 249 fos.write(data); 250 } finally { 251 if (fis != null) { 252 fis.close(); 253 } 254 if (fos != null) { 255 fos.close(); 256 } 257 } 258 } 259 } 260 } 261 262 267 public static byte[] getTestArray(int val) { 268 269 int length = val % 10; 270 length = length < 4 ? 4 : length; 271 byte[] test = new byte[length]; 272 test[3] = (byte) ((val >>> 0) & 0xff); 273 test[2] = (byte) ((val >>> 8) & 0xff); 274 test[1] = (byte) ((val >>> 16) & 0xff); 275 test[0] = (byte) ((val >>> 24) & 0xff); 276 return test; 277 } 278 279 283 public static int getTestVal(byte[] testArray) { 284 285 int val = 0; 286 val |= (testArray[3] & 0xff); 287 val |= ((testArray[2] & 0xff) << 8); 288 val |= ((testArray[1] & 0xff) << 16); 289 val |= ((testArray[0] & 0xff) << 24); 290 return val; 291 } 292 293 296 public static String dumpByteArray(byte[] b) { 297 298 StringBuffer sb = new StringBuffer (); 299 sb.append("<byteArray len = "); 300 sb.append(b.length); 301 sb.append(" data = \""); 302 for (int i = 0; i < b.length; i++) { 303 sb.append(b[i]).append(","); 304 } 305 sb.append("\"/>"); 306 return sb.toString(); 307 } 308 309 312 public static byte[] byteArrayCopy(byte[] ba) { 313 314 int len = ba.length; 315 byte[] ret = new byte[len]; 316 System.arraycopy(ba, 0, ret, 0, len); 317 return ret; 318 } 319 320 330 public static long validateNodeMemUsage(EnvironmentImpl envImpl, 331 boolean assertOnError) 332 throws DatabaseException { 333 334 long total = tallyNodeMemUsage(envImpl); 335 long nodeCacheUsage = envImpl.getMemoryBudget().getTreeMemoryUsage(); 336 NumberFormat formatter = NumberFormat.getNumberInstance(); 337 if (assertOnError) { 338 assert (total==nodeCacheUsage) : 339 "calculatedTotal=" + formatter.format(total) + 340 " envCacheUsage=" + formatter.format(nodeCacheUsage); 341 } else { 342 if (DEBUG) { 343 if (nodeCacheUsage != total) { 344 long diff = Math.abs(nodeCacheUsage - total); 345 if ((diff / nodeCacheUsage) > .05) { 346 System.out.println("calculatedTotal=" + 347 formatter.format(total) + 348 " envCacheUsage=" + 349 formatter.format(nodeCacheUsage)); 350 } 351 } 352 } 353 } 354 355 return nodeCacheUsage; 356 } 357 358 public static long tallyNodeMemUsage(EnvironmentImpl envImpl) 359 throws DatabaseException { 360 361 INList inList = envImpl.getInMemoryINs(); 362 inList.latchMajor(); 363 long total = 0; 364 try { 365 Iterator iter = inList.iterator(); 366 while (iter.hasNext()) { 367 IN in = (IN) iter.next(); 368 in.latch(); 369 try { 370 assert in.verifyMemorySize(): 371 "in nodeId=" + in.getNodeId() + 372 ' ' + in.getClass().getName(); 373 total += in.getInMemorySize(); 374 } finally { 375 in.releaseLatch(); 376 } 377 } 378 } finally { 379 inList.releaseMajorLatch(); 380 } 381 return total; 382 } 383 384 389 public static EnvironmentConfig initEnvConfig() { 390 391 EnvironmentConfig config = new EnvironmentConfig(); 392 String val = System.getProperty("isolationLevel"); 393 if (val != null && val.length() > 0) { 394 if ("serializable".equals(val)) { 395 config.setTxnSerializableIsolation(true); 396 } else if ("readCommitted".equals(val)) { 397 DbInternal.setTxnReadCommitted(config, true); 398 } else { 399 throw new IllegalArgumentException 400 ("Unknown isolationLevel system property value: " + val); 401 } 402 } 403 return config; 404 } 405 406 410 public static void clearIsolationLevel(EnvironmentConfig config) { 411 DbInternal.setTxnReadCommitted(config, false); 412 config.setTxnSerializableIsolation(false); 413 } 414 415 419 public static void loadLog(Class cls, String resourceName, File envHome) 420 throws IOException { 421 422 File logFile = new File (envHome, LOG_FILE_NAME); 423 InputStream is = cls.getResourceAsStream(resourceName); 424 OutputStream os = new FileOutputStream (logFile); 425 byte[] buf = new byte[is.available()]; 426 int len = is.read(buf); 427 if (buf.length != len) { 428 throw new IllegalStateException (); 429 } 430 os.write(buf, 0, len); 431 is.close(); 432 os.close(); 433 } 434 435 439 public static void logBINAndIN(Environment env, Cursor cursor) 440 throws DatabaseException { 441 442 BIN bin = getBIN(cursor); 443 Tree tree = bin.getDatabase().getTree(); 444 445 446 447 bin.latch(); 448 SearchResult result = tree.getParentINForChildIN(bin, true, true); 449 assert result.parent != null; 450 assert result.exactParentFound; 451 IN binParent = result.parent; 452 long binLsn = logIN(env, bin, true, binParent); 453 binParent.updateEntry(result.index, bin, binLsn); 454 result.parent.releaseLatch(); 455 456 457 binParent.latch(); 458 result = tree.getParentINForChildIN(binParent, true, true); 459 IN inParent = null; 460 if (result.parent != null) { 461 result.parent.releaseLatch(); 462 assert result.exactParentFound; 463 inParent = result.parent; 464 inParent.latch(); 465 } 466 final long inLsn = logIN(env, binParent, false, null); 467 if (inParent != null) { 468 inParent.updateEntry(result.index, binParent, inLsn); 469 inParent.releaseLatch(); 470 } else { 471 tree.withRootLatchedExclusive(new WithRootLatched() { 472 public IN doWork(ChildReference root) 473 throws DatabaseException { 474 root.setLsn(inLsn); 475 return null; 476 } 477 }); 478 } 479 } 480 481 484 public static long logIN(Environment env, 485 IN in, 486 boolean provisional, 487 IN parent) 488 throws DatabaseException { 489 490 EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env); 491 in.latch(); 492 long lsn; 493 if (provisional) { 494 lsn = in.log(envImpl.getLogManager(), 495 false, true, false, false, parent); } else { 501 lsn = in.log(envImpl.getLogManager()); 502 } 503 in.releaseLatch(); 504 return lsn; 505 } 506 507 510 public static IN getIN(BIN bin) 511 throws DatabaseException { 512 513 Tree tree = bin.getDatabase().getTree(); 514 bin.latch(); 515 SearchResult result = tree.getParentINForChildIN(bin, true, true); 516 assert result.parent != null; 517 result.parent.releaseLatch(); 518 assert result.exactParentFound; 519 return result.parent; 520 } 521 522 525 public static BIN getBIN(Cursor cursor) 526 throws DatabaseException { 527 528 CursorImpl impl = DbTestProxy.dbcGetCursorImpl(cursor); 529 BIN bin = impl.getDupBIN(); 530 if (bin == null) { 531 bin = impl.getBIN(); 532 assert bin != null; 533 } 534 return bin; 535 } 536 537 541 public static boolean checkTreeDepth(Database db, int desiredDepth) 542 throws DatabaseException { 543 544 Tree tree = DbInternal.dbGetDatabaseImpl(db).getTree(); 545 IN rootIN = tree.getRootIN(false ); 546 int level = 0; 547 if (rootIN != null) { 548 level = rootIN.getLevel() & IN.LEVEL_MASK; 549 rootIN.releaseLatch(); 550 } 551 552 return (desiredDepth == level); 553 } 554 555 558 static public boolean runLongTests() { 559 String longTestProp = System.getProperty(TestUtils.LONG_TEST); 560 if ((longTestProp != null) && 561 longTestProp.equalsIgnoreCase("true")) { 562 return true; 563 } else { 564 return false; 565 } 566 } 567 568 572 public static String skipVersion(Exception e) { 573 int versionHeaderLen = DatabaseException.getVersionHeader().length(); 574 return (e.getMessage().substring(versionHeaderLen)); 575 } 576 } 577 | Popular Tags |