Browse Source

Merge "Add main function wrapper for edp.java.adapt_for_oozie config"

Jenkins 4 years ago
parent
commit
13658d970b

+ 1
- 0
README.rst View File

@@ -6,4 +6,5 @@ Sahara-extra is place for Sahara components not included into the main `Sahara r
6 6
 Here is the list of components:
7 7
 
8 8
 * Sources for Swift filesystem implementation for Hadoop: https://github.com/openstack/sahara-extra/blob/master/hadoop-swiftfs/README.rst
9
+* Sources for main function wrapper that adapt for oozie: https://github.com/openstack/sahara-extra/blob/master/edp-adapt-for-oozie/README.rst
9 10
 * `Diskimage-builder <https://github.com/openstack/diskimage-builder>`_ elements moved to the new repo: https://github.com/openstack/sahara-image-elements

+ 24
- 0
edp-adapt-for-oozie/README.rst View File

@@ -0,0 +1,24 @@
1
+=======================
2
+Sources for main function wrapper that adapt for oozie
3
+=======================
4
+
5
+In order to pass configurations to MapReduce Application through oozie,
6
+it is necessary to add the following code.
7
+(https://github.com/openstack/sahara/blob/master/etc/edp-examples/edp-java/README.rst)
8
+
9
+    // This will add properties from the <configuration> tag specified
10
+    // in the Oozie workflow.  For java actions, Oozie writes the
11
+    // configuration values to a file pointed to by ooze.action.conf.xml
12
+    conf.addResource(new Path("file:///",
13
+                              System.getProperty("oozie.action.conf.xml")));
14
+
15
+This wrapper adds a above configuration file to a default resources and
16
+invoke actual main function.
17
+
18
+And this wrapper provides workaround for oozie's System.exit problem.
19
+(https://oozie.apache.org/docs/4.0.0/WorkflowFunctionalSpec.html#a3.2.7_Java_Action)
20
+In caller of oozie, System.exit is converted to exception.
21
+The application can call System.exit multiple times.
22
+
23
+This wrapper stores the argument of System.exit called in first.
24
+And return stored value if System.exit is called multiple times.

+ 55
- 0
edp-adapt-for-oozie/pom.xml View File

@@ -0,0 +1,55 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!--
3
+  Licensed under the Apache License, Version 2.0 (the "License");
4
+  you may not use this file except in compliance with the License.
5
+  You may obtain a copy of the License at
6
+
7
+    http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+  Unless required by applicable law or agreed to in writing, software
10
+  distributed under the License is distributed on an "AS IS" BASIS,
11
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+  See the License for the specific language governing permissions and
13
+  limitations under the License. See accompanying LICENSE file.
14
+-->
15
+<project xmlns="http://maven.apache.org/POM/4.0.0"
16
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
17
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
18
+  <modelVersion>4.0.0</modelVersion>
19
+
20
+    <groupId>org.openstack.sahara.edp</groupId>
21
+    <artifactId>edp-main-wrapper</artifactId>
22
+    <version>1.0.0-SNAPSHOT</version>
23
+    <name>EDP Java Action Main Wrapper for oozie</name>
24
+    <packaging>jar</packaging>
25
+
26
+  <properties>
27
+    <file.encoding>UTF-8</file.encoding>
28
+    <downloadSources>true</downloadSources>
29
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
30
+  </properties>
31
+
32
+  <build>
33
+    <plugins>
34
+      <plugin>
35
+        <groupId>org.apache.maven.plugins</groupId>
36
+        <artifactId>maven-compiler-plugin</artifactId>
37
+        <configuration>
38
+          <source>1.6</source>
39
+          <target>1.6</target>
40
+        </configuration>
41
+      </plugin>
42
+      <plugin>
43
+        <groupId>org.apache.maven.plugins</groupId>
44
+        <artifactId>maven-checkstyle-plugin</artifactId>
45
+        <configuration>
46
+          <configLocation>file://${basedir}/../hadoop-swiftfs/checkstyle.xml</configLocation>
47
+          <failOnViolation>false</failOnViolation>
48
+          <format>xml</format>
49
+          <format>html</format>
50
+        </configuration>
51
+      </plugin>
52
+    </plugins>
53
+  </build>
54
+
55
+</project>

+ 91
- 0
edp-adapt-for-oozie/src/main/java/org/openstack/sahara/edp/MainWrapper.java View File

@@ -0,0 +1,91 @@
1
+package org.openstack.sahara.edp;
2
+
3
+import java.lang.reflect.InvocationTargetException;
4
+import java.lang.reflect.Method;
5
+import java.security.Permission;
6
+import java.util.Arrays;
7
+
8
+public class MainWrapper {
9
+
10
+  public static void main(String[] args) throws Throwable {
11
+
12
+    // Load oozie configuration file
13
+    String actionConf = System.getProperty("oozie.action.conf.xml");
14
+    if (actionConf != null) {
15
+      Class<?> configClass
16
+        = Class.forName("org.apache.hadoop.conf.Configuration");
17
+      Method method = configClass.getMethod("addDefaultResource", String.class);
18
+      method.invoke(null, "action.xml");
19
+    }
20
+
21
+    SecurityManager originalSecurityManager = System.getSecurityManager();
22
+    WrapperSecurityManager newSecurityManager
23
+      = new WrapperSecurityManager(originalSecurityManager);
24
+    System.setSecurityManager(newSecurityManager);
25
+
26
+    Class<?> mainClass = Class.forName(args[0]);
27
+    Method mainMethod = mainClass.getMethod("main", String[].class);
28
+    String[] newArgs = Arrays.copyOfRange(args, 1, args.length);
29
+    Throwable exception = null;
30
+    try {
31
+      mainMethod.invoke(null, (Object) newArgs);
32
+    } catch (InvocationTargetException e) {
33
+      if (!newSecurityManager.getExitInvoked()) {
34
+        exception = e.getTargetException();
35
+      }
36
+    }
37
+
38
+    System.setSecurityManager(originalSecurityManager);
39
+
40
+    if (exception != null) {
41
+      throw exception;
42
+    }
43
+    if (newSecurityManager.getExitInvoked()) {
44
+      System.exit(newSecurityManager.getExitCode());
45
+    }
46
+  }
47
+
48
+  static class WrapperSecurityManager extends SecurityManager {
49
+    private static boolean exitInvoked = false;
50
+    private static int firstExitCode;
51
+    private SecurityManager securityManager;
52
+
53
+    public WrapperSecurityManager(SecurityManager securityManager) {
54
+      this.securityManager = securityManager;
55
+    }
56
+
57
+    @Override
58
+    public void checkPermission(Permission perm, Object context) {
59
+      if (securityManager != null) {
60
+        // check everything with the original SecurityManager
61
+        securityManager.checkPermission(perm, context);
62
+      }
63
+    }
64
+
65
+    @Override
66
+    public void checkPermission(Permission perm) {
67
+      if (securityManager != null) {
68
+        // check everything with the original SecurityManager
69
+        securityManager.checkPermission(perm);
70
+      }
71
+    }
72
+
73
+    @Override
74
+    public void checkExit(int status) throws SecurityException {
75
+      if (!exitInvoked) {
76
+        // save first System.exit status code
77
+        exitInvoked = true;
78
+        firstExitCode = status;
79
+      }
80
+      throw new SecurityException("Intercepted System.exit(" + status + ")");
81
+    }
82
+
83
+    public static boolean getExitInvoked() {
84
+      return exitInvoked;
85
+    }
86
+
87
+    public static int getExitCode() {
88
+      return firstExitCode;
89
+    }
90
+  }
91
+}

Loading…
Cancel
Save