Search in sources :

Example 1 with StackMaidDao

use of com.cloud.cluster.dao.StackMaidDao in project CloudStack-archive by CloudStack-extras.

the class TestAsync method testMaidClear.

public void testMaidClear() {
    Transaction txn = Transaction.open(Transaction.CLOUD_DB);
    StackMaidDao dao = new StackMaidDaoImpl();
    dao.pushCleanupDelegate(1L, 0, "delegate1", "Hello, world");
    dao.pushCleanupDelegate(1L, 1, "delegate2", new Long(100));
    dao.pushCleanupDelegate(1L, 2, "delegate3", null);
    dao.clearStack(1L);
    Assert.assertTrue(dao.popCleanupDelegate(1L) == null);
    txn.close();
}
Also used : Transaction(com.cloud.utils.db.Transaction) StackMaidDao(com.cloud.cluster.dao.StackMaidDao) StackMaidDaoImpl(com.cloud.cluster.dao.StackMaidDaoImpl)

Example 2 with StackMaidDao

use of com.cloud.cluster.dao.StackMaidDao in project CloudStack-archive by CloudStack-extras.

the class TestAsync method testMaidLeftovers.

public void testMaidLeftovers() {
    Thread[] threads = new Thread[3];
    for (int i = 0; i < 3; i++) {
        final int threadNum = i + 1;
        threads[i] = new Thread(new Runnable() {

            public void run() {
                Transaction txn = Transaction.open(Transaction.CLOUD_DB);
                StackMaidDao dao = new StackMaidDaoImpl();
                dao.pushCleanupDelegate(1L, 0, "delegate-" + threadNum, "Hello, world");
                dao.pushCleanupDelegate(1L, 1, "delegate-" + threadNum, new Long(100));
                dao.pushCleanupDelegate(1L, 2, "delegate-" + threadNum, null);
                txn.close();
            }
        });
        threads[i].start();
    }
    for (int i = 0; i < 3; i++) {
        try {
            threads[i].join();
        } catch (InterruptedException e) {
        }
    }
    Transaction txn = Transaction.open(Transaction.CLOUD_DB);
    StackMaidDao dao = new StackMaidDaoImpl();
    List<CheckPointVO> l = dao.listLeftoversByMsid(1L);
    for (CheckPointVO maid : l) {
        s_logger.info("" + maid.getThreadId() + " " + maid.getDelegate() + " " + maid.getContext());
    }
    txn.close();
}
Also used : Transaction(com.cloud.utils.db.Transaction) StackMaidDao(com.cloud.cluster.dao.StackMaidDao) StackMaidDaoImpl(com.cloud.cluster.dao.StackMaidDaoImpl) CheckPointVO(com.cloud.cluster.CheckPointVO)

Example 3 with StackMaidDao

use of com.cloud.cluster.dao.StackMaidDao in project CloudStack-archive by CloudStack-extras.

the class CheckPointManagerTest method testCompleteCase.

public void testCompleteCase() throws Exception {
    ComponentLocator locator = ComponentLocator.getCurrentLocator();
    CheckPointManagerImpl taskMgr = ComponentLocator.inject(CheckPointManagerImpl.class);
    assertTrue(taskMgr.configure("TaskManager", new HashMap<String, Object>()));
    assertTrue(taskMgr.start());
    MockMaid delegate = new MockMaid();
    delegate.setValue("first");
    long taskId = taskMgr.pushCheckPoint(delegate);
    StackMaidDao maidDao = locator.getDao(StackMaidDao.class);
    CheckPointVO task = maidDao.findById(taskId);
    assertEquals(task.getDelegate(), MockMaid.class.getName());
    MockMaid retrieved = (MockMaid) SerializerHelper.fromSerializedString(task.getContext());
    assertEquals(retrieved.getValue(), delegate.getValue());
    delegate.setValue("second");
    taskMgr.updateCheckPointState(taskId, delegate);
    task = maidDao.findById(taskId);
    assertEquals(task.getDelegate(), MockMaid.class.getName());
    retrieved = (MockMaid) SerializerHelper.fromSerializedString(task.getContext());
    assertEquals(retrieved.getValue(), delegate.getValue());
    taskMgr.popCheckPoint(taskId);
    assertNull(maidDao.findById(taskId));
}
Also used : ComponentLocator(com.cloud.utils.component.ComponentLocator) MockComponentLocator(com.cloud.utils.component.MockComponentLocator) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StackMaidDao(com.cloud.cluster.dao.StackMaidDao)

Example 4 with StackMaidDao

use of com.cloud.cluster.dao.StackMaidDao in project CloudStack-archive by CloudStack-extras.

the class TestAsync method testMaid.

/*
	public static class SampleAsyncResult {
		@Param(name="name", propName="name")
		private final String _name;
		
		@Param
		private final int count;
		
		public SampleAsyncResult(String name, int count) {
			_name = name;
			this.count = count;
		}
		
		public String getName() { return _name; }
		public int getCount() { return count; }
	}

	public void testDao() {
		AsyncJobDao dao = new AsyncJobDaoImpl();
		AsyncJobVO job = new AsyncJobVO(1, 1, "TestCmd", null);
		job.setInstanceType("user_vm");
		job.setInstanceId(1000L);
		
		char[] buf = new char[1024];
		for(int i = 0; i < 1024; i++)
			buf[i] = 'a';
			
		job.setResult(new String(buf));
		dao.persist(job);
		
		AsyncJobVO jobVerify = dao.findById(job.getId());
		
		Assert.assertTrue(jobVerify.getCmd().equals(job.getCmd()));
		Assert.assertTrue(jobVerify.getUserId() == 1);
		Assert.assertTrue(jobVerify.getAccountId() == 1);
		
		String result = jobVerify.getResult();
		for(int i = 0; i < 1024; i++)
			Assert.assertTrue(result.charAt(i) == 'a');
		
		jobVerify = dao.findInstancePendingAsyncJob("user_vm", 1000L);
		Assert.assertTrue(jobVerify != null);
		Assert.assertTrue(jobVerify.getCmd().equals(job.getCmd()));
		Assert.assertTrue(jobVerify.getUserId() == 1);
		Assert.assertTrue(jobVerify.getAccountId() == 1);
	}
	
	public void testSerialization() {
		List<Pair<String, Object>> l;
		int value = 1;
		l = SerializerHelper.toPairList(value, "result");
		Assert.assertTrue(l.size() == 1);
		Assert.assertTrue(l.get(0).first().equals("result"));
		Assert.assertTrue(l.get(0).second().equals("1"));
		l.clear();
		
		SampleAsyncResult result = new SampleAsyncResult("vmops", 1);
		l = SerializerHelper.toPairList(result, "result");
		
		Assert.assertTrue(l.size() == 2);
		Assert.assertTrue(l.get(0).first().equals("name"));
		Assert.assertTrue(l.get(0).second().equals("vmops"));
		Assert.assertTrue(l.get(1).first().equals("count"));
		Assert.assertTrue(l.get(1).second().equals("1"));
	}
	
	public void testAsyncResult() {
		AsyncJobResult result = new AsyncJobResult(1);
		
		result.setResultObject(100);
		Assert.assertTrue(result.getResult().equals("java.lang.Integer/100"));
		
		Object obj = result.getResultObject();
		Assert.assertTrue(obj instanceof Integer);
		Assert.assertTrue(((Integer)obj).intValue() == 100);
	}

	public void testTransaction() {
		Transaction txn = Transaction.open("testTransaction");
		try {
			txn.start();
			
			AsyncJobDao dao = new AsyncJobDaoImpl();
			AsyncJobVO job = new AsyncJobVO(1, 1, "TestCmd", null);
			job.setInstanceType("user_vm");
			job.setInstanceId(1000L);
			job.setResult("");
			dao.persist(job);
			txn.rollback();
		} finally {
			txn.close();
		}
	}
	
	public void testMorevingian() {
		int threadCount = 10;
		final int testCount = 10;
		
		Thread[] threads = new Thread[threadCount];
		for(int i = 0; i < threadCount; i++) {
			final int threadNum = i + 1;
			threads[i] = new Thread(new Runnable() {
				public void run() {
					for(int i = 0; i < testCount; i++) {
						Transaction txn = Transaction.open(Transaction.CLOUD_DB);
						try {
							AsyncJobDao dao = new AsyncJobDaoImpl();
							
							s_logger.info("Thread " + threadNum + " acquiring lock");
							AsyncJobVO job = dao.acquire(1L, 30);
							if(job != null) {
								s_logger.info("Thread " + threadNum + " acquired lock");
								
								try {
									Thread.sleep(Log4jEnabledTestCase.getRandomMilliseconds(1000, 3000));
								} catch (InterruptedException e) {
								}
								
								s_logger.info("Thread " + threadNum + " acquiring lock nestly");
								AsyncJobVO job2 = dao.acquire(1L, 30);
								if(job2 != null) {
									s_logger.info("Thread " + threadNum + " acquired lock nestly");
									
									try {
										Thread.sleep(Log4jEnabledTestCase.getRandomMilliseconds(1000, 3000));
									} catch (InterruptedException e) {
									}
									
									s_logger.info("Thread " + threadNum + " releasing lock (nestly acquired)");
									dao.release(1L);
									s_logger.info("Thread " + threadNum + " released lock (nestly acquired)");
									
								} else {
									s_logger.info("Thread " + threadNum + " was unable to acquire lock nestly");
								}
								
								s_logger.info("Thread " + threadNum + " releasing lock");
								dao.release(1L);
								s_logger.info("Thread " + threadNum + " released lock");
							} else {
								s_logger.info("Thread " + threadNum + " was unable to acquire lock");
							}
						} finally {
							txn.close();
						}
						
						try {
							Thread.sleep(Log4jEnabledTestCase.getRandomMilliseconds(1000, 10000));
						} catch (InterruptedException e) {
						}
					}
				}
			});
		}
		
		for(int i = 0; i < threadCount; i++) {
			threads[i].start();
		}
		
		for(int i = 0; i < threadCount; i++) {
			try {
				threads[i].join();
			} catch (InterruptedException e) {
			}
		}
	}
	*/
public void testMaid() {
    Transaction txn = Transaction.open(Transaction.CLOUD_DB);
    StackMaidDao dao = new StackMaidDaoImpl();
    dao.pushCleanupDelegate(1L, 0, "delegate1", "Hello, world");
    dao.pushCleanupDelegate(1L, 1, "delegate2", new Long(100));
    dao.pushCleanupDelegate(1L, 2, "delegate3", null);
    CheckPointVO item = dao.popCleanupDelegate(1L);
    Assert.assertTrue(item.getDelegate().equals("delegate3"));
    Assert.assertTrue(item.getContext() == null);
    item = dao.popCleanupDelegate(1L);
    Assert.assertTrue(item.getDelegate().equals("delegate2"));
    s_logger.info(item.getContext());
    item = dao.popCleanupDelegate(1L);
    Assert.assertTrue(item.getDelegate().equals("delegate1"));
    s_logger.info(item.getContext());
    txn.close();
}
Also used : Transaction(com.cloud.utils.db.Transaction) StackMaidDao(com.cloud.cluster.dao.StackMaidDao) StackMaidDaoImpl(com.cloud.cluster.dao.StackMaidDaoImpl) CheckPointVO(com.cloud.cluster.CheckPointVO)

Example 5 with StackMaidDao

use of com.cloud.cluster.dao.StackMaidDao in project CloudStack-archive by CloudStack-extras.

the class CheckPointManagerTest method testSimulatedReboot.

public void testSimulatedReboot() throws Exception {
    ComponentLocator locator = ComponentLocator.getCurrentLocator();
    CheckPointManagerImpl taskMgr = ComponentLocator.inject(CheckPointManagerImpl.class);
    assertTrue(taskMgr.configure("TaskManager", new HashMap<String, Object>()));
    assertTrue(taskMgr.start());
    MockMaid maid = new MockMaid();
    maid.setValue("first");
    long taskId = taskMgr.pushCheckPoint(maid);
    StackMaidDao maidDao = locator.getDao(StackMaidDao.class);
    CheckPointVO task = maidDao.findById(taskId);
    assertEquals(task.getDelegate(), MockMaid.class.getName());
    MockMaid retrieved = (MockMaid) SerializerHelper.fromSerializedString(task.getContext());
    assertEquals(retrieved.getValue(), maid.getValue());
    taskMgr.stop();
    assertNotNull(MockMaid.map.get(maid.getSeq()));
    taskMgr = ComponentLocator.inject(CheckPointManagerImpl.class);
    HashMap<String, Object> params = new HashMap<String, Object>();
    params.put(Config.TaskCleanupRetryInterval.key(), "1");
    taskMgr.configure("TaskManager", params);
    taskMgr.start();
    int i = 0;
    while (MockMaid.map.get(maid.getSeq()) != null && i++ < 5) {
        Thread.sleep(1000);
    }
    assertNull(MockMaid.map.get(maid.getSeq()));
}
Also used : ComponentLocator(com.cloud.utils.component.ComponentLocator) MockComponentLocator(com.cloud.utils.component.MockComponentLocator) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StackMaidDao(com.cloud.cluster.dao.StackMaidDao)

Aggregations

StackMaidDao (com.cloud.cluster.dao.StackMaidDao)6 StackMaidDaoImpl (com.cloud.cluster.dao.StackMaidDaoImpl)3 ComponentLocator (com.cloud.utils.component.ComponentLocator)3 MockComponentLocator (com.cloud.utils.component.MockComponentLocator)3 Transaction (com.cloud.utils.db.Transaction)3 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 CheckPointVO (com.cloud.cluster.CheckPointVO)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1