1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819package org.apache.giraph;
2021import org.apache.giraph.conf.GiraphConfiguration;
22import org.apache.giraph.conf.GiraphConstants;
23import org.apache.giraph.examples.GeneratedVertexReader;
24import org.apache.giraph.examples.SimpleMutateGraphComputation;
25import org.apache.giraph.examples.SimplePageRankComputation.SimplePageRankVertexInputFormat;
26import org.apache.giraph.examples.SimplePageRankComputation.SimplePageRankVertexOutputFormat;
27import org.apache.giraph.graph.DefaultVertexResolver;
28import org.apache.giraph.graph.Vertex;
29import org.apache.giraph.graph.VertexChanges;
30import org.apache.giraph.job.GiraphJob;
31import org.apache.hadoop.io.Writable;
32import org.apache.hadoop.io.WritableComparable;
33import org.junit.Test;
3435import java.io.IOException;
3637importstatic org.junit.Assert.assertTrue;
3839/**40 * Unit test for graph mutation41 */42publicclassTestMutateGraphextendsBspCase {
43publicTestMutateGraph() {
44super(TestMutateGraph.class.getName());
45 }
46/**47 * Custom vertex resolver48 */49publicstaticclass TestVertexResolver<I extends WritableComparable, V
50extends Writable, E extends Writable>
51extends DefaultVertexResolver {
52 @Override
53public Vertex resolve(WritableComparable vertexId, Vertex vertex,
54 VertexChanges vertexChanges, boolean hasMessages) {
55 Vertex originalVertex = vertex;
56// 1. If the vertex exists, first prune the edges57 removeEdges(vertex, vertexChanges);
5859// 2. If vertex removal desired, remove the vertex.60 vertex = removeVertexIfDesired(vertex, vertexChanges);
6162// If vertex removal happens do not add it back even if it has messages.63if (originalVertex != null && vertex == null) {
64 hasMessages = false;
65 }
6667// 3. If creation of vertex desired, pick first vertex68// 4. If vertex doesn't exist, but got messages or added edges, create69 vertex = addVertexIfDesired(vertexId, vertex, vertexChanges, hasMessages);
7071// 5. If edge addition, add the edges72 addEdges(vertex, vertexChanges);
7374return vertex;
75 }
76 }
7778/**79 * Run a job that tests the various graph mutations that can occur80 *81 * @throws IOException82 * @throws ClassNotFoundException83 * @throws InterruptedException84 */85 @Test
86publicvoid testMutateGraph()
87throws IOException, InterruptedException, ClassNotFoundException {
88 GiraphConfiguration conf = new GiraphConfiguration();
89 conf.setComputationClass(SimpleMutateGraphComputation.class);
90 conf.setVertexInputFormatClass(SimplePageRankVertexInputFormat.class);
91 conf.setVertexOutputFormatClass(SimplePageRankVertexOutputFormat.class);
92 conf.setWorkerContextClass(
93 SimpleMutateGraphComputation.SimpleMutateGraphVertexWorkerContext.class);
94 GiraphConstants.USER_PARTITION_COUNT.set(conf, 32);
95 conf.setNumComputeThreads(8);
96 GiraphConstants.VERTEX_RESOLVER_CLASS.set(conf, TestVertexResolver.class);
97 GiraphJob job = prepareJob(getCallingMethodName(), conf,
98 getTempPath(getCallingMethodName()));
99// Overwrite the number of vertices set in BspCase100 GeneratedVertexReader.READER_VERTICES.set(conf, 400);
101 assertTrue(job.run(true));
102 }
103 }