Project

General

Profile

1
package eu.dnetlib.data.hadoop.oozie;
2

    
3
import java.io.IOException;
4
import java.util.*;
5

    
6
import com.google.common.collect.Maps;
7
import com.google.common.collect.Sets;
8
import org.apache.commons.io.IOUtils;
9
import org.apache.commons.lang.StringUtils;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.apache.oozie.client.OozieClient;
13
import org.apache.oozie.client.OozieClientException;
14
import org.apache.oozie.client.WorkflowAction;
15
import org.apache.oozie.client.WorkflowJob;
16
import org.apache.oozie.client.WorkflowJob.Status;
17

    
18
import eu.dnetlib.data.hadoop.action.JobCompletion;
19
import eu.dnetlib.data.hadoop.action.JobMonitor;
20
import eu.dnetlib.data.hadoop.rmi.HadoopServiceException;
21

    
22
public class OozieJobMonitor extends JobMonitor {
23

    
24
	private static final Log log = LogFactory.getLog(JobMonitor.class); // NOPMD by marko on 11/24/08 5:02 PM
25

    
26
	private final OozieClient oozieClient;
27

    
28
	private final String jobId;
29

    
30
	public static final String ACTION_TYPE_SUBWORKFLOW = "sub-workflow";
31

    
32
	private Set<String> workflowActions = Sets.newHashSet();
33

    
34
	public OozieJobMonitor(final OozieClient oozieClient, String jobId, final JobCompletion callback) {
35
		super(callback);
36
		this.oozieClient = oozieClient;
37
		this.jobId = jobId;
38
	}
39

    
40
	public OozieJobMonitor(final OozieClient oozieClient, String jobId, final JobCompletion callback, final Set<String> workflowActions) {
41
		super(callback);
42
		this.oozieClient = oozieClient;
43
		this.jobId = jobId;
44
		this.workflowActions = workflowActions;
45
	}
46

    
47
	@Override
48
	public void run() {
49
		try {
50
			log.info("waiting for oozie job completion: " + getHadoopId());
51

    
52
			Status status = doGetStatus();
53
			while (status.equals(Status.RUNNING)) {
54
				Thread.sleep(monitorSleepTimeSecs * 1000);
55

    
56
				Status currentStatus = doGetStatus();
57
				if (!status.equals(currentStatus)) {
58
					status = currentStatus;
59
					lastActivity = new Date(System.currentTimeMillis());
60
				}
61
			}
62

    
63
			log.debug("job " + jobId + " finihsed with status: " + status);
64
			if (status == Status.SUCCEEDED) {
65
				// TODO set some content to return to the blackboard msg.
66

    
67
				final Properties report = getReport(getOozieClient(), getHadoopId(), workflowActions);
68
				if (report != null) {
69
					final Map<String, String> map = Maps.newHashMap();
70
					report.forEach((k, v) -> map.put(k.toString(), v.toString()));
71
					getCallback().done(map);
72
				} else {
73
					getCallback().done(new HashMap<>());
74
				}
75
            } else {
76
				// TODO retrieve some failure information from the oozie client.
77
				String msg = "hadoop job: " + getHadoopId() + " failed with status: " + getStatus() + ", oozie log:\n "
78
						+ getOozieClient().getJobLog(getHadoopId()) + "\n\n";
79
				getCallback().failed(msg, new HadoopServiceException(msg));
80
            }
81
		} catch (Throwable e) {
82
			getCallback().failed(getHadoopId(), e);
83
		}
84
	}
85

    
86
	/**
87
	 * Provides report entries when found for given oozie job identifier. Returns null when report not found.
88
	 */
89
	private static Properties getReport(final OozieClient oozieClient, final String oozieJobId, final Set<String> workflowActions) throws OozieClientException, IOException {
90
		WorkflowJob oozieJob = oozieClient.getJobInfo(oozieJobId);
91
		for (WorkflowAction currentAction : oozieJob.getActions()) {
92
			log.debug(String.format("looking for workflo actions to report, current: '%s'", currentAction.getName()));
93
			if (workflowActions.contains(currentAction.getName())) {
94
				if (ACTION_TYPE_SUBWORKFLOW.equals(currentAction.getType())) {
95
					Properties subworkflowProperties = getReport(oozieClient, currentAction.getExternalId(), workflowActions);
96
					if (subworkflowProperties != null) {
97
						return subworkflowProperties;
98
					}
99
				} else if (StringUtils.isNotBlank(currentAction.getData())) {
100
					Properties properties = new Properties();
101
					properties.load(IOUtils.toInputStream(currentAction.getData()));
102
					return properties;
103
				}
104
			}
105
		}
106

    
107
		return null;
108
	}
109

    
110
	@Override
111
	public String getHadoopId() {
112
		return jobId;
113
	}
114

    
115
	public OozieClient getOozieClient() {
116
		return oozieClient;
117
	}
118

    
119
	@Override
120
	public String getStatus() {
121
		try {
122
			return doGetStatus().toString();
123
		} catch (OozieClientException e) {
124
			log.error("error accessing job status", e);
125
			return "UNKNOWN";
126
		}
127
	}
128

    
129
	private Status doGetStatus() throws OozieClientException {
130
		return getOozieClient().getJobInfo(getHadoopId()).getStatus();
131
	}
132

    
133
	@Override
134
	public Date getLastActivity() {
135
		return lastActivity;
136
	}
137

    
138
	@Override
139
	public Date getStartTime() throws HadoopServiceException {
140
		try {
141
			return getOozieClient().getJobInfo(getHadoopId()).getStartTime();
142
		} catch (OozieClientException e) {
143
			throw new HadoopServiceException("unable to read job start time", e);
144
		}
145
	}
146

    
147
	@Override
148
	public String getTrackerUrl() {
149
		return getOozieClient().getOozieUrl();
150
	}
151

    
152
	@Override
153
	public void kill() {
154
		try {
155
			getOozieClient().kill(getHadoopId());
156
		} catch (OozieClientException e) {
157
			log.error("unable to kill job: " + getHadoopId(), e);
158
		}
159
	}
160

    
161
}
(2-2/2)