All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.datarouter.jobletmysql.selector.MysqlLockForUpdateJobletRequestSelector Maven / Gradle / Ivy

There is a newer version: 0.0.126
Show newest version
/*
 * Copyright © 2009 HotPads ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.datarouter.jobletmysql.selector;

import java.util.Optional;

import io.datarouter.client.mysql.execution.SessionExecutor;
import io.datarouter.client.mysql.field.codec.factory.MysqlFieldCodecFactory;
import io.datarouter.client.mysql.sql.MysqlSqlFactory;
import io.datarouter.joblet.JobletCounters;
import io.datarouter.joblet.JobletRequestSqlBuilder;
import io.datarouter.joblet.enums.JobletQueueMechanism;
import io.datarouter.joblet.queue.JobletRequestQueueManager;
import io.datarouter.joblet.queue.JobletRequestSelector;
import io.datarouter.joblet.service.JobletService;
import io.datarouter.joblet.storage.jobletrequest.DatarouterJobletRequestDao;
import io.datarouter.joblet.storage.jobletrequest.JobletRequest;
import io.datarouter.joblet.storage.jobletrequestqueue.JobletRequestQueueKey;
import io.datarouter.joblet.type.JobletType;
import io.datarouter.jobletmysql.txn.GetJobletRequest;
import io.datarouter.plugin.PluginConfigKey;
import io.datarouter.storage.client.DatarouterClients;
import io.datarouter.util.timer.PhaseTimer;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

@Singleton
public class MysqlLockForUpdateJobletRequestSelector implements JobletRequestSelector{

	@Inject
	private DatarouterClients datarouterClients;
	@Inject
	private DatarouterJobletRequestDao jobletRequestDao;
	@Inject
	private MysqlFieldCodecFactory mysqlFieldCodecFactory;
	@Inject
	private JobletRequestSqlBuilder jobletRequestSqlBuilder;
	@Inject
	private JobletRequestQueueManager jobletRequestQueueManager;
	@Inject
	private SessionExecutor sessionExecutor;
	@Inject
	private MysqlSqlFactory mysqlSqlFactory;
	@Inject
	private JobletService jobletService;

	@Override
	public Optional getJobletRequestForProcessing(
			PhaseTimer timer,
			JobletType type,
			String reservedBy){
		while(true){
			var mysqlOp = new GetJobletRequest(reservedBy, type, datarouterClients, jobletRequestDao,
					mysqlFieldCodecFactory, mysqlSqlFactory, jobletRequestSqlBuilder, jobletService);
			JobletRequest jobletRequest = sessionExecutor.runWithoutRetries(mysqlOp);
			timer.add("GetJobletRequest");
			if(jobletRequest == null){
				jobletRequestQueueManager.onJobletRequestMissForAllPriorities(type);//for back-off
				return Optional.empty();
			}
			if(!jobletRequest.getStatus().isRunning){
				continue;// weird flow. it was probably just marked as timedOut, so skip it
			}
			var queueKey = new JobletRequestQueueKey(type, jobletRequest.getKey().getPriority());
			JobletCounters.incQueueHit(queueKey.getQueueName());
			return Optional.of(jobletRequest);
		}
	}

	@Override
	public PluginConfigKey getKey(){
		return JobletQueueMechanism.JDBC_LOCK_FOR_UPDATE.getKey();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy