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

org.apache.uima.ducc.ws.DuccBoot Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.uima.ducc.ws;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.TreeMap;

import org.apache.uima.ducc.common.IDuccEnv;
import org.apache.uima.ducc.common.config.CommonConfiguration;
import org.apache.uima.ducc.common.internationalization.Messages;
import org.apache.uima.ducc.common.utils.DuccLogger;
import org.apache.uima.ducc.common.utils.DuccLoggerComponents;
import org.apache.uima.ducc.common.utils.DuccPropertiesResolver;
import org.apache.uima.ducc.common.utils.id.DuccId;
import org.apache.uima.ducc.transport.event.common.IDuccWorkJob;
import org.apache.uima.ducc.transport.event.common.IDuccWorkReservation;
import org.apache.uima.ducc.transport.event.common.IDuccWorkService;
import org.apache.uima.ducc.transport.event.common.history.HistoryFactory;
import org.apache.uima.ducc.transport.event.common.history.IHistoryPersistenceManager;


public class DuccBoot extends Thread {
	
	private static DuccLogger logger = DuccLoggerComponents.getWsLogger(DuccBoot.class.getName());
	private static Messages messages = Messages.getInstance();
	
	private static DuccPlugins duccPlugins = DuccPlugins.getInstance();
	
	private static DuccId jobid = null;
	
	public static long maxJobs = 4096;
	public static long maxReservations = 4096;
	public static long maxServices = 4096;
	
	public static void boot(CommonConfiguration commonConfiguration) {
		DuccBoot duccBoot = new DuccBoot();
		duccBoot.initialize(commonConfiguration);
		duccBoot.start();
	}
	
	public void run() {
		String location = "run";
		try {
			logger.info(location, jobid, "booting...");
			restore();
			logger.info(location, jobid, "ready.");
		}
		catch(Throwable t) {
			logger.error(location, jobid, t);
		}
		
	}
	
	private static long getLimit() {
		long limit = 0;
		try {
			String p_limit = DuccPropertiesResolver.getInstance().getProperty(DuccPropertiesResolver.ducc_ws_max_history_entries);
			if(p_limit != null) {
				p_limit = p_limit.trim();
				if(!p_limit.equals("unlimited")) {
					limit = Long.parseLong(p_limit);
				}
			}
		}
		catch(Throwable t) {
			t.printStackTrace();
		}
		// limit = 0;
		return limit;
	}
	
	private static TreeMap sort(ArrayList list) {
		String location = "sort";
		TreeMap map = new TreeMap();
		ListIterator listIterator = list.listIterator();
		while(listIterator.hasNext()) {
			try {
				String value = listIterator.next().trim();
				int index = value.indexOf('.');
				Integer key = Integer.parseInt(value.substring(0,index));
				map.put(key, value);
			}
			catch(Throwable t) {
				logger.warn(location, jobid, t);
			}
		}
		return map;
	}
	
	private void restoreReservations(IHistoryPersistenceManager hpm, DuccData duccData) {
		String location = "restoreReservations";
		ArrayList duccWorkReservations = hpm.reservationList();
		logger.info(location, jobid, messages.fetchLabel("Number of Reservations to restore")+duccWorkReservations.size());
		TreeMap map = sort(duccWorkReservations);
		Iterator iterator = map.descendingKeySet().iterator();
		int i = 0;
		int restored = 0;
		while(iterator.hasNext() && (++i < maxReservations)) {
			try {
				Integer key = iterator.next();
				logger.debug(location, jobid, messages.fetchLabel("restore")+key);
				String fileName = map.get(key);
				IDuccWorkReservation duccWorkReservation;
				duccWorkReservation = hpm.reservationRestore(fileName);
				if(duccWorkReservation != null) {
					duccData.putIfNotPresent(duccWorkReservation);
					duccPlugins.restore(duccWorkReservation);
					restored++;
				}
			}
			catch(Throwable t) {
				logger.warn(location, jobid, t);
			}
		}
		logger.info(location, jobid, messages.fetch("Reservations restored: "+restored));
	}
	
	private void restoreJobs(IHistoryPersistenceManager hpm, DuccData duccData) {
		String location = "restoreJobs";
		ArrayList duccWorkJobs = hpm.jobList();
		logger.info(location, jobid, messages.fetchLabel("Number of Jobs to restore")+duccWorkJobs.size());
		TreeMap map = sort(duccWorkJobs);
		Iterator iterator = map.descendingKeySet().iterator();
		int i = 0;
		int restored = 0;
		while(iterator.hasNext() && (++i < maxJobs)) {
			try {
				Integer key = iterator.next();
				logger.debug(location, jobid, messages.fetchLabel("restore")+key);
				String fileName = map.get(key);
				IDuccWorkJob duccWorkJob;
				duccWorkJob = hpm.jobRestore(fileName);
				if(duccWorkJob != null) {
					duccData.putIfNotPresent(duccWorkJob);
					duccPlugins.restore(duccWorkJob);
					restored++;
				}
			}
			catch(Throwable t) {
				logger.warn(location, jobid, t);
			}
		}
		logger.info(location, jobid, messages.fetch("Jobs restored: "+restored));
	}
	
	private void restoreServices(IHistoryPersistenceManager hpm, DuccData duccData) {
		String location = "restoreServices";
		ArrayList duccWorkServices = hpm.serviceList();
		logger.info(location, jobid, messages.fetchLabel("Number of Services to restore")+duccWorkServices.size());
		TreeMap map = sort(duccWorkServices);
		Iterator iterator = map.descendingKeySet().iterator();
		int i = 0;
		int restored = 0;
		while(iterator.hasNext() && (++i < maxServices)) {
			try {
				Integer key = iterator.next();
				logger.debug(location, jobid, messages.fetchLabel("restore")+key);
				String fileName = map.get(key);
				IDuccWorkService duccWorkService;
				duccWorkService = hpm.serviceRestore(fileName);
				if(duccWorkService != null) {
					duccData.putIfNotPresent(duccWorkService);
					duccPlugins.restore(duccWorkService);
					restored++;
				}
			}
			catch(Throwable t) {
				logger.warn(location, jobid, t);
			}
		}
		logger.info(location, jobid, messages.fetch("Services restored: "+restored));
	}
	
	private void initialize(CommonConfiguration commonConfiguration) {
		String location = "initialize";
		long limit = getLimit();
		if(limit > 0) {
			logger.info(location, jobid, messages.fetchLabel("max history limit")+limit);
			maxJobs = limit;
			maxReservations = limit;
			maxServices = limit;
		}
	}
	
	private void restore() {
		String location = "restore";
		logger.info(location, jobid, messages.fetchLabel("History directory")+IDuccEnv.DUCC_HISTORY_DIR);
		IHistoryPersistenceManager hpm = HistoryFactory.getInstance();
		DuccData duccData = DuccData.getInstance();
		restoreReservations(hpm, duccData);
		restoreJobs(hpm, duccData);
		restoreServices(hpm, duccData);
		duccData.report();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy