Commit f132fc44 authored by gmantele's avatar gmantele
Browse files

[TAP] Remake TAPFactory into an abstract class + let limit the number of jobs...

[TAP] Remake TAPFactory into an abstract class + let limit the number of jobs (async and sync) by allowing the TAPFactory to communicate with a connection pool
parent 3bb96c8f
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ package adql.db;
 * along with ADQLLibrary.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright 2011,2013-2014 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
 *                            Astronomishes Rechen Institute (ARI)
 *                            Astronomishes Rechen Institut (ARI)
 */

import java.util.ArrayList;
@@ -64,7 +64,7 @@ import adql.search.SimpleSearchHandler;
 * </i></p>
 * 
 * @author Gr&eacute;gory Mantelet (CDS;ARI)
 * @version 1.2 (04/2014)
 * @version 1.3 (08/2014)
 */
public class DBChecker implements QueryChecker {

@@ -86,7 +86,7 @@ public class DBChecker implements QueryChecker {
	 * 
	 * @param tables	List of all available tables.
	 */
	public DBChecker(final Collection<DBTable> tables){
	public DBChecker(final Collection<? extends DBTable> tables){
		setTables(tables);
	}

@@ -103,7 +103,7 @@ public class DBChecker implements QueryChecker {
	 * 
	 * @param tables	List of {@link DBTable}s.
	 */
	public final void setTables(final Collection<DBTable> tables){
	public final void setTables(final Collection<? extends DBTable> tables){
		if (tables == null)
			lstTables = new SearchTableList();
		else if (tables instanceof SearchTableList)
+1 −1
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ package adql.parser;
 * along with ADQLLibrary.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright 2012-2013 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
 *                       Astronomisches Rechen Institute (ARI)
 *                       Astronomisches Rechen Institut (ARI)
 */

import adql.db.DBChecker;
+1 −1
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ package cds.utils;
 * along with ADQLLibrary.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright 2012-2014 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
 *                       Astronomisches Rechen Institute (ARI)
 *                       Astronomisches Rechen Institut (ARI)
 */

import java.util.ArrayList;
+8 −9
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ import adql.translator.TranslationException;
 * 
 * 
 * @author Gr&eacute;gory Mantelet (CDS;ARI)
 * @version 2.0 (07/2014)
 * @version 2.0 (08/2014)
 */
public class ADQLExecutor {

@@ -121,7 +121,7 @@ public class ADQLExecutor {
		long start = System.currentTimeMillis();
		try{
			// Get a "database" connection:
			dbConn = service.getFactory().createDBConnection(report.jobID);
			dbConn = service.getFactory().getConnection(report.jobID);

			// 1. UPLOAD TABLES, if needed:
			if (tapParams != null && tapParams.getTableLoaders() != null && tapParams.getTableLoaders().length > 0){
@@ -157,19 +157,18 @@ public class ADQLExecutor {
			npe.printStackTrace();
			throw npe;
		}finally{
			// Drop all the uploaded tables (they are not supposed to exist after the query execution):
			try{
				dropUploadedTables();
			}catch(TAPException e){
				logger.error("JOB " + report.jobID + "\tCan not drop uploaded tables !", e);
			}
			try{
			// Free the connection (so that giving it back to a pool, if any, otherwise, just free resources):
			if (dbConn != null){
					dbConn.close();
				service.getFactory().freeConnection(dbConn);
				dbConn = null;
			}
			}catch(TAPException e){
				logger.error("JOB " + report.jobID + "\tCan not close the DB connection !", e);
			}
			// Set the total duration in the report:
			report.setTotalDuration(System.currentTimeMillis() - start);
			logger.queryFinished(report);
		}
+13 −60
Original line number Diff line number Diff line
@@ -16,66 +16,45 @@ package tap;
 * You should have received a copy of the GNU Lesser General Public License
 * along with TAPLibrary.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright 2012 - UDS/Centre de Données astronomiques de Strasbourg (CDS)
 * Copyright 2012,2014 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
 *                       Astronomisches Rechen Institut (ARI)
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tap.db.DBConnection;
import tap.metadata.TAPMetadata;
import tap.metadata.TAPSchema;
import tap.metadata.TAPTable;
import tap.parameters.TAPParameters;
import tap.upload.Uploader;
import uws.UWSException;
import uws.job.ErrorSummary;
import uws.job.JobThread;
import uws.job.Result;
import uws.job.UWSJob;
import uws.job.parameters.UWSParameters;
import uws.job.user.JobOwner;
import uws.service.AbstractUWSFactory;
import uws.service.UWSService;
import uws.service.backup.UWSBackupManager;
import adql.db.DBChecker;
import adql.db.DBTable;
import adql.parser.ADQLQueryFactory;
import adql.parser.QueryChecker;

public abstract class AbstractTAPFactory extends AbstractUWSFactory implements TAPFactory {

	protected final ServiceConnection service;
public abstract class AbstractTAPFactory extends TAPFactory {

	protected AbstractTAPFactory(ServiceConnection service) throws NullPointerException{
		if (service == null)
			throw new NullPointerException("Can not create a TAPFactory without a ServiceConnection instance !");

		this.service = service;
	}

	@Override
	public UWSService createUWS() throws TAPException, UWSException{
		return new UWSService(this.service.getFactory(), this.service.getFileManager(), this.service.getLogger());
		super(service);
	}

	@Override
	public UWSBackupManager createUWSBackupManager(final UWSService uws) throws TAPException, UWSException{
	public UWSBackupManager createUWSBackupManager(final UWSService uws) throws TAPException{
		return null;
	}

	@Override
	public UWSJob createJob(HttpServletRequest request, JobOwner owner) throws UWSException{
		if (!service.isAvailable())
			throw new UWSException(HttpServletResponse.SC_SERVICE_UNAVAILABLE, service.getAvailability());

	protected TAPJob createTAPJob(final HttpServletRequest request, final JobOwner owner) throws UWSException{
		try{
			TAPParameters tapParams = (TAPParameters)createUWSParameters(request);
			TAPParameters tapParams = createTAPParameters(request);
			return new TAPJob(owner, tapParams);
		}catch(TAPException te){
			throw new UWSException(UWSException.INTERNAL_SERVER_ERROR, te, "Can not create a TAP asynchronous job !");
@@ -83,38 +62,21 @@ public abstract class AbstractTAPFactory extends AbstractUWSFactory implements T
	}

	@Override
	public UWSJob createJob(String jobId, JobOwner owner, final UWSParameters params, long quote, long startTime, long endTime, List<Result> results, ErrorSummary error) throws UWSException{
		if (!service.isAvailable())
			throw new UWSException(HttpServletResponse.SC_SERVICE_UNAVAILABLE, service.getAvailability());
	protected TAPJob createTAPJob(final String jobId, final JobOwner owner, final TAPParameters params, final long quote, final long startTime, final long endTime, final List<Result> results, final ErrorSummary error) throws UWSException{
		try{
			return new TAPJob(jobId, owner, (TAPParameters)params, quote, startTime, endTime, results, error);
			return new TAPJob(jobId, owner, params, quote, startTime, endTime, results, error);
		}catch(TAPException te){
			throw new UWSException(UWSException.INTERNAL_SERVER_ERROR, te, "Can not create a TAP asynchronous job !");
		}
	}

	@Override
	public final JobThread createJobThread(final UWSJob job) throws UWSException{
		try{
			return new AsyncThread((TAPJob)job, createADQLExecutor());
		}catch(TAPException te){
			throw new UWSException(UWSException.INTERNAL_SERVER_ERROR, te, "Impossible to create an AsyncThread !");
		}
	}

	@Override
	public ADQLExecutor createADQLExecutor() throws TAPException{
		return new ADQLExecutor(service);
	}

	/**
	 * Extracts the parameters from the given request (multipart or not).
	 * This function is used only to set UWS parameters, not to create a TAP query (for that, see {@link TAPParameters}).
	 * 
	 * @see uws.service.AbstractUWSFactory#extractParameters(javax.servlet.http.HttpServletRequest, uws.service.UWS)
	 */
	@Override
	public UWSParameters createUWSParameters(HttpServletRequest request) throws UWSException{
	protected TAPParameters createTAPParameters(final HttpServletRequest request) throws UWSException{
		try{
			return new TAPParameters(request, service, getExpectedAdditionalParameters(), getInputParamControllers());
		}catch(TAPException te){
@@ -123,7 +85,7 @@ public abstract class AbstractTAPFactory extends AbstractUWSFactory implements T
	}

	@Override
	public UWSParameters createUWSParameters(Map<String,Object> params) throws UWSException{
	protected TAPParameters createTAPParameters(final Map<String,Object> params) throws UWSException{
		try{
			return new TAPParameters(service, params, getExpectedAdditionalParameters(), getInputParamControllers());
		}catch(TAPException te){
@@ -137,16 +99,7 @@ public abstract class AbstractTAPFactory extends AbstractUWSFactory implements T
	}

	@Override
	public QueryChecker createQueryChecker(TAPSchema uploadSchema) throws TAPException{
		TAPMetadata meta = service.getTAPMetadata();
		ArrayList<DBTable> tables = new ArrayList<DBTable>(meta.getNbTables());
		Iterator<TAPTable> it = meta.getTables();
		while(it.hasNext())
			tables.add(it.next());
		if (uploadSchema != null){
			for(TAPTable table : uploadSchema)
				tables.add(table);
		}
	protected QueryChecker createQueryChecker(final Collection<TAPTable> tables) throws TAPException{
		return new DBChecker(tables);
	}

Loading