Search in sources :

Example 1 with HostDaoImpl

use of com.cloud.host.dao.HostDaoImpl in project CloudStack-archive by CloudStack-extras.

the class TestAsyncJobManager method sequence.

public void sequence() {
    final HostDao hostDao = new HostDaoImpl();
    long seq = hostDao.getNextSequence(1);
    s_logger.info("******* seq : " + seq + " ********");
    HashMap<Long, Long> hashMap = new HashMap<Long, Long>();
    final Map<Long, Long> map = Collections.synchronizedMap(hashMap);
    s_count = 0;
    // test one million times
    final long maxCount = 1000000;
    Thread t1 = new Thread(new Runnable() {

        public void run() {
            while (s_count < maxCount) {
                s_count++;
                long seq = hostDao.getNextSequence(1);
                Assert.assertTrue(map.put(seq, seq) == null);
            }
        }
    });
    Thread t2 = new Thread(new Runnable() {

        public void run() {
            while (s_count < maxCount) {
                s_count++;
                long seq = hostDao.getNextSequence(1);
                Assert.assertTrue(map.put(seq, seq) == null);
            }
        }
    });
    t1.start();
    t2.start();
    try {
        t1.join();
        t2.join();
    } catch (InterruptedException e) {
    }
}
Also used : HashMap(java.util.HashMap) HostDaoImpl(com.cloud.host.dao.HostDaoImpl) HostDao(com.cloud.host.dao.HostDao)

Example 2 with HostDaoImpl

use of com.cloud.host.dao.HostDaoImpl in project CloudStack-archive by CloudStack-extras.

the class TestAsyncJobManager method tstLocking.

public void tstLocking() {
    int testThreads = 20;
    Thread[] threads = new Thread[testThreads];
    for (int i = 0; i < testThreads; i++) {
        final int current = i;
        threads[i] = new Thread(new Runnable() {

            public void run() {
                final HostDao hostDao = new HostDaoImpl();
                while (true) {
                    Transaction txn = Transaction.currentTxn();
                    try {
                        HostVO host = hostDao.acquireInLockTable(getRandomLockId(), 10);
                        if (host != null) {
                            s_logger.info("Thread " + (current + 1) + " acquired lock");
                            try {
                                Thread.sleep(getRandomMilliseconds(1000, 5000));
                            } catch (InterruptedException e) {
                            }
                            s_logger.info("Thread " + (current + 1) + " released lock");
                            hostDao.releaseFromLockTable(host.getId());
                            try {
                                Thread.sleep(getRandomMilliseconds(1000, 5000));
                            } catch (InterruptedException e) {
                            }
                        } else {
                            s_logger.info("Thread " + (current + 1) + " is not able to acquire lock");
                        }
                    } finally {
                        txn.close();
                    }
                }
            }
        });
        threads[i].start();
    }
    try {
        for (int i = 0; i < testThreads; i++) threads[i].join();
    } catch (InterruptedException e) {
    }
}
Also used : Transaction(com.cloud.utils.db.Transaction) HostDaoImpl(com.cloud.host.dao.HostDaoImpl) HostDao(com.cloud.host.dao.HostDao) HostVO(com.cloud.host.HostVO)

Aggregations

HostDao (com.cloud.host.dao.HostDao)2 HostDaoImpl (com.cloud.host.dao.HostDaoImpl)2 HostVO (com.cloud.host.HostVO)1 Transaction (com.cloud.utils.db.Transaction)1 HashMap (java.util.HashMap)1