List of usage examples for java.util.concurrent ExecutorService invokeAll
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException;
From source file:org.apache.sysml.runtime.matrix.data.LibMatrixMult.java
public static void matrixMultTransposeSelf(MatrixBlock m1, MatrixBlock ret, boolean leftTranspose, int k) throws DMLRuntimeException { //check inputs / outputs if (m1.isEmptyBlock(false)) { ret.examSparsity(); //turn empty dense into sparse return;/*from www .j ava 2 s. c o m*/ } //check no parallelization benefit (fallback to sequential) //check too small workload in terms of flops (fallback to sequential too) if (ret.rlen == 1 || leftTranspose && 1L * m1.rlen * m1.clen * m1.clen < PAR_MINFLOP_THRESHOLD || !leftTranspose && 1L * m1.clen * m1.rlen * m1.rlen < PAR_MINFLOP_THRESHOLD) { matrixMultTransposeSelf(m1, ret, leftTranspose); return; } //Timing time = new Timing(true); //pre-processing (no need to check isThreadSafe) m1 = prepMatrixMultTransposeSelfInput(m1, leftTranspose); ret.sparse = false; ret.allocateDenseBlock(); //core multi-threaded matrix mult computation try { ExecutorService pool = Executors.newFixedThreadPool(k); ArrayList<MatrixMultTransposeTask> tasks = new ArrayList<MatrixMultTransposeTask>(); //load balance via #tasks=2k due to triangular shape int blklen = (int) (Math.ceil((double) ret.rlen / (2 * k))); for (int i = 0; i < 2 * k & i * blklen < ret.rlen; i++) tasks.add(new MatrixMultTransposeTask(m1, ret, leftTranspose, i * blklen, Math.min((i + 1) * blklen, ret.rlen))); pool.invokeAll(tasks); pool.shutdown(); } catch (Exception ex) { throw new DMLRuntimeException(ex); } //post-processing copyUpperToLowerTriangle(ret); ret.recomputeNonZeros(); ret.examSparsity(); //System.out.println("TSMM k="+k+" ("+m1.isInSparseFormat()+","+m1.getNumRows()+","+m1.getNumColumns()+","+m1.getNonZeros()+","+leftTranspose+") in "+time.stop()); }
From source file:org.apache.sysml.runtime.matrix.data.LibMatrixMult.java
/** * Performs a parallel matrix multiplication chain operation of type t(X)%*%(X%*%v) or t(X)%*%(w*(X%*%v)). * The parameter k (k>=1) determines the max parallelism k' with k'=min(k, vcores, m1.rlen). * // w w w .ja v a2 s. co m * NOTE: This multi-threaded mmchain operation has additional memory requirements of k*ncol(X)*8bytes * for partial aggregation. Current max memory: 256KB; otherwise redirectly to sequential execution. * * @param mX X matrix * @param mV v matrix * @param mW w matrix * @param ret result matrix * @param ct chain type * @param k maximum parallelism * @throws DMLRuntimeException if DMLRuntimeException occurs */ public static void matrixMultChain(MatrixBlock mX, MatrixBlock mV, MatrixBlock mW, MatrixBlock ret, ChainType ct, int k) throws DMLRuntimeException { //check inputs / outputs (after that mV and mW guaranteed to be dense) if (mX.isEmptyBlock(false) || mV.isEmptyBlock(false) || (mW != null && mW.isEmptyBlock(false))) { ret.examSparsity(); //turn empty dense into sparse return; } //check too high additional memory requirements (fallback to sequential) //check too small workload in terms of flops (fallback to sequential too) if (8L * mV.rlen * k > MEM_OVERHEAD_THRESHOLD || 4L * mX.rlen * mX.clen < PAR_MINFLOP_THRESHOLD) { matrixMultChain(mX, mV, mW, ret, ct); return; } //Timing time = new Timing(true); //pre-processing (no need to check isThreadSafe) ret.sparse = false; ret.allocateDenseBlock(); //core matrix mult chain computation //(currently: always parallelization over number of rows) try { ExecutorService pool = Executors.newFixedThreadPool(k); ArrayList<MatrixMultChainTask> tasks = new ArrayList<MatrixMultChainTask>(); int blklen = (int) (Math.ceil((double) mX.rlen / k)); blklen += (blklen % 24 != 0) ? 24 - blklen % 24 : 0; for (int i = 0; i < k & i * blklen < mX.rlen; i++) tasks.add(new MatrixMultChainTask(mX, mV, mW, ct, i * blklen, Math.min((i + 1) * blklen, mX.rlen))); //execute tasks List<Future<double[]>> taskret = pool.invokeAll(tasks); pool.shutdown(); //aggregate partial results for (Future<double[]> task : taskret) vectAdd(task.get(), ret.denseBlock, 0, 0, mX.clen); } catch (Exception ex) { throw new DMLRuntimeException(ex); } //post-processing ret.recomputeNonZeros(); ret.examSparsity(); //System.out.println("MMChain "+ct.toString()+" k="+k+" ("+mX.isInSparseFormat()+","+mX.getNumRows()+","+mX.getNumColumns()+","+mX.getNonZeros()+")x" + // "("+mV.isInSparseFormat()+","+mV.getNumRows()+","+mV.getNumColumns()+","+mV.getNonZeros()+") in "+time.stop()); }
From source file:de.uzk.hki.da.pkg.MetsConsistencyChecker.java
/** * Checks the package consistency based on actual files * present in package the on the file system. * //from ww w . ja v a2 s . c om * For every file in the packagePath, a checksum will be computed. * Also a corresponding mets:FLocat element will be extracted and * the expected checksum will be compared to the computed one. * * @return true if a corresponding FLocat element could be found for every * file and the checksums matched, otherwise false * @throws Exception the exception */ public boolean checkPackageBasedOnFiles() throws Exception { boolean result = true; Namespace metsNS = Namespace.getNamespace("mets", "http://www.loc.gov/METS/"); Namespace xlinkNS = Namespace.getNamespace("xlink", "http://www.w3.org/1999/xlink"); String metsPath = packagePath + "/export_mets.xml"; SAXBuilder builder = new SAXBuilder(false); Document doc = builder.build(new File(metsPath)); ExecutorService executor = Executors.newFixedThreadPool(8); List<FileChecksumVerifierThread> threads = new ArrayList<FileChecksumVerifierThread>(); String absPackagePath = new File(packagePath).getAbsolutePath(); for (File f : (List<File>) FileUtils.listFiles(new File(packagePath), null, true)) { // skip the METS file itself if ("export_mets.xml".equals(f.getName())) { continue; } String relPath = f.getAbsolutePath().substring(absPackagePath.length() + 1); logger.debug("Verifying file: {}", relPath); String xpathExpr = "//mets:file/mets:FLocat[@xlink:href='" + relPath + "']"; XPath xpath = XPath.newInstance(xpathExpr); xpath.addNamespace(metsNS); xpath.addNamespace(xlinkNS); Element elemFLocat = (Element) xpath.selectSingleNode(doc); // check if METS contains FLocat element for file if (elemFLocat == null) { result = false; String msg = "Could not find FLocat element in METS metadata: " + relPath; logger.error(msg); messages.add(msg); continue; } Element elemFile = elemFLocat.getParentElement(); String checksum = elemFile.getAttributeValue("CHECKSUM"); String checksumType = elemFile.getAttributeValue("CHECKSUMTYPE"); // check if required attributes are set if (checksum == null) { logger.warn( "METS File Element in {} does not contain attribute CHECKSUM. File consistency can not be verified.", metsPath); continue; } if (checksumType == null) { logger.warn( "METS File Element in {} does not contain attribute CHECKSUM TYPE. File consistency can not be verified.", metsPath); continue; } logger.debug("Checking with algorithm: {}", checksumType); // calculate and verify checksum checksumType = checksumType.replaceAll("-", ""); try { MessageDigest algorithm = MessageDigest.getInstance(checksumType); threads.add(new FileChecksumVerifierThread(checksum, f, algorithm)); } catch (NoSuchAlgorithmException e) { logger.warn( "METS File Element in {} contains unknown CHECKSUM TYPE: {}. File consistency can not be verified.", metsPath, checksumType); continue; } } List<Future<ChecksumResult>> futures = executor.invokeAll(threads); for (Future<ChecksumResult> future : futures) { ChecksumResult cResult = future.get(); if (!cResult.isSuccess()) { result = false; logger.error(cResult.getMessage()); messages.add(cResult.getMessage()); } } return result; }
From source file:org.apache.sysml.runtime.matrix.data.LibMatrixMult.java
/** * Performs a multi-threaded matrix multiplication and stores the result in the output matrix. * The parameter k (k>=1) determines the max parallelism k' with k'=min(k, vcores, m1.rlen). * //from w ww .j ava 2 s. c om * @param m1 first matrix * @param m2 second matrix * @param ret result matrix * @param k maximum parallelism * @throws DMLRuntimeException if DMLRuntimeException occurs */ public static void matrixMult(MatrixBlock m1, MatrixBlock m2, MatrixBlock ret, int k) throws DMLRuntimeException { //check inputs / outputs if (m1.isEmptyBlock(false) || m2.isEmptyBlock(false)) { ret.examSparsity(); //turn empty dense into sparse return; } //check too high additional vector-matrix memory requirements (fallback to sequential) //check too small workload in terms of flops (fallback to sequential too) if (m1.rlen == 1 && (8L * m2.clen * k > MEM_OVERHEAD_THRESHOLD || !LOW_LEVEL_OPTIMIZATION || m2.clen == 1 || m1.isUltraSparse() || m2.isUltraSparse()) || 2L * m1.rlen * m1.clen * m2.clen < PAR_MINFLOP_THRESHOLD) { matrixMult(m1, m2, ret); return; } //Timing time = new Timing(true); //pre-processing: output allocation (in contrast to single-threaded, //we need to allocate sparse as well in order to prevent synchronization) boolean tm2 = checkPrepMatrixMultRightInput(m1, m2); m2 = prepMatrixMultRightInput(m1, m2); ret.sparse = (m1.isUltraSparse() || m2.isUltraSparse()); if (!ret.sparse) ret.allocateDenseBlock(); else ret.allocateSparseRowsBlock(); if (!ret.isThreadSafe()) { matrixMult(m1, m2, ret); return; } //prepare row-upper for special cases of vector-matrix / matrix-matrix boolean pm2r = checkParMatrixMultRightInputRows(m1, m2, k); boolean pm2c = checkParMatrixMultRightInputCols(m1, m2, k, pm2r); int num = pm2r ? m2.rlen : pm2c ? m2.clen : m1.rlen; //core multi-threaded matrix mult computation //(currently: always parallelization over number of rows) try { ExecutorService pool = Executors.newFixedThreadPool(k); ArrayList<MatrixMultTask> tasks = new ArrayList<MatrixMultTask>(); int nk = (pm2r || pm2c) ? k : UtilFunctions.roundToNext(Math.min(8 * k, num / 32), k); ArrayList<Integer> blklens = getBalancedBlockSizes(num, nk); for (int i = 0, lb = 0; i < blklens.size(); lb += blklens.get(i), i++) tasks.add(new MatrixMultTask(m1, m2, ret, tm2, pm2r, pm2c, lb, lb + blklens.get(i))); //execute tasks List<Future<Object>> taskret = pool.invokeAll(tasks); pool.shutdown(); //aggregate partial results (nnz, ret for vector/matrix) ret.nonZeros = 0; //reset after execute for (Future<Object> task : taskret) { if (pm2r) vectAdd((double[]) task.get(), ret.denseBlock, 0, 0, ret.rlen * ret.clen); else ret.nonZeros += (Long) task.get(); } if (pm2r) ret.recomputeNonZeros(); } catch (Exception ex) { throw new DMLRuntimeException(ex); } //post-processing (nnz maintained in parallel) ret.examSparsity(); //System.out.println("MM k="+k+" ("+m1.isInSparseFormat()+","+m1.getNumRows()+","+m1.getNumColumns()+","+m1.getNonZeros()+")x" + // "("+m2.isInSparseFormat()+","+m2.getNumRows()+","+m2.getNumColumns()+","+m2.getNonZeros()+") in "+time.stop()); }
From source file:org.apache.geode.internal.cache.wan.WANTestBase.java
public static void doMultiThreadedPuts(String regionName, int numPuts) { final AtomicInteger ai = new AtomicInteger(-1); final ExecutorService execService = Executors.newFixedThreadPool(5, new ThreadFactory() { AtomicInteger threadNum = new AtomicInteger(); public Thread newThread(final Runnable r) { Thread result = new Thread(r, "Client Put Thread-" + threadNum.incrementAndGet()); result.setDaemon(true);//from w w w. j av a 2 s . c om return result; } }); final Region r = cache.getRegion(Region.SEPARATOR + regionName); assertNotNull(r); List<Callable<Object>> tasks = new ArrayList<Callable<Object>>(); for (long i = 0; i < 5; i++) tasks.add(new PutTask(r, ai, numPuts)); try { List<Future<Object>> l = execService.invokeAll(tasks); for (Future<Object> f : l) f.get(); } catch (InterruptedException e1) { // TODO: eats exception e1.printStackTrace(); } catch (ExecutionException e) { // TODO: eats exceptions e.printStackTrace(); } execService.shutdown(); }
From source file:com.ibm.bi.dml.runtime.matrix.data.LibMatrixMult.java
/** * NOTE: This operation has limited NaN support, which is acceptable because all our sparse-safe operations * have only limited NaN support. If this is not intended behavior, please disable the rewrite. In detail, * this operator will produce for W/(U%*%t(V)) a zero intermediate for each zero in W (even if UVij is zero * which would give 0/0=NaN) but INF/-INF for non-zero entries in V where the corresponding cell in (Y%*%X) * is zero./*from w w w . j ava 2 s. com*/ * * @param mX * @param mU * @param mV * @param ret * @param wt * @param k * @throws DMLRuntimeException */ public static void matrixMultWDivMM(MatrixBlock mW, MatrixBlock mU, MatrixBlock mV, MatrixBlock ret, WDivMMType wt, int k) throws DMLRuntimeException { //check for empty result if (mW.isEmptyBlock(false) || (wt.isLeft() && mU.isEmptyBlock(false)) || (wt.isRight() && mV.isEmptyBlock(false)) || (wt.isBasic() && mW.isEmptyBlock(false))) { ret.examSparsity(); //turn empty dense into sparse return; } //Timing time = new Timing(true); //pre-processing ret.sparse = wt.isBasic() ? mW.sparse : false; ret.allocateDenseOrSparseBlock(); try { ExecutorService pool = Executors.newFixedThreadPool(k); ArrayList<MatrixMultWDivTask> tasks = new ArrayList<MatrixMultWDivTask>(); //create tasks (for wdivmm-left, parallelization over columns; //for wdivmm-right, parallelization over rows; both ensure disjoint results) if (wt.isLeft()) { int blklen = (int) (Math.ceil((double) mW.clen / k)); for (int j = 0; j < k & j * blklen < mW.clen; j++) tasks.add(new MatrixMultWDivTask(mW, mU, mV, ret, wt, 0, mW.rlen, j * blklen, Math.min((j + 1) * blklen, mW.clen))); } else { //basic/right int blklen = (int) (Math.ceil((double) mW.rlen / k)); for (int i = 0; i < k & i * blklen < mW.rlen; i++) tasks.add(new MatrixMultWDivTask(mW, mU, mV, ret, wt, i * blklen, Math.min((i + 1) * blklen, mW.rlen), 0, mW.clen)); } //execute tasks pool.invokeAll(tasks); pool.shutdown(); //aggregate partial nnz for (MatrixMultWDivTask task : tasks) ret.nonZeros += task.getPartialNnz(); } catch (InterruptedException e) { throw new DMLRuntimeException(e); } //post-processing ret.examSparsity(); //System.out.println("MMWDiv "+wt.toString()+" k="+k+" ("+mW.isInSparseFormat()+","+mW.getNumRows()+","+mW.getNumColumns()+","+mW.getNonZeros()+")x" + // "("+mV.isInSparseFormat()+","+mV.getNumRows()+","+mV.getNumColumns()+","+mV.getNonZeros()+") in "+time.stop()); }
From source file:org.apache.sysml.runtime.matrix.data.LibMatrixMult.java
/** * NOTE: This operation has limited NaN support, which is acceptable because all our sparse-safe operations * have only limited NaN support. If this is not intended behavior, please disable the rewrite. In detail, * this operator will produce for W/(U%*%t(V)) a zero intermediate for each zero in W (even if UVij is zero * which would give 0/0=NaN) but INF/-INF for non-zero entries in V where the corresponding cell in (Y%*%X) * is zero./* w w w .j av a2s . co m*/ * * @param mW matrix W * @param mU matrix U * @param mV matrix V * @param mX matrix X * @param ret result matrix * @param wt weighted divide matrix multiplication type * @param k maximum parallelism * @throws DMLRuntimeException if DMLRuntimeException occurs */ public static void matrixMultWDivMM(MatrixBlock mW, MatrixBlock mU, MatrixBlock mV, MatrixBlock mX, MatrixBlock ret, WDivMMType wt, int k) throws DMLRuntimeException { //check for empty result if (mW.isEmptyBlock(false) || (wt.isLeft() && mU.isEmptyBlock(false)) || (wt.isRight() && mV.isEmptyBlock(false)) || (wt.isBasic() && mW.isEmptyBlock(false))) { ret.examSparsity(); //turn empty dense into sparse return; } //Timing time = new Timing(true); //pre-processing ret.sparse = wt.isBasic() ? mW.sparse : false; ret.allocateDenseOrSparseBlock(); if (!ret.isThreadSafe()) { matrixMultWDivMM(mW, mU, mV, mX, ret, wt); return; } try { ExecutorService pool = Executors.newFixedThreadPool(k); ArrayList<MatrixMultWDivTask> tasks = new ArrayList<MatrixMultWDivTask>(); //create tasks (for wdivmm-left, parallelization over columns; //for wdivmm-right, parallelization over rows; both ensure disjoint results) if (wt.isLeft()) { int blklen = (int) (Math.ceil((double) mW.clen / k)); for (int j = 0; j < k & j * blklen < mW.clen; j++) tasks.add(new MatrixMultWDivTask(mW, mU, mV, mX, ret, wt, 0, mW.rlen, j * blklen, Math.min((j + 1) * blklen, mW.clen))); } else { //basic/right int blklen = (int) (Math.ceil((double) mW.rlen / k)); for (int i = 0; i < k & i * blklen < mW.rlen; i++) tasks.add(new MatrixMultWDivTask(mW, mU, mV, mX, ret, wt, i * blklen, Math.min((i + 1) * blklen, mW.rlen), 0, mW.clen)); } //execute tasks List<Future<Long>> taskret = pool.invokeAll(tasks); pool.shutdown(); //aggregate partial nnz and check for errors ret.nonZeros = 0; //reset after execute for (Future<Long> task : taskret) ret.nonZeros += task.get(); } catch (Exception e) { throw new DMLRuntimeException(e); } //post-processing ret.examSparsity(); //System.out.println("MMWDiv "+wt.toString()+" k="+k+" ("+mW.isInSparseFormat()+","+mW.getNumRows()+","+mW.getNumColumns()+","+mW.getNonZeros()+")x" + // "("+mV.isInSparseFormat()+","+mV.getNumRows()+","+mV.getNumColumns()+","+mV.getNonZeros()+") in "+time.stop()); }
From source file:org.hyperledger.core.bitcoin.TransactionTest.java
@Test public void transactionTest17() throws HyperLedgerException, InterruptedException, ExecutionException, IOException { ExecutorService executor = Executors.newFixedThreadPool(4); List<Callable<HyperLedgerException>> callables = new ArrayList<>(); // Malformed public key in multisig final Transaction p1 = wireFormatter.fromWireDump( "010000000123ba84a7ce8ab2785a838fa9ca50ad395bee25aba38dda28336cc337d08a599e000000006a4730440220320a58bf09ce578dd2ddf5381a34d80c3b659e2748c8fd8aa1b7ecc5b8c87665022019905d76a7bbc83cbc3fb6d33e7e8aae903716206e9cf1fcb75518ce37baf69a01210312c50bdc21e06827c0bdd3ef1ff849e24b17147f9a6f240c4af45bd235eb5819ffffffff0102000000000000004751210351efb6e91a31221652105d032a2508275f374cea63939ad72f1b1e02f477da782100f2b7816db49d55d24df7bdffdbc1e203b424e8cd39f5651ab938e5e4a193569e52ae00000000"); final Transaction p2 = wireFormatter.fromWireDump( "0100000001ab9b9c1610dd8dce68fb8d1d787537421a8610364d9f6907360b33739c464432000000006b483045022100dcd533f206756c83757bd0738905799dd0c7f505c22c567641b1b35573a9b24b02204c3773f60752ea67809aa32eb0a07c0f16bcfe073c99e84c8c30a328fa14874c0121031c9bfff835236f589ba409b364a9d2c392971c053cdfbbac9ccdd9f30eabb15bffffffff01404b4c00000000004751210351efb6e91a31221652105d032a2508275f374cea63939ad72f1b1e02f477da7821004f0331742bbc917ba2056a3b8a857ea47ec088dd10475ea311302112c9d24a7152ae00000000"); final Transaction n = wireFormatter.fromWireDump( "01000000025718fb915fb8b3a802bb699ddf04dd91261ef6715f5f2820a2b1b9b7e38b4f27000000004a004830450221008c2107ed4e026ab4319a591e8d9ec37719cdea053951c660566e3a3399428af502202ecd823d5f74a77cc2159d8af2d3ea5d36a702fef9a7edaaf562aef22ac35da401ffffffff038f52231b994efb980382e4d804efeadaee13cfe01abe0d969038ccb45ec17000000000490047304402200487cd787fde9b337ab87f9fe54b9fd46d5d1692aa58e97147a4fe757f6f944202203cbcfb9c0fc4e3c453938bbea9e5ae64030cf7a97fafaf460ea2cb54ed5651b501ffffffff0100093d00000000001976a9144dc39248253538b93d3a0eb122d16882b998145888ac00000000"); // parrallel script eval. Callable<HyperLedgerException> c1 = () -> { try {/* w w w.ja v a 2s.c o m*/ if (createScriptValidator(n, 0, p1.getOutput(0), EnumSet.of(ScriptVerifyFlag.P2SH), true).validate() .isValid()) { return null; } else { return new HyperLedgerException("script is not true"); } } catch (Exception e) { return new HyperLedgerException(e); } }; Callable<HyperLedgerException> c2 = () -> { try { if (createScriptValidator(n, 1, p2.getOutput(0), EnumSet.of(ScriptVerifyFlag.P2SH), true).validate() .isValid()) { return null; } else { return new HyperLedgerException("script is not true"); } } catch (Exception e) { return new HyperLedgerException(e); } }; for (int i = 0; i < 20; ++i) { callables.add(c1); callables.add(c2); } List<Future<HyperLedgerException>> results = executor.invokeAll(callables); for (Future<HyperLedgerException> e : results) { if (e.get() != null) { e.get().printStackTrace(); throw e.get(); } } }
From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java
private List<Long> threadTest(final int threadCount, final String tableId, final String rowId, final String colPrefix, final OrderedColumns orderedColumns, final boolean useNewDB, final boolean multipleWrites, final int numOfMultiWrites) throws InterruptedException, ExecutionException { final UniqueIdGenerator domainObject = new UniqueIdGenerator(); Callable<Long> task = new Callable<Long>() { @Override/*from w ww. j av a 2 s. co m*/ public synchronized Long call() { Long origVal = domainObject.nextId(); int testVal = origVal.intValue(); String testCol = colPrefix + testVal; ContentValues cvValues = null; OdkConnectionInterface dbToUse = db; if (useNewDB) { DbHandle uniqueKey = new DbHandle(AbstractODKDatabaseUtilsTest.class.getSimpleName() + testVal + AndroidConnectFactory.INTERNAL_TYPE_SUFFIX); dbToUse = OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface() .getConnection(getAppName(), uniqueKey); } try { if (multipleWrites) { for (int i = 1; i <= numOfMultiWrites; i++) { cvValues = new ContentValues(); cvValues.put(testCol, i); ODKDatabaseImplUtils.get().updateRowWithId(dbToUse, tableId, orderedColumns, cvValues, rowId, activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale); try { Thread.sleep(0); } catch (Exception e) { e.printStackTrace(); } } } else { cvValues = new ContentValues(); cvValues.put(testCol, testVal); ODKDatabaseImplUtils.get().updateRowWithId(dbToUse, tableId, orderedColumns, cvValues, rowId, activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale); } } catch (ActionNotAuthorizedException ex) { WebLogger.getLogger(dbToUse.getAppName()).printStackTrace(ex); throw new IllegalStateException(ex); } if (dbToUse != null && useNewDB) { dbToUse.releaseReference(); } return origVal; } }; List<Callable<Long>> tasks = Collections.nCopies(threadCount, task); ExecutorService executorService = Executors.newFixedThreadPool(threadCount); List<Future<Long>> futures = executorService.invokeAll(tasks); List<Long> resultList = new ArrayList<Long>(futures.size()); // Check for exceptions for (Future<Long> future : futures) { // Throws an exception if an exception was thrown by the task. resultList.add(future.get()); } // Validate the IDs assertEquals(threadCount, futures.size()); List<Long> expectedList = new ArrayList<Long>(threadCount); for (long i = 1; i <= threadCount; i++) { expectedList.add(i); } Collections.sort(resultList); assertEquals(expectedList, resultList); return resultList; }
From source file:de.hybris.platform.test.TransactionTest.java
@Test public void testLocking() throws Exception { if (Config.isHSQLDBUsed()) { LOG.warn("HDSQLDB doesnt seem to support SELECT FOR UPDATE properly so we don't test it any more"); return;// w w w .j ava 2 s . c o m } final ProductManager productManager = ProductManager.getInstance(); final Currency curr = C2LManager.getInstance().createCurrency("TestCurr"); /** Verify that we can begin a transaction, lock an entity, then commit without an exception occurring. */ { final Transaction transaction = Transaction.current(); try { assertNotNull("Transaction object is null", transaction); assertFalse("A previous transaction is already running.", transaction.isRunning()); transaction.begin(); final Product productForTest1 = productManager.createProduct("transactionLockingTest1"); transaction.commit(); transaction.begin(); transaction.lock(productForTest1); transaction.commit(); } catch (final Exception e) { transaction.rollback(); throw e; } } { /** Verify that an IllegalStateException is thrown if we attempt to lock outside of a transaction. */ final Transaction transaction = Transaction.current(); try { assertNotNull("Transaction object is null", transaction); assertFalse("A previous transaction is already running.", transaction.isRunning()); final Product productForTest2 = productManager.createProduct("transactionLockingTest2"); transaction.lock(productForTest2); fail("Expected IllegalStateException to occur when attempting to lock an item outside of a transaction."); } // An IllegalStateException is expected for this test to pass. catch (final IllegalStateException e) { // } } /** * Verify that if we attempt to acquire a lock on the same entity multiple times from the same transaction, that * no errors occur. */ { final Transaction transaction = Transaction.current(); try { assertNotNull("Transaction object is null", transaction); assertFalse("A previous transaction is already running.", transaction.isRunning()); final Product productForTest3 = productManager.createProduct("transactionLockingTest3"); transaction.begin(); for (int i = 0; i < 10; i++) { transaction.lock(productForTest3); } transaction.commit(); } catch (final Exception e) { transaction.rollback(); throw e; } } /** * Verify that if we begin a transaction, lock an entity, then commit multiple times that a lock can be acquired * each time. */ { final Transaction transaction = Transaction.current(); try { final Product productForTest4 = productManager.createProduct("transactionLockingTest4"); for (int i = 0; i < 10; i++) { assertNotNull("Transaction object is null", transaction); assertFalse("A previous transaction is already running.", transaction.isRunning()); transaction.begin(); transaction.lock(productForTest4); transaction.commit(); } } catch (final Exception e) { transaction.rollback(); throw e; } } /** * Verify that if we begin a transaction, lock an entity, then rollback multiple times that a lock can be acquired * each time. */ { final Transaction transaction = Transaction.current(); try { final Product productForTest5 = productManager.createProduct("transactionLockingTest5"); for (int i = 0; i < 10; i++) { assertNotNull("Transaction object is null", transaction); assertFalse("A previous transaction is already running.", transaction.isRunning()); transaction.begin(); transaction.lock(productForTest5); transaction.rollback(); } } catch (final Exception e) { transaction.rollback(); throw e; } } /** * Verify that we can not lock after a transaction has been committed. */ { final Transaction transaction = Transaction.current(); try { final Product productForTest6 = productManager.createProduct("transactionLockingTest6"); assertNotNull("Transaction object is null", transaction); assertFalse("A previous transaction is already running.", transaction.isRunning()); transaction.begin(); transaction.commit(); transaction.lock(productForTest6); fail("A lock was acquired after the transaction has been committed."); } // An IllegalStateException is expected for the test to pass catch (final IllegalStateException e) { // } } /** * Verify that we can not lock after a transaction has been rolled back. */ { final Transaction transaction = Transaction.current(); try { final Product productForTest7 = productManager.createProduct("transactionLockingTest7"); assertNotNull("Transaction object is null", transaction); assertFalse("A previous transaction is already running.", transaction.isRunning()); transaction.begin(); transaction.rollback(); transaction.lock(productForTest7); fail("A lock was acquired after the transaction has been rolled back."); } // An IllegalStateException is expected for the test to pass catch (final IllegalStateException e) { // } } /** * Verify multiple threads attempting to lock the same object and the behavior that occurs. */ try { final Order lockedOrder = OrderManager.getInstance().createOrder(// "lockedOrder", // JaloSession.getCurrentSession().getUser(), // curr, // Calendar.getInstance().getTime(), // true); lockedOrder.setTotal(0.0d); final ComposedType composedType = lockedOrder.getComposedType(); final String checkQuery = "SELECT " + composedType.getAttributeDescriptorIncludingPrivate(Order.TOTAL).getDatabaseColumn() + " FROM " + composedType.getTable() + " WHERE PK = ?"; final int THREADS = 16; // Create an executor service that uses 16 threads to test // the transaction locking final ExecutorService executor = Executors.newFixedThreadPool(// THREADS, // new ThreadFactory() { final Tenant threadFactoryTenant = Registry.getCurrentTenant(); @Override public Thread newThread(final Runnable runnable) { return new Thread() { protected void prepareThread() { Registry.setCurrentTenant(threadFactoryTenant); } protected void unprepareThread() { JaloSession.deactivate(); Registry.unsetCurrentTenant(); } @Override public void run() { try { prepareThread(); runnable.run(); } finally { unprepareThread(); } } }; } }); // Create 8 callables that will concurrently // attempt to lock the same object. final AtomicInteger stackCounter = new AtomicInteger(); final List<Callable<Object>> callables = new ArrayList<Callable<Object>>(); for (int j = 0; j < THREADS; j++) { callables.add(new Callable<Object>() { @Override public Object call() throws Exception { final PK pk = lockedOrder.getPK(); if (pk == null) { throw new IllegalStateException(); } for (int k = 0; k < 100; k++) { final Transaction transaction = Transaction.current(); assertNotNull("Transaction object is null", transaction); PreparedStatement statement = null; ResultSet resultSet = null; try { transaction.begin(); transaction.setTransactionIsolationLevel(Connection.TRANSACTION_READ_COMMITTED); transaction.lock(lockedOrder); final int stack = stackCounter.incrementAndGet(); if (stack > 1) { stackCounter.decrementAndGet(); throw new IllegalStateException("Got " + stack + " threads in protected area!"); } statement = transaction.getTXBoundConnection().prepareStatement(checkQuery); statement.setLong(1, lockedOrder.getPK().getLongValue()); resultSet = statement.executeQuery(); if (!resultSet.next()) { throw new IllegalStateException("Expected result set"); } final double dbValue = resultSet.getDouble(1); final double jaloValue = lockedOrder.getTotal(); if (Math.abs(dbValue - jaloValue) >= 1d) { throw new IllegalStateException( "Jalo value differs from db value : " + jaloValue + "<>" + dbValue); } lockedOrder.setTotal(jaloValue + 1.0d); stackCounter.decrementAndGet(); transaction.commit(); } catch (final Exception e) { e.printStackTrace(); transaction.rollback(); throw e; } finally { Utilities.tryToCloseJDBC(null, statement, resultSet, true); } } return null; } }); } // Get the value of each future to determine if an exception was thrown. for (final Future<Object> future : executor.invokeAll(callables)) { future.get(); } final double expected = THREADS * 100; assertEquals(// "Total value of order after all transaction differs", // expected, // ((Order) JaloSession.getCurrentSession().getItem(lockedOrder.getPK())).getTotal(), 0.000001); } catch (final IllegalStateException e) { e.printStackTrace(); throw e; } /** * Verify changes to a value on a lock */ // TODO: /** * Tests related to caching */ // TODO: }