private Optional<Ce.Task> searchTaskByUuid(DbSession dbSession, Request request) { String textQuery = request.getQ(); if (textQuery == null) { return Optional.empty(); } Optional<CeQueueDto> queue = dbClient.ceQueueDao().selectByUuid(dbSession, textQuery); if (queue.isPresent()) { return Optional.of(formatter.formatQueue(dbSession, queue.get())); } Optional<CeActivityDto> activity = dbClient.ceActivityDao().selectByUuid(dbSession, textQuery); return activity.map(ceActivityDto -> formatter.formatActivity(dbSession, ceActivityDto, null, emptyList())); }
@Override public long getPendingCount() { try (DbSession dbSession = dbClient.openSession(false)) { return dbClient.ceQueueDao().countByStatus(dbSession, CeQueueDto.Status.PENDING); } }
private void insertPending(CeQueueDto dto) { underTest.insert(db.getSession(), dto); db.commit(); }
@Override public Optional<CeTask> peek(String workerUuid) { requireNonNull(workerUuid, "workerUuid can't be null"); if (computeEngineStatus.getStatus() != ComputeEngineStatus.Status.STARTED || getWorkersPauseStatus() != WorkersPauseStatus.RESUMED) { return Optional.empty(); } try (DbSession dbSession = dbClient.openSession(false)) { CeQueueDao ceQueueDao = dbClient.ceQueueDao(); int i = ceQueueDao.resetToPendingForWorker(dbSession, workerUuid); if (i > 0) { dbSession.commit(); LOG.debug("{} in progress tasks reset for worker uuid {}", i, workerUuid); } Optional<CeQueueDto> opt = ceQueueDao.peek(dbSession, workerUuid); if (opt.isPresent()) { CeQueueDto taskDto = opt.get(); Map<String, ComponentDto> componentsByUuid = loadComponentDtos(dbSession, taskDto); Map<String, String> characteristics = dbClient.ceTaskCharacteristicsDao().selectByTaskUuids(dbSession, singletonList(taskDto.getUuid())).stream() .collect(uniqueIndex(CeTaskCharacteristicDto::getKey, CeTaskCharacteristicDto::getValue)); CeTask task = convertToTask(dbSession, taskDto, characteristics, ofNullable(taskDto.getComponentUuid()).map(componentsByUuid::get).orElse(null), ofNullable(taskDto.getMainComponentUuid()).map(componentsByUuid::get).orElse(null)); queueStatus.addInProgress(); return Optional.of(task); } return Optional.empty(); } }
@Test public void test_delete() { insertPending(TASK_UUID_1, MAIN_COMPONENT_UUID_1); insertPending(TASK_UUID_2, MAIN_COMPONENT_UUID_1); int deletedCount = underTest.deleteByUuid(db.getSession(), "UNKNOWN"); assertThat(deletedCount).isEqualTo(0); assertThat(underTest.selectByUuid(db.getSession(), TASK_UUID_1)).isPresent(); deletedCount = underTest.deleteByUuid(db.getSession(), TASK_UUID_1); assertThat(deletedCount).isEqualTo(1); assertThat(underTest.selectByUuid(db.getSession(), TASK_UUID_1)).isEmpty(); deletedCount = underTest.deleteByUuid(db.getSession(), TASK_UUID_2, null); assertThat(deletedCount).isEqualTo(1); assertThat(underTest.selectByUuid(db.getSession(), TASK_UUID_2)).isEmpty(); }
@Override public void handle(Request request, Response response) throws Exception { boolean isQueueEmpty = false; try (DbSession dbSession = dbClient.openSession(false)) { isQueueEmpty = dbClient.ceQueueDao().selectAllInAscOrder(dbSession).isEmpty(); } catch (Exception e) { // ignore this FP : https://gist.github.com/simonbrandhof/3d98f854d427519ef5b858a73b59585b Loggers.get(getClass()).error("Cannot select rows from ce_queue", e); } IOUtils.write(String.valueOf(isQueueEmpty), response.stream().output(), UTF_8); } }
private CeQueueDto insertInProgress(String uuid, String workerUuid) { CeQueueDto dto = new CeQueueDto() .setUuid(uuid) .setTaskType("foo") .setStatus(CeQueueDto.Status.PENDING); db.getDbClient().ceQueueDao().insert(session, dto); makeInProgress(dto, workerUuid); db.commit(); return db.getDbClient().ceQueueDao().selectByUuid(session, uuid).get(); }
public int deleteByUuid(DbSession session, String uuid) { return deleteByUuid(session, uuid, null); }
@Test public void selectUuidsNotInQueue() { insertData("U1"); insertData("U2"); assertThat(underTest.selectUuidsNotInQueue(dbTester.getSession())).containsOnly("U1", "U2"); CeQueueDto inQueue = new CeQueueDto().setUuid("U2").setTaskType(CeTaskTypes.REPORT).setStatus(CeQueueDto.Status.IN_PROGRESS); new CeQueueDao(system).insert(dbTester.getSession(), inQueue); assertThat(underTest.selectUuidsNotInQueue(dbTester.getSession())).containsOnly("U1"); }
@Test public void fail_in_progress_task() { CeTask task = submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(12))); CeQueueDto queueDto = db.getDbClient().ceQueueDao().peek(db.getSession(), WORKER_UUID).get(); underTest.fail(db.getSession(), queueDto, "TIMEOUT", "Failed on timeout"); Optional<CeActivityDto> activity = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), task.getUuid()); assertThat(activity.isPresent()).isTrue(); assertThat(activity.get().getStatus()).isEqualTo(CeActivityDto.Status.FAILED); assertThat(activity.get().getErrorType()).isEqualTo("TIMEOUT"); assertThat(activity.get().getErrorMessage()).isEqualTo("Failed on timeout"); assertThat(activity.get().getExecutedAt()).isEqualTo(NOW); assertThat(activity.get().getWorkerUuid()).isEqualTo(WORKER_UUID); }
@Override public void resetTasksWithUnknownWorkerUUIDs(Set<String> knownWorkerUUIDs) { try (DbSession dbSession = dbClient.openSession(false)) { dbClient.ceQueueDao().resetTasksWithUnknownWorkerUUIDs(dbSession, knownWorkerUUIDs); dbSession.commit(); } }
@Test public void resetToPendingForWorker_resets_status_of_non_pending_tasks_only_for_specified_workerUuid() { CeQueueDto[] worker1 = {insertPending("u1"), insertPending("u2"), insertPending("u3"), insertPending("u4")}; CeQueueDto[] worker2 = {insertPending("o1"), insertPending("o2"), insertPending("o3"), insertPending("o4")}; long startedAt = alwaysIncreasingSystem2.now(); makeInProgress(WORKER_UUID_1, startedAt, worker1[0]); makeInProgress(WORKER_UUID_1, startedAt, worker1[3]); makeInProgress(WORKER_UUID_2, startedAt, worker2[0]); makeInProgress(WORKER_UUID_2, startedAt, worker2[3]); underTestAlwaysIncreasingSystem2.resetToPendingForWorker(db.getSession(), WORKER_UUID_1); verifyResetToPendingForWorker(worker1[0], WORKER_UUID_1, startedAt); verifyUnchangedByResetToPendingForWorker(worker1[1]); verifyUnchangedByResetToPendingForWorker(worker1[2]); verifyResetToPendingForWorker(worker1[3], WORKER_UUID_1, startedAt); verifyInProgressUnchangedByResetToPendingForWorker(worker2[0], WORKER_UUID_2, startedAt); verifyUnchangedByResetToPendingForWorker(worker2[1]); verifyUnchangedByResetToPendingForWorker(worker2[2]); verifyInProgressUnchangedByResetToPendingForWorker(worker2[3], WORKER_UUID_2, startedAt); }
int cancelAll(boolean includeInProgress) { int count = 0; try (DbSession dbSession = dbClient.openSession(false)) { for (CeQueueDto queueDto : dbClient.ceQueueDao().selectAllInAscOrder(dbSession)) { if (includeInProgress || !queueDto.getStatus().equals(IN_PROGRESS)) { cancelImpl(dbSession, queueDto); count++; } } return count; } }
@Test public void test_delete_with_expected_status() { insertPending(TASK_UUID_1, MAIN_COMPONENT_UUID_1); insertInProgress(TASK_UUID_2, MAIN_COMPONENT_UUID_1); int deletedCount = underTest.deleteByUuid(db.getSession(), "UNKNOWN", null); assertThat(deletedCount).isEqualTo(0); assertThat(underTest.selectByUuid(db.getSession(), TASK_UUID_1)).isPresent(); deletedCount = underTest.deleteByUuid(db.getSession(), TASK_UUID_1, new DeleteIf(IN_PROGRESS)); assertThat(deletedCount).isEqualTo(0); assertThat(underTest.selectByUuid(db.getSession(), TASK_UUID_1)).isPresent(); deletedCount = underTest.deleteByUuid(db.getSession(), TASK_UUID_2, new DeleteIf(PENDING)); assertThat(deletedCount).isEqualTo(0); assertThat(underTest.selectByUuid(db.getSession(), TASK_UUID_2)).isPresent(); deletedCount = underTest.deleteByUuid(db.getSession(), TASK_UUID_1, new DeleteIf(PENDING)); assertThat(deletedCount).isEqualTo(1); assertThat(underTest.selectByUuid(db.getSession(), TASK_UUID_1)).isEmpty(); deletedCount = underTest.deleteByUuid(db.getSession(), TASK_UUID_2, new DeleteIf(IN_PROGRESS)); assertThat(deletedCount).isEqualTo(1); assertThat(underTest.selectByUuid(db.getSession(), TASK_UUID_2)).isEmpty(); }
protected void remove(DbSession dbSession, CeQueueDto queueDto, CeActivityDto activityDto) { String taskUuid = queueDto.getUuid(); CeQueueDto.Status expectedQueueDtoStatus = queueDto.getStatus(); dbClient.ceActivityDao().insert(dbSession, activityDto); dbClient.ceTaskInputDao().deleteByUuids(dbSession, singleton(taskUuid)); int deletedTasks = dbClient.ceQueueDao().deleteByUuid(dbSession, taskUuid, new DeleteIf(expectedQueueDtoStatus)); if (deletedTasks == 1) { dbSession.commit(); } else { Loggers.get(CeQueueImpl.class).debug( "Remove rolled back because task in queue with uuid {} and status {} could not be deleted", taskUuid, expectedQueueDtoStatus); dbSession.rollback(); } }
@Override public Optional<CeTask> peek(String workerUuid) { requireNonNull(workerUuid, "workerUuid can't be null"); if (computeEngineStatus.getStatus() != ComputeEngineStatus.Status.STARTED) { return Optional.empty(); } try (DbSession dbSession = dbClient.openSession(false)) { CeQueueDao ceQueueDao = dbClient.ceQueueDao(); int i = ceQueueDao.resetToPendingForWorker(dbSession, workerUuid); if (i > 0) { LOG.debug("{} in progress tasks reset for worker uuid {}", i, workerUuid); } Optional<CeQueueDto> dto = ceQueueDao.peek(dbSession, workerUuid, MAX_EXECUTION_COUNT); CeTask task = null; if (dto.isPresent()) { task = loadTask(dbSession, dto.get()); queueStatus.addInProgress(); } return Optional.ofNullable(task); } }
@Test public void pauseWorkers_marks_workers_as_pausing_if_some_tasks_in_progress() { submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(12))); db.getDbClient().ceQueueDao().peek(session, WORKER_UUID); // task is in-progress assertThat(underTest.getWorkersPauseStatus()).isEqualTo(CeQueue.WorkersPauseStatus.RESUMED); underTest.pauseWorkers(); assertThat(underTest.getWorkersPauseStatus()).isEqualTo(CeQueue.WorkersPauseStatus.PAUSING); }
@Test public void resetTasksWithUnknownWorkerUUIDs_with_empty_set_resets_status_of_all_pending_tasks() { CeQueueDto[] worker1 = {insertPending("u1"), insertPending("u2"), insertPending("u3"), insertPending("u4")}; CeQueueDto[] worker2 = {insertPending("o1"), insertPending("o2"), insertPending("o3"), insertPending("o4")}; long startedAt = alwaysIncreasingSystem2.now(); makeInProgress(WORKER_UUID_1, startedAt, worker1[0]); makeInProgress(WORKER_UUID_1, startedAt, worker1[3]); makeInProgress(WORKER_UUID_2, startedAt, worker2[0]); makeInProgress(WORKER_UUID_2, startedAt, worker2[3]); underTestAlwaysIncreasingSystem2.resetTasksWithUnknownWorkerUUIDs(db.getSession(), ImmutableSet.of()); verifyResetByResetTasks(worker1[0], startedAt); verifyUnchangedByResetToPendingForWorker(worker1[1]); verifyUnchangedByResetToPendingForWorker(worker1[2]); verifyResetByResetTasks(worker1[3], startedAt); verifyResetByResetTasks(worker2[0], startedAt); verifyUnchangedByResetToPendingForWorker(worker2[1]); verifyUnchangedByResetToPendingForWorker(worker2[2]); verifyResetByResetTasks(worker2[3], startedAt); }
@Override public void remove(CeTask task, CeActivityDto.Status status, @Nullable CeTaskResult taskResult, @Nullable Throwable error) { checkArgument(error == null || status == CeActivityDto.Status.FAILED, "Error can be provided only when status is FAILED"); long executionTimeInMs = 0L; try (DbSession dbSession = dbClient.openSession(false)) { CeQueueDto queueDto = dbClient.ceQueueDao().selectByUuid(dbSession, task.getUuid()) .orElseThrow(() -> new IllegalStateException("Task does not exist anymore: " + task)); CeActivityDto activityDto = new CeActivityDto(queueDto); activityDto.setStatus(status); executionTimeInMs = updateExecutionFields(activityDto); updateTaskResult(activityDto, taskResult); updateError(activityDto, error); remove(dbSession, queueDto, activityDto); } finally { updateQueueStatus(status, executionTimeInMs); } }
private void persist(CeQueueDto queueDto) { db.getDbClient().ceQueueDao().insert(db.getSession(), queueDto); db.commit(); }