1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.utils; |
20 | |
|
21 | |
import com.google.common.base.Charsets; |
22 | |
import com.google.common.io.Files; |
23 | |
import org.apache.giraph.graph.GiraphJob; |
24 | |
import org.apache.hadoop.conf.Configuration; |
25 | |
import org.apache.hadoop.fs.Path; |
26 | |
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; |
27 | |
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; |
28 | |
import org.apache.zookeeper.server.ServerConfig; |
29 | |
import org.apache.zookeeper.server.ZooKeeperServerMain; |
30 | |
import org.apache.zookeeper.server.quorum.QuorumPeerConfig; |
31 | |
|
32 | |
import java.io.File; |
33 | |
import java.io.IOException; |
34 | |
import java.util.Map; |
35 | |
import java.util.Properties; |
36 | |
import java.util.concurrent.ExecutorService; |
37 | |
import java.util.concurrent.Executors; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public class InternalVertexRunner { |
50 | |
|
51 | |
public static final int LOCAL_ZOOKEEPER_PORT = 22182; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | private InternalVertexRunner() { } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public static Iterable<String> run(Class<?> vertexClass, |
72 | |
Class<?> vertexInputFormatClass, Class<?> vertexOutputFormatClass, |
73 | |
Map<String, String> params, String... data) throws Exception { |
74 | 1 | return run(vertexClass, null, vertexInputFormatClass, |
75 | |
vertexOutputFormatClass, params, data); |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public static Iterable<String> run(Class<?> vertexClass, |
93 | |
Class<?> vertexCombinerClass, Class<?> vertexInputFormatClass, |
94 | |
Class<?> vertexOutputFormatClass, Map<String, String> params, |
95 | |
String... data) throws Exception { |
96 | |
|
97 | 3 | File tmpDir = null; |
98 | |
try { |
99 | |
|
100 | 3 | tmpDir = FileUtils.createTestDir(vertexClass); |
101 | 3 | File inputFile = FileUtils.createTempFile(tmpDir, "graph.txt"); |
102 | 3 | File outputDir = FileUtils.createTempDir(tmpDir, "output"); |
103 | 3 | File zkDir = FileUtils.createTempDir(tmpDir, "_bspZooKeeper"); |
104 | 3 | File zkMgrDir = FileUtils.createTempDir(tmpDir, "_defaultZkManagerDir"); |
105 | 3 | File checkpointsDir = FileUtils.createTempDir(tmpDir, "_checkpoints"); |
106 | |
|
107 | |
|
108 | 3 | FileUtils.writeLines(inputFile, data); |
109 | |
|
110 | |
|
111 | 3 | GiraphJob job = new GiraphJob(vertexClass.getName()); |
112 | 3 | job.setVertexClass(vertexClass); |
113 | 3 | job.setVertexInputFormatClass(vertexInputFormatClass); |
114 | 3 | job.setVertexOutputFormatClass(vertexOutputFormatClass); |
115 | |
|
116 | 3 | if (vertexCombinerClass != null) { |
117 | 2 | job.setVertexCombinerClass(vertexCombinerClass); |
118 | |
} |
119 | |
|
120 | 3 | job.setWorkerConfiguration(1, 1, 100.0f); |
121 | 3 | Configuration conf = job.getConfiguration(); |
122 | 3 | conf.setBoolean(GiraphJob.SPLIT_MASTER_WORKER, false); |
123 | 3 | conf.setBoolean(GiraphJob.LOCAL_TEST_MODE, true); |
124 | 3 | conf.set(GiraphJob.ZOOKEEPER_LIST, "localhost:" + |
125 | |
String.valueOf(LOCAL_ZOOKEEPER_PORT)); |
126 | |
|
127 | 3 | conf.set(GiraphJob.ZOOKEEPER_DIR, zkDir.toString()); |
128 | 3 | conf.set(GiraphJob.ZOOKEEPER_MANAGER_DIRECTORY, |
129 | |
zkMgrDir.toString()); |
130 | 3 | conf.set(GiraphJob.CHECKPOINT_DIRECTORY, checkpointsDir.toString()); |
131 | |
|
132 | 3 | for (Map.Entry<String, String> param : params.entrySet()) { |
133 | 2 | conf.set(param.getKey(), param.getValue()); |
134 | |
} |
135 | |
|
136 | 3 | FileInputFormat.addInputPath(job.getInternalJob(), |
137 | |
new Path(inputFile.toString())); |
138 | 3 | FileOutputFormat.setOutputPath(job.getInternalJob(), |
139 | |
new Path(outputDir.toString())); |
140 | |
|
141 | |
|
142 | 3 | Properties zkProperties = new Properties(); |
143 | 3 | zkProperties.setProperty("tickTime", "2000"); |
144 | 3 | zkProperties.setProperty("dataDir", zkDir.getAbsolutePath()); |
145 | 3 | zkProperties.setProperty("clientPort", |
146 | |
String.valueOf(LOCAL_ZOOKEEPER_PORT)); |
147 | 3 | zkProperties.setProperty("maxClientCnxns", "10000"); |
148 | 3 | zkProperties.setProperty("minSessionTimeout", "10000"); |
149 | 3 | zkProperties.setProperty("maxSessionTimeout", "100000"); |
150 | 3 | zkProperties.setProperty("initLimit", "10"); |
151 | 3 | zkProperties.setProperty("syncLimit", "5"); |
152 | 3 | zkProperties.setProperty("snapCount", "50000"); |
153 | |
|
154 | 3 | QuorumPeerConfig qpConfig = new QuorumPeerConfig(); |
155 | 3 | qpConfig.parseProperties(zkProperties); |
156 | |
|
157 | |
|
158 | 3 | final InternalZooKeeper zookeeper = new InternalZooKeeper(); |
159 | 3 | final ServerConfig zkConfig = new ServerConfig(); |
160 | 3 | zkConfig.readFrom(qpConfig); |
161 | |
|
162 | 3 | ExecutorService executorService = Executors.newSingleThreadExecutor(); |
163 | 3 | executorService.execute(new Runnable() { |
164 | |
@Override |
165 | |
public void run() { |
166 | |
try { |
167 | 3 | zookeeper.runFromConfig(zkConfig); |
168 | 0 | } catch (IOException e) { |
169 | 0 | throw new RuntimeException(e); |
170 | 3 | } |
171 | 3 | } |
172 | |
}); |
173 | |
try { |
174 | 3 | job.run(true); |
175 | |
} finally { |
176 | 3 | executorService.shutdown(); |
177 | 3 | zookeeper.end(); |
178 | 3 | } |
179 | |
|
180 | 3 | return Files.readLines(new File(outputDir, "part-m-00000"), |
181 | |
Charsets.UTF_8); |
182 | |
} finally { |
183 | 3 | FileUtils.delete(tmpDir); |
184 | |
} |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | 6 | private static class InternalZooKeeper extends ZooKeeperServerMain { |
193 | |
|
194 | |
|
195 | |
|
196 | |
void end() { |
197 | 3 | shutdown(); |
198 | 3 | } |
199 | |
} |
200 | |
} |