1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.giraph.jython.factories; |
19 | |
|
20 | |
import org.apache.giraph.conf.GiraphConfiguration; |
21 | |
import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration; |
22 | |
import org.apache.giraph.factories.ComputationFactory; |
23 | |
import org.apache.giraph.graph.Computation; |
24 | |
import org.apache.giraph.jython.JythonComputation; |
25 | |
import org.apache.giraph.jython.JythonGiraphComputation; |
26 | |
import org.apache.giraph.jython.JythonOptions; |
27 | |
import org.apache.giraph.jython.JythonUtils; |
28 | |
import org.apache.giraph.scripting.ScriptLoader; |
29 | |
import org.apache.log4j.Logger; |
30 | |
import org.python.core.PyObject; |
31 | |
import org.python.util.PythonInterpreter; |
32 | |
|
33 | |
import static com.google.common.base.Preconditions.checkNotNull; |
34 | |
import static org.apache.giraph.scripting.ScriptLoader.SCRIPTS_TO_LOAD; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class JythonComputationFactory implements ComputationFactory { |
40 | |
|
41 | |
private static volatile PyObject JYTHON_COMPUTATION_MODULE; |
42 | |
|
43 | |
|
44 | 0 | private static final Logger LOG = Logger.getLogger(JythonUtils.class); |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
private static void setPythonComputationModule(PyObject mod) { |
52 | 0 | JYTHON_COMPUTATION_MODULE = mod; |
53 | 0 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
private static PyObject getPythonComputationModule() { |
61 | 0 | return JYTHON_COMPUTATION_MODULE; |
62 | |
} |
63 | |
|
64 | |
@Override |
65 | |
public void initialize(ImmutableClassesGiraphConfiguration conf) { |
66 | 0 | PythonInterpreter interpreter = JythonUtils.getInterpreter(); |
67 | 0 | String className = computationName(conf); |
68 | 0 | PyObject pyComputationModule = interpreter.get(className); |
69 | 0 | checkNotNull(pyComputationModule, |
70 | |
"Could not find Jython Computation class " + className + |
71 | 0 | " in loaded scripts: " + ScriptLoader.getLoadedScripts()); |
72 | 0 | setPythonComputationModule(pyComputationModule); |
73 | 0 | } |
74 | |
|
75 | |
@Override |
76 | |
public Computation createComputation( |
77 | |
ImmutableClassesGiraphConfiguration conf) { |
78 | 0 | checkNotNull(JYTHON_COMPUTATION_MODULE, |
79 | |
"Jython Computation class not set in loaded scripts: " + |
80 | 0 | ScriptLoader.getLoadedScripts()); |
81 | |
|
82 | 0 | PyObject pyComputationObj = JYTHON_COMPUTATION_MODULE.__call__(); |
83 | 0 | Object computeObj = pyComputationObj.__tojava__(JythonComputation.class); |
84 | 0 | if (!(computeObj instanceof JythonComputation)) { |
85 | 0 | throw new IllegalStateException("getComputation: Jython object " + |
86 | 0 | computationName(conf) + " is not a JythonComputation"); |
87 | |
} |
88 | 0 | JythonComputation jythonCompute = (JythonComputation) computeObj; |
89 | 0 | conf.configureIfPossible(jythonCompute); |
90 | |
|
91 | 0 | JythonGiraphComputation giraphCompute = |
92 | |
new JythonGiraphComputation(jythonCompute); |
93 | 0 | giraphCompute.setConf(conf); |
94 | |
|
95 | 0 | jythonCompute.setGiraphCompute(giraphCompute); |
96 | |
|
97 | 0 | return giraphCompute; |
98 | |
} |
99 | |
|
100 | |
@Override |
101 | |
public void checkConfiguration(ImmutableClassesGiraphConfiguration conf) { |
102 | 0 | if (SCRIPTS_TO_LOAD.isDefaultValue(conf)) { |
103 | 0 | throw new IllegalStateException("checkConfiguration: " + |
104 | 0 | SCRIPTS_TO_LOAD.getKey() + " not set in configuration"); |
105 | |
} |
106 | 0 | if (!JythonOptions.JYTHON_COMPUTATION_CLASS_NAME.contains(conf)) { |
107 | 0 | throw new IllegalStateException("checkConfiguration: " + |
108 | 0 | JythonOptions.JYTHON_COMPUTATION_CLASS_NAME.getKey() + |
109 | |
" not set in configuration"); |
110 | |
} |
111 | 0 | } |
112 | |
|
113 | |
@Override |
114 | |
public String computationName(GiraphConfiguration conf) { |
115 | 0 | return JythonOptions.JYTHON_COMPUTATION_CLASS_NAME.get(conf); |
116 | |
} |
117 | |
} |