Search in sources :

Example 1 with ReserveJobletRequest

use of io.datarouter.jobletmysql.txn.ReserveJobletRequest in project datarouter by hotpads.

the class MysqlUpdateAndScanJobletRequestSelector method getJobletRequestForProcessing.

@Override
public Optional<JobletRequest> getJobletRequestForProcessing(PhaseTimer timer, JobletType<?> type, String reservedBy) {
    ReserveJobletRequest mysqlOp = new ReserveJobletRequest(reservedBy, type, datarouter, jobletRequestDao, mysqlSqlFactory, jobletRequestSqlBuilder);
    while (sessionExecutor.runWithoutRetries(mysqlOp)) {
        // returns false if no joblet found
        JobletRequest jobletRequest = jobletRequestDao.getReservedRequest(type, reservedBy, Isolation.readUncommitted);
        if (JobletStatus.CREATED == jobletRequest.getStatus()) {
            jobletRequest.setReservedBy(reservedBy);
            jobletRequest.setReservedAt(System.currentTimeMillis());
            jobletRequest.setStatus(JobletStatus.RUNNING);
            jobletRequestDao.put(jobletRequest);
            return Optional.of(jobletRequest);
        }
        // we got a previously timed-out joblet
        jobletRequest.incrementNumTimeouts();
        if (jobletRequest.getNumTimeouts() <= JobletService.MAX_JOBLET_RETRIES) {
            jobletRequestDao.put(jobletRequest);
            JobletRequestQueueKey queueKey = new JobletRequestQueueKey(type, jobletRequest.getKey().getPriority());
            datarouterJobletCounters.incQueueHit(queueKey.getQueueName());
            return Optional.of(jobletRequest);
        }
        jobletRequest.setStatus(JobletStatus.TIMED_OUT);
        jobletRequestDao.put(jobletRequest);
    // loop around for another
    }
    // for back-off
    jobletRequestQueueManager.onJobletRequestMissForAllPriorities(type);
    return Optional.empty();
}
Also used : ReserveJobletRequest(io.datarouter.jobletmysql.txn.ReserveJobletRequest) ReserveJobletRequest(io.datarouter.jobletmysql.txn.ReserveJobletRequest) JobletRequest(io.datarouter.joblet.storage.jobletrequest.JobletRequest) JobletRequestQueueKey(io.datarouter.joblet.storage.jobletrequestqueue.JobletRequestQueueKey)

Aggregations

JobletRequest (io.datarouter.joblet.storage.jobletrequest.JobletRequest)1 JobletRequestQueueKey (io.datarouter.joblet.storage.jobletrequestqueue.JobletRequestQueueKey)1 ReserveJobletRequest (io.datarouter.jobletmysql.txn.ReserveJobletRequest)1