Project

General

Profile

1
package eu.dnetlib.msro.workflows.sarasvati.loader;
2

    
3
import java.io.File;
4
import java.util.Comparator;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.concurrent.Executors;
8
import java.util.concurrent.PriorityBlockingQueue;
9
import java.util.concurrent.ScheduledExecutorService;
10
import java.util.concurrent.TimeUnit;
11
import javax.annotation.Resource;
12

    
13
import com.googlecode.sarasvati.Graph;
14
import com.googlecode.sarasvati.GraphProcess;
15
import com.googlecode.sarasvati.mem.MemEngine;
16
import com.googlecode.sarasvati.mem.MemGraphProcess;
17
import eu.dnetlib.enabling.common.Stoppable;
18
import eu.dnetlib.enabling.common.StoppableDetails;
19
import eu.dnetlib.enabling.common.StoppableDetails.StopStatus;
20
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
21
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
22
import eu.dnetlib.miscutils.datetime.DateUtils;
23
import eu.dnetlib.msro.rmi.MSROException;
24
import eu.dnetlib.msro.workflows.sarasvati.registry.GraphProcessRegistry;
25
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
26
import org.apache.commons.lang.math.NumberUtils;
27
import org.apache.commons.logging.Log;
28
import org.apache.commons.logging.LogFactory;
29
import org.springframework.beans.factory.annotation.Required;
30

    
31
public class WorkflowExecutor implements Stoppable {
32

    
33
	private static final Log log = LogFactory.getLog(WorkflowExecutor.class);
34
	private MemEngine engine;
35
	private GraphLoader graphLoader;
36
	private GraphProcessRegistry graphProcessRegistry;
37
	private ProfileToSarasvatiConverter profileToSarasvatiConverter;
38
	private ScheduledExecutorService queueConsumers;
39
	private boolean paused = false;
40
	private int maxRunningWorkflows = WorkflowsConstants.MAX_WF_THREADS;
41
	@Resource
42
	private UniqueServiceLocator serviceLocator;
43
	private PriorityBlockingQueue<GraphProcess> pendingProcs = new PriorityBlockingQueue<GraphProcess>(20, new Comparator<GraphProcess>() {
44

    
45
		@Override
46
		public int compare(final GraphProcess p1, final GraphProcess p2) {
47
			int n1 = NumberUtils.toInt(p1.getEnv().getAttribute(WorkflowsConstants.SYSTEM_WF_PRIORITY), WorkflowsConstants.DEFAULT_WF_PRIORITY);
48
			int n2 = NumberUtils.toInt(p2.getEnv().getAttribute(WorkflowsConstants.SYSTEM_WF_PRIORITY), WorkflowsConstants.DEFAULT_WF_PRIORITY);
49
			return NumberUtils.compare(n1, n2);
50
		}
51
	});
52

    
53
	public boolean isPaused() {
54
		return paused;
55
	}
56

    
57
	public void setPaused(final boolean paused) {
58
		this.paused = paused;
59
	}
60

    
61
	public void init() {
62
		this.queueConsumers = Executors.newScheduledThreadPool(WorkflowsConstants.MAX_WF_THREADS);
63
		final int period = 60;
64
		final int step = period / WorkflowsConstants.MAX_WF_THREADS;
65

    
66
		for (int i = 0; i < WorkflowsConstants.MAX_WF_THREADS; i++) {
67
			this.queueConsumers.scheduleAtFixedRate(new Runnable() {
68

    
69
				@Override
70
				public void run() {
71
					if (isPaused()) {
72
						return;
73
					}
74
					int running = graphProcessRegistry.countRunningWfs(false);
75
					if (running >= getMaxRunningWorkflows()) {
76
						if (log.isDebugEnabled()) {
77
							log.debug("reached max running workflows: " + running);
78
						}
79
						return;
80
					}
81

    
82
					final GraphProcess process = pendingProcs.poll();
83
					if (process != null) {
84
						log.info("Starting workflow: " + process);
85
						final long now = DateUtils.now();
86
						process.getEnv().setAttribute(WorkflowsConstants.SYSTEM_START_DATE, now);
87
						process.getEnv().setAttribute(WorkflowsConstants.SYSTEM_START_HUMAN_DATE, DateUtils.calculate_ISO8601(now));
88
						engine.startProcess(process);
89
					} else {
90
						log.debug("Process queue is empty");
91
					}
92
				}
93
			}, i * step, period, TimeUnit.SECONDS);
94
		}
95
	}
96

    
97
	public String startProcess(final String profileId) throws Exception {
98
		return startProcess(profileId, null);
99
	}
100

    
101
	public String startProcess(final String profileId, final Map<String, Object> params) throws Exception {
102
		final WfProfileDescriptor desc = profileToSarasvatiConverter.getSarasvatiWorkflow(profileId);
103

    
104
		if (isPaused()) {
105
			log.debug("Wf " + profileId + " not launched, because WorkflowExecutor is preparing for shutdown");
106
			throw new MSROException("WorkflowExecutor is preparing for shutdown");
107
		}
108
		if (!desc.isReady()) {
109
			log.warn("Wf " + profileId + " not launched, because it is not ready to start");
110
			throw new MSROException("Workflow " + profileId + " is not ready to start");
111
		}
112
		if (pendingProcs.size() > WorkflowsConstants.MAX_PENDING_PROCS_SIZE) {
113
			log.warn("Wf " + profileId + " not launched, Max number of pending procs reached: " + WorkflowsConstants.MAX_PENDING_PROCS_SIZE);
114
			throw new MSROException("Max number of pending procs reached: " + WorkflowsConstants.MAX_PENDING_PROCS_SIZE);
115
		}
116

    
117
		final File tmpFile = File.createTempFile("wftfs", null);
118
		try {
119
			final Graph graph = graphLoader.loadGraph(desc.getWorkflowXml());
120
			final GraphProcess process = new MemGraphProcess(graph);
121
			final String procId = graphProcessRegistry.registerProcess(process);
122

    
123
			graphProcessRegistry.associateProcessWithResource(process, profileId);
124

    
125
			process.getEnv().setAttribute(WorkflowsConstants.SYSTEM_WF_PROCESS_ID, procId);
126
			process.getEnv().setAttribute(WorkflowsConstants.SYSTEM_WF_PROFILE_ID, profileId);
127
			process.getEnv().setAttribute(WorkflowsConstants.SYSTEM_WF_PROFILE_NAME, desc.getName());
128
			process.getEnv().setAttribute(WorkflowsConstants.SYSTEM_WF_NAME, graph.getName());
129
			process.getEnv().setAttribute(WorkflowsConstants.SYSTEM_WF_PROFILE_FAMILY, desc.getType());
130
			process.getEnv().setAttribute(WorkflowsConstants.SYSTEM_WF_PRIORITY, desc.getPriority());
131

    
132
			if (params != null) {
133
				for (Map.Entry<String, Object> e : params.entrySet()) {
134
					process.getEnv().setAttribute(e.getKey(), e.getValue());
135
				}
136
			}
137

    
138
			log.info("Process " + process + " in queue, priority=" + desc.getPriority());
139
			pendingProcs.put(process);
140

    
141
			return procId;
142
		} catch (Exception e) {
143
			log.error("Error parsing workflow xml: " + desc.getWorkflowXml(), e);
144
			throw new IllegalArgumentException("Error parsing workflow");
145
		} finally {
146
			tmpFile.delete();
147
		}
148
	}
149

    
150
	public void startMetaWorkflow(final String id, final boolean manual) throws Exception {
151
		final String query = "/*[.//RESOURCE_IDENTIFIER/@value='" + id + "']//CONFIGURATION[@status='EXECUTABLE']/WORKFLOW/@id/string()";
152

    
153
		final ISLookUpService lookup = serviceLocator.getService(ISLookUpService.class);
154

    
155
		final List<String> list = lookup.quickSearchProfile(query);
156

    
157
		if (list == null || list.isEmpty()) { throw new MSROException("Metaworkflow " + id + " not launched"); }
158

    
159
		for (String wfId : list) {
160
			final String q = "/*[.//RESOURCE_IDENTIFIER/@value='" + wfId + "']//CONFIGURATION/@start/string()";
161
			if (manual || lookup.getResourceProfileByQuery(q).equals("auto")) {
162
				startProcess(wfId);
163
			} else {
164
				log.debug("Worflow " + wfId + " can not be launched AUTOMATICALLY");
165
			}
166
		}
167
	}
168

    
169
	public GraphLoader getGraphLoader() {
170
		return graphLoader;
171
	}
172

    
173
	@Required
174
	public void setGraphLoader(final GraphLoader graphLoader) {
175
		this.graphLoader = graphLoader;
176
	}
177

    
178
	public MemEngine getEngine() {
179
		return engine;
180
	}
181

    
182
	@Required
183
	public void setEngine(final MemEngine engine) {
184
		this.engine = engine;
185
	}
186

    
187
	public GraphProcessRegistry getGraphProcessRegistry() {
188
		return graphProcessRegistry;
189
	}
190

    
191
	@Required
192
	public void setGraphProcessRegistry(final GraphProcessRegistry graphProcessRegistry) {
193
		this.graphProcessRegistry = graphProcessRegistry;
194
	}
195

    
196
	public ProfileToSarasvatiConverter getProfileToSarasvatiConverter() {
197
		return profileToSarasvatiConverter;
198
	}
199

    
200
	@Required
201
	public void setProfileToSarasvatiConverter(final ProfileToSarasvatiConverter profileToSarasvatiConverter) {
202
		this.profileToSarasvatiConverter = profileToSarasvatiConverter;
203
	}
204

    
205
	@Override
206
	public void stop() {
207
		this.paused = true;
208
	}
209

    
210
	@Override
211
	public void resume() {
212
		this.paused = false;
213
	}
214

    
215
	@Override
216
	public StoppableDetails getStopDetails() {
217
		final int count = graphProcessRegistry.countRunningWfs();
218

    
219
		StoppableDetails.StopStatus status = StopStatus.RUNNING;
220
		if (isPaused()) {
221
			if (count == 0) {
222
				status = StopStatus.STOPPED;
223
			} else {
224
				status = StopStatus.STOPPING;
225
			}
226
		}
227
		graphProcessRegistry.listIdentifiers();
228

    
229
		return new StoppableDetails("D-NET workflow manager", "Running workflows: " + count, status);
230
	}
231

    
232
	public int getMaxRunningWorkflows() {
233
		return maxRunningWorkflows;
234
	}
235

    
236
	public void setMaxRunningWorkflows(final int maxRunningWorkflows) {
237
		this.maxRunningWorkflows = maxRunningWorkflows;
238
	}
239
}
(8-8/8)