This repository has been archived by the owner on Jan 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 160
/
FsActionExecutor.java
359 lines (326 loc) · 13.3 KB
/
FsActionExecutor.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* Copyright (c) 2010 Yahoo! Inc. All rights reserved.
* Licensed 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. See accompanying LICENSE file.
*/
package org.apache.oozie.action.hadoop;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.oozie.action.ActionExecutor;
import org.apache.oozie.action.ActionExecutorException;
import org.apache.oozie.client.WorkflowAction;
import org.apache.oozie.service.HadoopAccessorException;
import org.apache.oozie.service.HadoopAccessorService;
import org.apache.oozie.service.Services;
import org.apache.oozie.util.XmlUtils;
import org.jdom.Element;
/**
* File system action executor. <p/> This executes the file system mkdir, move and delete commands
*/
public class FsActionExecutor extends ActionExecutor {
public FsActionExecutor() {
super("fs");
}
Path getPath(Element element, String attribute) {
String str = element.getAttributeValue(attribute).trim();
return new Path(str);
}
void validatePath(Path path, boolean withScheme) throws ActionExecutorException {
String scheme = path.toUri().getScheme();
if (withScheme) {
if (scheme == null) {
throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FS001",
"Missing scheme in path [{0}]", path);
}
else {
if (!scheme.equals("hdfs")) {
throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FS002",
"Scheme [{0}] not supported in path [{1}]", scheme, path);
}
}
}
else {
if (scheme != null) {
throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FS003",
"Scheme [{0}] not allowed in path [{1}]", scheme, path);
}
}
}
void validateSameNN(Path source, Path dest) throws ActionExecutorException {
Path destPath = new Path(source, dest);
String t = destPath.toUri().getScheme() + destPath.toUri().getAuthority();
String s = source.toUri().getScheme() + source.toUri().getAuthority();
//checking whether NN prefix of source and target is same. can modify this to adjust for a set of multiple whitelisted NN
if(!t.equals(s)) {
throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FS007",
"move, target NN URI different from that of source", dest);
}
}
@SuppressWarnings("unchecked")
void doOperations(Context context, Element element) throws ActionExecutorException {
try {
FileSystem fs = context.getAppFileSystem();
boolean recovery = fs.exists(getRecoveryPath(context));
if (!recovery) {
fs.mkdirs(getRecoveryPath(context));
}
for (Element commandElement : (List<Element>) element.getChildren()) {
String command = commandElement.getName();
if (command.equals("mkdir")) {
Path path = getPath(commandElement, "path");
mkdir(context, path);
}
else {
if (command.equals("delete")) {
Path path = getPath(commandElement, "path");
delete(context, path);
}
else {
if (command.equals("move")) {
Path source = getPath(commandElement, "source");
Path target = getPath(commandElement, "target");
move(context, source, target, recovery);
}
else {
if (command.equals("chmod")) {
Path path = getPath(commandElement, "path");
String str = commandElement.getAttributeValue("dir-files");
boolean dirFiles = (str == null) || Boolean.parseBoolean(str);
String permissionsMask = commandElement.getAttributeValue("permissions").trim();
chmod(context, path, permissionsMask, dirFiles);
}
}
}
}
}
}
catch (Exception ex) {
throw convertException(ex);
}
}
/**
* @param path
* @param context
* @return FileSystem
* @throws HadoopAccessorException
*/
private FileSystem getFileSystemFor(Path path, Context context) throws HadoopAccessorException {
String user = context.getWorkflow().getUser();
String group = context.getWorkflow().getGroup();
return Services.get().get(HadoopAccessorService.class).createFileSystem(user, group, path.toUri(),
new Configuration());
}
/**
* @param path
* @param user
* @param group
* @return FileSystem
* @throws HadoopAccessorException
*/
private FileSystem getFileSystemFor(Path path, String user, String group) throws HadoopAccessorException {
return Services.get().get(HadoopAccessorService.class).createFileSystem(user, group, path.toUri(),
new Configuration());
}
void mkdir(Context context, Path path) throws ActionExecutorException {
try {
validatePath(path, true);
FileSystem fs = getFileSystemFor(path, context);
if (!fs.exists(path)) {
if (!fs.mkdirs(path)) {
throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FS004",
"mkdir, path [{0}] could not create directory", path);
}
}
}
catch (Exception ex) {
throw convertException(ex);
}
}
/**
* Delete path
*
* @param context
* @param path
* @throws ActionExecutorException
*/
public void delete(Context context, Path path) throws ActionExecutorException {
try {
validatePath(path, true);
FileSystem fs = getFileSystemFor(path, context);
if (fs.exists(path)) {
if (!fs.delete(path, true)) {
throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FS005",
"delete, path [{0}] could not delete path", path);
}
}
}
catch (Exception ex) {
throw convertException(ex);
}
}
/**
* Delete path
*
* @param user
* @param group
* @param path
* @throws ActionExecutorException
*/
public void delete(String user, String group, Path path) throws ActionExecutorException {
try {
validatePath(path, true);
FileSystem fs = getFileSystemFor(path, user, group);
if (fs.exists(path)) {
if (!fs.delete(path, true)) {
throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FS005",
"delete, path [{0}] could not delete path", path);
}
}
}
catch (Exception ex) {
throw convertException(ex);
}
}
/**
* Move source to target
*
* @param context
* @param source
* @param target
* @param recovery
* @throws ActionExecutorException
*/
public void move(Context context, Path source, Path target, boolean recovery) throws ActionExecutorException {
try {
validatePath(source, true);
validateSameNN(source, target);
FileSystem fs = getFileSystemFor(source, context);
if (!fs.exists(source) && !recovery) {
throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FS006",
"move, source path [{0}] does not exist", source);
}
if (!fs.rename(source, target) && !recovery) {
throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FS008",
"move, could not move [{0}] to [{1}]", source, target);
}
}
catch (Exception ex) {
throw convertException(ex);
}
}
void chmod(Context context, Path path, String permissions, boolean dirFiles) throws ActionExecutorException {
try {
validatePath(path, true);
FileSystem fs = getFileSystemFor(path, context);
if (!fs.exists(path)) {
throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FS009",
"chmod, path [{0}] does not exist", path);
}
FileStatus pathStatus = fs.getFileStatus(path);
Path[] paths;
if (dirFiles && pathStatus.isDir()) {
FileStatus[] filesStatus = fs.listStatus(path);
paths = new Path[filesStatus.length];
for (int i = 0; i < filesStatus.length; i++) {
paths[i] = filesStatus[i].getPath();
}
}
else {
paths = new Path[]{path};
}
FsPermission newFsPermission = createShortPermission(permissions, path);
fs.setPermission(path, newFsPermission);
for (Path p : paths) {
fs.setPermission(p, newFsPermission);
}
}
catch (Exception ex) {
throw convertException(ex);
}
}
FsPermission createShortPermission(String permissions, Path path) throws ActionExecutorException {
if (permissions.length() == 3) {
char user = permissions.charAt(0);
char group = permissions.charAt(1);
char other = permissions.charAt(2);
int useri = user - '0';
int groupi = group - '0';
int otheri = other - '0';
int mask = useri * 100 + groupi * 10 + otheri;
short omask = Short.parseShort(Integer.toString(mask), 8);
return new FsPermission(omask);
}
else {
if (permissions.length() == 10) {
return FsPermission.valueOf(permissions);
}
else {
throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FS010",
"chmod, path [{0}] invalid permissions mask [{1}]", path, permissions);
}
}
}
@Override
public void check(Context context, WorkflowAction action) throws ActionExecutorException {
}
@Override
public void kill(Context context, WorkflowAction action) throws ActionExecutorException {
}
@Override
public void start(Context context, WorkflowAction action) throws ActionExecutorException {
try {
context.setStartData("-", "-", "-");
Element actionXml = XmlUtils.parseXml(action.getConf());
doOperations(context, actionXml);
context.setExecutionData("OK", null);
}
catch (Exception ex) {
throw convertException(ex);
}
}
@Override
public void end(Context context, WorkflowAction action) throws ActionExecutorException {
String externalStatus = action.getExternalStatus();
WorkflowAction.Status status = externalStatus.equals("OK") ? WorkflowAction.Status.OK :
WorkflowAction.Status.ERROR;
context.setEndData(status, getActionSignal(status));
if (!context.getProtoActionConf().getBoolean("oozie.action.keep.action.dir", false)) {
try {
FileSystem fs = context.getAppFileSystem();
fs.delete(context.getActionDir(), true);
}
catch (Exception ex) {
throw convertException(ex);
}
}
}
@Override
public boolean isCompleted(String externalStatus) {
return true;
}
/**
* @param context
* @return
* @throws HadoopAccessorException
* @throws IOException
* @throws URISyntaxException
*/
public Path getRecoveryPath(Context context) throws HadoopAccessorException, IOException, URISyntaxException {
return new Path(context.getActionDir(), "fs-" + context.getRecoveryId());
}
}