Search in sources :

Example 16 with AsyncTask

use of org.ovirt.engine.core.common.businessentities.AsyncTask in project ovirt-engine by oVirt.

the class AsyncTaskDaoTest method testGet.

/**
 * Ensures that, if the id is valid, then retrieving a AsyncTask works as expected.
 */
@Test
public void testGet() {
    AsyncTask result = dao.get(existingAsyncTask.getTaskId());
    assertNotNull(result);
    assertEquals(existingAsyncTask, result);
}
Also used : AsyncTask(org.ovirt.engine.core.common.businessentities.AsyncTask) Test(org.junit.Test)

Example 17 with AsyncTask

use of org.ovirt.engine.core.common.businessentities.AsyncTask in project ovirt-engine by oVirt.

the class AsyncTaskDaoTest method testSave.

/**
 * Ensures that saving a ad_group works as expected.
 */
@Test
public void testSave() {
    dao.save(newAsyncTask);
    AsyncTask result = dao.get(newAsyncTask.getTaskId());
    /*
         * //Setting startTime to null is required as DB auto generates //the value of start time //Without this, the
         * comparison would fail result.setStartTime(null);
         */
    assertEquals(newAsyncTask, result);
}
Also used : AsyncTask(org.ovirt.engine.core.common.businessentities.AsyncTask) Test(org.junit.Test)

Example 18 with AsyncTask

use of org.ovirt.engine.core.common.businessentities.AsyncTask in project ovirt-engine by oVirt.

the class AsyncTaskManager method initAsyncTaskManager.

// NOTE - any change to the logic here and in the async tasks managed command requires inspection of
// CommandExecutor.handleUnmanagedCommands()
public void initAsyncTaskManager() {
    tasksInDbAfterRestart = new ConcurrentHashMap<>();
    Map<Guid, List<AsyncTask>> rootCommandIdToTasksMap = groupTasksByRootCommandId(coco.getAllAsyncTasksFromDb());
    int numberOfCommandsPartiallyExecuted = 0;
    for (Entry<Guid, List<AsyncTask>> entry : rootCommandIdToTasksMap.entrySet()) {
        if (isPartiallyExecutedCommand(rootCommandIdToTasksMap.get(entry.getKey()))) {
            log.info("Root Command '{}' has partially executed task.", entry.getKey());
            numberOfCommandsPartiallyExecuted++;
        }
    }
    irsBrokerLatch = new CountDownLatch(numberOfCommandsPartiallyExecuted);
    for (Entry<Guid, List<AsyncTask>> entry : rootCommandIdToTasksMap.entrySet()) {
        if (isPartiallyExecutedCommand(rootCommandIdToTasksMap.get(entry.getKey()))) {
            log.info("Root Command '{}' has partially executed tasks.", entry.getKey());
            handlePartiallyExecuteTasksOfCommand(rootCommandIdToTasksMap.get(entry.getKey()));
        }
        for (AsyncTask task : entry.getValue()) {
            if (!isPartiallyExecutedTask(task)) {
                tasksInDbAfterRestart.putIfAbsent(task.getStoragePoolId(), new ArrayList<>());
                tasksInDbAfterRestart.get(task.getStoragePoolId()).add(task);
            }
        }
    }
    try {
        irsBrokerLatch.await();
        log.info("Initialization of AsyncTaskManager completed successfully.");
    } catch (InterruptedException ignore) {
    }
}
Also used : AsyncTask(org.ovirt.engine.core.common.businessentities.AsyncTask) ArrayList(java.util.ArrayList) List(java.util.List) Guid(org.ovirt.engine.core.compat.Guid) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 19 with AsyncTask

use of org.ovirt.engine.core.common.businessentities.AsyncTask in project ovirt-engine by oVirt.

the class AsyncTaskManager method addStoragePoolExistingTasks.

/**
 * Retrieves from the specified storage pool the tasks that exist on it and
 * adds them to the manager.
 *
 * @param sp the storage pool to retrieve running tasks from
 */
public void addStoragePoolExistingTasks(StoragePool sp) {
    List<AsyncTaskCreationInfo> currPoolTasks = null;
    try {
        currPoolTasks = coco.getAllTasksInfo(sp.getId());
    } catch (RuntimeException e) {
        log.error("Getting existing tasks on Storage Pool '{}' failed: {}", sp.getName(), e.getMessage());
        log.debug("Exception", e);
    }
    if (currPoolTasks != null && currPoolTasks.size() > 0) {
        synchronized (this) {
            final List<SPMTask> newlyAddedTasks = new ArrayList<>();
            for (AsyncTaskCreationInfo creationInfo : currPoolTasks) {
                creationInfo.setStoragePoolID(sp.getId());
                if (!tasks.containsKey(creationInfo.getVdsmTaskId())) {
                    try {
                        SPMTask task;
                        if (partiallyCompletedCommandTasks.containsKey(creationInfo.getVdsmTaskId())) {
                            AsyncTask asyncTaskInDb = partiallyCompletedCommandTasks.get(creationInfo.getVdsmTaskId());
                            task = coco.construct(creationInfo, asyncTaskInDb);
                            if (task.getEntitiesMap() == null) {
                                task.setEntitiesMap(new HashMap<>());
                            }
                            partiallyCompletedCommandTasks.remove(task.getVdsmTaskId());
                            // mark it as a task of a partially completed command
                            // Will result in failure of the command
                            task.setPartiallyCompletedCommandTask(true);
                        } else {
                            task = asyncTaskFactory.construct(creationInfo);
                        }
                        addTaskToManager(task);
                        newlyAddedTasks.add(task);
                    } catch (Exception e) {
                        log.error("Failed to load task of type '{}' with id '{}': {}.", creationInfo.getTaskType(), creationInfo.getVdsmTaskId(), ExceptionUtils.getRootCauseMessage(e));
                        log.debug("Exception", e);
                    }
                }
            }
            TransactionSupport.executeInNewTransaction(() -> {
                for (SPMTask task : newlyAddedTasks) {
                    AsyncTaskUtils.addOrUpdateTaskInDB(coco, task);
                }
                return null;
            });
            for (SPMTask task : newlyAddedTasks) {
                startPollingTask(task.getVdsmTaskId());
            }
            log.info("Discovered {} tasks on Storage Pool '{}', {} added to manager.", currPoolTasks.size(), sp.getName(), newlyAddedTasks.size());
        }
    } else {
        log.info("Discovered no tasks on Storage Pool '{}'", sp.getName());
    }
    List<AsyncTask> tasksInDForStoragePool = tasksInDbAfterRestart.get(sp.getId());
    if (tasksInDForStoragePool != null) {
        for (AsyncTask task : tasksInDForStoragePool) {
            if (!tasks.containsKey(task.getVdsmTaskId())) {
                coco.removeByVdsmTaskId(task.getVdsmTaskId());
            }
        }
    }
    // Either the tasks were only in DB - so they were removed from db, or they are polled -
    // in any case no need to hold them in the map that represents the tasksInDbAfterRestart
    tasksInDbAfterRestart.remove(sp.getId());
}
Also used : AsyncTaskCreationInfo(org.ovirt.engine.core.common.asynctasks.AsyncTaskCreationInfo) SPMTask(org.ovirt.engine.core.bll.tasks.interfaces.SPMTask) ArrayList(java.util.ArrayList) AsyncTask(org.ovirt.engine.core.common.businessentities.AsyncTask) EngineException(org.ovirt.engine.core.common.errors.EngineException)

Example 20 with AsyncTask

use of org.ovirt.engine.core.common.businessentities.AsyncTask in project ovirt-engine by oVirt.

the class CoCoAsyncTaskHelper method removeTaskFromDbByTaskId.

public int removeTaskFromDbByTaskId(final Guid taskId) throws RuntimeException {
    return TransactionSupport.executeInScope(TransactionScopeOption.Required, () -> {
        AsyncTask asyncTask = DbFacade.getInstance().getAsyncTaskDao().get(taskId);
        int retVal = DbFacade.getInstance().getAsyncTaskDao().remove(taskId);
        if (shouldRemoveCommand(asyncTask)) {
            coco.get().removeCommand(asyncTask.getCommandId());
            if (!coco.get().hasCommandEntitiesWithRootCommandId(asyncTask.getRootCommandId())) {
                coco.get().removeCommand(asyncTask.getRootCommandId());
            }
        }
        return retVal;
    });
}
Also used : AsyncTask(org.ovirt.engine.core.common.businessentities.AsyncTask)

Aggregations

AsyncTask (org.ovirt.engine.core.common.businessentities.AsyncTask)24 Test (org.junit.Test)8 Guid (org.ovirt.engine.core.compat.Guid)7 ActionParametersBase (org.ovirt.engine.core.common.action.ActionParametersBase)5 ArrayList (java.util.ArrayList)4 AsyncTaskCreationInfo (org.ovirt.engine.core.common.asynctasks.AsyncTaskCreationInfo)2 EngineException (org.ovirt.engine.core.common.errors.EngineException)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Before (org.junit.Before)1 CommandMultiAsyncTasks (org.ovirt.engine.core.bll.CommandMultiAsyncTasks)1 ExecutionContext (org.ovirt.engine.core.bll.job.ExecutionContext)1 CommandCoordinator (org.ovirt.engine.core.bll.tasks.interfaces.CommandCoordinator)1 SPMTask (org.ovirt.engine.core.bll.tasks.interfaces.SPMTask)1 MultipleVmsValidator (org.ovirt.engine.core.bll.validator.MultipleVmsValidator)1 ActionReturnValue (org.ovirt.engine.core.common.action.ActionReturnValue)1 ActionType (org.ovirt.engine.core.common.action.ActionType)1