Package org.apache.commons.transaction.util

Examples of org.apache.commons.transaction.util.RendezvousBarrier


        final String res3 = "res3";

        // choose low timeout so sometimes an owner times out
        final ReadWriteUpgradeLockManager manager = new ReadWriteUpgradeLockManager(sLogger, 100);

        final RendezvousBarrier restart = new RendezvousBarrier("restart", 5, TIMEOUT, sLogger);
        final RendezvousBarrier start = new RendezvousBarrier("start", 5, TIMEOUT, sLogger);

        for (int i = 0; i < CONCURRENT_TESTS; i++) {
           
            System.out.print(".");

            Thread t1 = new Thread(new Runnable() {
                public void run() {
                    try {
                        try {
                            synchronized (start) {
                                start.meet();
                                start.reset();
                            }
                            manager.readLock(owner1, res1);
                            manager.readLock(owner1, res2);
                            manager.upgradeLock(owner1, res3);
                            manager.writeLock(owner1, res3);
                        } catch (LockException ie) {
                        } finally {
                            manager.releaseAll(owner1);
                            synchronized (restart) {
                                restart.meet();
                                restart.reset();
                            }
                        }
                    } catch (InterruptedException ie) {
                    }
                }
            }, "Thread #1");
            t1.start();

            Thread t2 = new Thread(new Runnable() {
                public void run() {
                    try {
                        try {
                            synchronized (start) {
                                start.meet();
                                start.reset();
                            }
                            manager.readLock(owner2, res1);
                            manager.readLock(owner2, res2);
                            manager.upgradeLock(owner2, res3);
                            manager.writeLock(owner2, res3);
                        } catch (LockException ie) {
                        } finally {
                            manager.releaseAll(owner2);
                            synchronized (restart) {
                                restart.meet();
                                restart.reset();
                            }
                        }
                    } catch (InterruptedException ie) {
                    }
                }
            }, "Thread #2");
            t2.start();

            Thread t3 = new Thread(new Runnable() {
                public void run() {
                    try {
                        try {
                            synchronized (start) {
                                start.meet();
                                start.reset();
                            }
                            manager.readLock(owner3, res1);
                            manager.readLock(owner3, res2);
                            manager.upgradeLock(owner3, res3);
                            manager.writeLock(owner3, res3);
                        } catch (LockException ie) {
                        } finally {
                            manager.releaseAll(owner3);
                            synchronized (restart) {
                                restart.meet();
                                restart.reset();
                            }
                        }
                    } catch (InterruptedException ie) {
                    }
                }
            }, "Thread #3");
            t3.start();

            Thread t4 = new Thread(new Runnable() {
                public void run() {
                    try {
                        try {
                            synchronized (start) {
                                start.meet();
                                start.reset();
                            }
                            manager.readLock(owner4, res1);
                            manager.readLock(owner4, res2);
                            manager.upgradeLock(owner4, res3);
                            manager.writeLock(owner4, res3);
                        } catch (LockException ie) {
                        } finally {
                            manager.releaseAll(owner4);
                            synchronized (restart) {
                                restart.meet();
                                restart.reset();
                            }
                        }
                    } catch (InterruptedException ie) {
                    }
                }
            }, "Thread #4");
            t4.start();

            try {
                try {
                    synchronized (start) {
                        start.meet();
                        start.reset();
                    }
                    manager.readLock("reader", res1);
                    manager.readLock("reader", res2);
                    manager.readLock("reader", res3);
View Full Code Here


        final ReadWriteUpgradeLockManager manager = new ReadWriteUpgradeLockManager(sLogger, 100);

        int concurrentThreads = 7;
        int threads = CONCURRENT_TESTS * concurrentThreads;
       
        final RendezvousBarrier end = new RendezvousBarrier("end", threads + 1, TIMEOUT, sLogger);
       
        sLogger.logInfo("\n\nStarting "+threads+" threads\n\n");

        for (int i = 0; i < CONCURRENT_TESTS; i++) {

            final int cnt = i;
           
            System.out.print(".");

            Thread t1 = new Thread(new Runnable() {
                public void run() {
                    try {
                        manager.readLock(owner1, res1);
                        manager.readLock(owner1, res2);
                        manager.upgradeLock(owner1, res3);
                        manager.writeLock(owner1, res3);
                    } catch (LockException ie) {
                        System.out.print("-");
                    } finally {
                        manager.releaseAll(owner1);
                        end.call();
                    }
                }
            }, "Thread #1");

            Thread t2 = new Thread(new Runnable() {
                public void run() {
                    try {
                        manager.readLock(owner2, res1);
                        manager.readLock(owner2, res2);
                        manager.upgradeLock(owner2, res3);
                        manager.writeLock(owner2, res3);
                    } catch (LockException ie) {
                        System.out.print("-");
                    } finally {
                        manager.releaseAll(owner2);
                        end.call();
                    }
                }
            }, "Thread #2");

            Thread t3 = new Thread(new Runnable() {
                public void run() {
                    try {
                        manager.startGlobalTimeout(owner3, 10 + cnt);
                        manager.readLock(owner3, res1);
                        manager.readLock(owner3, res2);
                        manager.upgradeLock(owner3, res3);
                        manager.writeLock(owner3, res3);
                    } catch (LockException le) {
                        if (le.getCode() == LockException.CODE_TIMED_OUT) {
                            System.out.print("*");
                        } else {
                            System.out.print("-");
                        }
                    } finally {
                        manager.releaseAll(owner3);
                        end.call();
                    }
                }
            }, "Thread #3");

            Thread t4 = new Thread(new Runnable() {
                public void run() {
                    try {
                        manager.readLock(owner4, res1);
                        manager.readLock(owner4, res2);
                        manager.upgradeLock(owner4, res3);
                        manager.writeLock(owner4, res3);
                    } catch (LockException le) {
                        System.out.print("-");
                    } finally {
                        manager.releaseAll(owner4);
                        end.call();
                    }
                }
            }, "Thread #4");

                Thread deadlock1 = new Thread(new Runnable() {
                public void run() {
                    try {
                        manager.writeLock(owner5, res2);
                        manager.writeLock(owner5, res1);
                    } catch (LockException le) {
                        assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
                        System.out.print("-");
                    } finally {
                        manager.releaseAll(owner5);
                        end.call();
                    }
                }
            }, "Deadlock1 Thread");

            Thread deadlock2 = new Thread(new Runnable() {
                public void run() {
                    try {
                        manager.readLock(owner6, res1);
                        manager.readLock(owner6, res2);
                    } catch (LockException le) {
                        assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
                        System.out.print("-");
                    } finally {
                        manager.releaseAll(owner6);
                        end.call();
                    }
                }
            }, "Deadlock1 Thread");

            Thread reader = new Thread(new Runnable() {
                public void run() {
                    try {
                        manager.readLock("reader", res1);
                        manager.readLock("reader", res2);
                        manager.readLock("reader", res3);
                    } catch (LockException ie) {
                        System.out.print("-");
                    } finally {
                        manager.releaseAll("reader");
                        end.call();
                    }
                }
            }, "Reader Thread");


            t4.start();
            t3.start();
            reader.start();
            t1.start();
            deadlock2.start();
            t2.start();
            deadlock1.start();
        }
        // wait until all threads have really terminated
        end.meet();

    }
View Full Code Here

       
        final FileResourceManager rm = createFRM();
       
        rm.start();

        final RendezvousBarrier shutdownBarrier = new RendezvousBarrier("Shutdown", 3, BARRIER_TIMEOUT, sLogger);
        final RendezvousBarrier start2Barrier = new RendezvousBarrier("Start2", BARRIER_TIMEOUT, sLogger);
        final RendezvousBarrier commit1Barrier = new RendezvousBarrier("Commit1", BARRIER_TIMEOUT, sLogger);

        final Object txId1 = "Create";

        Thread create = new Thread(new Runnable() {
            public void run() {
                try {
                    rm.startTransaction(txId1);

                    shutdownBarrier.call();
                    start2Barrier.call();

                    rm.createResource(txId1, "/olli/Hubert4");
                    rm.createResource(txId1, "/olli/Hubert5");
                    String msg = "Greetings from " + txId1 + "\n";
                    OutputStream out = rm.writeResource(txId1, "/olli/Hubert6");
                    out.write(msg.getBytes(ENCODING));

                    commit1Barrier.meet();

                    checkExactlyContains(
                        changeForTx(txId1) + "/olli",
                        new String[] { "Hubert4", "Hubert5", "Hubert6" },
                        new String[] { "", "", "Greetings from " + txId1 + "\n" });

                    rm.commitTransaction(txId1);

                    checkExactlyContains(
                        STORE + "/olli",
                        new String[] { "Hubert", "Hubert4", "Hubert5", "Hubert6" },
                        new String[] { "", "", "", "Greetings from " + txId1 + "\n" });

                } catch (Throwable e) {
                    System.err.println("Error: " + e);
                    e.printStackTrace();
                }
            }
        }, "Create Thread");

        Thread modify = new Thread(new Runnable() {
            public void run() {
                Object txId = null;
                try {

                    {
                        InputStream in = rm.readResource("/olli/Hubert6");
                        BufferedReader reader = new BufferedReader(new InputStreamReader(in, ENCODING));
                        String line = reader.readLine();
                        assertEquals(line, null);
                        in.close();
                    }

                    txId = "Modify";
                    rm.startTransaction(txId);
                    rm.setIsolationLevel(txId, ResourceManager.ISOLATION_LEVEL_READ_COMMITTED);

                    {
                        InputStream in = rm.readResource(txId, "/olli/Hubert6");
                        BufferedReader reader = new BufferedReader(new InputStreamReader(in, ENCODING));
                        String line = reader.readLine();
                        assertEquals(line, null);
                        in.close();
                    }

                    shutdownBarrier.call();

                    rm.createResource(txId, "/olli/Hubert1");
                    rm.createResource(txId, "/olli/Hubert2");
                    rm.createResource(txId, "/olli/Hubert3");

                    // wait until tx commits, so there already are Hubert4 and Hubert5 and
                    // Hubert6 changes
                    commit1Barrier.meet();

                    rm.createResource(txId, "/olli/Hubert4");
                    rm.createResource(txId, "/olli/Hubert5");

                    rm.createResource(txId, "/olli/Hubert6");
View Full Code Here

       
        final FileResourceManager rm = createFRM();
       
        rm.start();

        final RendezvousBarrier restart = new RendezvousBarrier("restart",
                TIMEOUT, sLogger);

        for (int i = 0; i < 25; i++) {

            final RendezvousBarrier deadlockBarrier1 = new RendezvousBarrier("deadlock" + i,
                    TIMEOUT, sLogger);

            Thread thread1 = new Thread(new Runnable() {
                public void run() {
                    try {
                        rm.startTransaction("tx1");
                        // first both threads get a lock, this one on res2
                        rm.createResource("tx1", "key2");
                        synchronized (deadlockBarrier1) {
                            deadlockBarrier1.meet();
                            deadlockBarrier1.reset();
                        }
                        // if I am first, the other thread will be dead, i.e.
                        // exactly one
                        rm.createResource("tx1", "key1");
                        rm.commitTransaction("tx1");
                    } catch (InterruptedException ie) {
                    } catch (ResourceManagerException e) {
                        assertEquals(e.getStatus(), ResourceManagerException.ERR_DEAD_LOCK);
                        deadlockCnt++;
                        try {
                            rm.rollbackTransaction("tx1");
                        } catch (ResourceManagerException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                    } finally {
                        try {
                        synchronized (restart) {
                            restart.meet();
                            restart.reset();
                        }
                        } catch (InterruptedException ie) {}

                    }
                }
            }, "Thread1");

            thread1.start();

            rm.startTransaction("tx2");
            try {
                // first both threads get a lock, this one on res2
                rm.deleteResource("tx2", "key1");
                synchronized (deadlockBarrier1) {
                    deadlockBarrier1.meet();
                    deadlockBarrier1.reset();
                }
                //          if I am first, the other thread will be dead, i.e. exactly
                // one
                rm.deleteResource("tx2", "key2");
                rm.commitTransaction("tx2");
View Full Code Here

        final Map map1 = new HashMap();

        final OptimisticMapWrapper txMap1 = (OptimisticMapWrapper) getNewWrapper(map1);

        final RendezvousBarrier beforeCommitBarrier =
            new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger);

        final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger);

        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                txMap1.startTransaction();
                try {
                    beforeCommitBarrier.meet();
                    txMap1.put("key1", "value2");
                    txMap1.commitTransaction();
                    afterCommitBarrier.call();
                } catch (InterruptedException e) {
                    logger.log(Level.WARNING, "Thread interrupted", e);
                    afterCommitBarrier.reset();
                    beforeCommitBarrier.reset();
                }
            }
        }, "Thread1");

        txMap1.put("key1", "value1");

        txMap1.startTransaction();
        thread1.start();

        report("value1", (String) txMap1.get("key1"));
        beforeCommitBarrier.call();
        afterCommitBarrier.meet();
        // we have serializable as isolation level, that's why I will still see the old value
        report("value1", (String) txMap1.get("key1"));

        // now when I override it it should of course be my value
        txMap1.put("key1", "value3");
View Full Code Here

    final Map map1 = new HashMap();

    final OptimisticMapWrapper txMap1 = (OptimisticMapWrapper) getNewWrapper(map1);

    final RendezvousBarrier beforeCommitBarrier =
      new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger);

    final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger);

    Thread thread1 = new Thread(new Runnable() {
      public void run() {
        txMap1.startTransaction();
        try {
          beforeCommitBarrier.meet();
          txMap1.put("key1", "value2");
          txMap1.commitTransaction();
          afterCommitBarrier.call();
        } catch (InterruptedException e) {
          logger.log(Level.WARNING, "Thread interrupted", e);
          afterCommitBarrier.reset();
          beforeCommitBarrier.reset();
        }
      }
    }, "Thread1");

    txMap1.put("key1", "value1");

    txMap1.startTransaction();
    thread1.start();

    report("value1", (String) txMap1.get("key1"));
    beforeCommitBarrier.call();
    afterCommitBarrier.meet();
    // we have serializable as isolation level, that's why I will still see the old value
    report("value1", (String) txMap1.get("key1"));

    // now when I override it it should of course be my value
    txMap1.put("key1", "value3");
View Full Code Here

        final Map map1 = new HashMap();

        final PessimisticMapWrapper txMap1 = (PessimisticMapWrapper) getNewWrapper(map1);

        final RendezvousBarrier beforeCommitBarrier =
            new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger);

        final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger);

        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                txMap1.startTransaction();
                txMap1.put("key1", "value2");
View Full Code Here

        final Map map1 = new HashMap();

        final PessimisticMapWrapper txMap1 = (PessimisticMapWrapper) getNewWrapper(map1);

        final RendezvousBarrier restart = new RendezvousBarrier("restart",
                TIMEOUT, sLogger);

        for (int i = 0; i < 25; i++) {

            final RendezvousBarrier deadlockBarrier1 = new RendezvousBarrier("deadlock" + i,
                    TIMEOUT, sLogger);

            Thread thread1 = new Thread(new Runnable() {
                public void run() {
                    txMap1.startTransaction();
                    try {
                        // first both threads get a lock, this one on res2
                        txMap1.put("key2", "value2");
                        synchronized (deadlockBarrier1) {
                            deadlockBarrier1.meet();
                            deadlockBarrier1.reset();
                        }
                        // if I am first, the other thread will be dead, i.e.
                        // exactly one
                        txMap1.put("key1", "value2");
                        txMap1.commitTransaction();
                    } catch (LockException le) {
                        assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
                        deadlockCnt++;
                        txMap1.rollbackTransaction();
                    } catch (InterruptedException ie) {
                    } finally {
                        try {
                        synchronized (restart) {
                            restart.meet();
                            restart.reset();
                        }
                        } catch (InterruptedException ie) {}

                    }
                }
            }, "Thread1");

            thread1.start();

            txMap1.startTransaction();
            try {
                // first both threads get a lock, this one on res2
                txMap1.get("key1");
                synchronized (deadlockBarrier1) {
                    deadlockBarrier1.meet();
                    deadlockBarrier1.reset();
                }
                //          if I am first, the other thread will be dead, i.e. exactly
                // one
                txMap1.get("key2");
                txMap1.commitTransaction();
View Full Code Here

        final Map map1 = new HashMap();

        final TransactionalMapWrapper txMap1 = getNewWrapper(map1);

        final RendezvousBarrier beforeCommitBarrier =
            new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger);

        final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger);

        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                txMap1.startTransaction();
                try {
                    beforeCommitBarrier.meet();
                    txMap1.put("key1", "value2");
                    txMap1.commitTransaction();
                    afterCommitBarrier.call();
                } catch (InterruptedException e) {
                    logger.log(Level.WARNING, "Thread interrupted", e);
                    afterCommitBarrier.reset();
                    beforeCommitBarrier.reset();
                }
            }
        }, "Thread1");

        txMap1.put("key1", "value1");

        txMap1.startTransaction();
        thread1.start();

        report("value1", (String) txMap1.get("key1"));
        beforeCommitBarrier.call();
        afterCommitBarrier.meet();
        // we have read committed as isolation level, that's why I will see the new value of the other thread now
        report("value2", (String) txMap1.get("key1"));

        // now when I override it it should of course be my value again
        txMap1.put("key1", "value3");
View Full Code Here

        txMap1.rollbackTransaction();
        assertEquals(txMap1.getTransactionState(), TransactionalMapWrapper.STATUS_NO_TRANSACTION);

        txMap1.startTransaction();
        final TransactionalMapWrapper.TxContext ctx = txMap1.suspendTransaction();
        final RendezvousBarrier afterSuspendBarrier =
            new RendezvousBarrier("After Suspend", 2, BARRIER_TIMEOUT, sLogger);

        new Thread(new Runnable() {
            public void run() {
                txMap1.resumeTransaction(ctx);
                txMap1.put("key2", "value2");
                txMap1.suspendTransaction();
                afterSuspendBarrier.call();
            }
        }).start();

        afterSuspendBarrier.meet();
        txMap1.resumeTransaction(ctx);

        assertEquals(txMap1.size(), 1);
        txMap1.put("key3", "value3");
        assertEquals(txMap1.size(), 2);
View Full Code Here

TOP

Related Classes of org.apache.commons.transaction.util.RendezvousBarrier

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.