This repository has been archived by the owner on Apr 4, 2021. It is now read-only.
/
LateRerunHandler.java
263 lines (239 loc) · 11.6 KB
/
LateRerunHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* 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.falcon.rerun.handler;
import org.apache.falcon.FalconException;
import org.apache.falcon.aspect.GenericAlert;
import org.apache.falcon.entity.EntityNotRegisteredException;
import org.apache.falcon.entity.EntityUtil;
import org.apache.falcon.entity.store.ConfigurationStore;
import org.apache.falcon.entity.v0.Entity;
import org.apache.falcon.entity.v0.EntityType;
import org.apache.falcon.entity.v0.SchemaHelper;
import org.apache.falcon.entity.v0.feed.Feed;
import org.apache.falcon.entity.v0.process.Input;
import org.apache.falcon.entity.v0.process.LateInput;
import org.apache.falcon.entity.v0.process.LateProcess;
import org.apache.falcon.entity.v0.process.PolicyType;
import org.apache.falcon.entity.v0.process.Process;
import org.apache.falcon.expression.ExpressionHelper;
import org.apache.falcon.hadoop.HadoopClientFactory;
import org.apache.falcon.rerun.event.LaterunEvent;
import org.apache.falcon.rerun.policy.AbstractRerunPolicy;
import org.apache.falcon.rerun.policy.RerunPolicyFactory;
import org.apache.falcon.rerun.queue.DelayedQueue;
import org.apache.falcon.workflow.WorkflowExecutionContext;
import org.apache.falcon.workflow.engine.AbstractWorkflowEngine;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.util.Date;
/**
* An implementation of handler for late reruns.
*
* @param <M>
*/
public class LateRerunHandler<M extends DelayedQueue<LaterunEvent>> extends
AbstractRerunHandler<LaterunEvent, M> {
private Thread daemon;
@Override
//SUSPEND CHECKSTYLE CHECK ParameterNumberCheck
public void handleRerun(String cluster, String entityType, String entityName, String nominalTime,
String runId, String wfId, String parentId, String workflowUser, long msgReceivedTime) {
try {
Entity entity = EntityUtil.getEntity(entityType, entityName);
int intRunId = Integer.parseInt(runId);
Date msgInsertTime = EntityUtil.parseDateUTC(nominalTime);
Long wait = getEventDelay(entity, nominalTime);
if (wait == -1) {
LOG.info("Late rerun expired for entity: {} ({})", entityType, entityName);
AbstractWorkflowEngine wfEngine = this.getWfEngine(entityType, entityName, entity.getACL().getOwner());
java.util.Properties properties = wfEngine.getWorkflowProperties(cluster, wfId);
String logDir = properties.getProperty("logDir");
String srcClusterName = properties.getProperty("srcClusterName");
Path lateLogPath = this.getLateLogPath(logDir,
EntityUtil.fromUTCtoURIDate(nominalTime), srcClusterName);
LOG.info("Going to delete path: {}", lateLogPath);
final String storageEndpoint = properties.getProperty(AbstractWorkflowEngine.NAME_NODE);
Configuration conf = getConfiguration(storageEndpoint);
FileSystem fs = HadoopClientFactory.get().createProxiedFileSystem(conf);
if (fs.exists(lateLogPath)) {
boolean deleted = fs.delete(lateLogPath, true);
if (deleted) {
LOG.info("Successfully deleted late file path: {}", lateLogPath);
}
}
return;
}
LOG.debug("Scheduling the late rerun for entity instance: {} ({}): {} And WorkflowId: {}",
entityType, entityName, nominalTime, wfId);
LaterunEvent event = new LaterunEvent(cluster, wfId, parentId, msgInsertTime.getTime(),
wait, entityType, entityName, nominalTime, intRunId, workflowUser);
offerToQueue(event);
} catch (Exception e) {
if (e instanceof EntityNotRegisteredException) {
LOG.warn("Entity {} of type {} doesn't exist in config store. So late rerun "
+ "cannot be done for workflow ", entityName,
entityType, wfId);
return;
}
LOG.error("Unable to schedule late rerun for entity instance: {} ({}): {} And WorkflowId: {}",
entityType, entityName, nominalTime, wfId, e);
GenericAlert.alertLateRerunFailed(entityType, entityName,
nominalTime, wfId, workflowUser, runId, e.getMessage());
}
}
//SUSPEND CHECKSTYLE CHECK ParameterNumberCheck
private long getEventDelay(Entity entity, String nominalTime) throws FalconException {
Date instanceDate = EntityUtil.parseDateUTC(nominalTime);
LateProcess lateProcess = EntityUtil.getLateProcess(entity);
if (lateProcess == null) {
LOG.warn("Late run not applicable for entity: {} ({})", entity.getEntityType(), entity.getName());
return -1;
}
PolicyType latePolicy = lateProcess.getPolicy();
Date cutOffTime = getCutOffTime(entity, nominalTime);
Date now = new Date();
Long wait;
if (now.after(cutOffTime)) {
LOG.warn("Feed Cut Off time: {} has expired, Late Rerun can not be scheduled",
SchemaHelper.formatDateUTC(cutOffTime));
return -1;
} else {
AbstractRerunPolicy rerunPolicy = RerunPolicyFactory
.getRetryPolicy(latePolicy);
wait = rerunPolicy.getDelay(lateProcess.getDelay(), instanceDate,
cutOffTime);
}
return wait;
}
public static Date addTime(Date date, long milliSecondsToAdd) {
return new Date(date.getTime() + milliSecondsToAdd);
}
public static Date getCutOffTime(Entity entity, String nominalTime) throws FalconException {
ExpressionHelper evaluator = ExpressionHelper.get();
Date instanceStart = EntityUtil.parseDateUTC(nominalTime);
ExpressionHelper.setReferenceDate(instanceStart);
Date endTime;
Date feedCutOff = new Date(0);
if (entity.getEntityType() == EntityType.FEED) {
if (((Feed) entity).getLateArrival() == null) {
LOG.debug("Feed's {} late arrival cut-off is not configured, returning", entity.getName());
return feedCutOff;
}
String lateCutOff = ((Feed) entity).getLateArrival().getCutOff()
.toString();
endTime = EntityUtil.parseDateUTC(nominalTime);
long feedCutOffPeriod = evaluator.evaluate(lateCutOff, Long.class);
endTime = addTime(endTime, feedCutOffPeriod);
return endTime;
} else if (entity.getEntityType() == EntityType.PROCESS) {
Process process = (Process) entity;
ConfigurationStore store = ConfigurationStore.get();
for (LateInput lp : process.getLateProcess().getLateInputs()) {
Feed feed = null;
String endInstanceTime = "";
if (process.getInputs() != null) {
for (Input input : process.getInputs().getInputs()) {
if (input.getName().equals(lp.getInput())) {
endInstanceTime = input.getEnd();
feed = store.get(EntityType.FEED, input.getFeed());
break;
}
}
}
if (feed == null) {
throw new IllegalStateException("No such feed: " + lp.getInput());
}
if (feed.getLateArrival() == null) {
LOG.debug("Feed's {} late arrival cut-off is not configured, ignoring this feed", feed.getName());
continue;
}
String lateCutOff = feed.getLateArrival().getCutOff()
.toString();
endTime = evaluator.evaluate(endInstanceTime, Date.class);
long feedCutOffPeriod = evaluator.evaluate(lateCutOff,
Long.class);
endTime = addTime(endTime, feedCutOffPeriod);
if (endTime.after(feedCutOff)) {
feedCutOff = endTime;
}
}
return feedCutOff;
} else {
throw new FalconException("Invalid entity while getting cut-off time:" + entity.getName());
}
}
@Override
public void init(M aDelayQueue) throws FalconException {
super.init(aDelayQueue);
daemon = new Thread(new LateRerunConsumer(this));
daemon.setName("LaterunHandler");
daemon.setDaemon(true);
daemon.start();
LOG.info("Laterun Handler thread started");
}
@Override
public void close() throws FalconException {
daemon.interrupt();
super.close();
}
public Path getLateLogPath(String logDir, String nominalTime,
String srcClusterName) {
//SrcClusterName valid only in case of feed
return new Path(logDir + "/latedata/" + nominalTime + "/"
+ (srcClusterName == null
? "" : srcClusterName));
}
public static Configuration getConfiguration(String storageEndpoint) throws FalconException {
Configuration conf = new Configuration();
conf.set(HadoopClientFactory.FS_DEFAULT_NAME_KEY, storageEndpoint);
return conf;
}
@Override
public void onSuccess(WorkflowExecutionContext context) throws FalconException {
Entity entity = EntityUtil.getEntity(context.getEntityType(), context.getEntityName());
//late data handling not applicable for feed retention action
if (context.getOperation() != WorkflowExecutionContext.EntityOperations.DELETE
&& EntityUtil.getLateProcess(entity) != null) {
handleRerun(context.getClusterName(), context.getEntityType(),
context.getEntityName(), context.getNominalTimeAsISO8601(),
context.getWorkflowRunIdString(), context.getWorkflowId(), context.getWorkflowParentId(),
context.getWorkflowUser(), context.getExecutionCompletionTime());
} else {
LOG.info("Late date handling not applicable for entityType: " + context.getEntityType()
+ ", entityName: " + context.getEntityName()
+ " operation: " + context.getOperation());
}
}
@Override
public void onFailure(WorkflowExecutionContext context) throws FalconException {
// do nothing since late data does not apply for failed workflows
}
@Override
public void onStart(WorkflowExecutionContext context) throws FalconException {
// do nothing
}
@Override
public void onSuspend(WorkflowExecutionContext context) throws FalconException {
// do nothing
}
@Override
public void onWait(WorkflowExecutionContext context) throws FalconException {
// Do nothing.
}
}